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

foundry: vm.dumpState skips empty accounts #6986

Merged
merged 2 commits into from
Feb 1, 2024
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
9 changes: 5 additions & 4 deletions crates/cheatcodes/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,22 +97,23 @@ impl Cheatcode for dumpStateCall {
let Self { pathToStateJson } = self;
let path = Path::new(pathToStateJson);

// Do not include system accounts in the dump.
let skip = |key: &Address| {
// Do not include system account or empty accounts in the dump.
let skip = |key: &Address, val: &Account| {
key == &CHEATCODE_ADDRESS ||
key == &CALLER ||
key == &HARDHAT_CONSOLE_ADDRESS ||
key == &TEST_CONTRACT_ADDRESS ||
key == &ccx.caller ||
key == &ccx.state.config.evm_opts.sender
key == &ccx.state.config.evm_opts.sender ||
val.is_empty()
};

let alloc = ccx
.data
.journaled_state
.state()
.into_iter()
.filter(|(key, _)| !skip(key))
.filter(|(key, val)| !skip(key, val))
.map(|(key, val)| {
(
key,
Expand Down
18 changes: 17 additions & 1 deletion testdata/cheats/dumpState.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ contract DumpStateTest is DSTest {

vm.setNonce(address(0x100), 1);
vm.deal(address(0x200), 1 ether);
vm.setNonce(address(0x300), 1);
vm.store(address(0x300), bytes32(uint256(1)), bytes32(uint256(2)));
vm.etch(address(0x400), hex"af");

Expand All @@ -85,7 +86,7 @@ contract DumpStateTest is DSTest {
assertEq(0, vm.parseJsonKeys(json, string.concat(".", vm.toString(address(0x200)), ".storage")).length);

assertEq(4, vm.parseJsonKeys(json, string.concat(".", vm.toString(address(0x300)))).length);
assertEq(0, vm.parseJsonUint(json, string.concat(".", vm.toString(address(0x300)), ".nonce")));
assertEq(1, vm.parseJsonUint(json, string.concat(".", vm.toString(address(0x300)), ".nonce")));
assertEq(0, vm.parseJsonUint(json, string.concat(".", vm.toString(address(0x300)), ".balance")));
assertEq(hex"", vm.parseJsonBytes(json, string.concat(".", vm.toString(address(0x300)), ".code")));
assertEq(1, vm.parseJsonKeys(json, string.concat(".", vm.toString(address(0x300)), ".storage")).length);
Expand Down Expand Up @@ -120,4 +121,19 @@ contract DumpStateTest is DSTest {

vm.removeFile(path);
}

function testDumpStateEmptyAccount() public {
string memory path = string.concat(vm.projectRoot(), "/fixtures/Json/test_dump_state_empty_account.json");

SimpleContract s = new SimpleContract();
vm.etch(address(s), hex"");
vm.resetNonce(address(s));

vm.dumpState(path);
string memory json = vm.readFile(path);
string[] memory keys = vm.parseJsonKeys(json, "");
assertEq(keys.length, 0);

vm.removeFile(path);
}
}
Loading