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(cheatcodes): getBlockNumber and getBlockTimestamp #6630

Merged
merged 8 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

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

/// Gets the current `block.number`
#[cheatcode(group = Evm, safety = Safe)]
function getBlockNumber() external view returns (uint256 height);

/// Sets `tx.gasprice`.
#[cheatcode(group = Evm, safety = Unsafe)]
function txGasPrice(uint256 newGasPrice) external;
Expand All @@ -325,6 +329,10 @@ interface Vm {
#[cheatcode(group = Evm, safety = Unsafe)]
function warp(uint256 newTimestamp) external;

/// Gets the current `block.timestamp`
Evalir marked this conversation as resolved.
Show resolved Hide resolved
#[cheatcode(group = Evm, safety = Safe)]
function getBlockTimestamp() external view returns (uint256 timestamp);

// -------- Account State --------

/// Sets an address' balance.
Expand Down
14 changes: 14 additions & 0 deletions crates/cheatcodes/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,13 @@ impl Cheatcode for rollCall {
}
}

impl Cheatcode for getBlockNumberCall {
fn apply_full<DB: DatabaseExt>(&self, ccx: &mut CheatsCtxt<DB>) -> Result {
let Self {} = self;
Ok(ccx.data.env.block.number.abi_encode())
}
}

impl Cheatcode for txGasPriceCall {
fn apply_full<DB: DatabaseExt>(&self, ccx: &mut CheatsCtxt<DB>) -> Result {
let Self { newGasPrice } = self;
Expand All @@ -226,6 +233,13 @@ impl Cheatcode for warpCall {
}
}

impl Cheatcode for getBlockTimestampCall {
fn apply_full<DB: DatabaseExt>(&self, ccx: &mut CheatsCtxt<DB>) -> Result {
let Self {} = self;
Ok(ccx.data.env.block.timestamp.abi_encode())
}
}

impl Cheatcode for dealCall {
fn apply_full<DB: DatabaseExt>(&self, ccx: &mut CheatsCtxt<DB>) -> Result {
let Self { account: address, newBalance: new_balance } = *self;
Expand Down
27 changes: 27 additions & 0 deletions testdata/cheats/GetBlockTimestamp.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity 0.8.18;

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

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

function testGetTimestamp() public {
uint256 timestamp = vm.getBlockTimestamp();
assertEq(timestamp, 1, "timestamp should be 1");
}

function testGetTimestampWithWarp() public {
uint256 timestamp = vm.getBlockTimestamp();
assertEq(timestamp, 1, "timestamp should be 1");
vm.warp(10);
assertEq(block.timestamp, 10, "warp failed");
Evalir marked this conversation as resolved.
Show resolved Hide resolved
}

function testGetTimestampWithWarpFuzzed(uint128 jump) public {
uint256 pre = vm.getBlockTimestamp();
vm.warp(pre + jump);
assertEq(vm.getBlockTimestamp(), pre + jump, "warp failed");
}
}
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.

25 changes: 25 additions & 0 deletions testdata/cheats/getBlockNumber.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity 0.8.18;

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

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

function testGetBlockNumber() public {
uint256 height = vm.getBlockNumber();
assertEq(height, uint256(block.number), "height should be equal to block.number");
}

function testGetBlockNumberWithRoll() public {
vm.roll(10);
assertEq(vm.getBlockNumber(), 10, "could not get correct block height after roll");
}

function testGetBlockNumberWithRollFuzzed(uint128 jump) public {
uint256 pre = vm.getBlockNumber();
vm.roll(pre + jump);
assertEq(vm.getBlockNumber(), pre + jump, "could not get correct block height after roll");
}
}
Loading