|
| 1 | +// SPDX-License-Identifier: MIT OR Apache-2.0 |
| 2 | +pragma solidity ^0.8.18; |
| 3 | + |
| 4 | +import "utils/Test.sol"; |
| 5 | + |
| 6 | +contract SlotNumberTest is Test { |
| 7 | + address constant SLOT_READER = address(0x1234); |
| 8 | + |
| 9 | + function setUp() public { |
| 10 | + vm.setEvmVersion("amsterdam"); |
| 11 | + // SLOTNUM; MSTORE(0); RETURN(0, 32). |
| 12 | + vm.etch(SLOT_READER, hex"4b60005260206000f3"); |
| 13 | + } |
| 14 | + |
| 15 | + function readSlotNumber() internal view returns (uint256) { |
| 16 | + (bool success, bytes memory result) = SLOT_READER.staticcall(""); |
| 17 | + require(success, "SLOTNUM failed"); |
| 18 | + return abi.decode(result, (uint256)); |
| 19 | + } |
| 20 | + |
| 21 | + function testRollSlot() public { |
| 22 | + uint256 number = vm.getBlockNumber(); |
| 23 | + uint256 timestamp = vm.getBlockTimestamp(); |
| 24 | + assertEq(vm.getSlotNumber(), readSlotNumber()); |
| 25 | + vm.rollSlot(100); |
| 26 | + assertEq(vm.getSlotNumber(), 100); |
| 27 | + assertEq(readSlotNumber(), 100); |
| 28 | + vm.rollSlot(0); |
| 29 | + assertEq(vm.getSlotNumber(), 0); |
| 30 | + assertEq(readSlotNumber(), 0); |
| 31 | + vm.rollSlot(type(uint64).max); |
| 32 | + assertEq(vm.getSlotNumber(), type(uint64).max); |
| 33 | + assertEq(readSlotNumber(), type(uint64).max); |
| 34 | + assertEq(vm.getBlockNumber(), number); |
| 35 | + assertEq(vm.getBlockTimestamp(), timestamp); |
| 36 | + } |
| 37 | + |
| 38 | + function testRollSlotFuzzed(uint64 first, uint64 second) public { |
| 39 | + vm.rollSlot(first); |
| 40 | + assertEq(vm.getSlotNumber(), first); |
| 41 | + assertEq(readSlotNumber(), first); |
| 42 | + vm.rollSlot(second); |
| 43 | + assertEq(vm.getSlotNumber(), second); |
| 44 | + assertEq(readSlotNumber(), second); |
| 45 | + } |
| 46 | + |
| 47 | + function testRollSlotSnapshot() public { |
| 48 | + vm.rollSlot(42); |
| 49 | + uint256 snapshot = vm.snapshotState(); |
| 50 | + vm.rollSlot(100); |
| 51 | + assertTrue(vm.revertToState(snapshot)); |
| 52 | + assertEq(vm.getSlotNumber(), 42); |
| 53 | + assertEq(readSlotNumber(), 42); |
| 54 | + } |
| 55 | + |
| 56 | + function testSlotNumberBeforeAmsterdam() public { |
| 57 | + vm.setEvmVersion("osaka"); |
| 58 | + vm._expectCheatcodeRevert( |
| 59 | + "vm.rollSlot: `rollSlot` is not supported before the Amsterdam hard fork; see EIP-7843: https://eips.ethereum.org/EIPS/eip-7843" |
| 60 | + ); |
| 61 | + vm.rollSlot(1); |
| 62 | + vm._expectCheatcodeRevert( |
| 63 | + "vm.getSlotNumber: `getSlotNumber` is not supported before the Amsterdam hard fork; see EIP-7843: https://eips.ethereum.org/EIPS/eip-7843" |
| 64 | + ); |
| 65 | + vm.getSlotNumber(); |
| 66 | + } |
| 67 | +} |
0 commit comments