From 0d9e1f4997f927c2657224defc12062aa838b960 Mon Sep 17 00:00:00 2001
From: joshieDo <93316087+joshieDo@users.noreply.github.com>
Date: Sat, 17 Jun 2023 01:58:16 +0100
Subject: [PATCH] chore: replaces `tx.get::
` with provider methods
(#3189)
---
bin/reth/src/debug_cmd/execution.rs | 8 ++-
bin/reth/src/debug_cmd/merkle.rs | 2 +-
bin/reth/src/node/mod.rs | 70 +++++++++----------
bin/reth/src/stage/run.rs | 5 +-
.../stages/benches/setup/account_hashing.rs | 2 +-
crates/stages/benches/setup/mod.rs | 4 +-
crates/stages/src/pipeline/mod.rs | 16 +++--
crates/stages/src/stage.rs | 7 +-
crates/stages/src/stages/execution.rs | 44 ++++--------
crates/stages/src/stages/hashing_account.rs | 2 +-
crates/stages/src/stages/headers.rs | 32 ++++-----
crates/stages/src/stages/sender_recovery.rs | 21 +++---
crates/stages/src/stages/total_difficulty.rs | 39 +++++------
crates/stages/src/stages/tx_lookup.rs | 24 +++----
crates/stages/src/test_utils/runner.rs | 2 +
crates/stages/src/test_utils/test_db.rs | 17 +++--
crates/storage/provider/src/post_state/mod.rs | 19 ++---
.../provider/src/providers/database/mod.rs | 18 ++---
.../src/providers/database/provider.rs | 20 ++----
crates/storage/provider/src/providers/mod.rs | 4 ++
.../storage/provider/src/test_utils/mock.rs | 4 ++
.../storage/provider/src/test_utils/noop.rs | 4 ++
.../provider/src/traits/transactions.rs | 5 ++
crates/storage/provider/src/transaction.rs | 3 +
24 files changed, 182 insertions(+), 190 deletions(-)
diff --git a/bin/reth/src/debug_cmd/execution.rs b/bin/reth/src/debug_cmd/execution.rs
index 82e62f925..3aeab6911 100644
--- a/bin/reth/src/debug_cmd/execution.rs
+++ b/bin/reth/src/debug_cmd/execution.rs
@@ -26,7 +26,7 @@ use reth_interfaces::{
use reth_network::NetworkHandle;
use reth_network_api::NetworkInfo;
use reth_primitives::{stage::StageId, BlockHashOrNumber, BlockNumber, ChainSpec, H256};
-use reth_provider::{providers::get_stage_checkpoint, ProviderFactory};
+use reth_provider::{ProviderFactory, StageCheckpointProvider};
use reth_staged_sync::utils::init::{init_db, init_genesis};
use reth_stages::{
sets::DefaultStages,
@@ -242,15 +242,17 @@ impl Command {
ctx.task_executor
.spawn_critical("events task", events::handle_events(Some(network.clone()), events));
+ let factory = ProviderFactory::new(&db, self.chain.clone());
+ let provider = factory.provider().map_err(PipelineError::Interface)?;
+
let latest_block_number =
- get_stage_checkpoint(&db.tx()?, StageId::Finish)?.unwrap_or_default().block_number;
+ provider.get_stage_checkpoint(StageId::Finish)?.unwrap_or_default().block_number;
if latest_block_number >= self.to {
info!(target: "reth::cli", latest = latest_block_number, "Nothing to run");
return Ok(())
}
let mut current_max_block = latest_block_number;
- let factory = ProviderFactory::new(&db, self.chain.clone());
while current_max_block < self.to {
let next_block = current_max_block + 1;
diff --git a/bin/reth/src/debug_cmd/merkle.rs b/bin/reth/src/debug_cmd/merkle.rs
index 58e4a5df3..1009fe6d8 100644
--- a/bin/reth/src/debug_cmd/merkle.rs
+++ b/bin/reth/src/debug_cmd/merkle.rs
@@ -9,7 +9,7 @@ use reth_primitives::{
stage::{StageCheckpoint, StageId},
ChainSpec,
};
-use reth_provider::ProviderFactory;
+use reth_provider::{ProviderFactory, StageCheckpointProvider};
use reth_staged_sync::utils::init::init_db;
use reth_stages::{
stages::{
diff --git a/bin/reth/src/node/mod.rs b/bin/reth/src/node/mod.rs
index 4a90aa5e9..b7b6b7215 100644
--- a/bin/reth/src/node/mod.rs
+++ b/bin/reth/src/node/mod.rs
@@ -23,8 +23,6 @@ use reth_config::Config;
use reth_db::{
database::Database,
mdbx::{Env, WriteMap},
- tables,
- transaction::DbTx,
};
use reth_discv4::DEFAULT_DISCOVERY_PORT;
use reth_downloaders::{
@@ -41,12 +39,10 @@ use reth_interfaces::{
};
use reth_network::{error::NetworkError, NetworkConfig, NetworkHandle, NetworkManager};
use reth_network_api::NetworkInfo;
-use reth_primitives::{
- stage::StageId, BlockHashOrNumber, ChainSpec, Head, Header, SealedHeader, H256,
-};
+use reth_primitives::{stage::StageId, BlockHashOrNumber, ChainSpec, Head, SealedHeader, H256};
use reth_provider::{
- providers::get_stage_checkpoint, BlockProvider, CanonStateSubscriptions, HeaderProvider,
- ProviderFactory,
+ BlockHashProvider, BlockProvider, CanonStateSubscriptions, HeaderProvider, ProviderFactory,
+ StageCheckpointProvider,
};
use reth_revm::Factory;
use reth_revm_inspectors::stack::Hook;
@@ -509,30 +505,31 @@ impl Command {
Ok(handle)
}
- fn lookup_head(
- &self,
- db: Arc>,
- ) -> Result {
- db.view(|tx| {
- let head = get_stage_checkpoint(tx, StageId::Finish)?.unwrap_or_default().block_number;
- let header = tx
- .get::(head)?
- .expect("the header for the latest block is missing, database is corrupt");
- let total_difficulty = tx.get::(head)?.expect(
- "the total difficulty for the latest block is missing, database is corrupt",
- );
- let hash = tx
- .get::(head)?
- .expect("the hash for the latest block is missing, database is corrupt");
- Ok::(Head {
- number: head,
- hash,
- difficulty: header.difficulty,
- total_difficulty: total_difficulty.into(),
- timestamp: header.timestamp,
- })
- })?
- .map_err(Into::into)
+ fn lookup_head(&self, db: Arc>) -> Result {
+ let factory = ProviderFactory::new(db, self.chain.clone());
+ let provider = factory.provider()?;
+
+ let head = provider.get_stage_checkpoint(StageId::Finish)?.unwrap_or_default().block_number;
+
+ let header = provider
+ .header_by_number(head)?
+ .expect("the header for the latest block is missing, database is corrupt");
+
+ let total_difficulty = provider
+ .header_td_by_number(head)?
+ .expect("the total difficulty for the latest block is missing, database is corrupt");
+
+ let hash = provider
+ .block_hash(head)?
+ .expect("the hash for the latest block is missing, database is corrupt");
+
+ Ok(Head {
+ number: head,
+ hash,
+ difficulty: header.difficulty,
+ total_difficulty,
+ timestamp: header.timestamp,
+ })
}
/// Attempt to look up the block number for the tip hash in the database.
@@ -565,13 +562,10 @@ impl Command {
DB: Database,
Client: HeadersClient,
{
- let header = db.view(|tx| -> Result