trie: clean up iterator constructors

Make it so each iterator has exactly one public constructor:

- NodeIterators can be created through a method.
- Iterators can be created through NewIterator on any NodeIterator.
This commit is contained in:
Felix Lange 2017-04-18 13:37:10 +02:00
parent f958d7d482
commit a13e920af0
9 changed files with 21 additions and 31 deletions

View File

@ -22,6 +22,7 @@ import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/trie"
) )
type DumpAccount struct { type DumpAccount struct {
@ -44,7 +45,7 @@ func (self *StateDB) RawDump() Dump {
Accounts: make(map[string]DumpAccount), Accounts: make(map[string]DumpAccount),
} }
it := self.trie.Iterator() it := trie.NewIterator(self.trie.NodeIterator())
for it.Next() { for it.Next() {
addr := self.trie.GetKey(it.Key) addr := self.trie.GetKey(it.Key)
var data Account var data Account
@ -61,7 +62,7 @@ func (self *StateDB) RawDump() Dump {
Code: common.Bytes2Hex(obj.Code(self.db)), Code: common.Bytes2Hex(obj.Code(self.db)),
Storage: make(map[string]string), Storage: make(map[string]string),
} }
storageIt := obj.getTrie(self.db).Iterator() storageIt := trie.NewIterator(obj.getTrie(self.db).NodeIterator())
for storageIt.Next() { for storageIt.Next() {
account.Storage[common.Bytes2Hex(self.trie.GetKey(storageIt.Key))] = common.Bytes2Hex(storageIt.Value) account.Storage[common.Bytes2Hex(self.trie.GetKey(storageIt.Key))] = common.Bytes2Hex(storageIt.Value)
} }

View File

@ -118,7 +118,7 @@ func (it *NodeIterator) step() error {
if err != nil { if err != nil {
return err return err
} }
it.dataIt = trie.NewNodeIterator(dataTrie) it.dataIt = dataTrie.NodeIterator()
if !it.dataIt.Next(true) { if !it.dataIt.Next(true) {
it.dataIt = nil it.dataIt = nil
} }

View File

@ -481,7 +481,7 @@ func (db *StateDB) ForEachStorage(addr common.Address, cb func(key, value common
cb(h, value) cb(h, value)
} }
it := so.getTrie(db.db).Iterator() it := trie.NewIterator(so.getTrie(db.db).NodeIterator())
for it.Next() { for it.Next() {
// ignore cached values // ignore cached values
key := common.BytesToHash(db.trie.GetKey(it.Key)) key := common.BytesToHash(db.trie.GetKey(it.Key))

View File

@ -31,15 +31,8 @@ type Iterator struct {
Value []byte // Current data value on which the iterator is positioned on Value []byte // Current data value on which the iterator is positioned on
} }
// NewIterator creates a new key-value iterator. // NewIterator creates a new key-value iterator from a node iterator
func NewIterator(trie *Trie) *Iterator { func NewIterator(it NodeIterator) *Iterator {
return &Iterator{
nodeIt: NewNodeIterator(trie),
}
}
// FromNodeIterator creates a new key-value iterator from a node iterator
func NewIteratorFromNodeIterator(it NodeIterator) *Iterator {
return &Iterator{ return &Iterator{
nodeIt: it, nodeIt: it,
} }
@ -99,8 +92,8 @@ type nodeIterator struct {
path []byte // Path to the current node path []byte // Path to the current node
} }
// NewNodeIterator creates an post-order trie iterator. // newNodeIterator creates an post-order trie iterator.
func NewNodeIterator(trie *Trie) NodeIterator { func newNodeIterator(trie *Trie) NodeIterator {
if trie.Hash() == emptyState { if trie.Hash() == emptyState {
return new(nodeIterator) return new(nodeIterator)
} }

View File

@ -42,7 +42,7 @@ func TestIterator(t *testing.T) {
trie.Commit() trie.Commit()
found := make(map[string]string) found := make(map[string]string)
it := NewIterator(trie) it := NewIterator(trie.NodeIterator())
for it.Next() { for it.Next() {
found[string(it.Key)] = string(it.Value) found[string(it.Key)] = string(it.Value)
} }
@ -72,7 +72,7 @@ func TestIteratorLargeData(t *testing.T) {
vals[string(value2.k)] = value2 vals[string(value2.k)] = value2
} }
it := NewIterator(trie) it := NewIterator(trie.NodeIterator())
for it.Next() { for it.Next() {
vals[string(it.Key)].t = true vals[string(it.Key)].t = true
} }
@ -99,7 +99,7 @@ func TestNodeIteratorCoverage(t *testing.T) {
// Gather all the node hashes found by the iterator // Gather all the node hashes found by the iterator
hashes := make(map[common.Hash]struct{}) hashes := make(map[common.Hash]struct{})
for it := NewNodeIterator(trie); it.Next(true); { for it := trie.NodeIterator(); it.Next(true); {
if it.Hash() != (common.Hash{}) { if it.Hash() != (common.Hash{}) {
hashes[it.Hash()] = struct{}{} hashes[it.Hash()] = struct{}{}
} }
@ -154,8 +154,8 @@ func TestDifferenceIterator(t *testing.T) {
trieb.Commit() trieb.Commit()
found := make(map[string]string) found := make(map[string]string)
di, _ := NewDifferenceIterator(NewNodeIterator(triea), NewNodeIterator(trieb)) di, _ := NewDifferenceIterator(triea.NodeIterator(), trieb.NodeIterator())
it := NewIteratorFromNodeIterator(di) it := NewIterator(di)
for it.Next() { for it.Next() {
found[string(it.Key)] = string(it.Value) found[string(it.Key)] = string(it.Value)
} }
@ -189,8 +189,8 @@ func TestUnionIterator(t *testing.T) {
} }
trieb.Commit() trieb.Commit()
di, _ := NewUnionIterator([]NodeIterator{NewNodeIterator(triea), NewNodeIterator(trieb)}) di, _ := NewUnionIterator([]NodeIterator{triea.NodeIterator(), trieb.NodeIterator()})
it := NewIteratorFromNodeIterator(di) it := NewIterator(di)
all := []struct{ k, v string }{ all := []struct{ k, v string }{
{"aardvark", "c"}, {"aardvark", "c"},

View File

@ -156,12 +156,8 @@ func (t *SecureTrie) Root() []byte {
return t.trie.Root() return t.trie.Root()
} }
func (t *SecureTrie) Iterator() *Iterator {
return t.trie.Iterator()
}
func (t *SecureTrie) NodeIterator() NodeIterator { func (t *SecureTrie) NodeIterator() NodeIterator {
return NewNodeIterator(&t.trie) return t.trie.NodeIterator()
} }
// CommitTo writes all nodes and the secure hash pre-images to the given database. // CommitTo writes all nodes and the secure hash pre-images to the given database.

View File

@ -80,7 +80,7 @@ func checkTrieConsistency(db Database, root common.Hash) error {
if err != nil { if err != nil {
return nil // // Consider a non existent state consistent return nil // // Consider a non existent state consistent
} }
it := NewNodeIterator(trie) it := trie.NodeIterator()
for it.Next(true) { for it.Next(true) {
} }
return it.Error() return it.Error()

View File

@ -126,8 +126,8 @@ func New(root common.Hash, db Database) (*Trie, error) {
} }
// Iterator returns an iterator over all mappings in the trie. // Iterator returns an iterator over all mappings in the trie.
func (t *Trie) Iterator() *Iterator { func (t *Trie) NodeIterator() NodeIterator {
return NewIterator(t) return newNodeIterator(t)
} }
// Get returns the value for key stored in the trie. // Get returns the value for key stored in the trie.

View File

@ -439,7 +439,7 @@ func runRandTest(rt randTest) bool {
tr = newtr tr = newtr
case opItercheckhash: case opItercheckhash:
checktr, _ := New(common.Hash{}, nil) checktr, _ := New(common.Hash{}, nil)
it := tr.Iterator() it := NewIterator(tr.NodeIterator())
for it.Next() { for it.Next() {
checktr.Update(it.Key, it.Value) checktr.Update(it.Key, it.Value)
} }