Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adjust gas-costs for EIP-2935 BLOCKHASH #1422

Merged
merged 1 commit into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this okay, it will warm load the storage?

*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
Loading