Skip to content

Commit 9f0e1f8

Browse files
AaronChen0ARR4N
authored andcommitted
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 8580388 commit 9f0e1f8

File tree

1 file changed

+8
-20
lines changed

1 file changed

+8
-20
lines changed

core/state/statedb.go

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package state
1919

2020
import (
2121
"fmt"
22+
"maps"
2223
"sort"
2324
"time"
2425

@@ -693,18 +694,18 @@ func (s *StateDB) Copy() *StateDB {
693694
db: s.db,
694695
trie: s.db.CopyTrie(s.trie),
695696
originalRoot: s.originalRoot,
696-
accounts: make(map[common.Hash][]byte),
697-
storages: make(map[common.Hash]map[common.Hash][]byte),
698-
accountsOrigin: make(map[common.Address][]byte),
699-
storagesOrigin: make(map[common.Address]map[common.Hash][]byte),
697+
accounts: copySet(s.accounts),
698+
storages: copy2DSet(s.storages),
699+
accountsOrigin: copySet(s.accountsOrigin),
700+
storagesOrigin: copy2DSet(s.storagesOrigin),
700701
stateObjects: make(map[common.Address]*stateObject, len(s.journal.dirties)),
701702
stateObjectsPending: make(map[common.Address]struct{}, len(s.stateObjectsPending)),
702703
stateObjectsDirty: make(map[common.Address]struct{}, len(s.journal.dirties)),
703-
stateObjectsDestruct: make(map[common.Address]*types.StateAccount, len(s.stateObjectsDestruct)),
704+
stateObjectsDestruct: maps.Clone(s.stateObjectsDestruct),
704705
refund: s.refund,
705706
logs: make(map[common.Hash][]*types.Log, len(s.logs)),
706707
logSize: s.logSize,
707-
preimages: make(map[common.Hash][]byte, len(s.preimages)),
708+
preimages: maps.Clone(s.preimages),
708709
journal: newJournal(),
709710
hasher: crypto.NewKeccakState(),
710711

@@ -747,16 +748,6 @@ func (s *StateDB) Copy() *StateDB {
747748
}
748749
state.stateObjectsDirty[addr] = struct{}{}
749750
}
750-
// Deep copy the destruction markers.
751-
for addr, value := range s.stateObjectsDestruct {
752-
state.stateObjectsDestruct[addr] = value
753-
}
754-
// Deep copy the state changes made in the scope of block
755-
// along with their original values.
756-
state.accounts = copySet(s.accounts)
757-
state.storages = copy2DSet(s.storages)
758-
state.accountsOrigin = copySet(state.accountsOrigin)
759-
state.storagesOrigin = copy2DSet(state.storagesOrigin)
760751

761752
// Deep copy the logs occurred in the scope of block
762753
for hash, logs := range s.logs {
@@ -767,10 +758,7 @@ func (s *StateDB) Copy() *StateDB {
767758
}
768759
state.logs[hash] = cpy
769760
}
770-
// Deep copy the preimages occurred in the scope of block
771-
for hash, preimage := range s.preimages {
772-
state.preimages[hash] = preimage
773-
}
761+
774762
// Do we need to copy the access list and transient storage?
775763
// In practice: No. At the start of a transaction, these two lists are empty.
776764
// In practice, we only ever copy state _between_ transactions/blocks, never

0 commit comments

Comments
 (0)