diff --git a/crates/trie/trie/src/node_iter.rs b/crates/trie/trie/src/node_iter.rs index 77eef810225..37cd8b295d0 100644 --- a/crates/trie/trie/src/node_iter.rs +++ b/crates/trie/trie/src/node_iter.rs @@ -277,12 +277,10 @@ where // `can_skip_node` is already set when the hash flag is set, so we don't need // to check for the hash flag explicitly. // - // It is possible that the branch node at the key `seek_key` is not stored in - // the database, so the walker will advance to the branch node after it. Because - // of this, we need to check that the current walker key has a prefix of the key - // that we seeked to. + // A deeper stored key may leave unvisited hashed entries between the seek + // prefix and the walker, so only skip the seek at the exact prefix. if can_skip_node && - self.walker.key().is_some_and(|key| key.starts_with(&seek_prefix)) && + self.walker.key().is_some_and(|key| key == &seek_prefix) && self.walker.children_are_in_trie() { trace!( @@ -307,21 +305,26 @@ where #[cfg(test)] mod tests { - use super::{TrieElement, TrieNodeIter}; + use super::*; use crate::{ hashed_cursor::{ - mock::MockHashedCursorFactory, noop::NoopHashedCursor, HashedCursorFactory, - HashedPostStateCursor, + mock::MockHashedCursorFactory, + noop::{NoopHashedCursor, NoopHashedCursorFactory}, + HashedCursorFactory, HashedPostStateCursor, HashedPostStateCursorFactory, }, mock::{KeyVisit, KeyVisitType}, trie_cursor::{ - mock::MockTrieCursorFactory, noop::NoopAccountTrieCursor, TrieCursorFactory, + mock::MockTrieCursorFactory, + noop::{NoopAccountTrieCursor, NoopTrieCursorFactory}, + InMemoryTrieCursorFactory, TrieCursorFactory, }, walker::TrieWalker, + StateRoot, }; use alloy_primitives::{ b256, map::{B256Map, HashMap}, + B256, U256, }; use alloy_trie::{ BranchNodeCompact, HashBuilder, Nibbles, TrieAccount, TrieMask, EMPTY_ROOT_HASH, @@ -329,8 +332,8 @@ mod tests { use itertools::Itertools; use reth_primitives_traits::Account; use reth_trie_common::{ - prefix_set::PrefixSetMut, updates::TrieUpdates, BranchNode, HashedPostState, LeafNode, - RlpNode, + prefix_set::PrefixSetMut, updates::TrieUpdates, BranchNode, HashedPostState, HashedStorage, + LeafNode, RlpNode, }; use std::collections::BTreeMap; @@ -380,6 +383,71 @@ mod tests { trie_updates.account_nodes } + #[test] + fn inserted_leaf_before_deeper_stored_branch_is_included() { + fn slot(prefix: &[u8]) -> B256 { + let mut bytes = [0; 32]; + bytes[..prefix.len()].copy_from_slice(prefix); + B256::from(bytes) + } + + let address = B256::repeat_byte(0xaa); + let account = || Account { nonce: 1, ..Default::default() }; + let old_slots = [ + slot(&[0x00]), + slot(&[0x0f, 0x00]), + slot(&[0x0f, 0x10]), + slot(&[0x10, 0xa0, 0x00]), + slot(&[0x10, 0xa0, 0x01]), + slot(&[0x10, 0xa0, 0x10]), + slot(&[0x10, 0xa1]), + ]; + let inserted = slot(&[0x10, 0x00]); + let old_state = + HashedPostState::default().with_accounts([(address, Some(account()))]).with_storages([ + (address, HashedStorage::from_iter(old_slots.map(|slot| (slot, U256::from(1))))), + ]); + let old_sorted = old_state.clone().into_sorted(); + let (old_root, old_updates) = StateRoot::new( + NoopTrieCursorFactory::default(), + HashedPostStateCursorFactory::new(NoopHashedCursorFactory::default(), &old_sorted), + ) + .with_prefix_sets(old_state.construct_prefix_sets().freeze()) + .root_with_updates() + .unwrap(); + + let new_state = HashedPostState::default() + .with_accounts([(address, Some(account()))]) + .with_storages([( + address, + HashedStorage::from_iter( + old_slots.into_iter().chain([inserted]).map(|slot| (slot, U256::from(1))), + ), + )]); + let delta = HashedPostState::default() + .with_accounts([(address, Some(account()))]) + .with_storages([(address, HashedStorage::from_iter([(inserted, U256::from(1))]))]); + let new_sorted = new_state.clone().into_sorted(); + let rebuilt_root = StateRoot::new( + NoopTrieCursorFactory::default(), + HashedPostStateCursorFactory::new(NoopHashedCursorFactory::default(), &new_sorted), + ) + .with_prefix_sets(new_state.construct_prefix_sets().freeze()) + .root() + .unwrap(); + let old_trie = old_updates.into_sorted(); + let incremental_root = StateRoot::new( + InMemoryTrieCursorFactory::new(NoopTrieCursorFactory::default(), &old_trie), + HashedPostStateCursorFactory::new(NoopHashedCursorFactory::default(), &new_sorted), + ) + .with_prefix_sets(delta.construct_prefix_sets().freeze()) + .root() + .unwrap(); + + assert_ne!(old_root, rebuilt_root); + assert_eq!(incremental_root, rebuilt_root); + } + #[test] fn test_trie_node_iter() { fn empty_leaf_rlp_for_key(key: Nibbles) -> RlpNode {