Skip to content

Commit 7362691

Browse files
authored
trie, consensus/clique: use maps.Clone (#29616)
1 parent ac21f9b commit 7362691

File tree

2 files changed

+11
-30
lines changed

2 files changed

+11
-30
lines changed

consensus/clique/snapshot.go

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package clique
1919
import (
2020
"bytes"
2121
"encoding/json"
22+
"maps"
2223
"slices"
2324
"time"
2425

@@ -108,28 +109,16 @@ func (s *Snapshot) store(db ethdb.Database) error {
108109

109110
// copy creates a deep copy of the snapshot, though not the individual votes.
110111
func (s *Snapshot) copy() *Snapshot {
111-
cpy := &Snapshot{
112+
return &Snapshot{
112113
config: s.config,
113114
sigcache: s.sigcache,
114115
Number: s.Number,
115116
Hash: s.Hash,
116-
Signers: make(map[common.Address]struct{}),
117-
Recents: make(map[uint64]common.Address),
118-
Votes: make([]*Vote, len(s.Votes)),
119-
Tally: make(map[common.Address]Tally),
120-
}
121-
for signer := range s.Signers {
122-
cpy.Signers[signer] = struct{}{}
117+
Signers: maps.Clone(s.Signers),
118+
Recents: maps.Clone(s.Recents),
119+
Votes: slices.Clone(s.Votes),
120+
Tally: maps.Clone(s.Tally),
123121
}
124-
for block, signer := range s.Recents {
125-
cpy.Recents[block] = signer
126-
}
127-
for address, tally := range s.Tally {
128-
cpy.Tally[address] = tally
129-
}
130-
copy(cpy.Votes, s.Votes)
131-
132-
return cpy
133122
}
134123

135124
// validVote returns whether it makes sense to cast the specified vote in the

trie/tracer.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package trie
1818

1919
import (
20+
"maps"
21+
2022
"github.com/ethereum/go-ethereum/common"
2123
)
2224

@@ -92,23 +94,13 @@ func (t *tracer) reset() {
9294

9395
// copy returns a deep copied tracer instance.
9496
func (t *tracer) copy() *tracer {
95-
var (
96-
inserts = make(map[string]struct{})
97-
deletes = make(map[string]struct{})
98-
accessList = make(map[string][]byte)
99-
)
100-
for path := range t.inserts {
101-
inserts[path] = struct{}{}
102-
}
103-
for path := range t.deletes {
104-
deletes[path] = struct{}{}
105-
}
97+
accessList := make(map[string][]byte, len(t.accessList))
10698
for path, blob := range t.accessList {
10799
accessList[path] = common.CopyBytes(blob)
108100
}
109101
return &tracer{
110-
inserts: inserts,
111-
deletes: deletes,
102+
inserts: maps.Clone(t.inserts),
103+
deletes: maps.Clone(t.deletes),
112104
accessList: accessList,
113105
}
114106
}

0 commit comments

Comments
 (0)