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 ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"maps"
"slices"
"github.com/XinFinOrg/XDPoSChain/common" "github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/common/lru" "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. // 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

View file

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