Skip to content

Commit

Permalink
trait bounding Display as well
Browse files Browse the repository at this point in the history
  • Loading branch information
i-m-aditya committed Jun 24, 2024
1 parent ce4bdd8 commit 0c5e8f0
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 39 deletions.
33 changes: 21 additions & 12 deletions crates/ethereum/evm/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ use reth_revm::{
};
use revm_primitives::{
db::{Database, DatabaseCommit},
BlockEnv, CfgEnvWithHandlerCfg, EnvWithHandlerCfg, ResultAndState,
BlockEnv, CfgEnvWithHandlerCfg, EVMError, EnvWithHandlerCfg, ResultAndState,
};
use std::sync::Arc;
use std::{fmt::Display, sync::Arc};

/// Provides executors to execute regular ethereum blocks
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -64,7 +64,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 @@ -78,20 +78,22 @@ impl<EvmConfig> BlockExecutorProvider for EthExecutorProvider<EvmConfig>
where
EvmConfig: ConfigureEvm,
{
type Executor<DB: Database<Error: Into<ProviderError>>> = EthBlockExecutor<EvmConfig, DB>;
type Executor<DB: Database<Error: Into<ProviderError> + Display>> =
EthBlockExecutor<EvmConfig, DB>;

type BatchExecutor<DB: Database<Error: Into<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: Into<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: Into<ProviderError>>,
DB: Database<Error: Into<ProviderError> + Display>,
{
let executor = self.eth_executor(db);
EthBatchExecutor {
Expand Down Expand Up @@ -139,7 +141,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 @@ -176,10 +179,16 @@ 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),
};
// 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 @@ -254,7 +263,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 @@ -352,7 +361,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 @@ -402,7 +411,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 = BatchBlockExecutionOutput;
Expand Down
14 changes: 8 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::{
BatchBlockExecutionOutput, BatchExecutor, BlockExecutionInput, BlockExecutionOutput,
BlockExecutorProvider, Executor,
Expand All @@ -18,15 +20,15 @@ where
A: BlockExecutorProvider,
B: BlockExecutorProvider,
{
type Executor<DB: Database<Error: Into<ProviderError>>> =
type Executor<DB: Database<Error: Into<ProviderError> + Display>> =
Either<A::Executor<DB>, B::Executor<DB>>;

type BatchExecutor<DB: Database<Error: Into<ProviderError>>> =
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: Into<ProviderError>>,
DB: Database<Error: Into<ProviderError> + Display>,
{
match self {
Self::Left(a) => Either::Left(a.executor(db)),
Expand All @@ -36,7 +38,7 @@ where

fn batch_executor<DB>(&self, db: DB, prune_modes: PruneModes) -> Self::BatchExecutor<DB>
where
DB: Database<Error: Into<ProviderError>>,
DB: Database<Error: Into<ProviderError> + Display>,
{
match self {
Self::Left(a) => Either::Left(a.batch_executor(db, prune_modes)),
Expand All @@ -59,7 +61,7 @@ where
Output = BlockExecutionOutput<Receipt>,
Error = BlockExecutionError,
>,
DB: Database<Error: Into<ProviderError>>,
DB: Database<Error: Into<ProviderError> + Display>,
{
type Input<'a> = BlockExecutionInput<'a, BlockWithSenders>;
type Output = BlockExecutionOutput<Receipt>;
Expand Down Expand Up @@ -87,7 +89,7 @@ where
Output = BatchBlockExecutionOutput,
Error = BlockExecutionError,
>,
DB: Database<Error: Into<ProviderError>>,
DB: Database<Error: Into<ProviderError> + Display>,
{
type Input<'a> = BlockExecutionInput<'a, BlockWithSenders>;
type Output = BatchBlockExecutionOutput;
Expand Down
18 changes: 10 additions & 8 deletions crates/evm/src/execute.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Traits for execution.
use std::fmt::Display;

use reth_primitives::{BlockNumber, BlockWithSenders, Receipt, Receipts, Request, Requests, U256};
use reth_prune_types::PruneModes;
use revm::db::BundleState;
Expand Down Expand Up @@ -172,15 +174,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: Into<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: Into<ProviderError>>>: for<'a> BatchExecutor<
type BatchExecutor<DB: Database<Error: Into<ProviderError> + Display>>: for<'a> BatchExecutor<
DB,
Input<'a> = BlockExecutionInput<'a, BlockWithSenders>,
// TODO: change to bundle state with receipts
Expand All @@ -193,7 +195,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: Into<ProviderError>>;
DB: Database<Error: Into<ProviderError> + Display>;

/// Creates a new batch executor with the given database and pruning modes.
///
Expand All @@ -204,7 +206,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: Into<ProviderError>>;
DB: Database<Error: Into<ProviderError> + Display>;
}

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

impl BlockExecutorProvider for TestExecutorProvider {
type Executor<DB: Database<Error: Into<ProviderError>>> = TestExecutor<DB>;
type BatchExecutor<DB: Database<Error: Into<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: Into<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: Into<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_primitives::{BlockNumber, BlockWithSenders, Receipt};
use reth_prune_types::PruneModes;
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: Into<ProviderError>>> = Self;
type Executor<DB: Database<Error: Into<ProviderError> + Display>> = Self;

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

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

fn batch_executor<DB>(&self, _: DB, _: PruneModes) -> Self::BatchExecutor<DB>
where
DB: Database<Error: Into<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: Into<ProviderError>>> = Self;
type Executor<DB: Database<Error: Into<ProviderError> + Display>> = Self;

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

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

fn batch_executor<DB>(&self, _: DB, _: PruneModes) -> Self::BatchExecutor<DB>
where
DB: Database<Error: Into<ProviderError>>,
DB: Database<Error: Into<ProviderError> + Display>,
{
self.clone()
}
Expand Down
8 changes: 4 additions & 4 deletions crates/revm/src/state_change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub const HISTORY_SERVE_WINDOW: u64 = 8192;
///
/// [EIP-2935]: https://eips.ethereum.org/EIPS/eip-2935
#[inline]
pub fn apply_blockhashes_update<DB: Database<Error = ProviderError> + DatabaseCommit>(
pub fn apply_blockhashes_update<DB: Database<Error: Into<ProviderError>> + DatabaseCommit>(
db: &mut DB,
chain_spec: &ChainSpec,
block_timestamp: u64,
Expand All @@ -101,7 +101,7 @@ where
// nonce of 1, so it does not get deleted.
let mut account: Account = db
.basic(HISTORY_STORAGE_ADDRESS)
.map_err(BlockValidationError::BlockHashAccountLoadingFailed)?
.map_err(|err| BlockValidationError::BlockHashAccountLoadingFailed(err.into()))?
.unwrap_or_else(|| AccountInfo {
nonce: 1,
code: Some(Bytecode::new_raw(HISTORY_STORAGE_CODE.clone())),
Expand All @@ -125,15 +125,15 @@ where
///
/// This calculates the correct storage slot in the `BLOCKHASH` history storage address, fetches the
/// blockhash and creates a [`EvmStorageSlot`] with appropriate previous and new values.
fn eip2935_block_hash_slot<DB: Database<Error = ProviderError>>(
fn eip2935_block_hash_slot<DB: Database<Error: Into<ProviderError>>>(
db: &mut DB,
block_number: u64,
block_hash: B256,
) -> Result<(U256, EvmStorageSlot), BlockValidationError> {
let slot = U256::from(block_number % HISTORY_SERVE_WINDOW);
let current_hash = db
.storage(HISTORY_STORAGE_ADDRESS, slot)
.map_err(BlockValidationError::BlockHashAccountLoadingFailed)?;
.map_err(|err| BlockValidationError::BlockHashAccountLoadingFailed(err.into()))?;

Ok((slot, EvmStorageSlot::new_changed(current_hash, block_hash.into())))
}
Expand Down

0 comments on commit 0c5e8f0

Please sign in to comment.