concurrent iterator: pass a constructor method instead of state.Trie

This allows us to use our alt trie implementation
This commit is contained in:
Roy Crihfield 2023-05-16 17:24:02 +08:00
parent 3555790e76
commit 79a7643a28
3 changed files with 9 additions and 6 deletions

View File

@ -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

View File

@ -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++ {

View File

@ -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))
}