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/// Mock trie cursor implementations.
15#[cfg(test)]
16pub mod mock;
17
18pub use self::{in_memory::*, subnode::CursorSubNode};
19
20/// Factory for creating trie cursors.
21#[auto_impl::auto_impl(&)]
22pub trait TrieCursorFactory {
23    /// The account trie cursor type.
24    type AccountTrieCursor: TrieCursor;
25    /// The storage trie cursor type.
26    type StorageTrieCursor: TrieCursor;
27
28    /// Create an account trie cursor.
29    fn account_trie_cursor(&self) -> Result<Self::AccountTrieCursor, DatabaseError>;
30
31    /// Create a storage tries cursor.
32    fn storage_trie_cursor(
33        &self,
34        hashed_address: B256,
35    ) -> Result<Self::StorageTrieCursor, DatabaseError>;
36}
37
38/// A cursor for traversing stored trie nodes. The cursor must iterate over keys in
39/// lexicographical order.
40#[auto_impl::auto_impl(&mut, Box)]
41pub trait TrieCursor: Send + Sync {
42    /// Move the cursor to the key and return if it is an exact match.
43    fn seek_exact(
44        &mut self,
45        key: Nibbles,
46    ) -> Result<Option<(Nibbles, BranchNodeCompact)>, DatabaseError>;
47
48    /// Move the cursor to the key and return a value matching of greater than the key.
49    fn seek(&mut self, key: Nibbles)
50        -> Result<Option<(Nibbles, BranchNodeCompact)>, DatabaseError>;
51
52    /// Move the cursor to the next key.
53    fn next(&mut self) -> Result<Option<(Nibbles, BranchNodeCompact)>, DatabaseError>;
54
55    /// Get the current entry.
56    fn current(&mut self) -> Result<Option<Nibbles>, DatabaseError>;
57}