Skip to content

Commit 1e9bf2a

Browse files
authored
core/state: fix bug in statedb.Copy and remove unnecessary preallocation (ethereum#29563)
This change removes an unnecessary preallocation and fixes a flaw with no-op copies of some parts of the statedb
1 parent 74e8d2d commit 1e9bf2a

File tree

1 file changed

+7
-17
lines changed

1 file changed

+7
-17
lines changed

core/state/statedb.go

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -696,18 +696,18 @@ func (s *StateDB) Copy() *StateDB {
696696
db: s.db,
697697
trie: s.db.CopyTrie(s.trie),
698698
originalRoot: s.originalRoot,
699-
accounts: make(map[common.Hash][]byte),
700-
storages: make(map[common.Hash]map[common.Hash][]byte),
701-
accountsOrigin: make(map[common.Address][]byte),
702-
storagesOrigin: make(map[common.Address]map[common.Hash][]byte),
699+
accounts: copySet(s.accounts),
700+
storages: copy2DSet(s.storages),
701+
accountsOrigin: copySet(s.accountsOrigin),
702+
storagesOrigin: copy2DSet(s.storagesOrigin),
703703
stateObjects: make(map[common.Address]*stateObject, len(s.journal.dirties)),
704704
stateObjectsPending: make(map[common.Address]struct{}, len(s.stateObjectsPending)),
705705
stateObjectsDirty: make(map[common.Address]struct{}, len(s.journal.dirties)),
706-
stateObjectsDestruct: make(map[common.Address]*types.StateAccount, len(s.stateObjectsDestruct)),
706+
stateObjectsDestruct: maps.Clone(s.stateObjectsDestruct),
707707
refund: s.refund,
708708
logs: make(map[common.Hash][]*types.Log, len(s.logs)),
709709
logSize: s.logSize,
710-
preimages: make(map[common.Hash][]byte, len(s.preimages)),
710+
preimages: maps.Clone(s.preimages),
711711
journal: newJournal(),
712712
hasher: crypto.NewKeccakState(),
713713

@@ -750,15 +750,6 @@ func (s *StateDB) Copy() *StateDB {
750750
}
751751
state.stateObjectsDirty[addr] = struct{}{}
752752
}
753-
// Deep copy the destruction markers.
754-
state.stateObjectsDestruct = maps.Clone(s.stateObjectsDestruct)
755-
756-
// Deep copy the state changes made in the scope of block
757-
// along with their original values.
758-
state.accounts = copySet(s.accounts)
759-
state.storages = copy2DSet(s.storages)
760-
state.accountsOrigin = copySet(state.accountsOrigin)
761-
state.storagesOrigin = copy2DSet(state.storagesOrigin)
762753

763754
// Deep copy the logs occurred in the scope of block
764755
for hash, logs := range s.logs {
@@ -769,8 +760,7 @@ func (s *StateDB) Copy() *StateDB {
769760
}
770761
state.logs[hash] = cpy
771762
}
772-
// Deep copy the preimages occurred in the scope of block
773-
state.preimages = maps.Clone(s.preimages)
763+
774764
// Do we need to copy the access list and transient storage?
775765
// In practice: No. At the start of a transaction, these two lists are empty.
776766
// In practice, we only ever copy state _between_ transactions/blocks, never

0 commit comments

Comments
 (0)