Skip to content

Commit

Permalink
chore: cast block number to u64 and not usize (#1727)
Browse files Browse the repository at this point in the history
* fix: cast block number to u64 and not usize

* clippy
  • Loading branch information
rakita authored Aug 28, 2024
1 parent c75e095 commit 0ae01d5
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 8 deletions.
2 changes: 1 addition & 1 deletion crates/primitives/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
Expand Down
9 changes: 4 additions & 5 deletions crates/revm/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -108,9 +108,8 @@ impl<EXT, DB: Database> Host for Context<EXT, DB> {
&mut self.evm.env
}

fn block_hash(&mut self, number: u64) -> Option<B256> {
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<B256> {
let block_number = as_u64_saturated!(self.env().block.number);

let Some(diff) = block_number.checked_sub(requested_number) else {
return Some(B256::ZERO);
Expand All @@ -124,7 +123,7 @@ impl<EXT, DB: Database> Host for Context<EXT, DB> {
if diff <= BLOCK_HASH_HISTORY {
return self
.evm
.block_hash(number)
.block_hash(requested_number)
.map_err(|e| self.evm.error = Err(e))
.ok();
}
Expand Down
4 changes: 2 additions & 2 deletions crates/revm/src/db/states/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ impl<DB: Database> Database for State<DB> {
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();
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit 0ae01d5

Please sign in to comment.