Skip to content

Commit

Permalink
adjust signature and add implementation for cheatcode
Browse files Browse the repository at this point in the history
  • Loading branch information
TropicalDog17 committed Jun 26, 2024
1 parent c436cef commit 336a4a5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
11 changes: 10 additions & 1 deletion crates/cheatcodes/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,16 @@ impl Cheatcode for stopAndReturnStateDiffCall {

impl Cheatcode for setBlockhashCall {
fn apply_full<DB: DatabaseExt>(&self, ccx: &mut CheatsCtxt<DB>) -> 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())
}
}

Expand Down
6 changes: 3 additions & 3 deletions crates/evm/core/src/backend/cow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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);
}
}

Expand Down
10 changes: 5 additions & 5 deletions crates/evm/core/src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -332,7 +332,7 @@ pub trait DatabaseExt: Database<Error = DatabaseError> {
}

/// 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);
Expand Down Expand Up @@ -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");

Expand Down Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 336a4a5

Please sign in to comment.