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

This commit is contained in:
wit liu 2025-11-14 22:30:21 +08:00 committed by GitHub
parent 3107006ac6
commit 9155d354c9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 22 additions and 31 deletions

View file

@ -19,6 +19,8 @@ package clique
import (
"bytes"
"encoding/json"
"maps"
"slices"
"github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/common/lru"
@ -104,28 +106,16 @@ func (s *Snapshot) store(db ethdb.Database) error {
// copy creates a deep copy of the snapshot, though not the individual votes.
func (s *Snapshot) copy() *Snapshot {
cpy := &Snapshot{
return &Snapshot{
config: s.config,
sigcache: s.sigcache,
Number: s.Number,
Hash: s.Hash,
Signers: make(map[common.Address]struct{}),
Recents: make(map[uint64]common.Address),
Votes: make([]*Vote, len(s.Votes)),
Tally: make(map[common.Address]Tally),
Signers: maps.Clone(s.Signers),
Recents: maps.Clone(s.Recents),
Votes: slices.Clone(s.Votes),
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

View file

@ -16,6 +16,12 @@
package trie
import (
"maps"
"github.com/XinFinOrg/XDPoSChain/common"
)
// tracer tracks the changes of trie nodes. During the trie operations,
// some nodes can be deleted from the trie, while these deleted nodes
// won't be captured by trie.Hasher or trie.Committer. Thus, these deleted
@ -154,24 +160,19 @@ func (t *tracer) reset() {
// copy returns a deep copied tracer instance.
func (t *tracer) copy() *tracer {
// Tracer isn't used right now, remove this check later.
if t == nil {
return nil
}
var (
insert = make(map[string]struct{})
delete = make(map[string]struct{})
origin = make(map[string][]byte)
)
for key := range t.insert {
insert[key] = struct{}{}
}
for key := range t.delete {
delete[key] = struct{}{}
}
for key, val := range t.origin {
origin[key] = val
insert := maps.Clone(t.insert)
delete := maps.Clone(t.delete)
origin := make(map[string][]byte, len(t.origin))
for k, v := range t.origin {
// `common.CopyBytes` ensures deep copy according to comment
origin[k] = common.CopyBytes(v)
}
return &tracer{
insert: insert,
delete: delete,