core/state: using maps.Clone #29365 (#1754)

This commit is contained in:
wit liu 2025-11-14 22:30:59 +08:00 committed by GitHub
parent 9155d354c9
commit 891a386a58
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 8 additions and 20 deletions

View file

@ -17,6 +17,8 @@
package state package state
import ( import (
"maps"
"github.com/XinFinOrg/XDPoSChain/common" "github.com/XinFinOrg/XDPoSChain/common"
) )
@ -57,16 +59,10 @@ func newAccessList() *accessList {
// Copy creates an independent copy of an accessList. // Copy creates an independent copy of an accessList.
func (al *accessList) Copy() *accessList { func (al *accessList) Copy() *accessList {
cp := newAccessList() cp := newAccessList()
for k, v := range al.addresses { cp.addresses = maps.Clone(al.addresses)
cp.addresses[k] = v
}
cp.slots = make([]map[common.Hash]struct{}, len(al.slots)) cp.slots = make([]map[common.Hash]struct{}, len(al.slots))
for i, slotMap := range al.slots { for i, slotMap := range al.slots {
newSlotmap := make(map[common.Hash]struct{}, len(slotMap)) cp.slots[i] = maps.Clone(slotMap)
for k := range slotMap {
newSlotmap[k] = struct{}{}
}
cp.slots[i] = newSlotmap
} }
return cp return cp
} }

View file

@ -20,6 +20,7 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"io" "io"
"maps"
"math/big" "math/big"
"time" "time"
@ -48,12 +49,7 @@ func (s Storage) String() (str string) {
} }
func (s Storage) Copy() Storage { func (s Storage) Copy() Storage {
cpy := make(Storage, len(s)) return maps.Clone(s)
for key, value := range s {
cpy[key] = value
}
return cpy
} }
// stateObject represents an Ethereum account which is being modified. // stateObject represents an Ethereum account which is being modified.

View file

@ -719,9 +719,7 @@ func (s *StateDB) Copy() *StateDB {
state.stateObjectsDirty[addr] = struct{}{} state.stateObjectsDirty[addr] = struct{}{}
} }
// Deep copy the destruction flag. // Deep copy the destruction flag.
for addr := range s.stateObjectsDestruct { state.stateObjectsDestruct = maps.Clone(s.stateObjectsDestruct)
state.stateObjectsDestruct[addr] = struct{}{}
}
// Deep copy the logs occurred in the scope of block // Deep copy the logs occurred in the scope of block
for hash, logs := range s.logs { for hash, logs := range s.logs {
cpy := make([]*types.Log, len(logs)) cpy := make([]*types.Log, len(logs))
@ -731,9 +729,7 @@ func (s *StateDB) Copy() *StateDB {
} }
state.logs[hash] = cpy state.logs[hash] = cpy
} }
for hash, preimage := range s.preimages { state.preimages = maps.Clone(s.preimages)
state.preimages[hash] = preimage
}
// Do we need to copy the access list and transient storage? // 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: No. At the start of a transaction, these two lists are empty.
// In practice, we only ever copy state _between_ transactions/blocks, never // In practice, we only ever copy state _between_ transactions/blocks, never