-
Notifications
You must be signed in to change notification settings - Fork 11.8k
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
Add string
and bytes
support to the StorageSlots
library
#4008
Conversation
🦋 Changeset detectedLatest commit: 26db62a The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
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.
Overall this is good. Minor comment below.
/** | ||
* @dev Returns an `StringSlot` representation of the string storage pointer `store`. | ||
*/ | ||
function getStringSlot(string storage store) internal pure returns (StringSlot storage r) { |
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.
contract Test {
struct MyStruct {
uint256 a;
bool b;
string c;
}
uint256 private constant KEY = uint256(keccak256("some.key.for.the.mapping"));
mapping(uint256 => MyStruct) private mymapping;
function getMyStruct() public view returns (uint256 slot, uint256 offset) {
MyStruct storage ptr = mymapping[KEY];
assembly {
slot := ptr.slot
offset := ptr.offset
}
}
function getMyStructString() public view returns (uint256 slot, uint256 offset) {
string storage ptr = mymapping[KEY].c;
assembly {
slot := ptr.slot
offset := ptr.offset
}
}
}
Offset is 0 in both cases,
slot for getMyStructString
is 2 more than getMyStruct
Co-authored-by: Francisco <[email protected]>
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 assume offset is zero. We've already asked anyway and should have confirmation soon.
String manipulation usefull for an upcoming ShortString library.
See #3969
PR Checklist
npx changeset add
)