trie: add comments

This commit is contained in:
Gary Rong 2025-07-18 14:46:56 +08:00
parent f64a89ff47
commit d5463bbc59
2 changed files with 14 additions and 6 deletions

View file

@ -17,6 +17,7 @@
package trie package trie
import ( import (
"bytes"
"fmt" "fmt"
"sync" "sync"
@ -101,6 +102,9 @@ func (h *hasher) hash(n node, force bool) []byte {
} }
} }
// encodeShortNode encodes the provided shortNode into the bytes. Notably, the
// return slice must be deep-copied explicitly, otherwise the underlying slice
// will be reused later.
func (h *hasher) encodeShortNode(n *shortNode) []byte { func (h *hasher) encodeShortNode(n *shortNode) []byte {
// Encode leaf node // Encode leaf node
if hasTerm(n.Key) { if hasTerm(n.Key) {
@ -127,8 +131,9 @@ var fnEncoderPool = sync.Pool{
}, },
} }
// encodeFullNode returns a copy of the supplied fullNode, with its child // encodeFullNode encodes the provided fullNode into the bytes. Notably, the
// being replaced by either the hash or an embedded node if the child is small. // return slice must be deep-copied explicitly, otherwise the underlying slice
// will be reused later.
func (h *hasher) encodeFullNode(n *fullNode) []byte { func (h *hasher) encodeFullNode(n *fullNode) []byte {
fn := fnEncoderPool.Get().(*fullnodeEncoder) fn := fnEncoderPool.Get().(*fullnodeEncoder)
fn.reset() fn.reset()
@ -181,7 +186,8 @@ func (h *hasher) encodedBytes() []byte {
return h.tmp return h.tmp
} }
// hashData hashes the provided data // hashData hashes the provided data. It is safe to modify the returned slice after
// the function returns.
func (h *hasher) hashData(data []byte) []byte { func (h *hasher) hashData(data []byte) []byte {
n := make([]byte, 32) n := make([]byte, 32)
h.sha.Reset() h.sha.Reset()
@ -200,12 +206,14 @@ 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. // Note, only resolved node (shortNode or fullNode) is expected for proofing.
//
// It is safe to modify the returned slice after the function returns.
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:
return h.encodeShortNode(n) return bytes.Clone(h.encodeShortNode(n))
case *fullNode: case *fullNode:
return h.encodeFullNode(n) return bytes.Clone(h.encodeFullNode(n))
default: default:
panic(fmt.Errorf("unexpected node type, %T", original)) panic(fmt.Errorf("unexpected node type, %T", original))
} }

View file

@ -242,7 +242,7 @@ func (it *nodeIterator) LeafProof() [][]byte {
// Gather nodes that end up as hash nodes (or the root) // Gather nodes that end up as hash nodes (or the root)
enc := hasher.proofHash(item.node) enc := hasher.proofHash(item.node)
if len(enc) >= 32 || i == 0 { if len(enc) >= 32 || i == 0 {
proofs = append(proofs, common.CopyBytes(enc)) proofs = append(proofs, enc)
} }
} }
return proofs return proofs