Skip to content

Commit

Permalink
replace with with_snapshot_provider on db provider
Browse files Browse the repository at this point in the history
  • Loading branch information
joshieDo committed Nov 2, 2023
1 parent 2fef51f commit 33dc284
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 24 deletions.
2 changes: 0 additions & 2 deletions crates/blockchain-tree/src/blockchain_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1071,7 +1071,6 @@ impl<DB: Database, EF: ExecutorFactory> BlockchainTree<DB, EF> {
let provider = DatabaseProvider::new_rw(
self.externals.db.tx_mut()?,
self.externals.chain_spec.clone(),
None,
);

let (blocks, state) = chain.into_inner();
Expand Down Expand Up @@ -1117,7 +1116,6 @@ impl<DB: Database, EF: ExecutorFactory> BlockchainTree<DB, EF> {
let provider = DatabaseProvider::new_rw(
self.externals.db.tx_mut()?,
self.externals.chain_spec.clone(),
None,
);

let tip = provider.last_block_number()?;
Expand Down
24 changes: 14 additions & 10 deletions crates/storage/provider/src/providers/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,27 @@ impl<DB: Database> ProviderFactory<DB> {
/// 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) -> RethResult<DatabaseProviderRO<'_, DB>> {
Ok(DatabaseProvider::new(
self.db.tx()?,
self.chain_spec.clone(),
self.snapshot_provider.clone(),
))
let mut provider = DatabaseProvider::new(self.db.tx()?, self.chain_spec.clone());

if let Some(snapshot_provider) = &self.snapshot_provider {
provider = provider.with_snapshot_provider(snapshot_provider.clone());
}

Ok(provider)
}

/// Returns a provider with a created `DbTxMut` inside, which allows fetching and updating
/// 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) -> RethResult<DatabaseProviderRW<'_, DB>> {
Ok(DatabaseProviderRW(DatabaseProvider::new_rw(
self.db.tx_mut()?,
self.chain_spec.clone(),
self.snapshot_provider.clone(),
)))
let mut provider = DatabaseProvider::new_rw(self.db.tx_mut()?, self.chain_spec.clone());

if let Some(snapshot_provider) = &self.snapshot_provider {
provider = provider.with_snapshot_provider(snapshot_provider.clone());
}

Ok(DatabaseProviderRW(provider))
}
}

Expand Down
22 changes: 10 additions & 12 deletions crates/storage/provider/src/providers/database/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,8 @@ pub struct DatabaseProvider<TX> {

impl<TX: DbTxMut> DatabaseProvider<TX> {
/// Creates a provider with an inner read-write transaction.
pub fn new_rw(
tx: TX,
chain_spec: Arc<ChainSpec>,
snapshot_provider: Option<Arc<SnapshotProvider>>,
) -> Self {
Self { tx, chain_spec, snapshot_provider }
pub fn new_rw(tx: TX, chain_spec: Arc<ChainSpec>) -> Self {
Self { tx, chain_spec, snapshot_provider: None }
}
}

Expand Down Expand Up @@ -163,12 +159,14 @@ where

impl<TX: DbTx> DatabaseProvider<TX> {
/// Creates a provider with an inner read-only transaction.
pub fn new(
tx: TX,
chain_spec: Arc<ChainSpec>,
snapshot_provider: Option<Arc<SnapshotProvider>>,
) -> Self {
Self { tx, chain_spec, snapshot_provider }
pub fn new(tx: TX, chain_spec: Arc<ChainSpec>) -> Self {
Self { tx, chain_spec, snapshot_provider: None }
}

/// Creates a new [`Self`] with access to a [`SnapshotProvider`].
pub fn with_snapshot_provider(mut self, snapshot_provider: Arc<SnapshotProvider>) -> Self {
self.snapshot_provider = Some(snapshot_provider);
self
}

/// Consume `DbTx` or `DbTxMut`.
Expand Down

0 comments on commit 33dc284

Please sign in to comment.