From 9155d354c935734ade04c64a555e8dbbabb92d11 Mon Sep 17 00:00:00 2001 From: wit liu <765765346@qq.com> Date: Fri, 14 Nov 2025 22:30:21 +0800 Subject: [PATCH] tracers, consensus/clique: use maps.Clone #29616 (#1755) --- consensus/clique/snapshot.go | 24 +++++++----------------- trie/utils.go | 29 +++++++++++++++-------------- 2 files changed, 22 insertions(+), 31 deletions(-) diff --git a/consensus/clique/snapshot.go b/consensus/clique/snapshot.go index ff7c5a4c8e..d158701ad9 100644 --- a/consensus/clique/snapshot.go +++ b/consensus/clique/snapshot.go @@ -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 diff --git a/trie/utils.go b/trie/utils.go index d462b31bd2..14b0048bad 100644 --- a/trie/utils.go +++ b/trie/utils.go @@ -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,