From ad41e29932252af28c69264cc62938f67f2f11d9 Mon Sep 17 00:00:00 2001 From: Daniel Liu <139250065@qq.com> Date: Fri, 29 Aug 2025 05:24:28 +0800 Subject: [PATCH] core/state: remove notion of fake storage #24916, fix XFN-150 (#1219) --- core/state/state_object.go | 32 -------------------------------- core/state/statedb.go | 4 ++-- internal/ethapi/api.go | 4 ++++ 3 files changed, 6 insertions(+), 34 deletions(-) diff --git a/core/state/state_object.go b/core/state/state_object.go index 31e017f991..0b14f28130 100644 --- a/core/state/state_object.go +++ b/core/state/state_object.go @@ -80,7 +80,6 @@ type stateObject struct { originStorage Storage // Storage cache of original entries to dedup rewrites, reset for every transaction pendingStorage Storage // Storage entries that need to be flushed to disk, at the end of an entire block dirtyStorage Storage // Storage entries that need to be flushed to disk - fakeStorage Storage // Fake storage which constructed by caller for debugging purpose. // Cache flags. dirtyCode bool // true if the code was updated @@ -186,10 +185,6 @@ func (s *stateObject) getTrie(db Database) Trie { // GetState retrieves a value from the account storage trie. func (s *stateObject) GetState(db Database, key common.Hash) common.Hash { - // If the fake storage is set, only lookup the state here(in the debugging mode) - if s.fakeStorage != nil { - return s.fakeStorage[key] - } // If we have a dirty value for this state entry, return it value, dirty := s.dirtyStorage[key] if dirty { @@ -200,10 +195,6 @@ func (s *stateObject) GetState(db Database, key common.Hash) common.Hash { } func (s *stateObject) GetCommittedState(db Database, key common.Hash) common.Hash { - // If the fake storage is set, only lookup the state here(in the debugging mode) - if s.fakeStorage != nil { - return s.fakeStorage[key] - } // If we have a pending write or clean cached, return that if value, pending := s.pendingStorage[key]; pending { return value @@ -233,11 +224,6 @@ func (s *stateObject) GetCommittedState(db Database, key common.Hash) common.Has // SetState updates a value in account storage. func (s *stateObject) SetState(db Database, key, value common.Hash) { - // If the fake storage is set, put the temporary state update here. - if s.fakeStorage != nil { - s.fakeStorage[key] = value - return - } // If the new value is the same as old, don't set prev := s.GetState(db, key) if prev == value { @@ -252,24 +238,6 @@ func (s *stateObject) SetState(db Database, key, value common.Hash) { s.setState(key, value) } -// SetStorage replaces the entire state storage with the given one. -// -// After this function is called, all original state will be ignored and state -// lookup only happens in the fake state storage. -// -// Note this function should only be used for debugging purpose. -func (s *stateObject) SetStorage(storage map[common.Hash]common.Hash) { - // Allocate fake storage if it's nil. - if s.fakeStorage == nil { - s.fakeStorage = make(Storage) - } - for key, value := range storage { - s.fakeStorage[key] = value - } - // Don't bother journal since this function should only be used for - // debugging and the `fake` storage won't be committed to database. -} - func (s *stateObject) setState(key, value common.Hash) { s.dirtyStorage[key] = value diff --git a/core/state/statedb.go b/core/state/statedb.go index 7b1f6b2304..d8c389606b 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -410,8 +410,8 @@ func (s *StateDB) SetState(addr common.Address, key, value common.Hash) { // storage. This function should only be used for debugging. func (s *StateDB) SetStorage(addr common.Address, storage map[common.Hash]common.Hash) { stateObject := s.GetOrNewStateObject(addr) - if stateObject != nil { - stateObject.SetStorage(storage) + for k, v := range storage { + stateObject.SetState(s.db, k, v) } } diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index 6b0403053e..65a3839ca1 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -569,6 +569,10 @@ func (diff *StateOverride) Apply(state *state.StateDB) error { } } } + // Now finalize the changes. Finalize is normally performed between transactions. + // By using finalize, the overrides are semantically behaving as + // if they were created in a transaction just before the tracing occur. + state.Finalise(false) return nil }