Skip to main content

reth_trie/hashed_cursor/
post_state.rs

1use super::{HashedCursor, HashedCursorFactory, HashedStorageCursor};
2use crate::forward_cursor::ForwardInMemoryCursor;
3use alloy_primitives::{B256, U256};
4use reth_primitives_traits::Account;
5use reth_storage_errors::db::DatabaseError;
6use reth_trie_common::HashedPostStateSorted;
7
8/// The hashed cursor factory for the post state.
9#[derive(Clone, Debug)]
10pub struct HashedPostStateCursorFactory<CF, T> {
11    cursor_factory: CF,
12    post_state: T,
13}
14
15impl<CF, T> HashedPostStateCursorFactory<CF, T> {
16    /// Create a new factory.
17    pub const fn new(cursor_factory: CF, post_state: T) -> Self {
18        Self { cursor_factory, post_state }
19    }
20}
21
22impl<'overlay, CF, T> HashedCursorFactory for HashedPostStateCursorFactory<CF, &'overlay T>
23where
24    CF: HashedCursorFactory,
25    T: AsRef<HashedPostStateSorted>,
26{
27    type AccountCursor<'cursor>
28        = HashedPostStateCursor<'overlay, CF::AccountCursor<'cursor>, Option<Account>>
29    where
30        Self: 'cursor;
31    type StorageCursor<'cursor>
32        = HashedPostStateCursor<'overlay, CF::StorageCursor<'cursor>, U256>
33    where
34        Self: 'cursor;
35
36    fn hashed_account_cursor(&self) -> Result<Self::AccountCursor<'_>, DatabaseError> {
37        let cursor = self.cursor_factory.hashed_account_cursor()?;
38        Ok(HashedPostStateCursor::new_account(cursor, self.post_state.as_ref()))
39    }
40
41    fn hashed_storage_cursor(
42        &self,
43        hashed_address: B256,
44    ) -> Result<Self::StorageCursor<'_>, DatabaseError> {
45        let post_state = self.post_state.as_ref();
46        let cursor = self.cursor_factory.hashed_storage_cursor(hashed_address)?;
47        Ok(HashedPostStateCursor::new_storage(cursor, post_state, hashed_address))
48    }
49}
50
51/// Trait for types that can be used with [`HashedPostStateCursor`] as a value.
52///
53/// This enables uniform handling of deletions across different wrapper types:
54/// - `Option<Account>`: `None` indicates deletion
55/// - `U256`: `U256::ZERO` indicates deletion (maps to `None`)
56///
57/// This design allows us to use `U256::ZERO`, rather than an Option, to indicate deletion for
58/// storage (which maps cleanly to how changesets are stored in the DB) while not requiring two
59/// different cursor implementations.
60pub trait HashedPostStateCursorValue: Copy {
61    /// The non-zero type returned by `into_option`.
62    /// For `Option<Account>`, this is `Account`.
63    /// For `U256`, this is `U256`.
64    type NonZero: Copy + std::fmt::Debug;
65
66    /// Returns `Some(&NonZero)` if the value is present, `None` if deleted.
67    fn into_option(self) -> Option<Self::NonZero>;
68}
69
70impl HashedPostStateCursorValue for Option<Account> {
71    type NonZero = Account;
72
73    fn into_option(self) -> Option<Self::NonZero> {
74        self
75    }
76}
77
78impl HashedPostStateCursorValue for U256 {
79    type NonZero = Self;
80
81    fn into_option(self) -> Option<Self::NonZero> {
82        (!self.is_zero()).then_some(self)
83    }
84}
85
86/// A cursor to iterate over state updates and corresponding database entries.
87/// It will always give precedence to the data from the post state updates.
88#[derive(Debug)]
89pub struct HashedPostStateCursor<'a, C, V>
90where
91    V: HashedPostStateCursorValue,
92{
93    /// The underlying cursor.
94    cursor: C,
95    /// Tracks whether the DB cursor is available, positioned, or exhausted.
96    db_cursor_state: DbCursorState<V::NonZero>,
97    /// Forward-only in-memory cursor over underlying V.
98    post_state_cursor: ForwardInMemoryCursor<'a, B256, V>,
99    /// The last hashed key that was returned by the cursor.
100    /// De facto, this is a current cursor position.
101    last_key: Option<B256>,
102    #[cfg(debug_assertions)]
103    /// Tracks whether `seek` has been called.
104    seeked: bool,
105    /// Reference to the full post state.
106    post_state: &'a HashedPostStateSorted,
107}
108
109#[derive(Debug)]
110enum DbCursorState<V> {
111    NeedsPosition,
112    Positioned((B256, V)),
113    Exhausted,
114}
115
116impl<V> DbCursorState<V> {
117    const fn entry(&self) -> Option<&(B256, V)> {
118        match self {
119            Self::Positioned(entry) => Some(entry),
120            Self::NeedsPosition | Self::Exhausted => None,
121        }
122    }
123
124    fn set_entry(&mut self, entry: Option<(B256, V)>) {
125        *self = match entry {
126            Some(entry) => Self::Positioned(entry),
127            None => Self::Exhausted,
128        };
129    }
130}
131
132impl<'a, C> HashedPostStateCursor<'a, C, Option<Account>>
133where
134    C: HashedCursor<Value = Account>,
135{
136    /// Create new account cursor which combines a DB cursor and the post state.
137    pub fn new_account(cursor: C, post_state: &'a HashedPostStateSorted) -> Self {
138        let post_state_cursor = ForwardInMemoryCursor::new(&post_state.accounts);
139        Self {
140            cursor,
141            db_cursor_state: DbCursorState::NeedsPosition,
142            post_state_cursor,
143            last_key: None,
144            #[cfg(debug_assertions)]
145            seeked: false,
146            post_state,
147        }
148    }
149}
150
151impl<'a, C> HashedPostStateCursor<'a, C, U256>
152where
153    C: HashedStorageCursor<Value = U256>,
154{
155    /// Create new storage cursor with full post state reference.
156    /// This allows the cursor to switch between storage tries when `set_hashed_address` is called.
157    pub fn new_storage(
158        cursor: C,
159        post_state: &'a HashedPostStateSorted,
160        hashed_address: B256,
161    ) -> Self {
162        let post_state_cursor = Self::get_storage_overlay(post_state, hashed_address);
163        Self {
164            cursor,
165            db_cursor_state: DbCursorState::NeedsPosition,
166            post_state_cursor,
167            last_key: None,
168            #[cfg(debug_assertions)]
169            seeked: false,
170            post_state,
171        }
172    }
173
174    /// Returns the storage overlay for `hashed_address`.
175    fn get_storage_overlay(
176        post_state: &'a HashedPostStateSorted,
177        hashed_address: B256,
178    ) -> ForwardInMemoryCursor<'a, B256, U256> {
179        let post_state_storage = post_state.storages.get(&hashed_address);
180        let storage_slots = post_state_storage.map(|u| u.storage_slots_ref()).unwrap_or(&[]);
181
182        ForwardInMemoryCursor::new(storage_slots)
183    }
184}
185
186impl<'a, C, V> HashedPostStateCursor<'a, C, V>
187where
188    C: HashedCursor<Value = V::NonZero>,
189    V: HashedPostStateCursorValue,
190{
191    const fn get_cursor_mut(&mut self) -> &mut C {
192        &mut self.cursor
193    }
194
195    /// Asserts that the next entry to be returned from the cursor is not previous to the last entry
196    /// returned.
197    fn set_last_key(&mut self, next_entry: &Option<(B256, V::NonZero)>) {
198        let next_key = next_entry.as_ref().map(|e| e.0);
199        debug_assert!(
200            self.last_key.is_none_or(|last| next_key.is_none_or(|next| next >= last)),
201            "Cannot return entry {:?} previous to the last returned entry at {:?}",
202            next_key,
203            self.last_key,
204        );
205        self.last_key = next_key;
206    }
207
208    /// Positions the DB cursor state using the underlying cursor when needed.
209    fn cursor_seek(&mut self, key: B256) -> Result<(), DatabaseError> {
210        // Only seek if:
211        // 1. We have a cursor entry and need to seek forward (entry.0 < key), OR
212        // 2. The DB cursor needs to be positioned.
213        let should_seek = match &self.db_cursor_state {
214            DbCursorState::NeedsPosition => true,
215            DbCursorState::Positioned((entry_key, _)) => entry_key < &key,
216            DbCursorState::Exhausted => false,
217        };
218
219        if should_seek {
220            let entry = self.get_cursor_mut().seek(key)?;
221            self.db_cursor_state.set_entry(entry);
222        }
223
224        Ok(())
225    }
226
227    /// Advances the DB cursor state to the subsequent entry using the underlying cursor.
228    fn cursor_next(&mut self) -> Result<(), DatabaseError> {
229        #[cfg(debug_assertions)]
230        {
231            debug_assert!(self.seeked);
232        }
233
234        // Exhausted DB state is stable; only advance when the DB cursor is positioned at an entry.
235        if matches!(self.db_cursor_state, DbCursorState::Positioned(_)) {
236            let entry = self.get_cursor_mut().next()?;
237            self.db_cursor_state.set_entry(entry);
238        }
239
240        Ok(())
241    }
242
243    /// Compares the current in-memory entry with the current entry of the cursor, and applies the
244    /// in-memory entry to the cursor entry as an overlay.
245    ///
246    /// This may consume and move forward the current entries when the overlay indicates a removed
247    /// node.
248    fn choose_next_entry(&mut self) -> Result<Option<(B256, V::NonZero)>, DatabaseError> {
249        loop {
250            let post_state_current =
251                self.post_state_cursor.current().copied().map(|(k, v)| (k, v.into_option()));
252            let db_entry = self.db_cursor_state.entry();
253
254            match (post_state_current, db_entry) {
255                (Some((mem_key, None)), _)
256                    if db_entry.is_none_or(|(db_key, _)| &mem_key < db_key) =>
257                {
258                    // If overlay has a removed value but DB cursor is exhausted or ahead of the
259                    // in-memory cursor then move ahead in-memory, as there might be further
260                    // non-removed overlay values.
261                    self.post_state_cursor.first_after(&mem_key);
262                }
263                (Some((mem_key, None)), Some((db_key, _))) if &mem_key == db_key => {
264                    // If overlay has a removed value which is returned from DB then move both
265                    // cursors ahead to the next key.
266                    self.post_state_cursor.first_after(&mem_key);
267                    self.cursor_next()?;
268                }
269                (Some((mem_key, Some(value))), _)
270                    if db_entry.is_none_or(|(db_key, _)| &mem_key <= db_key) =>
271                {
272                    // If overlay returns a value prior to the DB's value, or the DB is exhausted,
273                    // then we return the overlay's value.
274                    return Ok(Some((mem_key, value)))
275                }
276                // All other cases:
277                // - mem_key > db_key
278                // - overlay is exhausted
279                // Return the db_entry. If DB is also exhausted then this returns None.
280                _ => return Ok(db_entry.copied()),
281            }
282        }
283    }
284}
285
286impl<C, V> HashedCursor for HashedPostStateCursor<'_, C, V>
287where
288    C: HashedCursor<Value = V::NonZero>,
289    V: HashedPostStateCursorValue,
290{
291    type Value = V::NonZero;
292
293    /// Seek the next entry for a given hashed key.
294    ///
295    /// If the post state contains the exact match for the key, return it.
296    /// Otherwise, retrieve the next entries that are greater than or equal to the key from the
297    /// database and the post state. The two entries are compared and the lowest is returned.
298    ///
299    /// The returned account key is memoized and the cursor remains positioned at that key until
300    /// [`HashedCursor::seek`] or [`HashedCursor::next`] are called.
301    fn seek(&mut self, key: B256) -> Result<Option<(B256, Self::Value)>, DatabaseError> {
302        let post_state_entry =
303            self.post_state_cursor.seek(&key).copied().map(|(k, v)| (k, v.into_option()));
304
305        if let Some((mem_key, Some(value))) = post_state_entry &&
306            mem_key == key
307        {
308            #[cfg(debug_assertions)]
309            {
310                self.seeked = true;
311            }
312
313            // An exact overlay hit is the first logical entry at or after `key`, so the DB cursor
314            // can stay lazy until a later operation needs it.
315            if matches!(&self.db_cursor_state, DbCursorState::Positioned((db_key, _)) if db_key < &key)
316            {
317                self.db_cursor_state = DbCursorState::NeedsPosition;
318            }
319
320            let entry = Some((key, value));
321            self.set_last_key(&entry);
322            return Ok(entry)
323        }
324
325        self.cursor_seek(key)?;
326
327        #[cfg(debug_assertions)]
328        {
329            self.seeked = true;
330        }
331
332        let entry = self.choose_next_entry()?;
333        self.set_last_key(&entry);
334        Ok(entry)
335    }
336
337    /// Retrieve the next entry from the cursor.
338    ///
339    /// If the cursor is positioned at the entry, return the entry with next greater key.
340    /// Returns [None] if the previous memoized or the next greater entries are missing.
341    ///
342    /// NOTE: This function will not return any entry unless [`HashedCursor::seek`] has been called.
343    fn next(&mut self) -> Result<Option<(B256, Self::Value)>, DatabaseError> {
344        #[cfg(debug_assertions)]
345        {
346            debug_assert!(self.seeked, "Cursor must be seek'd before next is called");
347        }
348
349        // A `last_key` of `None` indicates that the cursor is exhausted.
350        let Some(last_key) = self.last_key else {
351            return Ok(None);
352        };
353
354        // If either cursor is currently pointing to the last entry which was returned then consume
355        // that entry so that `choose_next_entry` is looking at the subsequent one.
356        if let Some((key, _)) = self.post_state_cursor.current() &&
357            key == &last_key
358        {
359            self.post_state_cursor.first_after(&last_key);
360        }
361
362        if matches!(self.db_cursor_state, DbCursorState::NeedsPosition) {
363            self.cursor_seek(last_key)?;
364        }
365
366        if let Some((key, _)) = self.db_cursor_state.entry() &&
367            key == &last_key
368        {
369            self.cursor_next()?;
370        }
371
372        let entry = self.choose_next_entry()?;
373        self.set_last_key(&entry);
374        Ok(entry)
375    }
376
377    fn reset(&mut self) {
378        let Self { cursor, db_cursor_state, post_state_cursor, last_key, .. } = self;
379
380        cursor.reset();
381        post_state_cursor.reset();
382
383        *db_cursor_state = DbCursorState::NeedsPosition;
384        *last_key = None;
385        #[cfg(debug_assertions)]
386        {
387            self.seeked = false;
388        }
389    }
390}
391
392/// The cursor to iterate over post state hashed values and corresponding database entries.
393/// It will always give precedence to the data from the post state.
394impl<C> HashedStorageCursor for HashedPostStateCursor<'_, C, U256>
395where
396    C: HashedStorageCursor<Value = U256>,
397{
398    /// Returns `true` if the account has no storage entries.
399    ///
400    /// This function should be called before attempting to call [`HashedCursor::seek`] or
401    /// [`HashedCursor::next`].
402    fn is_storage_empty(&mut self) -> Result<bool, DatabaseError> {
403        let is_empty = self.seek(B256::ZERO)?.is_none();
404        self.reset();
405        Ok(is_empty)
406    }
407
408    fn set_hashed_address(&mut self, hashed_address: B256) {
409        self.reset();
410        self.cursor.set_hashed_address(hashed_address);
411        let post_state_cursor =
412            HashedPostStateCursor::<C, U256>::get_storage_overlay(self.post_state, hashed_address);
413        self.post_state_cursor = post_state_cursor;
414        self.db_cursor_state = DbCursorState::NeedsPosition;
415    }
416}
417
418#[cfg(test)]
419mod tests {
420    use super::*;
421    use crate::hashed_cursor::mock::MockHashedCursor;
422    use parking_lot::Mutex;
423    use std::{collections::BTreeMap, sync::Arc};
424
425    fn key(byte: u8) -> B256 {
426        B256::repeat_byte(byte)
427    }
428
429    fn storage_post_state(storage_slots: Vec<(B256, U256)>) -> HashedPostStateSorted {
430        let storage_sorted = reth_trie_common::HashedStorageSorted { storage_slots };
431        let mut storages = alloy_primitives::map::B256Map::default();
432        storages.insert(B256::ZERO, storage_sorted);
433        HashedPostStateSorted::new(Vec::new(), storages)
434    }
435
436    #[test]
437    fn test_seek_overlay_exact_hit_does_not_touch_db_until_next() {
438        let db_nodes = vec![(key(0x02), U256::from(2)), (key(0x03), U256::from(3))];
439        let post_state_nodes = vec![(key(0x02), U256::from(42))];
440
441        let db_nodes_map: BTreeMap<B256, U256> = db_nodes.into_iter().collect();
442        let db_nodes_arc = Arc::new(db_nodes_map);
443        let visited_keys = Arc::new(Mutex::new(Vec::new()));
444        let mock_cursor = MockHashedCursor::new(db_nodes_arc, visited_keys.clone());
445
446        let post_state = storage_post_state(post_state_nodes);
447        let mut cursor = HashedPostStateCursor::new_storage(mock_cursor, &post_state, B256::ZERO);
448
449        let result = cursor.seek(key(0x02)).unwrap();
450        assert_eq!(result, Some((key(0x02), U256::from(42))));
451        assert!(visited_keys.lock().is_empty(), "exact overlay hit should not touch the DB cursor");
452
453        let result = cursor.next().unwrap();
454        assert_eq!(result, Some((key(0x03), U256::from(3))));
455        assert!(!visited_keys.lock().is_empty(), "next should lazily position the DB cursor");
456    }
457
458    #[test]
459    fn test_seek_overlay_exact_hit_repositions_stale_db_on_next() {
460        let db_nodes = vec![(key(0x01), U256::from(1)), (key(0x03), U256::from(3))];
461        let post_state_nodes = vec![(key(0x02), U256::from(2))];
462
463        let db_nodes_map: BTreeMap<B256, U256> = db_nodes.into_iter().collect();
464        let db_nodes_arc = Arc::new(db_nodes_map);
465        let visited_keys = Arc::new(Mutex::new(Vec::new()));
466        let mock_cursor = MockHashedCursor::new(db_nodes_arc, visited_keys.clone());
467
468        let post_state = storage_post_state(post_state_nodes);
469        let mut cursor = HashedPostStateCursor::new_storage(mock_cursor, &post_state, B256::ZERO);
470
471        let result = cursor.seek(key(0x01)).unwrap();
472        assert_eq!(result, Some((key(0x01), U256::from(1))));
473        assert_eq!(visited_keys.lock().len(), 1);
474
475        let result = cursor.seek(key(0x02)).unwrap();
476        assert_eq!(result, Some((key(0x02), U256::from(2))));
477        assert_eq!(visited_keys.lock().len(), 1, "exact overlay hit should not seek the DB");
478
479        let result = cursor.next().unwrap();
480        assert_eq!(result, Some((key(0x03), U256::from(3))));
481    }
482
483    #[test]
484    fn test_seek_overlay_exact_deletion_still_seeks_db() {
485        let db_nodes = vec![(key(0x02), U256::from(2)), (key(0x03), U256::from(3))];
486        let post_state_nodes = vec![(key(0x02), U256::ZERO)];
487
488        let db_nodes_map: BTreeMap<B256, U256> = db_nodes.into_iter().collect();
489        let db_nodes_arc = Arc::new(db_nodes_map);
490        let visited_keys = Arc::new(Mutex::new(Vec::new()));
491        let mock_cursor = MockHashedCursor::new(db_nodes_arc, visited_keys.clone());
492
493        let post_state = storage_post_state(post_state_nodes);
494        let mut cursor = HashedPostStateCursor::new_storage(mock_cursor, &post_state, B256::ZERO);
495
496        let result = cursor.seek(key(0x02)).unwrap();
497        assert_eq!(result, Some((key(0x03), U256::from(3))));
498        assert!(!visited_keys.lock().is_empty(), "exact overlay deletion should consult the DB");
499    }
500
501    mod proptest_tests {
502        use super::*;
503        use itertools::Itertools;
504        use proptest::prelude::*;
505
506        /// Merge `db_nodes` with `post_state_nodes`, applying the post state overlay.
507        /// This properly handles deletions (ZERO values for U256, None for Account).
508        fn merge_with_overlay<V>(
509            db_nodes: Vec<(B256, V::NonZero)>,
510            post_state_nodes: Vec<(B256, V)>,
511        ) -> Vec<(B256, V::NonZero)>
512        where
513            V: HashedPostStateCursorValue,
514            V::NonZero: Copy,
515        {
516            db_nodes
517                .into_iter()
518                .merge_join_by(post_state_nodes, |db_entry, mem_entry| db_entry.0.cmp(&mem_entry.0))
519                .filter_map(|entry| match entry {
520                    // Only in db: keep it
521                    itertools::EitherOrBoth::Left((key, node)) => Some((key, node)),
522                    // Only in post state: keep if not a deletion
523                    itertools::EitherOrBoth::Right((key, wrapped)) => {
524                        wrapped.into_option().map(|val| (key, val))
525                    }
526                    // In both: post state takes precedence (keep if not a deletion)
527                    itertools::EitherOrBoth::Both(_, (key, wrapped)) => {
528                        wrapped.into_option().map(|val| (key, val))
529                    }
530                })
531                .collect()
532        }
533
534        /// Generate a strategy for U256 values
535        fn u256_strategy() -> impl Strategy<Value = U256> {
536            any::<u64>().prop_map(U256::from)
537        }
538
539        /// Generate a sorted vector of (B256, U256) entries
540        fn sorted_db_nodes_strategy() -> impl Strategy<Value = Vec<(B256, U256)>> {
541            prop::collection::vec((any::<u8>(), u256_strategy()), 0..20).prop_map(|entries| {
542                let mut result: Vec<(B256, U256)> = entries
543                    .into_iter()
544                    .map(|(byte, value)| (B256::repeat_byte(byte), value))
545                    .collect();
546                result.sort_by_key(|a| a.0);
547                result.dedup_by(|a, b| a.0 == b.0);
548                result
549            })
550        }
551
552        /// Generate a sorted vector of (B256, U256) entries (including deletions as ZERO)
553        fn sorted_post_state_nodes_strategy() -> impl Strategy<Value = Vec<(B256, U256)>> {
554            // Explicitly inject ZERO values to model post-state deletions.
555            prop::collection::vec((any::<u8>(), u256_strategy(), any::<bool>()), 0..20).prop_map(
556                |entries| {
557                    let mut result: Vec<(B256, U256)> = entries
558                        .into_iter()
559                        .map(|(byte, value, is_deletion)| {
560                            let effective_value = if is_deletion { U256::ZERO } else { value };
561                            (B256::repeat_byte(byte), effective_value)
562                        })
563                        .collect();
564                    result.sort_by_key(|a| a.0);
565                    result.dedup_by(|a, b| a.0 == b.0);
566                    result
567                },
568            )
569        }
570
571        proptest! {
572            #![proptest_config(ProptestConfig::with_cases(1000))]
573        /// Tests `HashedPostStateCursor` produces identical results to a pre-merged cursor
574        /// across 1000 random scenarios.
575        ///
576        /// For random DB entries and post-state changes, creates two cursors:
577        /// - Control: pre-merged data (expected behavior)
578        /// - Test: `HashedPostStateCursor` (lazy overlay)
579        ///
580        /// Executes random sequences of `next()` and `seek()` operations, asserting
581        /// both cursors return identical results.
582        #[test]
583        fn proptest_hashed_post_state_cursor(
584                db_nodes in sorted_db_nodes_strategy(),
585                post_state_nodes in sorted_post_state_nodes_strategy(),
586                op_choices in prop::collection::vec(any::<u8>(), 10..500),
587            ) {
588                reth_tracing::init_test_tracing();
589                use tracing::debug;
590
591                debug!("Starting proptest!");
592
593                // Create the expected results by merging the two sorted vectors,
594                // properly handling deletions (ZERO values in post_state_nodes)
595                let expected_combined = merge_with_overlay(db_nodes.clone(), post_state_nodes.clone());
596
597                // Collect all keys for operation generation
598                let all_keys: Vec<B256> = expected_combined.iter().map(|(k, _)| *k).collect();
599
600                // Create a control cursor using the combined result with a mock cursor
601                let control_db_map: BTreeMap<B256, U256> = expected_combined.into_iter().collect();
602                let control_db_arc = Arc::new(control_db_map);
603                let control_visited_keys = Arc::new(Mutex::new(Vec::new()));
604                let mut control_cursor = MockHashedCursor::new(control_db_arc, control_visited_keys);
605
606                // Create the HashedPostStateCursor being tested
607                let db_nodes_map: BTreeMap<B256, U256> = db_nodes.into_iter().collect();
608                let db_nodes_arc = Arc::new(db_nodes_map);
609                let visited_keys = Arc::new(Mutex::new(Vec::new()));
610                let mock_cursor = MockHashedCursor::new(db_nodes_arc, visited_keys);
611
612                // Create a HashedPostStateSorted with the storage data
613                let hashed_address = B256::ZERO;
614                let storage_sorted = reth_trie_common::HashedStorageSorted {
615                    storage_slots: post_state_nodes,
616                };
617                let mut storages = alloy_primitives::map::B256Map::default();
618                storages.insert(hashed_address, storage_sorted);
619                let post_state = HashedPostStateSorted::new(Vec::new(), storages);
620
621                let mut test_cursor = HashedPostStateCursor::new_storage(mock_cursor, &post_state, hashed_address);
622
623                // Test: seek to the beginning first
624                let control_first = control_cursor.seek(B256::ZERO).unwrap();
625                let test_first = test_cursor.seek(B256::ZERO).unwrap();
626                debug!(
627                    control=?control_first.as_ref().map(|(k, _)| k),
628                    test=?test_first.as_ref().map(|(k, _)| k),
629                    "Initial seek returned",
630                );
631                assert_eq!(control_first, test_first, "Initial seek mismatch");
632
633                // If both cursors returned None, nothing to test
634                if control_first.is_none() && test_first.is_none() {
635                    return Ok(());
636                }
637
638                // Track the last key returned from the cursor
639                let mut last_returned_key = control_first.as_ref().map(|(k, _)| *k);
640
641                // Execute a sequence of random operations
642                for choice in op_choices {
643                    let op_type = choice % 2; // Only 2 operation types: next and seek
644
645                    match op_type {
646                        0 => {
647                            // Next operation
648                            let control_result = control_cursor.next().unwrap();
649                            let test_result = test_cursor.next().unwrap();
650                            debug!(
651                                control=?control_result.as_ref().map(|(k, _)| k),
652                                test=?test_result.as_ref().map(|(k, _)| k),
653                                "Next returned",
654                            );
655                            assert_eq!(control_result, test_result, "Next operation mismatch");
656
657                            last_returned_key = control_result.as_ref().map(|(k, _)| *k);
658
659                            // Stop if both cursors are exhausted
660                            if control_result.is_none() && test_result.is_none() {
661                                break;
662                            }
663                        }
664                        _ => {
665                            // Seek operation - choose a key >= last_returned_key
666                            if all_keys.is_empty() {
667                                continue;
668                            }
669
670                            let valid_keys: Vec<_> = all_keys
671                                .iter()
672                                .filter(|k| last_returned_key.is_none_or(|last| **k >= last))
673                                .collect();
674
675                            if valid_keys.is_empty() {
676                                continue;
677                            }
678
679                            let key = *valid_keys[(choice as usize / 2) % valid_keys.len()];
680
681                            let control_result = control_cursor.seek(key).unwrap();
682                            let test_result = test_cursor.seek(key).unwrap();
683                            debug!(
684                                control=?control_result.as_ref().map(|(k, _)| k),
685                                test=?test_result.as_ref().map(|(k, _)| k),
686                                ?key,
687                                "Seek returned",
688                            );
689                            assert_eq!(control_result, test_result, "Seek operation mismatch for key {:?}", key);
690
691                            last_returned_key = control_result.as_ref().map(|(k, _)| *k);
692
693                            // Stop if both cursors are exhausted
694                            if control_result.is_none() && test_result.is_none() {
695                                break;
696                            }
697                        }
698                    }
699                }
700            }
701        }
702    }
703}