Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions crates/evm/execution-errors/src/trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use reth_storage_errors::{db::DatabaseError, provider::ProviderError};
use thiserror::Error;

/// State root errors.
#[derive(Error, PartialEq, Eq, Clone, Debug)]
#[derive(Error, Clone, Debug)]
pub enum StateRootError {
/// Internal database error.
#[error(transparent)]
Expand All @@ -27,7 +27,7 @@ impl From<StateRootError> for DatabaseError {
}

/// Storage root error.
#[derive(Error, PartialEq, Eq, Clone, Debug)]
#[derive(Error, Clone, Debug)]
pub enum StorageRootError {
/// Internal database error.
#[error(transparent)]
Expand All @@ -43,7 +43,7 @@ impl From<StorageRootError> for DatabaseError {
}

/// State proof errors.
#[derive(Error, PartialEq, Eq, Clone, Debug)]
#[derive(Error, Clone, Debug)]
pub enum StateProofError {
/// Internal database error.
#[error(transparent)]
Expand Down
55 changes: 10 additions & 45 deletions crates/optimism/trie/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,14 @@ use alloy_eips::eip1898::BlockWithParent;
use alloy_primitives::{map::HashMap, B256, U256};
use auto_impl::auto_impl;
use reth_primitives_traits::Account;
use reth_trie::{updates::TrieUpdates, BranchNodeCompact, HashedPostState, Nibbles};
use reth_trie::{
hashed_cursor::{HashedCursor, HashedStorageCursor},
trie_cursor::TrieCursor,
updates::TrieUpdates,
BranchNodeCompact, HashedPostState, Nibbles,
};
use std::{fmt::Debug, time::Duration};

/// Seeks and iterates over trie nodes in the database by path (lexicographical order)
pub trait OpProofsTrieCursorRO: Send + Sync {
/// Seek to an exact path, otherwise return None if not found.
fn seek_exact(
&mut self,
path: Nibbles,
) -> OpProofsStorageResult<Option<(Nibbles, BranchNodeCompact)>>;

/// Seek to a path, otherwise return the first path greater than the given path
/// lexicographically.
fn seek(
&mut self,
path: Nibbles,
) -> OpProofsStorageResult<Option<(Nibbles, BranchNodeCompact)>>;

/// Move the cursor to the next path and return it.
fn next(&mut self) -> OpProofsStorageResult<Option<(Nibbles, BranchNodeCompact)>>;

/// Get the current path.
fn current(&mut self) -> OpProofsStorageResult<Option<Nibbles>>;
}

/// Seeks and iterates over hashed entries in the database by key.
pub trait OpProofsHashedCursorRO: Send + Sync {
/// Value returned by the cursor.
type Value: Debug;

/// Seek an entry greater or equal to the given key and position the cursor there.
/// Returns the first entry with the key greater or equal to the sought key.
fn seek(&mut self, key: B256) -> OpProofsStorageResult<Option<(B256, Self::Value)>>;

/// Move the cursor to the next entry and return it.
fn next(&mut self) -> OpProofsStorageResult<Option<(B256, Self::Value)>>;

/// Returns `true` if there are no entries for a given key.
fn is_storage_empty(&mut self) -> OpProofsStorageResult<bool> {
Ok(self.seek(B256::ZERO)?.is_none())
}
}

/// Diff of trie updates and post state for a block.
#[derive(Debug, Clone, Default)]
pub struct BlockStateDiff {
Expand Down Expand Up @@ -98,22 +63,22 @@ pub struct OperationDurations {
#[auto_impl(Arc)]
pub trait OpProofsStore: Send + Sync + Debug {
/// Cursor for iterating over trie branches.
type StorageTrieCursor<'tx>: OpProofsTrieCursorRO + 'tx
type StorageTrieCursor<'tx>: TrieCursor + 'tx
where
Self: 'tx;

/// Cursor for iterating over account trie branches.
type AccountTrieCursor<'tx>: OpProofsTrieCursorRO + 'tx
type AccountTrieCursor<'tx>: TrieCursor + 'tx
where
Self: 'tx;

/// Cursor for iterating over storage leaves.
type StorageCursor<'tx>: OpProofsHashedCursorRO<Value = U256> + 'tx
type StorageCursor<'tx>: HashedStorageCursor<Value = U256> + Send + Sync + 'tx
where
Self: 'tx;

/// Cursor for iterating over account leaves.
type AccountHashedCursor<'tx>: OpProofsHashedCursorRO<Value = Account> + 'tx
type AccountHashedCursor<'tx>: HashedCursor<Value = Account> + Send + Sync + 'tx
where
Self: 'tx;

Expand Down
7 changes: 5 additions & 2 deletions crates/optimism/trie/src/backfill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,13 +348,16 @@ impl<'a, Tx: DbTx, S: OpProofsStore + Send> BackfillJob<'a, Tx, S> {
#[cfg(test)]
mod tests {
use super::*;
use crate::{InMemoryProofsStorage, OpProofsHashedCursorRO, OpProofsTrieCursorRO};
use crate::InMemoryProofsStorage;
use alloy_primitives::{keccak256, Address, U256};
use reth_db::{
cursor::DbCursorRW, test_utils::create_test_rw_db, transaction::DbTxMut, Database,
};
use reth_primitives_traits::Account;
use reth_trie::{BranchNodeCompact, StorageTrieEntry, StoredNibbles, StoredNibblesSubKey};
use reth_trie::{
hashed_cursor::HashedCursor, trie_cursor::TrieCursor, BranchNodeCompact, StorageTrieEntry,
StoredNibbles, StoredNibblesSubKey,
};
use std::sync::Arc;

/// Helper function to create a test branch node
Expand Down
31 changes: 15 additions & 16 deletions crates/optimism/trie/src/cursor.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Implementation of [`HashedCursor`] and [`TrieCursor`] for
//! [`OpProofsStorage`](crate::OpProofsStorage).

use crate::{OpProofsHashedCursorRO, OpProofsTrieCursorRO};
use alloy_primitives::{B256, U256};
use derive_more::{Constructor, From};
use reth_db::DatabaseError;
Expand All @@ -12,38 +11,38 @@ use reth_trie::{
BranchNodeCompact, Nibbles,
};

/// Manages reading storage or account trie nodes from [`OpProofsTrieCursorRO`].
/// Manages reading storage or account trie nodes from [`TrieCursor`].
#[derive(Debug, Clone, Constructor, From)]
pub struct OpProofsTrieCursor<C: OpProofsTrieCursorRO>(pub C);
pub struct OpProofsTrieCursor<C: TrieCursor>(pub C);

impl<C> TrieCursor for OpProofsTrieCursor<C>
where
C: OpProofsTrieCursorRO,
C: TrieCursor,
{
#[inline]
fn seek_exact(
&mut self,
key: Nibbles,
) -> Result<Option<(Nibbles, BranchNodeCompact)>, DatabaseError> {
Ok(self.0.seek_exact(key)?)
self.0.seek_exact(key)
}

#[inline]
fn seek(
&mut self,
key: Nibbles,
) -> Result<Option<(Nibbles, BranchNodeCompact)>, DatabaseError> {
Ok(self.0.seek(key)?)
self.0.seek(key)
}

#[inline]
fn next(&mut self) -> Result<Option<(Nibbles, BranchNodeCompact)>, DatabaseError> {
Ok(self.0.next()?)
self.0.next()
}

#[inline]
fn current(&mut self) -> Result<Option<Nibbles>, DatabaseError> {
Ok(self.0.current()?)
self.0.current()
}
}

Expand All @@ -53,18 +52,18 @@ pub struct OpProofsHashedAccountCursor<C>(pub C);

impl<C> HashedCursor for OpProofsHashedAccountCursor<C>
where
C: OpProofsHashedCursorRO<Value = Account> + Send + Sync,
C: HashedCursor<Value = Account> + Send + Sync,
{
type Value = Account;

#[inline]
fn seek(&mut self, key: B256) -> Result<Option<(B256, Self::Value)>, DatabaseError> {
Ok(self.0.seek(key)?)
self.0.seek(key)
}

#[inline]
fn next(&mut self) -> Result<Option<(B256, Self::Value)>, DatabaseError> {
Ok(self.0.next()?)
self.0.next()
}
}

Expand All @@ -74,27 +73,27 @@ pub struct OpProofsHashedStorageCursor<C>(pub C);

impl<C> HashedCursor for OpProofsHashedStorageCursor<C>
where
C: OpProofsHashedCursorRO<Value = U256> + Send + Sync,
C: HashedCursor<Value = U256> + Send + Sync,
{
type Value = U256;

#[inline]
fn seek(&mut self, key: B256) -> Result<Option<(B256, Self::Value)>, DatabaseError> {
Ok(self.0.seek(key)?)
self.0.seek(key)
}

#[inline]
fn next(&mut self) -> Result<Option<(B256, Self::Value)>, DatabaseError> {
Ok(self.0.next()?)
self.0.next()
}
}

impl<C> HashedStorageCursor for OpProofsHashedStorageCursor<C>
where
C: OpProofsHashedCursorRO<Value = U256> + Send + Sync,
C: HashedStorageCursor<Value = U256> + Send + Sync,
{
#[inline]
fn is_storage_empty(&mut self) -> Result<bool, DatabaseError> {
Ok(self.0.is_storage_empty()?)
self.0.is_storage_empty()
}
}
Loading
Loading