From 79a7643a2896577f6ae3ca3a357ade4b0a2dcd3b Mon Sep 17 00:00:00 2001 From: Roy Crihfield Date: Tue, 16 May 2023 17:24:02 +0800 Subject: [PATCH] concurrent iterator: pass a constructor method instead of state.Trie This allows us to use our alt trie implementation --- trie/concurrent_iterator/iterator.go | 8 ++++++-- trie/concurrent_iterator/iterator_test.go | 2 +- trie/concurrent_iterator/tracker/tracker.go | 5 ++--- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/trie/concurrent_iterator/iterator.go b/trie/concurrent_iterator/iterator.go index de015ad87..5bd5f7f59 100644 --- a/trie/concurrent_iterator/iterator.go +++ b/trie/concurrent_iterator/iterator.go @@ -31,6 +31,10 @@ type PrefixBoundIterator struct { EndPath []byte } +// IteratorConstructor is a constructor returning a NodeIterator, which is used to decouple this +// code from the trie implementation. +type IteratorConstructor = func(startKey []byte) trie.NodeIterator + func (it *PrefixBoundIterator) Next(descend bool) bool { if it.EndPath == nil { return it.NodeIterator.Next(descend) @@ -130,10 +134,10 @@ func eachPrefixRange(prefix []byte, nbins uint, callback func([]byte, []byte)) { } // SubtrieIterators cuts a trie by path prefix, returning `nbins` iterators covering its subtries -func SubtrieIterators(tree state.Trie, nbins uint) []trie.NodeIterator { +func SubtrieIterators(makeIterator IteratorConstructor, nbins uint) []trie.NodeIterator { var iters []trie.NodeIterator eachPrefixRange(nil, nbins, func(from []byte, to []byte) { - it := tree.NodeIterator(HexToKeyBytes(from)) + it := makeIterator(HexToKeyBytes(from)) iters = append(iters, NewPrefixBoundIterator(it, from, to)) }) return iters diff --git a/trie/concurrent_iterator/iterator_test.go b/trie/concurrent_iterator/iterator_test.go index de3080ff8..b39e60e25 100644 --- a/trie/concurrent_iterator/iterator_test.go +++ b/trie/concurrent_iterator/iterator_test.go @@ -82,7 +82,7 @@ func TestIterator(t *testing.T) { allPaths := fixt.Block1_Paths cases := []uint{1, 2, 4, 8, 16, 32} runCase := func(t *testing.T, nbins uint) { - iters := iter.SubtrieIterators(tree, nbins) + iters := iter.SubtrieIterators(tree.NodeIterator, nbins) ix := 0 for b := uint(0); b < nbins; b++ { for it := iters[b]; it.Next(true); ix++ { diff --git a/trie/concurrent_iterator/tracker/tracker.go b/trie/concurrent_iterator/tracker/tracker.go index 0ee5dad80..7c7029048 100644 --- a/trie/concurrent_iterator/tracker/tracker.go +++ b/trie/concurrent_iterator/tracker/tracker.go @@ -9,7 +9,6 @@ import ( "os/signal" "syscall" - "github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/trie" @@ -122,7 +121,7 @@ func (tr *Tracker) dump() error { // Restore attempts to read iterator state from file // if file doesn't exist, returns an empty slice with no error -func (tr *Tracker) Restore(tree state.Trie) ([]trie.NodeIterator, error) { +func (tr *Tracker) Restore(makeIterator iter.IteratorConstructor) ([]trie.NodeIterator, error) { file, err := os.Open(tr.recoveryFile) if err != nil { if os.IsNotExist(err) { @@ -169,7 +168,7 @@ func (tr *Tracker) Restore(tree state.Trie) ([]trie.NodeIterator, error) { decrementPath(startPath) startPath = append(startPath, 0) } - it := iter.NewPrefixBoundIterator(tree.NodeIterator(iter.HexToKeyBytes(startPath)), startPath, endPath) + it := iter.NewPrefixBoundIterator(makeIterator(iter.HexToKeyBytes(startPath)), startPath, endPath) ret = append(ret, tr.Tracked(it, recoveredPath)) }