Skip to content
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: vm.blobhashes #7001

Merged
merged 13 commits into from
Apr 25, 2024
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.

Loading