Skip to content

Commit

Permalink
test(store): test storage load with offset and manual pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
dk1a committed Nov 30, 2023
1 parent 504e25d commit eec797d
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions packages/store/test/Storage.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,41 @@ contract StorageTest is Test, GasReporter {
Storage.store({ storagePointer: storagePointer, offset: offset, data: abi.encodePacked((data)) });
assertEq(bytes16(Storage.loadField({ storagePointer: storagePointer, length: 16, offset: offset })), data);
}

function testStoreLoadToPointer() public {
uint256 memoryCorruptionCheck = 0x0101010101010101010101010101010101010101010101010101010101010101;
uint80 prefix = 0x01010101010101010101;
uint80 dataBeforeLoad = 0x00000000000000000000;
uint80 dataAfterLoad = 0xbeeeeeeeeeeeeeeeeeef;
uint256 postfix = 0x010101010101010101010101;

bytes memory testData = abi.encodePacked(
memoryCorruptionCheck,
prefix,
dataBeforeLoad,
postfix,
memoryCorruptionCheck
);
bytes memory expectedData = abi.encodePacked(
memoryCorruptionCheck,
prefix,
dataAfterLoad,
postfix,
memoryCorruptionCheck
);
uint256 memoryPointer;
/// @solidity memory-safe-assembly
assembly {
memoryPointer := add(testData, 0x20)
}
// skip prefixes
memoryPointer += 32 + 10;

uint256 storagePointer = uint256(keccak256("some location"));
Storage.store({ storagePointer: storagePointer, offset: 10, data: abi.encodePacked(dataAfterLoad) });

Storage.load({ storagePointer: storagePointer, length: 10, offset: 10, memoryPointer: memoryPointer });

assertEq(testData, expectedData);
}
}

0 comments on commit eec797d

Please sign in to comment.