From 7d25dfe6f8f7cf30151e85b83e43a5d76a7082e3 Mon Sep 17 00:00:00 2001 From: Roy Crihfield Date: Wed, 2 Aug 2023 20:49:14 +0800 Subject: [PATCH] node iterator seek: compare paths without terminator byte --- trie/iterator.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/trie/iterator.go b/trie/iterator.go index 83ccc0740f..214db1c292 100644 --- a/trie/iterator.go +++ b/trie/iterator.go @@ -311,7 +311,7 @@ func (it *nodeIterator) seek(prefix []byte) error { return errIteratorEnd } else if err != nil { return seekError{prefix, err} - } else if bytes.Compare(path, key) >= 0 { + } else if reachedPath(path, key) { return nil } it.push(state, parentIndex, path) @@ -554,6 +554,15 @@ func (it *nodeIterator) pop() { it.putInPool(last) } +// reachedPath normalizes a path by truncating a terminator if present, and returns true if it is +// greater than or equal to the target. +func reachedPath(path, target []byte) bool { + if hasTerm(path) { + path = path[:len(path)-1] + } + return bytes.Compare(path, target) >= 0 +} + func compareNodes(a, b NodeIterator) int { if cmp := bytes.Compare(a.Path(), b.Path()); cmp != 0 { return cmp