From 3b48fa09d38d1b66ffb4fef186b606f1bc4fdb31 Mon Sep 17 00:00:00 2001 From: Jon Cinque Date: Tue, 19 Oct 2021 01:50:35 +0200 Subject: [PATCH] stake-pool: Cleanup documentation in code (#2525) --- stake-pool/program/src/instruction.rs | 35 +++++++++++++++--------- stake-pool/program/src/processor.rs | 38 ++++++++++++++++----------- 2 files changed, 45 insertions(+), 28 deletions(-) diff --git a/stake-pool/program/src/instruction.rs b/stake-pool/program/src/instruction.rs index d4e64602ae0..e114a4d68c7 100644 --- a/stake-pool/program/src/instruction.rs +++ b/stake-pool/program/src/instruction.rs @@ -80,7 +80,8 @@ pub enum StakePoolInstruction { /// (Staker only) Adds stake account delegated to validator to the pool's /// list of managed validators. /// - /// The stake account will have the rent-exempt amount plus 1 SOL. + /// The stake account will have the rent-exempt amount plus + /// `crate::MINIMUM_ACTIVE_STAKE` (currently 0.001 SOL). /// /// 0. `[w]` Stake pool /// 1. `[s]` Staker @@ -99,8 +100,9 @@ pub enum StakePoolInstruction { /// (Staker only) Removes validator from the pool /// - /// Only succeeds if the validator stake account has the minimum of 1 SOL - /// plus the rent-exempt amount. + /// Only succeeds if the validator stake account has the minimum of + /// `crate::MINIMUM_ACTIVE_STAKE` (currently 0.001 SOL) plus the rent-exempt + /// amount. /// /// 0. `[w]` Stake pool /// 1. `[s]` Staker @@ -109,7 +111,7 @@ pub enum StakePoolInstruction { /// 4. `[w]` Validator stake list storage account /// 5. `[w]` Stake account to remove from the pool /// 6. `[]` Transient stake account, to check that that we're not trying to activate - /// 7. `[w]` Destination stake account, to receive the minimum SOL from the validator stake account. Must be + /// 7. `[w]` Destination stake account, to receive the minimum SOL from the validator stake account /// 8. `[]` Sysvar clock /// 9. `[]` Stake program id, RemoveValidatorFromPool, @@ -154,8 +156,9 @@ pub enum StakePoolInstruction { /// will do the work of merging once it's ready. /// /// This instruction only succeeds if the transient stake account does not exist. - /// The minimum amount to move is rent-exemption plus 1 SOL in order to avoid - /// issues on credits observed when merging active stakes later. + /// The minimum amount to move is rent-exemption plus `crate::MINIMUM_ACTIVE_STAKE` + /// (currently 0.001 SOL) in order to avoid issues on credits observed when + /// merging active stakes later. /// /// 0. `[]` Stake pool /// 1. `[s]` Stake pool staker @@ -275,11 +278,19 @@ pub enum StakePoolInstruction { /// /// Succeeds if the stake account has enough SOL to cover the desired amount /// of pool tokens, and if the withdrawal keeps the total staked amount - /// above the minimum of rent-exempt amount + 1 SOL. + /// above the minimum of rent-exempt amount + 0.001 SOL. /// - /// A validator stake account can be withdrawn from freely, and the reserve - /// can only be drawn from if there is no active stake left, where all - /// validator accounts are left with 1 lamport. + /// When allowing withdrawals, the order of priority goes: + /// + /// * preferred withdraw validator stake account (if set) + /// * validator stake accounts + /// * transient stake accounts + /// * reserve stake account + /// + /// A user can freely withdraw from a validator stake account, and if they + /// are all at the minimum, then they can withdraw from transient stake + /// accounts, and if they are all at minimum, then they can withdraw from + /// the reserve. /// /// 0. `[w]` Stake pool /// 1. `[w]` Validator stake list storage account @@ -301,8 +312,8 @@ pub enum StakePoolInstruction { /// /// 0. `[w]` StakePool /// 1. `[s]` Manager - /// 2. '[]` New manager pubkey - /// 3. '[]` New manager fee account + /// 2. `[s]` New manager + /// 3. `[]` New manager fee account SetManager, /// (Manager only) Update fee diff --git a/stake-pool/program/src/processor.rs b/stake-pool/program/src/processor.rs index 82ff479a610..aff89ee6a96 100644 --- a/stake-pool/program/src/processor.rs +++ b/stake-pool/program/src/processor.rs @@ -163,7 +163,7 @@ fn create_transient_stake_account<'a>( /// Program state handler. pub struct Processor {} impl Processor { - /// Issue a stake_deactivate instruction. + /// Issue a delegate_stake instruction. #[allow(clippy::too_many_arguments)] fn stake_delegate<'a>( stake_info: AccountInfo<'a>, @@ -1389,6 +1389,16 @@ impl Processor { stake_pool.check_reserve_stake(reserve_stake_info)?; check_stake_program(stake_program_info.key)?; + if validator_stake_accounts + .len() + .checked_rem(2) + .ok_or(StakePoolError::CalculationFailure)? + != 0 + { + msg!("Odd number of validator stake accounts passed in, should be pairs of validator stake and transient stake accounts"); + return Err(StakePoolError::UnexpectedValidatorListAccountSize.into()); + } + check_account_owner(validator_list_info, program_id)?; let mut validator_list_data = validator_list_info.data.borrow_mut(); let (validator_list_header, mut validator_slice) = @@ -1766,9 +1776,7 @@ impl Processor { let token_program_info = next_account_info(account_info_iter)?; let stake_program_info = next_account_info(account_info_iter)?; - if *stake_program_info.key != stake_program::id() { - return Err(ProgramError::IncorrectProgramId); - } + check_stake_program(stake_program_info.key)?; check_account_owner(stake_pool_info, program_id)?; let mut stake_pool = try_from_slice_unchecked::(&stake_pool_info.data.borrow())?; @@ -2102,18 +2110,16 @@ impl Processor { deposit_lamports, )?; - if pool_tokens_user > 0 { - Self::token_mint_to( - stake_pool_info.key, - token_program_info.clone(), - pool_mint_info.clone(), - dest_user_pool_info.clone(), - withdraw_authority_info.clone(), - AUTHORITY_WITHDRAW, - stake_pool.stake_withdraw_bump_seed, - pool_tokens_user, - )?; - } + Self::token_mint_to( + stake_pool_info.key, + token_program_info.clone(), + pool_mint_info.clone(), + dest_user_pool_info.clone(), + withdraw_authority_info.clone(), + AUTHORITY_WITHDRAW, + stake_pool.stake_withdraw_bump_seed, + pool_tokens_user, + )?; if pool_tokens_manager_deposit_fee > 0 { Self::token_mint_to(