Skip to content

Commit

Permalink
chore(trie): rename in-memory trie cursors
Browse files Browse the repository at this point in the history
  • Loading branch information
rkrasiuk committed Jun 30, 2024
1 parent a31b8b0 commit b984f99
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
4 changes: 2 additions & 2 deletions crates/trie/trie/src/trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ mod tests {
hashed_cursor::HashedPostStateCursorFactory,
prefix_set::PrefixSetMut,
test_utils::{state_root, state_root_prehashed, storage_root, storage_root_prehashed},
trie_cursor::TrieUpdatesCursorFactory,
trie_cursor::InMemoryTrieCursorFactory,
BranchNodeCompact, HashedPostState, HashedStorage, TrieMask,
};
use proptest::{prelude::ProptestConfig, proptest};
Expand Down Expand Up @@ -1488,7 +1488,7 @@ mod tests {
tx,
&post_state.clone().into_sorted(),
))
.with_trie_cursor_factory(TrieUpdatesCursorFactory::new(tx, &update.sorted()))
.with_trie_cursor_factory(InMemoryTrieCursorFactory::new(tx, &update.sorted()))
.with_prefix_set(prefix_sets.storage_prefix_sets.remove(&keccak256(address)).unwrap())
.root_with_updates()
.unwrap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,52 @@ use reth_trie_common::{BranchNodeCompact, Nibbles};

/// The trie cursor factory for the trie updates.
#[derive(Debug, Clone)]
pub struct TrieUpdatesCursorFactory<'a, CF> {
pub struct InMemoryTrieCursorFactory<'a, CF> {
cursor_factory: CF,
trie_updates: &'a TrieUpdatesSorted,
}

impl<'a, CF> TrieUpdatesCursorFactory<'a, CF> {
impl<'a, CF> InMemoryTrieCursorFactory<'a, CF> {
/// Create a new trie cursor factory.
pub const fn new(cursor_factory: CF, trie_updates: &'a TrieUpdatesSorted) -> Self {
Self { cursor_factory, trie_updates }
}
}

impl<'a, CF: TrieCursorFactory> TrieCursorFactory for TrieUpdatesCursorFactory<'a, CF> {
type AccountTrieCursor = TrieUpdatesAccountTrieCursor<'a, CF::AccountTrieCursor>;
type StorageTrieCursor = TrieUpdatesStorageTrieCursor<'a, CF::StorageTrieCursor>;
impl<'a, CF: TrieCursorFactory> TrieCursorFactory for InMemoryTrieCursorFactory<'a, CF> {
type AccountTrieCursor = InMemoryAccountTrieCursor<'a, CF::AccountTrieCursor>;
type StorageTrieCursor = InMemoryStorageTrieCursor<'a, CF::StorageTrieCursor>;

fn account_trie_cursor(&self) -> Result<Self::AccountTrieCursor, DatabaseError> {
let cursor = self.cursor_factory.account_trie_cursor()?;
Ok(TrieUpdatesAccountTrieCursor::new(cursor, self.trie_updates))
Ok(InMemoryAccountTrieCursor::new(cursor, self.trie_updates))
}

fn storage_trie_cursor(
&self,
hashed_address: B256,
) -> Result<Self::StorageTrieCursor, DatabaseError> {
let cursor = self.cursor_factory.storage_trie_cursor(hashed_address)?;
Ok(TrieUpdatesStorageTrieCursor::new(cursor, hashed_address, self.trie_updates))
Ok(InMemoryStorageTrieCursor::new(cursor, hashed_address, self.trie_updates))
}
}

/// The cursor to iterate over account trie updates and corresponding database entries.
/// It will always give precedence to the data from the trie updates.
#[derive(Debug)]
pub struct TrieUpdatesAccountTrieCursor<'a, C> {
pub struct InMemoryAccountTrieCursor<'a, C> {
cursor: C,
trie_updates: &'a TrieUpdatesSorted,
last_key: Option<TrieKey>,
}

impl<'a, C> TrieUpdatesAccountTrieCursor<'a, C> {
impl<'a, C> InMemoryAccountTrieCursor<'a, C> {
const fn new(cursor: C, trie_updates: &'a TrieUpdatesSorted) -> Self {
Self { cursor, trie_updates, last_key: None }
}
}

impl<'a, C: TrieCursor> TrieCursor for TrieUpdatesAccountTrieCursor<'a, C> {
impl<'a, C: TrieCursor> TrieCursor for InMemoryAccountTrieCursor<'a, C> {
fn seek_exact(
&mut self,
key: Nibbles,
Expand Down Expand Up @@ -109,21 +109,21 @@ impl<'a, C: TrieCursor> TrieCursor for TrieUpdatesAccountTrieCursor<'a, C> {
/// The cursor to iterate over storage trie updates and corresponding database entries.
/// It will always give precedence to the data from the trie updates.
#[derive(Debug)]
pub struct TrieUpdatesStorageTrieCursor<'a, C> {
pub struct InMemoryStorageTrieCursor<'a, C> {
cursor: C,
trie_update_index: usize,
trie_updates: &'a TrieUpdatesSorted,
hashed_address: B256,
last_key: Option<TrieKey>,
}

impl<'a, C> TrieUpdatesStorageTrieCursor<'a, C> {
impl<'a, C> InMemoryStorageTrieCursor<'a, C> {
const fn new(cursor: C, hashed_address: B256, trie_updates: &'a TrieUpdatesSorted) -> Self {
Self { cursor, trie_updates, trie_update_index: 0, hashed_address, last_key: None }
}
}

impl<'a, C: TrieCursor> TrieCursor for TrieUpdatesStorageTrieCursor<'a, C> {
impl<'a, C: TrieCursor> TrieCursor for InMemoryStorageTrieCursor<'a, C> {
fn seek_exact(
&mut self,
key: Nibbles,
Expand Down
10 changes: 8 additions & 2 deletions crates/trie/trie/src/trie_cursor/mod.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
use crate::{updates::TrieKey, BranchNodeCompact, Nibbles};
use reth_db::DatabaseError;
use reth_primitives::B256;

/// Database implementations of trie cursors.
mod database_cursors;

/// In-memory implementations of trie cursors.
mod in_memory;

/// Cursor for iterating over a subtrie.
mod subnode;
mod update;

/// Noop trie cursor implementations.
pub mod noop;

pub use self::{
database_cursors::{DatabaseAccountTrieCursor, DatabaseStorageTrieCursor},
in_memory::*,
subnode::CursorSubNode,
update::*,
};

/// Factory for creating trie cursors.
Expand Down

0 comments on commit b984f99

Please sign in to comment.