Patch for concurrent iterator & others (onto v1.11.6) #386

Closed
roysc wants to merge 1565 commits from v1.11.6-statediff-v5 into master
2 changed files with 22 additions and 5 deletions
Showing only changes of commit fb2ae8e995 - Show all commits

View File

@ -154,8 +154,11 @@ func (e seekError) Error() string {
} }
func newNodeIterator(trie *Trie, start []byte) NodeIterator { func newNodeIterator(trie *Trie, start []byte) NodeIterator {
if trie.Hash() == emptyState { if trie.Hash() == emptyRoot {
return new(nodeIterator) return &nodeIterator{
trie: trie,
err: errIteratorEnd,
}
} }
it := &nodeIterator{trie: trie} it := &nodeIterator{trie: trie}
it.err = it.seek(start) it.err = it.seek(start)
@ -425,7 +428,7 @@ func findChild(n *fullNode, index int, path []byte, ancestor common.Hash) (node,
func (it *nodeIterator) nextChild(parent *nodeIteratorState, ancestor common.Hash) (*nodeIteratorState, []byte, bool) { func (it *nodeIterator) nextChild(parent *nodeIteratorState, ancestor common.Hash) (*nodeIteratorState, []byte, bool) {
switch node := parent.node.(type) { switch node := parent.node.(type) {
case *fullNode: case *fullNode:
//Full node, move to the first non-nil child. // Full node, move to the first non-nil child.
if child, state, path, index := findChild(node, parent.index+1, it.path, ancestor); child != nil { if child, state, path, index := findChild(node, parent.index+1, it.path, ancestor); child != nil {
parent.index = index - 1 parent.index = index - 1
return state, path, true return state, path, true
@ -503,8 +506,9 @@ func (it *nodeIterator) push(state *nodeIteratorState, parentIndex *int, path []
} }
func (it *nodeIterator) pop() { func (it *nodeIterator) pop() {
parent := it.stack[len(it.stack)-1] last := it.stack[len(it.stack)-1]
it.path = it.path[:parent.pathlen] it.path = it.path[:last.pathlen]
it.stack[len(it.stack)-1] = nil
it.stack = it.stack[:len(it.stack)-1] it.stack = it.stack[:len(it.stack)-1]
} }

View File

@ -29,6 +29,19 @@ import (
"github.com/ethereum/go-ethereum/ethdb/memorydb" "github.com/ethereum/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) { func TestIterator(t *testing.T) {
trie := newEmpty() trie := newEmpty()
vals := []struct{ k, v string }{ vals := []struct{ k, v string }{