reth_trie/hashed_cursor/mod.rs
1use alloy_primitives::{B256, U256};
2use reth_primitives_traits::Account;
3use reth_storage_errors::db::DatabaseError;
4
5/// Implementation of hashed state cursor traits for the post state.
6mod post_state;
7pub use post_state::*;
8
9/// Implementation of noop hashed state cursor.
10pub mod noop;
11
12/// Mock trie cursor implementations.
13#[cfg(test)]
14pub mod mock;
15
16/// Metrics tracking hashed cursor implementations.
17pub mod metrics;
18#[cfg(feature = "metrics")]
19pub use metrics::HashedCursorMetrics;
20pub use metrics::{HashedCursorMetricsCache, InstrumentedHashedCursor};
21
22/// The factory trait for creating cursors over the hashed state.
23#[auto_impl::auto_impl(&)]
24pub trait HashedCursorFactory {
25 /// The hashed account cursor type.
26 type AccountCursor<'a>: HashedCursor<Value = Account>
27 where
28 Self: 'a;
29 /// The hashed storage cursor type.
30 type StorageCursor<'a>: HashedStorageCursor<Value = U256>
31 where
32 Self: 'a;
33
34 /// Returns a cursor for iterating over all hashed accounts in the state.
35 fn hashed_account_cursor(&self) -> Result<Self::AccountCursor<'_>, DatabaseError>;
36
37 /// Returns a cursor for iterating over all hashed storage entries in the state.
38 fn hashed_storage_cursor(
39 &self,
40 hashed_address: B256,
41 ) -> Result<Self::StorageCursor<'_>, DatabaseError>;
42}
43
44/// The cursor for iterating over hashed entries.
45#[auto_impl::auto_impl(&mut)]
46pub trait HashedCursor {
47 /// Value returned by the cursor.
48 type Value: std::fmt::Debug;
49
50 /// Seek an entry greater than or equal to the given key and position the cursor there.
51 /// Returns the first entry with the key greater than or equal to the sought key.
52 fn seek(&mut self, key: B256) -> Result<Option<(B256, Self::Value)>, DatabaseError>;
53
54 /// Move the cursor to the next entry and return it.
55 fn next(&mut self) -> Result<Option<(B256, Self::Value)>, DatabaseError>;
56
57 /// Reset the cursor to its initial state.
58 ///
59 /// # Important
60 ///
61 /// After calling this method, the subsequent operation MUST be a [`HashedCursor::seek`] call.
62 fn reset(&mut self);
63}
64
65/// The cursor for iterating over hashed storage entries.
66#[auto_impl::auto_impl(&mut)]
67pub trait HashedStorageCursor: HashedCursor {
68 /// Returns `true` if there are no entries for a given key.
69 fn is_storage_empty(&mut self) -> Result<bool, DatabaseError>;
70
71 /// Set the hashed address for the storage cursor.
72 ///
73 /// # Important
74 ///
75 /// After calling this method, the subsequent operation MUST be a [`HashedCursor::seek`] call.
76 fn set_hashed_address(&mut self, hashed_address: B256);
77}