Skip to content

Commit

Permalink
remove TxRef wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
fgimenez committed Jan 17, 2025
1 parent 8e0b6ef commit 14ec3ef
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 113 deletions.
22 changes: 8 additions & 14 deletions crates/trie/db/src/commitment.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
DatabaseHashedCursorFactory, DatabaseProof, DatabaseStateRoot, DatabaseStorageRoot,
DatabaseTrieCursorFactory, DatabaseTrieWitness, TxRef,
DatabaseTrieCursorFactory, DatabaseTrieWitness,
};
use reth_db::transaction::DbTx;
use reth_trie::{
Expand All @@ -27,19 +27,13 @@ pub trait StateCommitment: std::fmt::Debug + Send + Sync + Unpin + 'static {
pub struct MerklePatriciaTrie;

impl StateCommitment for MerklePatriciaTrie {
type StateRoot<'a, TX: DbTx + 'a> = StateRoot<
DatabaseTrieCursorFactory<TxRef<'a, TX>>,
DatabaseHashedCursorFactory<TxRef<'a, TX>>,
>;
type StorageRoot<'a, TX: DbTx + 'a> = StorageRoot<
DatabaseTrieCursorFactory<TxRef<'a, TX>>,
DatabaseHashedCursorFactory<TxRef<'a, TX>>,
>;
type StateRoot<'a, TX: DbTx + 'a> =
StateRoot<DatabaseTrieCursorFactory<&'a TX>, DatabaseHashedCursorFactory<&'a TX>>;
type StorageRoot<'a, TX: DbTx + 'a> =
StorageRoot<DatabaseTrieCursorFactory<&'a TX>, DatabaseHashedCursorFactory<&'a TX>>;
type StateProof<'a, TX: DbTx + 'a> =
Proof<DatabaseTrieCursorFactory<TxRef<'a, TX>>, DatabaseHashedCursorFactory<TxRef<'a, TX>>>;
type StateWitness<'a, TX: DbTx + 'a> = TrieWitness<
DatabaseTrieCursorFactory<TxRef<'a, TX>>,
DatabaseHashedCursorFactory<TxRef<'a, TX>>,
>;
Proof<DatabaseTrieCursorFactory<&'a TX>, DatabaseHashedCursorFactory<&'a TX>>;
type StateWitness<'a, TX: DbTx + 'a> =
TrieWitness<DatabaseTrieCursorFactory<&'a TX>, DatabaseHashedCursorFactory<&'a TX>>;
type KeyHasher = KeccakKeyHasher;
}
13 changes: 6 additions & 7 deletions crates/trie/db/src/hashed_cursor.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{DatabaseRef, TxRef};
use crate::DatabaseRef;
use alloy_primitives::{B256, U256};
use reth_db::tables;
use reth_db_api::{
Expand All @@ -23,15 +23,14 @@ impl<Provider> Clone for DatabaseHashedCursorFactory<Provider> {
}
}

impl<Provider> DatabaseHashedCursorFactory<Provider> {
impl<'a, Tx: DbTx> DatabaseHashedCursorFactory<&'a Tx> {
/// Create new [`DatabaseHashedCursorFactory`] from a transaction reference.
pub fn new<'a, Tx: DbTx>(tx: &'a Tx) -> DatabaseHashedCursorFactory<TxRef<'a, Tx>>
where
TxRef<'a, Tx>: DatabaseRef,
{
DatabaseHashedCursorFactory { provider: Arc::new(TxRef(tx)) }
pub fn new(tx: &'a Tx) -> Self {
DatabaseHashedCursorFactory { provider: Arc::new(tx) }
}
}

impl<Provider> DatabaseHashedCursorFactory<Provider> {
/// Create new [`DatabaseHashedCursorFactory`] from a provider.
pub const fn from_provider(provider: Arc<Provider>) -> Self {
Self { provider }
Expand Down
10 changes: 3 additions & 7 deletions crates/trie/db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,10 @@ pub trait DatabaseRef: Send + Sync {
fn tx_reference(&self) -> &Self::Tx;
}

/// A wrapper type that implements `DatabaseRef` for transaction references
#[derive(Debug)]
pub struct TxRef<'a, Tx>(pub &'a Tx);

impl<Tx: DbTx> DatabaseRef for TxRef<'_, Tx> {
type Tx = Tx;
impl<T: DbTx> DatabaseRef for &'_ T {
type Tx = T;

fn tx_reference(&self) -> &Self::Tx {
self.0
self
}
}
29 changes: 13 additions & 16 deletions crates/trie/db/src/proof.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{DatabaseHashedCursorFactory, DatabaseTrieCursorFactory, TxRef};
use crate::{DatabaseHashedCursorFactory, DatabaseTrieCursorFactory};
use alloy_primitives::{keccak256, map::HashMap, Address, B256};
use reth_db_api::transaction::DbTx;
use reth_execution_errors::StateProofError;
Expand Down Expand Up @@ -35,13 +35,13 @@ pub trait DatabaseProof<'a, TX> {
}

impl<'a, TX: DbTx> DatabaseProof<'a, TX>
for Proof<DatabaseTrieCursorFactory<TxRef<'a, TX>>, DatabaseHashedCursorFactory<TxRef<'a, TX>>>
for Proof<DatabaseTrieCursorFactory<&'a TX>, DatabaseHashedCursorFactory<&'a TX>>
{
/// Create a new [Proof] instance from database transaction.
fn from_tx(tx: &'a TX) -> Self {
Self::new(
Arc::new(DatabaseTrieCursorFactory::<TxRef<'a, TX>>::new(tx)),
Arc::new(DatabaseHashedCursorFactory::<TxRef<'a, TX>>::new(tx)),
Arc::new(DatabaseTrieCursorFactory::new(tx)),
Arc::new(DatabaseHashedCursorFactory::new(tx)),
)
}

Expand All @@ -55,11 +55,11 @@ impl<'a, TX: DbTx> DatabaseProof<'a, TX>
let state_sorted = input.state.into_sorted();
Self::from_tx(tx)
.with_trie_cursor_factory(InMemoryTrieCursorFactory::new(
DatabaseTrieCursorFactory::<TxRef<'a, TX>>::new(tx),
DatabaseTrieCursorFactory::new(tx),
Arc::new(nodes_sorted),
))
.with_hashed_cursor_factory(HashedPostStateCursorFactory::new(
DatabaseHashedCursorFactory::<TxRef<'a, TX>>::new(tx),
DatabaseHashedCursorFactory::new(tx),
Arc::new(state_sorted),
))
.with_prefix_sets_mut(input.prefix_sets)
Expand All @@ -75,11 +75,11 @@ impl<'a, TX: DbTx> DatabaseProof<'a, TX>
let state_sorted = input.state.into_sorted();
Self::from_tx(tx)
.with_trie_cursor_factory(InMemoryTrieCursorFactory::new(
DatabaseTrieCursorFactory::<TxRef<'a, TX>>::new(tx),
DatabaseTrieCursorFactory::new(tx),
Arc::new(nodes_sorted),
))
.with_hashed_cursor_factory(HashedPostStateCursorFactory::new(
DatabaseHashedCursorFactory::<TxRef<'a, TX>>::new(tx),
DatabaseHashedCursorFactory::new(tx),
Arc::new(state_sorted),
))
.with_prefix_sets_mut(input.prefix_sets)
Expand Down Expand Up @@ -110,15 +110,12 @@ pub trait DatabaseStorageProof<'a, TX> {
}

impl<'a, TX: DbTx> DatabaseStorageProof<'a, TX>
for StorageProof<
DatabaseTrieCursorFactory<TxRef<'a, TX>>,
DatabaseHashedCursorFactory<TxRef<'a, TX>>,
>
for StorageProof<DatabaseTrieCursorFactory<&'a TX>, DatabaseHashedCursorFactory<&'a TX>>
{
fn from_tx(tx: &'a TX, address: Address) -> Self {
Self::new(
Arc::new(DatabaseTrieCursorFactory::<TxRef<'a, TX>>::new(tx)),
Arc::new(DatabaseHashedCursorFactory::<TxRef<'a, TX>>::new(tx)),
Arc::new(DatabaseTrieCursorFactory::new(tx)),
Arc::new(DatabaseHashedCursorFactory::new(tx)),
address,
)
}
Expand All @@ -137,7 +134,7 @@ impl<'a, TX: DbTx> DatabaseStorageProof<'a, TX>
);
Self::from_tx(tx, address)
.with_hashed_cursor_factory(HashedPostStateCursorFactory::new(
DatabaseHashedCursorFactory::<TxRef<'a, TX>>::new(tx),
DatabaseHashedCursorFactory::new(tx),
Arc::new(state_sorted),
))
.with_prefix_set_mut(prefix_set)
Expand All @@ -159,7 +156,7 @@ impl<'a, TX: DbTx> DatabaseStorageProof<'a, TX>
);
Self::from_tx(tx, address)
.with_hashed_cursor_factory(HashedPostStateCursorFactory::new(
DatabaseHashedCursorFactory::<TxRef<'a, TX>>::new(tx),
DatabaseHashedCursorFactory::new(tx),
Arc::new(state_sorted),
))
.with_prefix_set_mut(prefix_set)
Expand Down
28 changes: 11 additions & 17 deletions crates/trie/db/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{DatabaseHashedCursorFactory, DatabaseTrieCursorFactory, PrefixSetLoader, TxRef};
use crate::{DatabaseHashedCursorFactory, DatabaseTrieCursorFactory, PrefixSetLoader};
use alloy_primitives::{
map::{AddressHashMap, B256HashMap},
Address, BlockNumber, B256, U256,
Expand Down Expand Up @@ -133,16 +133,10 @@ pub trait DatabaseHashedPostState<TX>: Sized {
}

impl<'a, TX: DbTx> DatabaseStateRoot<'a, TX>
for StateRoot<
DatabaseTrieCursorFactory<TxRef<'a, TX>>,
DatabaseHashedCursorFactory<TxRef<'a, TX>>,
>
for StateRoot<DatabaseTrieCursorFactory<&'a TX>, DatabaseHashedCursorFactory<&'a TX>>
{
fn from_tx(tx: &'a TX) -> Self {
Self::new(
DatabaseTrieCursorFactory::<TxRef<'a, TX>>::new(tx),
DatabaseHashedCursorFactory::<TxRef<'a, TX>>::new(tx),
)
Self::new(DatabaseTrieCursorFactory::new(tx), DatabaseHashedCursorFactory::new(tx))
}

fn incremental_root_calculator(
Expand Down Expand Up @@ -181,9 +175,9 @@ impl<'a, TX: DbTx> DatabaseStateRoot<'a, TX>
let prefix_sets = post_state.construct_prefix_sets().freeze();
let state_sorted = post_state.into_sorted();
StateRoot::new(
DatabaseTrieCursorFactory::<TxRef<'a, TX>>::new(tx),
DatabaseTrieCursorFactory::new(tx),
HashedPostStateCursorFactory::new(
DatabaseHashedCursorFactory::<TxRef<'a, TX>>::new(tx),
DatabaseHashedCursorFactory::new(tx),
Arc::new(state_sorted),
),
)
Expand All @@ -198,9 +192,9 @@ impl<'a, TX: DbTx> DatabaseStateRoot<'a, TX>
let prefix_sets = post_state.construct_prefix_sets().freeze();
let state_sorted = post_state.into_sorted();
StateRoot::new(
DatabaseTrieCursorFactory::<TxRef<'a, TX>>::new(tx),
DatabaseTrieCursorFactory::new(tx),
HashedPostStateCursorFactory::new(
DatabaseHashedCursorFactory::<TxRef<'a, TX>>::new(tx),
DatabaseHashedCursorFactory::new(tx),
Arc::new(state_sorted),
),
)
Expand All @@ -213,11 +207,11 @@ impl<'a, TX: DbTx> DatabaseStateRoot<'a, TX>
let nodes_sorted = input.nodes.into_sorted();
StateRoot::new(
InMemoryTrieCursorFactory::new(
DatabaseTrieCursorFactory::<TxRef<'a, TX>>::new(tx),
DatabaseTrieCursorFactory::new(tx),
Arc::new(nodes_sorted),
),
HashedPostStateCursorFactory::new(
DatabaseHashedCursorFactory::<TxRef<'a, TX>>::new(tx),
DatabaseHashedCursorFactory::new(tx),
Arc::new(state_sorted),
),
)
Expand All @@ -233,11 +227,11 @@ impl<'a, TX: DbTx> DatabaseStateRoot<'a, TX>
let nodes_sorted = input.nodes.into_sorted();
StateRoot::new(
InMemoryTrieCursorFactory::new(
DatabaseTrieCursorFactory::<TxRef<'a, TX>>::new(tx),
DatabaseTrieCursorFactory::new(tx),
Arc::new(nodes_sorted),
),
HashedPostStateCursorFactory::new(
DatabaseHashedCursorFactory::<TxRef<'a, TX>>::new(tx),
DatabaseHashedCursorFactory::new(tx),
Arc::new(state_sorted),
),
)
Expand Down
19 changes: 8 additions & 11 deletions crates/trie/db/src/storage.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{DatabaseHashedCursorFactory, DatabaseTrieCursorFactory, TxRef};
use crate::{DatabaseHashedCursorFactory, DatabaseTrieCursorFactory};
use alloy_primitives::{keccak256, map::hash_map, Address, BlockNumber, B256};
use reth_db::{cursor::DbCursorRO, models::BlockNumberAddress, tables, DatabaseError};
use reth_db_api::transaction::DbTx;
Expand Down Expand Up @@ -37,15 +37,12 @@ pub trait DatabaseHashedStorage<TX>: Sized {
}

impl<'a, TX: DbTx> DatabaseStorageRoot<'a, TX>
for StorageRoot<
DatabaseTrieCursorFactory<TxRef<'a, TX>>,
DatabaseHashedCursorFactory<TxRef<'a, TX>>,
>
for StorageRoot<DatabaseTrieCursorFactory<&'a TX>, DatabaseHashedCursorFactory<&'a TX>>
{
fn from_tx(tx: &'a TX, address: Address) -> Self {
Self::new(
DatabaseTrieCursorFactory::<TxRef<'a, TX>>::new(tx),
DatabaseHashedCursorFactory::<TxRef<'a, TX>>::new(tx),
DatabaseTrieCursorFactory::new(tx),
DatabaseHashedCursorFactory::new(tx),
address,
Default::default(),
#[cfg(feature = "metrics")]
Expand All @@ -55,8 +52,8 @@ impl<'a, TX: DbTx> DatabaseStorageRoot<'a, TX>

fn from_tx_hashed(tx: &'a TX, hashed_address: B256) -> Self {
Self::new_hashed(
DatabaseTrieCursorFactory::<TxRef<'a, TX>>::new(tx),
DatabaseHashedCursorFactory::<TxRef<'a, TX>>::new(tx),
DatabaseTrieCursorFactory::new(tx),
DatabaseHashedCursorFactory::new(tx),
hashed_address,
Default::default(),
#[cfg(feature = "metrics")]
Expand All @@ -73,9 +70,9 @@ impl<'a, TX: DbTx> DatabaseStorageRoot<'a, TX>
let state_sorted =
HashedPostState::from_hashed_storage(keccak256(address), hashed_storage).into_sorted();
StorageRoot::new(
DatabaseTrieCursorFactory::<TxRef<'a, TX>>::new(tx),
DatabaseTrieCursorFactory::new(tx),
HashedPostStateCursorFactory::new(
DatabaseHashedCursorFactory::<TxRef<'a, TX>>::new(tx),
DatabaseHashedCursorFactory::new(tx),
Arc::new(state_sorted),
),
address,
Expand Down
13 changes: 6 additions & 7 deletions crates/trie/db/src/trie_cursor.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{DatabaseRef, TxRef};
use crate::DatabaseRef;
use alloy_primitives::B256;
use reth_db::{
cursor::{DbCursorRW, DbDupCursorRW},
Expand Down Expand Up @@ -30,15 +30,14 @@ impl<Provider> Clone for DatabaseTrieCursorFactory<Provider> {
}
}

impl<Provider> DatabaseTrieCursorFactory<Provider> {
impl<'a, Tx: DbTx> DatabaseTrieCursorFactory<&'a Tx> {
/// Create new [`DatabaseTrieCursorFactory`] from a transaction reference.
pub fn new<'a, Tx: DbTx>(tx: &'a Tx) -> DatabaseTrieCursorFactory<TxRef<'a, Tx>>
where
TxRef<'a, Tx>: DatabaseRef,
{
DatabaseTrieCursorFactory { provider: Arc::new(TxRef(tx)) }
pub fn new(tx: &'a Tx) -> Self {
DatabaseTrieCursorFactory { provider: Arc::new(tx) }
}
}

impl<Provider> DatabaseTrieCursorFactory<Provider> {
/// Create new [`DatabaseTrieCursorFactory`] from a provider.
pub const fn from_provider(provider: Arc<Provider>) -> Self {
Self { provider }
Expand Down
16 changes: 5 additions & 11 deletions crates/trie/db/src/witness.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{DatabaseHashedCursorFactory, DatabaseTrieCursorFactory, TxRef};
use crate::{DatabaseHashedCursorFactory, DatabaseTrieCursorFactory};
use alloy_primitives::{map::B256HashMap, Bytes};
use reth_db_api::transaction::DbTx;
use reth_execution_errors::TrieWitnessError;
Expand All @@ -24,16 +24,10 @@ pub trait DatabaseTrieWitness<'a, TX> {
}

impl<'a, TX: DbTx> DatabaseTrieWitness<'a, TX>
for TrieWitness<
DatabaseTrieCursorFactory<TxRef<'a, TX>>,
DatabaseHashedCursorFactory<TxRef<'a, TX>>,
>
for TrieWitness<DatabaseTrieCursorFactory<&'a TX>, DatabaseHashedCursorFactory<&'a TX>>
{
fn from_tx(tx: &'a TX) -> Self {
Self::new(
DatabaseTrieCursorFactory::<TxRef<'a, TX>>::new(tx),
DatabaseHashedCursorFactory::<TxRef<'a, TX>>::new(tx),
)
Self::new(DatabaseTrieCursorFactory::new(tx), DatabaseHashedCursorFactory::new(tx))
}

fn overlay_witness(
Expand All @@ -45,11 +39,11 @@ impl<'a, TX: DbTx> DatabaseTrieWitness<'a, TX>
let state_sorted = input.state.into_sorted();
Self::from_tx(tx)
.with_trie_cursor_factory(InMemoryTrieCursorFactory::new(
DatabaseTrieCursorFactory::<TxRef<'a, TX>>::new(tx),
DatabaseTrieCursorFactory::new(tx),
Arc::new(nodes_sorted),
))
.with_hashed_cursor_factory(HashedPostStateCursorFactory::new(
DatabaseHashedCursorFactory::<TxRef<'a, TX>>::new(tx),
DatabaseHashedCursorFactory::new(tx),
Arc::new(state_sorted),
))
.with_prefix_sets_mut(input.prefix_sets)
Expand Down
9 changes: 3 additions & 6 deletions crates/trie/db/tests/fuzz_in_memory_nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,14 @@ use reth_trie::{
updates::{TrieUpdates, TrieUpdatesSorted},
HashedPostState, HashedStorage, StateRoot, StorageRoot,
};
use reth_trie_db::{DatabaseStateRoot, DatabaseStorageRoot, DatabaseTrieCursorFactory, TxRef};
use reth_trie_db::{DatabaseStateRoot, DatabaseStorageRoot, DatabaseTrieCursorFactory};
use std::{collections::BTreeMap, sync::Arc};

fn create_in_memory_factory<TX: DbTx>(
tx: &TX,
trie_nodes: TrieUpdatesSorted,
) -> InMemoryTrieCursorFactory<DatabaseTrieCursorFactory<TxRef<'_, TX>>> {
InMemoryTrieCursorFactory::new(
DatabaseTrieCursorFactory::<TxRef<'_, TX>>::new(tx),
Arc::new(trie_nodes),
)
) -> InMemoryTrieCursorFactory<DatabaseTrieCursorFactory<&'_ TX>> {
InMemoryTrieCursorFactory::new(DatabaseTrieCursorFactory::new(tx), Arc::new(trie_nodes))
}

proptest! {
Expand Down
Loading

0 comments on commit 14ec3ef

Please sign in to comment.