Skip to content

Commit

Permalink
feat: add vm.startStateDiffRecording and `vm.stopAndReturnStateDiff…
Browse files Browse the repository at this point in the history
…` cheats (#481)

* feat: add storage and access record cheats

* fix: rename return parameter to avoid shadowing

---------

Co-authored-by: Matt Solomon <matt@mattsolomon.dev>
  • Loading branch information
Inphi and mds1 authored Nov 17, 2023
1 parent bdea49f commit c22437a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
46 changes: 46 additions & 0 deletions src/Vm.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ interface VmSafe {
RecurrentPrank
}

enum AccountAccessKind {
Call,
DelegateCall,
CallCode,
StaticCall,
Create,
SelfDestruct,
Resume
}

struct Log {
bytes32[] topics;
bytes data;
Expand Down Expand Up @@ -76,6 +86,35 @@ interface VmSafe {
bytes stderr;
}

struct ChainInfo {
uint256 forkId;
uint256 chainId;
}

struct AccountAccess {
ChainInfo chainInfo;
AccountAccessKind kind;
address account;
address accessor;
bool initialized;
uint256 oldBalance;
uint256 newBalance;
bytes deployedCode;
uint256 value;
bytes data;
bool reverted;
StorageAccess[] storageAccesses;
}

struct StorageAccess {
address account;
bytes32 slot;
bool isWrite;
bytes32 previousValue;
bytes32 newValue;
bool reverted;
}

// ======== EVM ========

// Gets the address for a given private key
Expand All @@ -98,6 +137,13 @@ interface VmSafe {
// Gets all accessed reads and write slot from a `vm.record` session, for a given address
function accesses(address target) external returns (bytes32[] memory readSlots, bytes32[] memory writeSlots);

// Record all account accesses as part of CREATE, CALL or SELFDESTRUCT opcodes in order,
// along with the context of the calls.
function startStateDiffRecording() external;

// Returns an ordered array of all account accesses from a `vm.startStateDiffRecording` session.
function stopAndReturnStateDiff() external returns (AccountAccess[] memory accountAccesses);

// -------- Recording Map Writes --------

// Starts recording all map SSTOREs for later retrieval.
Expand Down
2 changes: 1 addition & 1 deletion test/Vm.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ contract VmTest is Test {
// inadvertently moved between Vm and VmSafe. This test must be updated each time a function is
// added to or removed from Vm or VmSafe.
function test_interfaceId() public {
assertEq(type(VmSafe).interfaceId, bytes4(0x1578a242), "VmSafe");
assertEq(type(VmSafe).interfaceId, bytes4(0x7006b885), "VmSafe");
assertEq(type(Vm).interfaceId, bytes4(0x316cedc3), "Vm");
}
}

0 comments on commit c22437a

Please sign in to comment.