-
Notifications
You must be signed in to change notification settings - Fork 11.9k
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
Add string
and bytes
support to the StorageSlots
library
#4008
Merged
frangio
merged 10 commits into
OpenZeppelin:master
from
Amxx:feature/storageslot/string-and-bytes
Feb 1, 2023
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c17b326
add string and bytes support to the StorageSlots library
Amxx 0148510
fix lint
Amxx ab66578
generate StorageSlot.sol procedurally
Amxx 1c0cfea
fix lint
Amxx 8a0ebab
minor refactor of StorageSlot template
Amxx 8496bc3
minor update
Amxx b960815
minimize change
Amxx ac13620
add changeset
Amxx 0690c19
Update scripts/generate/templates/StorageSlot.js
Amxx 26db62a
remove unecessary changeset
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 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 | ||
--- | ||
|
||
`StorageSlot`: Add support for `string` and `bytes`. |
This file contains 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 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 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 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,87 @@ | ||
const format = require('../format-lines'); | ||
const { capitalize, unique } = require('../../helpers'); | ||
|
||
const TYPES = [ | ||
{ type: 'address', isValueType: true, version: '4.1' }, | ||
{ type: 'bool', isValueType: true, name: 'Boolean', version: '4.1' }, | ||
{ type: 'bytes32', isValueType: true, version: '4.1' }, | ||
{ type: 'uint256', isValueType: true, version: '4.1' }, | ||
{ type: 'string', isValueType: false, version: '4.9' }, | ||
{ type: 'bytes', isValueType: false, version: '4.9' }, | ||
].map(type => Object.assign(type, { struct: (type.name ?? capitalize(type.type)) + 'Slot' })); | ||
|
||
const VERSIONS = unique(TYPES.map(t => t.version)).map( | ||
version => | ||
`_Available since v${version} for ${TYPES.filter(t => t.version == version) | ||
.map(t => `\`${t.type}\``) | ||
.join(', ')}._`, | ||
); | ||
|
||
const header = `\ | ||
pragma solidity ^0.8.0; | ||
|
||
/** | ||
* @dev Library for reading and writing primitive types to specific storage slots. | ||
* | ||
* Storage slots are often used to avoid storage conflict when dealing with upgradeable contracts. | ||
* This library helps with reading and writing to such slots without the need for inline assembly. | ||
* | ||
* The functions in this library return Slot structs that contain a \`value\` member that can be used to read or write. | ||
* | ||
* Example usage to set ERC1967 implementation slot: | ||
* \`\`\`solidity | ||
* contract ERC1967 { | ||
* bytes32 internal constant _IMPLEMENTATION_SLOT = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc; | ||
* | ||
* function _getImplementation() internal view returns (address) { | ||
* return StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value; | ||
* } | ||
* | ||
* function _setImplementation(address newImplementation) internal { | ||
* require(Address.isContract(newImplementation), "ERC1967: new implementation is not a contract"); | ||
* StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value = newImplementation; | ||
* } | ||
* } | ||
* \`\`\` | ||
* | ||
${VERSIONS.map(s => ` * ${s}`).join('\n')} | ||
*/ | ||
`; | ||
|
||
const struct = type => `\ | ||
struct ${type.struct} { | ||
${type.type} value; | ||
} | ||
`; | ||
|
||
const get = type => `\ | ||
/** | ||
* @dev Returns an \`${type.struct}\` with member \`value\` located at \`slot\`. | ||
*/ | ||
function get${type.struct}(bytes32 slot) internal pure returns (${type.struct} storage r) { | ||
/// @solidity memory-safe-assembly | ||
assembly { | ||
r.slot := slot | ||
} | ||
} | ||
`; | ||
|
||
const getStorage = type => `\ | ||
/** | ||
* @dev Returns an \`${type.struct}\` representation of the ${type.type} storage pointer \`store\`. | ||
*/ | ||
function get${type.struct}(${type.type} storage store) internal pure returns (${type.struct} storage r) { | ||
/// @solidity memory-safe-assembly | ||
assembly { | ||
r.slot := store.slot | ||
} | ||
} | ||
`; | ||
|
||
// GENERATE | ||
module.exports = format( | ||
header.trimEnd(), | ||
'library StorageSlot {', | ||
[...TYPES.map(struct), ...TYPES.flatMap(type => [get(type), type.isValueType ? '' : getStorage(type)])], | ||
'}', | ||
); |
This file contains 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 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can make the assumption that a string will always have offset zero. But I would consider adding an assertion/require just to make sure. Do you know what I mean?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Storage pointers have an
.offset
field in addition to the.slot
field. The offset indicates where in the slot the value begins. It can be non-zero when multiple values are packed in a slot. What I'm saying is that for a string pointer we can probably assume offset 0 but it's something we need to be aware of.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was not able to create a storage string pointer with an offset.
Offset is 0 in both cases,
slot for
getMyStructString
is 2 more thangetMyStruct