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
2 changes: 1 addition & 1 deletion bin/reth/src/debug_cmd/in_memory_merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ impl Command {
let tx = provider.tx_ref();
let hashed_cursor_factory = HashedPostStateCursorFactory::new(tx, &hashed_post_state);
let (in_memory_state_root, in_memory_updates) = StateRoot::new(tx)
.with_hashed_cursor_factory(&hashed_cursor_factory)
.with_hashed_cursor_factory(hashed_cursor_factory)
.with_changed_account_prefixes(account_prefix_set)
.with_changed_storage_prefixes(storage_prefix_set)
.root_with_updates()?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ impl BundleStateWithReceipts {
let (account_prefix_set, storage_prefix_set) = hashed_post_state.construct_prefix_sets();
let hashed_cursor_factory = HashedPostStateCursorFactory::new(tx, &hashed_post_state);
StateRoot::new(tx)
.with_hashed_cursor_factory(&hashed_cursor_factory)
.with_hashed_cursor_factory(hashed_cursor_factory)
.with_changed_account_prefixes(account_prefix_set)
.with_changed_storage_prefixes(storage_prefix_set)
.root()
Expand Down
10 changes: 5 additions & 5 deletions crates/trie/src/hashed_cursor/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ use reth_db::{
};
use reth_primitives::{Account, StorageEntry, B256};

impl<'a, 'tx, TX: DbTx<'tx>> HashedCursorFactory<'a> for TX {
type AccountCursor = <TX as DbTxGAT<'a>>::Cursor<tables::HashedAccount> where Self: 'a;
type StorageCursor = <TX as DbTxGAT<'a>>::DupCursor<tables::HashedStorage> where Self: 'a;
impl<'a, 'tx, TX: DbTx<'tx>> HashedCursorFactory for &'a TX {
type AccountCursor = <TX as DbTxGAT<'a>>::Cursor<tables::HashedAccount>;
type StorageCursor = <TX as DbTxGAT<'a>>::DupCursor<tables::HashedStorage>;

fn hashed_account_cursor(&'a self) -> Result<Self::AccountCursor, reth_db::DatabaseError> {
fn hashed_account_cursor(&self) -> Result<Self::AccountCursor, reth_db::DatabaseError> {
self.cursor_read::<tables::HashedAccount>()
}

fn hashed_storage_cursor(&'a self) -> Result<Self::StorageCursor, reth_db::DatabaseError> {
fn hashed_storage_cursor(&self) -> Result<Self::StorageCursor, reth_db::DatabaseError> {
self.cursor_dup_read::<tables::HashedStorage>()
}
}
Expand Down
14 changes: 5 additions & 9 deletions crates/trie/src/hashed_cursor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,17 @@ mod post_state;
pub use post_state::*;

/// The factory trait for creating cursors over the hashed state.
pub trait HashedCursorFactory<'a> {
pub trait HashedCursorFactory {
/// The hashed account cursor type.
type AccountCursor: HashedAccountCursor
where
Self: 'a;
type AccountCursor: HashedAccountCursor;
/// The hashed storage cursor type.
type StorageCursor: HashedStorageCursor
where
Self: 'a;
type StorageCursor: HashedStorageCursor;

/// Returns a cursor for iterating over all hashed accounts in the state.
fn hashed_account_cursor(&'a self) -> Result<Self::AccountCursor, reth_db::DatabaseError>;
fn hashed_account_cursor(&self) -> Result<Self::AccountCursor, reth_db::DatabaseError>;

/// Returns a cursor for iterating over all hashed storage entries in the state.
fn hashed_storage_cursor(&'a self) -> Result<Self::StorageCursor, reth_db::DatabaseError>;
fn hashed_storage_cursor(&self) -> Result<Self::StorageCursor, reth_db::DatabaseError>;
}

/// The cursor for iterating over hashed accounts.
Expand Down
38 changes: 19 additions & 19 deletions crates/trie/src/hashed_cursor/post_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,27 +156,31 @@ pub struct HashedPostStateCursorFactory<'a, 'b, TX> {
post_state: &'b HashedPostState,
}

impl<'a, 'b, TX> Clone for HashedPostStateCursorFactory<'a, 'b, TX> {
fn clone(&self) -> Self {
Self { tx: self.tx, post_state: self.post_state }
}
}

impl<'a, 'b, TX> HashedPostStateCursorFactory<'a, 'b, TX> {
/// Create a new factory.
pub fn new(tx: &'a TX, post_state: &'b HashedPostState) -> Self {
Self { tx, post_state }
}
}

impl<'a, 'b, 'tx, TX: DbTx<'tx>> HashedCursorFactory<'a>
for HashedPostStateCursorFactory<'a, 'b, TX>
where
'a: 'b,
{
type AccountCursor = HashedPostStateAccountCursor<'b, <TX as DbTxGAT<'a>>::Cursor<tables::HashedAccount>> where Self: 'a;
type StorageCursor = HashedPostStateStorageCursor<'b, <TX as DbTxGAT<'a>>::DupCursor<tables::HashedStorage>> where Self: 'a;
impl<'a, 'b, 'tx, TX: DbTx<'tx>> HashedCursorFactory for HashedPostStateCursorFactory<'a, 'b, TX> {
type AccountCursor =
HashedPostStateAccountCursor<'b, <TX as DbTxGAT<'a>>::Cursor<tables::HashedAccount>>;
type StorageCursor =
HashedPostStateStorageCursor<'b, <TX as DbTxGAT<'a>>::DupCursor<tables::HashedStorage>>;

fn hashed_account_cursor(&'a self) -> Result<Self::AccountCursor, reth_db::DatabaseError> {
fn hashed_account_cursor(&self) -> Result<Self::AccountCursor, reth_db::DatabaseError> {
let cursor = self.tx.cursor_read::<tables::HashedAccount>()?;
Ok(HashedPostStateAccountCursor::new(cursor, self.post_state))
}

fn hashed_storage_cursor(&'a self) -> Result<Self::StorageCursor, reth_db::DatabaseError> {
fn hashed_storage_cursor(&self) -> Result<Self::StorageCursor, reth_db::DatabaseError> {
let cursor = self.tx.cursor_dup_read::<tables::HashedStorage>()?;
Ok(HashedPostStateStorageCursor::new(cursor, self.post_state))
}
Expand Down Expand Up @@ -544,12 +548,10 @@ mod tests {
use reth_db::{database::Database, test_utils::create_test_rw_db, transaction::DbTxMut};
use std::collections::BTreeMap;

fn assert_account_cursor_order<'a, 'b>(
factory: &'a impl HashedCursorFactory<'b>,
fn assert_account_cursor_order(
factory: &impl HashedCursorFactory,
mut expected: impl Iterator<Item = (B256, Account)>,
) where
'a: 'b,
{
) {
let mut cursor = factory.hashed_account_cursor().unwrap();

let first_account = cursor.seek(B256::default()).unwrap();
Expand All @@ -563,12 +565,10 @@ mod tests {
assert!(cursor.next().unwrap().is_none());
}

fn assert_storage_cursor_order<'a, 'b>(
factory: &'a impl HashedCursorFactory<'b>,
fn assert_storage_cursor_order(
factory: &impl HashedCursorFactory,
expected: impl Iterator<Item = (B256, BTreeMap<B256, U256>)>,
) where
'a: 'b,
{
) {
let mut cursor = factory.hashed_storage_cursor().unwrap();

for (account, storage) in expected {
Expand Down
42 changes: 21 additions & 21 deletions crates/trie/src/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,32 +35,32 @@ use reth_primitives::{
/// After traversing the path, the proof generator continues to restore the root node of the trie
/// until completion. The root node is then inserted at the start of the proof.
#[derive(Debug)]
pub struct Proof<'a, 'b, TX, H> {
pub struct Proof<'a, TX, H> {
/// A reference to the database transaction.
tx: &'a TX,
/// The factory for hashed cursors.
hashed_cursor_factory: &'b H,
hashed_cursor_factory: H,
}

impl<'a, TX> Proof<'a, 'a, TX, TX> {
impl<'a, TX> Proof<'a, TX, &'a TX> {
/// Create a new [Proof] instance.
pub fn new(tx: &'a TX) -> Self {
Self { tx, hashed_cursor_factory: tx }
}
}

impl<'a, 'b, 'tx, TX, H> Proof<'a, 'b, TX, H>
impl<'a, 'tx, TX, H> Proof<'a, TX, H>
where
TX: DbTx<'tx>,
H: HashedCursorFactory<'b>,
H: HashedCursorFactory + Clone,
{
/// Generate an account proof from intermediate nodes.
pub fn account_proof(&self, address: Address) -> Result<Vec<Bytes>, ProofError> {
let hashed_address = keccak256(address);
let target_nibbles = Nibbles::unpack(hashed_address);

let mut proof_restorer =
ProofRestorer::new(self.tx)?.with_hashed_cursor_factory(self.hashed_cursor_factory)?;
let mut proof_restorer = ProofRestorer::new(self.tx)?
.with_hashed_cursor_factory(self.hashed_cursor_factory.clone())?;
let mut trie_cursor =
AccountTrieCursor::new(self.tx.cursor_read::<tables::AccountsTrie>()?);

Expand Down Expand Up @@ -101,7 +101,7 @@ where
fn traverse_path<T: DbCursorRO<'a, tables::AccountsTrie>>(
&self,
trie_cursor: &mut AccountTrieCursor<T>,
proof_restorer: &mut ProofRestorer<'a, 'b, TX, H>,
proof_restorer: &mut ProofRestorer<'a, TX, H>,
hashed_address: B256,
) -> Result<Vec<Bytes>, ProofError> {
let mut intermediate_proofs = Vec::new();
Expand Down Expand Up @@ -129,14 +129,14 @@ where
}
}

struct ProofRestorer<'a, 'b, TX, H>
struct ProofRestorer<'a, TX, H>
where
H: HashedCursorFactory<'b>,
H: HashedCursorFactory,
{
/// A reference to the database transaction.
tx: &'a TX,
/// The factory for hashed cursors.
hashed_cursor_factory: &'b H,
hashed_cursor_factory: H,
/// The hashed account cursor.
hashed_account_cursor: H::AccountCursor,
/// Pre-allocated buffer for account RLP encoding
Expand All @@ -145,7 +145,7 @@ where
node_rlp_buf: Vec<u8>,
}

impl<'a, 'tx, TX> ProofRestorer<'a, 'a, TX, TX>
impl<'a, 'tx, TX> ProofRestorer<'a, TX, &'a TX>
where
TX: DbTx<'tx>,
{
Expand All @@ -161,18 +161,18 @@ where
}
}

impl<'a, 'b, 'tx, TX, H> ProofRestorer<'a, 'b, TX, H>
impl<'a, 'tx, TX, H> ProofRestorer<'a, TX, H>
where
TX: DbTx<'tx> + HashedCursorFactory<'a>,
H: HashedCursorFactory<'b>,
TX: DbTx<'tx>,
H: HashedCursorFactory + Clone,
{
/// Set the hashed cursor factory.
fn with_hashed_cursor_factory<'c, HF>(
fn with_hashed_cursor_factory<HF>(
self,
hashed_cursor_factory: &'c HF,
) -> Result<ProofRestorer<'a, 'c, TX, HF>, ProofError>
hashed_cursor_factory: HF,
) -> Result<ProofRestorer<'a, TX, HF>, ProofError>
where
HF: HashedCursorFactory<'c>,
HF: HashedCursorFactory,
{
let hashed_account_cursor = hashed_cursor_factory.hashed_account_cursor()?;
Ok(ProofRestorer {
Expand Down Expand Up @@ -222,7 +222,7 @@ where

// Restore account's storage root.
let storage_root = StorageRoot::new_hashed(self.tx, hashed_address)
.with_hashed_cursor_factory(self.hashed_cursor_factory)
.with_hashed_cursor_factory(self.hashed_cursor_factory.clone())
.root()?;

self.account_rlp_buf.clear();
Expand Down Expand Up @@ -250,7 +250,7 @@ where

// Restore account's storage root.
let storage_root = StorageRoot::new_hashed(self.tx, hashed_address)
.with_hashed_cursor_factory(self.hashed_cursor_factory)
.with_hashed_cursor_factory(self.hashed_cursor_factory.clone())
.root()?;

self.account_rlp_buf.clear();
Expand Down
Loading