From d63f79ba2a92b22ca5fd79fb640dcf4af00f1cee Mon Sep 17 00:00:00 2001 From: Daniil Naumetc Date: Tue, 16 Jan 2024 22:32:54 +0100 Subject: [PATCH 1/3] feat(crates): custom evm error targeting optimism --- crates/primitives/src/result.rs | 6 ++++++ crates/revm/src/optimism/handler_register.rs | 20 +++++++++++++++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/crates/primitives/src/result.rs b/crates/primitives/src/result.rs index c91c9052eb..11acc43694 100644 --- a/crates/primitives/src/result.rs +++ b/crates/primitives/src/result.rs @@ -135,6 +135,11 @@ pub enum EVMError { Header(InvalidHeader), /// Database error. Database(DBError), + /// Custom error. + /// + /// Currently implemented for the Optimism handler. This is an interim solution pending + /// the development of a more generic error mechanism for integration with custom handlers. + Custom(String), } #[cfg(feature = "std")] @@ -146,6 +151,7 @@ impl fmt::Display for EVMError { EVMError::Transaction(e) => write!(f, "Transaction error: {e:?}"), EVMError::Header(e) => write!(f, "Header error: {e:?}"), EVMError::Database(e) => write!(f, "Database error: {e}"), + EVMError::Custom(e) => write!(f, "Custom error: {e}"), } } } diff --git a/crates/revm/src/optimism/handler_register.rs b/crates/revm/src/optimism/handler_register.rs index e3ff387fda..b0a5c91cdd 100644 --- a/crates/revm/src/optimism/handler_register.rs +++ b/crates/revm/src/optimism/handler_register.rs @@ -125,7 +125,9 @@ pub fn deduct_caller( if context.evm.env.tx.optimism.source_hash.is_none() { // get envelope let Some(enveloped_tx) = context.evm.env.tx.optimism.enveloped_tx.clone() else { - panic!("[OPTIMISM] Failed to load enveloped transaction."); + return Err(EVMError::Custom( + "[OPTIMISM] Failed to load enveloped transaction.".to_string(), + )); }; let tx_l1_cost = context @@ -166,11 +168,15 @@ pub fn reward_beneficiary( // If the transaction is not a deposit transaction, fees are paid out // to both the Base Fee Vault as well as the L1 Fee Vault. let Some(l1_block_info) = context.evm.l1_block_info.clone() else { - panic!("[OPTIMISM] Failed to load L1 block information."); + return Err(EVMError::Custom( + "[OPTIMISM] Failed to load L1 block information.".to_string(), + )); }; let Some(enveloped_tx) = &context.evm.env.tx.optimism.enveloped_tx else { - panic!("[OPTIMISM] Failed to load enveloped transaction."); + return Err(EVMError::Custom( + "[OPTIMISM] Failed to load enveloped transaction.".to_string(), + )); }; let l1_cost = l1_block_info.calculate_tx_l1_cost(enveloped_tx, SPEC::SPEC_ID); @@ -181,7 +187,9 @@ pub fn reward_beneficiary( .journaled_state .load_account(optimism::L1_FEE_RECIPIENT, &mut context.evm.db) else { - panic!("[OPTIMISM] Failed to load L1 Fee Vault account"); + return Err(EVMError::Custom( + "[OPTIMISM] Failed to load L1 Fee Vault account.".to_string(), + )); }; l1_fee_vault_account.mark_touch(); l1_fee_vault_account.info.balance += l1_cost; @@ -192,7 +200,9 @@ pub fn reward_beneficiary( .journaled_state .load_account(optimism::BASE_FEE_RECIPIENT, &mut context.evm.db) else { - panic!("[OPTIMISM] Failed to load Base Fee Vault account"); + return Err(EVMError::Custom( + "[OPTIMISM] Failed to load Base Fee Vault account.".to_string(), + )); }; base_fee_vault_account.mark_touch(); base_fee_vault_account.info.balance += context From 6727f3f42db0e04206222ff4fd167784c17eecaa Mon Sep 17 00:00:00 2001 From: Daniil Naumetc Date: Tue, 16 Jan 2024 22:51:44 +0100 Subject: [PATCH 2/3] fix(crates): alloc String --- crates/primitives/src/result.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/primitives/src/result.rs b/crates/primitives/src/result.rs index 11acc43694..34bb14877b 100644 --- a/crates/primitives/src/result.rs +++ b/crates/primitives/src/result.rs @@ -1,5 +1,5 @@ use crate::{Address, Bytes, Log, State, U256}; -use alloc::{boxed::Box, vec::Vec}; +use alloc::{boxed::Box, string::String, vec::Vec}; use core::fmt; /// Result of EVM execution. From 19801359440d72614ec7e4a49f2e2bd055acbfd0 Mon Sep 17 00:00:00 2001 From: Daniil Naumetc <11177808+kekonen@users.noreply.github.com> Date: Wed, 17 Jan 2024 11:38:10 +0100 Subject: [PATCH 3/3] Update crates/primitives/src/result.rs Co-authored-by: rakita --- crates/primitives/src/result.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/primitives/src/result.rs b/crates/primitives/src/result.rs index 34bb14877b..b765dd7aef 100644 --- a/crates/primitives/src/result.rs +++ b/crates/primitives/src/result.rs @@ -137,8 +137,7 @@ pub enum EVMError { Database(DBError), /// Custom error. /// - /// Currently implemented for the Optimism handler. This is an interim solution pending - /// the development of a more generic error mechanism for integration with custom handlers. + /// Useful for handler registers where custom logic would want to return their own custom error. Custom(String), }