From a9437e8dc27dc2845df64e0407b7afdb87e729ce Mon Sep 17 00:00:00 2001 From: Jeff Schroeder Date: Mon, 11 Sep 2023 17:29:41 +0000 Subject: [PATCH] ethereum: add arrayElementLocation() test helper Review feedback from @kcsongor in #3363 --- ethereum/forge-test/Governance.t.sol | 4 ++-- ethereum/forge-test/rv-helpers/TestUtils.sol | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/ethereum/forge-test/Governance.t.sol b/ethereum/forge-test/Governance.t.sol index ce5d60d6a0..597c4cbd62 100644 --- a/ethereum/forge-test/Governance.t.sol +++ b/ethereum/forge-test/Governance.t.sol @@ -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); @@ -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); diff --git a/ethereum/forge-test/rv-helpers/TestUtils.sol b/ethereum/forge-test/rv-helpers/TestUtils.sol index e22ee0cc82..188ec26f0d 100644 --- a/ethereum/forge-test/rv-helpers/TestUtils.sol +++ b/ethereum/forge-test/rv-helpers/TestUtils.sol @@ -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); + } }