Skip to content

Commit

Permalink
Rework create validator stake account (#1539)
Browse files Browse the repository at this point in the history
  • Loading branch information
joncinque authored Mar 30, 2021
1 parent f2de73d commit 9f38a6f
Show file tree
Hide file tree
Showing 10 changed files with 379 additions and 572 deletions.
16 changes: 6 additions & 10 deletions stake-pool/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,21 +277,21 @@ fn command_vsa_create(config: &Config, pool: &Pubkey, vote_account: &Pubkey) ->
create_validator_stake_account(
&spl_stake_pool::id(),
&pool,
&config.owner.pubkey(),
&config.fee_payer.pubkey(),
&stake_account,
&vote_account,
&config.owner.pubkey(),
&config.owner.pubkey(),
&solana_program::system_program::id(),
&stake_program_id(),
)?,
],
Some(&config.fee_payer.pubkey()),
);

let (recent_blockhash, fee_calculator) = config.rpc_client.get_recent_blockhash()?;
check_fee_payer_balance(config, fee_calculator.calculate_fee(&transaction.message()))?;
transaction.sign(&[config.fee_payer.as_ref()], recent_blockhash);
transaction.sign(
&[config.fee_payer.as_ref(), config.owner.as_ref()],
recent_blockhash,
);
send_transaction(&config, transaction)?;
Ok(())
}
Expand Down Expand Up @@ -377,7 +377,6 @@ fn command_vsa_add(
&token_receiver,
&pool_data.pool_mint,
&spl_token::id(),
&stake_program_id(),
)?,
]);

Expand Down Expand Up @@ -468,7 +467,6 @@ fn command_vsa_remove(
&withdraw_from,
&pool_data.pool_mint,
&spl_token::id(),
&stake_program_id(),
)?,
],
Some(&config.fee_payer.pubkey()),
Expand Down Expand Up @@ -645,7 +643,6 @@ fn command_deposit(
&pool_data.owner_fee_account,
&pool_data.pool_mint,
&spl_token::id(),
&stake_program_id(),
)?,
]);

Expand Down Expand Up @@ -984,7 +981,6 @@ fn command_withdraw(
&withdraw_from,
&pool_data.pool_mint,
&spl_token::id(),
&stake_program_id(),
withdraw_stake.pool_amount,
)?);
}
Expand Down Expand Up @@ -1163,7 +1159,7 @@ fn main() {
.help("Max number of validators included in the stake pool"),
)
)
.subcommand(SubCommand::with_name("create-validator-stake").about("Create a new stake account to use with the pool")
.subcommand(SubCommand::with_name("create-validator-stake").about("Create a new stake account to use with the pool. Must be signed by the pool owner.")
.arg(
Arg::with_name("pool")
.index(1)
Expand Down
52 changes: 24 additions & 28 deletions stake-pool/program/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
#![allow(clippy::too_many_arguments)]

use {
crate::stake,
borsh::{BorshDeserialize, BorshSchema, BorshSerialize},
solana_program::{
instruction::{AccountMeta, Instruction},
program_error::ProgramError,
pubkey::Pubkey,
sysvar,
system_program, sysvar,
},
};

Expand Down Expand Up @@ -48,17 +49,17 @@ pub enum StakePoolInstruction {
/// Creates new program account for accumulating stakes for a particular validator
///
/// 0. `[]` Stake pool account this stake will belong to
/// 1. `[ws]` Funding account (must be a system account)
/// 2. `[w]` Stake account to be created
/// 3. `[]` Validator this stake account will vote for
/// 4. `[]` Stake authority for the new stake account
/// 5. `[]` Withdraw authority for the new stake account
/// 6. `[]` Rent sysvar
/// 7. `[]` System program
/// 8. `[]` Stake program
/// 1. `[s]` Owner
/// 2. `[ws]` Funding account (must be a system account)
/// 3. `[w]` Stake account to be created
/// 4. `[]` Validator this stake account will vote for
/// 5. `[]` Rent sysvar
/// 6. `[]` System program
/// 7. `[]` Stake program
CreateValidatorStakeAccount,

/// Adds validator stake account to the pool
/// Adds stake account delegated to validator to the pool's list of
/// managed validators
///
/// 0. `[w]` Stake pool
/// 1. `[s]` Owner
Expand Down Expand Up @@ -185,24 +186,23 @@ pub fn initialize(
pub fn create_validator_stake_account(
program_id: &Pubkey,
stake_pool: &Pubkey,
owner: &Pubkey,
funder: &Pubkey,
stake_account: &Pubkey,
validator: &Pubkey,
stake_authority: &Pubkey,
withdraw_authority: &Pubkey,
system_program_id: &Pubkey,
stake_program_id: &Pubkey,
) -> Result<Instruction, ProgramError> {
let accounts = vec![
AccountMeta::new_readonly(*stake_pool, false),
AccountMeta::new_readonly(*owner, true),
AccountMeta::new(*funder, true),
AccountMeta::new(*stake_account, false),
AccountMeta::new_readonly(*validator, false),
AccountMeta::new_readonly(*stake_authority, false),
AccountMeta::new_readonly(*withdraw_authority, false),
AccountMeta::new_readonly(sysvar::rent::id(), false),
AccountMeta::new_readonly(*system_program_id, false),
AccountMeta::new_readonly(*stake_program_id, false),
AccountMeta::new_readonly(sysvar::clock::id(), false),
AccountMeta::new_readonly(sysvar::stake_history::id(), false),
AccountMeta::new_readonly(stake::config_id(), false),
AccountMeta::new_readonly(system_program::id(), false),
AccountMeta::new_readonly(stake::id(), false),
];
Ok(Instruction {
program_id: *program_id,
Expand All @@ -220,10 +220,9 @@ pub fn add_validator_to_pool(
stake_pool_withdraw: &Pubkey,
validator_list: &Pubkey,
stake_account: &Pubkey,
pool_tokens_to: &Pubkey,
pool_token_receiver: &Pubkey,
pool_mint: &Pubkey,
token_program_id: &Pubkey,
stake_program_id: &Pubkey,
) -> Result<Instruction, ProgramError> {
let accounts = vec![
AccountMeta::new(*stake_pool, false),
Expand All @@ -232,12 +231,12 @@ pub fn add_validator_to_pool(
AccountMeta::new_readonly(*stake_pool_withdraw, false),
AccountMeta::new(*validator_list, false),
AccountMeta::new(*stake_account, false),
AccountMeta::new(*pool_tokens_to, false),
AccountMeta::new(*pool_token_receiver, false),
AccountMeta::new(*pool_mint, false),
AccountMeta::new_readonly(sysvar::clock::id(), false),
AccountMeta::new_readonly(sysvar::stake_history::id(), false),
AccountMeta::new_readonly(*token_program_id, false),
AccountMeta::new_readonly(*stake_program_id, false),
AccountMeta::new_readonly(stake::id(), false),
];
Ok(Instruction {
program_id: *program_id,
Expand All @@ -258,7 +257,6 @@ pub fn remove_validator_from_pool(
burn_from: &Pubkey,
pool_mint: &Pubkey,
token_program_id: &Pubkey,
stake_program_id: &Pubkey,
) -> Result<Instruction, ProgramError> {
let accounts = vec![
AccountMeta::new(*stake_pool, false),
Expand All @@ -271,7 +269,7 @@ pub fn remove_validator_from_pool(
AccountMeta::new(*pool_mint, false),
AccountMeta::new_readonly(sysvar::clock::id(), false),
AccountMeta::new_readonly(*token_program_id, false),
AccountMeta::new_readonly(*stake_program_id, false),
AccountMeta::new_readonly(stake::id(), false),
];
Ok(Instruction {
program_id: *program_id,
Expand Down Expand Up @@ -330,7 +328,6 @@ pub fn deposit(
pool_fee_to: &Pubkey,
pool_mint: &Pubkey,
token_program_id: &Pubkey,
stake_program_id: &Pubkey,
) -> Result<Instruction, ProgramError> {
let accounts = vec![
AccountMeta::new(*stake_pool, false),
Expand All @@ -345,7 +342,7 @@ pub fn deposit(
AccountMeta::new_readonly(sysvar::clock::id(), false),
AccountMeta::new_readonly(sysvar::stake_history::id(), false),
AccountMeta::new_readonly(*token_program_id, false),
AccountMeta::new_readonly(*stake_program_id, false),
AccountMeta::new_readonly(stake::id(), false),
];
Ok(Instruction {
program_id: *program_id,
Expand All @@ -366,7 +363,6 @@ pub fn withdraw(
burn_from: &Pubkey,
pool_mint: &Pubkey,
token_program_id: &Pubkey,
stake_program_id: &Pubkey,
amount: u64,
) -> Result<Instruction, ProgramError> {
let accounts = vec![
Expand All @@ -380,7 +376,7 @@ pub fn withdraw(
AccountMeta::new(*pool_mint, false),
AccountMeta::new_readonly(sysvar::clock::id(), false),
AccountMeta::new_readonly(*token_program_id, false),
AccountMeta::new_readonly(*stake_program_id, false),
AccountMeta::new_readonly(stake::id(), false),
];
Ok(Instruction {
program_id: *program_id,
Expand Down
42 changes: 36 additions & 6 deletions stake-pool/program/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,24 +403,38 @@ impl Processor {
let account_info_iter = &mut accounts.iter();
// Stake pool account
let stake_pool_info = next_account_info(account_info_iter)?;
// Owner account
let owner_info = next_account_info(account_info_iter)?;
// Account creation funder account
let funder_info = next_account_info(account_info_iter)?;
// Stake account to be created
let stake_account_info = next_account_info(account_info_iter)?;
// Validator this stake account will vote for
let validator_info = next_account_info(account_info_iter)?;
// Stake authority for the new stake account
let stake_authority_info = next_account_info(account_info_iter)?;
// Withdraw authority for the new stake account
let withdraw_authority_info = next_account_info(account_info_iter)?;
// Rent sysvar account
let rent_info = next_account_info(account_info_iter)?;
let rent = &Rent::from_account_info(rent_info)?;
// Clock sysvar account
let clock_info = next_account_info(account_info_iter)?;
// Stake history sysvar account
let stake_history_info = next_account_info(account_info_iter)?;
// Stake config sysvar account
let stake_config_info = next_account_info(account_info_iter)?;
// System program id
let system_program_info = next_account_info(account_info_iter)?;
// Staking program id
let stake_program_info = next_account_info(account_info_iter)?;

// Get stake pool stake (and check if it is initialized)
if stake_pool_info.owner != program_id {
return Err(ProgramError::IncorrectProgramId);
}
let stake_pool = StakePool::try_from_slice(&stake_pool_info.data.borrow())?;
if !stake_pool.is_valid() {
return Err(StakePoolError::InvalidState.into());
}
stake_pool.check_owner(owner_info)?;

// Check program ids
if *system_program_info.key != solana_program::system_program::id() {
return Err(ProgramError::IncorrectProgramId);
Expand Down Expand Up @@ -466,8 +480,8 @@ impl Processor {
&stake::initialize(
&stake_account_info.key,
&stake::Authorized {
staker: *stake_authority_info.key,
withdrawer: *withdraw_authority_info.key,
staker: *owner_info.key,
withdrawer: *owner_info.key,
},
&stake::Lockup::default(),
),
Expand All @@ -476,6 +490,22 @@ impl Processor {
rent_info.clone(),
stake_program_info.clone(),
],
)?;

invoke(
&stake::delegate_stake(
&stake_account_info.key,
&owner_info.key,
&validator_info.key,
),
&[
stake_account_info.clone(),
validator_info.clone(),
clock_info.clone(),
stake_history_info.clone(),
stake_config_info.clone(),
owner_info.clone(),
],
)
}

Expand Down
16 changes: 15 additions & 1 deletion stake-pool/program/src/stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ use std::str::FromStr;
solana_program::declare_id!("Stake11111111111111111111111111111111111111");

const STAKE_CONFIG: &str = "StakeConfig11111111111111111111111111111111";
/// Id for stake config account
pub fn config_id() -> Pubkey {
Pubkey::from_str(STAKE_CONFIG).unwrap()
}

/// FIXME copied from solana stake program
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
Expand Down Expand Up @@ -489,8 +493,18 @@ pub fn delegate_stake(
AccountMeta::new_readonly(*vote_pubkey, false),
AccountMeta::new_readonly(sysvar::clock::id(), false),
AccountMeta::new_readonly(sysvar::stake_history::id(), false),
AccountMeta::new_readonly(Pubkey::from_str(STAKE_CONFIG).unwrap(), false),
AccountMeta::new_readonly(config_id(), false),
AccountMeta::new_readonly(*authorized_pubkey, true),
];
Instruction::new_with_bincode(id(), &StakeInstruction::DelegateStake, account_metas)
}

/// FIXME copied from stake program
pub fn deactivate_stake(stake_pubkey: &Pubkey, authorized_pubkey: &Pubkey) -> Instruction {
let account_metas = vec![
AccountMeta::new(*stake_pubkey, false),
AccountMeta::new_readonly(sysvar::clock::id(), false),
AccountMeta::new_readonly(*authorized_pubkey, true),
];
Instruction::new_with_bincode(id(), &StakeInstruction::Deactivate, account_metas)
}
Loading

0 comments on commit 9f38a6f

Please sign in to comment.