core/state: fix bug in statedb.Copy and remove unnecessary preallocation (#29563)

This change removes an unnecessary preallocation and fixes a flaw with no-op copies of some parts of the statedb
This commit is contained in:
Aaron Chen 2024-04-17 19:55:31 +08:00 committed by GitHub
parent 74e8d2da97
commit 1e9bf2a09e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 7 additions and 17 deletions

View File

@ -696,18 +696,18 @@ func (s *StateDB) Copy() *StateDB {
db: s.db, db: s.db,
trie: s.db.CopyTrie(s.trie), trie: s.db.CopyTrie(s.trie),
originalRoot: s.originalRoot, originalRoot: s.originalRoot,
accounts: make(map[common.Hash][]byte), accounts: copySet(s.accounts),
storages: make(map[common.Hash]map[common.Hash][]byte), storages: copy2DSet(s.storages),
accountsOrigin: make(map[common.Address][]byte), accountsOrigin: copySet(s.accountsOrigin),
storagesOrigin: make(map[common.Address]map[common.Hash][]byte), storagesOrigin: copy2DSet(s.storagesOrigin),
stateObjects: make(map[common.Address]*stateObject, len(s.journal.dirties)), stateObjects: make(map[common.Address]*stateObject, len(s.journal.dirties)),
stateObjectsPending: make(map[common.Address]struct{}, len(s.stateObjectsPending)), stateObjectsPending: make(map[common.Address]struct{}, len(s.stateObjectsPending)),
stateObjectsDirty: make(map[common.Address]struct{}, len(s.journal.dirties)), stateObjectsDirty: make(map[common.Address]struct{}, len(s.journal.dirties)),
stateObjectsDestruct: make(map[common.Address]*types.StateAccount, len(s.stateObjectsDestruct)), stateObjectsDestruct: maps.Clone(s.stateObjectsDestruct),
refund: s.refund, refund: s.refund,
logs: make(map[common.Hash][]*types.Log, len(s.logs)), logs: make(map[common.Hash][]*types.Log, len(s.logs)),
logSize: s.logSize, logSize: s.logSize,
preimages: make(map[common.Hash][]byte, len(s.preimages)), preimages: maps.Clone(s.preimages),
journal: newJournal(), journal: newJournal(),
hasher: crypto.NewKeccakState(), hasher: crypto.NewKeccakState(),
@ -750,15 +750,6 @@ func (s *StateDB) Copy() *StateDB {
} }
state.stateObjectsDirty[addr] = struct{}{} state.stateObjectsDirty[addr] = struct{}{}
} }
// Deep copy the destruction markers.
state.stateObjectsDestruct = maps.Clone(s.stateObjectsDestruct)
// Deep copy the state changes made in the scope of block
// along with their original values.
state.accounts = copySet(s.accounts)
state.storages = copy2DSet(s.storages)
state.accountsOrigin = copySet(state.accountsOrigin)
state.storagesOrigin = copy2DSet(state.storagesOrigin)
// Deep copy the logs occurred in the scope of block // Deep copy the logs occurred in the scope of block
for hash, logs := range s.logs { for hash, logs := range s.logs {
@ -769,8 +760,7 @@ func (s *StateDB) Copy() *StateDB {
} }
state.logs[hash] = cpy state.logs[hash] = cpy
} }
// Deep copy the preimages occurred in the scope of block
state.preimages = maps.Clone(s.preimages)
// Do we need to copy the access list and transient storage? // Do we need to copy the access list and transient storage?
// In practice: No. At the start of a transaction, these two lists are empty. // In practice: No. At the start of a transaction, these two lists are empty.
// In practice, we only ever copy state _between_ transactions/blocks, never // In practice, we only ever copy state _between_ transactions/blocks, never