2020-08-24 00:23:16 +00:00
|
|
|
//
|
|
|
|
// 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"
|
2020-08-31 16:06:03 +00:00
|
|
|
"math/bits"
|
2020-08-24 00:23:16 +00:00
|
|
|
|
2020-08-31 02:59:15 +00:00
|
|
|
"github.com/ethereum/go-ethereum/trie"
|
2020-08-24 00:23:16 +00:00
|
|
|
)
|
|
|
|
|
2023-09-26 11:34:41 +00:00
|
|
|
// IteratorConstructor is a constructor returning a NodeIterator, which is used to decouple this
|
|
|
|
// code from the trie implementation.
|
2024-03-15 04:53:34 +00:00
|
|
|
type IteratorConstructor = func(startKey []byte) (trie.NodeIterator, error)
|
2023-09-26 11:34:41 +00:00
|
|
|
|
2022-02-23 10:40:37 +00:00
|
|
|
// PrefixBoundIterator is a NodeIterator constrained by a lower & upper bound (as hex path prefixes)
|
2020-09-06 07:28:36 +00:00
|
|
|
type PrefixBoundIterator struct {
|
2022-02-18 05:35:07 +00:00
|
|
|
trie.NodeIterator
|
2023-09-26 11:34:41 +00:00
|
|
|
StartPath, EndPath []byte
|
2020-08-24 00:23:16 +00:00
|
|
|
}
|
|
|
|
|
2023-09-26 11:34:41 +00:00
|
|
|
// 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}
|
|
|
|
}
|
2023-09-19 16:58:23 +00:00
|
|
|
|
2020-09-06 07:28:36 +00:00
|
|
|
func (it *PrefixBoundIterator) Next(descend bool) bool {
|
2022-05-31 14:31:56 +00:00
|
|
|
if it.EndPath == nil {
|
|
|
|
return it.NodeIterator.Next(descend)
|
2020-08-24 00:23:16 +00:00
|
|
|
}
|
2022-02-18 05:35:07 +00:00
|
|
|
if !it.NodeIterator.Next(descend) {
|
2020-08-24 00:23:16 +00:00
|
|
|
return false
|
|
|
|
}
|
2023-09-19 16:58:23 +00:00
|
|
|
// 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
|
2020-08-24 00:23:16 +00:00
|
|
|
}
|
|
|
|
|
2023-09-26 11:34:41 +00:00
|
|
|
func (it *PrefixBoundIterator) Bounds() ([]byte, []byte) {
|
|
|
|
return it.StartPath, it.EndPath
|
2020-08-24 00:23:16 +00:00
|
|
|
}
|
|
|
|
|
2022-02-23 10:40:37 +00:00
|
|
|
// generates nibble slice prefixes at uniform intervals
|
2020-08-31 16:06:03 +00:00
|
|
|
type prefixGenerator struct {
|
2020-09-06 17:55:48 +00:00
|
|
|
current []byte
|
|
|
|
step byte
|
2020-08-31 16:06:03 +00:00
|
|
|
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++ {
|
2022-02-18 05:35:07 +00:00
|
|
|
divisor := byte(nbins & 0xF)
|
2020-08-31 16:06:03 +00:00
|
|
|
if divisor != 0 {
|
|
|
|
step = 0x10 / divisor
|
|
|
|
}
|
|
|
|
nbins = nbins >> 4
|
|
|
|
}
|
|
|
|
return prefixGenerator{
|
2020-09-06 17:55:48 +00:00
|
|
|
current: make([]byte, stepIndex),
|
|
|
|
step: step,
|
|
|
|
stepIndex: stepIndex - 1,
|
2020-08-31 16:06:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gen *prefixGenerator) Value() []byte {
|
|
|
|
return gen.current
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gen *prefixGenerator) HasNext() bool {
|
2022-02-18 05:35:07 +00:00
|
|
|
return gen.current[0] < 0x10
|
2020-08-31 16:06:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (gen *prefixGenerator) Next() {
|
2022-02-18 05:35:07 +00:00
|
|
|
// increment the cursor, and handle overflow
|
2020-08-31 16:06:03 +00:00
|
|
|
gen.current[gen.stepIndex] += gen.step
|
|
|
|
overflow := false
|
|
|
|
for ix := 0; ix < len(gen.current); ix++ {
|
2022-01-13 01:57:23 +00:00
|
|
|
rix := len(gen.current) - 1 - ix // index in prefix is reverse
|
|
|
|
if overflow { // apply overflow
|
2020-08-31 16:06:03 +00:00
|
|
|
gen.current[rix]++
|
|
|
|
overflow = false
|
|
|
|
}
|
2022-01-13 01:57:23 +00:00
|
|
|
// detect overflow at this index
|
2020-08-31 16:06:03 +00:00
|
|
|
if rix != 0 && gen.current[rix] > 0xf {
|
|
|
|
gen.current[rix] = 0
|
|
|
|
overflow = true
|
|
|
|
}
|
2020-08-24 00:23:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-19 16:58:23 +00:00
|
|
|
// MakePaths generates paths that cut trie domain into `nbins` uniform conterminous bins (w/ opt. prefix)
|
2020-09-01 03:22:40 +00:00
|
|
|
// eg. MakePaths([], 2) => [[0] [8]]
|
2023-09-19 16:58:23 +00:00
|
|
|
// MakePaths([4], 32) => [[4 0 0] [4 0 8] [4 1 0]... [4 f 8]]
|
2020-08-31 16:06:03 +00:00
|
|
|
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)
|
2020-08-24 00:23:16 +00:00
|
|
|
}
|
2020-08-31 16:06:03 +00:00
|
|
|
return res
|
2020-08-24 00:23:16 +00:00
|
|
|
}
|
|
|
|
|
2024-03-15 04:53:34 +00:00
|
|
|
func eachPrefixRange(prefix []byte, nbins uint, callback func([]byte, []byte) error) error {
|
2022-05-31 14:31:56 +00:00
|
|
|
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)
|
|
|
|
}
|
2024-03-15 04:53:34 +00:00
|
|
|
err := callback(key, prefixes[i+1])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-02-21 08:30:47 +00:00
|
|
|
}
|
2024-03-15 04:53:34 +00:00
|
|
|
return nil
|
2022-02-21 08:30:47 +00:00
|
|
|
}
|
|
|
|
|
2023-09-19 16:58:23 +00:00
|
|
|
// SubtrieIterators cuts a trie by path prefix, returning `nbins` iterators covering its subtries
|
2024-03-15 04:53:34 +00:00
|
|
|
func SubtrieIterators(makeIterator IteratorConstructor, nbins uint) ([]trie.NodeIterator, error) {
|
2020-09-06 07:30:02 +00:00
|
|
|
var iters []trie.NodeIterator
|
2024-03-15 04:53:34 +00:00
|
|
|
err := eachPrefixRange(nil, nbins, func(from []byte, to []byte) error {
|
|
|
|
it, err := makeIterator(HexToKeyBytes(from))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-19 16:58:23 +00:00
|
|
|
iters = append(iters, NewPrefixBoundIterator(it, to))
|
2024-03-15 04:53:34 +00:00
|
|
|
return nil
|
2020-09-06 07:30:02 +00:00
|
|
|
})
|
2024-03-15 04:53:34 +00:00
|
|
|
return iters, err
|
2020-08-24 00:23:16 +00:00
|
|
|
}
|