Skip to content

Commit

Permalink
feat: adjust gas-costs for EIP-2935 BLOCKHASH (#1422)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes authored May 16, 2024
1 parent 4763c8b commit 6e81b88
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 37 deletions.
24 changes: 11 additions & 13 deletions crates/interpreter/src/instructions/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,16 @@ pub fn blockhash<H: Host + ?Sized, SPEC: Spec>(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 {
Expand All @@ -143,12 +142,11 @@ pub fn blockhash<H: Host + ?Sized, SPEC: Spec>(interpreter: &mut Interpreter, ho

pub fn sload<H: Host + ?Sized, SPEC: Spec>(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;
}

Expand Down
24 changes: 0 additions & 24 deletions crates/interpreter/src/instructions/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 6e81b88

Please sign in to comment.