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

improve: add support of compactUpdateVoteState instruction #2704

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

EugeneButusov
Copy link

No description provided.

Copy link

changeset-bot bot commented May 10, 2024

⚠️ No Changeset found

Latest commit: 63f2d98

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@mergify mergify bot added the community label May 10, 2024
@mergify mergify bot requested a review from a team May 10, 2024 07:46
voteAccount: instruction.keys[0].pubkey,
voteAuthority: instruction.keys[1].pubkey,
voteStateUpdate: {
lockouts: lockoutOffsets.map(
Copy link
Author

Choose a reason for hiding this comment

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

this transformation is made to be compliant with the output provided by api.mainnet-beta.solana.com, since node returns lockouts, while instruction for "compact" contains offset values.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I see what you're going for here. For anyone else reading, this is required due to the custom Serde serialization method used for CompactUpdateVoteState, ie. serde_compact_vote_state_update.

Since this is required at a serialization level, and you'll use this twice (here and in the transaction function), why not roll this into a custom serialization layout?

You can define a class MyLayout extends Layout<T> and implement the required methods that can transform from the instruction input type to the proper byte layout, and back.

We did this in SPL Token JS to mimic Rust's COption::<Pubkey> and you can use that as an example.
https://github.com/solana-labs/solana-program-library/blob/d1c03cc6164ebdeebef19190de28b7a71f4e4cb6/token/js/src/serialization.ts#L5-L39

Copy link
Collaborator

@buffalojoec buffalojoec left a comment

Choose a reason for hiding this comment

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

Thanks for adding! I left a few comments on this.

@@ -96,6 +97,17 @@ export type UpdateValidatorIdentityParams = {
nodePubkey: PublicKey;
};

export type CompactUpdateVoteStateParams = {
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: can you document this type, like the other parameter types?

@@ -96,6 +97,17 @@ export type UpdateValidatorIdentityParams = {
nodePubkey: PublicKey;
};

export type CompactUpdateVoteStateParams = {
voteAccount: PublicKey,
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: Just to follow the pattern of the others:

Suggested change
voteAccount: PublicKey,
votePubkey: PublicKey,

voteStateUpdate: {
lockouts: Lockout[],
root: number,
hash: PublicKey,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Unfortunately, the legacy library doesn't have an opaque type for a hash, but it looks like the rest of the package uses string. It definitely can't be PublicKey here, for a few reasons.

However, when it comes to decoding, you can use the publicKey layout, since it will perform base58 decoding and assert the resulting length is 32 bytes.

@@ -342,6 +402,23 @@ const VOTE_INSTRUCTION_LAYOUTS = Object.freeze<{
Layout.voteAuthorizeWithSeedArgs(),
]),
},
CompactUpdateVoteState: {
index: 12,
layout: BufferLayout.struct<VoteInstructionInputData['CompactUpdateVoteState']>([
Copy link
Collaborator

Choose a reason for hiding this comment

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

You can add this whole layout to layout.ts and then just call Layout.voteCompactUpdateVoteState() here.


const keys = [
{pubkey: voteAccount, isSigner: false, isWritable: true},
{pubkey: voteAuthority, isSigner: true, isWritable: true},
Copy link
Collaborator

Choose a reason for hiding this comment

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

Docs on this instruction only declare the authority as signer, not writable.

voteAccount: instruction.keys[0].pubkey,
voteAuthority: instruction.keys[1].pubkey,
voteStateUpdate: {
lockouts: lockoutOffsets.map(
Copy link
Collaborator

Choose a reason for hiding this comment

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

I see what you're going for here. For anyone else reading, this is required due to the custom Serde serialization method used for CompactUpdateVoteState, ie. serde_compact_vote_state_update.

Since this is required at a serialization level, and you'll use this twice (here and in the transaction function), why not roll this into a custom serialization layout?

You can define a class MyLayout extends Layout<T> and implement the required methods that can transform from the instruction input type to the proper byte layout, and back.

We did this in SPL Token JS to mimic Rust's COption::<Pubkey> and you can use that as an example.
https://github.com/solana-labs/solana-program-library/blob/d1c03cc6164ebdeebef19190de28b7a71f4e4cb6/token/js/src/serialization.ts#L5-L39

@EugeneButusov
Copy link
Author

@buffalojoec, thank you for the review, I'll handle the feedback in the closest days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants