From 91538667d8f03aa9c425ccf8460ed809d7b2f75d Mon Sep 17 00:00:00 2001 From: Iaroslav Mazur Date: Wed, 10 Jan 2024 19:27:58 +0200 Subject: [PATCH] refactor: enhance readability --- crates/interpreter/src/instruction_result.rs | 46 +++++++++---------- .../src/instructions/arithmetic.rs | 2 +- crates/interpreter/src/instructions/opcode.rs | 2 +- crates/primitives/src/result.rs | 4 +- crates/revm/src/context.rs | 4 +- crates/revm/src/journaled_state.rs | 2 +- 6 files changed, 29 insertions(+), 31 deletions(-) diff --git a/crates/interpreter/src/instruction_result.rs b/crates/interpreter/src/instruction_result.rs index 4833bd6f4f..8d79eed668 100644 --- a/crates/interpreter/src/instruction_result.rs +++ b/crates/interpreter/src/instruction_result.rs @@ -14,7 +14,7 @@ pub enum InstructionResult { // revert codes Revert = 0x10, // revert opcode CallTooDeep, - OutOfFund, + OutOfFunds, // Actions CallOrCreate = 0x20, @@ -62,11 +62,13 @@ impl From for InstructionResult { impl From for InstructionResult { fn from(value: HaltReason) -> Self { match value { - HaltReason::OutOfGas(OutOfGasError::BasicOutOfGas) => Self::OutOfGas, - HaltReason::OutOfGas(OutOfGasError::InvalidOperand) => Self::InvalidOperandOOG, - HaltReason::OutOfGas(OutOfGasError::Memory) => Self::MemoryOOG, - HaltReason::OutOfGas(OutOfGasError::MemoryLimit) => Self::MemoryLimitOOG, - HaltReason::OutOfGas(OutOfGasError::Precompile) => Self::PrecompileOOG, + HaltReason::OutOfGas(error) => match error { + OutOfGasError::Basic => Self::OutOfGas, + OutOfGasError::InvalidOperand => Self::InvalidOperandOOG, + OutOfGasError::Memory => Self::MemoryOOG, + OutOfGasError::MemoryLimit => Self::MemoryLimitOOG, + OutOfGasError::Precompile => Self::PrecompileOOG, + }, HaltReason::OpcodeNotFound => Self::OpcodeNotFound, HaltReason::InvalidFEOpcode => Self::InvalidFEOpcode, HaltReason::InvalidJump => Self::InvalidJump, @@ -83,7 +85,7 @@ impl From for InstructionResult { HaltReason::OverflowPayment => Self::OverflowPayment, HaltReason::StateChangeDuringStaticCall => Self::StateChangeDuringStaticCall, HaltReason::CallNotAllowedInsideStatic => Self::CallNotAllowedInsideStatic, - HaltReason::OutOfFund => Self::OutOfFund, + HaltReason::OutOfFunds => Self::OutOfFunds, HaltReason::CallTooDeep => Self::CallTooDeep, #[cfg(feature = "optimism")] HaltReason::FailedDeposit => Self::FatalExternalError, @@ -104,7 +106,7 @@ macro_rules! return_ok { #[macro_export] macro_rules! return_revert { () => { - InstructionResult::Revert | InstructionResult::CallTooDeep | InstructionResult::OutOfFund + InstructionResult::Revert | InstructionResult::CallTooDeep | InstructionResult::OutOfFunds }; } @@ -216,22 +218,18 @@ impl From for SuccessOrHalt { InstructionResult::Revert => Self::Revert, InstructionResult::CallOrCreate => Self::InternalCallOrCreate, // used only in interpreter loop InstructionResult::CallTooDeep => Self::Halt(HaltReason::CallTooDeep), // not gonna happen for first call - InstructionResult::OutOfFund => Self::Halt(HaltReason::OutOfFund), // Check for first call is done separately. - InstructionResult::OutOfGas => Self::Halt(HaltReason::OutOfGas( - revm_primitives::OutOfGasError::BasicOutOfGas, - )), - InstructionResult::MemoryLimitOOG => Self::Halt(HaltReason::OutOfGas( - revm_primitives::OutOfGasError::MemoryLimit, - )), - InstructionResult::MemoryOOG => { - Self::Halt(HaltReason::OutOfGas(revm_primitives::OutOfGasError::Memory)) + InstructionResult::OutOfFunds => Self::Halt(HaltReason::OutOfFunds), // Check for first call is done separately. + InstructionResult::OutOfGas => Self::Halt(HaltReason::OutOfGas(OutOfGasError::Basic)), + InstructionResult::MemoryLimitOOG => { + Self::Halt(HaltReason::OutOfGas(OutOfGasError::MemoryLimit)) + } + InstructionResult::MemoryOOG => Self::Halt(HaltReason::OutOfGas(OutOfGasError::Memory)), + InstructionResult::PrecompileOOG => { + Self::Halt(HaltReason::OutOfGas(OutOfGasError::Precompile)) + } + InstructionResult::InvalidOperandOOG => { + Self::Halt(HaltReason::OutOfGas(OutOfGasError::InvalidOperand)) } - InstructionResult::PrecompileOOG => Self::Halt(HaltReason::OutOfGas( - revm_primitives::OutOfGasError::Precompile, - )), - InstructionResult::InvalidOperandOOG => Self::Halt(HaltReason::OutOfGas( - revm_primitives::OutOfGasError::InvalidOperand, - )), InstructionResult::OpcodeNotFound => Self::Halt(HaltReason::OpcodeNotFound), InstructionResult::CallNotAllowedInsideStatic => { Self::Halt(HaltReason::CallNotAllowedInsideStatic) @@ -293,7 +291,7 @@ mod tests { let revert_results = vec![ InstructionResult::Revert, InstructionResult::CallTooDeep, - InstructionResult::OutOfFund, + InstructionResult::OutOfFunds, ]; for result in revert_results { diff --git a/crates/interpreter/src/instructions/arithmetic.rs b/crates/interpreter/src/instructions/arithmetic.rs index e0700ee3f3..724f5ff9c3 100644 --- a/crates/interpreter/src/instructions/arithmetic.rs +++ b/crates/interpreter/src/instructions/arithmetic.rs @@ -5,7 +5,7 @@ use crate::{ Host, InstructionResult, Interpreter, }; -pub fn wrapped_add(interpreter: &mut Interpreter, _host: &mut H) { +pub fn wrapping_add(interpreter: &mut Interpreter, _host: &mut H) { gas!(interpreter, gas::VERYLOW); pop_top!(interpreter, op1, op2); *op2 = op1.wrapping_add(*op2); diff --git a/crates/interpreter/src/instructions/opcode.rs b/crates/interpreter/src/instructions/opcode.rs index f6265d122b..116353803c 100644 --- a/crates/interpreter/src/instructions/opcode.rs +++ b/crates/interpreter/src/instructions/opcode.rs @@ -94,7 +94,7 @@ where opcodes! { 0x00 => STOP => control::stop, - 0x01 => ADD => arithmetic::wrapped_add, + 0x01 => ADD => arithmetic::wrapping_add, 0x02 => MUL => arithmetic::wrapping_mul, 0x03 => SUB => arithmetic::wrapping_sub, 0x04 => DIV => arithmetic::div, diff --git a/crates/primitives/src/result.rs b/crates/primitives/src/result.rs index 666cfca11a..c91c9052eb 100644 --- a/crates/primitives/src/result.rs +++ b/crates/primitives/src/result.rs @@ -388,7 +388,7 @@ pub enum HaltReason { OverflowPayment, StateChangeDuringStaticCall, CallNotAllowedInsideStatic, - OutOfFund, + OutOfFunds, CallTooDeep, /* Optimism errors */ @@ -400,7 +400,7 @@ pub enum HaltReason { #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub enum OutOfGasError { // Basic OOG error - BasicOutOfGas, + Basic, // Tried to expand past REVM limit MemoryLimit, // Basic OOG error from memory expansion diff --git a/crates/revm/src/context.rs b/crates/revm/src/context.rs index 52a3e97abe..b0c07472c7 100644 --- a/crates/revm/src/context.rs +++ b/crates/revm/src/context.rs @@ -230,7 +230,7 @@ impl EvmContext { // Check if caller has enough balance to send to the created contract. if caller_balance < inputs.value { - return return_error(InstructionResult::OutOfFund); + return return_error(InstructionResult::OutOfFunds); } // Increase nonce of caller and check if it overflows @@ -631,7 +631,7 @@ mod tests { let FrameOrResult::Result(err) = res else { panic!("Expected FrameOrResult::Result"); }; - assert_eq!(err.result, InstructionResult::OutOfFund); + assert_eq!(err.result, InstructionResult::OutOfFunds); let checkpointed = vec![vec![JournalEntry::AccountLoaded { address: contract }]]; assert_eq!(evm_context.journaled_state.journal, checkpointed); assert_eq!(evm_context.journaled_state.depth, 0); diff --git a/crates/revm/src/journaled_state.rs b/crates/revm/src/journaled_state.rs index 8aa4d51fd5..185b5c00c2 100644 --- a/crates/revm/src/journaled_state.rs +++ b/crates/revm/src/journaled_state.rs @@ -176,7 +176,7 @@ impl JournaledState { let from_balance = &mut from_account.info.balance; *from_balance = from_balance .checked_sub(balance) - .ok_or(InstructionResult::OutOfFund)?; + .ok_or(InstructionResult::OutOfFunds)?; // add balance to let to_account = &mut self.state.get_mut(to).unwrap();