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(forge): getNonce and setNonce cheatcodes #631

Closed
wants to merge 1 commit into from
Closed
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
8 changes: 4 additions & 4 deletions Cargo.lock

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

12 changes: 12 additions & 0 deletions evm-adapters/src/sputnik/cheatcodes/cheatcode_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -867,13 +867,25 @@ impl<'a, 'b, B: Backend, P: PrecompileSet> CheatcodeStackExecutor<'a, 'b, B, P>
self.add_debug(CheatOp::EXPECTCALL);
self.state_mut().expected_calls.entry(inner.0).or_default().push(inner.1.to_vec());
}

HEVMCalls::Label(inner) => {
self.add_debug(CheatOp::LABEL);
let address = inner.0;
let label = inner.1;

self.state_mut().labels.insert(address, label);
}

HEVMCalls::GetNonce(inner) => {
self.add_debug(CheatOp::GETNONCE);
let nonce = self.state().basic(inner.0).nonce;
res = ethers::abi::encode(&[Token::Uint(nonce)]);
}
HEVMCalls::SetNonce(inner) => {
self.add_debug(CheatOp::SETNONCE);
// TODO

}
};

self.fill_trace(&trace, true, Some(res.clone()), pre_index);
Expand Down
4 changes: 4 additions & 0 deletions evm-adapters/src/sputnik/cheatcodes/debugger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ pub enum CheatOp {
EXPECTCALL,
GETCODE,
LABEL,
SETNONCE,
GETNONCE,
}

impl From<CheatOp> for OpCode {
Expand Down Expand Up @@ -214,6 +216,8 @@ impl CheatOp {
CheatOp::EXPECTCALL => "VM_EXPECTCALL",
CheatOp::GETCODE => "VM_GETCODE",
CheatOp::LABEL => "VM_LABEL",
CheatOp::GETNONCE => "VM_GETNONCE",
CheatOp::SETNONCE => "VM_SETNONCE",
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions evm-adapters/src/sputnik/cheatcodes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ ethers::contract::abigen!(
expectCall(address,bytes)
getCode(string)
label(address,string)
getNonce(address)
setNonce(address,uint256)
]"#,
);
pub use hevm_mod::{HEVMCalls, HEVM_ABI};
Expand Down
30 changes: 30 additions & 0 deletions evm-adapters/testdata/CheatCodes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ interface Hevm {
function getCode(string calldata) external returns (bytes memory);
// Labels an address in call traces
function label(address, string calldata) external;

// Get account nonce
function getNonce(address) external returns (uint256);

// Set account nonce
function setNonce(address,uint256) external;
}

contract HasStorage {
Expand Down Expand Up @@ -614,6 +620,25 @@ contract CheatCodes is DSTest {
extcodecopy(who, add(o_code, 0x20), 0, size)
}
}

function testSetAndGetNonce() public {
address alice = address(1337);
assertEq(hevm.getNonce(alice), 0);

MockMe target = new MockMe();

hevm.startPrank(alice);
target.setA();
target.setA();
assertEq(hevm.getNonce(alice), 2);

hevm.setNonce(alice, 20);
assertEq(hevm.getNonce(alice), 20);

hevm.setNonce(alice, 14);
assertEq(hevm.getNonce(alice), 14);
hevm.stopPrank();
}
}

contract Label {
Expand Down Expand Up @@ -793,6 +818,7 @@ contract ExpectEmit {
}

contract MockMe {
uint256 _a;
function numberA() public returns (uint256) {
return 1;
}
Expand All @@ -804,6 +830,10 @@ contract MockMe {
function add(uint256 a, uint256 b) public returns (uint256) {
return a + b;
}

function setA() public {
_a = 1;
}
}

contract MockInner {
Expand Down