mirror of https://github.com/ethereum/go-ethereum
core/state: move state log mechanism to a separate layer (#30569)
This PR moves the logging/tracing-facilities out of `*state.StateDB`, in to a wrapping struct which implements `vm.StateDB` instead. In most places, it is a pretty straight-forward change: - First, hoisting the invocations from state objects up to the statedb. - Then making the mutation-methods simply return the previous value, so that the external logging layer could log everything. Some internal code uses the direct object-accessors to mutate the state, particularly in testing and in setting up state overrides, which means that these changes are unobservable for the hooked layer. Thus, configuring the overrides are not necessarily part of the API we want to publish. The trickiest part about the layering is that when the selfdestructs are finally deleted during `Finalise`, there's the possibility that someone sent some ether to it, which is burnt at that point, and thus needs to be logged. The hooked layer reaches into the inner layer to figure out these events. In package `vm`, the conversion from `state.StateDB + hooks` into a hooked `vm.StateDB` is performed where needed. --------- Co-authored-by: Gary Rong <garyrong0905@gmail.com>pull/30650/head
parent
a5fe7353cf
commit
459bb4a647
@ -0,0 +1,242 @@ |
||||
// Copyright 2024 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package state |
||||
|
||||
import ( |
||||
"math/big" |
||||
|
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/core/stateless" |
||||
"github.com/ethereum/go-ethereum/core/tracing" |
||||
"github.com/ethereum/go-ethereum/core/types" |
||||
"github.com/ethereum/go-ethereum/crypto" |
||||
"github.com/ethereum/go-ethereum/params" |
||||
"github.com/ethereum/go-ethereum/trie/utils" |
||||
"github.com/holiman/uint256" |
||||
) |
||||
|
||||
// hookedStateDB represents a statedb which emits calls to tracing-hooks
|
||||
// on state operations.
|
||||
type hookedStateDB struct { |
||||
inner *StateDB |
||||
hooks *tracing.Hooks |
||||
} |
||||
|
||||
// NewHookedState wraps the given stateDb with the given hooks
|
||||
func NewHookedState(stateDb *StateDB, hooks *tracing.Hooks) *hookedStateDB { |
||||
s := &hookedStateDB{stateDb, hooks} |
||||
if s.hooks == nil { |
||||
s.hooks = new(tracing.Hooks) |
||||
} |
||||
return s |
||||
} |
||||
|
||||
func (s *hookedStateDB) CreateAccount(addr common.Address) { |
||||
s.inner.CreateAccount(addr) |
||||
} |
||||
|
||||
func (s *hookedStateDB) CreateContract(addr common.Address) { |
||||
s.inner.CreateContract(addr) |
||||
} |
||||
|
||||
func (s *hookedStateDB) GetBalance(addr common.Address) *uint256.Int { |
||||
return s.inner.GetBalance(addr) |
||||
} |
||||
|
||||
func (s *hookedStateDB) GetNonce(addr common.Address) uint64 { |
||||
return s.inner.GetNonce(addr) |
||||
} |
||||
|
||||
func (s *hookedStateDB) GetCodeHash(addr common.Address) common.Hash { |
||||
return s.inner.GetCodeHash(addr) |
||||
} |
||||
|
||||
func (s *hookedStateDB) GetCode(addr common.Address) []byte { |
||||
return s.inner.GetCode(addr) |
||||
} |
||||
|
||||
func (s *hookedStateDB) GetCodeSize(addr common.Address) int { |
||||
return s.inner.GetCodeSize(addr) |
||||
} |
||||
|
||||
func (s *hookedStateDB) AddRefund(u uint64) { |
||||
s.inner.AddRefund(u) |
||||
} |
||||
|
||||
func (s *hookedStateDB) SubRefund(u uint64) { |
||||
s.inner.SubRefund(u) |
||||
} |
||||
|
||||
func (s *hookedStateDB) GetRefund() uint64 { |
||||
return s.inner.GetRefund() |
||||
} |
||||
|
||||
func (s *hookedStateDB) GetCommittedState(addr common.Address, hash common.Hash) common.Hash { |
||||
return s.inner.GetCommittedState(addr, hash) |
||||
} |
||||
|
||||
func (s *hookedStateDB) GetState(addr common.Address, hash common.Hash) common.Hash { |
||||
return s.inner.GetState(addr, hash) |
||||
} |
||||
|
||||
func (s *hookedStateDB) GetStorageRoot(addr common.Address) common.Hash { |
||||
return s.inner.GetStorageRoot(addr) |
||||
} |
||||
|
||||
func (s *hookedStateDB) GetTransientState(addr common.Address, key common.Hash) common.Hash { |
||||
return s.inner.GetTransientState(addr, key) |
||||
} |
||||
|
||||
func (s *hookedStateDB) SetTransientState(addr common.Address, key, value common.Hash) { |
||||
s.inner.SetTransientState(addr, key, value) |
||||
} |
||||
|
||||
func (s *hookedStateDB) HasSelfDestructed(addr common.Address) bool { |
||||
return s.inner.HasSelfDestructed(addr) |
||||
} |
||||
|
||||
func (s *hookedStateDB) Exist(addr common.Address) bool { |
||||
return s.inner.Exist(addr) |
||||
} |
||||
|
||||
func (s *hookedStateDB) Empty(addr common.Address) bool { |
||||
return s.inner.Empty(addr) |
||||
} |
||||
|
||||
func (s *hookedStateDB) AddressInAccessList(addr common.Address) bool { |
||||
return s.inner.AddressInAccessList(addr) |
||||
} |
||||
|
||||
func (s *hookedStateDB) SlotInAccessList(addr common.Address, slot common.Hash) (addressOk bool, slotOk bool) { |
||||
return s.inner.SlotInAccessList(addr, slot) |
||||
} |
||||
|
||||
func (s *hookedStateDB) AddAddressToAccessList(addr common.Address) { |
||||
s.inner.AddAddressToAccessList(addr) |
||||
} |
||||
|
||||
func (s *hookedStateDB) AddSlotToAccessList(addr common.Address, slot common.Hash) { |
||||
s.inner.AddSlotToAccessList(addr, slot) |
||||
} |
||||
|
||||
func (s *hookedStateDB) PointCache() *utils.PointCache { |
||||
return s.inner.PointCache() |
||||
} |
||||
|
||||
func (s *hookedStateDB) Prepare(rules params.Rules, sender, coinbase common.Address, dest *common.Address, precompiles []common.Address, txAccesses types.AccessList) { |
||||
s.inner.Prepare(rules, sender, coinbase, dest, precompiles, txAccesses) |
||||
} |
||||
|
||||
func (s *hookedStateDB) RevertToSnapshot(i int) { |
||||
s.inner.RevertToSnapshot(i) |
||||
} |
||||
|
||||
func (s *hookedStateDB) Snapshot() int { |
||||
return s.inner.Snapshot() |
||||
} |
||||
|
||||
func (s *hookedStateDB) AddPreimage(hash common.Hash, bytes []byte) { |
||||
s.inner.Snapshot() |
||||
} |
||||
|
||||
func (s *hookedStateDB) Witness() *stateless.Witness { |
||||
return s.inner.Witness() |
||||
} |
||||
|
||||
func (s *hookedStateDB) SubBalance(addr common.Address, amount *uint256.Int, reason tracing.BalanceChangeReason) uint256.Int { |
||||
prev := s.inner.SubBalance(addr, amount, reason) |
||||
if s.hooks.OnBalanceChange != nil && !amount.IsZero() { |
||||
newBalance := new(uint256.Int).Sub(&prev, amount) |
||||
s.hooks.OnBalanceChange(addr, prev.ToBig(), newBalance.ToBig(), reason) |
||||
} |
||||
return prev |
||||
} |
||||
|
||||
func (s *hookedStateDB) AddBalance(addr common.Address, amount *uint256.Int, reason tracing.BalanceChangeReason) uint256.Int { |
||||
prev := s.inner.AddBalance(addr, amount, reason) |
||||
if s.hooks.OnBalanceChange != nil && !amount.IsZero() { |
||||
newBalance := new(uint256.Int).Add(&prev, amount) |
||||
s.hooks.OnBalanceChange(addr, prev.ToBig(), newBalance.ToBig(), reason) |
||||
} |
||||
return prev |
||||
} |
||||
|
||||
func (s *hookedStateDB) SetNonce(address common.Address, nonce uint64) { |
||||
s.inner.SetNonce(address, nonce) |
||||
if s.hooks.OnNonceChange != nil { |
||||
s.hooks.OnNonceChange(address, nonce-1, nonce) |
||||
} |
||||
} |
||||
|
||||
func (s *hookedStateDB) SetCode(address common.Address, code []byte) { |
||||
s.inner.SetCode(address, code) |
||||
if s.hooks.OnCodeChange != nil { |
||||
s.hooks.OnCodeChange(address, types.EmptyCodeHash, nil, crypto.Keccak256Hash(code), code) |
||||
} |
||||
} |
||||
|
||||
func (s *hookedStateDB) SetState(address common.Address, key common.Hash, value common.Hash) common.Hash { |
||||
prev := s.inner.SetState(address, key, value) |
||||
if s.hooks.OnStorageChange != nil && prev != value { |
||||
s.hooks.OnStorageChange(address, key, prev, value) |
||||
} |
||||
return prev |
||||
} |
||||
|
||||
func (s *hookedStateDB) SelfDestruct(address common.Address) uint256.Int { |
||||
prev := s.inner.SelfDestruct(address) |
||||
if !prev.IsZero() { |
||||
if s.hooks.OnBalanceChange != nil { |
||||
s.hooks.OnBalanceChange(address, prev.ToBig(), new(big.Int), tracing.BalanceDecreaseSelfdestruct) |
||||
} |
||||
} |
||||
return prev |
||||
} |
||||
|
||||
func (s *hookedStateDB) SelfDestruct6780(address common.Address) (uint256.Int, bool) { |
||||
prev, changed := s.inner.SelfDestruct6780(address) |
||||
if !prev.IsZero() && changed { |
||||
if s.hooks.OnBalanceChange != nil { |
||||
s.hooks.OnBalanceChange(address, prev.ToBig(), new(big.Int), tracing.BalanceDecreaseSelfdestruct) |
||||
} |
||||
} |
||||
return prev, changed |
||||
} |
||||
|
||||
func (s *hookedStateDB) AddLog(log *types.Log) { |
||||
// The inner will modify the log (add fields), so invoke that first
|
||||
s.inner.AddLog(log) |
||||
if s.hooks.OnLog != nil { |
||||
s.hooks.OnLog(log) |
||||
} |
||||
} |
||||
|
||||
func (s *hookedStateDB) Finalise(deleteEmptyObjects bool) { |
||||
defer s.inner.Finalise(deleteEmptyObjects) |
||||
if s.hooks.OnBalanceChange == nil { |
||||
return |
||||
} |
||||
for addr := range s.inner.journal.dirties { |
||||
obj := s.inner.stateObjects[addr] |
||||
if obj != nil && obj.selfDestructed { |
||||
// If ether was sent to account post-selfdestruct it is burnt.
|
||||
if bal := obj.Balance(); bal.Sign() != 0 { |
||||
s.hooks.OnBalanceChange(addr, bal.ToBig(), new(big.Int), tracing.BalanceDecreaseSelfdestructBurn) |
||||
} |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,130 @@ |
||||
// Copyright 2024 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package state |
||||
|
||||
import ( |
||||
"fmt" |
||||
"math/big" |
||||
"testing" |
||||
|
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/core/tracing" |
||||
"github.com/ethereum/go-ethereum/core/types" |
||||
"github.com/holiman/uint256" |
||||
) |
||||
|
||||
// This method tests that the 'burn' from sending-to-selfdestructed accounts
|
||||
// is accounted for.
|
||||
// (There is also a higher-level test in eth/tracers: TestSupplySelfDestruct )
|
||||
func TestBurn(t *testing.T) { |
||||
// Note: burn can happen even after EIP-6780, if within one single transaction,
|
||||
// the following occur:
|
||||
// 1. contract B creates contract A
|
||||
// 2. contract A is destructed
|
||||
// 3. constract B sends ether to A
|
||||
|
||||
var burned = new(uint256.Int) |
||||
s, _ := New(types.EmptyRootHash, NewDatabaseForTesting()) |
||||
hooked := NewHookedState(s, &tracing.Hooks{ |
||||
OnBalanceChange: func(addr common.Address, prev, new *big.Int, reason tracing.BalanceChangeReason) { |
||||
if reason == tracing.BalanceDecreaseSelfdestructBurn { |
||||
burned.Add(burned, uint256.MustFromBig(prev)) |
||||
} |
||||
}, |
||||
}) |
||||
createAndDestroy := func(addr common.Address) { |
||||
hooked.AddBalance(addr, uint256.NewInt(100), tracing.BalanceChangeUnspecified) |
||||
hooked.CreateContract(addr) |
||||
hooked.SelfDestruct(addr) |
||||
// sanity-check that balance is now 0
|
||||
if have, want := hooked.GetBalance(addr), new(uint256.Int); !have.Eq(want) { |
||||
t.Fatalf("post-destruct balance wrong: have %v want %v", have, want) |
||||
} |
||||
} |
||||
addA := common.Address{0xaa} |
||||
addB := common.Address{0xbb} |
||||
addC := common.Address{0xcc} |
||||
|
||||
// Tx 1: create and destroy address A and B in one tx
|
||||
createAndDestroy(addA) |
||||
createAndDestroy(addB) |
||||
hooked.AddBalance(addA, uint256.NewInt(200), tracing.BalanceChangeUnspecified) |
||||
hooked.AddBalance(addB, uint256.NewInt(200), tracing.BalanceChangeUnspecified) |
||||
hooked.Finalise(true) |
||||
|
||||
// Tx 2: create and destroy address C, then commit
|
||||
createAndDestroy(addC) |
||||
hooked.AddBalance(addC, uint256.NewInt(200), tracing.BalanceChangeUnspecified) |
||||
hooked.Finalise(true) |
||||
|
||||
s.Commit(0, false) |
||||
if have, want := burned, uint256.NewInt(600); !have.Eq(want) { |
||||
t.Fatalf("burn-count wrong, have %v want %v", have, want) |
||||
} |
||||
} |
||||
|
||||
// TestHooks is a basic sanity-check of all hooks
|
||||
func TestHooks(t *testing.T) { |
||||
inner, _ := New(types.EmptyRootHash, NewDatabaseForTesting()) |
||||
inner.SetTxContext(common.Hash{0x11}, 100) // For the log
|
||||
var result []string |
||||
var wants = []string{ |
||||
"0xaa00000000000000000000000000000000000000.balance: 0->100 (BalanceChangeUnspecified)", |
||||
"0xaa00000000000000000000000000000000000000.balance: 100->50 (BalanceChangeTransfer)", |
||||
"0xaa00000000000000000000000000000000000000.nonce: 1336->1337", |
||||
"0xaa00000000000000000000000000000000000000.code: (0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470) ->0x1325 (0xa12ae05590de0c93a00bc7ac773c2fdb621e44f814985e72194f921c0050f728)", |
||||
"0xaa00000000000000000000000000000000000000.storage slot 0x0000000000000000000000000000000000000000000000000000000000000001: 0x0000000000000000000000000000000000000000000000000000000000000000 ->0x0000000000000000000000000000000000000000000000000000000000000011", |
||||
"0xaa00000000000000000000000000000000000000.storage slot 0x0000000000000000000000000000000000000000000000000000000000000001: 0x0000000000000000000000000000000000000000000000000000000000000011 ->0x0000000000000000000000000000000000000000000000000000000000000022", |
||||
"log 100", |
||||
} |
||||
emitF := func(format string, a ...any) { |
||||
result = append(result, fmt.Sprintf(format, a...)) |
||||
} |
||||
sdb := NewHookedState(inner, &tracing.Hooks{ |
||||
OnBalanceChange: func(addr common.Address, prev, new *big.Int, reason tracing.BalanceChangeReason) { |
||||
emitF("%v.balance: %v->%v (%v)", addr, prev, new, reason) |
||||
}, |
||||
OnNonceChange: func(addr common.Address, prev, new uint64) { |
||||
emitF("%v.nonce: %v->%v", addr, prev, new) |
||||
}, |
||||
OnCodeChange: func(addr common.Address, prevCodeHash common.Hash, prevCode []byte, codeHash common.Hash, code []byte) { |
||||
emitF("%v.code: %#x (%v) ->%#x (%v)", addr, prevCode, prevCodeHash, code, codeHash) |
||||
}, |
||||
OnStorageChange: func(addr common.Address, slot common.Hash, prev, new common.Hash) { |
||||
emitF("%v.storage slot %v: %v ->%v", addr, slot, prev, new) |
||||
}, |
||||
OnLog: func(log *types.Log) { |
||||
emitF("log %v", log.TxIndex) |
||||
}, |
||||
}) |
||||
sdb.AddBalance(common.Address{0xaa}, uint256.NewInt(100), tracing.BalanceChangeUnspecified) |
||||
sdb.SubBalance(common.Address{0xaa}, uint256.NewInt(50), tracing.BalanceChangeTransfer) |
||||
sdb.SetNonce(common.Address{0xaa}, 1337) |
||||
sdb.SetCode(common.Address{0xaa}, []byte{0x13, 37}) |
||||
sdb.SetState(common.Address{0xaa}, common.HexToHash("0x01"), common.HexToHash("0x11")) |
||||
sdb.SetState(common.Address{0xaa}, common.HexToHash("0x01"), common.HexToHash("0x22")) |
||||
sdb.SetTransientState(common.Address{0xaa}, common.HexToHash("0x02"), common.HexToHash("0x01")) |
||||
sdb.SetTransientState(common.Address{0xaa}, common.HexToHash("0x02"), common.HexToHash("0x02")) |
||||
sdb.AddLog(&types.Log{ |
||||
Address: common.Address{0xbb}, |
||||
}) |
||||
for i, want := range wants { |
||||
if have := result[i]; have != want { |
||||
t.Fatalf("error event %d, have\n%v\nwant%v\n", i, have, want) |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue