From 01d13de9d04e33e8c80856b92302ce3e29b21efd Mon Sep 17 00:00:00 2001 From: weiihann Date: Thu, 9 Apr 2026 15:06:49 +0800 Subject: [PATCH] trie/bintrie: use left/right in comments instead of children[i] --- trie/bintrie/binary_node.go | 2 +- trie/bintrie/internal_node.go | 4 ++-- trie/bintrie/internal_node_test.go | 16 ++++++++-------- trie/bintrie/iterator.go | 2 +- trie/bintrie/iterator_test.go | 2 +- trie/bintrie/stem_node_test.go | 8 ++++---- 6 files changed, 17 insertions(+), 17 deletions(-) diff --git a/trie/bintrie/binary_node.go b/trie/bintrie/binary_node.go index 7085f012c8..efb055426a 100644 --- a/trie/bintrie/binary_node.go +++ b/trie/bintrie/binary_node.go @@ -61,7 +61,7 @@ type BinaryNode interface { func SerializeNode(node BinaryNode) []byte { switch n := (node).(type) { 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 serialized[0] = nodeTypeInternal copy(serialized[1:33], n.children[0].Hash().Bytes()) diff --git a/trie/bintrie/internal_node.go b/trie/bintrie/internal_node.go index cdef032f68..98c828829e 100644 --- a/trie/bintrie/internal_node.go +++ b/trie/bintrie/internal_node.go @@ -58,7 +58,7 @@ func keyToPath(depth int, key []byte) ([]byte, error) { // InternalNode is a binary trie internal node. type InternalNode struct { - children [2]BinaryNode // children[0] = left, children[1] = right + children [2]BinaryNode // 0: left, 1: right depth int 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: - // 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 // for narrow state updates that touch a single path through the trie). if bt.depth < parallelDepth() && isDirty(bt.children[0]) && isDirty(bt.children[1]) { diff --git a/trie/bintrie/internal_node_test.go b/trie/bintrie/internal_node_test.go index ceb9263cd4..ba85dca50a 100644 --- a/trie/bintrie/internal_node_test.go +++ b/trie/bintrie/internal_node_test.go @@ -138,10 +138,10 @@ func TestInternalNodeInsert(t *testing.T) { 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) 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 @@ -149,10 +149,10 @@ func TestInternalNodeInsert(t *testing.T) { 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) 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 copiedLeft, ok := copiedInternal.children[0].(*StemNode) 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) 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) @@ -333,10 +333,10 @@ func TestInternalNodeInsertValuesAtStem(t *testing.T) { 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) 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]) { diff --git a/trie/bintrie/iterator.go b/trie/bintrie/iterator.go index 616280d51b..8c4d98ef75 100644 --- a/trie/bintrie/iterator.go +++ b/trie/bintrie/iterator.go @@ -64,7 +64,7 @@ func (it *binaryNodeIterator) Next(descend bool) bool { switch node := it.current.(type) { 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] for context.Index < 2 { diff --git a/trie/bintrie/iterator_test.go b/trie/bintrie/iterator_test.go index 22a3fd1b80..497c814564 100644 --- a/trie/bintrie/iterator_test.go +++ b/trie/bintrie/iterator_test.go @@ -167,7 +167,7 @@ func TestIteratorHashedNodeNilData(t *testing.T) { 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 // triggers the nil-data guard in the iterator. root.children[1] = HashedNode(common.Hash{}) diff --git a/trie/bintrie/stem_node_test.go b/trie/bintrie/stem_node_test.go index 0056d9a850..dc5a74e4dc 100644 --- a/trie/bintrie/stem_node_test.go +++ b/trie/bintrie/stem_node_test.go @@ -102,19 +102,19 @@ func TestStemNodeInsertDifferentStem(t *testing.T) { 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) 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) { 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) 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]) { t.Errorf("Right stem mismatch")