From ab6419ccd8b11e041e27f8865f59ab111a2c6161 Mon Sep 17 00:00:00 2001 From: cui <523516579@qq.com> Date: Tue, 2 Apr 2024 20:56:12 +0800 Subject: [PATCH] core/state: use maps.Clone (#29365) core: using maps.Clone --- core/state/access_list.go | 12 ++++-------- core/state/state_object.go | 7 ++----- core/state/statedb.go | 10 ++++------ 3 files changed, 10 insertions(+), 19 deletions(-) diff --git a/core/state/access_list.go b/core/state/access_list.go index 4194691345..718bf17cf7 100644 --- a/core/state/access_list.go +++ b/core/state/access_list.go @@ -17,6 +17,8 @@ package state import ( + "maps" + "github.com/ethereum/go-ethereum/common" ) @@ -57,16 +59,10 @@ func newAccessList() *accessList { // Copy creates an independent copy of an accessList. func (a *accessList) Copy() *accessList { cp := newAccessList() - for k, v := range a.addresses { - cp.addresses[k] = v - } + cp.addresses = maps.Clone(a.addresses) cp.slots = make([]map[common.Hash]struct{}, len(a.slots)) for i, slotMap := range a.slots { - newSlotmap := make(map[common.Hash]struct{}, len(slotMap)) - for k := range slotMap { - newSlotmap[k] = struct{}{} - } - cp.slots[i] = newSlotmap + cp.slots[i] = maps.Clone(slotMap) } return cp } diff --git a/core/state/state_object.go b/core/state/state_object.go index 33b593f069..1aa7946fd0 100644 --- a/core/state/state_object.go +++ b/core/state/state_object.go @@ -20,6 +20,7 @@ import ( "bytes" "fmt" "io" + "maps" "time" "github.com/ethereum/go-ethereum/common" @@ -47,11 +48,7 @@ func (s Storage) String() (str string) { } func (s Storage) Copy() Storage { - cpy := make(Storage, len(s)) - for key, value := range s { - cpy[key] = value - } - return cpy + return maps.Clone(s) } // stateObject represents an Ethereum account which is being modified. diff --git a/core/state/statedb.go b/core/state/statedb.go index 8cdcbc40c3..f2c2e7a798 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -19,6 +19,7 @@ package state import ( "fmt" + "maps" "math/big" "slices" "sort" @@ -750,9 +751,8 @@ func (s *StateDB) Copy() *StateDB { state.stateObjectsDirty[addr] = struct{}{} } // Deep copy the destruction markers. - for addr, value := range s.stateObjectsDestruct { - state.stateObjectsDestruct[addr] = value - } + state.stateObjectsDestruct = maps.Clone(s.stateObjectsDestruct) + // Deep copy the state changes made in the scope of block // along with their original values. state.accounts = copySet(s.accounts) @@ -770,9 +770,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 - } + state.preimages = maps.Clone(s.preimages) // 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