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: implement EIP-2935 #1354

Merged
merged 9 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
23 changes: 19 additions & 4 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;
use revm_primitives::{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 @@ -103,14 +103,28 @@ pub fn extcodecopy<H: Host + ?Sized, SPEC: Spec>(interpreter: &mut Interpreter,
.set_data(memory_offset, code_offset, len, &code.original_bytes());
}

pub fn blockhash<H: Host + ?Sized>(interpreter: &mut Interpreter, host: &mut H) {
pub fn blockhash<H: Host + ?Sized, SPEC: Spec>(interpreter: &mut Interpreter, host: &mut H) {
gas!(interpreter, gas::BLOCKHASH);
pop_top!(interpreter, number);

if let Some(diff) = host.env().block.number.checked_sub(*number) {
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 diff <= BLOCK_HASH_HISTORY && diff != 0 {
if SPEC::enabled(PRAGUE) && diff <= HISTORY_SERVE_WINDOW && diff != 0 {
DaniPopes marked this conversation as resolved.
Show resolved Hide resolved
let Some((value, is_cold)) = host.sload(
HISTORY_STORAGE_ADDRESS,
number.wrapping_rem(U256::from(HISTORY_SERVE_WINDOW)),
) else {
interpreter.instruction_result = InstructionResult::FatalExternalError;
return;
};
gas!(interpreter, gas::sload_cost(SPEC::SPEC_ID, is_cold));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe extract the SLOAD logic from its function into a separate function where we also call here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yeah i had this in mind too, i just forgor

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

i tried making a func but mut borrow of interpreter from pop_top! prevented me, so I made an sload! macro instead

*number = value;
return;
} else if diff <= BLOCK_HASH_HISTORY && diff != 0 {
let Some(hash) = host.block_hash(*number) else {
interpreter.instruction_result = InstructionResult::FatalExternalError;
return;
Expand All @@ -119,6 +133,7 @@ pub fn blockhash<H: Host + ?Sized>(interpreter: &mut Interpreter, host: &mut H)
return;
}
}

*number = U256::ZERO;
}

Expand Down
2 changes: 1 addition & 1 deletion crates/interpreter/src/opcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ opcodes! {
0x3D => RETURNDATASIZE => system::returndatasize::<H, SPEC> => stack_io<0, 1>;
0x3E => RETURNDATACOPY => system::returndatacopy::<H, SPEC> => stack_io<3, 0>;
0x3F => EXTCODEHASH => host::extcodehash::<H, SPEC> => stack_io<1, 1>, not_eof;
0x40 => BLOCKHASH => host::blockhash => stack_io<1, 1>;
0x40 => BLOCKHASH => host::blockhash::<H, SPEC> => stack_io<1, 1>;
0x41 => COINBASE => host_env::coinbase => stack_io<0, 1>;
0x42 => TIMESTAMP => host_env::timestamp => stack_io<0, 1>;
0x43 => NUMBER => host_env::block_number => stack_io<0, 1>;
Expand Down
14 changes: 13 additions & 1 deletion crates/primitives/src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
use alloy_primitives::address;

use crate::Address;
onbjerg marked this conversation as resolved.
Show resolved Hide resolved

/// EIP-170: Contract code size limit
/// By default limit is 0x6000 (~25kb)
pub const MAX_CODE_SIZE: usize = 0x6000;

/// Number of block hashes that EVM can access in the past
/// Number of block hashes that EVM can access in the past (pre-Prague).
pub const BLOCK_HASH_HISTORY: usize = 256;

/// EIP-2935: Serve historical block hashes from state
///
/// Number of block hashes the EVM can access in the past (Prague).
pub const HISTORY_SERVE_WINDOW: usize = 8192;

/// EIP-2935: Serve historical block hashes from state
///
/// The address where historical blockhashes are available.
pub const HISTORY_STORAGE_ADDRESS: Address = address!("25a219378dad9b3503c8268c9ca836a52427a4fb");
onbjerg marked this conversation as resolved.
Show resolved Hide resolved

/// EIP-3860: Limit and meter initcode
///
/// Limit of maximum initcode size is 2 * MAX_CODE_SIZE
Expand Down
Loading