Skip to content

Commit

Permalink
feat: vm.blobhashes (foundry-rs#7001)
Browse files Browse the repository at this point in the history
* feat: `vm.blobhashes`

* chore: rename

* test: add `vm.blobhashes` test

* fix: add missing vm fn

* fix: `cargo cheat`

* fix: specify blobhashes loc

* fix: use solidity 0.8.24 for blobhash test

* chore: reinsert noop line ¯\_(ツ)_/¯

* refactor: move cancun cheats to sep dir

* temp

* temp

* feat(cheatcodes): getBlobhashes

---------

Co-authored-by: Yash Atreya <[email protected]>
  • Loading branch information
onbjerg and yash-atreya authored Apr 25, 2024
1 parent 1fc4aa3 commit 19d69f2
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 0 deletions.
40 changes: 40 additions & 0 deletions crates/cheatcodes/assets/cheatcodes.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions crates/cheatcodes/spec/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,18 @@ interface Vm {
#[cheatcode(group = Evm, safety = Unsafe)]
function prevrandao(uint256 newPrevrandao) external;

/// Sets the blobhashes in the transaction.
/// Not available on EVM versions before Cancun.
/// If used on unsupported EVM versions it will revert.
#[cheatcode(group = Evm, safety = Unsafe)]
function blobhashes(bytes32[] calldata blobhashes) external;

/// Gets the blockhashes from the current transaction.
/// Not available on EVM versions before Cancun.
/// If used on unsupported EVM versions it will revert.
#[cheatcode(group = Evm, safety = Unsafe)]
function getBlobhashes() external view returns (bytes32[] memory blobhashes);

/// Sets `block.height`.
#[cheatcode(group = Evm, safety = Unsafe)]
function roll(uint256 newHeight) external;
Expand Down
25 changes: 25 additions & 0 deletions crates/cheatcodes/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,31 @@ impl Cheatcode for prevrandao_1Call {
}
}

impl Cheatcode for blobhashesCall {
fn apply_full<DB: DatabaseExt>(&self, ccx: &mut CheatsCtxt<DB>) -> Result {
let Self { blobhashes } = self;
ensure!(
ccx.ecx.spec_id() >= SpecId::CANCUN,
"`blobhash` is not supported before the Cancun hard fork; \
see EIP-4844: https://eips.ethereum.org/EIPS/eip-4844"
);
ccx.ecx.env.tx.blob_hashes.clone_from(blobhashes);
Ok(Default::default())
}
}

impl Cheatcode for getBlobhashesCall {
fn apply_full<DB: DatabaseExt>(&self, ccx: &mut CheatsCtxt<DB>) -> Result {
let Self {} = self;
ensure!(
ccx.ecx.spec_id() >= SpecId::CANCUN,
"`blobhash` is not supported before the Cancun hard fork; \
see EIP-4844: https://eips.ethereum.org/EIPS/eip-4844"
);
Ok(ccx.ecx.env.tx.blob_hashes.clone().abi_encode())
}
}

impl Cheatcode for rollCall {
fn apply_full<DB: DatabaseExt>(&self, ccx: &mut CheatsCtxt<DB>) -> Result {
let Self { newHeight } = self;
Expand Down
20 changes: 20 additions & 0 deletions testdata/cancun/cheats/Blobhashes.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity ^0.8.25;

import "ds-test/test.sol";
import "cheats/Vm.sol";

contract BlobhashesTest is DSTest {
Vm constant vm = Vm(HEVM_ADDRESS);

function testSetAndGetBlobhashes() public {
bytes32[] memory blobhashes = new bytes32[](2);
blobhashes[0] = bytes32(0x0000000000000000000000000000000000000000000000000000000000000001);
blobhashes[1] = bytes32(0x0000000000000000000000000000000000000000000000000000000000000002);
vm.blobhashes(blobhashes);

bytes32[] memory gotBlobhashes = vm.getBlobhashes();
assertEq(gotBlobhashes[0], blobhashes[0]);
assertEq(gotBlobhashes[1], blobhashes[1]);
}
}
2 changes: 2 additions & 0 deletions testdata/cheats/Vm.sol

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 19d69f2

Please sign in to comment.