Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
refactor: sload! macro
  • Loading branch information
onbjerg committed Apr 24, 2024
commit 74cea763ec57ca3e0dbfc7773bcea1ffcef8c957
57 changes: 31 additions & 26 deletions crates/interpreter/src/instructions/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
Host, InstructionResult, SStoreResult,
};
use core::cmp::min;
use revm_primitives::{BLOCK_HASH_HISTORY, HISTORY_SERVE_WINDOW, HISTORY_STORAGE_ADDRESS};
use revm_primitives::{Address, BLOCK_HASH_HISTORY, HISTORY_SERVE_WINDOW, HISTORY_STORAGE_ADDRESS};
use std::vec::Vec;

pub fn balance<H: Host + ?Sized, SPEC: Spec>(interpreter: &mut Interpreter, host: &mut H) {
Expand Down Expand Up @@ -109,28 +109,32 @@ pub fn blockhash<H: Host + ?Sized, SPEC: Spec>(interpreter: &mut Interpreter, ho

let block_number = host.env().block.number;

if let Some(diff) = block_number.checked_sub(*number) {
let diff = as_usize_saturated!(diff);

// blockhash should push zero if number is same as current block number.
if SPEC::enabled(PRAGUE) && diff <= HISTORY_SERVE_WINDOW && diff != 0 {
let Some((value, is_cold)) = host.sload(
HISTORY_STORAGE_ADDRESS,
number.wrapping_rem(U256::from(HISTORY_SERVE_WINDOW)),
) else {
interpreter.instruction_result = InstructionResult::FatalExternalError;
match block_number.checked_sub(*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 <= HISTORY_SERVE_WINDOW {
let value = sload!(
interpreter,
host,
HISTORY_STORAGE_ADDRESS,
number.wrapping_rem(U256::from(HISTORY_SERVE_WINDOW))
);
*number = value;
return;
};
gas!(interpreter, gas::sload_cost(SPEC::SPEC_ID, is_cold));
*number = value;
return;
} else if diff <= BLOCK_HASH_HISTORY && diff != 0 {
let Some(hash) = host.block_hash(*number) else {
interpreter.instruction_result = InstructionResult::FatalExternalError;
} else if diff <= BLOCK_HASH_HISTORY {
let Some(hash) = host.block_hash(*number) else {
interpreter.instruction_result = InstructionResult::FatalExternalError;
return;
};
*number = U256::from_be_bytes(hash.0);
return;
};
*number = U256::from_be_bytes(hash.0);
return;
}
}
_ => {
// If blockhash is requested for the current block, the hash should be 0, so we fall
// through.
}
}

Expand All @@ -139,11 +143,12 @@ 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 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));
let value = sload!(
interpreter,
host,
interpreter.contract.target_address,
*index
);
*index = value;
}

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