Skip to content
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 support for variable-sized items in remainder-sized serializers #107

Merged
merged 1 commit into from
Jan 9, 2024

Conversation

lorisleiva
Copy link
Collaborator

No description provided.

Copy link

changeset-bot bot commented Jan 9, 2024

🦋 Changeset detected

Latest commit: 0bd676b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 21 packages
Name Type
@metaplex-foundation/umi-serializers Patch
@metaplex-foundation/umi Patch
@metaplex-foundation/umi-bundle-defaults Patch
@metaplex-foundation/umi-bundle-tests Patch
@metaplex-foundation/umi-downloader-http Patch
@metaplex-foundation/umi-eddsa-web3js Patch
@metaplex-foundation/umi-http-fetch Patch
@metaplex-foundation/umi-program-repository Patch
@metaplex-foundation/umi-rpc-chunk-get-accounts Patch
@metaplex-foundation/umi-rpc-web3js Patch
@metaplex-foundation/umi-serializer-beet Patch
@metaplex-foundation/umi-serializer-data-view Patch
@metaplex-foundation/umi-signer-derived Patch
@metaplex-foundation/umi-signer-wallet-adapters Patch
@metaplex-foundation/umi-storage-mock Patch
@metaplex-foundation/umi-transaction-factory-web3js Patch
@metaplex-foundation/umi-uploader-aws Patch
@metaplex-foundation/umi-uploader-bundlr Patch
@metaplex-foundation/umi-uploader-irys Patch
@metaplex-foundation/umi-uploader-nft-storage Patch
@metaplex-foundation/umi-web3js-adapters Patch

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

@lorisleiva lorisleiva merged commit ff727cb into main Jan 9, 2024
6 checks passed
@lorisleiva lorisleiva deleted the loris/remainder-size-with-variable-item branch January 9, 2024 16:54
@github-actions github-actions bot mentioned this pull request Jan 6, 2024
lorisleiva added a commit to solana-labs/solana-web3.js that referenced this pull request Jan 10, 2024
…nder-sized codecs (#2020)

This PR allows `Array`, `Map` and `Set` codecs of `"remainder"` size to have variable-size items (as [recently updated on Umi's serializers](metaplex-foundation/umi#107)).

These array-like codecs support the following three configurable size strategies:

- `{ size: 42 }` means a fixed-size array of 42 items.
- `{ size: getU32Codec() }` means the array is prefixed by a `u32` number storing its number of items.
- `{ size: "remainder" }` means the remainder of the bytes should be used to encode and/or decode the items inside the array.

Prior to this PR, we were enforcing arrays of `"remainder"` size to have fixed-size items. This allowed us to pre-compute the size of the array by dividing the size of what's left in the bytes by the fixed size of its item codec. If the modulo of that division was not zero, we would throw an error.

However, this restriction is not actually necessary and prevents valid use cases. This PR lifts this restriction and allows arrays of `"remainder"` size to have variable-size items. When decoding the bytes, instead of pre-computing the size of the array, we simply decode the rest of the bytes continuously until we've reached the end of the bytes. If anything goes wrong during that process, the item codec will throw an error as it won't get the expected number of bytes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant