diff --git a/crates/cheatcodes/src/evm.rs b/crates/cheatcodes/src/evm.rs index ca8495a3a27e..5000df928f2c 100644 --- a/crates/cheatcodes/src/evm.rs +++ b/crates/cheatcodes/src/evm.rs @@ -569,7 +569,16 @@ impl Cheatcode for stopAndReturnStateDiffCall { impl Cheatcode for setBlockhashCall { fn apply_full(&self, ccx: &mut CheatsCtxt) -> Result { - todo!() + let Self { blockNumber, blockHash } = self; + let block_number: U256 = *blockNumber; + ensure!( + block_number <= ccx.ecx.env.block.number, + "block number must be less than or equal to the current block number" + ); + + ccx.ecx.db.set_blockhash(*blockNumber, *blockHash); + + Ok(Default::default()) } } diff --git a/crates/evm/core/src/backend/cow.rs b/crates/evm/core/src/backend/cow.rs index 9aa3ce0eb304..236909cfdb0a 100644 --- a/crates/evm/core/src/backend/cow.rs +++ b/crates/evm/core/src/backend/cow.rs @@ -126,7 +126,7 @@ impl<'a> DatabaseExt for CowBackend<'a> { fn delete_snapshot(&mut self, id: U256) -> bool { // delete snapshot requires a previous snapshot to be initialized if let Some(backend) = self.initialized_backend_mut() { - return backend.delete_snapshot(id); + return backend.delete_snapshot(id) } false } @@ -245,13 +245,13 @@ impl<'a> DatabaseExt for CowBackend<'a> { self.backend.has_cheatcode_access(account) } - fn set_blockhash(&mut self, block_number: B256, block_hash: B256) { + fn set_blockhash(&mut self, block_number: U256, block_hash: B256) { self.backend .to_mut() .mem_db() .to_owned() .block_hashes - .insert(block_number.into(), block_hash); + .insert(block_number, block_hash); } } diff --git a/crates/evm/core/src/backend/mod.rs b/crates/evm/core/src/backend/mod.rs index 00a26d2fe86f..4dc11edd8f21 100644 --- a/crates/evm/core/src/backend/mod.rs +++ b/crates/evm/core/src/backend/mod.rs @@ -8,7 +8,7 @@ use crate::{ InspectorExt, }; use alloy_genesis::GenesisAccount; -use alloy_primitives::{keccak256, uint, Address, B256, B64, U256, U64}; +use alloy_primitives::{keccak256, uint, Address, B256, U256}; use alloy_rpc_types::{Block, BlockNumberOrTag, BlockTransactions, Transaction}; use alloy_serde::WithOtherFields; use eyre::Context; @@ -332,7 +332,7 @@ pub trait DatabaseExt: Database { } /// set the blockhash for the given block number - fn set_blockhash(&mut self, block_number: B256, block_hash: B256); + fn set_blockhash(&mut self, block_number: U256, block_hash: B256); } struct _ObjectSafe(dyn DatabaseExt); @@ -858,7 +858,7 @@ impl Backend { if tx.hash == tx_hash { // found the target transaction - return Ok(Some(tx)); + return Ok(Some(tx)) } trace!(tx=?tx.hash, "committing transaction"); @@ -1374,8 +1374,8 @@ impl DatabaseExt for Backend { self.inner.cheatcode_access_accounts.contains(account) } - fn set_blockhash(&mut self, block_number: B256, block_hash: B256) { - self.mem_db.block_hashes.insert(block_number.into(), block_hash); + fn set_blockhash(&mut self, block_number: U256, block_hash: B256) { + self.mem_db.block_hashes.insert(block_number, block_hash); } }