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

add: resetNonce cheatcode #5033

Merged
merged 5 commits into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions abi/abi/HEVM.sol
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ getLabel(address)(string)
assume(bool)
setNonce(address,uint64)
getNonce(address)
resetNonce(address)
setNonceUnsafe(address,uint64)
chainId(uint256)
txGasPrice(uint256)

Expand Down
75 changes: 74 additions & 1 deletion abi/src/bindings/hevm.rs

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions evm/src/executor/inspector/cheatcodes/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,25 @@ pub fn apply<DB: DatabaseExt>(
},
)??
}
HEVMCalls::SetNonceUnsafe(inner) => {
with_journaled_account(
&mut data.journaled_state,
data.db,
inner.0,
|account| -> Result {
// nonce must increment only
joaquinlpereyra marked this conversation as resolved.
Show resolved Hide resolved
let new = inner.1;
account.info.nonce = new;
Ok(Bytes::new())
},
)??
}
HEVMCalls::ResetNonce(inner) => {
with_journaled_account(&mut data.journaled_state, data.db, inner.0, |account| -> Result {
account.info.nonce = 0;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing I just thought of is that contract nonces start at 1, so we probably need to check if the account has code and then set it to 1 or 0 based on that: https://eips.ethereum.org/EIPS/eip-161

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pulled into #5043 to track

Ok(Bytes::new())
})??
}
HEVMCalls::GetNonce(inner) => {
correct_sender_nonce(
b160_to_h160(data.env.tx.caller),
Expand Down
6 changes: 6 additions & 0 deletions testdata/cheats/Cheats.sol
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,12 @@ interface Cheats {
// Get nonce for an account
function getNonce(address) external returns (uint64);

// Resets the nonce for an account
function resetNonce(address) external;

// Set nonce for an account
joaquinlpereyra marked this conversation as resolved.
Show resolved Hide resolved
function setNonceUnsafe(address, uint64) external;

// Set block.chainid (newChainId)
function chainId(uint256) external;

Expand Down
34 changes: 34 additions & 0 deletions testdata/cheats/ResetNonce.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// SPDX-License-Identifier: Unlicense
pragma solidity >=0.8.0;

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

contract Foo {
function f() external view returns (uint256) {
return 1;
}
}

contract ResetNonce is DSTest {
Cheats constant cheats = Cheats(HEVM_ADDRESS);
Foo public foo;

function setUp() public {
foo = new Foo();
}

function testResetNonce() public {
cheats.setNonce(address(foo), 10);

// makes sure working correctly after mutating nonce.
foo.f();
assertEq(cheats.getNonce(address(foo)), 10);
foo.f();

// now make sure that it is reset after calling the cheatcode.
cheats.resetNonce(address(foo));
assertEq(cheats.getNonce(address(foo)), 0);
foo.f();
}
}
34 changes: 34 additions & 0 deletions testdata/cheats/SetNonceUnsafe.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// SPDX-License-Identifier: Unlicense
pragma solidity >=0.8.18;

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

contract Foo {
function f() external view returns (uint256) {
return 1;
}
}

contract SetNonceTest is DSTest {
Cheats constant cheats = Cheats(HEVM_ADDRESS);
Foo public foo;

function setUp() public {
foo = new Foo();
}

function testSetNonceUnsafe() public {
cheats.setNonceUnsafe(address(foo), 10);
// makes sure working correctly after mutating nonce.
foo.f();
assertEq(cheats.getNonce(address(foo)), 10);
foo.f();
}

function testDoesNotFailDecreasingNonce() public {
cheats.setNonce(address(foo), 10);
cheats.setNonceUnsafe(address(foo), 5);
assertEq(cheats.getNonce(address(foo)), 5);
}
}