diff options
author | Felix Lange <fjl@twurst.com> | 2017-04-18 19:37:10 +0800 |
---|---|---|
committer | Felix Lange <fjl@twurst.com> | 2017-04-25 08:14:31 +0800 |
commit | a13e920af01692cb07a520cda688f1cc5b5469dd (patch) | |
tree | 2321214787947ca0b4f302225aa2950617dd5cdd /trie | |
parent | f958d7d4822d257598ae36fc3b381040faa5bb30 (diff) | |
download | go-tangerine-a13e920af01692cb07a520cda688f1cc5b5469dd.tar.gz go-tangerine-a13e920af01692cb07a520cda688f1cc5b5469dd.tar.zst go-tangerine-a13e920af01692cb07a520cda688f1cc5b5469dd.zip |
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.
Diffstat (limited to 'trie')
-rw-r--r-- | trie/iterator.go | 15 | ||||
-rw-r--r-- | trie/iterator_test.go | 14 | ||||
-rw-r--r-- | trie/secure_trie.go | 6 | ||||
-rw-r--r-- | trie/sync_test.go | 2 | ||||
-rw-r--r-- | trie/trie.go | 4 | ||||
-rw-r--r-- | trie/trie_test.go | 2 |
6 files changed, 16 insertions, 27 deletions
diff --git a/trie/iterator.go b/trie/iterator.go index dd63a0c5a..fef5b2593 100644 --- a/trie/iterator.go +++ b/trie/iterator.go @@ -31,15 +31,8 @@ type Iterator struct { Value []byte // Current data value on which the iterator is positioned on } -// NewIterator creates a new key-value iterator. -func NewIterator(trie *Trie) *Iterator { - return &Iterator{ - nodeIt: NewNodeIterator(trie), - } -} - -// FromNodeIterator creates a new key-value iterator from a node iterator -func NewIteratorFromNodeIterator(it NodeIterator) *Iterator { +// NewIterator creates a new key-value iterator from a node iterator +func NewIterator(it NodeIterator) *Iterator { return &Iterator{ nodeIt: it, } @@ -99,8 +92,8 @@ type nodeIterator struct { path []byte // Path to the current node } -// NewNodeIterator creates an post-order trie iterator. -func NewNodeIterator(trie *Trie) NodeIterator { +// newNodeIterator creates an post-order trie iterator. +func newNodeIterator(trie *Trie) NodeIterator { if trie.Hash() == emptyState { return new(nodeIterator) } diff --git a/trie/iterator_test.go b/trie/iterator_test.go index c101bb7b0..04d51aaf5 100644 --- a/trie/iterator_test.go +++ b/trie/iterator_test.go @@ -42,7 +42,7 @@ func TestIterator(t *testing.T) { trie.Commit() found := make(map[string]string) - it := NewIterator(trie) + it := NewIterator(trie.NodeIterator()) for it.Next() { found[string(it.Key)] = string(it.Value) } @@ -72,7 +72,7 @@ func TestIteratorLargeData(t *testing.T) { vals[string(value2.k)] = value2 } - it := NewIterator(trie) + it := NewIterator(trie.NodeIterator()) for it.Next() { vals[string(it.Key)].t = true } @@ -99,7 +99,7 @@ func TestNodeIteratorCoverage(t *testing.T) { // Gather all the node hashes found by the iterator 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{}) { hashes[it.Hash()] = struct{}{} } @@ -154,8 +154,8 @@ func TestDifferenceIterator(t *testing.T) { trieb.Commit() found := make(map[string]string) - di, _ := NewDifferenceIterator(NewNodeIterator(triea), NewNodeIterator(trieb)) - it := NewIteratorFromNodeIterator(di) + di, _ := NewDifferenceIterator(triea.NodeIterator(), trieb.NodeIterator()) + it := NewIterator(di) for it.Next() { found[string(it.Key)] = string(it.Value) } @@ -189,8 +189,8 @@ func TestUnionIterator(t *testing.T) { } trieb.Commit() - di, _ := NewUnionIterator([]NodeIterator{NewNodeIterator(triea), NewNodeIterator(trieb)}) - it := NewIteratorFromNodeIterator(di) + di, _ := NewUnionIterator([]NodeIterator{triea.NodeIterator(), trieb.NodeIterator()}) + it := NewIterator(di) all := []struct{ k, v string }{ {"aardvark", "c"}, diff --git a/trie/secure_trie.go b/trie/secure_trie.go index 113fb6a1a..201716d18 100644 --- a/trie/secure_trie.go +++ b/trie/secure_trie.go @@ -156,12 +156,8 @@ func (t *SecureTrie) Root() []byte { return t.trie.Root() } -func (t *SecureTrie) Iterator() *Iterator { - return t.trie.Iterator() -} - 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. diff --git a/trie/sync_test.go b/trie/sync_test.go index acae039cd..6d345ad3f 100644 --- a/trie/sync_test.go +++ b/trie/sync_test.go @@ -80,7 +80,7 @@ func checkTrieConsistency(db Database, root common.Hash) error { if err != nil { return nil // // Consider a non existent state consistent } - it := NewNodeIterator(trie) + it := trie.NodeIterator() for it.Next(true) { } return it.Error() diff --git a/trie/trie.go b/trie/trie.go index e61bd0383..dbffc0ac3 100644 --- a/trie/trie.go +++ b/trie/trie.go @@ -126,8 +126,8 @@ func New(root common.Hash, db Database) (*Trie, error) { } // Iterator returns an iterator over all mappings in the trie. -func (t *Trie) Iterator() *Iterator { - return NewIterator(t) +func (t *Trie) NodeIterator() NodeIterator { + return newNodeIterator(t) } // Get returns the value for key stored in the trie. diff --git a/trie/trie_test.go b/trie/trie_test.go index 01ae3a4e7..cacb08824 100644 --- a/trie/trie_test.go +++ b/trie/trie_test.go @@ -439,7 +439,7 @@ func runRandTest(rt randTest) bool { tr = newtr case opItercheckhash: checktr, _ := New(common.Hash{}, nil) - it := tr.Iterator() + it := NewIterator(tr.NodeIterator()) for it.Next() { checktr.Update(it.Key, it.Value) } |