From 9affb6a582f7cbc990bfe962a485c9182224f5d5 Mon Sep 17 00:00:00 2001 From: Daniel Liu <139250065@qq.com> Date: Wed, 2 Jul 2025 14:53:00 +0800 Subject: [PATCH] core/state: memory efficient, add AddPreimage test #16663 (#1182) --- core/state/statedb.go | 6 ++---- core/state/statedb_test.go | 9 +++++++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/core/state/statedb.go b/core/state/statedb.go index 79912b8865..3c7c60d65a 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -19,6 +19,7 @@ package state import ( "fmt" + "maps" "math/big" "sort" "time" @@ -637,7 +638,7 @@ func (s *StateDB) Copy() *StateDB { refund: s.refund, logs: make(map[common.Hash][]*types.Log, len(s.logs)), logSize: s.logSize, - preimages: make(map[common.Hash][]byte), + preimages: maps.Clone(s.preimages), journal: newJournal(), } // Copy the dirty states, logs, and preimages @@ -683,9 +684,6 @@ func (s *StateDB) Copy() *StateDB { state.logs[hash] = cpy } - for hash, preimage := range s.preimages { - state.preimages[hash] = preimage - } // Do we need to copy the access list? In practice: No. At the start of a // transaction, the access list is empty. In practice, we only ever copy state // _between_ transactions/blocks, never in the middle of a transaction. diff --git a/core/state/statedb_test.go b/core/state/statedb_test.go index e7d54fc3d7..0aa1f6ef4d 100644 --- a/core/state/statedb_test.go +++ b/core/state/statedb_test.go @@ -305,6 +305,15 @@ func newTestAction(addr common.Address, r *rand.Rand) testAction { }, args: make([]int64, 2), }, + { + name: "AddPreimage", + fn: func(a testAction, s *StateDB) { + preimage := []byte{1} + hash := common.BytesToHash(preimage) + s.AddPreimage(hash, preimage) + }, + args: make([]int64, 1), + }, } action := actions[r.Intn(len(actions))] var nameargs []string