Skip to content

Commit

Permalink
core/state: fix bug in statedb.Copy and remove unnecessary preallocat…
Browse files Browse the repository at this point in the history
…ion (ethereum#29563)

This change removes an unnecessary preallocation and fixes a flaw with no-op copies of some parts of the statedb
  • Loading branch information
AaronChen0 authored and ARR4N committed Feb 17, 2025
1 parent 8efe53f commit 023add6
Showing 1 changed file with 8 additions and 20 deletions.
28 changes: 8 additions & 20 deletions core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package state

import (
"fmt"
"maps"
"sort"
"time"

Expand Down Expand Up @@ -693,18 +694,18 @@ func (s *StateDB) Copy() *StateDB {
db: s.db,
trie: s.db.CopyTrie(s.trie),
originalRoot: s.originalRoot,
accounts: make(map[common.Hash][]byte),
storages: make(map[common.Hash]map[common.Hash][]byte),
accountsOrigin: make(map[common.Address][]byte),
storagesOrigin: make(map[common.Address]map[common.Hash][]byte),
accounts: copySet(s.accounts),
storages: copy2DSet(s.storages),
accountsOrigin: copySet(s.accountsOrigin),
storagesOrigin: copy2DSet(s.storagesOrigin),
stateObjects: make(map[common.Address]*stateObject, len(s.journal.dirties)),
stateObjectsPending: make(map[common.Address]struct{}, len(s.stateObjectsPending)),
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,
logs: make(map[common.Hash][]*types.Log, len(s.logs)),
logSize: s.logSize,
preimages: make(map[common.Hash][]byte, len(s.preimages)),
preimages: maps.Clone(s.preimages),
journal: newJournal(),
hasher: crypto.NewKeccakState(),

Expand Down Expand Up @@ -747,16 +748,6 @@ func (s *StateDB) Copy() *StateDB {
}
state.stateObjectsDirty[addr] = struct{}{}
}
// Deep copy the destruction markers.
for addr, value := range s.stateObjectsDestruct {
state.stateObjectsDestruct[addr] = value
}
// 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
for hash, logs := range s.logs {
Expand All @@ -767,10 +758,7 @@ func (s *StateDB) Copy() *StateDB {
}
state.logs[hash] = cpy
}
// Deep copy the preimages occurred in the scope of block
for hash, preimage := range s.preimages {
state.preimages[hash] = preimage
}

// 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, we only ever copy state _between_ transactions/blocks, never
Expand Down

0 comments on commit 023add6

Please sign in to comment.