From f6189ca98b433be6f4955c324291a17b33a5c05b Mon Sep 17 00:00:00 2001 From: Roman Krasiuk Date: Fri, 1 Dec 2023 13:59:40 +0200 Subject: [PATCH] chore(evm): use provider errors --- crates/interfaces/src/executor.rs | 9 ++++++--- crates/payload/basic/src/lib.rs | 8 +++++--- crates/payload/basic/src/optimism.rs | 4 +--- crates/payload/builder/src/error.rs | 9 +++++---- crates/revm/src/database.rs | 4 ++-- crates/revm/src/optimism/mod.rs | 6 +++--- crates/revm/src/optimism/processor.rs | 10 ++++++++-- crates/revm/src/processor.rs | 15 +++++++-------- 8 files changed, 37 insertions(+), 28 deletions(-) diff --git a/crates/interfaces/src/executor.rs b/crates/interfaces/src/executor.rs index fa677ad814c8..e6d2d1f3f831 100644 --- a/crates/interfaces/src/executor.rs +++ b/crates/interfaces/src/executor.rs @@ -1,4 +1,4 @@ -use crate::RethError; +use crate::provider::ProviderError; use reth_primitives::{ revm_primitives::EVMError, BlockNumHash, Bloom, GotExpected, GotExpectedBoxed, PruneSegmentError, B256, @@ -15,7 +15,7 @@ pub enum BlockValidationError { hash: B256, /// The EVM error. #[source] - error: Box>, + error: Box>, }, /// Error when recovering the sender for a transaction #[error("failed to recover sender for transaction")] @@ -127,11 +127,14 @@ pub enum BlockExecutionError { #[derive(Error, Debug, Clone, PartialEq, Eq)] pub enum OptimismBlockExecutionError { /// Error when trying to parse L1 block info - #[error("Could not get L1 block info from L2 block: {message:?}")] + #[error("could not get L1 block info from L2 block: {message:?}")] L1BlockInfoError { /// The inner error message message: String, }, + /// Thrown when force deploy of create2deployer code fails. + #[error("failed to force create2deployer account code")] + ForceCreate2DeployerFail, } impl BlockExecutionError { diff --git a/crates/payload/basic/src/lib.rs b/crates/payload/basic/src/lib.rs index db3b9491c0b0..a275ad5d78a1 100644 --- a/crates/payload/basic/src/lib.rs +++ b/crates/payload/basic/src/lib.rs @@ -13,7 +13,7 @@ use crate::metrics::PayloadBuilderMetrics; use alloy_rlp::Encodable; use futures_core::ready; use futures_util::FutureExt; -use reth_interfaces::{RethError, RethResult}; +use reth_interfaces::RethResult; use reth_payload_builder::{ database::CachedReads, error::PayloadBuilderError, BuiltPayload, KeepPayloadJobAlive, PayloadBuilderAttributes, PayloadId, PayloadJob, PayloadJobGenerator, @@ -30,7 +30,9 @@ use reth_primitives::{ Block, BlockNumberOrTag, Bytes, ChainSpec, Header, IntoRecoveredTransaction, Receipt, Receipts, SealedBlock, Withdrawal, B256, EMPTY_OMMER_ROOT_HASH, U256, }; -use reth_provider::{BlockReaderIdExt, BlockSource, BundleStateWithReceipts, StateProviderFactory}; +use reth_provider::{ + BlockReaderIdExt, BlockSource, BundleStateWithReceipts, ProviderError, StateProviderFactory, +}; use reth_revm::{ database::StateProviderDatabase, state_change::{apply_beacon_root_contract_call, post_block_withdrawals_balance_increments}, @@ -1159,7 +1161,7 @@ impl WithdrawalsOutcome { /// Returns the withdrawals root. /// /// Returns `None` values pre shanghai -fn commit_withdrawals>( +fn commit_withdrawals>( db: &mut State, chain_spec: &ChainSpec, timestamp: u64, diff --git a/crates/payload/basic/src/optimism.rs b/crates/payload/basic/src/optimism.rs index d316676d139b..2f27854586c2 100644 --- a/crates/payload/basic/src/optimism.rs +++ b/crates/payload/basic/src/optimism.rs @@ -60,9 +60,7 @@ where // the above check for empty blocks will never be hit on OP chains. reth_revm::optimism::ensure_create2_deployer(chain_spec.clone(), attributes.timestamp, &mut db) .map_err(|_| { - PayloadBuilderError::Internal(RethError::Custom( - "Failed to force create2deployer account code".to_string(), - )) + PayloadBuilderError::Optimism(OptimismPayloadBuilderError::ForceCreate2DeployerFail) })?; let mut receipts = Vec::new(); diff --git a/crates/payload/builder/src/error.rs b/crates/payload/builder/src/error.rs index d42366d121cc..cd998f156a4a 100644 --- a/crates/payload/builder/src/error.rs +++ b/crates/payload/builder/src/error.rs @@ -22,7 +22,7 @@ pub enum PayloadBuilderError { Internal(#[from] RethError), /// Unrecoverable error during evm execution. #[error("evm execution error: {0}")] - EvmExecutionError(EVMError), + EvmExecutionError(EVMError), /// Thrown if the payload requests withdrawals before Shanghai activation. #[error("withdrawals set before Shanghai activation")] WithdrawalsBeforeShanghai, @@ -39,22 +39,23 @@ impl From for PayloadBuilderError { } /// Optimism specific payload building errors. +#[cfg(feature = "optimism")] #[derive(Debug, thiserror::Error)] pub enum OptimismPayloadBuilderError { /// Thrown when a transaction fails to convert to a /// [reth_primitives::TransactionSignedEcRecovered]. - #[cfg(feature = "optimism")] #[error("failed to convert deposit transaction to TransactionSignedEcRecovered")] TransactionEcRecoverFailed, /// Thrown when the L1 block info could not be parsed from the calldata of the /// first transaction supplied in the payload attributes. - #[cfg(feature = "optimism")] #[error("failed to parse L1 block info from L1 info tx calldata")] L1BlockInfoParseFailed, /// Thrown when a database account could not be loaded. #[error("failed to load account {0:?}")] - #[cfg(feature = "optimism")] AccountLoadFailed(revm_primitives::Address), + /// Thrown when force deploy of create2deployer code fails. + #[error("failed to force create2deployer account code")] + ForceCreate2DeployerFail, } impl From for PayloadBuilderError { diff --git a/crates/revm/src/database.rs b/crates/revm/src/database.rs index 6712020b4306..0486043a5ae4 100644 --- a/crates/revm/src/database.rs +++ b/crates/revm/src/database.rs @@ -1,6 +1,6 @@ use reth_interfaces::RethError; use reth_primitives::{Address, B256, KECCAK_EMPTY, U256}; -use reth_provider::StateProvider; +use reth_provider::{ProviderError, StateProvider}; use revm::{ db::{CacheDB, DatabaseRef}, primitives::{AccountInfo, Bytecode}, @@ -40,7 +40,7 @@ impl StateProviderDatabase { } impl Database for StateProviderDatabase { - type Error = RethError; + type Error = ProviderError; fn basic(&mut self, address: Address) -> Result, Self::Error> { Ok(self.0.basic_account(address)?.map(|account| AccountInfo { diff --git a/crates/revm/src/optimism/mod.rs b/crates/revm/src/optimism/mod.rs index 3ecb632a6c33..812b8f4695ca 100644 --- a/crates/revm/src/optimism/mod.rs +++ b/crates/revm/src/optimism/mod.rs @@ -180,9 +180,9 @@ where // If the canyon hardfork is active at the current timestamp, and it was not active at the // previous block timestamp (heuristically, block time is not perfectly constant at 2s), and the // chain is an optimism chain, then we need to force-deploy the create2 deployer contract. - if chain_spec.is_fork_active_at_timestamp(Hardfork::Canyon, timestamp) && - !chain_spec.is_fork_active_at_timestamp(Hardfork::Canyon, timestamp - 2) && - chain_spec.is_optimism() + if chain_spec.is_optimism() && + chain_spec.is_fork_active_at_timestamp(Hardfork::Canyon, timestamp) && + !chain_spec.is_fork_active_at_timestamp(Hardfork::Canyon, timestamp - 2) { trace!(target: "evm", "Forcing create2 deployer contract deployment on Canyon transition"); diff --git a/crates/revm/src/optimism/processor.rs b/crates/revm/src/optimism/processor.rs index c0f3ba6a57ee..cef7cc204eac 100644 --- a/crates/revm/src/optimism/processor.rs +++ b/crates/revm/src/optimism/processor.rs @@ -1,5 +1,7 @@ use crate::processor::{verify_receipt, EVMProcessor}; -use reth_interfaces::executor::{BlockExecutionError, BlockValidationError}; +use reth_interfaces::executor::{ + BlockExecutionError, BlockValidationError, OptimismBlockExecutionError, +}; use reth_primitives::{ revm::compat::into_reth_log, revm_primitives::ResultAndState, Address, Block, Hardfork, Receipt, U256, @@ -74,7 +76,11 @@ impl<'a> BlockExecutor for EVMProcessor<'a> { // so we can safely assume that this will always be triggered upon the transition and that // the above check for empty blocks will never be hit on OP chains. super::ensure_create2_deployer(self.chain_spec().clone(), block.timestamp, self.db_mut()) - .map_err(|_| BlockExecutionError::ProviderError)?; + .map_err(|_| { + BlockExecutionError::OptimismBlockExecution( + OptimismBlockExecutionError::ForceCreate2DeployerFail, + ) + })?; let mut cumulative_gas_used = 0; let mut receipts = Vec::with_capacity(block.body.len()); diff --git a/crates/revm/src/processor.rs b/crates/revm/src/processor.rs index 0a4806e7d461..708943d88b77 100644 --- a/crates/revm/src/processor.rs +++ b/crates/revm/src/processor.rs @@ -4,17 +4,16 @@ use crate::{ stack::{InspectorStack, InspectorStackConfig}, state_change::{apply_beacon_root_contract_call, post_block_balance_increments}, }; -use reth_interfaces::{ - executor::{BlockExecutionError, BlockValidationError}, - RethError, -}; +use reth_interfaces::executor::{BlockExecutionError, BlockValidationError}; use reth_primitives::{ revm::env::{fill_cfg_and_block_env, fill_tx_env}, Address, Block, BlockNumber, Bloom, ChainSpec, GotExpected, Hardfork, Header, PruneMode, PruneModes, PruneSegmentError, Receipt, ReceiptWithBloom, Receipts, TransactionSigned, B256, MINIMUM_PRUNING_DISTANCE, U256, }; -use reth_provider::{BlockExecutor, BlockExecutorStats, PrunableBlockExecutor, StateProvider}; +use reth_provider::{ + BlockExecutor, BlockExecutorStats, ProviderError, PrunableBlockExecutor, StateProvider, +}; use revm::{ db::{states::bundle_state::BundleRetention, StateDBBox}, primitives::ResultAndState, @@ -53,7 +52,7 @@ pub struct EVMProcessor<'a> { /// The configured chain-spec pub(crate) chain_spec: Arc, /// revm instance that contains database and env environment. - pub(crate) evm: EVM>, + pub(crate) evm: EVM>, /// Hook and inspector stack that we want to invoke on that hook. stack: InspectorStack, /// The collection of receipts. @@ -115,7 +114,7 @@ impl<'a> EVMProcessor<'a> { /// Create a new EVM processor with the given revm state. pub fn new_with_state( chain_spec: Arc, - revm_state: StateDBBox<'a, RethError>, + revm_state: StateDBBox<'a, ProviderError>, ) -> Self { let mut evm = EVM::new(); evm.database(revm_state); @@ -143,7 +142,7 @@ impl<'a> EVMProcessor<'a> { } /// Returns a reference to the database - pub fn db_mut(&mut self) -> &mut StateDBBox<'a, RethError> { + pub fn db_mut(&mut self) -> &mut StateDBBox<'a, ProviderError> { // Option will be removed from EVM in the future. // as it is always some. // https://github.com/bluealloy/revm/issues/697