From 7ea867c72711788688c89baf1a7aae44221088ca Mon Sep 17 00:00:00 2001 From: Matthieu Vachon Date: Fri, 15 Mar 2024 17:16:44 -0400 Subject: [PATCH] Reverted some changes that are not required anymore with the new tracing interface The new tracing interface avoids an import cycle we have before around `core`, `core/vm` and `core/state`. So I can revert some of the changes needed previously. --- core/state/statedb.go | 5 ++++- core/state/statedb_test.go | 20 ++++++++++---------- core/vm/interface.go | 5 +---- eth/gasestimator/gasestimator.go | 6 ++---- 4 files changed, 17 insertions(+), 19 deletions(-) diff --git a/core/state/statedb.go b/core/state/statedb.go index 6552f59b7d..37565f97b3 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -28,6 +28,7 @@ import ( "github.com/ethereum/go-ethereum/core/state/snapshot" "github.com/ethereum/go-ethereum/core/tracing" "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/metrics" @@ -701,7 +702,7 @@ func (s *StateDB) CreateAccount(addr common.Address) { // Copy creates a deep, independent copy of the state. // Snapshots of the copied state cannot be applied to the copy. -func (s *StateDB) Copy() interface{} { +func (s *StateDB) Copy() vm.StateDB { // Copy all the basic fields, initialize the memory ones state := &StateDB{ db: s.db, @@ -1409,6 +1410,8 @@ func (s *StateDB) convertAccountSet(set map[common.Address]*types.StateAccount) return ret } +func (s *StateDB) SetEVM(evm *vm.EVM) {} + // copySet returns a deep-copied set. func copySet[k comparable](set map[k][]byte) map[k][]byte { copied := make(map[k][]byte, len(set)) diff --git a/core/state/statedb_test.go b/core/state/statedb_test.go index 1f3a145c5d..213b32ca02 100644 --- a/core/state/statedb_test.go +++ b/core/state/statedb_test.go @@ -174,24 +174,24 @@ func TestCopy(t *testing.T) { orig.Finalise(false) // Copy the state - copy := orig.Copy().(*StateDB) + copy := orig.Copy() // Copy the copy state - ccopy := copy.Copy().(*StateDB) + ccopy := copy.Copy() // modify all in memory for i := byte(0); i < 255; i++ { origObj := orig.GetOrNewStateObject(common.BytesToAddress([]byte{i})) - copyObj := copy.GetOrNewStateObject(common.BytesToAddress([]byte{i})) - ccopyObj := ccopy.GetOrNewStateObject(common.BytesToAddress([]byte{i})) + copyObj := copy.(*StateDB).GetOrNewStateObject(common.BytesToAddress([]byte{i})) + ccopyObj := ccopy.(*StateDB).GetOrNewStateObject(common.BytesToAddress([]byte{i})) origObj.AddBalance(big.NewInt(2*int64(i)), tracing.BalanceChangeUnspecified) copyObj.AddBalance(big.NewInt(3*int64(i)), tracing.BalanceChangeUnspecified) ccopyObj.AddBalance(big.NewInt(4*int64(i)), tracing.BalanceChangeUnspecified) orig.updateStateObject(origObj) - copy.updateStateObject(copyObj) - ccopy.updateStateObject(copyObj) + copy.(*StateDB).updateStateObject(copyObj) + ccopy.(*StateDB).updateStateObject(copyObj) } // Finalise the changes on all concurrently @@ -203,15 +203,15 @@ func TestCopy(t *testing.T) { var wg sync.WaitGroup wg.Add(3) go finalise(&wg, orig) - go finalise(&wg, copy) - go finalise(&wg, ccopy) + go finalise(&wg, copy.(*StateDB)) + go finalise(&wg, ccopy.(*StateDB)) wg.Wait() // Verify that the three states have been updated independently for i := byte(0); i < 255; i++ { origObj := orig.GetOrNewStateObject(common.BytesToAddress([]byte{i})) - copyObj := copy.GetOrNewStateObject(common.BytesToAddress([]byte{i})) - ccopyObj := ccopy.GetOrNewStateObject(common.BytesToAddress([]byte{i})) + copyObj := copy.(*StateDB).GetOrNewStateObject(common.BytesToAddress([]byte{i})) + ccopyObj := ccopy.(*StateDB).GetOrNewStateObject(common.BytesToAddress([]byte{i})) if want := big.NewInt(3 * int64(i)); origObj.Balance().Cmp(want) != 0 { t.Errorf("orig obj %d: balance mismatch: have %v, want %v", i, origObj.Balance(), want) diff --git a/core/vm/interface.go b/core/vm/interface.go index c2a4fe7e96..8d6431310b 100644 --- a/core/vm/interface.go +++ b/core/vm/interface.go @@ -88,15 +88,12 @@ type StateDB interface { Finalise(deleteEmptyObjects bool) Commit(block uint64, deleteEmptyObjects bool) (common.Hash, error) SetTxContext(thash common.Hash, ti int) - Copy() interface{} + Copy() StateDB IntermediateRoot(deleteEmptyObjects bool) common.Hash GetLogs(hash common.Hash, blockNumber uint64, blockHash common.Hash) []*types.Log TxIndex() int Preimages() map[common.Hash][]byte SetLogger(logger *tracing.Hooks) -} - -type EVMAssignable interface { SetEVM(evm *EVM) } diff --git a/eth/gasestimator/gasestimator.go b/eth/gasestimator/gasestimator.go index d06afd032a..1c19930d81 100644 --- a/eth/gasestimator/gasestimator.go +++ b/eth/gasestimator/gasestimator.go @@ -209,13 +209,11 @@ func run(ctx context.Context, call *core.Message, opts *Options) (*core.Executio msgContext = core.NewEVMTxContext(call) evmContext = core.NewEVMBlockContext(opts.Header, opts.Chain, nil) - dirtyState = opts.State.Copy().(vm.StateDB) + dirtyState = opts.State.Copy() evm = vm.NewEVM(evmContext, msgContext, dirtyState, opts.Config, vm.Config{NoBaseFee: true}) ) + dirtyState.SetEVM(evm) - if assignable, ok := dirtyState.(vm.EVMAssignable); ok { - assignable.SetEVM(evm) - } // Monitor the outer context and interrupt the EVM upon cancellation. To avoid // a dangling goroutine until the outer estimation finishes, create an internal // context for the lifetime of this method call.