Skip to content

Commit

Permalink
refactor: add error handling to evm module (lambdaclass#142)
Browse files Browse the repository at this point in the history
**Motivation**

Add error handling to `execute_tx` and remove unwrap
<!-- Why does this pull request exist? What are its goals? -->

**Description**

Add `EvmError`
Map revm's `EVMError` to `EvmError`
Add error handling to `execute_tx`

<!-- A clear and concise general description of the changes this PR
introduces -->

<!-- Link to issues: Resolves lambdaclass#111, Resolves lambdaclass#222 -->

Closes  None
  • Loading branch information
fmoletta authored Jul 12, 2024
1 parent f0c69f3 commit 5080f90
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
30 changes: 30 additions & 0 deletions crates/core/src/evm/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use std::fmt::Display;

use revm::primitives::result::EVMError as RevmError;
use thiserror::Error;

#[derive(Debug, Error)]
pub enum EvmError {
#[error("Invalid Transaction: {0}")]
Transaction(String),
#[error("Invalid Header: {0}")]
Header(String),
#[error("DB error: {0}")]
DB(String),
#[error("{0}")]
Custom(String),
#[error("{0}")]
Precompile(String),
}

impl<T: Display> From<RevmError<T>> for EvmError {
fn from(value: RevmError<T>) -> Self {
match value {
RevmError::Transaction(err) => EvmError::Transaction(err.to_string()),
RevmError::Header(err) => EvmError::Header(err.to_string()),
RevmError::Database(err) => EvmError::DB(err.to_string()),
RevmError::Custom(err) => EvmError::Custom(err),
RevmError::Precompile(err) => EvmError::Precompile(err),
}
}
}
8 changes: 5 additions & 3 deletions crates/core/src/evm/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod errors;
mod execution_result;

use crate::types::TxKind;
Expand All @@ -18,6 +19,7 @@ use revm::primitives::AccountInfo as RevmAccountInfo;
use revm::primitives::Address as RevmAddress;
use revm::primitives::TxKind as RevmTxKind;
// Export needed types
pub use errors::EvmError;
pub use execution_result::*;
pub use revm::primitives::SpecId;

Expand All @@ -26,7 +28,7 @@ pub fn execute_tx(
header: &BlockHeader,
pre: &HashMap<Address, Account>, // TODO: Modify this type when we have a defined State structure
spec_id: SpecId,
) -> ExecutionResult {
) -> Result<ExecutionResult, EvmError> {
let block_env = block_env(header);
let tx_env = tx_env(tx);
let cache_state = cache_state(pre);
Expand All @@ -43,8 +45,8 @@ pub fn execute_tx(
.with_external_context(TracerEip3155::new(Box::new(std::io::stderr())).without_summary())
.append_handler_register(inspector_handle_register)
.build();
let tx_result = evm.transact().unwrap();
tx_result.result.into()
let tx_result = evm.transact().map_err(EvmError::from)?;
Ok(tx_result.result.into())
}

fn cache_state(pre: &HashMap<Address, Account>) -> CacheState {
Expand Down
1 change: 1 addition & 0 deletions ef_tests/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ fn execute_test(test: &TestUnit) {
&pre,
SpecId::CANCUN,
)
.unwrap()
.is_success());
}

Expand Down

0 comments on commit 5080f90

Please sign in to comment.