-
Notifications
You must be signed in to change notification settings - Fork 12.4k
StorageSlot library : add storage slot derivation tooling and UDVT typed slot representation. #4965
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 14 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
d30144b
Add storage slot derivation tooling and UDVT typed slot representation.
Amxx e282a33
generate fuzzing tests
Amxx 8705253
update hardhat to support cancun
Amxx f6bc398
fix cassing
Amxx 45a7048
update hardhat-ignore-warnings
Amxx 1ac21fd
fix hardhat-expose exclusion
Amxx a13c2bb
fix test
Amxx 3f96b4c
re-enable variant testing for mapping key
Amxx c592c26
add support for string and bytes keys in mapping derivation
Amxx 7c5b4c6
apply recommandations for PR
Amxx 88b3c19
divide into 3 libraries
Amxx 97b3048
Update .changeset/kind-planets-cough.md
Amxx 38f3143
add changeset
Amxx c1a83f1
min diff
Amxx b6ff468
Update scripts/generate/templates/StorageSlot.js
Amxx f19086c
fix lint
Amxx 3c11e1d
move UDVT to types/TypedSlot.sol
Amxx 24a584b
changeset
Amxx 4676af3
Update .changeset/kind-planets-cough.md
Amxx cd62c3d
doc
Amxx 921a46d
Merge branch 'features/expended-storage-slot' of https://github.com/A…
Amxx 2cd2c38
codespell
Amxx 6445973
update
Amxx 40c4943
rename
Amxx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| 'openzeppelin-solidity': minor | ||
| --- | ||
|
|
||
| `SlotDerivation`: Add library for storage slot derivation. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| 'openzeppelin-solidity': minor | ||
| --- | ||
|
|
||
| `TransientSlot`: Add library for typed-slot representation with transient storage support. | ||
Amxx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| // This file was procedurally generated from scripts/generate/templates/SlotDerivation.js. | ||
|
|
||
| pragma solidity ^0.8.20; | ||
|
|
||
| /** | ||
| * @dev Library for computing storage (and transient storage) locations from namespaces and deriving slots | ||
| * corresponding to standard patterns. The derivation method for array and mapping matches the storage layout used by | ||
| * the solidity language / compiler. | ||
| * | ||
| * See https://docs.soliditylang.org/en/v0.8.20/internals/layout_in_storage.html#mappings-and-dynamic-arrays[Solidity docs for mappings and dynamic arrays.]. | ||
| */ | ||
| library SlotDerivation { | ||
| /** | ||
| * @dev Derive an ERC-1967 slot from a string (namespace). | ||
| */ | ||
| function erc1967slot(string memory namespace) internal pure returns (bytes32 slot) { | ||
| /// @solidity memory-safe-assembly | ||
| assembly { | ||
| slot := sub(keccak256(add(namespace, 0x20), mload(namespace)), 1) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @dev Derive an ERC-7201 slot from a string (namespace). | ||
| */ | ||
| function erc7201slot(string memory namespace) internal pure returns (bytes32 slot) { | ||
| /// @solidity memory-safe-assembly | ||
| assembly { | ||
| mstore(0x00, sub(keccak256(add(namespace, 0x20), mload(namespace)), 1)) | ||
| slot := and(keccak256(0x00, 0x20), not(0xff)) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @dev Add an offset to a slot to get the n-th element of a structure or an array. | ||
| */ | ||
| function offset(bytes32 slot, uint256 pos) internal pure returns (bytes32 result) { | ||
| unchecked { | ||
| return bytes32(uint256(slot) + pos); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @dev Derive the location of the first element in an array from the slot where the length is stored. | ||
| */ | ||
| function deriveArray(bytes32 slot) internal pure returns (bytes32 result) { | ||
| /// @solidity memory-safe-assembly | ||
| assembly { | ||
| mstore(0x00, slot) | ||
| result := keccak256(0x00, 0x20) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @dev Derive the location of a mapping element from the key. | ||
| */ | ||
| function deriveMapping(bytes32 slot, address key) internal pure returns (bytes32 result) { | ||
| /// @solidity memory-safe-assembly | ||
| assembly { | ||
| mstore(0x00, key) | ||
| mstore(0x20, slot) | ||
| result := keccak256(0x00, 0x40) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @dev Derive the location of a mapping element from the key. | ||
| */ | ||
| function deriveMapping(bytes32 slot, bool key) internal pure returns (bytes32 result) { | ||
| /// @solidity memory-safe-assembly | ||
| assembly { | ||
| mstore(0x00, key) | ||
| mstore(0x20, slot) | ||
| result := keccak256(0x00, 0x40) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @dev Derive the location of a mapping element from the key. | ||
| */ | ||
| function deriveMapping(bytes32 slot, bytes32 key) internal pure returns (bytes32 result) { | ||
| /// @solidity memory-safe-assembly | ||
| assembly { | ||
| mstore(0x00, key) | ||
| mstore(0x20, slot) | ||
| result := keccak256(0x00, 0x40) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @dev Derive the location of a mapping element from the key. | ||
| */ | ||
| function deriveMapping(bytes32 slot, uint256 key) internal pure returns (bytes32 result) { | ||
| /// @solidity memory-safe-assembly | ||
| assembly { | ||
| mstore(0x00, key) | ||
| mstore(0x20, slot) | ||
| result := keccak256(0x00, 0x40) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @dev Derive the location of a mapping element from the key. | ||
| */ | ||
| function deriveMapping(bytes32 slot, int256 key) internal pure returns (bytes32 result) { | ||
| /// @solidity memory-safe-assembly | ||
| assembly { | ||
| mstore(0x00, key) | ||
| mstore(0x20, slot) | ||
| result := keccak256(0x00, 0x40) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @dev Derive the location of a mapping element from the key. | ||
| */ | ||
| function deriveMapping(bytes32 slot, string memory key) internal pure returns (bytes32 result) { | ||
| /// @solidity memory-safe-assembly | ||
| assembly { | ||
| let length := mload(key) | ||
| let begin := add(key, 0x20) | ||
| let end := add(begin, length) | ||
| let cache := mload(end) | ||
| mstore(end, slot) | ||
| result := keccak256(begin, add(length, 0x20)) | ||
| mstore(end, cache) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @dev Derive the location of a mapping element from the key. | ||
| */ | ||
| function deriveMapping(bytes32 slot, bytes memory key) internal pure returns (bytes32 result) { | ||
| /// @solidity memory-safe-assembly | ||
| assembly { | ||
| let length := mload(key) | ||
| let begin := add(key, 0x20) | ||
| let end := add(begin, length) | ||
| let cache := mload(end) | ||
| mstore(end, slot) | ||
| result := keccak256(begin, add(length, 0x20)) | ||
| mstore(end, cache) | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.