diff --git a/crates/primitives/src/constants.rs b/crates/primitives/src/constants.rs index c8223300bf..e2af7499de 100644 --- a/crates/primitives/src/constants.rs +++ b/crates/primitives/src/constants.rs @@ -6,7 +6,7 @@ use alloy_primitives::{address, Address}; pub const MAX_CODE_SIZE: usize = 0x6000; /// Number of block hashes that EVM can access in the past (pre-Prague). -pub const BLOCK_HASH_HISTORY: usize = 256; +pub const BLOCK_HASH_HISTORY: u64 = 256; /// EIP-2935: Serve historical block hashes from state /// diff --git a/crates/revm/src/context.rs b/crates/revm/src/context.rs index d715296b43..15ac574072 100644 --- a/crates/revm/src/context.rs +++ b/crates/revm/src/context.rs @@ -8,7 +8,7 @@ pub use context_precompiles::{ }; pub use evm_context::EvmContext; pub use inner_evm_context::InnerEvmContext; -use revm_interpreter::as_usize_saturated; +use revm_interpreter::as_u64_saturated; use crate::{ db::{Database, EmptyDB}, @@ -108,9 +108,8 @@ impl Host for Context { &mut self.evm.env } - fn block_hash(&mut self, number: u64) -> Option { - let block_number = as_usize_saturated!(self.env().block.number); - let requested_number = usize::try_from(number).unwrap_or(usize::MAX); + fn block_hash(&mut self, requested_number: u64) -> Option { + let block_number = as_u64_saturated!(self.env().block.number); let Some(diff) = block_number.checked_sub(requested_number) else { return Some(B256::ZERO); @@ -124,7 +123,7 @@ impl Host for Context { if diff <= BLOCK_HASH_HISTORY { return self .evm - .block_hash(number) + .block_hash(requested_number) .map_err(|e| self.evm.error = Err(e)) .ok(); } diff --git a/crates/revm/src/db/states/state.rs b/crates/revm/src/db/states/state.rs index fd584fcae3..9c3cc114e7 100644 --- a/crates/revm/src/db/states/state.rs +++ b/crates/revm/src/db/states/state.rs @@ -278,7 +278,7 @@ impl Database for State { let ret = *entry.insert(self.database.block_hash(number)?); // prune all hashes that are older then BLOCK_HASH_HISTORY - let last_block = number.saturating_sub(BLOCK_HASH_HISTORY as u64); + let last_block = number.saturating_sub(BLOCK_HASH_HISTORY); while let Some(entry) = self.block_hashes.first_entry() { if *entry.key() < last_block { entry.remove(); @@ -315,7 +315,7 @@ mod tests { state.block_hash(1u64).unwrap(); state.block_hash(2u64).unwrap(); - let test_number = BLOCK_HASH_HISTORY as u64 + 2; + let test_number = BLOCK_HASH_HISTORY + 2; let block1_hash = keccak256(U256::from(1).to_string().as_bytes()); let block2_hash = keccak256(U256::from(2).to_string().as_bytes());