From 94b1c3c85d6bc2fd477c98b00a27fe9fe4d4e8b9 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Thu, 16 May 2024 16:36:01 +0300 Subject: [PATCH] feat: adjust gas-costs for EIP-2935 BLOCKHASH --- crates/interpreter/src/instructions/host.rs | 24 +++++++++---------- crates/interpreter/src/instructions/macros.rs | 24 ------------------- 2 files changed, 11 insertions(+), 37 deletions(-) diff --git a/crates/interpreter/src/instructions/host.rs b/crates/interpreter/src/instructions/host.rs index 43cba873b0..eea5dc20a5 100644 --- a/crates/interpreter/src/instructions/host.rs +++ b/crates/interpreter/src/instructions/host.rs @@ -110,17 +110,16 @@ pub fn blockhash(interpreter: &mut Interpreter, ho let block_number = host.env().block.number; match block_number.checked_sub(*number) { + // blockhash should push zero if number is same as current block number. Some(diff) if !diff.is_zero() => { let diff = as_usize_saturated!(diff); - // blockhash should push zero if number is same as current block number. if SPEC::enabled(PRAGUE) && diff <= BLOCKHASH_SERVE_WINDOW { - let value = sload!( - interpreter, - host, - BLOCKHASH_STORAGE_ADDRESS, - number.wrapping_rem(U256::from(BLOCKHASH_SERVE_WINDOW)) - ); + let index = number.wrapping_rem(U256::from(BLOCKHASH_SERVE_WINDOW)); + let Some((value, _)) = host.sload(BLOCKHASH_STORAGE_ADDRESS, index) else { + interpreter.instruction_result = InstructionResult::FatalExternalError; + return; + }; *number = value; return; } else if diff <= BLOCK_HASH_HISTORY { @@ -143,12 +142,11 @@ pub fn blockhash(interpreter: &mut Interpreter, ho pub fn sload(interpreter: &mut Interpreter, host: &mut H) { pop_top!(interpreter, index); - let value = sload!( - interpreter, - host, - interpreter.contract.target_address, - *index - ); + let Some((value, is_cold)) = host.sload(interpreter.contract.target_address, *index) else { + interpreter.instruction_result = InstructionResult::FatalExternalError; + return; + }; + gas!(interpreter, gas::sload_cost(SPEC::SPEC_ID, is_cold)); *index = value; } diff --git a/crates/interpreter/src/instructions/macros.rs b/crates/interpreter/src/instructions/macros.rs index a4ee4f4b3e..2592956ec7 100644 --- a/crates/interpreter/src/instructions/macros.rs +++ b/crates/interpreter/src/instructions/macros.rs @@ -45,30 +45,6 @@ macro_rules! check { }; } -/// Performs an `SLOAD` on the target account and storage index. -/// -/// If the slot could not be loaded, or if the gas cost could not be charged, the expanded code -/// sets the instruction result and returns accordingly. -/// -/// # Note -/// -/// This macro charges gas. -/// -/// # Returns -/// -/// Expands to the value of the storage slot. -#[macro_export] -macro_rules! sload { - ($interp:expr, $host:expr, $address:expr, $index:expr) => {{ - let Some((value, is_cold)) = $host.sload($address, $index) else { - $interp.instruction_result = $crate::InstructionResult::FatalExternalError; - return; - }; - $crate::gas!($interp, $crate::gas::sload_cost(SPEC::SPEC_ID, is_cold)); - value - }}; -} - /// Records a `gas` cost and fails the instruction if it would exceed the available gas. #[macro_export] macro_rules! gas {