Skip to content

Commit

Permalink
feat: Convert optimism panic into graceful error (bluealloy#982)
Browse files Browse the repository at this point in the history
* feat(crates): custom evm error targeting optimism

* fix(crates): alloc String

* Update crates/primitives/src/result.rs

Co-authored-by: rakita <[email protected]>

---------

Co-authored-by: rakita <[email protected]>
  • Loading branch information
kekonen and rakita authored Jan 17, 2024
1 parent ed524cb commit 4aa835a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
7 changes: 6 additions & 1 deletion crates/primitives/src/result.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -135,6 +135,10 @@ pub enum EVMError<DBError> {
Header(InvalidHeader),
/// Database error.
Database(DBError),
/// Custom error.
///
/// Useful for handler registers where custom logic would want to return their own custom error.
Custom(String),
}

#[cfg(feature = "std")]
Expand All @@ -146,6 +150,7 @@ impl<DBError: fmt::Display> fmt::Display for EVMError<DBError> {
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}"),
}
}
}
Expand Down
20 changes: 15 additions & 5 deletions crates/revm/src/optimism/handler_register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ pub fn deduct_caller<SPEC: Spec, EXT, DB: Database>(
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
Expand Down Expand Up @@ -166,11 +168,15 @@ pub fn reward_beneficiary<SPEC: Spec, EXT, DB: Database>(
// 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);
Expand All @@ -181,7 +187,9 @@ pub fn reward_beneficiary<SPEC: Spec, EXT, DB: Database>(
.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;
Expand All @@ -192,7 +200,9 @@ pub fn reward_beneficiary<SPEC: Spec, EXT, DB: Database>(
.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
Expand Down

0 comments on commit 4aa835a

Please sign in to comment.