-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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(cheatcodes): add vm.setBlockhash #8258
feat(cheatcodes): add vm.setBlockhash #8258
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cool, a few more nits.
I'd also appreciate if you could rollback the additional ;
hi @mattsse @DaniPopes, could you guide me how to run the integration test. when I run
|
f699397
to
336a4a5
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
last nit to make the test pass
Sorry this is my first time contributing to Foundry, I haven't setup my formatter properly which cause ci fails. Thank you for your patience. |
11ea9fb
to
5d3da00
Compare
@TropicalDog17 No problem, thanks for contributing! Could you please enable "allow edits from maintainers" on the right side bar? |
Hey @DaniPopes unfortunately this PR is coming from an organization-owned fork, which as I know doesn't have the allow edits from maintainers option. Could you provide the needed changes and I will update the PR asap. Thanks! |
@TropicalDog17 could you please rebase/merge master? |
Here's the patch I wanted to apply. This should fix the remaining tests. diff --git a/crates/cheatcodes/src/evm.rs b/crates/cheatcodes/src/evm.rs
index b005ac54ab..bb6867fb39 100644
--- a/crates/cheatcodes/src/evm.rs
+++ b/crates/cheatcodes/src/evm.rs
@@ -569,15 +569,12 @@ impl Cheatcode for stopAndReturnStateDiffCall {
impl Cheatcode for setBlockhashCall {
fn apply_stateful<DB: DatabaseExt>(&self, ccx: &mut CheatsCtxt<DB>) -> Result {
- let Self { blockNumber, blockHash } = self;
- let block_number: U256 = *blockNumber;
+ let Self { blockNumber, blockHash } = *self;
ensure!(
- block_number <= ccx.ecx.env.block.number,
+ blockNumber <= 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);
-
+ 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 7355d08e95..203ea97056 100644
--- a/crates/evm/core/src/backend/cow.rs
+++ b/crates/evm/core/src/backend/cow.rs
@@ -246,7 +246,7 @@ impl<'a> DatabaseExt for CowBackend<'a> {
}
fn set_blockhash(&mut self, block_number: U256, block_hash: B256) {
- self.backend.to_mut().mem_db().to_owned().block_hashes.insert(block_number, block_hash);
+ self.backend.to_mut().set_blockhash(block_number, block_hash);
}
}
diff --git a/crates/evm/core/src/backend/mod.rs b/crates/evm/core/src/backend/mod.rs
index b09ced1057..48f4aa4b35 100644
--- a/crates/evm/core/src/backend/mod.rs
+++ b/crates/evm/core/src/backend/mod.rs
@@ -331,7 +331,7 @@ pub trait DatabaseExt: Database<Error = DatabaseError> + DatabaseCommit {
Ok(())
}
- /// set the blockhash for the given block number
+ /// Set the blockhash for the given block number.
fn set_blockhash(&mut self, block_number: U256, block_hash: B256);
}
@@ -1375,7 +1375,11 @@ impl DatabaseExt for Backend {
}
fn set_blockhash(&mut self, block_number: U256, block_hash: B256) {
- self.mem_db.block_hashes.insert(block_number, block_hash);
+ if let Some(db) = self.active_fork_db_mut() {
+ db.block_hashes.insert(block_number, block_hash);
+ } else {
+ self.mem_db.block_hashes.insert(block_number, block_hash);
+ }
}
}
diff --git a/crates/forge/tests/it/invariant.rs b/crates/forge/tests/it/invariant.rs
index 537569745a..06637f8e56 100644
--- a/crates/forge/tests/it/invariant.rs
+++ b/crates/forge/tests/it/invariant.rs
@@ -391,7 +391,7 @@ async fn test_shrink_fail_on_revert() {
runner.test_options.fuzz.seed = Some(U256::from(119u32));
runner.test_options.invariant.fail_on_revert = true;
runner.test_options.invariant.runs = 1;
- runner.test_options.invariant.depth = 100;
+ runner.test_options.invariant.depth = 1000;
match get_counterexample!(runner, &filter) {
CounterExample::Single(_) => panic!("CounterExample should be a sequence."),
diff --git a/testdata/default/cheats/SetBlockhash.t.sol b/testdata/default/cheats/SetBlockhash.t.sol
index 76ca854b67..f6c2af5f66 100644
--- a/testdata/default/cheats/SetBlockhash.t.sol
+++ b/testdata/default/cheats/SetBlockhash.t.sol
@@ -9,8 +9,8 @@ contract SetBlockhash is DSTest {
function testSetBlockhash() public {
bytes32 blockHash = 0x1234567890123456789012345678901234567890123456789012345678901234;
- vm.setBlockhash(1, blockHash);
- bytes32 expected = blockhash(1);
+ vm.setBlockhash(block.number - 1, blockHash);
+ bytes32 expected = blockhash(block.number - 1);
assertEq(blockHash, expected);
}
} We also have to document that |
…setblockhash-cheatcode
…/foundry into tuan/add-setblockhash-cheatcode
ab9274b
to
56f522d
Compare
56f522d
to
74d4191
Compare
Could you please push one more commit (empty or otherwise)? GitHub Actions was broken yesterday |
Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pending @DaniPopes
@@ -430,6 +430,10 @@ interface Vm { | |||
#[cheatcode(group = Evm, safety = Safe)] | |||
function getBlobBaseFee() external view returns (uint256 blobBaseFee); | |||
|
|||
/// Set blockhash for the current block. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add the docs with the limitations here too
…/foundry into tuan/add-setblockhash-cheatcode
one last thing is |
Motivation
Closes #7929
Solution