Skip to content

Commit 15f52a2

Browse files
authored
core/state: fix code existence not marked correctly (#33415)
When iterating over a map with value types in Go, the loop variable is a copy. In `markCodeExistence`, assigning to `code.exists` modified only the local copy, not the actual map entry, causing the existence flag to always remain false. This resulted in overcounting contract codes in state size statistics, as codes that already existed in the database were incorrectly counted as new. Fix by changing `codes` from `map[common.Address]contractCode` to `map[common.Address]*contractCode`, so mutations apply directly to the struct.
1 parent e20b05e commit 15f52a2

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

core/state/stateupdate.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ type stateUpdate struct {
8383
storagesOrigin map[common.Address]map[common.Hash][]byte
8484
rawStorageKey bool
8585

86-
codes map[common.Address]contractCode // codes contains the set of dirty codes
87-
nodes *trienode.MergedNodeSet // Aggregated dirty nodes caused by state changes
86+
codes map[common.Address]*contractCode // codes contains the set of dirty codes
87+
nodes *trienode.MergedNodeSet // Aggregated dirty nodes caused by state changes
8888
}
8989

9090
// empty returns a flag indicating the state transition is empty or not.
@@ -104,7 +104,7 @@ func newStateUpdate(rawStorageKey bool, originRoot common.Hash, root common.Hash
104104
accountsOrigin = make(map[common.Address][]byte)
105105
storages = make(map[common.Hash]map[common.Hash][]byte)
106106
storagesOrigin = make(map[common.Address]map[common.Hash][]byte)
107-
codes = make(map[common.Address]contractCode)
107+
codes = make(map[common.Address]*contractCode)
108108
)
109109
// Since some accounts might be destroyed and recreated within the same
110110
// block, deletions must be aggregated first.
@@ -126,7 +126,7 @@ func newStateUpdate(rawStorageKey bool, originRoot common.Hash, root common.Hash
126126
// Aggregate dirty contract codes if they are available.
127127
addr := op.address
128128
if op.code != nil {
129-
codes[addr] = *op.code
129+
codes[addr] = op.code
130130
}
131131
accounts[addrHash] = op.data
132132

0 commit comments

Comments
 (0)