-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterator.go
158 lines (142 loc) · 4.86 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//
// Copyright © 2020 Vulcanize, Inc
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package iterator
import (
"bytes"
"math/bits"
"github.com/ethereum/go-ethereum/trie"
)
// IteratorConstructor is a constructor returning a NodeIterator, which is used to decouple this
// code from the trie implementation.
type IteratorConstructor = func(startKey []byte) (trie.NodeIterator, error)
// PrefixBoundIterator is a NodeIterator constrained by a lower & upper bound (as hex path prefixes)
type PrefixBoundIterator struct {
trie.NodeIterator
StartPath, EndPath []byte
}
// NewPrefixBoundIterator returns an iterator with an upper bound value (hex path prefix)
func NewPrefixBoundIterator(it trie.NodeIterator, to []byte) *PrefixBoundIterator {
return &PrefixBoundIterator{NodeIterator: it, StartPath: it.Path(), EndPath: to}
}
func (it *PrefixBoundIterator) Next(descend bool) bool {
if it.EndPath == nil {
return it.NodeIterator.Next(descend)
}
if !it.NodeIterator.Next(descend) {
return false
}
// Stop if underlying iterator went past upper bound.
// Note: this results in a single node of overlap between binned iterators. The more correct
// behavior would be to make this a strict less-than, so that iterators cover mutually disjoint
// subtries. Unfortunately, the NodeIterator constructor takes a compact path, meaning
// odd-length paths must be padded with a 0, so e.g. [8] becomes [8, 0], which means we would
// skip [8]. So, we use <= here to cover that node for the "next" bin.
return bytes.Compare(it.Path(), it.EndPath) <= 0
}
func (it *PrefixBoundIterator) Bounds() ([]byte, []byte) {
return it.StartPath, it.EndPath
}
// generates nibble slice prefixes at uniform intervals
type prefixGenerator struct {
current []byte
step byte
stepIndex uint
}
func newPrefixGenerator(nbins uint) prefixGenerator {
if bits.OnesCount(nbins) != 1 {
panic("nbins must be a power of 2")
}
// determine step dist. and path index at which to step
var step byte
var stepIndex uint
for ; nbins != 0; stepIndex++ {
divisor := byte(nbins & 0xF)
if divisor != 0 {
step = 0x10 / divisor
}
nbins = nbins >> 4
}
return prefixGenerator{
current: make([]byte, stepIndex),
step: step,
stepIndex: stepIndex - 1,
}
}
func (gen *prefixGenerator) Value() []byte {
return gen.current
}
func (gen *prefixGenerator) HasNext() bool {
return gen.current[0] < 0x10
}
func (gen *prefixGenerator) Next() {
// increment the cursor, and handle overflow
gen.current[gen.stepIndex] += gen.step
overflow := false
for ix := 0; ix < len(gen.current); ix++ {
rix := len(gen.current) - 1 - ix // index in prefix is reverse
if overflow { // apply overflow
gen.current[rix]++
overflow = false
}
// detect overflow at this index
if rix != 0 && gen.current[rix] > 0xf {
gen.current[rix] = 0
overflow = true
}
}
}
// MakePaths generates paths that cut trie domain into `nbins` uniform conterminous bins (w/ opt. prefix)
// eg. MakePaths([], 2) => [[0] [8]]
// MakePaths([4], 32) => [[4 0 0] [4 0 8] [4 1 0]... [4 f 8]]
func MakePaths(prefix []byte, nbins uint) [][]byte {
var res [][]byte
for it := newPrefixGenerator(nbins); it.HasNext(); it.Next() {
next := make([]byte, len(prefix))
copy(next, prefix)
next = append(next, it.Value()...)
res = append(res, next)
}
return res
}
func eachPrefixRange(prefix []byte, nbins uint, callback func([]byte, []byte) error) error {
prefixes := MakePaths(prefix, nbins)
prefixes = append(prefixes, nil) // include tail
prefixes[0] = nil // set bin 0 left bound to nil to include root
for i := 0; i < len(prefixes)-1; i++ {
key := prefixes[i]
if len(key)%2 != 0 { // zero-pad for odd-length keys
key = append(key, 0)
}
err := callback(key, prefixes[i+1])
if err != nil {
return err
}
}
return nil
}
// SubtrieIterators cuts a trie by path prefix, returning `nbins` iterators covering its subtries
func SubtrieIterators(makeIterator IteratorConstructor, nbins uint) ([]trie.NodeIterator, error) {
var iters []trie.NodeIterator
err := eachPrefixRange(nil, nbins, func(from []byte, to []byte) error {
it, err := makeIterator(HexToKeyBytes(from))
if err != nil {
return err
}
iters = append(iters, NewPrefixBoundIterator(it, to))
return nil
})
return iters, err
}