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

Commit b638992

Browse files
authored
[feat] enum error in GethExecStep (#1037)
* make error enum * remove non_exhaustive
1 parent 8e0fd2e commit b638992

File tree

8 files changed

+238
-74
lines changed

8 files changed

+238
-74
lines changed

bus-mapping/src/circuit_input_builder/input_state_ref.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1510,7 +1510,7 @@ impl<'a> CircuitInputStateRef<'a> {
15101510
return Ok(Some(ExecError::InvalidOpcode));
15111511
}
15121512

1513-
if let Some(error) = &step.error {
1513+
if let Some(error) = step.error {
15141514
return Ok(Some(get_step_reported_error(&step.op, error)));
15151515
}
15161516

bus-mapping/src/circuit_input_builder/tracer_tests.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,14 @@ use crate::{
44
error::{
55
ContractAddressCollisionError, DepthError, ExecError, InsufficientBalanceError, OogError,
66
},
7-
geth_errors::{
8-
GETH_ERR_GAS_UINT_OVERFLOW, GETH_ERR_OUT_OF_GAS, GETH_ERR_STACK_OVERFLOW,
9-
GETH_ERR_STACK_UNDERFLOW,
10-
},
117
operation::RWCounter,
128
state_db::Account,
139
};
1410
use eth_types::{
1511
address, bytecode,
1612
evm_types::{stack::Stack, Gas, Memory, OpcodeId},
1713
geth_types::GethData,
18-
word, Bytecode, Hash, ToAddress, ToWord, Word,
14+
word, Bytecode, GethExecError, Hash, ToAddress, ToWord, Word,
1915
};
2016
use lazy_static::lazy_static;
2117
use mock::{
@@ -1503,7 +1499,7 @@ fn tracer_err_gas_uint_overflow() {
15031499
let step = &block.geth_traces[0].struct_logs[index];
15041500
let next_step = block.geth_traces[0].struct_logs.get(index + 1);
15051501
assert_eq!(step.op, OpcodeId::MSTORE);
1506-
assert_eq!(step.error, Some(GETH_ERR_GAS_UINT_OVERFLOW.to_string()));
1502+
assert_eq!(step.error, Some(GethExecError::GasUintOverflow));
15071503

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

1677-
assert_eq!(struct_logs[1].error, Some(GETH_ERR_OUT_OF_GAS.to_string()));
1673+
assert_eq!(struct_logs[1].error, Some(GethExecError::OutOfGas));
16781674
}
16791675

16801676
#[test]
@@ -1697,10 +1693,13 @@ fn tracer_err_stack_overflow() {
16971693
let index = block.geth_traces[0].struct_logs.len() - 1; // PUSH2
16981694
let step = &block.geth_traces[0].struct_logs[index];
16991695
let next_step = block.geth_traces[0].struct_logs.get(index + 1);
1700-
assert_eq!(
1696+
assert!(matches!(
17011697
step.error,
1702-
Some(format!("{GETH_ERR_STACK_OVERFLOW} 1024 (1023)"))
1703-
);
1698+
Some(GethExecError::StackOverflow {
1699+
stack_len: 1024,
1700+
limit: 1023,
1701+
})
1702+
));
17041703

17051704
let mut builder = CircuitInputBuilderTx::new(&block, step);
17061705
assert_eq!(
@@ -1728,10 +1727,13 @@ fn tracer_err_stack_underflow() {
17281727
let index = 0; // SWAP5
17291728
let step = &block.geth_traces[0].struct_logs[index];
17301729
let next_step = block.geth_traces[0].struct_logs.get(index + 1);
1731-
assert_eq!(
1730+
assert!(matches!(
17321731
step.error,
1733-
Some(format!("{GETH_ERR_STACK_UNDERFLOW} (0 <=> 6)",))
1734-
);
1732+
Some(GethExecError::StackUnderflow {
1733+
stack_len: 0,
1734+
required: 6,
1735+
})
1736+
));
17351737

17361738
let mut builder = CircuitInputBuilderTx::new(&block, step);
17371739
assert_eq!(

bus-mapping/src/error.rs

Lines changed: 39 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
//! Error module for the bus-mapping crate
22
33
use core::fmt::{Display, Formatter, Result as FmtResult};
4-
use eth_types::{evm_types::OpcodeId, Address, GethExecStep, Word, H256};
4+
use eth_types::{evm_types::OpcodeId, Address, GethExecError, GethExecStep, Word, H256};
55
use ethers_providers::ProviderError;
66
use std::error::Error as StdError;
77

8-
use crate::geth_errors::{
9-
GETH_ERR_GAS_UINT_OVERFLOW, GETH_ERR_OUT_OF_GAS, GETH_ERR_STACK_OVERFLOW,
10-
GETH_ERR_STACK_UNDERFLOW, GETH_ERR_WRITE_PROTECTION,
11-
};
12-
138
/// Error type for any BusMapping related failure.
149
#[derive(Debug)]
1510
pub enum Error {
@@ -185,42 +180,43 @@ pub enum ExecError {
185180
}
186181

187182
// TODO: Move to impl block.
188-
pub(crate) fn get_step_reported_error(op: &OpcodeId, error: &str) -> ExecError {
189-
if [GETH_ERR_OUT_OF_GAS, GETH_ERR_GAS_UINT_OVERFLOW].contains(&error) {
190-
// NOTE: We report a GasUintOverflow error as an OutOfGas error
191-
let oog_err = match op {
192-
OpcodeId::MLOAD | OpcodeId::MSTORE | OpcodeId::MSTORE8 => {
193-
OogError::StaticMemoryExpansion
194-
}
195-
OpcodeId::RETURN | OpcodeId::REVERT => OogError::DynamicMemoryExpansion,
196-
OpcodeId::CALLDATACOPY
197-
| OpcodeId::CODECOPY
198-
| OpcodeId::EXTCODECOPY
199-
| OpcodeId::RETURNDATACOPY => OogError::MemoryCopy,
200-
OpcodeId::BALANCE | OpcodeId::EXTCODESIZE | OpcodeId::EXTCODEHASH => {
201-
OogError::AccountAccess
202-
}
203-
OpcodeId::LOG0 | OpcodeId::LOG1 | OpcodeId::LOG2 | OpcodeId::LOG3 | OpcodeId::LOG4 => {
204-
OogError::Log
205-
}
206-
OpcodeId::EXP => OogError::Exp,
207-
OpcodeId::SHA3 => OogError::Sha3,
208-
OpcodeId::CALL | OpcodeId::CALLCODE | OpcodeId::DELEGATECALL | OpcodeId::STATICCALL => {
209-
OogError::Call
210-
}
211-
OpcodeId::SLOAD | OpcodeId::SSTORE => OogError::SloadSstore,
212-
OpcodeId::CREATE | OpcodeId::CREATE2 => OogError::Create,
213-
OpcodeId::SELFDESTRUCT => OogError::SelfDestruct,
214-
_ => OogError::Constant,
215-
};
216-
ExecError::OutOfGas(oog_err)
217-
} else if error.starts_with(GETH_ERR_STACK_OVERFLOW) {
218-
ExecError::StackOverflow
219-
} else if error.starts_with(GETH_ERR_STACK_UNDERFLOW) {
220-
ExecError::StackUnderflow
221-
} else if error.starts_with(GETH_ERR_WRITE_PROTECTION) {
222-
ExecError::WriteProtection
223-
} else {
224-
panic!("Unknown GethExecStep.error: {error}");
183+
pub(crate) fn get_step_reported_error(op: &OpcodeId, error: GethExecError) -> ExecError {
184+
match error {
185+
GethExecError::OutOfGas | GethExecError::GasUintOverflow => {
186+
// NOTE: We report a GasUintOverflow error as an OutOfGas error
187+
let oog_err = match op {
188+
OpcodeId::MLOAD | OpcodeId::MSTORE | OpcodeId::MSTORE8 => {
189+
OogError::StaticMemoryExpansion
190+
}
191+
OpcodeId::RETURN | OpcodeId::REVERT => OogError::DynamicMemoryExpansion,
192+
OpcodeId::CALLDATACOPY
193+
| OpcodeId::CODECOPY
194+
| OpcodeId::EXTCODECOPY
195+
| OpcodeId::RETURNDATACOPY => OogError::MemoryCopy,
196+
OpcodeId::BALANCE | OpcodeId::EXTCODESIZE | OpcodeId::EXTCODEHASH => {
197+
OogError::AccountAccess
198+
}
199+
OpcodeId::LOG0
200+
| OpcodeId::LOG1
201+
| OpcodeId::LOG2
202+
| OpcodeId::LOG3
203+
| OpcodeId::LOG4 => OogError::Log,
204+
OpcodeId::EXP => OogError::Exp,
205+
OpcodeId::SHA3 => OogError::Sha3,
206+
OpcodeId::CALL
207+
| OpcodeId::CALLCODE
208+
| OpcodeId::DELEGATECALL
209+
| OpcodeId::STATICCALL => OogError::Call,
210+
OpcodeId::SLOAD | OpcodeId::SSTORE => OogError::SloadSstore,
211+
OpcodeId::CREATE | OpcodeId::CREATE2 => OogError::Create,
212+
OpcodeId::SELFDESTRUCT => OogError::SelfDestruct,
213+
_ => OogError::Constant,
214+
};
215+
ExecError::OutOfGas(oog_err)
216+
}
217+
GethExecError::StackOverflow { .. } => ExecError::StackOverflow,
218+
GethExecError::StackUnderflow { .. } => ExecError::StackUnderflow,
219+
GethExecError::WriteProtection => ExecError::WriteProtection,
220+
_ => panic!("Unknown GethExecStep.error: {error}"),
225221
}
226222
}

bus-mapping/src/geth_errors.rs

Lines changed: 0 additions & 10 deletions
This file was deleted.

bus-mapping/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,6 @@ pub mod circuit_input_builder;
234234
pub mod error;
235235
pub mod evm;
236236
pub mod exec_trace;
237-
pub(crate) mod geth_errors;
238237
pub mod l2_predeployed;
239238
pub mod mock;
240239
pub mod operation;

eth-types/src/l2_types.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use crate::{
44
evm_types::{Gas, GasCost, Memory, OpcodeId, ProgramCounter, Stack, Storage},
5-
Block, GethExecStep, GethExecTrace, Hash, Transaction, Word, H256,
5+
Block, GethExecError, GethExecStep, GethExecTrace, Hash, Transaction, Word, H256,
66
};
77
use ethers_core::types::{Address, Bytes, U256, U64};
88
use serde::{Deserialize, Serialize};
@@ -225,7 +225,7 @@ pub struct ExecStep {
225225
#[serde(default)]
226226
pub refund: u64,
227227
pub depth: isize,
228-
pub error: Option<String>,
228+
pub error: Option<GethExecError>,
229229
pub stack: Option<Vec<Word>>,
230230
pub memory: Option<Vec<Word>>,
231231
pub storage: Option<HashMap<Word, Word>>,

0 commit comments

Comments
 (0)