Add string and bytes support to the StorageSlots library#4008
Add string and bytes support to the StorageSlots library#4008frangio merged 10 commits intoOpenZeppelin:masterfrom
string and bytes support to the StorageSlots library#4008Conversation
🦋 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 |
frangio
left a comment
There was a problem hiding this comment.
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.
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.
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.
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 <frangio.1@gmail.com>
frangio
left a comment
There was a problem hiding this comment.
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)