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

refactor(experimental): add getConstantCodec to @solana/codecs-data-structures #2400

Merged
merged 1 commit into from
Apr 2, 2024

Conversation

lorisleiva
Copy link
Collaborator

@lorisleiva lorisleiva commented Apr 1, 2024

This PR adds two new helpers: containsBytes and getConstantCodec.

The containsBytes helper checks if a Uint8Array contains another Uint8Array at a given offset.

containsBytes(new Uint8Array([1, 2, 3, 4]), new Uint8Array([2, 3]), 1); // true
containsBytes(new Uint8Array([1, 2, 3, 4]), new Uint8Array([2, 3]), 2); // false

The getConstantCodec function accepts any Uint8Array and returns a Codec<void>. When encoding, it will set the provided Uint8Array as-is. When decoding, it will assert that the next bytes contain the provided Uint8Array and move the offset forward.

const codec = getConstantCodec(new Uint8Array([1, 2, 3]));

codec.encode(undefined); // 0x010203
codec.decode(new Uint8Array([1, 2, 3])); // undefined
codec.decode(new Uint8Array([1, 2, 4])); // Throws an error.

Copy link

changeset-bot bot commented Apr 1, 2024

🦋 Changeset detected

Latest commit: 2e26dc2

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

This PR includes changesets to release 35 packages
Name Type
@solana/codecs-data-structures Patch
@solana/codecs-core Patch
@solana/errors Patch
@solana/codecs Patch
@solana/transactions Patch
@solana/accounts Patch
@solana/addresses Patch
@solana/codecs-numbers Patch
@solana/codecs-strings Patch
@solana/keys Patch
@solana/options Patch
@solana/rpc-api Patch
@solana/rpc-types Patch
@solana/assertions Patch
@solana/compat Patch
@solana/instructions Patch
@solana/web3.js-experimental Patch
@solana/rpc-spec Patch
@solana/rpc-subscriptions-spec Patch
@solana/rpc-subscriptions-transport-websocket Patch
@solana/rpc-subscriptions Patch
@solana/rpc-transport-http Patch
@solana/rpc Patch
@solana/signers Patch
@solana/sysvars Patch
@solana/transaction-confirmation Patch
@solana/web3.js-legacy-sham Patch
@solana/programs Patch
@solana/rpc-graphql Patch
@solana/rpc-subscriptions-api Patch
@solana/rpc-parsed-types Patch
@solana/rpc-transformers Patch
@solana/functional Patch
@solana/rpc-spec-types Patch
@solana/webcrypto-ed25519-polyfill 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

Copy link
Collaborator Author

lorisleiva commented Apr 1, 2024

* Returns true if and only if the provided `data` byte array contains
* the provided `bytes` byte array at the specified `offset`.
*/
export function containsBytes(data: Uint8Array, bytes: Uint8Array, offset: number): boolean {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will return true if both data and bytes are of zero length. I don't know how you feel about that.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does nothing not contain nothing? 😅

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added via Giphy

Copy link
Collaborator

@steveluscher steveluscher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added via Giphy

Copy link
Collaborator Author

lorisleiva commented Apr 2, 2024

Merge activity

  • Apr 2, 6:48 PM EDT: @lorisleiva started a stack merge that includes this pull request via Graphite.
  • Apr 2, 6:58 PM EDT: The Graphite merge of this pull request was cancelled.
  • Apr 2, 6:58 PM EDT: @lorisleiva started a stack merge that includes this pull request via Graphite.
  • Apr 2, 6:59 PM EDT: Graphite rebased this pull request as part of a merge.
  • Apr 2, 7:01 PM EDT: @lorisleiva merged this pull request with Graphite.

@lorisleiva lorisleiva force-pushed the loris/refactor-discriminated-union-codec branch from 1b06158 to 8b0af35 Compare April 2, 2024 22:56
Base automatically changed from loris/refactor-discriminated-union-codec to master April 2, 2024 22:58
@lorisleiva lorisleiva merged commit ebb03cd into master Apr 2, 2024
7 checks passed
@lorisleiva lorisleiva deleted the loris/constant-codec branch April 2, 2024 23:01
Copy link
Contributor

github-actions bot commented Apr 3, 2024

🎉 This PR is included in version 1.91.4 🎉

The release is available on:

Your semantic-release bot 📦🚀

Copy link
Contributor

Because there has been no activity on this PR for 14 days since it was merged, it has been automatically locked. Please open a new issue if it requires a follow up.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants