2019-08-15 14:35:48 -07:00
|
|
|
use crate::{
|
|
|
|
config, id,
|
2019-09-26 13:29:29 -07:00
|
|
|
stake_state::{Authorized, Lockup, StakeAccount, StakeAuthorize, StakeState},
|
2019-08-15 14:35:48 -07:00
|
|
|
};
|
2019-04-01 16:45:53 -07:00
|
|
|
use log::*;
|
2019-09-09 18:17:32 -07:00
|
|
|
use num_derive::{FromPrimitive, ToPrimitive};
|
2019-04-01 16:45:53 -07:00
|
|
|
use serde_derive::{Deserialize, Serialize};
|
2019-08-12 20:59:57 -07:00
|
|
|
use solana_sdk::{
|
2020-03-02 12:28:43 -08:00
|
|
|
clock::{Epoch, UnixTimestamp},
|
2020-06-17 10:39:14 -07:00
|
|
|
decode_error::DecodeError,
|
2021-02-05 22:40:07 -08:00
|
|
|
feature_set,
|
2020-05-29 23:17:44 -07:00
|
|
|
instruction::{AccountMeta, Instruction, InstructionError},
|
2020-10-26 09:14:57 -07:00
|
|
|
keyed_account::{from_keyed_account, get_signers, next_keyed_account, KeyedAccount},
|
2020-10-28 20:21:50 -07:00
|
|
|
process_instruction::InvokeContext,
|
2020-06-17 10:39:14 -07:00
|
|
|
program_utils::limited_deserialize,
|
2019-08-12 20:59:57 -07:00
|
|
|
pubkey::Pubkey,
|
2019-11-04 12:31:24 -08:00
|
|
|
system_instruction,
|
2020-10-26 09:14:57 -07:00
|
|
|
sysvar::{self, clock::Clock, rent::Rent, stake_history::StakeHistory},
|
2019-08-12 20:59:57 -07:00
|
|
|
};
|
2019-12-02 14:42:05 -08:00
|
|
|
use thiserror::Error;
|
2019-04-01 16:45:53 -07:00
|
|
|
|
2019-09-09 18:17:32 -07:00
|
|
|
/// Reasons the stake might have had an error
|
2019-12-02 14:42:05 -08:00
|
|
|
#[derive(Error, Debug, Clone, PartialEq, FromPrimitive, ToPrimitive)]
|
2019-09-09 18:17:32 -07:00
|
|
|
pub enum StakeError {
|
2019-12-02 14:42:05 -08:00
|
|
|
#[error("not enough credits to redeem")]
|
2019-09-09 18:17:32 -07:00
|
|
|
NoCreditsToRedeem,
|
2019-12-02 14:42:05 -08:00
|
|
|
|
|
|
|
#[error("lockup has not yet expired")]
|
2019-09-16 17:47:42 -07:00
|
|
|
LockupInForce,
|
2019-12-02 14:42:05 -08:00
|
|
|
|
|
|
|
#[error("stake already deactivated")]
|
2019-10-15 12:50:31 -07:00
|
|
|
AlreadyDeactivated,
|
2019-12-02 14:42:05 -08:00
|
|
|
|
|
|
|
#[error("one re-delegation permitted per epoch")]
|
2019-10-29 14:42:45 -07:00
|
|
|
TooSoonToRedelegate,
|
2019-12-02 14:42:05 -08:00
|
|
|
|
|
|
|
#[error("split amount is more than is staked")]
|
2019-10-31 11:07:27 -07:00
|
|
|
InsufficientStake,
|
2020-06-10 17:22:47 -07:00
|
|
|
|
2020-11-19 13:44:16 -08:00
|
|
|
#[error("stake account with transient stake cannot be merged")]
|
|
|
|
MergeTransientStake,
|
2020-06-10 17:22:47 -07:00
|
|
|
|
2020-11-19 13:44:16 -08:00
|
|
|
#[error("stake account merge failed due to different authority, lockups or state")]
|
2020-06-10 17:22:47 -07:00
|
|
|
MergeMismatch,
|
2021-02-05 22:40:07 -08:00
|
|
|
|
|
|
|
#[error("custodian address not present")]
|
|
|
|
CustodianMissing,
|
|
|
|
|
|
|
|
#[error("custodian signature not present")]
|
|
|
|
CustodianSignatureMissing,
|
2019-09-09 18:17:32 -07:00
|
|
|
}
|
2019-12-02 14:42:05 -08:00
|
|
|
|
2019-09-09 18:17:32 -07:00
|
|
|
impl<E> DecodeError<E> for StakeError {
|
|
|
|
fn type_of() -> &'static str {
|
|
|
|
"StakeError"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-26 13:29:29 -07:00
|
|
|
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
|
2019-04-01 16:45:53 -07:00
|
|
|
pub enum StakeInstruction {
|
2020-05-29 19:29:24 -07:00
|
|
|
/// Initialize a stake with lockup and authorization information
|
2019-09-04 13:34:09 -07:00
|
|
|
///
|
2020-05-29 19:29:24 -07:00
|
|
|
/// # Account references
|
|
|
|
/// 0. [WRITE] Uninitialized stake account
|
|
|
|
/// 1. [] Rent sysvar
|
2019-09-04 13:34:09 -07:00
|
|
|
///
|
2019-09-26 13:29:29 -07:00
|
|
|
/// Authorized carries pubkeys that must sign staker transactions
|
|
|
|
/// and withdrawer transactions.
|
|
|
|
/// Lockup carries information about withdrawal restrictions
|
|
|
|
Initialize(Authorized, Lockup),
|
2019-09-16 17:47:42 -07:00
|
|
|
|
2019-09-26 13:29:29 -07:00
|
|
|
/// Authorize a key to manage stake or withdrawal
|
2019-09-12 19:03:28 -07:00
|
|
|
///
|
2020-05-29 19:29:24 -07:00
|
|
|
/// # Account references
|
|
|
|
/// 0. [WRITE] Stake account to be updated
|
2021-01-25 11:34:54 -08:00
|
|
|
/// 1. [] Clock sysvar
|
2020-05-29 19:29:24 -07:00
|
|
|
/// 2. [SIGNER] The stake or withdraw authority
|
2021-01-26 09:44:44 -08:00
|
|
|
/// 3. Optional: [SIGNER] Lockup authority, if updating StakeAuthorize::Withdrawer before
|
|
|
|
/// lockup expiration
|
2019-09-26 13:29:29 -07:00
|
|
|
Authorize(Pubkey, StakeAuthorize),
|
|
|
|
|
2020-05-29 19:29:24 -07:00
|
|
|
/// Delegate a stake to a particular vote account
|
2019-05-21 07:32:38 -07:00
|
|
|
///
|
2020-05-29 19:29:24 -07:00
|
|
|
/// # Account references
|
|
|
|
/// 0. [WRITE] Initialized stake account to be delegated
|
|
|
|
/// 1. [] Vote account to which this stake will be delegated
|
|
|
|
/// 2. [] Clock sysvar
|
|
|
|
/// 3. [] Stake history sysvar that carries stake warmup/cooldown history
|
|
|
|
/// 4. [] Address of config account that carries stake config
|
|
|
|
/// 5. [SIGNER] Stake authority
|
2019-06-10 12:17:29 -07:00
|
|
|
///
|
2019-09-11 09:48:29 -07:00
|
|
|
/// The entire balance of the staking account is staked. DelegateStake
|
|
|
|
/// can be called multiple times, but re-delegation is delayed
|
|
|
|
/// by one epoch
|
|
|
|
DelegateStake,
|
2019-04-01 16:45:53 -07:00
|
|
|
|
2020-05-29 19:29:24 -07:00
|
|
|
/// Split u64 tokens and stake off a stake account into another stake account.
|
2019-10-31 11:07:27 -07:00
|
|
|
///
|
2020-05-29 19:29:24 -07:00
|
|
|
/// # Account references
|
|
|
|
/// 0. [WRITE] Stake account to be split; must be in the Initialized or Stake state
|
|
|
|
/// 1. [WRITE] Uninitialized stake account that will take the split-off amount
|
|
|
|
/// 2. [SIGNER] Stake authority
|
2019-10-31 11:07:27 -07:00
|
|
|
Split(u64),
|
|
|
|
|
2019-06-21 22:28:34 -07:00
|
|
|
/// Withdraw unstaked lamports from the stake account
|
|
|
|
///
|
2020-05-29 19:29:24 -07:00
|
|
|
/// # Account references
|
|
|
|
/// 0. [WRITE] Stake account from which to withdraw
|
|
|
|
/// 1. [WRITE] Recipient account
|
|
|
|
/// 2. [] Clock sysvar
|
|
|
|
/// 3. [] Stake history sysvar that carries stake warmup/cooldown history
|
|
|
|
/// 4. [SIGNER] Withdraw authority
|
|
|
|
/// 5. Optional: [SIGNER] Lockup authority, if before lockup expiration
|
2019-06-21 22:28:34 -07:00
|
|
|
///
|
2020-05-29 19:29:24 -07:00
|
|
|
/// The u64 is the portion of the stake account balance to be withdrawn,
|
|
|
|
/// must be `<= StakeAccount.lamports - staked_lamports`.
|
2019-06-21 22:28:34 -07:00
|
|
|
Withdraw(u64),
|
2019-06-21 23:45:03 -07:00
|
|
|
|
|
|
|
/// Deactivates the stake in the account
|
2019-09-11 09:48:29 -07:00
|
|
|
///
|
2020-05-29 19:29:24 -07:00
|
|
|
/// # Account references
|
|
|
|
/// 0. [WRITE] Delegated stake account
|
|
|
|
/// 1. [] Clock sysvar
|
|
|
|
/// 2. [SIGNER] Stake authority
|
2019-06-21 23:45:03 -07:00
|
|
|
Deactivate,
|
2020-01-28 20:59:53 -08:00
|
|
|
|
|
|
|
/// Set stake lockup
|
|
|
|
///
|
2020-05-29 19:29:24 -07:00
|
|
|
/// # Account references
|
|
|
|
/// 0. [WRITE] Initialized stake account
|
|
|
|
/// 1. [SIGNER] Lockup authority
|
2020-03-02 12:28:43 -08:00
|
|
|
SetLockup(LockupArgs),
|
2020-06-10 17:22:47 -07:00
|
|
|
|
2021-02-25 08:24:39 -08:00
|
|
|
/// Merge two stake accounts.
|
|
|
|
///
|
|
|
|
/// Both accounts must have identical lockup and authority keys. A merge
|
|
|
|
/// is possible between two stakes in the following states with no additional
|
|
|
|
/// conditions:
|
|
|
|
///
|
|
|
|
/// * two deactivated stakes
|
|
|
|
/// * an inactive stake into an activating stake during its activation epoch
|
|
|
|
///
|
|
|
|
/// For the following cases, the voter pubkey and vote credits observed must match:
|
|
|
|
///
|
|
|
|
/// * two activated stakes
|
|
|
|
/// * two activating accounts that share an activation epoch, during the activation epoch
|
|
|
|
///
|
|
|
|
/// All other combinations of stake states will fail to merge, including all
|
|
|
|
/// "transient" states, where a stake is activating or deactivating with a
|
|
|
|
/// non-zero effective stake.
|
2020-06-10 17:22:47 -07:00
|
|
|
///
|
|
|
|
/// # Account references
|
|
|
|
/// 0. [WRITE] Destination stake account for the merge
|
|
|
|
/// 1. [WRITE] Source stake account for to merge. This account will be drained
|
|
|
|
/// 2. [] Clock sysvar
|
|
|
|
/// 3. [] Stake history sysvar that carries stake warmup/cooldown history
|
|
|
|
/// 4. [SIGNER] Stake authority
|
|
|
|
Merge,
|
2020-08-21 11:28:01 -07:00
|
|
|
|
|
|
|
/// Authorize a key to manage stake or withdrawal with a derived key
|
|
|
|
///
|
|
|
|
/// # Account references
|
|
|
|
/// 0. [WRITE] Stake account to be updated
|
|
|
|
/// 1. [SIGNER] Base key of stake or withdraw authority
|
2021-01-25 11:34:54 -08:00
|
|
|
/// 2. [] Clock sysvar
|
2021-01-26 09:44:44 -08:00
|
|
|
/// 3. Optional: [SIGNER] Lockup authority, if updating StakeAuthorize::Withdrawer before
|
|
|
|
/// lockup expiration
|
2020-08-21 11:28:01 -07:00
|
|
|
AuthorizeWithSeed(AuthorizeWithSeedArgs),
|
2020-03-02 12:28:43 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default, Debug, Serialize, Deserialize, PartialEq, Clone, Copy)]
|
|
|
|
pub struct LockupArgs {
|
|
|
|
pub unix_timestamp: Option<UnixTimestamp>,
|
|
|
|
pub epoch: Option<Epoch>,
|
|
|
|
pub custodian: Option<Pubkey>,
|
2019-04-01 16:45:53 -07:00
|
|
|
}
|
|
|
|
|
2020-08-21 11:28:01 -07:00
|
|
|
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
|
|
|
|
pub struct AuthorizeWithSeedArgs {
|
|
|
|
pub new_authorized_pubkey: Pubkey,
|
|
|
|
pub stake_authorize: StakeAuthorize,
|
|
|
|
pub authority_seed: String,
|
|
|
|
pub authority_owner: Pubkey,
|
|
|
|
}
|
|
|
|
|
2020-01-20 12:33:27 -08:00
|
|
|
fn initialize(stake_pubkey: &Pubkey, authorized: &Authorized, lockup: &Lockup) -> Instruction {
|
2021-03-03 21:46:48 -08:00
|
|
|
Instruction::new_with_bincode(
|
2019-10-31 11:07:27 -07:00
|
|
|
id(),
|
|
|
|
&StakeInstruction::Initialize(*authorized, *lockup),
|
|
|
|
vec![
|
|
|
|
AccountMeta::new(*stake_pubkey, false),
|
2019-11-05 08:38:35 -08:00
|
|
|
AccountMeta::new_readonly(sysvar::rent::id(), false),
|
2019-10-31 11:07:27 -07:00
|
|
|
],
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-12-14 04:38:24 -08:00
|
|
|
pub fn create_account_with_seed(
|
|
|
|
from_pubkey: &Pubkey,
|
|
|
|
stake_pubkey: &Pubkey,
|
2019-12-29 16:42:24 -08:00
|
|
|
base: &Pubkey,
|
2019-12-14 04:38:24 -08:00
|
|
|
seed: &str,
|
|
|
|
authorized: &Authorized,
|
|
|
|
lockup: &Lockup,
|
|
|
|
lamports: u64,
|
|
|
|
) -> Vec<Instruction> {
|
|
|
|
vec![
|
|
|
|
system_instruction::create_account_with_seed(
|
|
|
|
from_pubkey,
|
|
|
|
stake_pubkey,
|
2019-12-29 16:42:24 -08:00
|
|
|
base,
|
2019-12-14 04:38:24 -08:00
|
|
|
seed,
|
|
|
|
lamports,
|
|
|
|
std::mem::size_of::<StakeState>() as u64,
|
|
|
|
&id(),
|
|
|
|
),
|
|
|
|
initialize(stake_pubkey, authorized, lockup),
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_account(
|
2019-09-04 13:34:09 -07:00
|
|
|
from_pubkey: &Pubkey,
|
|
|
|
stake_pubkey: &Pubkey,
|
2019-09-26 13:29:29 -07:00
|
|
|
authorized: &Authorized,
|
|
|
|
lockup: &Lockup,
|
2019-09-29 21:18:15 -07:00
|
|
|
lamports: u64,
|
2019-09-04 13:34:09 -07:00
|
|
|
) -> Vec<Instruction> {
|
|
|
|
vec![
|
2019-09-20 14:10:39 -07:00
|
|
|
system_instruction::create_account(
|
2019-09-04 13:34:09 -07:00
|
|
|
from_pubkey,
|
|
|
|
stake_pubkey,
|
|
|
|
lamports,
|
|
|
|
std::mem::size_of::<StakeState>() as u64,
|
|
|
|
&id(),
|
|
|
|
),
|
2019-10-31 11:07:27 -07:00
|
|
|
initialize(stake_pubkey, authorized, lockup),
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2020-01-20 12:33:27 -08:00
|
|
|
fn _split(
|
|
|
|
stake_pubkey: &Pubkey,
|
|
|
|
authorized_pubkey: &Pubkey,
|
|
|
|
lamports: u64,
|
|
|
|
split_stake_pubkey: &Pubkey,
|
|
|
|
) -> Instruction {
|
|
|
|
let account_metas = vec![
|
|
|
|
AccountMeta::new(*stake_pubkey, false),
|
|
|
|
AccountMeta::new(*split_stake_pubkey, false),
|
2020-05-29 23:17:44 -07:00
|
|
|
AccountMeta::new_readonly(*authorized_pubkey, true),
|
|
|
|
];
|
2020-01-20 12:33:27 -08:00
|
|
|
|
2021-03-03 21:46:48 -08:00
|
|
|
Instruction::new_with_bincode(id(), &StakeInstruction::Split(lamports), account_metas)
|
2020-01-20 12:33:27 -08:00
|
|
|
}
|
|
|
|
|
2019-10-31 11:07:27 -07:00
|
|
|
pub fn split(
|
|
|
|
stake_pubkey: &Pubkey,
|
|
|
|
authorized_pubkey: &Pubkey,
|
|
|
|
lamports: u64,
|
|
|
|
split_stake_pubkey: &Pubkey,
|
|
|
|
) -> Vec<Instruction> {
|
|
|
|
vec![
|
2020-05-22 16:39:01 -07:00
|
|
|
system_instruction::create_account(
|
|
|
|
authorized_pubkey, // Sending 0, so any signer will suffice
|
|
|
|
split_stake_pubkey,
|
|
|
|
0,
|
|
|
|
std::mem::size_of::<StakeState>() as u64,
|
|
|
|
&id(),
|
|
|
|
),
|
2020-01-20 12:33:27 -08:00
|
|
|
_split(
|
2019-10-31 11:07:27 -07:00
|
|
|
stake_pubkey,
|
2020-01-20 12:33:27 -08:00
|
|
|
authorized_pubkey,
|
|
|
|
lamports,
|
2019-10-31 11:07:27 -07:00
|
|
|
split_stake_pubkey,
|
2020-01-20 12:33:27 -08:00
|
|
|
),
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn split_with_seed(
|
|
|
|
stake_pubkey: &Pubkey,
|
|
|
|
authorized_pubkey: &Pubkey,
|
|
|
|
lamports: u64,
|
2021-02-17 05:25:02 -08:00
|
|
|
split_stake_pubkey: &Pubkey, // derived using create_with_seed()
|
2020-01-20 12:33:27 -08:00
|
|
|
base: &Pubkey, // base
|
|
|
|
seed: &str, // seed
|
|
|
|
) -> Vec<Instruction> {
|
|
|
|
vec![
|
2020-05-22 16:39:01 -07:00
|
|
|
system_instruction::create_account_with_seed(
|
|
|
|
authorized_pubkey, // Sending 0, so any signer will suffice
|
2020-01-20 12:33:27 -08:00
|
|
|
split_stake_pubkey,
|
|
|
|
base,
|
|
|
|
seed,
|
2020-05-22 16:39:01 -07:00
|
|
|
0,
|
2019-10-31 11:07:27 -07:00
|
|
|
std::mem::size_of::<StakeState>() as u64,
|
|
|
|
&id(),
|
|
|
|
),
|
2020-01-20 12:33:27 -08:00
|
|
|
_split(
|
|
|
|
stake_pubkey,
|
|
|
|
authorized_pubkey,
|
|
|
|
lamports,
|
|
|
|
split_stake_pubkey,
|
|
|
|
),
|
2019-09-04 13:34:09 -07:00
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2020-06-10 17:22:47 -07:00
|
|
|
pub fn merge(
|
|
|
|
destination_stake_pubkey: &Pubkey,
|
|
|
|
source_stake_pubkey: &Pubkey,
|
|
|
|
authorized_pubkey: &Pubkey,
|
|
|
|
) -> Vec<Instruction> {
|
|
|
|
let account_metas = vec![
|
|
|
|
AccountMeta::new(*destination_stake_pubkey, false),
|
|
|
|
AccountMeta::new(*source_stake_pubkey, false),
|
|
|
|
AccountMeta::new_readonly(sysvar::clock::id(), false),
|
|
|
|
AccountMeta::new_readonly(sysvar::stake_history::id(), false),
|
|
|
|
AccountMeta::new_readonly(*authorized_pubkey, true),
|
|
|
|
];
|
|
|
|
|
2021-03-03 21:46:48 -08:00
|
|
|
vec![Instruction::new_with_bincode(
|
2020-06-10 17:22:47 -07:00
|
|
|
id(),
|
|
|
|
&StakeInstruction::Merge,
|
|
|
|
account_metas,
|
|
|
|
)]
|
|
|
|
}
|
|
|
|
|
2019-12-14 04:38:24 -08:00
|
|
|
pub fn create_account_and_delegate_stake(
|
2019-06-10 12:17:29 -07:00
|
|
|
from_pubkey: &Pubkey,
|
2019-09-04 13:34:09 -07:00
|
|
|
stake_pubkey: &Pubkey,
|
2019-06-10 12:17:29 -07:00
|
|
|
vote_pubkey: &Pubkey,
|
2019-09-26 13:29:29 -07:00
|
|
|
authorized: &Authorized,
|
2019-12-14 04:38:24 -08:00
|
|
|
lockup: &Lockup,
|
2019-09-29 21:18:15 -07:00
|
|
|
lamports: u64,
|
2019-06-10 12:17:29 -07:00
|
|
|
) -> Vec<Instruction> {
|
2019-12-14 04:38:24 -08:00
|
|
|
let mut instructions = create_account(from_pubkey, stake_pubkey, authorized, lockup, lamports);
|
2019-09-29 21:18:15 -07:00
|
|
|
instructions.push(delegate_stake(
|
|
|
|
stake_pubkey,
|
|
|
|
&authorized.staker,
|
|
|
|
vote_pubkey,
|
|
|
|
));
|
2019-06-10 12:17:29 -07:00
|
|
|
instructions
|
|
|
|
}
|
|
|
|
|
2019-12-16 16:02:40 -08:00
|
|
|
pub fn create_account_with_seed_and_delegate_stake(
|
|
|
|
from_pubkey: &Pubkey,
|
|
|
|
stake_pubkey: &Pubkey,
|
2019-12-29 16:42:24 -08:00
|
|
|
base: &Pubkey,
|
2019-12-16 16:02:40 -08:00
|
|
|
seed: &str,
|
|
|
|
vote_pubkey: &Pubkey,
|
|
|
|
authorized: &Authorized,
|
|
|
|
lockup: &Lockup,
|
|
|
|
lamports: u64,
|
|
|
|
) -> Vec<Instruction> {
|
|
|
|
let mut instructions = create_account_with_seed(
|
|
|
|
from_pubkey,
|
|
|
|
stake_pubkey,
|
2019-12-29 16:42:24 -08:00
|
|
|
base,
|
2019-12-16 16:02:40 -08:00
|
|
|
seed,
|
|
|
|
authorized,
|
|
|
|
lockup,
|
|
|
|
lamports,
|
|
|
|
);
|
|
|
|
instructions.push(delegate_stake(
|
|
|
|
stake_pubkey,
|
|
|
|
&authorized.staker,
|
|
|
|
vote_pubkey,
|
|
|
|
));
|
|
|
|
instructions
|
|
|
|
}
|
|
|
|
|
2019-09-12 19:03:28 -07:00
|
|
|
pub fn authorize(
|
|
|
|
stake_pubkey: &Pubkey,
|
|
|
|
authorized_pubkey: &Pubkey,
|
|
|
|
new_authorized_pubkey: &Pubkey,
|
2019-09-26 13:29:29 -07:00
|
|
|
stake_authorize: StakeAuthorize,
|
2021-01-26 09:44:44 -08:00
|
|
|
custodian_pubkey: Option<&Pubkey>,
|
2019-09-12 19:03:28 -07:00
|
|
|
) -> Instruction {
|
2021-01-26 09:44:44 -08:00
|
|
|
let mut account_metas = vec![
|
2020-01-01 11:03:29 -08:00
|
|
|
AccountMeta::new(*stake_pubkey, false),
|
|
|
|
AccountMeta::new_readonly(sysvar::clock::id(), false),
|
2020-05-29 23:17:44 -07:00
|
|
|
AccountMeta::new_readonly(*authorized_pubkey, true),
|
|
|
|
];
|
2019-09-12 19:03:28 -07:00
|
|
|
|
2021-01-26 09:44:44 -08:00
|
|
|
if let Some(custodian_pubkey) = custodian_pubkey {
|
|
|
|
account_metas.push(AccountMeta::new_readonly(*custodian_pubkey, true));
|
|
|
|
}
|
|
|
|
|
2021-03-03 21:46:48 -08:00
|
|
|
Instruction::new_with_bincode(
|
2019-09-12 19:03:28 -07:00
|
|
|
id(),
|
2019-09-26 13:29:29 -07:00
|
|
|
&StakeInstruction::Authorize(*new_authorized_pubkey, stake_authorize),
|
2019-09-12 19:03:28 -07:00
|
|
|
account_metas,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-08-21 11:28:01 -07:00
|
|
|
pub fn authorize_with_seed(
|
|
|
|
stake_pubkey: &Pubkey,
|
|
|
|
authority_base: &Pubkey,
|
|
|
|
authority_seed: String,
|
|
|
|
authority_owner: &Pubkey,
|
|
|
|
new_authorized_pubkey: &Pubkey,
|
|
|
|
stake_authorize: StakeAuthorize,
|
2021-01-26 09:44:44 -08:00
|
|
|
custodian_pubkey: Option<&Pubkey>,
|
2020-08-21 11:28:01 -07:00
|
|
|
) -> Instruction {
|
2021-01-26 09:44:44 -08:00
|
|
|
let mut account_metas = vec![
|
2020-08-21 11:28:01 -07:00
|
|
|
AccountMeta::new(*stake_pubkey, false),
|
|
|
|
AccountMeta::new_readonly(*authority_base, true),
|
2021-01-25 11:34:54 -08:00
|
|
|
AccountMeta::new_readonly(sysvar::clock::id(), false),
|
2020-08-21 11:28:01 -07:00
|
|
|
];
|
|
|
|
|
2021-01-26 09:44:44 -08:00
|
|
|
if let Some(custodian_pubkey) = custodian_pubkey {
|
|
|
|
account_metas.push(AccountMeta::new_readonly(*custodian_pubkey, true));
|
|
|
|
}
|
|
|
|
|
2020-08-21 11:28:01 -07:00
|
|
|
let args = AuthorizeWithSeedArgs {
|
|
|
|
new_authorized_pubkey: *new_authorized_pubkey,
|
|
|
|
stake_authorize,
|
|
|
|
authority_seed,
|
|
|
|
authority_owner: *authority_owner,
|
|
|
|
};
|
|
|
|
|
2021-03-03 21:46:48 -08:00
|
|
|
Instruction::new_with_bincode(
|
2020-08-21 11:28:01 -07:00
|
|
|
id(),
|
|
|
|
&StakeInstruction::AuthorizeWithSeed(args),
|
|
|
|
account_metas,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-09-29 21:18:15 -07:00
|
|
|
pub fn delegate_stake(
|
|
|
|
stake_pubkey: &Pubkey,
|
|
|
|
authorized_pubkey: &Pubkey,
|
|
|
|
vote_pubkey: &Pubkey,
|
|
|
|
) -> Instruction {
|
2019-11-18 12:43:47 -08:00
|
|
|
let account_metas = vec![
|
|
|
|
AccountMeta::new(*stake_pubkey, false),
|
|
|
|
AccountMeta::new_readonly(*vote_pubkey, false),
|
|
|
|
AccountMeta::new_readonly(sysvar::clock::id(), false),
|
2020-01-29 17:59:14 -08:00
|
|
|
AccountMeta::new_readonly(sysvar::stake_history::id(), false),
|
2019-11-18 12:43:47 -08:00
|
|
|
AccountMeta::new_readonly(crate::config::id(), false),
|
2020-05-29 23:17:44 -07:00
|
|
|
AccountMeta::new_readonly(*authorized_pubkey, true),
|
|
|
|
];
|
2021-03-03 21:46:48 -08:00
|
|
|
Instruction::new_with_bincode(id(), &StakeInstruction::DelegateStake, account_metas)
|
2019-04-01 16:45:53 -07:00
|
|
|
}
|
|
|
|
|
2019-09-29 21:18:15 -07:00
|
|
|
pub fn withdraw(
|
|
|
|
stake_pubkey: &Pubkey,
|
2019-12-04 21:25:01 -08:00
|
|
|
withdrawer_pubkey: &Pubkey,
|
2019-09-29 21:18:15 -07:00
|
|
|
to_pubkey: &Pubkey,
|
|
|
|
lamports: u64,
|
2020-04-20 17:16:50 -07:00
|
|
|
custodian_pubkey: Option<&Pubkey>,
|
2019-09-29 21:18:15 -07:00
|
|
|
) -> Instruction {
|
2020-04-20 17:16:50 -07:00
|
|
|
let mut account_metas = vec![
|
2019-11-18 12:43:47 -08:00
|
|
|
AccountMeta::new(*stake_pubkey, false),
|
|
|
|
AccountMeta::new(*to_pubkey, false),
|
|
|
|
AccountMeta::new_readonly(sysvar::clock::id(), false),
|
|
|
|
AccountMeta::new_readonly(sysvar::stake_history::id(), false),
|
2020-05-29 23:17:44 -07:00
|
|
|
AccountMeta::new_readonly(*withdrawer_pubkey, true),
|
|
|
|
];
|
2019-12-04 21:25:01 -08:00
|
|
|
|
2020-04-20 17:16:50 -07:00
|
|
|
if let Some(custodian_pubkey) = custodian_pubkey {
|
2020-05-29 23:17:44 -07:00
|
|
|
account_metas.push(AccountMeta::new_readonly(*custodian_pubkey, true));
|
2020-04-20 17:16:50 -07:00
|
|
|
}
|
2019-12-04 21:25:01 -08:00
|
|
|
|
2021-03-03 21:46:48 -08:00
|
|
|
Instruction::new_with_bincode(id(), &StakeInstruction::Withdraw(lamports), account_metas)
|
2019-06-21 22:28:34 -07:00
|
|
|
}
|
|
|
|
|
2019-10-07 15:07:01 -07:00
|
|
|
pub fn deactivate_stake(stake_pubkey: &Pubkey, authorized_pubkey: &Pubkey) -> Instruction {
|
2019-11-18 12:43:47 -08:00
|
|
|
let account_metas = vec![
|
|
|
|
AccountMeta::new(*stake_pubkey, false),
|
|
|
|
AccountMeta::new_readonly(sysvar::clock::id(), false),
|
2020-05-29 23:17:44 -07:00
|
|
|
AccountMeta::new_readonly(*authorized_pubkey, true),
|
|
|
|
];
|
2021-03-03 21:46:48 -08:00
|
|
|
Instruction::new_with_bincode(id(), &StakeInstruction::Deactivate, account_metas)
|
2019-06-21 23:45:03 -07:00
|
|
|
}
|
|
|
|
|
2020-01-28 20:59:53 -08:00
|
|
|
pub fn set_lockup(
|
|
|
|
stake_pubkey: &Pubkey,
|
2020-03-02 12:28:43 -08:00
|
|
|
lockup: &LockupArgs,
|
2020-01-28 20:59:53 -08:00
|
|
|
custodian_pubkey: &Pubkey,
|
|
|
|
) -> Instruction {
|
2020-05-29 23:17:44 -07:00
|
|
|
let account_metas = vec![
|
|
|
|
AccountMeta::new(*stake_pubkey, false),
|
|
|
|
AccountMeta::new_readonly(*custodian_pubkey, true),
|
|
|
|
];
|
2021-03-03 21:46:48 -08:00
|
|
|
Instruction::new_with_bincode(id(), &StakeInstruction::SetLockup(*lockup), account_metas)
|
2020-01-28 20:59:53 -08:00
|
|
|
}
|
|
|
|
|
2019-04-01 16:45:53 -07:00
|
|
|
pub fn process_instruction(
|
|
|
|
_program_id: &Pubkey,
|
2020-01-22 17:54:06 -08:00
|
|
|
keyed_accounts: &[KeyedAccount],
|
2019-04-01 16:45:53 -07:00
|
|
|
data: &[u8],
|
2021-01-21 09:59:24 -08:00
|
|
|
invoke_context: &mut dyn InvokeContext,
|
2019-04-01 16:45:53 -07:00
|
|
|
) -> Result<(), InstructionError> {
|
|
|
|
trace!("process_instruction: {:?}", data);
|
|
|
|
trace!("keyed_accounts: {:?}", keyed_accounts);
|
|
|
|
|
2019-10-14 15:02:24 -07:00
|
|
|
let signers = get_signers(keyed_accounts);
|
2019-04-01 16:45:53 -07:00
|
|
|
|
2020-01-22 17:54:06 -08:00
|
|
|
let keyed_accounts = &mut keyed_accounts.iter();
|
|
|
|
let me = &next_keyed_account(keyed_accounts)?;
|
2019-04-01 16:45:53 -07:00
|
|
|
|
2020-12-22 10:20:38 -08:00
|
|
|
if me.owner()? != id() {
|
2021-02-26 14:21:34 -08:00
|
|
|
if invoke_context.is_feature_active(&feature_set::check_program_owner::id()) {
|
|
|
|
return Err(InstructionError::InvalidAccountOwner);
|
|
|
|
} else {
|
|
|
|
return Err(InstructionError::IncorrectProgramId);
|
|
|
|
}
|
2020-12-22 10:20:38 -08:00
|
|
|
}
|
|
|
|
|
2019-10-23 19:56:07 -07:00
|
|
|
match limited_deserialize(data)? {
|
2019-10-31 11:07:27 -07:00
|
|
|
StakeInstruction::Initialize(authorized, lockup) => me.initialize(
|
|
|
|
&authorized,
|
|
|
|
&lockup,
|
2020-10-26 09:14:57 -07:00
|
|
|
&from_keyed_account::<Rent>(next_keyed_account(keyed_accounts)?)?,
|
2019-10-31 11:07:27 -07:00
|
|
|
),
|
2020-04-21 21:05:49 -07:00
|
|
|
StakeInstruction::Authorize(authorized_pubkey, stake_authorize) => {
|
2021-02-05 22:40:07 -08:00
|
|
|
let require_custodian_for_locked_stake_authorize = invoke_context.is_feature_active(
|
|
|
|
&feature_set::require_custodian_for_locked_stake_authorize::id(),
|
|
|
|
);
|
|
|
|
|
|
|
|
if require_custodian_for_locked_stake_authorize {
|
|
|
|
let clock = from_keyed_account::<Clock>(next_keyed_account(keyed_accounts)?)?;
|
|
|
|
let _current_authority = next_keyed_account(keyed_accounts)?;
|
|
|
|
let custodian = keyed_accounts.next().map(|ka| ka.unsigned_key());
|
|
|
|
|
|
|
|
me.authorize(
|
|
|
|
&signers,
|
|
|
|
&authorized_pubkey,
|
|
|
|
stake_authorize,
|
|
|
|
require_custodian_for_locked_stake_authorize,
|
|
|
|
&clock,
|
|
|
|
custodian,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
me.authorize(
|
|
|
|
&signers,
|
|
|
|
&authorized_pubkey,
|
|
|
|
stake_authorize,
|
|
|
|
require_custodian_for_locked_stake_authorize,
|
|
|
|
&Clock::default(),
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
}
|
2020-08-21 11:28:01 -07:00
|
|
|
}
|
|
|
|
StakeInstruction::AuthorizeWithSeed(args) => {
|
|
|
|
let authority_base = next_keyed_account(keyed_accounts)?;
|
2021-02-05 22:40:07 -08:00
|
|
|
let require_custodian_for_locked_stake_authorize = invoke_context.is_feature_active(
|
|
|
|
&feature_set::require_custodian_for_locked_stake_authorize::id(),
|
|
|
|
);
|
|
|
|
|
|
|
|
if require_custodian_for_locked_stake_authorize {
|
|
|
|
let clock = from_keyed_account::<Clock>(next_keyed_account(keyed_accounts)?)?;
|
|
|
|
let custodian = keyed_accounts.next().map(|ka| ka.unsigned_key());
|
|
|
|
|
|
|
|
me.authorize_with_seed(
|
|
|
|
&authority_base,
|
|
|
|
&args.authority_seed,
|
|
|
|
&args.authority_owner,
|
|
|
|
&args.new_authorized_pubkey,
|
|
|
|
args.stake_authorize,
|
|
|
|
require_custodian_for_locked_stake_authorize,
|
|
|
|
&clock,
|
|
|
|
custodian,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
me.authorize_with_seed(
|
|
|
|
&authority_base,
|
|
|
|
&args.authority_seed,
|
|
|
|
&args.authority_owner,
|
|
|
|
&args.new_authorized_pubkey,
|
|
|
|
args.stake_authorize,
|
|
|
|
require_custodian_for_locked_stake_authorize,
|
|
|
|
&Clock::default(),
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
}
|
2020-04-21 21:05:49 -07:00
|
|
|
}
|
2019-09-11 09:48:29 -07:00
|
|
|
StakeInstruction::DelegateStake => {
|
2019-10-14 15:02:24 -07:00
|
|
|
let vote = next_keyed_account(keyed_accounts)?;
|
2019-06-17 19:34:21 -07:00
|
|
|
|
2020-01-29 17:59:14 -08:00
|
|
|
me.delegate(
|
2019-10-14 15:02:24 -07:00
|
|
|
&vote,
|
2020-10-26 09:14:57 -07:00
|
|
|
&from_keyed_account::<Clock>(next_keyed_account(keyed_accounts)?)?,
|
|
|
|
&from_keyed_account::<StakeHistory>(next_keyed_account(keyed_accounts)?)?,
|
2019-10-14 15:02:24 -07:00
|
|
|
&config::from_keyed_account(next_keyed_account(keyed_accounts)?)?,
|
|
|
|
&signers,
|
2019-08-15 14:35:48 -07:00
|
|
|
)
|
2019-04-01 16:45:53 -07:00
|
|
|
}
|
2019-10-31 11:07:27 -07:00
|
|
|
StakeInstruction::Split(lamports) => {
|
2020-01-22 17:54:06 -08:00
|
|
|
let split_stake = &next_keyed_account(keyed_accounts)?;
|
2019-10-31 11:07:27 -07:00
|
|
|
me.split(lamports, split_stake, &signers)
|
|
|
|
}
|
2020-06-10 17:22:47 -07:00
|
|
|
StakeInstruction::Merge => {
|
|
|
|
let source_stake = &next_keyed_account(keyed_accounts)?;
|
|
|
|
me.merge(
|
2021-01-21 09:59:24 -08:00
|
|
|
invoke_context,
|
2020-06-10 17:22:47 -07:00
|
|
|
source_stake,
|
2020-10-26 09:14:57 -07:00
|
|
|
&from_keyed_account::<Clock>(next_keyed_account(keyed_accounts)?)?,
|
|
|
|
&from_keyed_account::<StakeHistory>(next_keyed_account(keyed_accounts)?)?,
|
2020-06-10 17:22:47 -07:00
|
|
|
&signers,
|
|
|
|
)
|
|
|
|
}
|
2019-10-31 11:07:27 -07:00
|
|
|
|
2019-06-21 22:28:34 -07:00
|
|
|
StakeInstruction::Withdraw(lamports) => {
|
2020-01-22 17:54:06 -08:00
|
|
|
let to = &next_keyed_account(keyed_accounts)?;
|
2019-06-21 22:28:34 -07:00
|
|
|
me.withdraw(
|
|
|
|
lamports,
|
2019-10-14 15:02:24 -07:00
|
|
|
to,
|
2020-10-26 09:14:57 -07:00
|
|
|
&from_keyed_account::<Clock>(next_keyed_account(keyed_accounts)?)?,
|
|
|
|
&from_keyed_account::<StakeHistory>(next_keyed_account(keyed_accounts)?)?,
|
2020-07-31 12:37:53 -07:00
|
|
|
next_keyed_account(keyed_accounts)?,
|
|
|
|
keyed_accounts.next(),
|
2019-06-21 22:28:34 -07:00
|
|
|
)
|
|
|
|
}
|
2020-01-29 17:59:14 -08:00
|
|
|
StakeInstruction::Deactivate => me.deactivate(
|
2020-10-26 09:14:57 -07:00
|
|
|
&from_keyed_account::<Clock>(next_keyed_account(keyed_accounts)?)?,
|
2019-10-14 15:02:24 -07:00
|
|
|
&signers,
|
|
|
|
),
|
2020-01-28 20:59:53 -08:00
|
|
|
|
|
|
|
StakeInstruction::SetLockup(lockup) => me.set_lockup(&lockup, &signers),
|
2019-04-01 16:45:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use bincode::serialize;
|
2020-10-26 09:14:57 -07:00
|
|
|
use solana_sdk::{
|
2021-03-11 16:09:04 -08:00
|
|
|
account::{self, Account, AccountSharedData},
|
2020-10-28 20:21:50 -07:00
|
|
|
process_instruction::MockInvokeContext,
|
2020-10-26 09:14:57 -07:00
|
|
|
rent::Rent,
|
2020-10-28 22:01:07 -07:00
|
|
|
sysvar::stake_history::StakeHistory,
|
2020-10-26 09:14:57 -07:00
|
|
|
};
|
2020-01-22 09:11:56 -08:00
|
|
|
use std::cell::RefCell;
|
2020-11-16 12:42:59 -08:00
|
|
|
use std::str::FromStr;
|
2020-01-22 09:11:56 -08:00
|
|
|
|
2021-03-09 13:06:07 -08:00
|
|
|
fn create_default_account() -> RefCell<AccountSharedData> {
|
|
|
|
RefCell::new(AccountSharedData::default())
|
2020-01-22 09:11:56 -08:00
|
|
|
}
|
2019-04-01 16:45:53 -07:00
|
|
|
|
2021-03-09 13:06:07 -08:00
|
|
|
fn create_default_stake_account() -> RefCell<AccountSharedData> {
|
2021-03-11 16:09:04 -08:00
|
|
|
RefCell::new(AccountSharedData::from(Account {
|
2020-12-22 10:20:38 -08:00
|
|
|
owner: id(),
|
2021-03-11 16:09:04 -08:00
|
|
|
..Account::default()
|
|
|
|
}))
|
2020-12-22 10:20:38 -08:00
|
|
|
}
|
|
|
|
|
2020-11-16 12:42:59 -08:00
|
|
|
fn invalid_stake_state_pubkey() -> Pubkey {
|
|
|
|
Pubkey::from_str("BadStake11111111111111111111111111111111111").unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn invalid_vote_state_pubkey() -> Pubkey {
|
|
|
|
Pubkey::from_str("BadVote111111111111111111111111111111111111").unwrap()
|
|
|
|
}
|
|
|
|
|
2020-12-22 10:20:38 -08:00
|
|
|
fn spoofed_stake_state_pubkey() -> Pubkey {
|
|
|
|
Pubkey::from_str("SpoofedStake1111111111111111111111111111111").unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn spoofed_stake_program_id() -> Pubkey {
|
|
|
|
Pubkey::from_str("Spoofed111111111111111111111111111111111111").unwrap()
|
|
|
|
}
|
|
|
|
|
2019-05-07 17:08:49 -07:00
|
|
|
fn process_instruction(instruction: &Instruction) -> Result<(), InstructionError> {
|
2020-01-22 17:54:06 -08:00
|
|
|
let accounts: Vec<_> = instruction
|
2019-06-17 19:34:21 -07:00
|
|
|
.accounts
|
|
|
|
.iter()
|
|
|
|
.map(|meta| {
|
2020-01-22 09:11:56 -08:00
|
|
|
RefCell::new(if sysvar::clock::check_id(&meta.pubkey) {
|
2021-03-09 13:06:07 -08:00
|
|
|
account::create_account_shared_data(&sysvar::clock::Clock::default(), 1)
|
2019-07-12 16:38:15 -07:00
|
|
|
} else if sysvar::rewards::check_id(&meta.pubkey) {
|
2021-03-09 13:06:07 -08:00
|
|
|
account::create_account_shared_data(&sysvar::rewards::Rewards::new(0.0), 1)
|
2019-08-12 20:59:57 -07:00
|
|
|
} else if sysvar::stake_history::check_id(&meta.pubkey) {
|
2021-03-09 13:06:07 -08:00
|
|
|
account::create_account_shared_data(&StakeHistory::default(), 1)
|
2019-08-15 14:35:48 -07:00
|
|
|
} else if config::check_id(&meta.pubkey) {
|
2019-11-25 15:11:55 -08:00
|
|
|
config::create_account(0, &config::Config::default())
|
2019-10-03 14:22:48 -07:00
|
|
|
} else if sysvar::rent::check_id(&meta.pubkey) {
|
2021-03-09 13:06:07 -08:00
|
|
|
account::create_account_shared_data(&Rent::default(), 1)
|
2020-11-16 12:42:59 -08:00
|
|
|
} else if meta.pubkey == invalid_stake_state_pubkey() {
|
2021-03-11 16:09:04 -08:00
|
|
|
AccountSharedData::from(Account {
|
2020-12-13 17:26:34 -08:00
|
|
|
owner: id(),
|
2021-03-11 16:09:04 -08:00
|
|
|
..Account::default()
|
|
|
|
})
|
2020-11-16 12:42:59 -08:00
|
|
|
} else if meta.pubkey == invalid_vote_state_pubkey() {
|
2021-03-11 16:09:04 -08:00
|
|
|
AccountSharedData::from(Account {
|
2020-12-13 17:26:34 -08:00
|
|
|
owner: solana_vote_program::id(),
|
2021-03-11 16:09:04 -08:00
|
|
|
..Account::default()
|
|
|
|
})
|
2020-12-22 10:20:38 -08:00
|
|
|
} else if meta.pubkey == spoofed_stake_state_pubkey() {
|
2021-03-11 16:09:04 -08:00
|
|
|
AccountSharedData::from(Account {
|
2020-12-22 10:20:38 -08:00
|
|
|
owner: spoofed_stake_program_id(),
|
2021-03-11 16:09:04 -08:00
|
|
|
..Account::default()
|
|
|
|
})
|
2019-06-17 19:34:21 -07:00
|
|
|
} else {
|
2021-03-11 16:09:04 -08:00
|
|
|
AccountSharedData::from(Account {
|
2020-12-22 10:20:38 -08:00
|
|
|
owner: id(),
|
2021-03-11 16:09:04 -08:00
|
|
|
..Account::default()
|
|
|
|
})
|
2020-01-22 09:11:56 -08:00
|
|
|
})
|
2019-06-17 19:34:21 -07:00
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2019-05-07 17:08:49 -07:00
|
|
|
{
|
2020-01-22 17:54:06 -08:00
|
|
|
let keyed_accounts: Vec<_> = instruction
|
2019-05-07 17:08:49 -07:00
|
|
|
.accounts
|
|
|
|
.iter()
|
2020-01-22 17:54:06 -08:00
|
|
|
.zip(accounts.iter())
|
2019-05-07 17:08:49 -07:00
|
|
|
.map(|(meta, account)| KeyedAccount::new(&meta.pubkey, meta.is_signer, account))
|
|
|
|
.collect();
|
2020-10-28 20:21:50 -07:00
|
|
|
super::process_instruction(
|
|
|
|
&Pubkey::default(),
|
|
|
|
&keyed_accounts,
|
|
|
|
&instruction.data,
|
|
|
|
&mut MockInvokeContext::default(),
|
|
|
|
)
|
2019-05-07 17:08:49 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_stake_process_instruction() {
|
2019-10-31 11:07:27 -07:00
|
|
|
assert_eq!(
|
|
|
|
process_instruction(&initialize(
|
|
|
|
&Pubkey::default(),
|
|
|
|
&Authorized::default(),
|
|
|
|
&Lockup::default()
|
|
|
|
)),
|
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
process_instruction(&authorize(
|
|
|
|
&Pubkey::default(),
|
|
|
|
&Pubkey::default(),
|
|
|
|
&Pubkey::default(),
|
2021-01-26 09:44:44 -08:00
|
|
|
StakeAuthorize::Staker,
|
|
|
|
None,
|
2019-10-31 11:07:27 -07:00
|
|
|
)),
|
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
process_instruction(
|
|
|
|
&split(
|
|
|
|
&Pubkey::default(),
|
|
|
|
&Pubkey::default(),
|
|
|
|
100,
|
2020-11-16 12:42:59 -08:00
|
|
|
&invalid_stake_state_pubkey(),
|
2020-05-22 16:39:01 -07:00
|
|
|
)[1]
|
2020-01-20 12:33:27 -08:00
|
|
|
),
|
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
2020-06-10 17:22:47 -07:00
|
|
|
assert_eq!(
|
|
|
|
process_instruction(
|
2020-11-16 12:42:59 -08:00
|
|
|
&merge(
|
|
|
|
&Pubkey::default(),
|
|
|
|
&invalid_stake_state_pubkey(),
|
|
|
|
&Pubkey::default(),
|
|
|
|
)[0]
|
2020-06-10 17:22:47 -07:00
|
|
|
),
|
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
2020-01-20 12:33:27 -08:00
|
|
|
assert_eq!(
|
|
|
|
process_instruction(
|
|
|
|
&split_with_seed(
|
|
|
|
&Pubkey::default(),
|
|
|
|
&Pubkey::default(),
|
|
|
|
100,
|
2020-11-16 12:42:59 -08:00
|
|
|
&invalid_stake_state_pubkey(),
|
2020-01-20 12:33:27 -08:00
|
|
|
&Pubkey::default(),
|
|
|
|
"seed"
|
2019-10-31 11:07:27 -07:00
|
|
|
)[1]
|
|
|
|
),
|
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
2019-05-07 17:08:49 -07:00
|
|
|
assert_eq!(
|
2019-09-29 21:18:15 -07:00
|
|
|
process_instruction(&delegate_stake(
|
|
|
|
&Pubkey::default(),
|
|
|
|
&Pubkey::default(),
|
2020-11-16 12:42:59 -08:00
|
|
|
&invalid_vote_state_pubkey(),
|
2019-09-29 21:18:15 -07:00
|
|
|
)),
|
2019-05-07 17:08:49 -07:00
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
2019-06-21 22:28:34 -07:00
|
|
|
assert_eq!(
|
2019-09-29 21:18:15 -07:00
|
|
|
process_instruction(&withdraw(
|
|
|
|
&Pubkey::default(),
|
|
|
|
&Pubkey::default(),
|
2020-10-19 12:12:08 -07:00
|
|
|
&solana_sdk::pubkey::new_rand(),
|
2020-04-20 17:16:50 -07:00
|
|
|
100,
|
|
|
|
None,
|
2019-09-29 21:18:15 -07:00
|
|
|
)),
|
2019-06-21 22:28:34 -07:00
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
2019-06-21 23:45:03 -07:00
|
|
|
assert_eq!(
|
2019-10-07 15:07:01 -07:00
|
|
|
process_instruction(&deactivate_stake(&Pubkey::default(), &Pubkey::default())),
|
2019-06-21 23:45:03 -07:00
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
2020-01-28 20:59:53 -08:00
|
|
|
assert_eq!(
|
|
|
|
process_instruction(&set_lockup(
|
|
|
|
&Pubkey::default(),
|
2020-03-02 12:28:43 -08:00
|
|
|
&LockupArgs::default(),
|
2020-01-28 20:59:53 -08:00
|
|
|
&Pubkey::default()
|
|
|
|
)),
|
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
2019-05-07 17:08:49 -07:00
|
|
|
}
|
|
|
|
|
2020-12-22 10:20:38 -08:00
|
|
|
#[test]
|
|
|
|
fn test_spoofed_stake_accounts() {
|
|
|
|
assert_eq!(
|
|
|
|
process_instruction(&initialize(
|
|
|
|
&spoofed_stake_state_pubkey(),
|
|
|
|
&Authorized::default(),
|
|
|
|
&Lockup::default()
|
|
|
|
)),
|
2021-02-26 14:21:34 -08:00
|
|
|
Err(InstructionError::InvalidAccountOwner),
|
2020-12-22 10:20:38 -08:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
process_instruction(&authorize(
|
|
|
|
&spoofed_stake_state_pubkey(),
|
|
|
|
&Pubkey::default(),
|
|
|
|
&Pubkey::default(),
|
2021-01-26 09:44:44 -08:00
|
|
|
StakeAuthorize::Staker,
|
|
|
|
None,
|
2020-12-22 10:20:38 -08:00
|
|
|
)),
|
2021-02-26 14:21:34 -08:00
|
|
|
Err(InstructionError::InvalidAccountOwner),
|
2020-12-22 10:20:38 -08:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
process_instruction(
|
|
|
|
&split(
|
|
|
|
&spoofed_stake_state_pubkey(),
|
|
|
|
&Pubkey::default(),
|
|
|
|
100,
|
|
|
|
&Pubkey::default(),
|
|
|
|
)[1]
|
|
|
|
),
|
2021-02-26 14:21:34 -08:00
|
|
|
Err(InstructionError::InvalidAccountOwner),
|
2020-12-22 10:20:38 -08:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
process_instruction(
|
|
|
|
&split(
|
|
|
|
&Pubkey::default(),
|
|
|
|
&Pubkey::default(),
|
|
|
|
100,
|
|
|
|
&spoofed_stake_state_pubkey(),
|
|
|
|
)[1]
|
|
|
|
),
|
|
|
|
Err(InstructionError::IncorrectProgramId),
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
process_instruction(
|
|
|
|
&merge(
|
|
|
|
&spoofed_stake_state_pubkey(),
|
|
|
|
&Pubkey::default(),
|
|
|
|
&Pubkey::default(),
|
|
|
|
)[0]
|
|
|
|
),
|
2021-02-26 14:21:34 -08:00
|
|
|
Err(InstructionError::InvalidAccountOwner),
|
2020-12-22 10:20:38 -08:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
process_instruction(
|
|
|
|
&merge(
|
|
|
|
&Pubkey::default(),
|
|
|
|
&spoofed_stake_state_pubkey(),
|
|
|
|
&Pubkey::default(),
|
|
|
|
)[0]
|
|
|
|
),
|
|
|
|
Err(InstructionError::IncorrectProgramId),
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
process_instruction(
|
|
|
|
&split_with_seed(
|
|
|
|
&spoofed_stake_state_pubkey(),
|
|
|
|
&Pubkey::default(),
|
|
|
|
100,
|
|
|
|
&Pubkey::default(),
|
|
|
|
&Pubkey::default(),
|
|
|
|
"seed"
|
|
|
|
)[1]
|
|
|
|
),
|
2021-02-26 14:21:34 -08:00
|
|
|
Err(InstructionError::InvalidAccountOwner),
|
2020-12-22 10:20:38 -08:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
process_instruction(&delegate_stake(
|
|
|
|
&spoofed_stake_state_pubkey(),
|
|
|
|
&Pubkey::default(),
|
|
|
|
&Pubkey::default(),
|
|
|
|
)),
|
2021-02-26 14:21:34 -08:00
|
|
|
Err(InstructionError::InvalidAccountOwner),
|
2020-12-22 10:20:38 -08:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
process_instruction(&withdraw(
|
|
|
|
&spoofed_stake_state_pubkey(),
|
|
|
|
&Pubkey::default(),
|
|
|
|
&solana_sdk::pubkey::new_rand(),
|
|
|
|
100,
|
|
|
|
None,
|
|
|
|
)),
|
2021-02-26 14:21:34 -08:00
|
|
|
Err(InstructionError::InvalidAccountOwner),
|
2020-12-22 10:20:38 -08:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
process_instruction(&deactivate_stake(
|
|
|
|
&spoofed_stake_state_pubkey(),
|
|
|
|
&Pubkey::default()
|
|
|
|
)),
|
2021-02-26 14:21:34 -08:00
|
|
|
Err(InstructionError::InvalidAccountOwner),
|
2020-12-22 10:20:38 -08:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
process_instruction(&set_lockup(
|
|
|
|
&spoofed_stake_state_pubkey(),
|
|
|
|
&LockupArgs::default(),
|
|
|
|
&Pubkey::default()
|
|
|
|
)),
|
2021-02-26 14:21:34 -08:00
|
|
|
Err(InstructionError::InvalidAccountOwner),
|
2020-12-22 10:20:38 -08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-04-01 16:45:53 -07:00
|
|
|
#[test]
|
|
|
|
fn test_stake_process_instruction_decode_bail() {
|
|
|
|
// these will not call stake_state, have bogus contents
|
|
|
|
|
2019-09-11 09:48:29 -07:00
|
|
|
// gets the "is_empty()" check
|
|
|
|
assert_eq!(
|
|
|
|
super::process_instruction(
|
|
|
|
&Pubkey::default(),
|
2020-01-22 17:54:06 -08:00
|
|
|
&[],
|
2019-09-26 13:29:29 -07:00
|
|
|
&serialize(&StakeInstruction::Initialize(
|
|
|
|
Authorized::default(),
|
|
|
|
Lockup::default()
|
|
|
|
))
|
|
|
|
.unwrap(),
|
2020-10-28 20:21:50 -07:00
|
|
|
&mut MockInvokeContext::default()
|
2019-09-11 09:48:29 -07:00
|
|
|
),
|
2019-10-14 15:02:24 -07:00
|
|
|
Err(InstructionError::NotEnoughAccountKeys),
|
2019-09-11 09:48:29 -07:00
|
|
|
);
|
|
|
|
|
2019-10-31 11:07:27 -07:00
|
|
|
// no account for rent
|
|
|
|
assert_eq!(
|
|
|
|
super::process_instruction(
|
|
|
|
&Pubkey::default(),
|
2020-01-22 17:54:06 -08:00
|
|
|
&[KeyedAccount::new(
|
2019-10-31 11:07:27 -07:00
|
|
|
&Pubkey::default(),
|
|
|
|
false,
|
2020-12-22 10:20:38 -08:00
|
|
|
&create_default_stake_account(),
|
2019-10-31 11:07:27 -07:00
|
|
|
)],
|
|
|
|
&serialize(&StakeInstruction::Initialize(
|
|
|
|
Authorized::default(),
|
|
|
|
Lockup::default()
|
|
|
|
))
|
|
|
|
.unwrap(),
|
2020-10-28 20:21:50 -07:00
|
|
|
&mut MockInvokeContext::default()
|
2019-10-31 11:07:27 -07:00
|
|
|
),
|
|
|
|
Err(InstructionError::NotEnoughAccountKeys),
|
|
|
|
);
|
|
|
|
|
|
|
|
// rent fails to deserialize
|
|
|
|
assert_eq!(
|
|
|
|
super::process_instruction(
|
|
|
|
&Pubkey::default(),
|
2020-01-22 17:54:06 -08:00
|
|
|
&[
|
2020-12-22 10:20:38 -08:00
|
|
|
KeyedAccount::new(&Pubkey::default(), false, &create_default_stake_account()),
|
|
|
|
KeyedAccount::new(&sysvar::rent::id(), false, &create_default_account())
|
2019-10-31 11:07:27 -07:00
|
|
|
],
|
|
|
|
&serialize(&StakeInstruction::Initialize(
|
|
|
|
Authorized::default(),
|
|
|
|
Lockup::default()
|
|
|
|
))
|
|
|
|
.unwrap(),
|
2020-10-28 20:21:50 -07:00
|
|
|
&mut MockInvokeContext::default()
|
2019-10-31 11:07:27 -07:00
|
|
|
),
|
|
|
|
Err(InstructionError::InvalidArgument),
|
|
|
|
);
|
|
|
|
|
|
|
|
// fails to deserialize stake state
|
|
|
|
assert_eq!(
|
|
|
|
super::process_instruction(
|
|
|
|
&Pubkey::default(),
|
2020-01-22 17:54:06 -08:00
|
|
|
&[
|
2020-12-22 10:20:38 -08:00
|
|
|
KeyedAccount::new(&Pubkey::default(), false, &create_default_stake_account()),
|
2019-10-31 11:07:27 -07:00
|
|
|
KeyedAccount::new(
|
|
|
|
&sysvar::rent::id(),
|
|
|
|
false,
|
2021-03-09 13:06:07 -08:00
|
|
|
&RefCell::new(account::create_account_shared_data(&Rent::default(), 0))
|
2019-10-31 11:07:27 -07:00
|
|
|
)
|
|
|
|
],
|
|
|
|
&serialize(&StakeInstruction::Initialize(
|
|
|
|
Authorized::default(),
|
|
|
|
Lockup::default()
|
|
|
|
))
|
|
|
|
.unwrap(),
|
2020-10-28 20:21:50 -07:00
|
|
|
&mut MockInvokeContext::default()
|
2019-10-31 11:07:27 -07:00
|
|
|
),
|
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
|
|
|
|
2019-09-11 09:48:29 -07:00
|
|
|
// gets the first check in delegate, wrong number of accounts
|
2019-04-01 16:45:53 -07:00
|
|
|
assert_eq!(
|
2019-05-07 17:08:49 -07:00
|
|
|
super::process_instruction(
|
2019-04-01 16:45:53 -07:00
|
|
|
&Pubkey::default(),
|
2020-01-22 17:54:06 -08:00
|
|
|
&[KeyedAccount::new(
|
2019-04-01 16:45:53 -07:00
|
|
|
&Pubkey::default(),
|
|
|
|
false,
|
2020-12-22 10:20:38 -08:00
|
|
|
&create_default_stake_account()
|
2020-01-22 09:11:56 -08:00
|
|
|
),],
|
2019-09-11 09:48:29 -07:00
|
|
|
&serialize(&StakeInstruction::DelegateStake).unwrap(),
|
2020-10-28 20:21:50 -07:00
|
|
|
&mut MockInvokeContext::default()
|
2019-04-01 16:45:53 -07:00
|
|
|
),
|
2019-10-14 15:02:24 -07:00
|
|
|
Err(InstructionError::NotEnoughAccountKeys),
|
2019-04-01 16:45:53 -07:00
|
|
|
);
|
|
|
|
|
2019-05-07 17:08:49 -07:00
|
|
|
// gets the sub-check for number of args
|
2019-04-01 16:45:53 -07:00
|
|
|
assert_eq!(
|
2019-05-07 17:08:49 -07:00
|
|
|
super::process_instruction(
|
2019-04-01 16:45:53 -07:00
|
|
|
&Pubkey::default(),
|
2020-01-22 17:54:06 -08:00
|
|
|
&[KeyedAccount::new(
|
2019-06-03 09:04:51 -07:00
|
|
|
&Pubkey::default(),
|
|
|
|
false,
|
2020-12-22 10:20:38 -08:00
|
|
|
&create_default_stake_account()
|
2020-01-22 09:11:56 -08:00
|
|
|
)],
|
2019-09-11 09:48:29 -07:00
|
|
|
&serialize(&StakeInstruction::DelegateStake).unwrap(),
|
2020-10-28 20:21:50 -07:00
|
|
|
&mut MockInvokeContext::default()
|
2019-04-01 16:45:53 -07:00
|
|
|
),
|
2019-10-14 15:02:24 -07:00
|
|
|
Err(InstructionError::NotEnoughAccountKeys),
|
2019-04-01 16:45:53 -07:00
|
|
|
);
|
|
|
|
|
2019-06-17 19:34:21 -07:00
|
|
|
// gets the check non-deserialize-able account in delegate_stake
|
2020-11-16 12:42:59 -08:00
|
|
|
let mut bad_vote_account = create_default_account();
|
|
|
|
bad_vote_account.get_mut().owner = solana_vote_program::id();
|
2019-05-07 17:08:49 -07:00
|
|
|
assert_eq!(
|
|
|
|
super::process_instruction(
|
|
|
|
&Pubkey::default(),
|
2020-01-22 17:54:06 -08:00
|
|
|
&[
|
2020-12-22 10:20:38 -08:00
|
|
|
KeyedAccount::new(&Pubkey::default(), true, &create_default_stake_account()),
|
2020-11-16 12:42:59 -08:00
|
|
|
KeyedAccount::new(&Pubkey::default(), false, &bad_vote_account),
|
2019-06-17 19:34:21 -07:00
|
|
|
KeyedAccount::new(
|
2019-07-12 16:38:15 -07:00
|
|
|
&sysvar::clock::id(),
|
2019-06-17 19:34:21 -07:00
|
|
|
false,
|
2021-03-09 13:06:07 -08:00
|
|
|
&RefCell::new(account::create_account_shared_data(
|
|
|
|
&sysvar::clock::Clock::default(),
|
|
|
|
1
|
|
|
|
))
|
2019-06-17 19:34:21 -07:00
|
|
|
),
|
2020-01-29 17:59:14 -08:00
|
|
|
KeyedAccount::new(
|
|
|
|
&sysvar::stake_history::id(),
|
|
|
|
false,
|
2021-03-09 13:06:07 -08:00
|
|
|
&RefCell::new(account::create_account_shared_data(
|
2020-10-28 22:01:07 -07:00
|
|
|
&sysvar::stake_history::StakeHistory::default(),
|
|
|
|
1
|
|
|
|
))
|
2020-01-29 17:59:14 -08:00
|
|
|
),
|
2019-08-15 14:35:48 -07:00
|
|
|
KeyedAccount::new(
|
|
|
|
&config::id(),
|
|
|
|
false,
|
2020-01-22 17:54:06 -08:00
|
|
|
&RefCell::new(config::create_account(0, &config::Config::default()))
|
2019-08-15 14:35:48 -07:00
|
|
|
),
|
2019-05-07 17:08:49 -07:00
|
|
|
],
|
2019-09-11 09:48:29 -07:00
|
|
|
&serialize(&StakeInstruction::DelegateStake).unwrap(),
|
2020-10-28 20:21:50 -07:00
|
|
|
&mut MockInvokeContext::default()
|
2019-05-07 17:08:49 -07:00
|
|
|
),
|
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
|
|
|
|
2019-07-12 16:38:15 -07:00
|
|
|
// Tests 3rd keyed account is of correct type (Clock instead of rewards) in withdraw
|
2019-06-21 22:28:34 -07:00
|
|
|
assert_eq!(
|
|
|
|
super::process_instruction(
|
|
|
|
&Pubkey::default(),
|
2020-01-22 17:54:06 -08:00
|
|
|
&[
|
2020-12-22 10:20:38 -08:00
|
|
|
KeyedAccount::new(&Pubkey::default(), false, &create_default_stake_account()),
|
2020-01-22 17:54:06 -08:00
|
|
|
KeyedAccount::new(&Pubkey::default(), false, &create_default_account()),
|
2019-06-21 22:28:34 -07:00
|
|
|
KeyedAccount::new(
|
2019-07-12 16:38:15 -07:00
|
|
|
&sysvar::rewards::id(),
|
2019-06-21 22:28:34 -07:00
|
|
|
false,
|
2021-03-09 13:06:07 -08:00
|
|
|
&RefCell::new(account::create_account_shared_data(
|
2020-10-28 22:01:07 -07:00
|
|
|
&sysvar::rewards::Rewards::new(0.0),
|
|
|
|
1
|
|
|
|
))
|
2019-06-21 22:28:34 -07:00
|
|
|
),
|
2019-08-12 20:59:57 -07:00
|
|
|
KeyedAccount::new(
|
|
|
|
&sysvar::stake_history::id(),
|
|
|
|
false,
|
2021-03-09 13:06:07 -08:00
|
|
|
&RefCell::new(account::create_account_shared_data(
|
|
|
|
&StakeHistory::default(),
|
|
|
|
1,
|
|
|
|
))
|
2019-08-12 20:59:57 -07:00
|
|
|
),
|
2019-06-21 22:28:34 -07:00
|
|
|
],
|
|
|
|
&serialize(&StakeInstruction::Withdraw(42)).unwrap(),
|
2020-10-28 20:21:50 -07:00
|
|
|
&mut MockInvokeContext::default()
|
2019-06-21 22:28:34 -07:00
|
|
|
),
|
|
|
|
Err(InstructionError::InvalidArgument),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Tests correct number of accounts are provided in withdraw
|
|
|
|
assert_eq!(
|
|
|
|
super::process_instruction(
|
|
|
|
&Pubkey::default(),
|
2020-01-22 17:54:06 -08:00
|
|
|
&[KeyedAccount::new(
|
2019-10-14 15:02:24 -07:00
|
|
|
&Pubkey::default(),
|
|
|
|
false,
|
2020-12-22 10:20:38 -08:00
|
|
|
&create_default_stake_account()
|
2019-10-14 15:02:24 -07:00
|
|
|
)],
|
2019-06-21 22:28:34 -07:00
|
|
|
&serialize(&StakeInstruction::Withdraw(42)).unwrap(),
|
2020-10-28 20:21:50 -07:00
|
|
|
&mut MockInvokeContext::default()
|
2019-06-21 22:28:34 -07:00
|
|
|
),
|
2019-10-14 15:02:24 -07:00
|
|
|
Err(InstructionError::NotEnoughAccountKeys),
|
2019-06-21 22:28:34 -07:00
|
|
|
);
|
2019-06-21 23:45:03 -07:00
|
|
|
|
2019-07-12 16:38:15 -07:00
|
|
|
// Tests 2nd keyed account is of correct type (Clock instead of rewards) in deactivate
|
2019-06-21 23:45:03 -07:00
|
|
|
assert_eq!(
|
|
|
|
super::process_instruction(
|
|
|
|
&Pubkey::default(),
|
2020-01-22 17:54:06 -08:00
|
|
|
&[
|
2020-12-22 10:20:38 -08:00
|
|
|
KeyedAccount::new(&Pubkey::default(), false, &create_default_stake_account()),
|
2019-06-21 23:45:03 -07:00
|
|
|
KeyedAccount::new(
|
2019-07-12 16:38:15 -07:00
|
|
|
&sysvar::rewards::id(),
|
2019-06-21 23:45:03 -07:00
|
|
|
false,
|
2021-03-09 13:06:07 -08:00
|
|
|
&RefCell::new(account::create_account_shared_data(
|
2020-10-28 22:01:07 -07:00
|
|
|
&sysvar::rewards::Rewards::new(0.0),
|
|
|
|
1
|
|
|
|
))
|
2019-06-21 23:45:03 -07:00
|
|
|
),
|
|
|
|
],
|
|
|
|
&serialize(&StakeInstruction::Deactivate).unwrap(),
|
2020-10-28 20:21:50 -07:00
|
|
|
&mut MockInvokeContext::default()
|
2019-06-21 23:45:03 -07:00
|
|
|
),
|
|
|
|
Err(InstructionError::InvalidArgument),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Tests correct number of accounts are provided in deactivate
|
|
|
|
assert_eq!(
|
|
|
|
super::process_instruction(
|
|
|
|
&Pubkey::default(),
|
2020-01-22 17:54:06 -08:00
|
|
|
&[],
|
2019-06-21 23:45:03 -07:00
|
|
|
&serialize(&StakeInstruction::Deactivate).unwrap(),
|
2020-10-28 20:21:50 -07:00
|
|
|
&mut MockInvokeContext::default()
|
2019-06-21 23:45:03 -07:00
|
|
|
),
|
2019-10-14 15:02:24 -07:00
|
|
|
Err(InstructionError::NotEnoughAccountKeys),
|
2019-06-21 23:45:03 -07:00
|
|
|
);
|
2019-04-01 16:45:53 -07:00
|
|
|
}
|
|
|
|
|
2019-09-09 18:17:32 -07:00
|
|
|
#[test]
|
|
|
|
fn test_custom_error_decode() {
|
|
|
|
use num_traits::FromPrimitive;
|
|
|
|
fn pretty_err<T>(err: InstructionError) -> String
|
|
|
|
where
|
|
|
|
T: 'static + std::error::Error + DecodeError<T> + FromPrimitive,
|
|
|
|
{
|
2020-04-01 09:01:11 -07:00
|
|
|
if let InstructionError::Custom(code) = err {
|
2019-09-09 18:17:32 -07:00
|
|
|
let specific_error: T = T::decode_custom_error_to_enum(code).unwrap();
|
|
|
|
format!(
|
|
|
|
"{:?}: {}::{:?} - {}",
|
|
|
|
err,
|
|
|
|
T::type_of(),
|
|
|
|
specific_error,
|
|
|
|
specific_error,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
"".to_string()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert_eq!(
|
2020-04-01 09:01:11 -07:00
|
|
|
"Custom(0): StakeError::NoCreditsToRedeem - not enough credits to redeem",
|
2019-09-09 18:17:32 -07:00
|
|
|
pretty_err::<StakeError>(StakeError::NoCreditsToRedeem.into())
|
|
|
|
)
|
|
|
|
}
|
2019-04-01 16:45:53 -07:00
|
|
|
}
|