Skip to content

Commit

Permalink
Implement VoteInstruction::AuthorizeWithSeed & `VoteInstruction::Au…
Browse files Browse the repository at this point in the history
…thorizeWithSeedChecked` (#25928)

* [vote_authorize_with_seed] Add `VoteInstruction::AuthorizeWithSeed`

* [vote_authorize_with_seed] You can now update a vote account's authority if it's a derived key for which you control the base key

* [vote_authorize_with_seed] Add test helper to create a vote account whose authorities are derived keys

* [vote_authorize_with_seed] Write tests to assert the behavior of `VoteInstruction::AuthorizeWithSeed`

* [vote_authorize_with_seed] Feature gate the `VoteInstruction::AuthorizeWithSeed` processor

* [vote_authorize_with_seed] Add `VoteInstruction::AuthorizeWithSeed` to transaction status parser

* [vote_authorize_with_seed] Add `VoteInstruction::AuthorizeWithSeed` to docs

* [vote_authorize_with_seed] Add `VoteInstruction::AuthorizeCheckedWithSeed`

* [vote_authorize_with_seed] You can now update a vote account's authority (while checking that the new authority has signed) if it's a derived
key for which you control the base key

* [vote_authorize_with_seed] Add `VoteInstruction::AuthorizeCheckedWithSeed` to transaction status parser

* [vote_authorize_with_seed] Write tests to assert the behavior of `VoteInstruction::AuthorizeCheckedWithSeed`
  • Loading branch information
steveluscher authored Jun 14, 2022
1 parent 7b786ff commit 45d11f3
Show file tree
Hide file tree
Showing 7 changed files with 870 additions and 5 deletions.
7 changes: 7 additions & 0 deletions docs/src/cluster/stake-delegation-and-rewards.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ VoteState is the current state of all the votes the validator has submitted to t

Updates the account with a new authorized voter or withdrawer, according to the VoteAuthorize parameter \(`Voter` or `Withdrawer`\). The transaction must be signed by the Vote account's current `authorized_voter` or `authorized_withdrawer`.

- `account[0]` - RW - The VoteState.
`VoteState::authorized_voter` or `authorized_withdrawer` is set to `Pubkey`.

### VoteInstruction::AuthorizeWithSeed\(VoteAuthorizeWithSeedArgs\)

Updates the account with a new authorized voter or withdrawer, according to the VoteAuthorize parameter \(`Voter` or `Withdrawer`\). Unlike `VoteInstruction::Authorize` this instruction is for use when the Vote account's current `authorized_voter` or `authorized_withdrawer` is a derived key. The transaction must be signed by someone who can sign for the base key of that derived key.

- `account[0]` - RW - The VoteState.
`VoteState::authorized_voter` or `authorized_withdrawer` is set to `Pubkey`.

Expand Down
81 changes: 80 additions & 1 deletion programs/vote/src/vote_instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
use {
crate::{
id,
vote_state::{Vote, VoteAuthorize, VoteInit, VoteState, VoteStateUpdate},
vote_state::{
Vote, VoteAuthorize, VoteAuthorizeCheckedWithSeedArgs, VoteAuthorizeWithSeedArgs,
VoteInit, VoteState, VoteStateUpdate,
},
},
serde_derive::{Deserialize, Serialize},
solana_sdk::{
Expand Down Expand Up @@ -99,6 +102,30 @@ pub enum VoteInstruction {
/// 0. `[Write]` Vote account to vote with
/// 1. `[SIGNER]` Vote authority
UpdateVoteStateSwitch(VoteStateUpdate, Hash),

/// Given that the current Voter or Withdrawer authority is a derived key,
/// this instruction allows someone who can sign for that derived key's
/// base key to authorize a new Voter or Withdrawer for a vote account.
///
/// # Account references
/// 0. `[Write]` Vote account to be updated
/// 1. `[]` Clock sysvar
/// 2. `[SIGNER]` Base key of current Voter or Withdrawer authority's derived key
AuthorizeWithSeed(VoteAuthorizeWithSeedArgs),

/// Given that the current Voter or Withdrawer authority is a derived key,
/// this instruction allows someone who can sign for that derived key's
/// base key to authorize a new Voter or Withdrawer for a vote account.
///
/// This instruction behaves like `AuthorizeWithSeed` with the additional requirement
/// that the new vote or withdraw authority must also be a signer.
///
/// # Account references
/// 0. `[Write]` Vote account to be updated
/// 1. `[]` Clock sysvar
/// 2. `[SIGNER]` Base key of current Voter or Withdrawer authority's derived key
/// 3. `[SIGNER]` New vote or withdraw authority
AuthorizeCheckedWithSeed(VoteAuthorizeCheckedWithSeedArgs),
}

fn initialize_account(vote_pubkey: &Pubkey, vote_init: &VoteInit) -> Instruction {
Expand Down Expand Up @@ -190,6 +217,58 @@ pub fn authorize_checked(
)
}

pub fn authorize_with_seed(
vote_pubkey: &Pubkey,
current_authority_base_key: &Pubkey,
current_authority_derived_key_owner: &Pubkey,
current_authority_derived_key_seed: &str,
new_authority: &Pubkey,
authorization_type: VoteAuthorize,
) -> Instruction {
let account_metas = vec![
AccountMeta::new(*vote_pubkey, false),
AccountMeta::new_readonly(sysvar::clock::id(), false),
AccountMeta::new_readonly(*current_authority_base_key, true),
];

Instruction::new_with_bincode(
id(),
&VoteInstruction::AuthorizeWithSeed(VoteAuthorizeWithSeedArgs {
authorization_type,
current_authority_derived_key_owner: *current_authority_derived_key_owner,
current_authority_derived_key_seed: current_authority_derived_key_seed.to_string(),
new_authority: *new_authority,
}),
account_metas,
)
}

pub fn authorize_checked_with_seed(
vote_pubkey: &Pubkey,
current_authority_base_key: &Pubkey,
current_authority_derived_key_owner: &Pubkey,
current_authority_derived_key_seed: &str,
new_authority: &Pubkey,
authorization_type: VoteAuthorize,
) -> Instruction {
let account_metas = vec![
AccountMeta::new(*vote_pubkey, false),
AccountMeta::new_readonly(sysvar::clock::id(), false),
AccountMeta::new_readonly(*current_authority_base_key, true),
AccountMeta::new_readonly(*new_authority, true),
];

Instruction::new_with_bincode(
id(),
&VoteInstruction::AuthorizeCheckedWithSeed(VoteAuthorizeCheckedWithSeedArgs {
authorization_type,
current_authority_derived_key_owner: *current_authority_derived_key_owner,
current_authority_derived_key_seed: current_authority_derived_key_seed.to_string(),
}),
account_metas,
)
}

pub fn update_validator_identity(
vote_pubkey: &Pubkey,
authorized_withdrawer_pubkey: &Pubkey,
Expand Down
Loading

0 comments on commit 45d11f3

Please sign in to comment.