Skip to content

Conversation

@mediocregopher
Copy link
Collaborator

The InMemoryTrieOverlay::next method was previously very broken, in that it did not ever descend into lower parts of the trie. For example a given current key of 0xA, and subsequent keys of 0xA0 and 0xB, next was returning 0xB. This was due to incorrect usage of the Nibbles::increment method.

Fixing this issue involved essentially rewriting the entire type. The new implementation includes proptests to ensure that it functions correctly.

Some extra debug checks have also been added to ensure that the overlay is actually being used correctly.

The `InMemoryTrieOverlay::next` method was previously very broken, in
that it did not ever descend into lower parts of the trie. For example
a given current key of `0xA`, and subsequent keys of `0xA0` and `0xB`,
`next` was returning `0xB`. This was due to incorrect usage of the
`Nibbles::increment` method.

Fixing this issue involved essentially rewriting the entire type. The
new implementation includes proptests to ensure that it functions
correctly.

Some extra debug checks have also been added to ensure that the overlay
is actually being used correctly.
@github-project-automation github-project-automation bot moved this to Backlog in Reth Tracker Oct 24, 2025
@mediocregopher mediocregopher added C-bug An unexpected or incorrect behavior A-db Related to the database A-trie Related to Merkle Patricia Trie implementation labels Oct 24, 2025
@codspeed-hq
Copy link

codspeed-hq bot commented Oct 24, 2025

CodSpeed Performance Report

Merging #19277 will not alter performance

Comparing mediocregopher/in-mem-trie-overlay-fix (d288552) with main (be73e4a)

Summary

✅ 77 untouched

Copy link
Member

@Rjected Rjected left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The methods seem equivalent to the old behavior, except now we always iterate in lexicographic order, the tests are also nice

Comment on lines 236 to 250
// If either cursor is currently pointing to the last entry which was returned then consume
// that entry so that `next_inner` is looking at the subsequent one.
if let Some((key, _)) = self.in_memory_cursor.current() &&
key == &last_key
{
self.in_memory_cursor.first_after(&last_key);
}

if let Some((key, _)) = &self.cursor_entry &&
key == &last_key
{
self.cursor_next()?;
}

let entry = self.next_inner()?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, I wonder if there's a better name for next_inner because in this method, the cursors are mostly being advanced here by the cursor_next / first_after calls (except in the case of removed nodes).

@github-project-automation github-project-automation bot moved this from Backlog to In Progress in Reth Tracker Oct 24, 2025
0x00101010 pushed a commit to 0x00101010/reth that referenced this pull request Oct 26, 2025
Comment on lines +119 to +121
// If already seeked to the given key then don't do anything. Also if we're seeked past
// the given key then don't anything, because `TrieCursor` is specifically a
// forward-only cursor.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would guess seeking to a key less than the current key should not happen because it will return an incorrect result, can we add a debug assert for that here?

@mediocregopher mediocregopher added this pull request to the merge queue Oct 27, 2025
Merged via the queue into main with commit 7e59141 Oct 27, 2025
42 checks passed
@mediocregopher mediocregopher deleted the mediocregopher/in-mem-trie-overlay-fix branch October 27, 2025 15:34
@github-project-automation github-project-automation bot moved this from In Progress to Done in Reth Tracker Oct 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-db Related to the database A-trie Related to Merkle Patricia Trie implementation C-bug An unexpected or incorrect behavior

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants