From bd58553839ee8a5db003cd04280c171c2a30b4ec Mon Sep 17 00:00:00 2001 From: Steven Date: Mon, 27 Mar 2023 12:52:01 +0800 Subject: [PATCH] fix: merge some errors to `StackOnlyOpcode` in bus-mapping (#431) * Merge some errors to `StackOnlyOpcode` in bus-mapping. * Fix lint. --- bus-mapping/src/evm/opcodes.rs | 30 +++++++-------- bus-mapping/src/evm/opcodes/error_oog_exp.rs | 38 ------------------- bus-mapping/src/evm/opcodes/error_oog_sha3.rs | 38 ------------------- .../evm/opcodes/error_oog_static_memory.rs | 36 ------------------ bus-mapping/src/evm/opcodes/error_simple.rs | 31 --------------- bus-mapping/src/evm/opcodes/stackonlyop.rs | 18 ++++++++- 6 files changed, 30 insertions(+), 161 deletions(-) delete mode 100644 bus-mapping/src/evm/opcodes/error_oog_exp.rs delete mode 100644 bus-mapping/src/evm/opcodes/error_oog_sha3.rs delete mode 100644 bus-mapping/src/evm/opcodes/error_oog_static_memory.rs delete mode 100644 bus-mapping/src/evm/opcodes/error_simple.rs diff --git a/bus-mapping/src/evm/opcodes.rs b/bus-mapping/src/evm/opcodes.rs index 4ab67438f7..9e9855bfce 100644 --- a/bus-mapping/src/evm/opcodes.rs +++ b/bus-mapping/src/evm/opcodes.rs @@ -62,15 +62,11 @@ mod error_invalid_creation_code; mod error_invalid_jump; mod error_oog_call; mod error_oog_dynamic_memory; -mod error_oog_exp; mod error_oog_log; mod error_oog_memory_copy; -mod error_oog_sha3; mod error_oog_sload_sstore; -mod error_oog_static_memory; mod error_precompile_failed; mod error_return_data_outofbound; -mod error_simple; mod error_write_protection; #[cfg(test)] @@ -96,15 +92,11 @@ use error_invalid_creation_code::ErrorCreationCode; use error_invalid_jump::InvalidJump; use error_oog_call::OOGCall; use error_oog_dynamic_memory::OOGDynamicMemory; -use error_oog_exp::OOGExp; use error_oog_log::ErrorOOGLog; use error_oog_memory_copy::OOGMemoryCopy; -use error_oog_sha3::OOGSha3; use error_oog_sload_sstore::OOGSloadSstore; -use error_oog_static_memory::OOGStaticMemory; use error_precompile_failed::PrecompileFailed; use error_return_data_outofbound::ErrorReturnDataOutOfBound; -use error_simple::ErrorSimple; use error_write_protection::ErrorWriteProtection; use exp::Exponentiation; use extcodecopy::Extcodecopy; @@ -287,28 +279,34 @@ fn fn_gen_error_state_associated_ops( ) -> Option { match error { ExecError::InvalidJump => Some(InvalidJump::gen_associated_ops), - ExecError::InvalidOpcode => Some(ErrorSimple::gen_associated_ops), + ExecError::InvalidOpcode => Some(StackOnlyOpcode::<0, 0, true>::gen_associated_ops), ExecError::Depth => { let op = geth_step.op; assert!(op.is_call()); Some(fn_gen_associated_ops(&op)) } ExecError::OutOfGas(OogError::Call) => Some(OOGCall::gen_associated_ops), - ExecError::OutOfGas(OogError::Constant) => Some(ErrorSimple::gen_associated_ops), + ExecError::OutOfGas(OogError::Constant) => { + Some(StackOnlyOpcode::<0, 0, true>::gen_associated_ops) + } ExecError::OutOfGas(OogError::Log) => Some(ErrorOOGLog::gen_associated_ops), ExecError::OutOfGas(OogError::DynamicMemoryExpansion) => { Some(OOGDynamicMemory::gen_associated_ops) } ExecError::OutOfGas(OogError::StaticMemoryExpansion) => { - Some(OOGStaticMemory::gen_associated_ops) + Some(StackOnlyOpcode::<1, 0, true>::gen_associated_ops) + } + ExecError::OutOfGas(OogError::Exp) => { + Some(StackOnlyOpcode::<2, 0, true>::gen_associated_ops) } - ExecError::OutOfGas(OogError::Exp) => Some(OOGExp::gen_associated_ops), ExecError::OutOfGas(OogError::MemoryCopy) => Some(OOGMemoryCopy::gen_associated_ops), - ExecError::OutOfGas(OogError::Sha3) => Some(OOGSha3::gen_associated_ops), + ExecError::OutOfGas(OogError::Sha3) => { + Some(StackOnlyOpcode::<2, 0, true>::gen_associated_ops) + } ExecError::OutOfGas(OogError::SloadSstore) => Some(OOGSloadSstore::gen_associated_ops), // ExecError:: - ExecError::StackOverflow => Some(ErrorSimple::gen_associated_ops), - ExecError::StackUnderflow => Some(ErrorSimple::gen_associated_ops), + ExecError::StackOverflow => Some(StackOnlyOpcode::<0, 0, true>::gen_associated_ops), + ExecError::StackUnderflow => Some(StackOnlyOpcode::<0, 0, true>::gen_associated_ops), ExecError::CodeStoreOutOfGas => Some(ErrorCodeStore::gen_associated_ops), ExecError::MaxCodeSizeExceeded => Some(ErrorCodeStore::gen_associated_ops), // call & callcode can encounter InsufficientBalance error, Use pop-7 generic CallOpcode @@ -331,7 +329,7 @@ fn fn_gen_error_state_associated_ops( OpcodeId::CREATE2 => Some(StackOnlyOpcode::<4, 1>::gen_associated_ops), _ => unreachable!(), }, - ExecError::GasUintOverflow => Some(ErrorSimple::gen_associated_ops), + ExecError::GasUintOverflow => Some(StackOnlyOpcode::<0, 0, true>::gen_associated_ops), ExecError::InvalidCreationCode => Some(ErrorCreationCode::gen_associated_ops), // more future errors place here _ => { diff --git a/bus-mapping/src/evm/opcodes/error_oog_exp.rs b/bus-mapping/src/evm/opcodes/error_oog_exp.rs deleted file mode 100644 index 657c2f9c6d..0000000000 --- a/bus-mapping/src/evm/opcodes/error_oog_exp.rs +++ /dev/null @@ -1,38 +0,0 @@ -use super::{Opcode, OpcodeId}; -use crate::{ - circuit_input_builder::{CircuitInputStateRef, ExecStep}, - error::{ExecError, OogError}, - Error, -}; -use eth_types::GethExecStep; - -/// Placeholder structure used to implement [`Opcode`] trait over it -/// corresponding to the [`OogError::Exp`](crate::error::OogError::Exp). -#[derive(Clone, Copy, Debug)] -pub(crate) struct OOGExp; - -impl Opcode for OOGExp { - fn gen_associated_ops( - state: &mut CircuitInputStateRef, - geth_steps: &[GethExecStep], - ) -> Result, Error> { - let geth_step = &geth_steps[0]; - debug_assert_eq!(geth_step.op, OpcodeId::EXP); - - let mut exec_step = state.new_step(geth_step)?; - exec_step.error = Some(ExecError::OutOfGas(OogError::Exp)); - - for i in 0..2 { - state.stack_read( - &mut exec_step, - geth_step.stack.nth_last_filled(i), - geth_step.stack.nth_last(i)?, - )?; - } - - state.gen_restore_context_ops(&mut exec_step, geth_steps)?; - state.handle_return(geth_step)?; - - Ok(vec![exec_step]) - } -} diff --git a/bus-mapping/src/evm/opcodes/error_oog_sha3.rs b/bus-mapping/src/evm/opcodes/error_oog_sha3.rs deleted file mode 100644 index cd70c6ae36..0000000000 --- a/bus-mapping/src/evm/opcodes/error_oog_sha3.rs +++ /dev/null @@ -1,38 +0,0 @@ -use crate::{ - circuit_input_builder::{CircuitInputStateRef, ExecStep}, - error::{ExecError, OogError}, - evm::Opcode, - Error, -}; -use eth_types::{evm_types::OpcodeId, GethExecStep}; - -/// Placeholder structure used to implement [`Opcode`] trait over it -/// corresponding to the [`OogError::Sha3`](crate::error::OogError::Sha3). -#[derive(Clone, Copy, Debug)] -pub(crate) struct OOGSha3; - -impl Opcode for OOGSha3 { - fn gen_associated_ops( - state: &mut CircuitInputStateRef, - geth_steps: &[GethExecStep], - ) -> Result, Error> { - let geth_step = &geth_steps[0]; - debug_assert_eq!(geth_step.op, OpcodeId::SHA3); - - let mut exec_step = state.new_step(geth_step)?; - exec_step.error = Some(ExecError::OutOfGas(OogError::Sha3)); - - for i in 0..2 { - state.stack_read( - &mut exec_step, - geth_step.stack.nth_last_filled(i), - geth_step.stack.nth_last(i)?, - )?; - } - - state.gen_restore_context_ops(&mut exec_step, geth_steps)?; - state.handle_return(geth_step)?; - - Ok(vec![exec_step]) - } -} diff --git a/bus-mapping/src/evm/opcodes/error_oog_static_memory.rs b/bus-mapping/src/evm/opcodes/error_oog_static_memory.rs deleted file mode 100644 index 96dbf173b4..0000000000 --- a/bus-mapping/src/evm/opcodes/error_oog_static_memory.rs +++ /dev/null @@ -1,36 +0,0 @@ -use crate::{ - circuit_input_builder::{CircuitInputStateRef, ExecStep}, - evm::Opcode, - Error, -}; -use eth_types::{evm_types::OpcodeId, GethExecStep}; - -#[derive(Clone, Copy, Debug)] -pub(crate) struct OOGStaticMemory {} - -impl Opcode for OOGStaticMemory { - fn gen_associated_ops( - state: &mut CircuitInputStateRef, - geth_steps: &[GethExecStep], - ) -> Result, Error> { - let geth_step = &geth_steps[0]; - debug_assert!( - [OpcodeId::MLOAD, OpcodeId::MSTORE, OpcodeId::MSTORE8].contains(&geth_step.op) - ); - - let mut exec_step = state.new_step(geth_step)?; - let next_step = geth_steps.get(1); - exec_step.error = state.get_step_err(geth_step, next_step).unwrap(); - - state.stack_read( - &mut exec_step, - geth_step.stack.last_filled(), - geth_step.stack.last()?, - )?; - - state.gen_restore_context_ops(&mut exec_step, geth_steps)?; - state.handle_return(geth_step)?; - - Ok(vec![exec_step]) - } -} diff --git a/bus-mapping/src/evm/opcodes/error_simple.rs b/bus-mapping/src/evm/opcodes/error_simple.rs deleted file mode 100644 index 1c1840d94b..0000000000 --- a/bus-mapping/src/evm/opcodes/error_simple.rs +++ /dev/null @@ -1,31 +0,0 @@ -use crate::{ - circuit_input_builder::{CircuitInputStateRef, ExecStep}, - evm::Opcode, - Error, -}; -use eth_types::GethExecStep; - -#[derive(Debug, Copy, Clone)] -pub(crate) struct ErrorSimple; - -// ErrorSimple is to deal with errors with general common ops as below -// - added error to current `ExecStep` -// - restore call context -// no extra ops e.g. stack read etc... -impl Opcode for ErrorSimple { - fn gen_associated_ops( - state: &mut CircuitInputStateRef, - geth_steps: &[GethExecStep], - ) -> Result, Error> { - let geth_step = &geth_steps[0]; - let mut exec_step = state.new_step(geth_step)?; - let next_step = geth_steps.get(1); - exec_step.error = state.get_step_err(geth_step, next_step).unwrap(); - - // handles all required steps - state.gen_restore_context_ops(&mut exec_step, geth_steps)?; - state.handle_return(geth_step)?; - - Ok(vec![exec_step]) - } -} diff --git a/bus-mapping/src/evm/opcodes/stackonlyop.rs b/bus-mapping/src/evm/opcodes/stackonlyop.rs index 21ec6bdcfe..c82d5ffeb0 100644 --- a/bus-mapping/src/evm/opcodes/stackonlyop.rs +++ b/bus-mapping/src/evm/opcodes/stackonlyop.rs @@ -12,9 +12,15 @@ use eth_types::GethExecStep; /// - N = 2: BinaryOpcode /// - N = 3: TernaryOpcode #[derive(Debug, Copy, Clone)] -pub(crate) struct StackOnlyOpcode; +pub(crate) struct StackOnlyOpcode< + const N_POP: usize, + const N_PUSH: usize, + const IS_ERR: bool = { false }, +>; -impl Opcode for StackOnlyOpcode { +impl Opcode + for StackOnlyOpcode +{ fn gen_associated_ops( state: &mut CircuitInputStateRef, geth_steps: &[GethExecStep], @@ -39,6 +45,14 @@ impl Opcode for StackOnlyOpcode