reth_trie/trie_cursor/
mod.rs

1use crate::{BranchNodeCompact, Nibbles};
2use alloy_primitives::B256;
3use reth_storage_errors::db::DatabaseError;
4
5/// In-memory implementations of trie cursors.
6mod in_memory;
7
8/// Cursor for iterating over a subtrie.
9pub mod subnode;
10
11/// Noop trie cursor implementations.
12pub mod noop;
13
14/// Depth-first trie iterator.
15pub mod depth_first;
16
17/// Mock trie cursor implementations.
18#[cfg(test)]
19pub mod mock;
20
21pub use self::{depth_first::DepthFirstTrieIterator, in_memory::*, subnode::CursorSubNode};
22
23/// Factory for creating trie cursors.
24#[auto_impl::auto_impl(&)]
25pub trait TrieCursorFactory {
26    /// The account trie cursor type.
27    type AccountTrieCursor: TrieCursor;
28    /// The storage trie cursor type.
29    type StorageTrieCursor: TrieCursor;
30
31    /// Create an account trie cursor.
32    fn account_trie_cursor(&self) -> Result<Self::AccountTrieCursor, DatabaseError>;
33
34    /// Create a storage tries cursor.
35    fn storage_trie_cursor(
36        &self,
37        hashed_address: B256,
38    ) -> Result<Self::StorageTrieCursor, DatabaseError>;
39}
40
41/// A cursor for traversing stored trie nodes. The cursor must iterate over keys in
42/// lexicographical order.
43#[auto_impl::auto_impl(&mut, Box)]
44pub trait TrieCursor: Send + Sync {
45    /// Move the cursor to the key and return if it is an exact match.
46    fn seek_exact(
47        &mut self,
48        key: Nibbles,
49    ) -> Result<Option<(Nibbles, BranchNodeCompact)>, DatabaseError>;
50
51    /// Move the cursor to the key and return a value matching of greater than the key.
52    fn seek(&mut self, key: Nibbles)
53        -> Result<Option<(Nibbles, BranchNodeCompact)>, DatabaseError>;
54
55    /// Move the cursor to the next key.
56    fn next(&mut self) -> Result<Option<(Nibbles, BranchNodeCompact)>, DatabaseError>;
57
58    /// Get the current entry.
59    fn current(&mut self) -> Result<Option<Nibbles>, DatabaseError>;
60}