trie/bintrie: use left/right in comments instead of children[i]

This commit is contained in:
weiihann 2026-04-09 15:06:49 +08:00
parent 92e9074c5b
commit 01d13de9d0
6 changed files with 17 additions and 17 deletions

View file

@ -61,7 +61,7 @@ type BinaryNode interface {
func SerializeNode(node BinaryNode) []byte { func SerializeNode(node BinaryNode) []byte {
switch n := (node).(type) { switch n := (node).(type) {
case *InternalNode: case *InternalNode:
// InternalNode: 1 byte type + 32 bytes children[0] hash + 32 bytes children[1] hash // InternalNode: 1 byte type + 32 bytes left hash + 32 bytes right hash
var serialized [NodeTypeBytes + HashSize + HashSize]byte var serialized [NodeTypeBytes + HashSize + HashSize]byte
serialized[0] = nodeTypeInternal serialized[0] = nodeTypeInternal
copy(serialized[1:33], n.children[0].Hash().Bytes()) copy(serialized[1:33], n.children[0].Hash().Bytes())

View file

@ -58,7 +58,7 @@ func keyToPath(depth int, key []byte) ([]byte, error) {
// InternalNode is a binary trie internal node. // InternalNode is a binary trie internal node.
type InternalNode struct { type InternalNode struct {
children [2]BinaryNode // children[0] = left, children[1] = right children [2]BinaryNode // 0: left, 1: right
depth int depth int
mustRecompute bool // true if the hash needs to be recomputed mustRecompute bool // true if the hash needs to be recomputed
@ -125,7 +125,7 @@ func (bt *InternalNode) Hash() common.Hash {
} }
// At shallow depths, parallelize when both children need rehashing: // At shallow depths, parallelize when both children need rehashing:
// hash children[0] in a goroutine, children[1] inline, then combine. // hash left subtree in a goroutine, right subtree inline, then combine.
// Skip goroutine overhead when only one child is dirty (common case // Skip goroutine overhead when only one child is dirty (common case
// for narrow state updates that touch a single path through the trie). // for narrow state updates that touch a single path through the trie).
if bt.depth < parallelDepth() && isDirty(bt.children[0]) && isDirty(bt.children[1]) { if bt.depth < parallelDepth() && isDirty(bt.children[0]) && isDirty(bt.children[1]) {

View file

@ -138,10 +138,10 @@ func TestInternalNodeInsert(t *testing.T) {
t.Fatalf("Expected InternalNode, got %T", newNode) t.Fatalf("Expected InternalNode, got %T", newNode)
} }
// Check that children[0] is now a StemNode // Check that left child is now a StemNode
leftStem, ok := internalNode.children[0].(*StemNode) leftStem, ok := internalNode.children[0].(*StemNode)
if !ok { if !ok {
t.Fatalf("Expected children[0] to be StemNode, got %T", internalNode.children[0]) t.Fatalf("Expected left child to be StemNode, got %T", internalNode.children[0])
} }
// Check the inserted value // Check the inserted value
@ -149,10 +149,10 @@ func TestInternalNodeInsert(t *testing.T) {
t.Errorf("Value mismatch: expected %x, got %x", leftValue, leftStem.Values[10]) t.Errorf("Value mismatch: expected %x, got %x", leftValue, leftStem.Values[10])
} }
// children[1] should still be Empty // Right child should still be Empty
_, ok = internalNode.children[1].(Empty) _, ok = internalNode.children[1].(Empty)
if !ok { if !ok {
t.Errorf("Expected children[1] to remain Empty, got %T", internalNode.children[1]) t.Errorf("Expected right child to remain Empty, got %T", internalNode.children[1])
} }
} }
@ -194,12 +194,12 @@ func TestInternalNodeCopy(t *testing.T) {
// Check that children are copied // Check that children are copied
copiedLeft, ok := copiedInternal.children[0].(*StemNode) copiedLeft, ok := copiedInternal.children[0].(*StemNode)
if !ok { if !ok {
t.Fatalf("Expected children[0] to be StemNode, got %T", copiedInternal.children[0]) t.Fatalf("Expected left child to be StemNode, got %T", copiedInternal.children[0])
} }
copiedRight, ok := copiedInternal.children[1].(*StemNode) copiedRight, ok := copiedInternal.children[1].(*StemNode)
if !ok { if !ok {
t.Fatalf("Expected children[1] to be StemNode, got %T", copiedInternal.children[1]) t.Fatalf("Expected right child to be StemNode, got %T", copiedInternal.children[1])
} }
// Verify deep copy (children should be different objects) // Verify deep copy (children should be different objects)
@ -333,10 +333,10 @@ func TestInternalNodeInsertValuesAtStem(t *testing.T) {
t.Fatalf("Expected InternalNode, got %T", newNode) t.Fatalf("Expected InternalNode, got %T", newNode)
} }
// Check that children[0] is now a StemNode with the values // Check that left child is now a StemNode with the values
leftStem, ok := internalNode.children[0].(*StemNode) leftStem, ok := internalNode.children[0].(*StemNode)
if !ok { if !ok {
t.Fatalf("Expected children[0] to be StemNode, got %T", internalNode.children[0]) t.Fatalf("Expected left child to be StemNode, got %T", internalNode.children[0])
} }
if !bytes.Equal(leftStem.Values[5], values[5]) { if !bytes.Equal(leftStem.Values[5], values[5]) {

View file

@ -64,7 +64,7 @@ func (it *binaryNodeIterator) Next(descend bool) bool {
switch node := it.current.(type) { switch node := it.current.(type) {
case *InternalNode: case *InternalNode:
// index: 0 = nothing visited, 1=children[0] visited, 2=children[1] visited // index: 0 = nothing visited, 1=left visited, 2=right visited
context := &it.stack[len(it.stack)-1] context := &it.stack[len(it.stack)-1]
for context.Index < 2 { for context.Index < 2 {

View file

@ -167,7 +167,7 @@ func TestIteratorHashedNodeNilData(t *testing.T) {
t.Fatalf("expected InternalNode root, got %T", tr.root) t.Fatalf("expected InternalNode root, got %T", tr.root)
} }
// Replace children[1] with a zero-hash HashedNode. nodeResolver // Replace right child with a zero-hash HashedNode. nodeResolver
// short-circuits on common.Hash{} and returns (nil, nil), which // short-circuits on common.Hash{} and returns (nil, nil), which
// triggers the nil-data guard in the iterator. // triggers the nil-data guard in the iterator.
root.children[1] = HashedNode(common.Hash{}) root.children[1] = HashedNode(common.Hash{})

View file

@ -102,19 +102,19 @@ func TestStemNodeInsertDifferentStem(t *testing.T) {
t.Errorf("Expected depth 0, got %d", internalNode.depth) t.Errorf("Expected depth 0, got %d", internalNode.depth)
} }
// Original stem should be at children[0] (bit 0) // Original stem should be on the left (bit 0)
leftStem, ok := internalNode.children[0].(*StemNode) leftStem, ok := internalNode.children[0].(*StemNode)
if !ok { if !ok {
t.Fatalf("Expected children[0] to be StemNode, got %T", internalNode.children[0]) t.Fatalf("Expected left child to be StemNode, got %T", internalNode.children[0])
} }
if !bytes.Equal(leftStem.Stem, stem1) { if !bytes.Equal(leftStem.Stem, stem1) {
t.Errorf("Left stem mismatch") t.Errorf("Left stem mismatch")
} }
// New stem should be at children[1] (bit 1) // New stem should be on the right (bit 1)
rightStem, ok := internalNode.children[1].(*StemNode) rightStem, ok := internalNode.children[1].(*StemNode)
if !ok { if !ok {
t.Fatalf("Expected children[1] to be StemNode, got %T", internalNode.children[1]) t.Fatalf("Expected right child to be StemNode, got %T", internalNode.children[1])
} }
if !bytes.Equal(rightStem.Stem, key[:31]) { if !bytes.Equal(rightStem.Stem, key[:31]) {
t.Errorf("Right stem mismatch") t.Errorf("Right stem mismatch")