|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity >=0.8.13 <0.9.0; |
| 3 | + |
| 4 | +/// @notice Index of a 128-slot storage page, as defined in MIP-8: `slot / 128`. See `Pages`. |
| 5 | +type PageIndex is uint256; |
| 6 | + |
| 7 | +using {Pages.add, Pages.slot, Pages.slotUnbounded} for PageIndex global; |
| 8 | + |
| 9 | +/// @notice Handle for a page. Declare it where the structure that uses the page lives. See `Pages.page`. |
| 10 | +struct PageHandle { |
| 11 | + uint256 _anchor; |
| 12 | +} |
| 13 | + |
| 14 | +using {Pages.page} for PageHandle global; |
| 15 | + |
| 16 | +/** |
| 17 | + * @title Pages |
| 18 | + * @notice Implements page and slot arithmetic for MIP-8 storage pages. |
| 19 | + * @dev MIP-8 groups 128 slots of 32 bytes into a 4 KiB page: |
| 20 | + * |
| 21 | + * page_index(slot) = slot / 128 |
| 22 | + * offset(slot) = slot % 128 |
| 23 | + * slot(page, offset) = page * 128 + offset |
| 24 | + * |
| 25 | + * MIP-8: https://github.com/monad-crypto/MIPs/blob/6e78a6ac39547882f9905fba86d2c794eb1768ef/MIPs/MIP-8.md |
| 26 | + * |
| 27 | + * Example usage: |
| 28 | + * ``` |
| 29 | + * PageHandle internal ledger; |
| 30 | + * |
| 31 | + * PageIndex page = ledger.page(); |
| 32 | + * Words.ref(page.slot(0)).value = total; |
| 33 | + * Words.ref(page.slotUnbounded(1 + i)).value = amount; |
| 34 | + * ``` |
| 35 | + */ |
| 36 | +library Pages { |
| 37 | + uint256 internal constant SLOTS_PER_PAGE = 128; |
| 38 | + |
| 39 | + /// @dev The offset passed to `slot` is 128 or more. |
| 40 | + error OffsetOutOfPage(uint256 offset); |
| 41 | + |
| 42 | + /** |
| 43 | + * @notice Returns the page of a handle. |
| 44 | + * @dev The page that contains `keccak256(abi.encode(slot))`, where `slot` is the storage slot of |
| 45 | + * `handle`, whether `handle` is a state variable or a member of a struct, array, or mapping. A |
| 46 | + * dynamic array declared in place of `handle` would start its data there, so distinct declarations |
| 47 | + * get distinct pages. Offset zero is the start of that page, not the hash itself, so a handle that |
| 48 | + * replaces an array does not alias the array's old elements. The page is derived from the slot of |
| 49 | + * `handle`, not the page around it, and the slot itself is never written. |
| 50 | + * @param handle Handle of the page. |
| 51 | + * @return The page. |
| 52 | + */ |
| 53 | + function page(PageHandle storage handle) internal pure returns (PageIndex) { |
| 54 | + uint256 anchor; |
| 55 | + assembly ("memory-safe") { |
| 56 | + anchor := handle.slot |
| 57 | + } |
| 58 | + return fromSlot(uint256(keccak256(abi.encode(anchor)))); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @notice Returns the page that contains a storage slot. |
| 63 | + * @param storageSlot Storage slot to locate. |
| 64 | + * @return The page that contains the slot. |
| 65 | + */ |
| 66 | + function fromSlot(uint256 storageSlot) internal pure returns (PageIndex) { |
| 67 | + return PageIndex.wrap(storageSlot / SLOTS_PER_PAGE); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @notice Returns the page located `pages` pages after a base page. |
| 72 | + * @param base Base page. |
| 73 | + * @param pages Number of pages to add. |
| 74 | + * @return The selected page. |
| 75 | + */ |
| 76 | + function add(PageIndex base, uint256 pages) internal pure returns (PageIndex) { |
| 77 | + return PageIndex.wrap(PageIndex.unwrap(base) + pages); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * @notice Returns the slot at an offset inside a page. |
| 82 | + * @dev Reverts with `OffsetOutOfPage` when `offset` is 128 or more. Use `slotUnbounded` to reach later pages. |
| 83 | + * @param base Page that holds the slot. |
| 84 | + * @param offset Position of the slot in the page, from zero through 127. |
| 85 | + * @return The storage slot. |
| 86 | + */ |
| 87 | + function slot(PageIndex base, uint256 offset) internal pure returns (uint256) { |
| 88 | + if (offset >= SLOTS_PER_PAGE) revert OffsetOutOfPage(offset); |
| 89 | + return slotUnbounded(base, offset); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @notice Returns the slot at `index`, counting from the first slot of a page, with no bound on `index`. |
| 94 | + * @dev Crosses into later pages when `index` is 128 or more. |
| 95 | + * @param base Page whose first slot is index zero. |
| 96 | + * @param index Number of slots after the first slot of the page. |
| 97 | + * @return The storage slot. |
| 98 | + */ |
| 99 | + function slotUnbounded(PageIndex base, uint256 index) internal pure returns (uint256) { |
| 100 | + return PageIndex.unwrap(base) * SLOTS_PER_PAGE + index; |
| 101 | + } |
| 102 | +} |
0 commit comments