Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix backward iterator seek #215

Merged
merged 6 commits into from
May 14, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
seek after range should be after exiting
  • Loading branch information
cheme committed May 13, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 6be3d22c9fd7b39b238178e445383ad93ae7d62f
6 changes: 3 additions & 3 deletions trie-db/src/iterator.rs
Original file line number Diff line number Diff line change
@@ -173,7 +173,7 @@ impl<L: TrieLayout> TrieDBRawIterator<L> {
NodePlan::Leaf { partial: partial_plan, .. } => {
let slice = partial_plan.build(node_data);
if (fwd && slice < partial) || (!fwd && slice > partial) {
crumb.status = Status::Exiting;
crumb.status = Status::AftExiting;
return Ok(false);
}
return Ok(slice.starts_with(&partial));
@@ -182,7 +182,7 @@ impl<L: TrieLayout> TrieDBRawIterator<L> {
let slice = partial_plan.build(node_data);
if !partial.starts_with(&slice) {
if (fwd && slice < partial) || (!fwd && slice > partial) {
crumb.status = Status::Exiting;
crumb.status = Status::AftExiting;
self.key_nibbles.append_partial(slice.right());
return Ok(false);
}
@@ -230,7 +230,7 @@ impl<L: TrieLayout> TrieDBRawIterator<L> {
let slice = partial_plan.build(node_data);
if !partial.starts_with(&slice) {
if (fwd && slice < partial) || (!fwd && slice > partial) {
crumb.status = Status::Exiting;
crumb.status = Status::AftExiting;
self.key_nibbles.append_partial(slice.right());
self.key_nibbles.push((nibble_ops::NIBBLE_LENGTH - 1) as u8);
return Ok(false);
20 changes: 16 additions & 4 deletions trie-db/test/src/double_ended_iterator.rs
Original file line number Diff line number Diff line change
@@ -201,10 +201,7 @@ fn seek_back_works_internal<T: TrieLayout>() {
let mut iter = TrieDBNodeDoubleEndedIterator::new(&trie).unwrap();

<dyn TrieDoubleEndedIterator<T, Item = _>>::seek(&mut iter, &hex!("")[..]).unwrap();
match iter.next_back() {
Some(Ok((prefix, _, _))) => assert_eq!(prefix, nibble_vec(hex!(""), 0)),
_ => panic!("unexpected item"),
}
assert!(iter.next_back().is_none());

<dyn TrieDoubleEndedIterator<T, Item = _>>::seek(&mut iter, &hex!("03")[..]).unwrap();
match iter.next_back() {
@@ -406,3 +403,18 @@ fn prefix_over_empty_works_internal<T: TrieLayout>() {
iter.prefix(&hex!("00")[..]).unwrap();
assert!(iter.next_back().is_none());
}

test_layouts!(next_back_weird_behaviour_1, next_back_weird_behaviour_internal_1);
fn next_back_weird_behaviour_internal_1<T: TrieLayout>() {
use trie_db::TrieIterator;

let pairs = vec![(vec![11], b"bbbb".to_vec())];

let (memdb, root) = build_trie_db::<T>(&pairs);
let trie = TrieDBBuilder::<T>::new(&memdb, &root).build();

let mut iter = trie_db::triedb::TrieDBDoubleEndedIterator::new(&trie).unwrap();

iter.seek(&[10]).unwrap();
assert!(iter.next_back().is_none());
}