Skip to content

Commit

Permalink
refactor: enhance readability (#968)
Browse files Browse the repository at this point in the history
  • Loading branch information
IaroslavMazur authored Jan 15, 2024
1 parent 7dce101 commit f5db653
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 31 deletions.
46 changes: 22 additions & 24 deletions crates/interpreter/src/instruction_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub enum InstructionResult {
// revert codes
Revert = 0x10, // revert opcode
CallTooDeep,
OutOfFund,
OutOfFunds,

// Actions
CallOrCreate = 0x20,
Expand Down Expand Up @@ -62,11 +62,13 @@ impl From<SuccessReason> for InstructionResult {
impl From<HaltReason> 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,
Expand All @@ -83,7 +85,7 @@ impl From<HaltReason> 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,
Expand All @@ -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
};
}

Expand Down Expand Up @@ -216,22 +218,18 @@ impl From<InstructionResult> 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)
Expand Down Expand Up @@ -293,7 +291,7 @@ mod tests {
let revert_results = vec![
InstructionResult::Revert,
InstructionResult::CallTooDeep,
InstructionResult::OutOfFund,
InstructionResult::OutOfFunds,
];

for result in revert_results {
Expand Down
2 changes: 1 addition & 1 deletion crates/interpreter/src/instructions/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
Host, InstructionResult, Interpreter,
};

pub fn wrapped_add<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
pub fn wrapping_add<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
gas!(interpreter, gas::VERYLOW);
pop_top!(interpreter, op1, op2);
*op2 = op1.wrapping_add(*op2);
Expand Down
2 changes: 1 addition & 1 deletion crates/interpreter/src/instructions/opcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions crates/primitives/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ pub enum HaltReason {
OverflowPayment,
StateChangeDuringStaticCall,
CallNotAllowedInsideStatic,
OutOfFund,
OutOfFunds,
CallTooDeep,

/* Optimism errors */
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions crates/revm/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ impl<DB: Database> EvmContext<DB> {

// 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
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion crates/revm/src/journaled_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit f5db653

Please sign in to comment.