Compare commits
2 Commits
6bb6d4e27f
...
9201eca2b4
Author | SHA1 | Date | |
---|---|---|---|
9201eca2b4 | |||
9d890b2282 |
10
iterator.go
10
iterator.go
@ -40,9 +40,13 @@ func (it *PrefixBoundIterator) Next(descend bool) bool {
|
||||
if !it.NodeIterator.Next(descend) {
|
||||
return false
|
||||
}
|
||||
// stop if underlying iterator went past upper bound
|
||||
cmp := bytes.Compare(it.Path(), it.EndPath)
|
||||
return cmp <= 0
|
||||
// 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
|
||||
}
|
||||
|
||||
// NewPrefixBoundIterator returns an iterator with an upper bound value (hex path prefix)
|
||||
|
@ -87,12 +87,13 @@ func TestIterator(t *testing.T) {
|
||||
for b := uint(0); b < nbins; b++ {
|
||||
for it := iters[b]; it.Next(true); ix++ {
|
||||
if !bytes.Equal(allPaths[ix], it.Path()) {
|
||||
t.Fatalf("wrong path value\nexpected:\t%v\nactual:\t\t%v",
|
||||
allPaths[ix], it.Path())
|
||||
t.Fatalf("wrong path value in bin %d (index %d)\nexpected:\t%v\nactual:\t\t%v",
|
||||
b, ix, allPaths[ix], it.Path())
|
||||
}
|
||||
}
|
||||
// if the last node path was even-length, it will be duplicated
|
||||
if len(allPaths[ix-1])&0b1 == 0 {
|
||||
// if the last node path for the previous bin was even-length, the next iterator
|
||||
// will seek to the same node and it will be duplicated (see comment in Next()).
|
||||
if len(allPaths[ix-1])&1 == 0 {
|
||||
ix--
|
||||
}
|
||||
}
|
||||
|
@ -46,8 +46,8 @@ func (tr *Tracker) CaptureSignal(cancelCtx context.CancelFunc) {
|
||||
go func() {
|
||||
sig := <-sigChan
|
||||
log.Error("Signal received (%v), stopping", "signal", sig)
|
||||
// cancel context on receiving a signal
|
||||
// on ctx cancellation, all the iterators complete processing of their current node before stopping
|
||||
// Cancel context on receiving a signal. On cancellation, all tracked iterators complete
|
||||
// processing of their current node before stopping.
|
||||
cancelCtx()
|
||||
}()
|
||||
}
|
||||
@ -90,8 +90,9 @@ func (tr *Tracker) dump() error {
|
||||
return out.WriteAll(rows)
|
||||
}
|
||||
|
||||
// Restore attempts to read iterator state from file
|
||||
// if file doesn't exist, returns an empty slice with no error
|
||||
// Restore attempts to read iterator state from the recovery file.
|
||||
// If the file doesn't exist, returns an empty slice with no error.
|
||||
// Restored iterators are constructed in the same order as in the returned slice.
|
||||
func (tr *Tracker) Restore(makeIterator iter.IteratorConstructor) ([]trie.NodeIterator, error) {
|
||||
file, err := os.Open(tr.recoveryFile)
|
||||
if err != nil {
|
||||
@ -128,10 +129,9 @@ func (tr *Tracker) Restore(makeIterator iter.IteratorConstructor) ([]trie.NodeIt
|
||||
}
|
||||
|
||||
// force the lower bound path to an even length (required by geth API/HexToKeyBytes)
|
||||
if len(recoveredPath)&0b1 == 1 {
|
||||
// decrement first to avoid skipped nodes
|
||||
decrementPath(recoveredPath)
|
||||
recoveredPath = append(recoveredPath, 0)
|
||||
if len(recoveredPath)&1 == 1 {
|
||||
// to avoid skipped nodes, we must rewind by one index
|
||||
recoveredPath = rewindPath(recoveredPath)
|
||||
}
|
||||
it := makeIterator(iter.HexToKeyBytes(recoveredPath))
|
||||
boundIt := iter.NewPrefixBoundIterator(it, endPath)
|
||||
@ -184,25 +184,22 @@ func (it *Iterator) Next(descend bool) bool {
|
||||
return ret
|
||||
}
|
||||
|
||||
// Subtracts 1 from the last byte in a path slice, carrying if needed.
|
||||
// Does nothing, returning false, for all-zero inputs.
|
||||
func decrementPath(path []byte) bool {
|
||||
// check for all zeros
|
||||
allzero := true
|
||||
for i := 0; i < len(path); i++ {
|
||||
allzero = allzero && path[i] == 0
|
||||
// Rewinds to the path of the previous (pre-order) node:
|
||||
// If the last byte of the path is zero, pops it. Otherwise, decrements it
|
||||
// and pads with 0xF to 64 bytes (e.g. [1] => [0 f f f ...]).
|
||||
// Returns the passed path (which is also modified in place)
|
||||
func rewindPath(path []byte) []byte {
|
||||
if len(path) == 0 {
|
||||
return path
|
||||
}
|
||||
if allzero {
|
||||
return false
|
||||
if path[len(path)-1] == 0 {
|
||||
return path[:len(path)-1]
|
||||
}
|
||||
for i := len(path) - 1; i >= 0; i-- {
|
||||
val := path[i]
|
||||
path[i]--
|
||||
if val == 0 {
|
||||
path[i] = 0xf
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
path[len(path)-1]--
|
||||
padded := make([]byte, 64)
|
||||
i := copy(padded, path)
|
||||
for ; i < len(padded); i++ {
|
||||
padded[i] = 0xf
|
||||
}
|
||||
return true
|
||||
return padded
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user