From 8572c36ee7fc2e53f8d1b40344298d9ac89bb5e8 Mon Sep 17 00:00:00 2001 From: Roy Crihfield Date: Mon, 6 Jun 2022 20:08:38 +0800 Subject: [PATCH] decrement path from restored iterator to cover node gaps --- pkg/snapshot/tracker.go | 1 + pkg/snapshot/util.go | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/pkg/snapshot/tracker.go b/pkg/snapshot/tracker.go index 8ca9b34..3bbdec6 100644 --- a/pkg/snapshot/tracker.go +++ b/pkg/snapshot/tracker.go @@ -139,6 +139,7 @@ func (tr *iteratorTracker) restore(tree state.Trie) ([]trie.NodeIterator, error) // Force the lower bound path to an even length if len(paths[0])&0b1 == 1 { + decrementPath(paths[0]) // decrement first to avoid skipped nodes paths[0] = append(paths[0], 0) } it := iter.NewPrefixBoundIterator(tree.NodeIterator(iter.HexToKeyBytes(paths[0])), paths[1]) diff --git a/pkg/snapshot/util.go b/pkg/snapshot/util.go index a2ca7ee..e9db47d 100644 --- a/pkg/snapshot/util.go +++ b/pkg/snapshot/util.go @@ -28,3 +28,26 @@ func NewPublisher(mode SnapshotMode, config *Config) (snapt.Publisher, error) { } return nil, fmt.Errorf("invalid snapshot mode: %s", mode) } + +// 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 + } + if allzero { + return false + } + for i := len(path) - 1; i >= 0; i-- { + val := path[i] + path[i]-- + if val == 0 { + path[i] = 0xf + } else { + return true + } + } + return true +}