-
Notifications
You must be signed in to change notification settings - Fork 12.4k
Add slot derivation library #4975
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
Merged
ernestognw
merged 14 commits into
OpenZeppelin:master
from
Amxx:features/slot-derivation
Mar 27, 2024
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
788a9c2
Add slot derivation library
Amxx 994dde2
unit tests for coverage
Amxx 5d87da3
update
Amxx d691ec9
Apply PR suggestions
ernestognw c07237c
Improve documentation
ernestognw 21bded7
remove erc1967Slot from SlotDerivation.sol
Amxx e28ecbc
update documentation
Amxx f004ab7
add note about storage checks
Amxx 7fa858b
more complete example
Amxx 3fd00ab
Update templates
ernestognw 70aa6b7
Merge branch 'master' into features/slot-derivation
ernestognw 0337d25
Generate
ernestognw f849734
re-enable using SlotDerivation in Arrays.sol for unsafeAccess
Amxx 3b60f2d
missing 'using for' statement
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 a library of methods for derivating common storage slots. |
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
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,161 @@ | ||
| // 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.]. | ||
| * | ||
| * Example usage: | ||
| * ```solidity | ||
| * contract Example { | ||
| * // Add the library methods | ||
| * using StorageSlot for bytes32; | ||
| * using SlotDerivation for bytes32; | ||
| * | ||
| * // Declare a namespace | ||
| * string private constant _NAMESPACE = "<namespace>" // eg. OpenZeppelin.Slot | ||
| * | ||
| * function setValueInNamespace(uint256 key, address newValue) internal { | ||
| * _NAMESPACE.erc7201Slot().deriveMapping(key).getAddressSlot().value = newValue; | ||
| * } | ||
| * | ||
| * function getValueInNamespace(uint256 key) internal view returns (address) { | ||
| * return _NAMESPACE.erc7201Slot().deriveMapping(key).getAddressSlot().value; | ||
| * } | ||
| * } | ||
| * ``` | ||
| * | ||
| * TIP: Consider using this library along with {StorageSlot}. | ||
| * | ||
| * NOTE: This library provides a way to manipulate storage locations in a non-standard way. Tooling for checking | ||
| * upgrade safety will ignore the slots accessed through this library. | ||
| */ | ||
| library SlotDerivation { | ||
| /** | ||
| * @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.