Skip to content

Commit

Permalink
ethereum: add arrayElementLocation() test helper
Browse files Browse the repository at this point in the history
Review feedback from @kcsongor in #3363
  • Loading branch information
SEJeff committed Sep 11, 2023
1 parent d9aacf1 commit a9437e8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 2 additions & 2 deletions ethereum/forge-test/Governance.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ contract TestGovernance is TestUtils {
for(uint8 i = 0; i < newGuardianSet.length; i++) {
vm.assume(newGuardianSet[i] != address(0));
// New GuardianSet key array elements should be initialized from zero to non-zero
vm.assume(storageSlot != bytes32(uint256(keccak256(abi.encodePacked(hashedLocationOffset(1, GUARDIANSETS_SLOT, 0)))) + i));
vm.assume(storageSlot != arrayElementLocation(hashedLocationOffset(1, GUARDIANSETS_SLOT, 0), i));
}

vm.chainId(EVMCHAINID);
Expand Down Expand Up @@ -769,7 +769,7 @@ contract TestGovernance is TestUtils {
for(uint8 i = 0; i < newGuardianSet.length; i++) {
vm.assume(newGuardianSet[i] != address(0));
// New GuardianSet key array elements should be initialized from zero to non-zero
vm.assume(storageSlot != bytes32(uint256(keccak256(abi.encodePacked(hashedLocationOffset(1, GUARDIANSETS_SLOT, 0)))) + i));
vm.assume(storageSlot != arrayElementLocation(hashedLocationOffset(1, GUARDIANSETS_SLOT, 0), i));
}

vm.chainId(EVMCHAINID);
Expand Down
6 changes: 6 additions & 0 deletions ethereum/forge-test/rv-helpers/TestUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,10 @@ contract TestUtils is Test, KEVMCheats {

signature = abi.encodePacked(r, s,(v - 27));
}

// @dev compute the storage slot of an array based on its key and offset
// @dev `keyHash` is generally from `hashedLocationOffset()`
function arrayElementLocation(bytes32 keyHash, uint8 arrayOffset) public pure returns (bytes32) {
return bytes32(uint256(keccak256(abi.encodePacked(keyHash))) + arrayOffset);
}
}

0 comments on commit a9437e8

Please sign in to comment.