Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Convert optimism panic into graceful error #982

Merged
merged 3 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading