trie, consensus/clique: use maps.Clone (#29616)

This commit is contained in:
Aaron Chen 2024-04-24 20:27:58 +08:00 committed by GitHub
parent ac21f9bfb5
commit 7362691479
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 30 deletions

@ -19,6 +19,7 @@ package clique
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"maps"
"slices" "slices"
"time" "time"
@ -108,28 +109,16 @@ func (s *Snapshot) store(db ethdb.Database) error {
// copy creates a deep copy of the snapshot, though not the individual votes. // copy creates a deep copy of the snapshot, though not the individual votes.
func (s *Snapshot) copy() *Snapshot { func (s *Snapshot) copy() *Snapshot {
cpy := &Snapshot{ return &Snapshot{
config: s.config, config: s.config,
sigcache: s.sigcache, sigcache: s.sigcache,
Number: s.Number, Number: s.Number,
Hash: s.Hash, Hash: s.Hash,
Signers: make(map[common.Address]struct{}), Signers: maps.Clone(s.Signers),
Recents: make(map[uint64]common.Address), Recents: maps.Clone(s.Recents),
Votes: make([]*Vote, len(s.Votes)), Votes: slices.Clone(s.Votes),
Tally: make(map[common.Address]Tally), Tally: maps.Clone(s.Tally),
} }
for signer := range s.Signers {
cpy.Signers[signer] = struct{}{}
}
for block, signer := range s.Recents {
cpy.Recents[block] = signer
}
for address, tally := range s.Tally {
cpy.Tally[address] = tally
}
copy(cpy.Votes, s.Votes)
return cpy
} }
// validVote returns whether it makes sense to cast the specified vote in the // validVote returns whether it makes sense to cast the specified vote in the

@ -17,6 +17,8 @@
package trie package trie
import ( import (
"maps"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
) )
@ -92,23 +94,13 @@ func (t *tracer) reset() {
// copy returns a deep copied tracer instance. // copy returns a deep copied tracer instance.
func (t *tracer) copy() *tracer { func (t *tracer) copy() *tracer {
var ( accessList := make(map[string][]byte, len(t.accessList))
inserts = make(map[string]struct{})
deletes = make(map[string]struct{})
accessList = make(map[string][]byte)
)
for path := range t.inserts {
inserts[path] = struct{}{}
}
for path := range t.deletes {
deletes[path] = struct{}{}
}
for path, blob := range t.accessList { for path, blob := range t.accessList {
accessList[path] = common.CopyBytes(blob) accessList[path] = common.CopyBytes(blob)
} }
return &tracer{ return &tracer{
inserts: inserts, inserts: maps.Clone(t.inserts),
deletes: deletes, deletes: maps.Clone(t.deletes),
accessList: accessList, accessList: accessList,
} }
} }