diff --git a/crates/interpreter/src/instructions/host.rs b/crates/interpreter/src/instructions/host.rs index 416efaa8da..b6378b37d4 100644 --- a/crates/interpreter/src/instructions/host.rs +++ b/crates/interpreter/src/instructions/host.rs @@ -13,12 +13,10 @@ use revm_primitives::BLOCK_HASH_HISTORY; pub fn balance(interpreter: &mut Interpreter, host: &mut dyn Host) { pop_address!(interpreter, address); - let ret = host.balance(address); - if ret.is_none() { + let Some((balance, is_cold)) = host.balance(address) else { interpreter.instruction_result = InstructionResult::FatalExternalError; return; - } - let (balance, is_cold) = ret.unwrap(); + }; gas!( interpreter, if SPEC::enabled(ISTANBUL) { @@ -37,23 +35,19 @@ pub fn selfbalance(interpreter: &mut Interpreter, host: &mut dyn Hos // EIP-1884: Repricing for trie-size-dependent opcodes check!(interpreter, SPEC::enabled(ISTANBUL)); gas!(interpreter, gas::LOW); - let ret = host.balance(interpreter.contract.address); - if ret.is_none() { + let Some((balance, _)) = host.balance(interpreter.contract.address) else { interpreter.instruction_result = InstructionResult::FatalExternalError; return; - } - let (balance, _) = ret.unwrap(); + }; push!(interpreter, balance); } pub fn extcodesize(interpreter: &mut Interpreter, host: &mut dyn Host) { pop_address!(interpreter, address); - let ret = host.code(address); - if ret.is_none() { + let Some((code, is_cold)) = host.code(address) else { interpreter.instruction_result = InstructionResult::FatalExternalError; return; - } - let (code, is_cold) = ret.unwrap(); + }; if SPEC::enabled(BERLIN) { gas!( interpreter, @@ -75,12 +69,10 @@ pub fn extcodesize(interpreter: &mut Interpreter, host: &mut dyn Hos pub fn extcodehash(interpreter: &mut Interpreter, host: &mut dyn Host) { check!(interpreter, SPEC::enabled(CONSTANTINOPLE)); // EIP-1052: EXTCODEHASH opcode pop_address!(interpreter, address); - let ret = host.code_hash(address); - if ret.is_none() { + let Some((code_hash, is_cold)) = host.code_hash(address) else { interpreter.instruction_result = InstructionResult::FatalExternalError; return; - } - let (code_hash, is_cold) = ret.unwrap(); + }; if SPEC::enabled(BERLIN) { gas!( interpreter, @@ -102,12 +94,10 @@ pub fn extcodecopy(interpreter: &mut Interpreter, host: &mut dyn Hos pop_address!(interpreter, address); pop!(interpreter, memory_offset, code_offset, len_u256); - let ret = host.code(address); - if ret.is_none() { + let Some((code, is_cold)) = host.code(address) else { interpreter.instruction_result = InstructionResult::FatalExternalError; return; - } - let (code, is_cold) = ret.unwrap(); + }; let len = as_usize_or_fail!(interpreter, len_u256, InstructionResult::InvalidOperandOOG); gas_or_fail!( @@ -139,12 +129,11 @@ pub fn blockhash(interpreter: &mut Interpreter, host: &mut dyn Host) { let diff = as_usize_saturated!(diff); // blockhash should push zero if number is same as current block number. if diff <= BLOCK_HASH_HISTORY && diff != 0 { - let ret = host.block_hash(*number); - if ret.is_none() { + let Some(hash) = host.block_hash(*number) else { interpreter.instruction_result = InstructionResult::FatalExternalError; return; - } - *number = U256::from_be_bytes(*ret.unwrap()); + }; + *number = U256::from_be_bytes(hash.0); return; } } @@ -154,12 +143,10 @@ pub fn blockhash(interpreter: &mut Interpreter, host: &mut dyn Host) { pub fn sload(interpreter: &mut Interpreter, host: &mut dyn Host) { pop!(interpreter, index); - let ret = host.sload(interpreter.contract.address, index); - if ret.is_none() { + let Some((value, is_cold)) = host.sload(interpreter.contract.address, index) else { interpreter.instruction_result = InstructionResult::FatalExternalError; return; - } - let (value, is_cold) = ret.unwrap(); + }; gas!(interpreter, gas::sload_cost::(is_cold)); push!(interpreter, value); } @@ -168,12 +155,12 @@ pub fn sstore(interpreter: &mut Interpreter, host: &mut dyn Host) { check_staticcall!(interpreter); pop!(interpreter, index, value); - let ret = host.sstore(interpreter.contract.address, index, value); - if ret.is_none() { + let Some((original, old, new, is_cold)) = + host.sstore(interpreter.contract.address, index, value) + else { interpreter.instruction_result = InstructionResult::FatalExternalError; return; - } - let (original, old, new, is_cold) = ret.unwrap(); + }; gas_or_fail!(interpreter, { let remaining_gas = interpreter.gas.remaining(); gas::sstore_cost::(original, old, new, remaining_gas, is_cold) @@ -238,12 +225,10 @@ pub fn selfdestruct(interpreter: &mut Interpreter, host: &mut dyn Ho check_staticcall!(interpreter); pop_address!(interpreter, target); - let res = host.selfdestruct(interpreter.contract.address, target); - if res.is_none() { + let Some(res) = host.selfdestruct(interpreter.contract.address, target) else { interpreter.instruction_result = InstructionResult::FatalExternalError; return; - } - let res = res.unwrap(); + }; // EIP-3529: Reduction in refunds if !SPEC::enabled(LONDON) && !res.previously_destroyed { @@ -482,12 +467,10 @@ fn prepare_call_inputs( }; // load account and calculate gas cost. - let res = host.load_account(to); - if res.is_none() { + let Some((is_cold, exist)) = host.load_account(to) else { interpreter.instruction_result = InstructionResult::FatalExternalError; return; - } - let (is_cold, exist) = res.unwrap(); + }; let is_new = !exist; gas!( diff --git a/crates/interpreter/src/instructions/macros.rs b/crates/interpreter/src/instructions/macros.rs index a0da29b7b3..3c06e02ca1 100644 --- a/crates/interpreter/src/instructions/macros.rs +++ b/crates/interpreter/src/instructions/macros.rs @@ -1,5 +1,3 @@ -pub use crate::InstructionResult; - macro_rules! check_staticcall { ($interp:expr) => { if $interp.is_static { @@ -21,7 +19,7 @@ macro_rules! check { macro_rules! gas { ($interp:expr, $gas:expr) => { if crate::USE_GAS { - if !$interp.gas.record_cost(($gas)) { + if !$interp.gas.record_cost($gas) { $interp.instruction_result = InstructionResult::OutOfGas; return; } @@ -30,11 +28,11 @@ macro_rules! gas { } macro_rules! refund { - ($interp:expr, $gas:expr) => {{ + ($interp:expr, $gas:expr) => { if crate::USE_GAS { - $interp.gas.gas_refund($gas); + $interp.gas.record_refund($gas); } - }}; + }; } macro_rules! gas_or_fail { @@ -52,11 +50,9 @@ macro_rules! gas_or_fail { } macro_rules! memory_resize { - ($interp:expr, $offset:expr, $len:expr) => {{ - let len: usize = $len; - let offset: usize = $offset; + ($interp:expr, $offset:expr, $len:expr) => { if let Some(new_size) = - crate::interpreter::memory::next_multiple_of_32(offset.saturating_add(len)) + crate::interpreter::memory::next_multiple_of_32($offset.saturating_add($len)) { #[cfg(feature = "memory_limit")] if new_size > ($interp.memory_limit as usize) { @@ -78,7 +74,7 @@ macro_rules! memory_resize { $interp.instruction_result = InstructionResult::MemoryOOG; return; } - }}; + }; } macro_rules! pop_address { @@ -102,7 +98,7 @@ macro_rules! pop_address { } macro_rules! pop { - ( $interp:expr, $x1:ident) => { + ($interp:expr, $x1:ident) => { if $interp.stack.len() < 1 { $interp.instruction_result = InstructionResult::StackUnderflow; return; @@ -110,7 +106,7 @@ macro_rules! pop { // Safety: Length is checked above. let $x1 = unsafe { $interp.stack.pop_unsafe() }; }; - ( $interp:expr, $x1:ident, $x2:ident) => { + ($interp:expr, $x1:ident, $x2:ident) => { if $interp.stack.len() < 2 { $interp.instruction_result = InstructionResult::StackUnderflow; return; @@ -118,7 +114,7 @@ macro_rules! pop { // Safety: Length is checked above. let ($x1, $x2) = unsafe { $interp.stack.pop2_unsafe() }; }; - ( $interp:expr, $x1:ident, $x2:ident, $x3:ident) => { + ($interp:expr, $x1:ident, $x2:ident, $x3:ident) => { if $interp.stack.len() < 3 { $interp.instruction_result = InstructionResult::StackUnderflow; return; @@ -127,7 +123,7 @@ macro_rules! pop { let ($x1, $x2, $x3) = unsafe { $interp.stack.pop3_unsafe() }; }; - ( $interp:expr, $x1:ident, $x2:ident, $x3:ident, $x4:ident) => { + ($interp:expr, $x1:ident, $x2:ident, $x3:ident, $x4:ident) => { if $interp.stack.len() < 4 { $interp.instruction_result = InstructionResult::StackUnderflow; return; @@ -138,7 +134,7 @@ macro_rules! pop { } macro_rules! pop_top { - ( $interp:expr, $x1:ident) => { + ($interp:expr, $x1:ident) => { if $interp.stack.len() < 1 { $interp.instruction_result = InstructionResult::StackUnderflow; return; @@ -146,7 +142,7 @@ macro_rules! pop_top { // Safety: Length is checked above. let $x1 = unsafe { $interp.stack.top_unsafe() }; }; - ( $interp:expr, $x1:ident, $x2:ident) => { + ($interp:expr, $x1:ident, $x2:ident) => { if $interp.stack.len() < 2 { $interp.instruction_result = InstructionResult::StackUnderflow; return; @@ -154,7 +150,7 @@ macro_rules! pop_top { // Safety: Length is checked above. let ($x1, $x2) = unsafe { $interp.stack.pop_top_unsafe() }; }; - ( $interp:expr, $x1:ident, $x2:ident, $x3:ident) => { + ($interp:expr, $x1:ident, $x2:ident, $x3:ident) => { if $interp.stack.len() < 3 { $interp.instruction_result = InstructionResult::StackUnderflow; return; @@ -165,59 +161,57 @@ macro_rules! pop_top { } macro_rules! push_b256 { - ( $interp:expr, $( $x:expr ),* ) => ( - $( - match $interp.stack.push_b256($x) { - Ok(()) => (), - Err(e) => { - $interp.instruction_result = e; - return - }, - } - )* - ) + ($interp:expr, $($x:expr),* $(,)?) => ($( + match $interp.stack.push_b256($x) { + Ok(()) => {}, + Err(e) => { + $interp.instruction_result = e; + return; + }, + } + )*) } macro_rules! push { - ( $interp:expr, $( $x:expr ),* ) => ( - $( - match $interp.stack.push($x) { - Ok(()) => (), - Err(e) => { $interp.instruction_result = e; - return - } , - } - )* - ) + ($interp:expr, $($x:expr),* $(,)?) => ($( + match $interp.stack.push($x) { + Ok(()) => {}, + Err(e) => { + $interp.instruction_result = e; + return; + } + } + )*) } macro_rules! as_u64_saturated { - ( $v:expr ) => {{ - if $v.as_limbs()[1] != 0 || $v.as_limbs()[2] != 0 || $v.as_limbs()[3] != 0 { - u64::MAX + ($v:expr) => {{ + let x: &[u64; 4] = $v.as_limbs(); + if x[1] == 0 && x[2] == 0 && x[3] == 0 { + x[0] } else { - $v.as_limbs()[0] + u64::MAX } }}; } macro_rules! as_usize_saturated { - ( $v:expr ) => {{ + ($v:expr) => { as_u64_saturated!($v) as usize - }}; + }; } macro_rules! as_usize_or_fail { - ( $interp:expr, $v:expr ) => {{ + ($interp:expr, $v:expr) => { as_usize_or_fail!($interp, $v, InstructionResult::InvalidOperandOOG) - }}; + }; - ( $interp:expr, $v:expr, $reason:expr ) => {{ - if $v.as_limbs()[1] != 0 || $v.as_limbs()[2] != 0 || $v.as_limbs()[3] != 0 { + ($interp:expr, $v:expr, $reason:expr) => {{ + let x = $v.as_limbs(); + if x[1] != 0 || x[2] != 0 || x[3] != 0 { $interp.instruction_result = $reason; return; } - - $v.as_limbs()[0] as usize + x[0] as usize }}; }