From 891a386a58081d3cfa685a7d89b423a08df1fb43 Mon Sep 17 00:00:00 2001 From: wit liu <765765346@qq.com> Date: Fri, 14 Nov 2025 22:30:59 +0800 Subject: [PATCH] core/state: using maps.Clone #29365 (#1754) --- core/state/access_list.go | 12 ++++-------- core/state/state_object.go | 8 ++------ core/state/statedb.go | 8 ++------ 3 files changed, 8 insertions(+), 20 deletions(-) diff --git a/core/state/access_list.go b/core/state/access_list.go index 0723649ad7..383270e63c 100644 --- a/core/state/access_list.go +++ b/core/state/access_list.go @@ -17,6 +17,8 @@ package state import ( + "maps" + "github.com/XinFinOrg/XDPoSChain/common" ) @@ -57,16 +59,10 @@ func newAccessList() *accessList { // Copy creates an independent copy of an accessList. func (al *accessList) Copy() *accessList { cp := newAccessList() - for k, v := range al.addresses { - cp.addresses[k] = v - } + cp.addresses = maps.Clone(al.addresses) cp.slots = make([]map[common.Hash]struct{}, len(al.slots)) for i, slotMap := range al.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 64c9ee8567..a294af1cd7 100644 --- a/core/state/state_object.go +++ b/core/state/state_object.go @@ -20,6 +20,7 @@ import ( "bytes" "fmt" "io" + "maps" "math/big" "time" @@ -48,12 +49,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 d8869a2d4d..3f55b8fb27 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -719,9 +719,7 @@ func (s *StateDB) Copy() *StateDB { state.stateObjectsDirty[addr] = struct{}{} } // Deep copy the destruction flag. - for addr := range s.stateObjectsDestruct { - state.stateObjectsDestruct[addr] = struct{}{} - } + state.stateObjectsDestruct = maps.Clone(s.stateObjectsDestruct) // Deep copy the logs occurred in the scope of block for hash, logs := range s.logs { cpy := make([]*types.Log, len(logs)) @@ -731,9 +729,7 @@ func (s *StateDB) Copy() *StateDB { } state.logs[hash] = cpy } - 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