diff --git a/bin/reth/src/db/snapshots/bench.rs b/bin/reth/src/db/snapshots/bench.rs index 2505b23d4..928898205 100644 --- a/bin/reth/src/db/snapshots/bench.rs +++ b/bin/reth/src/db/snapshots/bench.rs @@ -25,7 +25,7 @@ pub(crate) fn bench( ) -> eyre::Result<()> where F1: FnMut() -> eyre::Result, - F2: Fn(DatabaseProviderRO<'_, DatabaseEnv>) -> eyre::Result, + F2: Fn(DatabaseProviderRO) -> eyre::Result, R: Debug + PartialEq, { let (db, chain) = db; diff --git a/bin/reth/src/db/snapshots/headers.rs b/bin/reth/src/db/snapshots/headers.rs index e4537cd6c..d05ff80c8 100644 --- a/bin/reth/src/db/snapshots/headers.rs +++ b/bin/reth/src/db/snapshots/headers.rs @@ -22,7 +22,7 @@ use std::{ impl Command { pub(crate) fn generate_headers_snapshot( &self, - provider: &DatabaseProviderRO<'_, DB>, + provider: &DatabaseProviderRO, compression: Compression, inclusion_filter: InclusionFilter, phf: PerfectHashingFunction, diff --git a/bin/reth/src/db/snapshots/receipts.rs b/bin/reth/src/db/snapshots/receipts.rs index dc8708ac0..b24eccda5 100644 --- a/bin/reth/src/db/snapshots/receipts.rs +++ b/bin/reth/src/db/snapshots/receipts.rs @@ -22,7 +22,7 @@ use std::{ impl Command { pub(crate) fn generate_receipts_snapshot( &self, - provider: &DatabaseProviderRO<'_, DB>, + provider: &DatabaseProviderRO, compression: Compression, inclusion_filter: InclusionFilter, phf: PerfectHashingFunction, diff --git a/bin/reth/src/db/snapshots/transactions.rs b/bin/reth/src/db/snapshots/transactions.rs index 00c06102e..94a61d262 100644 --- a/bin/reth/src/db/snapshots/transactions.rs +++ b/bin/reth/src/db/snapshots/transactions.rs @@ -22,7 +22,7 @@ use std::{ impl Command { pub(crate) fn generate_transactions_snapshot( &self, - provider: &DatabaseProviderRO<'_, DB>, + provider: &DatabaseProviderRO, compression: Compression, inclusion_filter: InclusionFilter, phf: PerfectHashingFunction, diff --git a/bin/reth/src/init.rs b/bin/reth/src/init.rs index 6b3d63804..04b290362 100644 --- a/bin/reth/src/init.rs +++ b/bin/reth/src/init.rs @@ -1,7 +1,7 @@ //! Reth genesis initialization utility functions. use reth_db::{ cursor::DbCursorRO, - database::{Database, DatabaseGAT}, + database::Database, tables, transaction::{DbTx, DbTxMut}, }; @@ -94,7 +94,7 @@ pub fn init_genesis( /// Inserts the genesis state into the database. pub fn insert_genesis_state( - tx: &>::TXMut, + tx: &::TXMut, genesis: &reth_primitives::Genesis, ) -> ProviderResult<()> { let mut state_init: BundleStateInit = HashMap::new(); @@ -160,7 +160,7 @@ pub fn insert_genesis_state( /// Inserts hashes for the genesis state. pub fn insert_genesis_hashes( - provider: &DatabaseProviderRW<'_, &DB>, + provider: &DatabaseProviderRW<&DB>, genesis: &reth_primitives::Genesis, ) -> ProviderResult<()> { // insert and hash accounts to hashing table @@ -184,7 +184,7 @@ pub fn insert_genesis_hashes( /// Inserts history indices for genesis accounts and storage. pub fn insert_genesis_history( - provider: &DatabaseProviderRW<'_, &DB>, + provider: &DatabaseProviderRW<&DB>, genesis: &reth_primitives::Genesis, ) -> ProviderResult<()> { let account_transitions = @@ -204,7 +204,7 @@ pub fn insert_genesis_history( /// Inserts header for the genesis state. pub fn insert_genesis_header( - tx: &>::TXMut, + tx: &::TXMut, chain: Arc, ) -> ProviderResult<()> { let header = chain.sealed_genesis_header(); @@ -236,7 +236,7 @@ mod tests { #[allow(clippy::type_complexity)] fn collect_table_entries( - tx: &>::TX, + tx: &::TX, ) -> Result>, InitDatabaseError> where DB: Database, diff --git a/crates/prune/src/segments/account_history.rs b/crates/prune/src/segments/account_history.rs index c1b5ae682..d8d94764b 100644 --- a/crates/prune/src/segments/account_history.rs +++ b/crates/prune/src/segments/account_history.rs @@ -32,7 +32,7 @@ impl Segment for AccountHistory { #[instrument(level = "trace", target = "pruner", skip(self, provider), ret)] fn prune( &self, - provider: &DatabaseProviderRW<'_, DB>, + provider: &DatabaseProviderRW, input: PruneInput, ) -> Result { let range = match input.get_next_block_range() { diff --git a/crates/prune/src/segments/headers.rs b/crates/prune/src/segments/headers.rs index 12ba19416..a1e951665 100644 --- a/crates/prune/src/segments/headers.rs +++ b/crates/prune/src/segments/headers.rs @@ -33,7 +33,7 @@ impl Segment for Headers { #[instrument(level = "trace", target = "pruner", skip(self, provider), ret)] fn prune( &self, - provider: &DatabaseProviderRW<'_, DB>, + provider: &DatabaseProviderRW, input: PruneInput, ) -> Result { let block_range = match input.get_next_block_range() { @@ -91,7 +91,7 @@ impl Headers { /// Returns `done`, number of pruned rows and last pruned block number. fn prune_table>( &self, - provider: &DatabaseProviderRW<'_, DB>, + provider: &DatabaseProviderRW, range: RangeInclusive, delete_limit: usize, ) -> RethResult<(bool, usize, BlockNumber)> { diff --git a/crates/prune/src/segments/history.rs b/crates/prune/src/segments/history.rs index bb3352a39..4836eeb84 100644 --- a/crates/prune/src/segments/history.rs +++ b/crates/prune/src/segments/history.rs @@ -14,7 +14,7 @@ use reth_provider::DatabaseProviderRW; /// /// Returns total number of processed (walked) and deleted entities. pub(crate) fn prune_history_indices( - provider: &DatabaseProviderRW<'_, DB>, + provider: &DatabaseProviderRW, to_block: BlockNumber, key_matches: impl Fn(&T::Key, &T::Key) -> bool, last_key: impl Fn(&T::Key) -> T::Key, diff --git a/crates/prune/src/segments/mod.rs b/crates/prune/src/segments/mod.rs index 62fda6195..339c4e013 100644 --- a/crates/prune/src/segments/mod.rs +++ b/crates/prune/src/segments/mod.rs @@ -45,14 +45,14 @@ pub trait Segment: Debug + Send + Sync { /// Prune data for [Self::segment] using the provided input. fn prune( &self, - provider: &DatabaseProviderRW<'_, DB>, + provider: &DatabaseProviderRW, input: PruneInput, ) -> Result; /// Save checkpoint for [Self::segment] to the database. fn save_checkpoint( &self, - provider: &DatabaseProviderRW<'_, DB>, + provider: &DatabaseProviderRW, checkpoint: PruneCheckpoint, ) -> ProviderResult<()> { provider.save_prune_checkpoint(self.segment(), checkpoint) @@ -80,7 +80,7 @@ impl PruneInput { /// To get the range end: get last tx number for `to_block`. pub(crate) fn get_next_tx_num_range( &self, - provider: &DatabaseProviderRW<'_, DB>, + provider: &DatabaseProviderRW, ) -> RethResult>> { let from_tx_number = self.previous_checkpoint // Checkpoint exists, prune from the next transaction after the highest pruned one diff --git a/crates/prune/src/segments/receipts.rs b/crates/prune/src/segments/receipts.rs index fb97897e0..acbdd6829 100644 --- a/crates/prune/src/segments/receipts.rs +++ b/crates/prune/src/segments/receipts.rs @@ -31,7 +31,7 @@ impl Segment for Receipts { #[instrument(level = "trace", target = "pruner", skip(self, provider), ret)] fn prune( &self, - provider: &DatabaseProviderRW<'_, DB>, + provider: &DatabaseProviderRW, input: PruneInput, ) -> Result { let tx_range = match input.get_next_tx_num_range(provider)? { @@ -71,7 +71,7 @@ impl Segment for Receipts { fn save_checkpoint( &self, - provider: &DatabaseProviderRW<'_, DB>, + provider: &DatabaseProviderRW, checkpoint: PruneCheckpoint, ) -> ProviderResult<()> { provider.save_prune_checkpoint(PruneSegment::Receipts, checkpoint)?; diff --git a/crates/prune/src/segments/receipts_by_logs.rs b/crates/prune/src/segments/receipts_by_logs.rs index 8f9faa4fd..aec6d7a2c 100644 --- a/crates/prune/src/segments/receipts_by_logs.rs +++ b/crates/prune/src/segments/receipts_by_logs.rs @@ -32,7 +32,7 @@ impl Segment for ReceiptsByLogs { #[instrument(level = "trace", target = "pruner", skip(self, provider), ret)] fn prune( &self, - provider: &DatabaseProviderRW<'_, DB>, + provider: &DatabaseProviderRW, input: PruneInput, ) -> Result { // Contract log filtering removes every receipt possible except the ones in the list. So, diff --git a/crates/prune/src/segments/sender_recovery.rs b/crates/prune/src/segments/sender_recovery.rs index aa8d48a2e..14fcdfae3 100644 --- a/crates/prune/src/segments/sender_recovery.rs +++ b/crates/prune/src/segments/sender_recovery.rs @@ -30,7 +30,7 @@ impl Segment for SenderRecovery { #[instrument(level = "trace", target = "pruner", skip(self, provider), ret)] fn prune( &self, - provider: &DatabaseProviderRW<'_, DB>, + provider: &DatabaseProviderRW, input: PruneInput, ) -> Result { let tx_range = match input.get_next_tx_num_range(provider)? { diff --git a/crates/prune/src/segments/storage_history.rs b/crates/prune/src/segments/storage_history.rs index aa68eb714..1bf294a97 100644 --- a/crates/prune/src/segments/storage_history.rs +++ b/crates/prune/src/segments/storage_history.rs @@ -36,7 +36,7 @@ impl Segment for StorageHistory { #[instrument(level = "trace", target = "pruner", skip(self, provider), ret)] fn prune( &self, - provider: &DatabaseProviderRW<'_, DB>, + provider: &DatabaseProviderRW, input: PruneInput, ) -> Result { let range = match input.get_next_block_range() { diff --git a/crates/prune/src/segments/transaction_lookup.rs b/crates/prune/src/segments/transaction_lookup.rs index 6785a22fc..4a094f460 100644 --- a/crates/prune/src/segments/transaction_lookup.rs +++ b/crates/prune/src/segments/transaction_lookup.rs @@ -31,7 +31,7 @@ impl Segment for TransactionLookup { #[instrument(level = "trace", target = "pruner", skip(self, provider), ret)] fn prune( &self, - provider: &DatabaseProviderRW<'_, DB>, + provider: &DatabaseProviderRW, input: PruneInput, ) -> Result { let (start, end) = match input.get_next_tx_num_range(provider)? { diff --git a/crates/prune/src/segments/transactions.rs b/crates/prune/src/segments/transactions.rs index d06e97b65..c70fd1197 100644 --- a/crates/prune/src/segments/transactions.rs +++ b/crates/prune/src/segments/transactions.rs @@ -30,7 +30,7 @@ impl Segment for Transactions { #[instrument(level = "trace", target = "pruner", skip(self, provider), ret)] fn prune( &self, - provider: &DatabaseProviderRW<'_, DB>, + provider: &DatabaseProviderRW, input: PruneInput, ) -> Result { let tx_range = match input.get_next_tx_num_range(provider)? { diff --git a/crates/rpc/rpc/src/debug.rs b/crates/rpc/rpc/src/debug.rs index b8b313d80..0dffc6661 100644 --- a/crates/rpc/rpc/src/debug.rs +++ b/crates/rpc/rpc/src/debug.rs @@ -458,7 +458,7 @@ where opts: GethDebugTracingOptions, env: Env, at: BlockId, - db: &mut SubState>, + db: &mut SubState, ) -> EthResult<(GethTrace, revm_primitives::State)> { let GethDebugTracingOptions { config, tracer, tracer_config, .. } = opts; diff --git a/crates/rpc/rpc/src/eth/api/mod.rs b/crates/rpc/rpc/src/eth/api/mod.rs index b30b4db56..5312c8229 100644 --- a/crates/rpc/rpc/src/eth/api/mod.rs +++ b/crates/rpc/rpc/src/eth/api/mod.rs @@ -206,7 +206,7 @@ where /// Returns the state at the given [BlockId] enum. /// /// Note: if not [BlockNumberOrTag::Pending] then this will only return canonical state. See also - pub fn state_at_block_id(&self, at: BlockId) -> EthResult> { + pub fn state_at_block_id(&self, at: BlockId) -> EthResult { Ok(self.provider().state_by_block_id(at)?) } @@ -216,7 +216,7 @@ where pub fn state_at_block_id_or_latest( &self, block_id: Option, - ) -> EthResult> { + ) -> EthResult { if let Some(block_id) = block_id { self.state_at_block_id(block_id) } else { @@ -225,12 +225,12 @@ where } /// Returns the state at the given block number - pub fn state_at_hash(&self, block_hash: B256) -> RethResult> { + pub fn state_at_hash(&self, block_hash: B256) -> RethResult { Ok(self.provider().history_by_block_hash(block_hash)?) } /// Returns the _latest_ state - pub fn latest_state(&self) -> RethResult> { + pub fn latest_state(&self) -> RethResult { Ok(self.provider().latest()?) } } diff --git a/crates/rpc/rpc/src/eth/api/transactions.rs b/crates/rpc/rpc/src/eth/api/transactions.rs index 78307ee33..58af7c769 100644 --- a/crates/rpc/rpc/src/eth/api/transactions.rs +++ b/crates/rpc/rpc/src/eth/api/transactions.rs @@ -51,7 +51,7 @@ use revm::L1BlockInfo; use std::ops::Div; /// Helper alias type for the state's [CacheDB] -pub(crate) type StateCacheDB<'r> = CacheDB>>; +pub(crate) type StateCacheDB = CacheDB>; /// Commonly used transaction related functions for the [EthApi] type in the `eth_` namespace. /// @@ -63,17 +63,17 @@ pub trait EthTransactions: Send + Sync { fn call_gas_limit(&self) -> u64; /// Returns the state at the given [BlockId] - fn state_at(&self, at: BlockId) -> EthResult>; + fn state_at(&self, at: BlockId) -> EthResult; /// Executes the closure with the state that corresponds to the given [BlockId]. fn with_state_at_block(&self, at: BlockId, f: F) -> EthResult where - F: FnOnce(StateProviderBox<'_>) -> EthResult; + F: FnOnce(StateProviderBox) -> EthResult; /// Executes the closure with the state that corresponds to the given [BlockId] on a new task async fn spawn_with_state_at_block(&self, at: BlockId, f: F) -> EthResult where - F: FnOnce(StateProviderBox<'_>) -> EthResult + Send + 'static, + F: FnOnce(StateProviderBox) -> EthResult + Send + 'static, T: Send + 'static; /// Returns the revm evm env for the requested [BlockId] @@ -154,7 +154,7 @@ pub trait EthTransactions: Send + Sync { f: F, ) -> EthResult where - F: for<'r> FnOnce(StateCacheDB<'r>, Env) -> EthResult + Send + 'static, + F: FnOnce(StateCacheDB, Env) -> EthResult + Send + 'static, R: Send + 'static; /// Executes the call request at the given [BlockId]. @@ -175,7 +175,7 @@ pub trait EthTransactions: Send + Sync { inspector: I, ) -> EthResult<(ResultAndState, Env)> where - I: for<'r> Inspector> + Send + 'static; + I: Inspector + Send + 'static; /// Executes the transaction on top of the given [BlockId] with a tracer configured by the /// config. @@ -209,9 +209,7 @@ pub trait EthTransactions: Send + Sync { f: F, ) -> EthResult where - F: for<'a> FnOnce(TracingInspector, ResultAndState, StateCacheDB<'a>) -> EthResult - + Send - + 'static, + F: FnOnce(TracingInspector, ResultAndState, StateCacheDB) -> EthResult + Send + 'static, R: Send + 'static; /// Fetches the transaction and the transaction's block @@ -236,12 +234,7 @@ pub trait EthTransactions: Send + Sync { f: F, ) -> EthResult> where - F: for<'a> FnOnce( - TransactionInfo, - TracingInspector, - ResultAndState, - StateCacheDB<'a>, - ) -> EthResult + F: FnOnce(TransactionInfo, TracingInspector, ResultAndState, StateCacheDB) -> EthResult + Send + 'static, R: Send + 'static; @@ -269,7 +262,7 @@ pub trait EthTransactions: Send + Sync { TracingInspector, ExecutionResult, &'a State, - &'a CacheDB>>, + &'a CacheDB>, ) -> EthResult + Send + 'static, @@ -293,7 +286,7 @@ pub trait EthTransactions: Send + Sync { TracingInspector, ExecutionResult, &'a State, - &'a CacheDB>>, + &'a CacheDB>, ) -> EthResult + Send + 'static, @@ -312,13 +305,13 @@ where self.inner.gas_cap } - fn state_at(&self, at: BlockId) -> EthResult> { + fn state_at(&self, at: BlockId) -> EthResult { self.state_at_block_id(at) } fn with_state_at_block(&self, at: BlockId, f: F) -> EthResult where - F: FnOnce(StateProviderBox<'_>) -> EthResult, + F: FnOnce(StateProviderBox) -> EthResult, { let state = self.state_at(at)?; f(state) @@ -326,7 +319,7 @@ where async fn spawn_with_state_at_block(&self, at: BlockId, f: F) -> EthResult where - F: FnOnce(StateProviderBox<'_>) -> EthResult + Send + 'static, + F: FnOnce(StateProviderBox) -> EthResult + Send + 'static, T: Send + 'static, { self.spawn_tracing_task_with(move |this| { @@ -595,7 +588,7 @@ where f: F, ) -> EthResult where - F: for<'r> FnOnce(StateCacheDB<'r>, Env) -> EthResult + Send + 'static, + F: FnOnce(StateCacheDB, Env) -> EthResult + Send + 'static, R: Send + 'static, { let (cfg, block_env, at) = self.evm_env_at(at).await?; @@ -638,7 +631,7 @@ where inspector: I, ) -> EthResult<(ResultAndState, Env)> where - I: for<'r> Inspector> + Send + 'static, + I: Inspector + Send + 'static, { self.spawn_with_call_at(request, at, overrides, move |db, env| inspect(db, env, inspector)) .await @@ -672,9 +665,7 @@ where f: F, ) -> EthResult where - F: for<'a> FnOnce(TracingInspector, ResultAndState, StateCacheDB<'a>) -> EthResult - + Send - + 'static, + F: FnOnce(TracingInspector, ResultAndState, StateCacheDB) -> EthResult + Send + 'static, R: Send + 'static, { self.spawn_with_state_at_block(at, move |state| { @@ -712,12 +703,7 @@ where f: F, ) -> EthResult> where - F: for<'a> FnOnce( - TransactionInfo, - TracingInspector, - ResultAndState, - StateCacheDB<'a>, - ) -> EthResult + F: FnOnce(TransactionInfo, TracingInspector, ResultAndState, StateCacheDB) -> EthResult + Send + 'static, R: Send + 'static, @@ -764,7 +750,7 @@ where TracingInspector, ExecutionResult, &'a State, - &'a CacheDB>>, + &'a CacheDB>, ) -> EthResult + Send + 'static, @@ -786,7 +772,7 @@ where TracingInspector, ExecutionResult, &'a State, - &'a CacheDB>>, + &'a CacheDB>, ) -> EthResult + Send + 'static, diff --git a/crates/snapshot/src/segments/headers.rs b/crates/snapshot/src/segments/headers.rs index d6852c73d..2bf73b2f7 100644 --- a/crates/snapshot/src/segments/headers.rs +++ b/crates/snapshot/src/segments/headers.rs @@ -37,7 +37,7 @@ impl Segment for Headers { fn snapshot( &self, - provider: &DatabaseProviderRO<'_, DB>, + provider: &DatabaseProviderRO, directory: impl AsRef, range: RangeInclusive, ) -> ProviderResult<()> { diff --git a/crates/snapshot/src/segments/mod.rs b/crates/snapshot/src/segments/mod.rs index ec9061ebc..88cdc52ef 100644 --- a/crates/snapshot/src/segments/mod.rs +++ b/crates/snapshot/src/segments/mod.rs @@ -31,7 +31,7 @@ pub trait Segment: Default { /// file's save location. fn snapshot( &self, - provider: &DatabaseProviderRO<'_, DB>, + provider: &DatabaseProviderRO, directory: impl AsRef, range: RangeInclusive, ) -> ProviderResult<()>; @@ -42,7 +42,7 @@ pub trait Segment: Default { /// Generates the dataset to train a zstd dictionary with the most recent rows (at most 1000). fn dataset_for_compression>( &self, - provider: &DatabaseProviderRO<'_, DB>, + provider: &DatabaseProviderRO, range: &RangeInclusive, range_len: usize, ) -> ProviderResult>> { @@ -58,7 +58,7 @@ pub trait Segment: Default { /// Returns a [`NippyJar`] according to the desired configuration. The `directory` parameter /// determines the snapshot file's save location. pub(crate) fn prepare_jar( - provider: &DatabaseProviderRO<'_, DB>, + provider: &DatabaseProviderRO, directory: impl AsRef, segment: SnapshotSegment, segment_config: SegmentConfig, diff --git a/crates/snapshot/src/segments/receipts.rs b/crates/snapshot/src/segments/receipts.rs index c40949a0d..4b82a7133 100644 --- a/crates/snapshot/src/segments/receipts.rs +++ b/crates/snapshot/src/segments/receipts.rs @@ -34,7 +34,7 @@ impl Segment for Receipts { fn snapshot( &self, - provider: &DatabaseProviderRO<'_, DB>, + provider: &DatabaseProviderRO, directory: impl AsRef, block_range: RangeInclusive, ) -> ProviderResult<()> { diff --git a/crates/snapshot/src/segments/transactions.rs b/crates/snapshot/src/segments/transactions.rs index 4367f1ce0..585bc9625 100644 --- a/crates/snapshot/src/segments/transactions.rs +++ b/crates/snapshot/src/segments/transactions.rs @@ -34,7 +34,7 @@ impl Segment for Transactions { fn snapshot( &self, - provider: &DatabaseProviderRO<'_, DB>, + provider: &DatabaseProviderRO, directory: impl AsRef, block_range: RangeInclusive, ) -> ProviderResult<()> { diff --git a/crates/snapshot/src/snapshotter.rs b/crates/snapshot/src/snapshotter.rs index d9c1f6aeb..030db93ce 100644 --- a/crates/snapshot/src/snapshotter.rs +++ b/crates/snapshot/src/snapshotter.rs @@ -291,7 +291,7 @@ impl Snapshotter { fn get_snapshot_target_tx_range( &self, - provider: &DatabaseProviderRO<'_, DB>, + provider: &DatabaseProviderRO, block_to_tx_number_cache: &mut HashMap, highest_snapshot: Option, block_range: &RangeInclusive, diff --git a/crates/stages/src/stage.rs b/crates/stages/src/stage.rs index 1fc2b29c1..b165fd070 100644 --- a/crates/stages/src/stage.rs +++ b/crates/stages/src/stage.rs @@ -76,7 +76,7 @@ impl ExecInput { /// the number of transactions exceeds the threshold. pub fn next_block_range_with_transaction_threshold( &self, - provider: &DatabaseProviderRW<'_, DB>, + provider: &DatabaseProviderRW, tx_threshold: u64, ) -> Result<(Range, RangeInclusive, bool), StageError> { let start_block = self.next_block(); @@ -234,14 +234,14 @@ pub trait Stage: Send + Sync { /// upon invoking this method. fn execute( &mut self, - provider: &DatabaseProviderRW<'_, &DB>, + provider: &DatabaseProviderRW<&DB>, input: ExecInput, ) -> Result; /// Unwind the stage. fn unwind( &mut self, - provider: &DatabaseProviderRW<'_, &DB>, + provider: &DatabaseProviderRW<&DB>, input: UnwindInput, ) -> Result; } diff --git a/crates/stages/src/stages/bodies.rs b/crates/stages/src/stages/bodies.rs index 8dc6de073..cb57c44f8 100644 --- a/crates/stages/src/stages/bodies.rs +++ b/crates/stages/src/stages/bodies.rs @@ -98,7 +98,7 @@ impl Stage for BodyStage { /// header, limited by the stage's batch size. fn execute( &mut self, - provider: &DatabaseProviderRW<'_, &DB>, + provider: &DatabaseProviderRW<&DB>, input: ExecInput, ) -> Result { if input.target_reached() { @@ -185,7 +185,7 @@ impl Stage for BodyStage { /// Unwind the stage. fn unwind( &mut self, - provider: &DatabaseProviderRW<'_, &DB>, + provider: &DatabaseProviderRW<&DB>, input: UnwindInput, ) -> Result { self.buffer.take(); @@ -245,7 +245,7 @@ impl Stage for BodyStage { // beforehand how many bytes we need to download. So the good solution would be to measure the // progress in gas as a proxy to size. Execution stage uses a similar approach. fn stage_checkpoint( - provider: &DatabaseProviderRW<'_, DB>, + provider: &DatabaseProviderRW, ) -> Result { Ok(EntitiesCheckpoint { processed: provider.tx_ref().entries::()? as u64, diff --git a/crates/stages/src/stages/execution.rs b/crates/stages/src/stages/execution.rs index d6ffc67df..15a97cb19 100644 --- a/crates/stages/src/stages/execution.rs +++ b/crates/stages/src/stages/execution.rs @@ -110,7 +110,7 @@ impl ExecutionStage { /// Execute the stage. pub fn execute_inner( &mut self, - provider: &DatabaseProviderRW<'_, &DB>, + provider: &DatabaseProviderRW<&DB>, input: ExecInput, ) -> Result { if input.target_reached() { @@ -228,7 +228,7 @@ impl ExecutionStage { /// been previously executed. fn adjust_prune_modes( &self, - provider: &DatabaseProviderRW<'_, &DB>, + provider: &DatabaseProviderRW<&DB>, start_block: u64, max_block: u64, ) -> Result { @@ -247,7 +247,7 @@ impl ExecutionStage { } fn execution_checkpoint( - provider: &DatabaseProviderRW<'_, &DB>, + provider: &DatabaseProviderRW<&DB>, start_block: BlockNumber, max_block: BlockNumber, checkpoint: StageCheckpoint, @@ -314,7 +314,7 @@ fn execution_checkpoint( } fn calculate_gas_used_from_headers( - provider: &DatabaseProviderRW<'_, &DB>, + provider: &DatabaseProviderRW<&DB>, range: RangeInclusive, ) -> Result { let mut gas_total = 0; @@ -340,7 +340,7 @@ impl Stage for ExecutionStage { /// Execute the stage fn execute( &mut self, - provider: &DatabaseProviderRW<'_, &DB>, + provider: &DatabaseProviderRW<&DB>, input: ExecInput, ) -> Result { self.execute_inner(provider, input) @@ -349,7 +349,7 @@ impl Stage for ExecutionStage { /// Unwind the stage. fn unwind( &mut self, - provider: &DatabaseProviderRW<'_, &DB>, + provider: &DatabaseProviderRW<&DB>, input: UnwindInput, ) -> Result { let tx = provider.tx_ref(); diff --git a/crates/stages/src/stages/finish.rs b/crates/stages/src/stages/finish.rs index d0a63e890..26357aedc 100644 --- a/crates/stages/src/stages/finish.rs +++ b/crates/stages/src/stages/finish.rs @@ -18,7 +18,7 @@ impl Stage for FinishStage { fn execute( &mut self, - _provider: &DatabaseProviderRW<'_, &DB>, + _provider: &DatabaseProviderRW<&DB>, input: ExecInput, ) -> Result { Ok(ExecOutput { checkpoint: StageCheckpoint::new(input.target()), done: true }) @@ -26,7 +26,7 @@ impl Stage for FinishStage { fn unwind( &mut self, - _provider: &DatabaseProviderRW<'_, &DB>, + _provider: &DatabaseProviderRW<&DB>, input: UnwindInput, ) -> Result { Ok(UnwindOutput { checkpoint: StageCheckpoint::new(input.unwind_to) }) diff --git a/crates/stages/src/stages/hashing_account.rs b/crates/stages/src/stages/hashing_account.rs index 4eab05e09..fc3424f2e 100644 --- a/crates/stages/src/stages/hashing_account.rs +++ b/crates/stages/src/stages/hashing_account.rs @@ -79,7 +79,7 @@ impl AccountHashingStage { /// Proceeds to go to the `BlockTransitionIndex` end, go back `transitions` and change the /// account state in the `AccountChangeSet` table. pub fn seed( - provider: &DatabaseProviderRW<'_, DB>, + provider: &DatabaseProviderRW, opts: SeedOpts, ) -> Result, StageError> { use reth_db::models::AccountBeforeTx; @@ -134,7 +134,7 @@ impl Stage for AccountHashingStage { /// Execute the stage. fn execute( &mut self, - provider: &DatabaseProviderRW<'_, &DB>, + provider: &DatabaseProviderRW<&DB>, input: ExecInput, ) -> Result { if input.target_reached() { @@ -266,7 +266,7 @@ impl Stage for AccountHashingStage { /// Unwind the stage. fn unwind( &mut self, - provider: &DatabaseProviderRW<'_, &DB>, + provider: &DatabaseProviderRW<&DB>, input: UnwindInput, ) -> Result { let (range, unwind_progress, _) = @@ -288,7 +288,7 @@ impl Stage for AccountHashingStage { } fn stage_checkpoint_progress( - provider: &DatabaseProviderRW<'_, &DB>, + provider: &DatabaseProviderRW<&DB>, ) -> Result { Ok(EntitiesCheckpoint { processed: provider.tx_ref().entries::()? as u64, diff --git a/crates/stages/src/stages/hashing_storage.rs b/crates/stages/src/stages/hashing_storage.rs index da2fd38ac..73d6277a8 100644 --- a/crates/stages/src/stages/hashing_storage.rs +++ b/crates/stages/src/stages/hashing_storage.rs @@ -53,7 +53,7 @@ impl Stage for StorageHashingStage { /// Execute the stage. fn execute( &mut self, - provider: &DatabaseProviderRW<'_, &DB>, + provider: &DatabaseProviderRW<&DB>, input: ExecInput, ) -> Result { let tx = provider.tx_ref(); @@ -192,7 +192,7 @@ impl Stage for StorageHashingStage { /// Unwind the stage. fn unwind( &mut self, - provider: &DatabaseProviderRW<'_, &DB>, + provider: &DatabaseProviderRW<&DB>, input: UnwindInput, ) -> Result { let (range, unwind_progress, _) = @@ -213,7 +213,7 @@ impl Stage for StorageHashingStage { } fn stage_checkpoint_progress( - provider: &DatabaseProviderRW<'_, &DB>, + provider: &DatabaseProviderRW<&DB>, ) -> Result { Ok(EntitiesCheckpoint { processed: provider.tx_ref().entries::()? as u64, diff --git a/crates/stages/src/stages/headers.rs b/crates/stages/src/stages/headers.rs index 40ffa8d94..a150415bc 100644 --- a/crates/stages/src/stages/headers.rs +++ b/crates/stages/src/stages/headers.rs @@ -2,7 +2,7 @@ use crate::{ExecInput, ExecOutput, Stage, StageError, UnwindInput, UnwindOutput} use futures_util::StreamExt; use reth_db::{ cursor::{DbCursorRO, DbCursorRW}, - database::{Database, DatabaseGAT}, + database::Database, tables, transaction::{DbTx, DbTxMut}, }; @@ -60,7 +60,7 @@ where fn is_stage_done( &self, - tx: &>::TXMut, + tx: &::TXMut, checkpoint: u64, ) -> Result { let mut header_cursor = tx.cursor_read::()?; @@ -76,7 +76,7 @@ where /// Note: this writes the headers with rising block numbers. fn write_headers( &self, - tx: &>::TXMut, + tx: &::TXMut, headers: Vec, ) -> Result, StageError> { trace!(target: "sync::stages::headers", len = headers.len(), "writing headers"); @@ -176,7 +176,7 @@ where /// starting from the tip of the chain fn execute( &mut self, - provider: &DatabaseProviderRW<'_, &DB>, + provider: &DatabaseProviderRW<&DB>, input: ExecInput, ) -> Result { let current_checkpoint = input.checkpoint(); @@ -279,7 +279,7 @@ where /// Unwind the stage. fn unwind( &mut self, - provider: &DatabaseProviderRW<'_, &DB>, + provider: &DatabaseProviderRW<&DB>, input: UnwindInput, ) -> Result { self.buffer.take(); diff --git a/crates/stages/src/stages/index_account_history.rs b/crates/stages/src/stages/index_account_history.rs index b1e7721dc..4a0df0c29 100644 --- a/crates/stages/src/stages/index_account_history.rs +++ b/crates/stages/src/stages/index_account_history.rs @@ -44,7 +44,7 @@ impl Stage for IndexAccountHistoryStage { /// Execute the stage. fn execute( &mut self, - provider: &DatabaseProviderRW<'_, &DB>, + provider: &DatabaseProviderRW<&DB>, mut input: ExecInput, ) -> Result { if let Some((target_prunable_block, prune_mode)) = self @@ -87,7 +87,7 @@ impl Stage for IndexAccountHistoryStage { /// Unwind the stage. fn unwind( &mut self, - provider: &DatabaseProviderRW<'_, &DB>, + provider: &DatabaseProviderRW<&DB>, input: UnwindInput, ) -> Result { let (range, unwind_progress, _) = diff --git a/crates/stages/src/stages/index_storage_history.rs b/crates/stages/src/stages/index_storage_history.rs index f9896fb4f..b5ef6fda9 100644 --- a/crates/stages/src/stages/index_storage_history.rs +++ b/crates/stages/src/stages/index_storage_history.rs @@ -43,7 +43,7 @@ impl Stage for IndexStorageHistoryStage { /// Execute the stage. fn execute( &mut self, - provider: &DatabaseProviderRW<'_, &DB>, + provider: &DatabaseProviderRW<&DB>, mut input: ExecInput, ) -> Result { if let Some((target_prunable_block, prune_mode)) = self @@ -85,7 +85,7 @@ impl Stage for IndexStorageHistoryStage { /// Unwind the stage. fn unwind( &mut self, - provider: &DatabaseProviderRW<'_, &DB>, + provider: &DatabaseProviderRW<&DB>, input: UnwindInput, ) -> Result { let (range, unwind_progress, _) = diff --git a/crates/stages/src/stages/merkle.rs b/crates/stages/src/stages/merkle.rs index 4354b5628..602db5723 100644 --- a/crates/stages/src/stages/merkle.rs +++ b/crates/stages/src/stages/merkle.rs @@ -80,7 +80,7 @@ impl MerkleStage { /// Gets the hashing progress pub fn get_execution_checkpoint( &self, - provider: &DatabaseProviderRW<'_, &DB>, + provider: &DatabaseProviderRW<&DB>, ) -> Result, StageError> { let buf = provider.get_stage_checkpoint_progress(StageId::MerkleExecute)?.unwrap_or_default(); @@ -96,7 +96,7 @@ impl MerkleStage { /// Saves the hashing progress pub fn save_execution_checkpoint( &mut self, - provider: &DatabaseProviderRW<'_, &DB>, + provider: &DatabaseProviderRW<&DB>, checkpoint: Option, ) -> Result<(), StageError> { let mut buf = vec![]; @@ -127,7 +127,7 @@ impl Stage for MerkleStage { /// Execute the stage. fn execute( &mut self, - provider: &DatabaseProviderRW<'_, &DB>, + provider: &DatabaseProviderRW<&DB>, input: ExecInput, ) -> Result { let threshold = match self { @@ -261,7 +261,7 @@ impl Stage for MerkleStage { /// Unwind the stage. fn unwind( &mut self, - provider: &DatabaseProviderRW<'_, &DB>, + provider: &DatabaseProviderRW<&DB>, input: UnwindInput, ) -> Result { let tx = provider.tx_ref(); diff --git a/crates/stages/src/stages/mod.rs b/crates/stages/src/stages/mod.rs index 771de3586..8197ec834 100644 --- a/crates/stages/src/stages/mod.rs +++ b/crates/stages/src/stages/mod.rs @@ -124,7 +124,7 @@ mod tests { expect_num_receipts: usize, expect_num_acc_changesets: usize, expect_num_storage_changesets: usize| async move { - let provider: DatabaseProviderRW<'_, &DatabaseEnv> = factory.provider_rw().unwrap(); + let provider: DatabaseProviderRW<&DatabaseEnv> = factory.provider_rw().unwrap(); // Check execution and create receipts and changesets according to the pruning // configuration diff --git a/crates/stages/src/stages/sender_recovery.rs b/crates/stages/src/stages/sender_recovery.rs index cdafd9e62..551a70ccd 100644 --- a/crates/stages/src/stages/sender_recovery.rs +++ b/crates/stages/src/stages/sender_recovery.rs @@ -56,7 +56,7 @@ impl Stage for SenderRecoveryStage { /// the [`TxSenders`][reth_db::tables::TxSenders] table. fn execute( &mut self, - provider: &DatabaseProviderRW<'_, &DB>, + provider: &DatabaseProviderRW<&DB>, input: ExecInput, ) -> Result { if input.target_reached() { @@ -168,7 +168,7 @@ impl Stage for SenderRecoveryStage { /// Unwind the stage. fn unwind( &mut self, - provider: &DatabaseProviderRW<'_, &DB>, + provider: &DatabaseProviderRW<&DB>, input: UnwindInput, ) -> Result { let (_, unwind_to, _) = input.unwind_block_range_with_threshold(self.commit_threshold); @@ -207,7 +207,7 @@ fn recover_sender( } fn stage_checkpoint( - provider: &DatabaseProviderRW<'_, &DB>, + provider: &DatabaseProviderRW<&DB>, ) -> Result { let pruned_entries = provider .get_prune_checkpoint(PruneSegment::SenderRecovery)? diff --git a/crates/stages/src/stages/total_difficulty.rs b/crates/stages/src/stages/total_difficulty.rs index 1cdaa971c..042f1b6a6 100644 --- a/crates/stages/src/stages/total_difficulty.rs +++ b/crates/stages/src/stages/total_difficulty.rs @@ -50,7 +50,7 @@ impl Stage for TotalDifficultyStage { /// Write total difficulty entries fn execute( &mut self, - provider: &DatabaseProviderRW<'_, &DB>, + provider: &DatabaseProviderRW<&DB>, input: ExecInput, ) -> Result { let tx = provider.tx_ref(); @@ -100,7 +100,7 @@ impl Stage for TotalDifficultyStage { /// Unwind the stage. fn unwind( &mut self, - provider: &DatabaseProviderRW<'_, &DB>, + provider: &DatabaseProviderRW<&DB>, input: UnwindInput, ) -> Result { let (_, unwind_to, _) = input.unwind_block_range_with_threshold(self.commit_threshold); @@ -115,7 +115,7 @@ impl Stage for TotalDifficultyStage { } fn stage_checkpoint( - provider: &DatabaseProviderRW<'_, DB>, + provider: &DatabaseProviderRW, ) -> Result { Ok(EntitiesCheckpoint { processed: provider.tx_ref().entries::()? as u64, diff --git a/crates/stages/src/stages/tx_lookup.rs b/crates/stages/src/stages/tx_lookup.rs index 0de9ce74b..f6ef73a9a 100644 --- a/crates/stages/src/stages/tx_lookup.rs +++ b/crates/stages/src/stages/tx_lookup.rs @@ -51,7 +51,7 @@ impl Stage for TransactionLookupStage { /// Write transaction hash -> id entries fn execute( &mut self, - provider: &DatabaseProviderRW<'_, &DB>, + provider: &DatabaseProviderRW<&DB>, mut input: ExecInput, ) -> Result { if let Some((target_prunable_block, prune_mode)) = self @@ -129,7 +129,7 @@ impl Stage for TransactionLookupStage { /// Unwind the stage. fn unwind( &mut self, - provider: &DatabaseProviderRW<'_, &DB>, + provider: &DatabaseProviderRW<&DB>, input: UnwindInput, ) -> Result { let tx = provider.tx_ref(); @@ -164,7 +164,7 @@ impl Stage for TransactionLookupStage { } fn stage_checkpoint( - provider: &DatabaseProviderRW<'_, &DB>, + provider: &DatabaseProviderRW<&DB>, ) -> Result { let pruned_entries = provider .get_prune_checkpoint(PruneSegment::TransactionLookup)? diff --git a/crates/stages/src/test_utils/stage.rs b/crates/stages/src/test_utils/stage.rs index 85e88841b..a73773e6b 100644 --- a/crates/stages/src/test_utils/stage.rs +++ b/crates/stages/src/test_utils/stage.rs @@ -47,7 +47,7 @@ impl Stage for TestStage { fn execute( &mut self, - _: &DatabaseProviderRW<'_, &DB>, + _: &DatabaseProviderRW<&DB>, _input: ExecInput, ) -> Result { self.exec_outputs @@ -57,7 +57,7 @@ impl Stage for TestStage { fn unwind( &mut self, - _: &DatabaseProviderRW<'_, &DB>, + _: &DatabaseProviderRW<&DB>, _input: UnwindInput, ) -> Result { self.unwind_outputs diff --git a/crates/stages/src/test_utils/test_db.rs b/crates/stages/src/test_utils/test_db.rs index 56361f212..586a58c99 100644 --- a/crates/stages/src/test_utils/test_db.rs +++ b/crates/stages/src/test_utils/test_db.rs @@ -1,12 +1,12 @@ use reth_db::{ common::KeyValue, cursor::{DbCursorRO, DbCursorRW, DbDupCursorRO}, - database::DatabaseGAT, + database::Database, models::{AccountBeforeTx, StoredBlockBodyIndices}, table::{Table, TableRow}, tables, test_utils::{create_test_rw_db, create_test_rw_db_with_path, TempDatabase}, - transaction::{DbTx, DbTxGAT, DbTxMut, DbTxMutGAT}, + transaction::{DbTx, DbTxMut}, DatabaseEnv, DatabaseError as DbError, }; use reth_interfaces::{test_utils::generators::ChangeSet, RethResult}; @@ -57,12 +57,12 @@ impl TestTransaction { } /// Return a database wrapped in [DatabaseProviderRW]. - pub fn inner_rw(&self) -> DatabaseProviderRW<'_, Arc>> { + pub fn inner_rw(&self) -> DatabaseProviderRW>> { self.factory.provider_rw().expect("failed to create db container") } /// Return a database wrapped in [DatabaseProviderRO]. - pub fn inner(&self) -> DatabaseProviderRO<'_, Arc>> { + pub fn inner(&self) -> DatabaseProviderRO>> { self.factory.provider().expect("failed to create db container") } @@ -74,7 +74,7 @@ impl TestTransaction { /// Invoke a callback with transaction committing it afterwards pub fn commit(&self, f: F) -> Result<(), DbError> where - F: FnOnce(&>::TXMut) -> Result<(), DbError>, + F: FnOnce(&::TXMut) -> Result<(), DbError>, { let mut tx = self.inner_rw(); f(tx.tx_ref())?; @@ -85,7 +85,7 @@ impl TestTransaction { /// Invoke a callback with a read transaction pub fn query(&self, f: F) -> Result where - F: FnOnce(&>::TX) -> Result, + F: FnOnce(&::TX) -> Result, { f(self.inner().tx_ref()) } diff --git a/crates/storage/db/src/abstraction/common.rs b/crates/storage/db/src/abstraction/common.rs index 29a1e3429..9bce16e39 100644 --- a/crates/storage/db/src/abstraction/common.rs +++ b/crates/storage/db/src/abstraction/common.rs @@ -1,3 +1,5 @@ +use crate::{abstraction::table::*, DatabaseError}; + /// A key-value pair for table `T`. pub type KeyValue = (::Key, ::Value); @@ -16,13 +18,20 @@ pub type IterPairResult = Option, DatabaseError>>; /// A value only result for table `T`. pub type ValueOnlyResult = Result::Value>, DatabaseError>; -use crate::{abstraction::table::*, DatabaseError}; - -// Sealed trait helper to prevent misuse of the API. +// Sealed trait helper to prevent misuse of the Database API. mod sealed { + use crate::{database::Database, mock::DatabaseMock, DatabaseEnv}; + use std::sync::Arc; + + /// Sealed trait to limit the implementors of the Database trait. pub trait Sealed: Sized {} - #[allow(missing_debug_implementations)] - pub struct Bounds(T); - impl Sealed for Bounds {} + + impl Sealed for &DB {} + impl Sealed for Arc {} + impl Sealed for DatabaseEnv {} + impl Sealed for DatabaseMock {} + + #[cfg(any(test, feature = "test-utils"))] + impl Sealed for crate::test_utils::TempDatabase {} } -pub(crate) use sealed::{Bounds, Sealed}; +pub(crate) use sealed::Sealed; diff --git a/crates/storage/db/src/abstraction/database.rs b/crates/storage/db/src/abstraction/database.rs index eacf845bb..e185b4438 100644 --- a/crates/storage/db/src/abstraction/database.rs +++ b/crates/storage/db/src/abstraction/database.rs @@ -1,35 +1,31 @@ use crate::{ - common::{Bounds, Sealed}, + abstraction::common::Sealed, table::TableImporter, transaction::{DbTx, DbTxMut}, DatabaseError, }; use std::{fmt::Debug, sync::Arc}; -/// Implements the GAT method from: -/// . +/// Main Database trait that can open read-only and read-write transactions. /// -/// Sealed trait which cannot be implemented by 3rd parties, exposed only for implementers -pub trait DatabaseGAT<'a, __ImplicitBounds: Sealed = Bounds<&'a Self>>: Send + Sync { - /// RO database transaction - type TX: DbTx + Send + Sync + Debug; - /// RW database transaction - type TXMut: DbTxMut + DbTx + TableImporter + Send + Sync + Debug; -} +/// Sealed trait which cannot be implemented by 3rd parties, exposed only for consumption. +pub trait Database: Send + Sync + Sealed { + /// Read-Only database transaction + type TX: DbTx + Send + Sync + Debug + 'static; + /// Read-Write database transaction + type TXMut: DbTxMut + DbTx + TableImporter + Send + Sync + Debug + 'static; -/// Main Database trait that spawns transactions to be executed. -pub trait Database: for<'a> DatabaseGAT<'a> { /// Create read only transaction. - fn tx(&self) -> Result<>::TX, DatabaseError>; + fn tx(&self) -> Result; /// Create read write transaction only possible if database is open with write access. - fn tx_mut(&self) -> Result<>::TXMut, DatabaseError>; + fn tx_mut(&self) -> Result; /// Takes a function and passes a read-only transaction into it, making sure it's closed in the /// end of the execution. fn view(&self, f: F) -> Result where - F: FnOnce(&>::TX) -> T, + F: FnOnce(&Self::TX) -> T, { let tx = self.tx()?; @@ -43,7 +39,7 @@ pub trait Database: for<'a> DatabaseGAT<'a> { /// the end of the execution. fn update(&self, f: F) -> Result where - F: FnOnce(&>::TXMut) -> T, + F: FnOnce(&Self::TXMut) -> T, { let tx = self.tx_mut()?; @@ -54,34 +50,27 @@ pub trait Database: for<'a> DatabaseGAT<'a> { } } -// Generic over Arc -impl<'a, DB: Database> DatabaseGAT<'a> for Arc { - type TX = >::TX; - type TXMut = >::TXMut; -} - impl Database for Arc { - fn tx(&self) -> Result<>::TX, DatabaseError> { + type TX = ::TX; + type TXMut = ::TXMut; + + fn tx(&self) -> Result { ::tx(self) } - fn tx_mut(&self) -> Result<>::TXMut, DatabaseError> { + fn tx_mut(&self) -> Result { ::tx_mut(self) } } -// Generic over reference -impl<'a, DB: Database> DatabaseGAT<'a> for &DB { - type TX = >::TX; - type TXMut = >::TXMut; -} - impl Database for &DB { - fn tx(&self) -> Result<>::TX, DatabaseError> { + type TX = ::TX; + type TXMut = ::TXMut; + fn tx(&self) -> Result { ::tx(self) } - fn tx_mut(&self) -> Result<>::TXMut, DatabaseError> { + fn tx_mut(&self) -> Result { ::tx_mut(self) } } diff --git a/crates/storage/db/src/abstraction/mock.rs b/crates/storage/db/src/abstraction/mock.rs index c094eb944..f1f0854fb 100644 --- a/crates/storage/db/src/abstraction/mock.rs +++ b/crates/storage/db/src/abstraction/mock.rs @@ -1,17 +1,16 @@ //! Mock database -use std::{collections::BTreeMap, ops::RangeBounds}; - use crate::{ common::{PairResult, ValueOnlyResult}, cursor::{ DbCursorRO, DbCursorRW, DbDupCursorRO, DbDupCursorRW, DupWalker, RangeWalker, ReverseWalker, Walker, }, - database::{Database, DatabaseGAT}, + database::Database, table::{DupSort, Table, TableImporter}, - transaction::{DbTx, DbTxGAT, DbTxMut, DbTxMutGAT}, + transaction::{DbTx, DbTxMut}, DatabaseError, }; +use std::{collections::BTreeMap, ops::RangeBounds}; /// Mock database used for testing with inner BTreeMap structure /// TODO @@ -22,19 +21,15 @@ pub struct DatabaseMock { } impl Database for DatabaseMock { - fn tx(&self) -> Result<>::TX, DatabaseError> { - Ok(TxMock::default()) - } - - fn tx_mut(&self) -> Result<>::TXMut, DatabaseError> { - Ok(TxMock::default()) - } -} - -impl<'a> DatabaseGAT<'a> for DatabaseMock { type TX = TxMock; - type TXMut = TxMock; + fn tx(&self) -> Result { + Ok(TxMock::default()) + } + + fn tx_mut(&self) -> Result { + Ok(TxMock::default()) + } } /// Mock read only tx @@ -44,17 +39,10 @@ pub struct TxMock { _table: BTreeMap, Vec>, } -impl<'a> DbTxGAT<'a> for TxMock { +impl DbTx for TxMock { type Cursor = CursorMock; type DupCursor = CursorMock; -} -impl<'a> DbTxMutGAT<'a> for TxMock { - type CursorMut = CursorMock; - type DupCursorMut = CursorMock; -} - -impl DbTx for TxMock { fn get(&self, _key: T::Key) -> Result, DatabaseError> { todo!() } @@ -65,13 +53,11 @@ impl DbTx for TxMock { fn abort(self) {} - fn cursor_read(&self) -> Result<>::Cursor, DatabaseError> { + fn cursor_read(&self) -> Result, DatabaseError> { Ok(CursorMock { _cursor: 0 }) } - fn cursor_dup_read( - &self, - ) -> Result<>::DupCursor, DatabaseError> { + fn cursor_dup_read(&self) -> Result, DatabaseError> { Ok(CursorMock { _cursor: 0 }) } @@ -81,6 +67,9 @@ impl DbTx for TxMock { } impl DbTxMut for TxMock { + type CursorMut = CursorMock; + type DupCursorMut = CursorMock; + fn put(&self, _key: T::Key, _value: T::Value) -> Result<(), DatabaseError> { todo!() } @@ -97,15 +86,11 @@ impl DbTxMut for TxMock { todo!() } - fn cursor_write( - &self, - ) -> Result<>::CursorMut, DatabaseError> { + fn cursor_write(&self) -> Result, DatabaseError> { todo!() } - fn cursor_dup_write( - &self, - ) -> Result<>::DupCursorMut, DatabaseError> { + fn cursor_dup_write(&self) -> Result, DatabaseError> { todo!() } } diff --git a/crates/storage/db/src/abstraction/transaction.rs b/crates/storage/db/src/abstraction/transaction.rs index bbbd775d7..472563f35 100644 --- a/crates/storage/db/src/abstraction/transaction.rs +++ b/crates/storage/db/src/abstraction/transaction.rs @@ -1,39 +1,16 @@ use crate::{ - common::{Bounds, Sealed}, cursor::{DbCursorRO, DbCursorRW, DbDupCursorRO, DbDupCursorRW}, table::{DupSort, Table}, DatabaseError, }; -/// Implements the GAT method from: -/// . -/// -/// Sealed trait which cannot be implemented by 3rd parties, exposed only for implementers -pub trait DbTxGAT<'a, __ImplicitBounds: Sealed = Bounds<&'a Self>>: Send + Sync { - /// Cursor GAT - type Cursor: DbCursorRO + Send + Sync; - /// DupCursor GAT - type DupCursor: DbDupCursorRO + DbCursorRO + Send + Sync; -} - -/// Implements the GAT method from: -/// . -/// -/// Sealed trait which cannot be implemented by 3rd parties, exposed only for implementers -pub trait DbTxMutGAT<'a, __ImplicitBounds: Sealed = Bounds<&'a Self>>: Send + Sync { - /// Cursor GAT - type CursorMut: DbCursorRW + DbCursorRO + Send + Sync; - /// DupCursor GAT - type DupCursorMut: DbDupCursorRW - + DbCursorRW - + DbDupCursorRO - + DbCursorRO - + Send - + Sync; -} - /// Read only transaction -pub trait DbTx: for<'a> DbTxGAT<'a> { +pub trait DbTx: Send + Sync { + /// Cursor type for this read-only transaction + type Cursor: DbCursorRO + Send + Sync; + /// DupCursor type for this read-only transaction + type DupCursor: DbDupCursorRO + DbCursorRO + Send + Sync; + /// Get value fn get(&self, key: T::Key) -> Result, DatabaseError>; /// Commit for read only transaction will consume and free transaction and allows @@ -42,17 +19,25 @@ pub trait DbTx: for<'a> DbTxGAT<'a> { /// Aborts transaction fn abort(self); /// Iterate over read only values in table. - fn cursor_read(&self) -> Result<>::Cursor, DatabaseError>; + fn cursor_read(&self) -> Result, DatabaseError>; /// Iterate over read only values in dup sorted table. - fn cursor_dup_read( - &self, - ) -> Result<>::DupCursor, DatabaseError>; + fn cursor_dup_read(&self) -> Result, DatabaseError>; /// Returns number of entries in the table. fn entries(&self) -> Result; } /// Read write transaction that allows writing to database -pub trait DbTxMut: for<'a> DbTxMutGAT<'a> { +pub trait DbTxMut: Send + Sync { + /// Read-Write Cursor type + type CursorMut: DbCursorRW + DbCursorRO + Send + Sync; + /// Read-Write DupCursor type + type DupCursorMut: DbDupCursorRW + + DbCursorRW + + DbDupCursorRO + + DbCursorRO + + Send + + Sync; + /// Put value to database fn put(&self, key: T::Key, value: T::Value) -> Result<(), DatabaseError>; /// Delete value from database @@ -61,11 +46,7 @@ pub trait DbTxMut: for<'a> DbTxMutGAT<'a> { /// Clears database. fn clear(&self) -> Result<(), DatabaseError>; /// Cursor mut - fn cursor_write( - &self, - ) -> Result<>::CursorMut, DatabaseError>; + fn cursor_write(&self) -> Result, DatabaseError>; /// DupCursor mut. - fn cursor_dup_write( - &self, - ) -> Result<>::DupCursorMut, DatabaseError>; + fn cursor_dup_write(&self) -> Result, DatabaseError>; } diff --git a/crates/storage/db/src/implementation/mdbx/mod.rs b/crates/storage/db/src/implementation/mdbx/mod.rs index e895b4015..3f6a5db88 100644 --- a/crates/storage/db/src/implementation/mdbx/mod.rs +++ b/crates/storage/db/src/implementation/mdbx/mod.rs @@ -1,7 +1,7 @@ //! Module that interacts with MDBX. use crate::{ - database::{Database, DatabaseGAT}, + database::Database, tables::{TableType, Tables}, utils::default_page_size, DatabaseError, @@ -40,20 +40,18 @@ pub struct DatabaseEnv { with_metrics: bool, } -impl<'a> DatabaseGAT<'a> for DatabaseEnv { +impl Database for DatabaseEnv { type TX = tx::Tx; type TXMut = tx::Tx; -} -impl Database for DatabaseEnv { - fn tx(&self) -> Result<>::TX, DatabaseError> { + fn tx(&self) -> Result { Ok(Tx::new_with_metrics( self.inner.begin_ro_txn().map_err(|e| DatabaseError::InitTx(e.into()))?, self.with_metrics, )) } - fn tx_mut(&self) -> Result<>::TXMut, DatabaseError> { + fn tx_mut(&self) -> Result { Ok(Tx::new_with_metrics( self.inner.begin_rw_txn().map_err(|e| DatabaseError::InitTx(e.into()))?, self.with_metrics, diff --git a/crates/storage/db/src/implementation/mdbx/tx.rs b/crates/storage/db/src/implementation/mdbx/tx.rs index c62a54e67..8da3df45c 100644 --- a/crates/storage/db/src/implementation/mdbx/tx.rs +++ b/crates/storage/db/src/implementation/mdbx/tx.rs @@ -7,7 +7,7 @@ use crate::{ }, table::{Compress, DupSort, Encode, Table, TableImporter}, tables::{utils::decode_one, Tables, NUM_TABLES}, - transaction::{DbTx, DbTxGAT, DbTxMut, DbTxMutGAT}, + transaction::{DbTx, DbTxMut}, DatabaseError, }; use parking_lot::RwLock; @@ -167,19 +167,12 @@ impl Drop for MetricsHandler { } } -impl<'a, K: TransactionKind> DbTxGAT<'a> for Tx { - type Cursor = Cursor; - type DupCursor = Cursor; -} - -impl<'a, K: TransactionKind> DbTxMutGAT<'a> for Tx { - type CursorMut = Cursor; - type DupCursorMut = Cursor; -} - impl TableImporter for Tx {} impl DbTx for Tx { + type Cursor = Cursor; + type DupCursor = Cursor; + fn get(&self, key: T::Key) -> Result::Value>, DatabaseError> { self.execute_with_operation_metric::(Operation::Get, None, |tx| { tx.get(self.get_dbi::()?, key.encode().as_ref()) @@ -202,14 +195,12 @@ impl DbTx for Tx { } // Iterate over read only values in database. - fn cursor_read(&self) -> Result<>::Cursor, DatabaseError> { + fn cursor_read(&self) -> Result, DatabaseError> { self.new_cursor() } /// Iterate over read only values in database. - fn cursor_dup_read( - &self, - ) -> Result<>::DupCursor, DatabaseError> { + fn cursor_dup_read(&self) -> Result, DatabaseError> { self.new_cursor() } @@ -224,6 +215,9 @@ impl DbTx for Tx { } impl DbTxMut for Tx { + type CursorMut = Cursor; + type DupCursorMut = Cursor; + fn put(&self, key: T::Key, value: T::Value) -> Result<(), DatabaseError> { let key = key.encode(); let value = value.compress(); @@ -268,15 +262,11 @@ impl DbTxMut for Tx { Ok(()) } - fn cursor_write( - &self, - ) -> Result<>::CursorMut, DatabaseError> { + fn cursor_write(&self) -> Result, DatabaseError> { self.new_cursor() } - fn cursor_dup_write( - &self, - ) -> Result<>::DupCursorMut, DatabaseError> { + fn cursor_dup_write(&self) -> Result, DatabaseError> { self.new_cursor() } } diff --git a/crates/storage/db/src/lib.rs b/crates/storage/db/src/lib.rs index 250177dfb..e813bf0d1 100644 --- a/crates/storage/db/src/lib.rs +++ b/crates/storage/db/src/lib.rs @@ -153,7 +153,7 @@ pub fn open_db(path: &Path, log_level: Option) -> eyre::Result DatabaseGAT<'a> for TempDatabase { - type TX = >::TX; - type TXMut = >::TXMut; - } - impl Database for TempDatabase { - fn tx(&self) -> Result<>::TX, DatabaseError> { + type TX = ::TX; + type TXMut = ::TXMut; + fn tx(&self) -> Result { self.db().tx() } - fn tx_mut(&self) -> Result<>::TXMut, DatabaseError> { + fn tx_mut(&self) -> Result { self.db().tx_mut() } } diff --git a/crates/storage/provider/src/providers/database/mod.rs b/crates/storage/provider/src/providers/database/mod.rs index c21cbdd68..48fa6eaa0 100644 --- a/crates/storage/provider/src/providers/database/mod.rs +++ b/crates/storage/provider/src/providers/database/mod.rs @@ -50,7 +50,7 @@ impl ProviderFactory { /// Returns a provider with a created `DbTx` inside, which allows fetching data from the /// database using different types of providers. Example: [`HeaderProvider`] /// [`BlockHashReader`]. This may fail if the inner read database transaction fails to open. - pub fn provider(&self) -> ProviderResult> { + pub fn provider(&self) -> ProviderResult> { let mut provider = DatabaseProvider::new(self.db.tx()?, self.chain_spec.clone()); if let Some(snapshot_provider) = &self.snapshot_provider { @@ -64,7 +64,7 @@ impl ProviderFactory { /// data from the database using different types of providers. Example: [`HeaderProvider`] /// [`BlockHashReader`]. This may fail if the inner read/write database transaction fails to /// open. - pub fn provider_rw(&self) -> ProviderResult> { + pub fn provider_rw(&self) -> ProviderResult> { let mut provider = DatabaseProvider::new_rw(self.db.tx_mut()?, self.chain_spec.clone()); if let Some(snapshot_provider) = &self.snapshot_provider { @@ -123,7 +123,7 @@ impl Clone for ProviderFactory { impl ProviderFactory { /// Storage provider for latest block - pub fn latest(&self) -> ProviderResult> { + pub fn latest(&self) -> ProviderResult { trace!(target: "providers::db", "Returning latest state provider"); Ok(Box::new(LatestStateProvider::new(self.db.tx()?))) } @@ -132,7 +132,7 @@ impl ProviderFactory { fn state_provider_by_block_number( &self, mut block_number: BlockNumber, - ) -> ProviderResult> { + ) -> ProviderResult { let provider = self.provider()?; if block_number == provider.best_block_number().unwrap_or_default() && @@ -175,17 +175,14 @@ impl ProviderFactory { pub fn history_by_block_number( &self, block_number: BlockNumber, - ) -> ProviderResult> { + ) -> ProviderResult { let state_provider = self.state_provider_by_block_number(block_number)?; trace!(target: "providers::db", ?block_number, "Returning historical state provider for block number"); Ok(state_provider) } /// Storage provider for state at that given block hash - pub fn history_by_block_hash( - &self, - block_hash: BlockHash, - ) -> ProviderResult> { + pub fn history_by_block_hash(&self, block_hash: BlockHash) -> ProviderResult { let block_number = self .provider()? .block_number(block_hash)? diff --git a/crates/storage/provider/src/providers/database/provider.rs b/crates/storage/provider/src/providers/database/provider.rs index ad289f198..81daf53de 100644 --- a/crates/storage/provider/src/providers/database/provider.rs +++ b/crates/storage/provider/src/providers/database/provider.rs @@ -14,7 +14,7 @@ use itertools::{izip, Itertools}; use reth_db::{ common::KeyValue, cursor::{DbCursorRO, DbCursorRW, DbDupCursorRO}, - database::{Database, DatabaseGAT}, + database::Database, models::{ sharded_key, storage_sharded_key::StorageShardedKey, AccountBeforeTx, BlockNumberAddress, ShardedKey, StoredBlockBodyIndices, StoredBlockOmmers, StoredBlockWithdrawals, @@ -55,39 +55,37 @@ use std::{ use tracing::{debug, warn}; /// A [`DatabaseProvider`] that holds a read-only database transaction. -pub type DatabaseProviderRO<'this, DB> = DatabaseProvider<>::TX>; +pub type DatabaseProviderRO = DatabaseProvider<::TX>; /// A [`DatabaseProvider`] that holds a read-write database transaction. /// /// Ideally this would be an alias type. However, there's some weird compiler error (), that forces us to wrap this in a struct instead. /// Once that issue is solved, we can probably revert back to being an alias type. #[derive(Debug)] -pub struct DatabaseProviderRW<'this, DB: Database>( - pub DatabaseProvider<>::TXMut>, -); +pub struct DatabaseProviderRW(pub DatabaseProvider<::TXMut>); -impl<'this, DB: Database> Deref for DatabaseProviderRW<'this, DB> { - type Target = DatabaseProvider<>::TXMut>; +impl Deref for DatabaseProviderRW { + type Target = DatabaseProvider<::TXMut>; fn deref(&self) -> &Self::Target { &self.0 } } -impl DerefMut for DatabaseProviderRW<'_, DB> { +impl DerefMut for DatabaseProviderRW { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } } -impl<'this, DB: Database> DatabaseProviderRW<'this, DB> { +impl DatabaseProviderRW { /// Commit database transaction pub fn commit(self) -> ProviderResult { self.0.commit() } /// Consume `DbTx` or `DbTxMut`. - pub fn into_tx(self) -> >::TXMut { + pub fn into_tx(self) -> ::TXMut { self.0.into_tx() } } diff --git a/crates/storage/provider/src/providers/mod.rs b/crates/storage/provider/src/providers/mod.rs index 528577963..df782365e 100644 --- a/crates/storage/provider/src/providers/mod.rs +++ b/crates/storage/provider/src/providers/mod.rs @@ -508,7 +508,7 @@ where Tree: BlockchainTreePendingStateProvider + BlockchainTreeViewer, { /// Storage provider for latest block - fn latest(&self) -> ProviderResult> { + fn latest(&self) -> ProviderResult { trace!(target: "providers::blockchain", "Getting latest block state provider"); self.database.latest() } @@ -516,18 +516,18 @@ where fn history_by_block_number( &self, block_number: BlockNumber, - ) -> ProviderResult> { + ) -> ProviderResult { trace!(target: "providers::blockchain", ?block_number, "Getting history by block number"); self.ensure_canonical_block(block_number)?; self.database.history_by_block_number(block_number) } - fn history_by_block_hash(&self, block_hash: BlockHash) -> ProviderResult> { + fn history_by_block_hash(&self, block_hash: BlockHash) -> ProviderResult { trace!(target: "providers::blockchain", ?block_hash, "Getting history by block hash"); self.database.history_by_block_hash(block_hash) } - fn state_by_block_hash(&self, block: BlockHash) -> ProviderResult> { + fn state_by_block_hash(&self, block: BlockHash) -> ProviderResult { trace!(target: "providers::blockchain", ?block, "Getting state by block hash"); let mut state = self.history_by_block_hash(block); @@ -546,7 +546,7 @@ where /// /// If there's no pending block available then the latest state provider is returned: /// [Self::latest] - fn pending(&self) -> ProviderResult> { + fn pending(&self) -> ProviderResult { trace!(target: "providers::blockchain", "Getting provider for pending state"); if let Some(block) = self.tree.pending_block_num_hash() { @@ -559,10 +559,7 @@ where self.latest() } - fn pending_state_by_hash( - &self, - block_hash: B256, - ) -> ProviderResult>> { + fn pending_state_by_hash(&self, block_hash: B256) -> ProviderResult> { if let Some(state) = self.tree.find_pending_state_provider(block_hash) { return Ok(Some(self.pending_with_provider(state)?)) } @@ -572,7 +569,7 @@ where fn pending_with_provider( &self, bundle_state_data: Box, - ) -> ProviderResult> { + ) -> ProviderResult { let canonical_fork = bundle_state_data.canonical_fork(); trace!(target: "providers::blockchain", ?canonical_fork, "Returning post state provider"); diff --git a/crates/storage/provider/src/test_utils/blocks.rs b/crates/storage/provider/src/test_utils/blocks.rs index 30ee18f6c..3162266bc 100644 --- a/crates/storage/provider/src/test_utils/blocks.rs +++ b/crates/storage/provider/src/test_utils/blocks.rs @@ -10,7 +10,7 @@ use reth_primitives::{ use std::collections::HashMap; /// Assert genesis block -pub fn assert_genesis_block(provider: &DatabaseProviderRW<'_, DB>, g: SealedBlock) { +pub fn assert_genesis_block(provider: &DatabaseProviderRW, g: SealedBlock) { let n = g.number; let h = B256::ZERO; let tx = provider; diff --git a/crates/storage/provider/src/test_utils/mock.rs b/crates/storage/provider/src/test_utils/mock.rs index f32b4fd81..b9f3dd667 100644 --- a/crates/storage/provider/src/test_utils/mock.rs +++ b/crates/storage/provider/src/test_utils/mock.rs @@ -581,73 +581,67 @@ impl EvmEnvProvider for MockEthProvider { } impl StateProviderFactory for MockEthProvider { - fn latest(&self) -> ProviderResult> { + fn latest(&self) -> ProviderResult { Ok(Box::new(self.clone())) } - fn history_by_block_number(&self, _block: BlockNumber) -> ProviderResult> { + fn history_by_block_number(&self, _block: BlockNumber) -> ProviderResult { Ok(Box::new(self.clone())) } - fn history_by_block_hash(&self, _block: BlockHash) -> ProviderResult> { + fn history_by_block_hash(&self, _block: BlockHash) -> ProviderResult { Ok(Box::new(self.clone())) } - fn state_by_block_hash(&self, _block: BlockHash) -> ProviderResult> { + fn state_by_block_hash(&self, _block: BlockHash) -> ProviderResult { Ok(Box::new(self.clone())) } - fn pending(&self) -> ProviderResult> { + fn pending(&self) -> ProviderResult { Ok(Box::new(self.clone())) } - fn pending_state_by_hash( - &self, - _block_hash: B256, - ) -> ProviderResult>> { + fn pending_state_by_hash(&self, _block_hash: B256) -> ProviderResult> { Ok(Some(Box::new(self.clone()))) } fn pending_with_provider<'a>( &'a self, _bundle_state_data: Box, - ) -> ProviderResult> { + ) -> ProviderResult { Ok(Box::new(self.clone())) } } impl StateProviderFactory for Arc { - fn latest(&self) -> ProviderResult> { + fn latest(&self) -> ProviderResult { Ok(Box::new(self.clone())) } - fn history_by_block_number(&self, _block: BlockNumber) -> ProviderResult> { + fn history_by_block_number(&self, _block: BlockNumber) -> ProviderResult { Ok(Box::new(self.clone())) } - fn history_by_block_hash(&self, _block: BlockHash) -> ProviderResult> { + fn history_by_block_hash(&self, _block: BlockHash) -> ProviderResult { Ok(Box::new(self.clone())) } - fn state_by_block_hash(&self, _block: BlockHash) -> ProviderResult> { + fn state_by_block_hash(&self, _block: BlockHash) -> ProviderResult { Ok(Box::new(self.clone())) } - fn pending(&self) -> ProviderResult> { + fn pending(&self) -> ProviderResult { Ok(Box::new(self.clone())) } - fn pending_state_by_hash( - &self, - _block_hash: B256, - ) -> ProviderResult>> { + fn pending_state_by_hash(&self, _block_hash: B256) -> ProviderResult> { Ok(Some(Box::new(self.clone()))) } fn pending_with_provider<'a>( &'a self, _bundle_state_data: Box, - ) -> ProviderResult> { + ) -> ProviderResult { Ok(Box::new(self.clone())) } } diff --git a/crates/storage/provider/src/test_utils/noop.rs b/crates/storage/provider/src/test_utils/noop.rs index 7c2b761ce..87632f1d9 100644 --- a/crates/storage/provider/src/test_utils/noop.rs +++ b/crates/storage/provider/src/test_utils/noop.rs @@ -347,37 +347,34 @@ impl EvmEnvProvider for NoopProvider { } impl StateProviderFactory for NoopProvider { - fn latest(&self) -> ProviderResult> { + fn latest(&self) -> ProviderResult { Ok(Box::new(*self)) } - fn history_by_block_number(&self, _block: BlockNumber) -> ProviderResult> { + fn history_by_block_number(&self, _block: BlockNumber) -> ProviderResult { Ok(Box::new(*self)) } - fn history_by_block_hash(&self, _block: BlockHash) -> ProviderResult> { + fn history_by_block_hash(&self, _block: BlockHash) -> ProviderResult { Ok(Box::new(*self)) } - fn state_by_block_hash(&self, _block: BlockHash) -> ProviderResult> { + fn state_by_block_hash(&self, _block: BlockHash) -> ProviderResult { Ok(Box::new(*self)) } - fn pending(&self) -> ProviderResult> { + fn pending(&self) -> ProviderResult { Ok(Box::new(*self)) } - fn pending_state_by_hash( - &self, - _block_hash: B256, - ) -> ProviderResult>> { + fn pending_state_by_hash(&self, _block_hash: B256) -> ProviderResult> { Ok(Some(Box::new(*self))) } fn pending_with_provider<'a>( &'a self, _bundle_state_data: Box, - ) -> ProviderResult> { + ) -> ProviderResult { Ok(Box::new(*self)) } } diff --git a/crates/storage/provider/src/traits/state.rs b/crates/storage/provider/src/traits/state.rs index 158ec2bb4..8cb985f35 100644 --- a/crates/storage/provider/src/traits/state.rs +++ b/crates/storage/provider/src/traits/state.rs @@ -9,7 +9,7 @@ use reth_primitives::{ use reth_trie::updates::TrieUpdates; /// Type alias of boxed [StateProvider]. -pub type StateProviderBox<'a> = Box; +pub type StateProviderBox = Box; /// An abstraction for a type that provides state data. #[auto_impl(&, Arc, Box)] @@ -100,13 +100,13 @@ pub trait StateProvider: BlockHashReader + AccountReader + StateRootProvider + S /// to be used, since block `n` was executed on its parent block's state. pub trait StateProviderFactory: BlockIdReader + Send + Sync { /// Storage provider for latest block. - fn latest(&self) -> ProviderResult>; + fn latest(&self) -> ProviderResult; /// Returns a [StateProvider] indexed by the given [BlockId]. /// /// Note: if a number or hash is provided this will __only__ look at historical(canonical) /// state. - fn state_by_block_id(&self, block_id: BlockId) -> ProviderResult> { + fn state_by_block_id(&self, block_id: BlockId) -> ProviderResult { match block_id { BlockId::Number(block_number) => self.state_by_block_number_or_tag(block_number), BlockId::Hash(block_hash) => self.history_by_block_hash(block_hash.into()), @@ -119,7 +119,7 @@ pub trait StateProviderFactory: BlockIdReader + Send + Sync { fn state_by_block_number_or_tag( &self, number_or_tag: BlockNumberOrTag, - ) -> ProviderResult> { + ) -> ProviderResult { match number_or_tag { BlockNumberOrTag::Latest => self.latest(), BlockNumberOrTag::Finalized => { @@ -153,40 +153,37 @@ pub trait StateProviderFactory: BlockIdReader + Send + Sync { /// /// /// Note: this only looks at historical blocks, not pending blocks. - fn history_by_block_number(&self, block: BlockNumber) -> ProviderResult>; + fn history_by_block_number(&self, block: BlockNumber) -> ProviderResult; /// Returns a historical [StateProvider] indexed by the given block hash. /// /// Note: this only looks at historical blocks, not pending blocks. - fn history_by_block_hash(&self, block: BlockHash) -> ProviderResult>; + fn history_by_block_hash(&self, block: BlockHash) -> ProviderResult; /// Returns _any_[StateProvider] with matching block hash. /// /// This will return a [StateProvider] for either a historical or pending block. - fn state_by_block_hash(&self, block: BlockHash) -> ProviderResult>; + fn state_by_block_hash(&self, block: BlockHash) -> ProviderResult; /// Storage provider for pending state. /// /// Represents the state at the block that extends the canonical chain by one. /// If there's no `pending` block, then this is equal to [StateProviderFactory::latest] - fn pending(&self) -> ProviderResult>; + fn pending(&self) -> ProviderResult; /// Storage provider for pending state for the given block hash. /// /// Represents the state at the block that extends the canonical chain. /// /// If the block couldn't be found, returns `None`. - fn pending_state_by_hash( - &self, - block_hash: B256, - ) -> ProviderResult>>; + fn pending_state_by_hash(&self, block_hash: B256) -> ProviderResult>; /// Return a [StateProvider] that contains bundle state data provider. /// Used to inspect or execute transaction on the pending state. fn pending_with_provider( &self, bundle_state_data: Box, - ) -> ProviderResult>; + ) -> ProviderResult; } /// Blockchain trait provider that gives access to the blockchain state that is not yet committed diff --git a/crates/trie/src/hashed_cursor/default.rs b/crates/trie/src/hashed_cursor/default.rs index 5641c2892..d49feedd1 100644 --- a/crates/trie/src/hashed_cursor/default.rs +++ b/crates/trie/src/hashed_cursor/default.rs @@ -2,13 +2,13 @@ use super::{HashedAccountCursor, HashedCursorFactory, HashedStorageCursor}; use reth_db::{ cursor::{DbCursorRO, DbDupCursorRO}, tables, - transaction::{DbTx, DbTxGAT}, + transaction::DbTx, }; use reth_primitives::{Account, StorageEntry, B256}; impl<'a, TX: DbTx> HashedCursorFactory for &'a TX { - type AccountCursor = >::Cursor; - type StorageCursor = >::DupCursor; + type AccountCursor = ::Cursor; + type StorageCursor = ::DupCursor; fn hashed_account_cursor(&self) -> Result { self.cursor_read::() diff --git a/crates/trie/src/hashed_cursor/post_state.rs b/crates/trie/src/hashed_cursor/post_state.rs index b6dff3027..62e028657 100644 --- a/crates/trie/src/hashed_cursor/post_state.rs +++ b/crates/trie/src/hashed_cursor/post_state.rs @@ -3,7 +3,7 @@ use crate::prefix_set::{PrefixSet, PrefixSetMut}; use reth_db::{ cursor::{DbCursorRO, DbDupCursorRO}, tables, - transaction::{DbTx, DbTxGAT}, + transaction::DbTx, }; use reth_primitives::{trie::Nibbles, Account, StorageEntry, B256, U256}; use std::collections::{HashMap, HashSet}; @@ -171,9 +171,9 @@ impl<'a, 'b, TX> HashedPostStateCursorFactory<'a, 'b, TX> { impl<'a, 'b, TX: DbTx> HashedCursorFactory for HashedPostStateCursorFactory<'a, 'b, TX> { type AccountCursor = - HashedPostStateAccountCursor<'b, >::Cursor>; + HashedPostStateAccountCursor<'b, ::Cursor>; type StorageCursor = - HashedPostStateStorageCursor<'b, >::DupCursor>; + HashedPostStateStorageCursor<'b, ::DupCursor>; fn hashed_account_cursor(&self) -> Result { let cursor = self.tx.cursor_read::()?; diff --git a/crates/trie/src/trie.rs b/crates/trie/src/trie.rs index 7a7d3a5a0..b1ca79681 100644 --- a/crates/trie/src/trie.rs +++ b/crates/trie/src/trie.rs @@ -1254,7 +1254,7 @@ mod tests { } fn extension_node_storage_trie( - tx: &DatabaseProviderRW<'_, &DatabaseEnv>, + tx: &DatabaseProviderRW<&DatabaseEnv>, hashed_address: B256, ) -> (B256, HashMap) { let value = U256::from(1); @@ -1282,7 +1282,7 @@ mod tests { (root, updates) } - fn extension_node_trie(tx: &DatabaseProviderRW<'_, &DatabaseEnv>) -> B256 { + fn extension_node_trie(tx: &DatabaseProviderRW<&DatabaseEnv>) -> B256 { let a = Account { nonce: 0, balance: U256::from(1u64), bytecode_hash: Some(B256::random()) }; let val = encode_account(a, None); diff --git a/docs/crates/db.md b/docs/crates/db.md index 679ddcb01..cf0161d2b 100644 --- a/docs/crates/db.md +++ b/docs/crates/db.md @@ -65,24 +65,23 @@ There are many tables within the node, all used to store different types of data ## Database -Reth's database design revolves around it's main [Database trait](https://github.com/paradigmxyz/reth/blob/eaca2a4a7fbbdc2f5cd15eab9a8a18ede1891bda/crates/storage/db/src/abstraction/database.rs#L21), which takes advantage of [generic associated types](https://blog.rust-lang.org/2022/10/28/gats-stabilization.html) and [a few design tricks](https://sabrinajewson.org/blog/the-better-alternative-to-lifetime-gats#the-better-gats) to implement the database's functionality across many types. Let's take a quick look at the `Database` trait and how it works. +Reth's database design revolves around it's main [Database trait](https://github.com/paradigmxyz/reth/blob/eaca2a4a7fbbdc2f5cd15eab9a8a18ede1891bda/crates/storage/db/src/abstraction/database.rs#L21), which implements the database's functionality across many types. Let's take a quick look at the `Database` trait and how it works. [File: crates/storage/db/src/abstraction/database.rs](https://github.com/paradigmxyz/reth/blob/eaca2a4a7fbbdc2f5cd15eab9a8a18ede1891bda/crates/storage/db/src/abstraction/database.rs#L21) ```rust ignore /// Main Database trait that spawns transactions to be executed. -pub trait Database: for<'a> DatabaseGAT<'a> { - /// Create read only transaction. - fn tx(&self) -> Result<>::TX, Error>; - - /// Create read write transaction only possible if database is open with write access. - fn tx_mut(&self) -> Result<>::TXMut, Error>; +pub trait Database { + /// RO database transaction + type TX: DbTx + Send + Sync + Debug; + /// RW database transaction + type TXMut: DbTxMut + DbTx + TableImporter + Send + Sync + Debug; /// Takes a function and passes a read-only transaction into it, making sure it's closed in the /// end of the execution. fn view(&self, f: F) -> Result where - F: Fn(&>::TX) -> T, + F: Fn(&::TX) -> T, { let tx = self.tx()?; @@ -96,7 +95,7 @@ pub trait Database: for<'a> DatabaseGAT<'a> { /// the end of the execution. fn update(&self, f: F) -> Result where - F: Fn(&>::TXMut) -> T, + F: Fn(&::TXMut) -> T, { let tx = self.tx_mut()?; @@ -116,7 +115,7 @@ Any type that implements the `Database` trait can create a database transaction, pub struct Transaction<'this, DB: Database> { /// A handle to the DB. pub(crate) db: &'this DB, - tx: Option<>::TXMut>, + tx: Option<::TXMut>, } //--snip-- @@ -134,26 +133,10 @@ where } ``` -The `Database` trait also implements the `DatabaseGAT` trait which defines two associated types `TX` and `TXMut`. +The `Database` defines two associated types `TX` and `TXMut`. [File: crates/storage/db/src/abstraction/database.rs](https://github.com/paradigmxyz/reth/blob/main/crates/storage/db/src/abstraction/database.rs#L11) -```rust ignore -/// Implements the GAT method from: -/// https://sabrinajewson.org/blog/the-better-alternative-to-lifetime-gats#the-better-gats. -/// -/// Sealed trait which cannot be implemented by 3rd parties, exposed only for implementers -pub trait DatabaseGAT<'a, __ImplicitBounds: Sealed = Bounds<&'a Self>>: Send + Sync { - /// RO database transaction - type TX: DbTx + Send + Sync; - /// RW database transaction - type TXMut: DbTxMut + DbTx + Send + Sync; -} -``` - -In Rust, associated types are like generics in that they can be any type fitting the generic's definition, with the difference being that associated types are associated with a trait and can only be used in the context of that trait. - -In the code snippet above, the `DatabaseGAT` trait has two associated types, `TX` and `TXMut`. The `TX` type can be any type that implements the `DbTx` trait, which provides a set of functions to interact with read only transactions. @@ -161,26 +144,40 @@ The `TX` type can be any type that implements the `DbTx` trait, which provides a ```rust ignore /// Read only transaction -pub trait DbTx: for<'a> DbTxGAT<'a> { +pub trait DbTx: Send + Sync { + /// Cursor type for this read-only transaction + type Cursor: DbCursorRO + Send + Sync; + /// DupCursor type for this read-only transaction + type DupCursor: DbDupCursorRO + DbCursorRO + Send + Sync; + /// Get value fn get(&self, key: T::Key) -> Result, Error>; /// Commit for read only transaction will consume and free transaction and allows /// freeing of memory pages fn commit(self) -> Result; /// Iterate over read only values in table. - fn cursor(&self) -> Result<>::Cursor, Error>; + fn cursor(&self) -> Result, Error>; /// Iterate over read only values in dup sorted table. - fn cursor_dup(&self) -> Result<>::DupCursor, Error>; + fn cursor_dup(&self) -> Result, Error>; } ``` -The `TXMut` type can be any type that implements the `DbTxMut` trait, which provides a set of functions to interact with read/write transactions. +The `TXMut` type can be any type that implements the `DbTxMut` trait, which provides a set of functions to interact with read/write transactions and the associated cursor types. [File: crates/storage/db/src/abstraction/transaction.rs](https://github.com/paradigmxyz/reth/blob/main/crates/storage/db/src/abstraction/transaction.rs#L49) ```rust ignore /// Read write transaction that allows writing to database -pub trait DbTxMut: for<'a> DbTxMutGAT<'a> { +pub trait DbTxMut: Send + Sync { + /// Read-Write Cursor type + type CursorMut: DbCursorRW + DbCursorRO + Send + Sync; + /// Read-Write DupCursor type + type DupCursorMut: DbDupCursorRW + + DbCursorRW + + DbDupCursorRO + + DbCursorRO + + Send + + Sync; /// Put value to database fn put(&self, key: T::Key, value: T::Value) -> Result<(), Error>; /// Delete value from database @@ -188,11 +185,11 @@ pub trait DbTxMut: for<'a> DbTxMutGAT<'a> { /// Clears database. fn clear(&self) -> Result<(), Error>; /// Cursor for writing - fn cursor_write(&self) -> Result<>::CursorMut, Error>; + fn cursor_write(&self) -> Result, Error>; /// DupCursor for writing fn cursor_dup_write( &self, - ) -> Result<>::DupCursorMut, Error>; + ) -> Result, Error>; } ``` @@ -220,14 +217,14 @@ where //--snip-- impl<'a, DB: Database> Deref for Transaction<'a, DB> { - type Target = >::TXMut; + type Target = ::TXMut; fn deref(&self) -> &Self::Target { self.tx.as_ref().expect("Tried getting a reference to a non-existent transaction") } } ``` -The `Transaction` struct implements the `Deref` trait, which returns a reference to its `tx` field, which is a `TxMut`. Recall that `TxMut` is a generic type on the `DatabaseGAT` trait, which is defined as `type TXMut: DbTxMut + DbTx + Send + Sync;`, giving it access to all of the functions available to `DbTx`, including the `DbTx::get()` function. +The `Transaction` struct implements the `Deref` trait, which returns a reference to its `tx` field, which is a `TxMut`. Recall that `TxMut` is a generic type on the `Database` trait, which is defined as `type TXMut: DbTxMut + DbTx + Send + Sync;`, giving it access to all of the functions available to `DbTx`, including the `DbTx::get()` function. Notice that the function uses a [turbofish](https://techblog.tonsser.com/posts/what-is-rusts-turbofish) to define which table to use when passing in the `key` to the `DbTx::get()` function. Taking a quick look at the function definition, a generic `T` is defined that implements the `Table` trait mentioned at the beginning of this chapter. @@ -268,19 +265,6 @@ This next example uses the `DbTx::cursor()` method to get a `Cursor`. The `Curso ``` -We are almost at the last stop in the tour of the `db` crate. In addition to the methods provided by the `DbTx` and `DbTxMut` traits, `DbTx` also inherits the `DbTxGAT` trait, while `DbTxMut` inherits `DbTxMutGAT`. These next two traits provide various associated types related to cursors as well as methods to utilize the cursor types. - -[File: crates/storage/db/src/abstraction/transaction.rs](https://github.com/paradigmxyz/reth/blob/main/crates/storage/db/src/abstraction/transaction.rs#L12-L17) - -```rust ignore -pub trait DbTxGAT<'a, __ImplicitBounds: Sealed = Bounds<&'a Self>>: Send + Sync { - /// Cursor GAT - type Cursor: DbCursorRO<'a, T> + Send + Sync; - /// DupCursor GAT - type DupCursor: DbDupCursorRO<'a, T> + DbCursorRO<'a, T> + Send + Sync; -} -``` - Lets look at an examples of how cursors are used. The code snippet below contains the `unwind` method from the `BodyStage` defined in the `stages` crate. This function is responsible for unwinding any changes to the database if there is an error when executing the body stage within the Reth pipeline. [File: crates/stages/src/stages/bodies.rs](https://github.com/paradigmxyz/reth/blob/main/crates/stages/src/stages/bodies.rs#L205-L238)