Skip to content

Commit 6b5ccf9

Browse files
committed
trie: polish
1 parent aae088d commit 6b5ccf9

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

trie/hasher.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ func (h *hasher) hash(n node, force bool) []byte {
6767
if len(enc) < 32 && !force {
6868
// Nodes smaller than 32 bytes are embedded directly in their parent.
6969
// In such cases, return the raw encoded blob instead of the node hash.
70+
// It's essential to deep-copy the node blob, as the underlying buffer
71+
// of enc will be reused later.
7072
buf := make([]byte, len(enc))
7173
copy(buf, enc)
7274
return buf
@@ -80,6 +82,8 @@ func (h *hasher) hash(n node, force bool) []byte {
8082
if len(enc) < 32 && !force {
8183
// Nodes smaller than 32 bytes are embedded directly in their parent.
8284
// In such cases, return the raw encoded blob instead of the node hash.
85+
// It's essential to deep-copy the node blob, as the underlying buffer
86+
// of enc will be reused later.
8387
buf := make([]byte, len(enc))
8488
copy(buf, enc)
8589
return buf
@@ -137,12 +141,11 @@ func (h *hasher) encodeFullNode(n *fullNode) []byte {
137141
}
138142
wg.Add(1)
139143
go func(i int) {
140-
hasher := newHasher(false)
141-
if child := n.Children[i]; child != nil {
142-
fn.Children[i] = hasher.hash(child, false)
143-
}
144-
returnHasherToPool(hasher)
145-
wg.Done()
144+
defer wg.Done()
145+
146+
h := newHasher(false)
147+
defer returnHasherToPool(h)
148+
fn.Children[i] = h.hash(n.Children[i], false)
146149
}(i)
147150
}
148151
wg.Wait()
@@ -196,6 +199,7 @@ func (h *hasher) hashDataTo(dst, data []byte) {
196199
}
197200

198201
// proofHash is used to construct trie proofs, returning the rlp-encoded node blobs.
202+
// Note, only resolved node (shortNode or fullNode) is expected for proofing.
199203
func (h *hasher) proofHash(original node) []byte {
200204
switch n := original.(type) {
201205
case *shortNode:

trie/trie_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,6 @@ func (s *spongeDb) Flush() {
863863
s.sponge.Write([]byte(key))
864864
s.sponge.Write([]byte(s.values[key]))
865865
}
866-
//fmt.Println(len(s.keys))
867866
}
868867

869868
// spongeBatch is a dummy batch which immediately writes to the underlying spongedb

0 commit comments

Comments
 (0)