Skip to content

Commit

Permalink
Using associated trait bound for db error (#8951)
Browse files Browse the repository at this point in the history
  • Loading branch information
i-m-aditya authored Jun 25, 2024
1 parent bbc4f30 commit ec5795f
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 54 deletions.
40 changes: 24 additions & 16 deletions crates/ethereum/evm/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use crate::{
dao_fork::{DAO_HARDFORK_BENEFICIARY, DAO_HARDKFORK_ACCOUNTS},
EthEvmConfig,
};
#[cfg(not(feature = "std"))]
use alloc::{sync::Arc, vec, vec::Vec};
use reth_chainspec::{ChainSpec, MAINNET};
use reth_ethereum_consensus::validate_block_post_execution;
use reth_evm::{
Expand All @@ -29,15 +31,11 @@ use reth_revm::{
};
use revm_primitives::{
db::{Database, DatabaseCommit},
BlockEnv, CfgEnvWithHandlerCfg, EnvWithHandlerCfg, ResultAndState,
BlockEnv, CfgEnvWithHandlerCfg, EVMError, EnvWithHandlerCfg, ResultAndState,
};

#[cfg(not(feature = "std"))]
use alloc::{sync::Arc, vec, vec::Vec};

#[cfg(feature = "std")]
use std::sync::Arc;

use std::{fmt::Display, sync::Arc};
/// Provides executors to execute regular ethereum blocks
#[derive(Debug, Clone)]
pub struct EthExecutorProvider<EvmConfig = EthEvmConfig> {
Expand Down Expand Up @@ -70,7 +68,7 @@ where
{
fn eth_executor<DB>(&self, db: DB) -> EthBlockExecutor<EvmConfig, DB>
where
DB: Database<Error = ProviderError>,
DB: Database<Error: Into<ProviderError>>,
{
EthBlockExecutor::new(
self.chain_spec.clone(),
Expand All @@ -84,20 +82,22 @@ impl<EvmConfig> BlockExecutorProvider for EthExecutorProvider<EvmConfig>
where
EvmConfig: ConfigureEvm,
{
type Executor<DB: Database<Error = ProviderError>> = EthBlockExecutor<EvmConfig, DB>;
type Executor<DB: Database<Error: Into<ProviderError> + Display>> =
EthBlockExecutor<EvmConfig, DB>;

type BatchExecutor<DB: Database<Error = ProviderError>> = EthBatchExecutor<EvmConfig, DB>;
type BatchExecutor<DB: Database<Error: Into<ProviderError> + Display>> =
EthBatchExecutor<EvmConfig, DB>;

fn executor<DB>(&self, db: DB) -> Self::Executor<DB>
where
DB: Database<Error = ProviderError>,
DB: Database<Error: Into<ProviderError> + Display>,
{
self.eth_executor(db)
}

fn batch_executor<DB>(&self, db: DB, prune_modes: PruneModes) -> Self::BatchExecutor<DB>
where
DB: Database<Error = ProviderError>,
DB: Database<Error: Into<ProviderError> + Display>,
{
let executor = self.eth_executor(db);
EthBatchExecutor {
Expand Down Expand Up @@ -145,7 +145,8 @@ where
mut evm: Evm<'_, Ext, &mut State<DB>>,
) -> Result<EthExecuteOutput, BlockExecutionError>
where
DB: Database<Error = ProviderError>,
DB: Database,
DB::Error: Into<ProviderError> + std::fmt::Display,
{
// apply pre execution changes
apply_beacon_root_contract_call(
Expand Down Expand Up @@ -182,10 +183,17 @@ where

// Execute transaction.
let ResultAndState { result, state } = evm.transact().map_err(move |err| {
let new_err = match err {
EVMError::Transaction(e) => EVMError::Transaction(e),
EVMError::Header(e) => EVMError::Header(e),
EVMError::Database(e) => EVMError::Database(e.into()),
EVMError::Custom(e) => EVMError::Custom(e),
EVMError::Precompile(e) => EVMError::Precompile(e),
};
// Ensure hash is calculated for error log, if not already done
BlockValidationError::EVM {
hash: transaction.recalculate_hash(),
error: err.into(),
error: Box::new(new_err),
}
})?;
evm.db_mut().commit(state);
Expand Down Expand Up @@ -260,7 +268,7 @@ impl<EvmConfig, DB> EthBlockExecutor<EvmConfig, DB> {
impl<EvmConfig, DB> EthBlockExecutor<EvmConfig, DB>
where
EvmConfig: ConfigureEvm,
DB: Database<Error = ProviderError>,
DB: Database<Error: Into<ProviderError> + Display>,
{
/// Configures a new evm configuration and block environment for the given block.
///
Expand Down Expand Up @@ -358,7 +366,7 @@ where
impl<EvmConfig, DB> Executor<DB> for EthBlockExecutor<EvmConfig, DB>
where
EvmConfig: ConfigureEvm,
DB: Database<Error = ProviderError>,
DB: Database<Error: Into<ProviderError> + std::fmt::Display>,
{
type Input<'a> = BlockExecutionInput<'a, BlockWithSenders>;
type Output = BlockExecutionOutput<Receipt>;
Expand Down Expand Up @@ -408,7 +416,7 @@ impl<EvmConfig, DB> EthBatchExecutor<EvmConfig, DB> {
impl<EvmConfig, DB> BatchExecutor<DB> for EthBatchExecutor<EvmConfig, DB>
where
EvmConfig: ConfigureEvm,
DB: Database<Error = ProviderError>,
DB: Database<Error: Into<ProviderError> + Display>,
{
type Input<'a> = BlockExecutionInput<'a, BlockWithSenders>;
type Output = ExecutionOutcome;
Expand Down
16 changes: 10 additions & 6 deletions crates/evm/src/either.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Helper type that represents one of two possible executor types
use std::fmt::Display;

use crate::execute::{
BatchExecutor, BlockExecutionInput, BlockExecutionOutput, BlockExecutorProvider, Executor,
};
Expand All @@ -18,13 +20,15 @@ where
A: BlockExecutorProvider,
B: BlockExecutorProvider,
{
type Executor<DB: Database<Error = ProviderError>> = Either<A::Executor<DB>, B::Executor<DB>>;
type BatchExecutor<DB: Database<Error = ProviderError>> =
type Executor<DB: Database<Error: Into<ProviderError> + Display>> =
Either<A::Executor<DB>, B::Executor<DB>>;

type BatchExecutor<DB: Database<Error: Into<ProviderError> + Display>> =
Either<A::BatchExecutor<DB>, B::BatchExecutor<DB>>;

fn executor<DB>(&self, db: DB) -> Self::Executor<DB>
where
DB: Database<Error = ProviderError>,
DB: Database<Error: Into<ProviderError> + Display>,
{
match self {
Self::Left(a) => Either::Left(a.executor(db)),
Expand All @@ -34,7 +38,7 @@ where

fn batch_executor<DB>(&self, db: DB, prune_modes: PruneModes) -> Self::BatchExecutor<DB>
where
DB: Database<Error = ProviderError>,
DB: Database<Error: Into<ProviderError> + Display>,
{
match self {
Self::Left(a) => Either::Left(a.batch_executor(db, prune_modes)),
Expand All @@ -57,7 +61,7 @@ where
Output = BlockExecutionOutput<Receipt>,
Error = BlockExecutionError,
>,
DB: Database<Error = ProviderError>,
DB: Database<Error: Into<ProviderError> + Display>,
{
type Input<'a> = BlockExecutionInput<'a, BlockWithSenders>;
type Output = BlockExecutionOutput<Receipt>;
Expand Down Expand Up @@ -85,7 +89,7 @@ where
Output = ExecutionOutcome,
Error = BlockExecutionError,
>,
DB: Database<Error = ProviderError>,
DB: Database<Error: Into<ProviderError> + Display>,
{
type Input<'a> = BlockExecutionInput<'a, BlockWithSenders>;
type Output = ExecutionOutcome;
Expand Down
17 changes: 9 additions & 8 deletions crates/evm/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use reth_primitives::{BlockNumber, BlockWithSenders, Receipt, Request, U256};
use reth_prune_types::PruneModes;
use revm::db::BundleState;
use revm_primitives::db::Database;
use std::fmt::Display;

#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
Expand Down Expand Up @@ -142,15 +143,15 @@ pub trait BlockExecutorProvider: Send + Sync + Clone + Unpin + 'static {
///
/// It is not expected to validate the state trie root, this must be done by the caller using
/// the returned state.
type Executor<DB: Database<Error = ProviderError>>: for<'a> Executor<
type Executor<DB: Database<Error: Into<ProviderError> + Display>>: for<'a> Executor<
DB,
Input<'a> = BlockExecutionInput<'a, BlockWithSenders>,
Output = BlockExecutionOutput<Receipt>,
Error = BlockExecutionError,
>;

/// An executor that can execute a batch of blocks given a database.
type BatchExecutor<DB: Database<Error = ProviderError>>: for<'a> BatchExecutor<
type BatchExecutor<DB: Database<Error: Into<ProviderError> + Display>>: for<'a> BatchExecutor<
DB,
Input<'a> = BlockExecutionInput<'a, BlockWithSenders>,
Output = ExecutionOutcome,
Expand All @@ -162,7 +163,7 @@ pub trait BlockExecutorProvider: Send + Sync + Clone + Unpin + 'static {
/// This is used to execute a single block and get the changed state.
fn executor<DB>(&self, db: DB) -> Self::Executor<DB>
where
DB: Database<Error = ProviderError>;
DB: Database<Error: Into<ProviderError> + Display>;

/// Creates a new batch executor with the given database and pruning modes.
///
Expand All @@ -173,7 +174,7 @@ pub trait BlockExecutorProvider: Send + Sync + Clone + Unpin + 'static {
/// execution.
fn batch_executor<DB>(&self, db: DB, prune_modes: PruneModes) -> Self::BatchExecutor<DB>
where
DB: Database<Error = ProviderError>;
DB: Database<Error: Into<ProviderError> + Display>;
}

#[cfg(test)]
Expand All @@ -187,19 +188,19 @@ mod tests {
struct TestExecutorProvider;

impl BlockExecutorProvider for TestExecutorProvider {
type Executor<DB: Database<Error = ProviderError>> = TestExecutor<DB>;
type BatchExecutor<DB: Database<Error = ProviderError>> = TestExecutor<DB>;
type Executor<DB: Database<Error: Into<ProviderError> + Display>> = TestExecutor<DB>;
type BatchExecutor<DB: Database<Error: Into<ProviderError> + Display>> = TestExecutor<DB>;

fn executor<DB>(&self, _db: DB) -> Self::Executor<DB>
where
DB: Database<Error = ProviderError>,
DB: Database<Error: Into<ProviderError> + Display>,
{
TestExecutor(PhantomData)
}

fn batch_executor<DB>(&self, _db: DB, _prune_modes: PruneModes) -> Self::BatchExecutor<DB>
where
DB: Database<Error = ProviderError>,
DB: Database<Error: Into<ProviderError> + Display>,
{
TestExecutor(PhantomData)
}
Expand Down
10 changes: 6 additions & 4 deletions crates/evm/src/noop.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! A no operation block executor implementation.
use std::fmt::Display;

use reth_execution_errors::BlockExecutionError;
use reth_execution_types::ExecutionOutcome;
use reth_primitives::{BlockNumber, BlockWithSenders, Receipt};
Expand All @@ -19,20 +21,20 @@ const UNAVAILABLE_FOR_NOOP: &str = "execution unavailable for noop";
pub struct NoopBlockExecutorProvider;

impl BlockExecutorProvider for NoopBlockExecutorProvider {
type Executor<DB: Database<Error = ProviderError>> = Self;
type Executor<DB: Database<Error: Into<ProviderError> + Display>> = Self;

type BatchExecutor<DB: Database<Error = ProviderError>> = Self;
type BatchExecutor<DB: Database<Error: Into<ProviderError> + Display>> = Self;

fn executor<DB>(&self, _: DB) -> Self::Executor<DB>
where
DB: Database<Error = ProviderError>,
DB: Database<Error: Into<ProviderError> + Display>,
{
Self
}

fn batch_executor<DB>(&self, _: DB, _: PruneModes) -> Self::BatchExecutor<DB>
where
DB: Database<Error = ProviderError>,
DB: Database<Error: Into<ProviderError> + Display>,
{
Self
}
Expand Down
10 changes: 5 additions & 5 deletions crates/evm/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use reth_primitives::{BlockNumber, BlockWithSenders, Receipt};
use reth_prune_types::PruneModes;
use reth_storage_errors::provider::ProviderError;
use revm_primitives::db::Database;
use std::sync::Arc;
use std::{fmt::Display, sync::Arc};

/// A [`BlockExecutorProvider`] that returns mocked execution results.
#[derive(Clone, Debug, Default)]
Expand All @@ -26,20 +26,20 @@ impl MockExecutorProvider {
}

impl BlockExecutorProvider for MockExecutorProvider {
type Executor<DB: Database<Error = ProviderError>> = Self;
type Executor<DB: Database<Error: Into<ProviderError> + Display>> = Self;

type BatchExecutor<DB: Database<Error = ProviderError>> = Self;
type BatchExecutor<DB: Database<Error: Into<ProviderError> + Display>> = Self;

fn executor<DB>(&self, _: DB) -> Self::Executor<DB>
where
DB: Database<Error = ProviderError>,
DB: Database<Error: Into<ProviderError> + Display>,
{
self.clone()
}

fn batch_executor<DB>(&self, _: DB, _: PruneModes) -> Self::BatchExecutor<DB>
where
DB: Database<Error = ProviderError>,
DB: Database<Error: Into<ProviderError> + Display>,
{
self.clone()
}
Expand Down
Loading

0 comments on commit ec5795f

Please sign in to comment.