trie: polish

This commit is contained in:
Gary Rong 2025-05-26 09:57:34 +08:00
parent e7e19e7ff1
commit f64a89ff47
2 changed files with 10 additions and 7 deletions

View file

@ -67,6 +67,8 @@ func (h *hasher) hash(n node, force bool) []byte {
if len(enc) < 32 && !force { if len(enc) < 32 && !force {
// Nodes smaller than 32 bytes are embedded directly in their parent. // Nodes smaller than 32 bytes are embedded directly in their parent.
// In such cases, return the raw encoded blob instead of the node hash. // In such cases, return the raw encoded blob instead of the node hash.
// It's essential to deep-copy the node blob, as the underlying buffer
// of enc will be reused later.
buf := make([]byte, len(enc)) buf := make([]byte, len(enc))
copy(buf, enc) copy(buf, enc)
return buf return buf
@ -80,6 +82,8 @@ func (h *hasher) hash(n node, force bool) []byte {
if len(enc) < 32 && !force { if len(enc) < 32 && !force {
// Nodes smaller than 32 bytes are embedded directly in their parent. // Nodes smaller than 32 bytes are embedded directly in their parent.
// In such cases, return the raw encoded blob instead of the node hash. // In such cases, return the raw encoded blob instead of the node hash.
// It's essential to deep-copy the node blob, as the underlying buffer
// of enc will be reused later.
buf := make([]byte, len(enc)) buf := make([]byte, len(enc))
copy(buf, enc) copy(buf, enc)
return buf return buf
@ -137,12 +141,11 @@ func (h *hasher) encodeFullNode(n *fullNode) []byte {
} }
wg.Add(1) wg.Add(1)
go func(i int) { go func(i int) {
hasher := newHasher(false) defer wg.Done()
if child := n.Children[i]; child != nil {
fn.Children[i] = hasher.hash(child, false) h := newHasher(false)
} defer returnHasherToPool(h)
returnHasherToPool(hasher) fn.Children[i] = h.hash(n.Children[i], false)
wg.Done()
}(i) }(i)
} }
wg.Wait() wg.Wait()
@ -196,6 +199,7 @@ func (h *hasher) hashDataTo(dst, data []byte) {
} }
// proofHash is used to construct trie proofs, returning the rlp-encoded node blobs. // proofHash is used to construct trie proofs, returning the rlp-encoded node blobs.
// Note, only resolved node (shortNode or fullNode) is expected for proofing.
func (h *hasher) proofHash(original node) []byte { func (h *hasher) proofHash(original node) []byte {
switch n := original.(type) { switch n := original.(type) {
case *shortNode: case *shortNode:

View file

@ -863,7 +863,6 @@ func (s *spongeDb) Flush() {
s.sponge.Write([]byte(key)) s.sponge.Write([]byte(key))
s.sponge.Write([]byte(s.values[key])) s.sponge.Write([]byte(s.values[key]))
} }
//fmt.Println(len(s.keys))
} }
// spongeBatch is a dummy batch which immediately writes to the underlying spongedb // spongeBatch is a dummy batch which immediately writes to the underlying spongedb