|
| 1 | +// SPDX-License-Identifier: GPL-3.0 |
| 2 | +pragma solidity ^0.8.20; |
| 3 | + |
| 4 | +import {Test} from "forge-std/Test.sol"; |
| 5 | + |
| 6 | +import {ModuleStorageLib, StoragePointer} from "../../src/libraries/ModuleStorageLib.sol"; |
| 7 | + |
| 8 | +contract ModuleStorageLibTest is Test { |
| 9 | + using ModuleStorageLib for bytes; |
| 10 | + using ModuleStorageLib for bytes32; |
| 11 | + |
| 12 | + uint256 public constant FUZZ_ARR_SIZE = 32; |
| 13 | + |
| 14 | + address public account1; |
| 15 | + |
| 16 | + struct TestStruct { |
| 17 | + uint256 a; |
| 18 | + uint256 b; |
| 19 | + } |
| 20 | + |
| 21 | + function setUp() public { |
| 22 | + account1 = makeAddr("account1"); |
| 23 | + } |
| 24 | + |
| 25 | + function test_storagePointer() public { |
| 26 | + bytes memory key = ModuleStorageLib.allocateAssociatedStorageKey(account1, 0, 1); |
| 27 | + |
| 28 | + StoragePointer ptr = ModuleStorageLib.associatedStorageLookup( |
| 29 | + key, hex"00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff" |
| 30 | + ); |
| 31 | + TestStruct storage val = _castPtrToStruct(ptr); |
| 32 | + |
| 33 | + vm.record(); |
| 34 | + val.a = 0xdeadbeef; |
| 35 | + val.b = 123; |
| 36 | + (, bytes32[] memory accountWrites) = vm.accesses(address(this)); |
| 37 | + |
| 38 | + // printStorageReadsAndWrites(address(this)); |
| 39 | + |
| 40 | + assertEq(accountWrites.length, 2); |
| 41 | + bytes32 expectedKey = keccak256( |
| 42 | + abi.encodePacked( |
| 43 | + uint256(uint160(account1)), |
| 44 | + uint256(0), |
| 45 | + hex"00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff" |
| 46 | + ) |
| 47 | + ); |
| 48 | + assertEq(accountWrites[0], expectedKey); |
| 49 | + assertEq(vm.load(address(this), expectedKey), bytes32(uint256(0xdeadbeef))); |
| 50 | + assertEq(accountWrites[1], bytes32(uint256(expectedKey) + 1)); |
| 51 | + assertEq(vm.load(address(this), bytes32(uint256(expectedKey) + 1)), bytes32(uint256(123))); |
| 52 | + } |
| 53 | + |
| 54 | + function testFuzz_storagePointer( |
| 55 | + address account, |
| 56 | + uint256 batchIndex, |
| 57 | + bytes32 inputKey, |
| 58 | + uint256[FUZZ_ARR_SIZE] calldata values |
| 59 | + ) public { |
| 60 | + bytes memory key = ModuleStorageLib.allocateAssociatedStorageKey(account, batchIndex, 1); |
| 61 | + uint256[FUZZ_ARR_SIZE] storage val = |
| 62 | + _castPtrToArray(ModuleStorageLib.associatedStorageLookup(key, inputKey)); |
| 63 | + // Write values to storage |
| 64 | + vm.record(); |
| 65 | + for (uint256 i = 0; i < FUZZ_ARR_SIZE; i++) { |
| 66 | + val[i] = values[i]; |
| 67 | + } |
| 68 | + // Assert the writes took place in the right location, and the correct value is stored there |
| 69 | + (, bytes32[] memory accountWrites) = vm.accesses(address(this)); |
| 70 | + assertEq(accountWrites.length, FUZZ_ARR_SIZE); |
| 71 | + for (uint256 i = 0; i < FUZZ_ARR_SIZE; i++) { |
| 72 | + bytes32 expectedKey = bytes32( |
| 73 | + uint256(keccak256(abi.encodePacked(uint256(uint160(account)), uint256(batchIndex), inputKey))) + i |
| 74 | + ); |
| 75 | + assertEq(accountWrites[i], expectedKey); |
| 76 | + assertEq(vm.load(address(this), expectedKey), bytes32(uint256(values[i]))); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + function _castPtrToArray(StoragePointer ptr) internal pure returns (uint256[FUZZ_ARR_SIZE] storage val) { |
| 81 | + /// @solidity memory-safe-assembly |
| 82 | + assembly { |
| 83 | + val.slot := ptr |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + function _castPtrToStruct(StoragePointer ptr) internal pure returns (TestStruct storage val) { |
| 88 | + /// @solidity memory-safe-assembly |
| 89 | + assembly { |
| 90 | + val.slot := ptr |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments