From c4f8785f6a119d96b5e13e9f4106c7aa2cfad63d Mon Sep 17 00:00:00 2001 From: panos Date: Fri, 5 Dec 2025 11:18:34 +0800 Subject: [PATCH] fix: fix two issues in trie iterator (ethereum/go-ethereum#24539) --- trie/iterator.go | 12 ++++++++---- trie/iterator_test.go | 13 +++++++++++++ trie/trie.go | 10 ++-------- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/trie/iterator.go b/trie/iterator.go index c6654d85a..13e409a2b 100644 --- a/trie/iterator.go +++ b/trie/iterator.go @@ -151,8 +151,11 @@ func (e seekError) Error() string { } func newNodeIterator(trie *Trie, start []byte) NodeIterator { - if trie.Hash() == emptyState { - return new(nodeIterator) + if trie.Hash() == emptyRoot { + return &nodeIterator{ + trie: trie, + err: errIteratorEnd, + } } it := &nodeIterator{trie: trie} it.err = it.seek(start) @@ -480,8 +483,9 @@ func (it *nodeIterator) push(state *nodeIteratorState, parentIndex *int, path [] } func (it *nodeIterator) pop() { - parent := it.stack[len(it.stack)-1] - it.path = it.path[:parent.pathlen] + last := it.stack[len(it.stack)-1] + it.path = it.path[:last.pathlen] + it.stack[len(it.stack)-1] = nil it.stack = it.stack[:len(it.stack)-1] } diff --git a/trie/iterator_test.go b/trie/iterator_test.go index 7cf046a63..a5ea3a2f6 100644 --- a/trie/iterator_test.go +++ b/trie/iterator_test.go @@ -29,6 +29,19 @@ import ( "github.com/morph-l2/go-ethereum/ethdb/memorydb" ) +func TestEmptyIterator(t *testing.T) { + trie := newEmpty() + iter := trie.NodeIterator(nil) + + seen := make(map[string]struct{}) + for iter.Next(true) { + seen[string(iter.Path())] = struct{}{} + } + if len(seen) != 0 { + t.Fatal("Unexpected trie node iterated") + } +} + func TestIterator(t *testing.T) { trie := newEmpty() vals := []struct{ k, v string }{ diff --git a/trie/trie.go b/trie/trie.go index 872b0f428..db975ab5c 100644 --- a/trie/trie.go +++ b/trie/trie.go @@ -25,18 +25,12 @@ import ( "github.com/morph-l2/go-ethereum/common" "github.com/morph-l2/go-ethereum/core/types" - "github.com/morph-l2/go-ethereum/crypto" "github.com/morph-l2/go-ethereum/log" "github.com/morph-l2/go-ethereum/rlp" ) -var ( - // emptyRoot is the known root hash of an empty trie. - emptyRoot = common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421") - - // emptyState is the known hash of an empty state trie entry. - emptyState = crypto.Keccak256Hash(nil) -) +// emptyRoot is the known root hash of an empty trie. +var emptyRoot = common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421") // LeafCallback is a callback type invoked when a trie operation reaches a leaf // node.