-
Notifications
You must be signed in to change notification settings - Fork 199
/
iterator.go
81 lines (66 loc) · 1.66 KB
/
iterator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package trie
import (
"github.com/ElrondNetwork/elrond-go-core/core/check"
"github.com/ElrondNetwork/elrond-go/common"
)
type iterator struct {
currentNode node
nextNodes []node
db common.DBWriteCacher
}
// NewIterator creates a new instance of trie iterator
func NewIterator(trie common.Trie) (*iterator, error) {
if check.IfNil(trie) {
return nil, ErrNilTrie
}
pmt, ok := trie.(*patriciaMerkleTrie)
if !ok {
return nil, ErrWrongTypeAssertion
}
trieStorage := trie.GetStorageManager()
nextNodes, err := pmt.root.getChildren(trieStorage.Database())
if err != nil {
return nil, err
}
return &iterator{
currentNode: pmt.root,
nextNodes: nextNodes,
db: trieStorage.Database(),
}, nil
}
// HasNext returns true if there is a next node
func (it *iterator) HasNext() bool {
return len(it.nextNodes) > 0
}
// Next moves the iterator to the next node
func (it *iterator) Next() error {
n := it.nextNodes[0]
err := n.isEmptyOrNil()
if err != nil {
return ErrNilNode
}
it.currentNode = n
nextChildren, err := it.currentNode.getChildren(it.db)
if err != nil {
return err
}
it.nextNodes = append(it.nextNodes, nextChildren...)
it.nextNodes = it.nextNodes[1:]
return nil
}
// MarshalizedNode marshalizes the current node, and then returns the serialized node
func (it *iterator) MarshalizedNode() ([]byte, error) {
err := it.currentNode.setHash()
if err != nil {
return nil, err
}
return it.currentNode.getEncodedNode()
}
// GetHash returns the current node hash
func (it *iterator) GetHash() ([]byte, error) {
err := it.currentNode.setHash()
if err != nil {
return nil, err
}
return it.currentNode.getHash(), nil
}