Skip to content

Commit

Permalink
foundry: vm.dumpState skips empty accounts (foundry-rs#6986)
Browse files Browse the repository at this point in the history
* foundry: `vm.dumpState` skips empty accounts

Update `vm.dumpState` to also skip empty accounts.
Empty accounts are not particularly useful and would
otherwise require a postprocessing step after the state dump
to remove.

Adds a unit test showing that it works and updates an old unit
test as an empty account is defined by a nonce of 0, no balance
and no code. If there is storage, it still counts as an empty
account.

cargo fmt

* testdata: cleanup
  • Loading branch information
tynes authored and RPate97 committed Feb 3, 2024
1 parent fe0ef96 commit 74bcddb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
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);
}
}

0 comments on commit 74bcddb

Please sign in to comment.