Skip to content
This repository was archived by the owner on Apr 18, 2025. It is now read-only.

[feat] enum error in GethExecStep #1037

Merged
merged 4 commits into from
Nov 23, 2023
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
2 changes: 1 addition & 1 deletion bus-mapping/src/circuit_input_builder/input_state_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1510,7 +1510,7 @@ impl<'a> CircuitInputStateRef<'a> {
return Ok(Some(ExecError::InvalidOpcode));
}

if let Some(error) = &step.error {
if let Some(error) = step.error {
return Ok(Some(get_step_reported_error(&step.op, error)));
}

Expand Down
28 changes: 15 additions & 13 deletions bus-mapping/src/circuit_input_builder/tracer_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,14 @@ use crate::{
error::{
ContractAddressCollisionError, DepthError, ExecError, InsufficientBalanceError, OogError,
},
geth_errors::{
GETH_ERR_GAS_UINT_OVERFLOW, GETH_ERR_OUT_OF_GAS, GETH_ERR_STACK_OVERFLOW,
GETH_ERR_STACK_UNDERFLOW,
},
operation::RWCounter,
state_db::Account,
};
use eth_types::{
address, bytecode,
evm_types::{stack::Stack, Gas, Memory, OpcodeId},
geth_types::GethData,
word, Bytecode, Hash, ToAddress, ToWord, Word,
word, Bytecode, GethExecError, Hash, ToAddress, ToWord, Word,
};
use lazy_static::lazy_static;
use mock::{
Expand Down Expand Up @@ -1503,7 +1499,7 @@ fn tracer_err_gas_uint_overflow() {
let step = &block.geth_traces[0].struct_logs[index];
let next_step = block.geth_traces[0].struct_logs.get(index + 1);
assert_eq!(step.op, OpcodeId::MSTORE);
assert_eq!(step.error, Some(GETH_ERR_GAS_UINT_OVERFLOW.to_string()));
assert_eq!(step.error, Some(GethExecError::GasUintOverflow));

let mut builder = CircuitInputBuilderTx::new(&block, step);
assert_eq!(
Expand Down Expand Up @@ -1674,7 +1670,7 @@ fn tracer_err_out_of_gas() {
.into();
let struct_logs = &block.geth_traces[0].struct_logs;

assert_eq!(struct_logs[1].error, Some(GETH_ERR_OUT_OF_GAS.to_string()));
assert_eq!(struct_logs[1].error, Some(GethExecError::OutOfGas));
}

#[test]
Expand All @@ -1697,10 +1693,13 @@ fn tracer_err_stack_overflow() {
let index = block.geth_traces[0].struct_logs.len() - 1; // PUSH2
let step = &block.geth_traces[0].struct_logs[index];
let next_step = block.geth_traces[0].struct_logs.get(index + 1);
assert_eq!(
assert!(matches!(
step.error,
Some(format!("{GETH_ERR_STACK_OVERFLOW} 1024 (1023)"))
);
Some(GethExecError::StackOverflow {
stack_len: 1024,
limit: 1023,
})
));

let mut builder = CircuitInputBuilderTx::new(&block, step);
assert_eq!(
Expand Down Expand Up @@ -1728,10 +1727,13 @@ fn tracer_err_stack_underflow() {
let index = 0; // SWAP5
let step = &block.geth_traces[0].struct_logs[index];
let next_step = block.geth_traces[0].struct_logs.get(index + 1);
assert_eq!(
assert!(matches!(
step.error,
Some(format!("{GETH_ERR_STACK_UNDERFLOW} (0 <=> 6)",))
);
Some(GethExecError::StackUnderflow {
stack_len: 0,
required: 6,
})
));

let mut builder = CircuitInputBuilderTx::new(&block, step);
assert_eq!(
Expand Down
82 changes: 39 additions & 43 deletions bus-mapping/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
//! Error module for the bus-mapping crate

use core::fmt::{Display, Formatter, Result as FmtResult};
use eth_types::{evm_types::OpcodeId, Address, GethExecStep, Word, H256};
use eth_types::{evm_types::OpcodeId, Address, GethExecError, GethExecStep, Word, H256};
use ethers_providers::ProviderError;
use std::error::Error as StdError;

use crate::geth_errors::{
GETH_ERR_GAS_UINT_OVERFLOW, GETH_ERR_OUT_OF_GAS, GETH_ERR_STACK_OVERFLOW,
GETH_ERR_STACK_UNDERFLOW, GETH_ERR_WRITE_PROTECTION,
};

/// Error type for any BusMapping related failure.
#[derive(Debug)]
pub enum Error {
Expand Down Expand Up @@ -185,42 +180,43 @@ pub enum ExecError {
}

// TODO: Move to impl block.
pub(crate) fn get_step_reported_error(op: &OpcodeId, error: &str) -> ExecError {
if [GETH_ERR_OUT_OF_GAS, GETH_ERR_GAS_UINT_OVERFLOW].contains(&error) {
// NOTE: We report a GasUintOverflow error as an OutOfGas error
let oog_err = match op {
OpcodeId::MLOAD | OpcodeId::MSTORE | OpcodeId::MSTORE8 => {
OogError::StaticMemoryExpansion
}
OpcodeId::RETURN | OpcodeId::REVERT => OogError::DynamicMemoryExpansion,
OpcodeId::CALLDATACOPY
| OpcodeId::CODECOPY
| OpcodeId::EXTCODECOPY
| OpcodeId::RETURNDATACOPY => OogError::MemoryCopy,
OpcodeId::BALANCE | OpcodeId::EXTCODESIZE | OpcodeId::EXTCODEHASH => {
OogError::AccountAccess
}
OpcodeId::LOG0 | OpcodeId::LOG1 | OpcodeId::LOG2 | OpcodeId::LOG3 | OpcodeId::LOG4 => {
OogError::Log
}
OpcodeId::EXP => OogError::Exp,
OpcodeId::SHA3 => OogError::Sha3,
OpcodeId::CALL | OpcodeId::CALLCODE | OpcodeId::DELEGATECALL | OpcodeId::STATICCALL => {
OogError::Call
}
OpcodeId::SLOAD | OpcodeId::SSTORE => OogError::SloadSstore,
OpcodeId::CREATE | OpcodeId::CREATE2 => OogError::Create,
OpcodeId::SELFDESTRUCT => OogError::SelfDestruct,
_ => OogError::Constant,
};
ExecError::OutOfGas(oog_err)
} else if error.starts_with(GETH_ERR_STACK_OVERFLOW) {
ExecError::StackOverflow
} else if error.starts_with(GETH_ERR_STACK_UNDERFLOW) {
ExecError::StackUnderflow
} else if error.starts_with(GETH_ERR_WRITE_PROTECTION) {
ExecError::WriteProtection
} else {
panic!("Unknown GethExecStep.error: {error}");
pub(crate) fn get_step_reported_error(op: &OpcodeId, error: GethExecError) -> ExecError {
match error {
GethExecError::OutOfGas | GethExecError::GasUintOverflow => {
// NOTE: We report a GasUintOverflow error as an OutOfGas error
let oog_err = match op {
OpcodeId::MLOAD | OpcodeId::MSTORE | OpcodeId::MSTORE8 => {
OogError::StaticMemoryExpansion
}
OpcodeId::RETURN | OpcodeId::REVERT => OogError::DynamicMemoryExpansion,
OpcodeId::CALLDATACOPY
| OpcodeId::CODECOPY
| OpcodeId::EXTCODECOPY
| OpcodeId::RETURNDATACOPY => OogError::MemoryCopy,
OpcodeId::BALANCE | OpcodeId::EXTCODESIZE | OpcodeId::EXTCODEHASH => {
OogError::AccountAccess
}
OpcodeId::LOG0
| OpcodeId::LOG1
| OpcodeId::LOG2
| OpcodeId::LOG3
| OpcodeId::LOG4 => OogError::Log,
OpcodeId::EXP => OogError::Exp,
OpcodeId::SHA3 => OogError::Sha3,
OpcodeId::CALL
| OpcodeId::CALLCODE
| OpcodeId::DELEGATECALL
| OpcodeId::STATICCALL => OogError::Call,
OpcodeId::SLOAD | OpcodeId::SSTORE => OogError::SloadSstore,
OpcodeId::CREATE | OpcodeId::CREATE2 => OogError::Create,
OpcodeId::SELFDESTRUCT => OogError::SelfDestruct,
_ => OogError::Constant,
};
ExecError::OutOfGas(oog_err)
}
GethExecError::StackOverflow { .. } => ExecError::StackOverflow,
GethExecError::StackUnderflow { .. } => ExecError::StackUnderflow,
GethExecError::WriteProtection => ExecError::WriteProtection,
_ => panic!("Unknown GethExecStep.error: {error}"),
}
}
10 changes: 0 additions & 10 deletions bus-mapping/src/geth_errors.rs

This file was deleted.

1 change: 0 additions & 1 deletion bus-mapping/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,6 @@ pub mod circuit_input_builder;
pub mod error;
pub mod evm;
pub mod exec_trace;
pub(crate) mod geth_errors;
pub mod l2_predeployed;
pub mod mock;
pub mod operation;
Expand Down
4 changes: 2 additions & 2 deletions eth-types/src/l2_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use crate::{
evm_types::{Gas, GasCost, Memory, OpcodeId, ProgramCounter, Stack, Storage},
Block, GethExecStep, GethExecTrace, Hash, Transaction, Word, H256,
Block, GethExecError, GethExecStep, GethExecTrace, Hash, Transaction, Word, H256,
};
use ethers_core::types::{Address, Bytes, U256, U64};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -225,7 +225,7 @@ pub struct ExecStep {
#[serde(default)]
pub refund: u64,
pub depth: isize,
pub error: Option<String>,
pub error: Option<GethExecError>,
pub stack: Option<Vec<Word>>,
pub memory: Option<Vec<Word>>,
pub storage: Option<HashMap<Word, Word>>,
Expand Down
Loading