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: add vm.roll cheatcode #20

Merged
merged 1 commit into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 13 additions & 2 deletions e2e-tests/contracts/TestCheatcodes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,23 @@ contract TestCheatcodes {
require(success, "setGreeting failed");
}

function setNonce(address account, uint256 nonce) external {
function testSetNonce(address account, uint256 nonce) external {
(bool success, ) = CHEATCODE_ADDRESS.call(abi.encodeWithSignature("setNonce(address,uint64)", account, nonce));
require(success, "setNonce failed");
}

function warp(uint256 timestamp) external {
function testRoll(uint256 blockNumber) external {
uint256 initialBlockNumber = block.number;
require(blockNumber != initialBlockNumber, "block number must be different than current block number");

(bool success, ) = CHEATCODE_ADDRESS.call(abi.encodeWithSignature("roll(uint256)", blockNumber));
require(success, "roll failed");

uint256 finalBlockNumber = block.number;
require(finalBlockNumber == blockNumber, "block number was not changed");
}

function testWarp(uint256 timestamp) external {
uint256 initialTimestamp = block.timestamp;
require(timestamp != initialTimestamp, "timestamp must be different than current block timestamp");

Expand Down
10 changes: 6 additions & 4 deletions e2e-tests/test/cheatcodes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe("Cheatcodes", function () {
// Act
const cheatcodes = await deployContract(deployer, "TestCheatcodes", []);
const initialNonce = await provider.getTransactionCount(randomWallet.address);
const tx = await cheatcodes.setNonce(randomWallet.address, 1234);
const tx = await cheatcodes.testSetNonce(randomWallet.address, 1234);
const receipt = await tx.wait();

// Assert
Expand All @@ -71,10 +71,12 @@ describe("Cheatcodes", function () {
const wallet = new Wallet(RichAccounts[0].PrivateKey);
const deployer = new Deployer(hre, wallet);
const contract = await deployContract(deployer, "TestCheatcodes", []);
const randomWallet = Wallet.createRandom().connect(provider);

const blockNumber = await provider.getBlockNumber();
const newBlockNumber = blockNumber + 345;

// Act
const tx = await contract.testRoll({ gasLimit: 1000000 });
const tx = await contract.testRoll(newBlockNumber, { gasLimit: 1000000 });
const receipt = await tx.wait();

// Assert
Expand All @@ -92,7 +94,7 @@ describe("Cheatcodes", function () {

// Act
const cheatcodes = await deployContract(deployer, "TestCheatcodes", []);
const tx = await cheatcodes.warp(expectedTimestamp, {
const tx = await cheatcodes.testWarp(expectedTimestamp, {
gasLimit: 1000000,
});
expectedTimestamp += 2; // New transaction will add two blocks
Expand Down
16 changes: 16 additions & 0 deletions src/cheatcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ abigen!(
function deal(address who, uint256 newBalance)
function etch(address who, bytes calldata code)
function setNonce(address account, uint64 nonce)
function roll(uint256 blockNumber)
function warp(uint256 timestamp)
]"#
);
Expand Down Expand Up @@ -167,6 +168,21 @@ impl<F: NodeCtx> CheatcodeTracer<F> {
);
storage.set_value(nonce_key, u256_to_h256(enforced_full_nonce));
}
Roll(RollCall { block_number }) => {
tracing::info!("Setting block number to {}", block_number);

let key = StorageKey::new(
AccountTreeId::new(zksync_types::SYSTEM_CONTEXT_ADDRESS),
zksync_types::CURRENT_VIRTUAL_BLOCK_INFO_POSITION,
);
let mut storage = storage.borrow_mut();
let (_, block_timestamp) =
unpack_block_info(h256_to_u256(storage.read_value(&key)));
storage.set_value(
key,
u256_to_h256(pack_block_info(block_number.as_u64(), block_timestamp)),
);
}
Warp(WarpCall { timestamp }) => {
tracing::info!("Setting block timestamp {}", timestamp);
self.node_ctx.set_time(timestamp.as_u64());
Expand Down