mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-24 05:36:46 +00:00
* trie: fix two issues in trie iterator (#24539) * trie: fix memory leak in trie iterator In the trie iterator, live nodes are tracked in a stack while iterating. Popped node states should be explictly set to nil in order to get garbage-collected. * trie: fix empty trie iterator * fix lint * chore: auto version bump [bot] --------- Co-authored-by: rjl493456442 <garyrong0905@gmail.com> Co-authored-by: HAOYUatHZ <HAOYUatHZ@users.noreply.github.com>
This commit is contained in:
parent
bfe803001b
commit
760f234a1a
3 changed files with 22 additions and 9 deletions
|
|
@ -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]
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -29,6 +29,19 @@ import (
|
|||
"github.com/scroll-tech/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 }{
|
||||
|
|
|
|||
|
|
@ -25,7 +25,6 @@ import (
|
|||
|
||||
"github.com/scroll-tech/go-ethereum/common"
|
||||
"github.com/scroll-tech/go-ethereum/core/types"
|
||||
"github.com/scroll-tech/go-ethereum/crypto"
|
||||
"github.com/scroll-tech/go-ethereum/log"
|
||||
"github.com/scroll-tech/go-ethereum/rlp"
|
||||
)
|
||||
|
|
@ -33,9 +32,6 @@ import (
|
|||
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)
|
||||
)
|
||||
|
||||
// LeafCallback is a callback type invoked when a trie operation reaches a leaf
|
||||
|
|
|
|||
Loading…
Reference in a new issue