aboutsummaryrefslogtreecommitdiffstats
path: root/ethtrie/iterator.go
blob: 43f49741607a93a1182794577a47a32a0f7ef1ac (plain) (blame)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package ethtrie

import (
    "bytes"

    "github.com/ethereum/go-ethereum/ethutil"
)

type NodeType byte

const (
    EmptyNode NodeType = iota
    BranchNode
    LeafNode
    ExtNode
)

func getType(node *ethutil.Value) NodeType {
    if node.Len() == 0 {
        return EmptyNode
    }

    if node.Len() == 2 {
        k := CompactDecode(node.Get(0).Str())
        if HasTerm(k) {
            return LeafNode
        }

        return ExtNode
    }

    return BranchNode
}

type Iterator struct {
    Path [][]byte
    trie *Trie

    Key   []byte
    Value *ethutil.Value
}

func NewIterator(trie *Trie) *Iterator {
    return &Iterator{trie: trie}
}

func (self *Iterator) key(node *ethutil.Value, path [][]byte) []byte {
    switch getType(node) {
    case LeafNode:
        k := RemTerm(CompactDecode(node.Get(0).Str()))

        self.Path = append(path, k)
        self.Value = node.Get(1)

        return k
    case BranchNode:
        if node.Get(16).Len() > 0 {
            return []byte{16}
        }

        for i := byte(0); i < 16; i++ {
            o := self.key(self.trie.getNode(node.Get(int(i)).Raw()), append(path, []byte{i}))
            if o != nil {
                return append([]byte{i}, o...)
            }
        }
    case ExtNode:
        currKey := node.Get(0).Bytes()

        return self.key(self.trie.getNode(node.Get(1).Raw()), append(path, currKey))
    }

    return nil
}

func (self *Iterator) next(node *ethutil.Value, key []byte, path [][]byte) []byte {
    switch typ := getType(node); typ {
    case EmptyNode:
        return nil
    case BranchNode:
        if len(key) > 0 {
            subNode := self.trie.getNode(node.Get(int(key[0])).Raw())

            o := self.next(subNode, key[1:], append(path, key[:1]))
            if o != nil {
                return append([]byte{key[0]}, o...)
            }
        }

        var r byte = 0
        if len(key) > 0 {
            r = key[0] + 1
        }

        for i := r; i < 16; i++ {
            subNode := self.trie.getNode(node.Get(int(i)).Raw())
            o := self.key(subNode, append(path, []byte{i}))
            if o != nil {
                return append([]byte{i}, o...)
            }
        }
    case LeafNode, ExtNode:
        k := RemTerm(CompactDecode(node.Get(0).Str()))
        if typ == LeafNode {
            if bytes.Compare([]byte(k), []byte(key)) > 0 {
                self.Value = node.Get(1)
                self.Path = append(path, k)

                return k
            }
        } else {
            subNode := self.trie.getNode(node.Get(1).Raw())
            subKey := key[len(k):]
            var ret []byte
            if BeginsWith(key, k) {
                ret = self.next(subNode, subKey, append(path, k))
            } else if bytes.Compare(k, key[:len(k)]) > 0 {
                ret = self.key(node, append(path, k))
            } else {
                ret = nil
            }

            if ret != nil {
                return append(k, ret...)
            }
        }
    }

    return nil
}

// Get the next in keys
func (self *Iterator) Next(key string) []byte {
    self.trie.mut.Lock()
    defer self.trie.mut.Unlock()

    k := RemTerm(CompactHexDecode(key))
    n := self.next(self.trie.getNode(self.trie.Root), k, nil)

    self.Key = []byte(DecodeCompact(n))

    return self.Key
}