Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(provider): migrate providers to ProviderError #5473

Merged
merged 5 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/reth/src/chain/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl ImportCommand {
debug!(target: "reth::cli", ?tip, "Tip manually set");

let factory = ProviderFactory::new(&db, self.chain.clone());
let provider = factory.provider().map_err(PipelineError::Interface)?;
let provider = factory.provider()?;

let latest_block_number =
provider.get_stage_checkpoint(StageId::Finish)?.map(|ch| ch.block_number);
Expand Down
7 changes: 3 additions & 4 deletions bin/reth/src/debug_cmd/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use reth_stages::{
ExecutionStage, ExecutionStageThresholds, HeaderSyncMode, SenderRecoveryStage,
TotalDifficultyStage,
},
Pipeline, PipelineError, StageSet,
Pipeline, StageSet,
};
use reth_tasks::TaskExecutor;
use std::{
Expand Down Expand Up @@ -234,7 +234,7 @@ impl Command {
)?;

let factory = ProviderFactory::new(&db, self.chain.clone());
let provider = factory.provider().map_err(PipelineError::Interface)?;
let provider = factory.provider()?;

let latest_block_number =
provider.get_stage_checkpoint(StageId::Finish)?.map(|ch| ch.block_number);
Expand Down Expand Up @@ -269,8 +269,7 @@ impl Command {
// Unwind the pipeline without committing.
{
factory
.provider_rw()
.map_err(PipelineError::Interface)?
.provider_rw()?
.take_block_and_execution_range(&self.chain, next_block..=target_block)?;
}

Expand Down
2 changes: 1 addition & 1 deletion bin/reth/src/debug_cmd/in_memory_merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ impl Command {
provider_rw.insert_block(block.clone(), None, None)?;
block_state.write_to_db(provider_rw.tx_ref(), OriginalValuesKnown::No)?;
let storage_lists = provider_rw.changed_storages_with_range(block.number..=block.number)?;
let storages = provider_rw.plainstate_storages(storage_lists)?;
let storages = provider_rw.plain_state_storages(storage_lists)?;
provider_rw.insert_storage_for_hashing(storages)?;
let account_lists = provider_rw.changed_accounts_with_range(block.number..=block.number)?;
let accounts = provider_rw.basic_accounts(account_lists)?;
Expand Down
4 changes: 2 additions & 2 deletions bin/reth/src/debug_cmd/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use reth_stages::{
AccountHashingStage, ExecutionStage, ExecutionStageThresholds, MerkleStage,
StorageHashingStage, MERKLE_STAGE_DEFAULT_CLEAN_THRESHOLD,
},
ExecInput, PipelineError, Stage,
ExecInput, Stage,
};
use reth_tasks::TaskExecutor;
use std::{
Expand Down Expand Up @@ -121,7 +121,7 @@ impl Command {
// initialize the database
let db = Arc::new(init_db(db_path, self.db.log_level)?);
let factory = ProviderFactory::new(&db, self.chain.clone());
let provider_rw = factory.provider_rw().map_err(PipelineError::Interface)?;
let provider_rw = factory.provider_rw()?;

// Configure and build network
let network_secret_path =
Expand Down
24 changes: 13 additions & 11 deletions bin/reth/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ use reth_db::{
tables,
transaction::{DbTx, DbTxMut},
};
use reth_interfaces::{db::DatabaseError, RethError};
use reth_interfaces::{db::DatabaseError, provider::ProviderResult};
use reth_primitives::{
stage::StageId, Account, Bytecode, ChainSpec, Receipts, StorageEntry, B256, U256,
};
use reth_provider::{
bundle_state::{BundleStateInit, RevertsInit},
BundleStateWithReceipts, DatabaseProviderRW, HashingWriter, HistoryWriter, OriginalValuesKnown,
ProviderFactory,
ProviderError, ProviderFactory,
};
use std::{
collections::{BTreeMap, HashMap},
Expand All @@ -33,13 +33,15 @@ pub enum InitDatabaseError {
database_hash: B256,
},

/// Low-level database error.
/// Provider error.
#[error(transparent)]
DBError(#[from] DatabaseError),
Provider(#[from] ProviderError),
}

/// Internal error.
#[error(transparent)]
InternalError(#[from] RethError),
impl From<DatabaseError> for InitDatabaseError {
fn from(error: DatabaseError) -> Self {
Self::Provider(ProviderError::Database(error))
}
}

/// Write the genesis block if it has not already been written
Expand Down Expand Up @@ -94,7 +96,7 @@ pub fn init_genesis<DB: Database>(
pub fn insert_genesis_state<DB: Database>(
tx: &<DB as DatabaseGAT<'_>>::TXMut,
genesis: &reth_primitives::Genesis,
) -> Result<(), InitDatabaseError> {
) -> ProviderResult<()> {
let mut state_init: BundleStateInit = HashMap::new();
let mut reverts_init = HashMap::new();
let mut contracts: HashMap<B256, Bytecode> = HashMap::new();
Expand Down Expand Up @@ -160,7 +162,7 @@ pub fn insert_genesis_state<DB: Database>(
pub fn insert_genesis_hashes<DB: Database>(
provider: &DatabaseProviderRW<'_, &DB>,
genesis: &reth_primitives::Genesis,
) -> Result<(), InitDatabaseError> {
) -> ProviderResult<()> {
// insert and hash accounts to hashing table
let alloc_accounts =
genesis.alloc.clone().into_iter().map(|(addr, account)| (addr, Some(account.into())));
Expand All @@ -184,7 +186,7 @@ pub fn insert_genesis_hashes<DB: Database>(
pub fn insert_genesis_history<DB: Database>(
provider: &DatabaseProviderRW<'_, &DB>,
genesis: &reth_primitives::Genesis,
) -> Result<(), InitDatabaseError> {
) -> ProviderResult<()> {
let account_transitions =
genesis.alloc.keys().map(|addr| (*addr, vec![0])).collect::<BTreeMap<_, _>>();
provider.insert_account_history_index(account_transitions)?;
Expand All @@ -204,7 +206,7 @@ pub fn insert_genesis_history<DB: Database>(
pub fn insert_genesis_header<DB: Database>(
tx: &<DB as DatabaseGAT<'_>>::TXMut,
chain: Arc<ChainSpec>,
) -> Result<(), InitDatabaseError> {
) -> ProviderResult<()> {
let header = chain.sealed_genesis_header();

tx.put::<tables::CanonicalHeaders>(0, header.hash)?;
Expand Down
3 changes: 1 addition & 2 deletions bin/reth/src/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1048,7 +1048,6 @@ mod tests {
use super::*;
use crate::args::utils::SUPPORTED_CHAINS;
use reth_discv4::DEFAULT_DISCOVERY_PORT;
use reth_primitives::DEV;
use std::{
net::{IpAddr, Ipv4Addr},
path::Path,
Expand Down Expand Up @@ -1154,7 +1153,7 @@ mod tests {
#[cfg(not(feature = "optimism"))] // dev mode not yet supported in op-reth
fn parse_dev() {
let cmd = NodeCommand::<()>::parse_from(["reth", "--dev"]);
let chain = DEV.clone();
let chain = reth_primitives::DEV.clone();
assert_eq!(cmd.chain.chain, chain.chain);
assert_eq!(cmd.chain.genesis_hash, chain.genesis_hash);
assert_eq!(
Expand Down
8 changes: 4 additions & 4 deletions bin/reth/src/stage/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use reth_stages::{
IndexAccountHistoryStage, IndexStorageHistoryStage, MerkleStage, SenderRecoveryStage,
StorageHashingStage, TransactionLookupStage,
},
ExecInput, ExecOutput, PipelineError, Stage, UnwindInput,
ExecInput, ExecOutput, Stage, UnwindInput,
};
use std::{any::Any, net::SocketAddr, path::PathBuf, sync::Arc};
use tracing::*;
Expand Down Expand Up @@ -125,7 +125,7 @@ impl Command {
info!(target: "reth::cli", "Database opened");

let factory = ProviderFactory::new(&db, self.chain.clone());
let mut provider_rw = factory.provider_rw().map_err(PipelineError::Interface)?;
let mut provider_rw = factory.provider_rw()?;

if let Some(listen_addr) = self.metrics {
info!(target: "reth::cli", "Starting metrics endpoint at {}", listen_addr);
Expand Down Expand Up @@ -247,7 +247,7 @@ impl Command {

if self.commit {
provider_rw.commit()?;
provider_rw = factory.provider_rw().map_err(PipelineError::Interface)?;
provider_rw = factory.provider_rw()?;
}
}
}
Expand All @@ -264,7 +264,7 @@ impl Command {

if self.commit {
provider_rw.commit()?;
provider_rw = factory.provider_rw().map_err(PipelineError::Interface)?;
provider_rw = factory.provider_rw()?;
}
}

Expand Down
14 changes: 10 additions & 4 deletions crates/consensus/beacon/src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ where
inconsistent_stage_checkpoint = stage_checkpoint,
"Pipeline sync progress is inconsistent"
);
return self.blockchain.block_hash(first_stage_checkpoint)
return Ok(self.blockchain.block_hash(first_stage_checkpoint)?)
}
}

Expand Down Expand Up @@ -1670,7 +1670,7 @@ where
},
Err(error) => {
error!(target: "consensus::engine", ?error, "Error getting canonical header for continuous sync");
return Some(Err(error.into()))
return Some(Err(RethError::Provider(error).into()))
}
};
self.blockchain.set_canonical_head(max_header);
Expand Down Expand Up @@ -1836,7 +1836,10 @@ where
cx,
EngineContext {
tip_block_number: this.blockchain.canonical_tip().number,
finalized_block_number: this.blockchain.finalized_block_number()?,
finalized_block_number: this
.blockchain
.finalized_block_number()
.map_err(RethError::Provider)?,
},
)? {
this.on_hook_result(result)?;
Expand Down Expand Up @@ -1908,7 +1911,10 @@ where
cx,
EngineContext {
tip_block_number: this.blockchain.canonical_tip().number,
finalized_block_number: this.blockchain.finalized_block_number()?,
finalized_block_number: this
.blockchain
.finalized_block_number()
.map_err(RethError::Provider)?,
DaniPopes marked this conversation as resolved.
Show resolved Hide resolved
},
this.sync.is_pipeline_active(),
)? {
Expand Down
34 changes: 20 additions & 14 deletions crates/consensus/common/src/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,8 +484,8 @@ mod tests {
use super::*;
use mockall::mock;
use reth_interfaces::{
provider::ProviderResult,
test_utils::generators::{self, Rng},
RethResult,
};
use reth_primitives::{
constants::eip4844::DATA_GAS_PER_BLOB, hex_literal::hex, proofs, Account, Address,
Expand All @@ -498,13 +498,13 @@ mod tests {
WithdrawalsProvider {}

impl WithdrawalsProvider for WithdrawalsProvider {
fn latest_withdrawal(&self) -> RethResult<Option<Withdrawal>> ;
fn latest_withdrawal(&self) -> ProviderResult<Option<Withdrawal>> ;

fn withdrawals_by_block(
&self,
_id: BlockHashOrNumber,
_timestamp: u64,
) -> RethResult<Option<Vec<Withdrawal>>> ;
) -> ProviderResult<Option<Vec<Withdrawal>>> ;
}
}

Expand Down Expand Up @@ -537,45 +537,51 @@ mod tests {
}

impl AccountReader for Provider {
fn basic_account(&self, _address: Address) -> RethResult<Option<Account>> {
fn basic_account(&self, _address: Address) -> ProviderResult<Option<Account>> {
Ok(self.account)
}
}

impl HeaderProvider for Provider {
fn is_known(&self, _block_hash: &BlockHash) -> RethResult<bool> {
fn is_known(&self, _block_hash: &BlockHash) -> ProviderResult<bool> {
Ok(self.is_known)
}

fn header(&self, _block_number: &BlockHash) -> RethResult<Option<Header>> {
fn header(&self, _block_number: &BlockHash) -> ProviderResult<Option<Header>> {
Ok(self.parent.clone())
}

fn header_by_number(&self, _num: u64) -> RethResult<Option<Header>> {
fn header_by_number(&self, _num: u64) -> ProviderResult<Option<Header>> {
Ok(self.parent.clone())
}

fn header_td(&self, _hash: &BlockHash) -> RethResult<Option<U256>> {
fn header_td(&self, _hash: &BlockHash) -> ProviderResult<Option<U256>> {
Ok(None)
}

fn header_td_by_number(&self, _number: BlockNumber) -> RethResult<Option<U256>> {
fn header_td_by_number(&self, _number: BlockNumber) -> ProviderResult<Option<U256>> {
Ok(None)
}

fn headers_range(&self, _range: impl RangeBounds<BlockNumber>) -> RethResult<Vec<Header>> {
fn headers_range(
&self,
_range: impl RangeBounds<BlockNumber>,
) -> ProviderResult<Vec<Header>> {
Ok(vec![])
}

fn sealed_header(&self, _block_number: BlockNumber) -> RethResult<Option<SealedHeader>> {
fn sealed_header(
&self,
_block_number: BlockNumber,
) -> ProviderResult<Option<SealedHeader>> {
Ok(None)
}

fn sealed_headers_while(
&self,
_range: impl RangeBounds<BlockNumber>,
_predicate: impl FnMut(&SealedHeader) -> bool,
) -> RethResult<Vec<SealedHeader>> {
) -> ProviderResult<Vec<SealedHeader>> {
Ok(vec![])
}
}
Expand All @@ -585,11 +591,11 @@ mod tests {
&self,
_id: BlockHashOrNumber,
_timestamp: u64,
) -> RethResult<Option<Vec<Withdrawal>>> {
) -> ProviderResult<Option<Vec<Withdrawal>>> {
self.withdrawals_provider.withdrawals_by_block(_id, _timestamp)
}

fn latest_withdrawal(&self) -> RethResult<Option<Withdrawal>> {
fn latest_withdrawal(&self) -> ProviderResult<Option<Withdrawal>> {
self.withdrawals_provider.latest_withdrawal()
}
}
Expand Down
6 changes: 5 additions & 1 deletion crates/interfaces/src/blockchain_tree/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use crate::{
consensus::ConsensusError,
executor::{BlockExecutionError, BlockValidationError},
provider::ProviderError,
};
use reth_primitives::{BlockHash, BlockNumber, SealedBlock};

Expand Down Expand Up @@ -201,6 +202,9 @@ pub enum InsertBlockErrorKind {
/// Block violated tree invariants.
#[error(transparent)]
Tree(#[from] BlockchainTreeError),
/// Provider error.
#[error(transparent)]
Provider(#[from] ProviderError),
/// An internal error occurred, like interacting with the database.
#[error(transparent)]
Internal(#[from] Box<dyn std::error::Error + Send + Sync>),
Expand Down Expand Up @@ -260,7 +264,7 @@ impl InsertBlockErrorKind {
BlockchainTreeError::BlockBufferingFailed { .. } => false,
}
}
InsertBlockErrorKind::Internal(_) => {
InsertBlockErrorKind::Provider(_) | InsertBlockErrorKind::Internal(_) => {
// any other error, such as database errors, are considered internal errors
false
}
Expand Down
6 changes: 0 additions & 6 deletions crates/interfaces/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ impl From<crate::blockchain_tree::error::BlockchainTreeError> for RethError {
}
}

impl From<reth_nippy_jar::NippyJarError> for RethError {
fn from(err: reth_nippy_jar::NippyJarError) -> Self {
RethError::Custom(err.to_string())
}
}

impl From<reth_primitives::fs::FsPathError> for RethError {
fn from(err: reth_primitives::fs::FsPathError) -> Self {
RethError::Custom(err.to_string())
Expand Down
Loading