2021-06-15 09:04:00 -07:00
|
|
|
use {
|
|
|
|
crate::{config, stake_state::StakeAccount},
|
|
|
|
log::*,
|
|
|
|
solana_sdk::{
|
|
|
|
feature_set,
|
|
|
|
instruction::InstructionError,
|
|
|
|
keyed_account::{from_keyed_account, get_signers, keyed_account_at_index},
|
|
|
|
process_instruction::{get_sysvar, InvokeContext},
|
|
|
|
program_utils::limited_deserialize,
|
|
|
|
pubkey::Pubkey,
|
|
|
|
stake::{instruction::StakeInstruction, program::id},
|
|
|
|
sysvar::{self, clock::Clock, rent::Rent, stake_history::StakeHistory},
|
|
|
|
},
|
2019-08-15 14:35:48 -07:00
|
|
|
};
|
2020-08-21 11:28:01 -07:00
|
|
|
|
2021-06-15 09:04:00 -07:00
|
|
|
#[deprecated(
|
|
|
|
since = "1.8.0",
|
|
|
|
note = "Please use `solana_sdk::stake::instruction` or `solana_program::stake::instruction` instead"
|
|
|
|
)]
|
|
|
|
pub use solana_sdk::stake::instruction::*;
|
2020-01-28 20:59:53 -08:00
|
|
|
|
2019-04-01 16:45:53 -07:00
|
|
|
pub fn process_instruction(
|
|
|
|
_program_id: &Pubkey,
|
|
|
|
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> {
|
2021-04-19 09:48:48 -07:00
|
|
|
let keyed_accounts = invoke_context.get_keyed_accounts()?;
|
|
|
|
|
2019-04-01 16:45:53 -07:00
|
|
|
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
|
|
|
|
2021-04-19 09:48:48 -07:00
|
|
|
let me = &keyed_account_at_index(keyed_accounts, 0)?;
|
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,
|
2021-04-19 09:48:48 -07:00
|
|
|
&from_keyed_account::<Rent>(keyed_account_at_index(keyed_accounts, 1)?)?,
|
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 {
|
2021-04-19 09:48:48 -07:00
|
|
|
let clock =
|
|
|
|
from_keyed_account::<Clock>(keyed_account_at_index(keyed_accounts, 1)?)?;
|
|
|
|
let _current_authority = keyed_account_at_index(keyed_accounts, 2)?;
|
|
|
|
let custodian =
|
|
|
|
keyed_account_at_index(keyed_accounts, 3).map(|ka| ka.unsigned_key());
|
2021-02-05 22:40:07 -08:00
|
|
|
|
|
|
|
me.authorize(
|
|
|
|
&signers,
|
|
|
|
&authorized_pubkey,
|
|
|
|
stake_authorize,
|
|
|
|
require_custodian_for_locked_stake_authorize,
|
|
|
|
&clock,
|
2021-04-19 09:48:48 -07:00
|
|
|
custodian.ok(),
|
2021-02-05 22:40:07 -08:00
|
|
|
)
|
|
|
|
} 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) => {
|
2021-04-19 09:48:48 -07:00
|
|
|
let authority_base = keyed_account_at_index(keyed_accounts, 1)?;
|
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 {
|
2021-04-19 09:48:48 -07:00
|
|
|
let clock =
|
|
|
|
from_keyed_account::<Clock>(keyed_account_at_index(keyed_accounts, 2)?)?;
|
|
|
|
let custodian =
|
|
|
|
keyed_account_at_index(keyed_accounts, 3).map(|ka| ka.unsigned_key());
|
2021-02-05 22:40:07 -08:00
|
|
|
|
|
|
|
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,
|
2021-04-19 09:48:48 -07:00
|
|
|
custodian.ok(),
|
2021-02-05 22:40:07 -08:00
|
|
|
)
|
|
|
|
} 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 => {
|
2021-05-20 22:31:55 -07:00
|
|
|
let can_reverse_deactivation =
|
|
|
|
invoke_context.is_feature_active(&feature_set::stake_program_v4::id());
|
2021-04-19 09:48:48 -07:00
|
|
|
let vote = keyed_account_at_index(keyed_accounts, 1)?;
|
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,
|
2021-04-19 09:48:48 -07:00
|
|
|
&from_keyed_account::<Clock>(keyed_account_at_index(keyed_accounts, 2)?)?,
|
|
|
|
&from_keyed_account::<StakeHistory>(keyed_account_at_index(keyed_accounts, 3)?)?,
|
|
|
|
&config::from_keyed_account(keyed_account_at_index(keyed_accounts, 4)?)?,
|
2019-10-14 15:02:24 -07:00
|
|
|
&signers,
|
2021-05-20 22:31:55 -07:00
|
|
|
can_reverse_deactivation,
|
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) => {
|
2021-04-19 09:48:48 -07:00
|
|
|
let split_stake = &keyed_account_at_index(keyed_accounts, 1)?;
|
2019-10-31 11:07:27 -07:00
|
|
|
me.split(lamports, split_stake, &signers)
|
|
|
|
}
|
2020-06-10 17:22:47 -07:00
|
|
|
StakeInstruction::Merge => {
|
2021-04-19 09:48:48 -07:00
|
|
|
let source_stake = &keyed_account_at_index(keyed_accounts, 1)?;
|
2021-05-20 19:23:36 -07:00
|
|
|
let can_merge_expired_lockups =
|
|
|
|
invoke_context.is_feature_active(&feature_set::stake_program_v4::id());
|
2020-06-10 17:22:47 -07:00
|
|
|
me.merge(
|
2021-01-21 09:59:24 -08:00
|
|
|
invoke_context,
|
2020-06-10 17:22:47 -07:00
|
|
|
source_stake,
|
2021-04-19 09:48:48 -07:00
|
|
|
&from_keyed_account::<Clock>(keyed_account_at_index(keyed_accounts, 2)?)?,
|
|
|
|
&from_keyed_account::<StakeHistory>(keyed_account_at_index(keyed_accounts, 3)?)?,
|
2020-06-10 17:22:47 -07:00
|
|
|
&signers,
|
2021-05-20 19:23:36 -07:00
|
|
|
can_merge_expired_lockups,
|
2020-06-10 17:22:47 -07:00
|
|
|
)
|
|
|
|
}
|
2019-10-31 11:07:27 -07:00
|
|
|
|
2019-06-21 22:28:34 -07:00
|
|
|
StakeInstruction::Withdraw(lamports) => {
|
2021-04-19 09:48:48 -07:00
|
|
|
let to = &keyed_account_at_index(keyed_accounts, 1)?;
|
2019-06-21 22:28:34 -07:00
|
|
|
me.withdraw(
|
|
|
|
lamports,
|
2019-10-14 15:02:24 -07:00
|
|
|
to,
|
2021-04-19 09:48:48 -07:00
|
|
|
&from_keyed_account::<Clock>(keyed_account_at_index(keyed_accounts, 2)?)?,
|
|
|
|
&from_keyed_account::<StakeHistory>(keyed_account_at_index(keyed_accounts, 3)?)?,
|
|
|
|
keyed_account_at_index(keyed_accounts, 4)?,
|
|
|
|
keyed_account_at_index(keyed_accounts, 5).ok(),
|
2021-05-20 19:01:08 -07:00
|
|
|
invoke_context.is_feature_active(&feature_set::stake_program_v4::id()),
|
2019-06-21 22:28:34 -07:00
|
|
|
)
|
|
|
|
}
|
2020-01-29 17:59:14 -08:00
|
|
|
StakeInstruction::Deactivate => me.deactivate(
|
2021-04-19 09:48:48 -07:00
|
|
|
&from_keyed_account::<Clock>(keyed_account_at_index(keyed_accounts, 1)?)?,
|
2019-10-14 15:02:24 -07:00
|
|
|
&signers,
|
|
|
|
),
|
2020-01-28 20:59:53 -08:00
|
|
|
|
2021-05-20 14:04:07 -07:00
|
|
|
StakeInstruction::SetLockup(lockup) => {
|
|
|
|
let clock = if invoke_context.is_feature_active(&feature_set::stake_program_v4::id()) {
|
|
|
|
Some(get_sysvar::<Clock>(invoke_context, &sysvar::clock::id())?)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
me.set_lockup(&lockup, &signers, clock.as_ref())
|
|
|
|
}
|
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-04-23 10:34:22 -07:00
|
|
|
account::{self, Account, AccountSharedData, WritableAccount},
|
2021-06-15 09:04:00 -07:00
|
|
|
instruction::Instruction,
|
2021-04-19 09:48:48 -07:00
|
|
|
keyed_account::KeyedAccount,
|
2021-05-20 14:04:07 -07:00
|
|
|
process_instruction::{mock_set_sysvar, MockInvokeContext},
|
2020-10-26 09:14:57 -07:00
|
|
|
rent::Rent,
|
2021-06-15 09:04:00 -07:00
|
|
|
stake::{
|
|
|
|
config as stake_config,
|
|
|
|
instruction::{self, LockupArgs},
|
|
|
|
state::{Authorized, Lockup, StakeAuthorize},
|
|
|
|
},
|
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-24 23:23:20 -07:00
|
|
|
account::create_account_shared_data_for_test(&sysvar::clock::Clock::default())
|
2019-07-12 16:38:15 -07:00
|
|
|
} else if sysvar::rewards::check_id(&meta.pubkey) {
|
2021-03-24 23:23:20 -07:00
|
|
|
account::create_account_shared_data_for_test(&sysvar::rewards::Rewards::new(
|
|
|
|
0.0,
|
|
|
|
))
|
2019-08-12 20:59:57 -07:00
|
|
|
} else if sysvar::stake_history::check_id(&meta.pubkey) {
|
2021-03-24 23:23:20 -07:00
|
|
|
account::create_account_shared_data_for_test(&StakeHistory::default())
|
2021-06-15 09:04:00 -07:00
|
|
|
} else if stake_config::check_id(&meta.pubkey) {
|
|
|
|
config::create_account(0, &stake_config::Config::default())
|
2019-10-03 14:22:48 -07:00
|
|
|
} else if sysvar::rent::check_id(&meta.pubkey) {
|
2021-03-24 23:23:20 -07:00
|
|
|
account::create_account_shared_data_for_test(&Rent::default())
|
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();
|
2021-05-20 14:04:07 -07:00
|
|
|
|
|
|
|
let mut invoke_context = MockInvokeContext::new(keyed_accounts);
|
|
|
|
mock_set_sysvar(
|
|
|
|
&mut invoke_context,
|
|
|
|
sysvar::clock::id(),
|
|
|
|
sysvar::clock::Clock::default(),
|
2020-10-28 20:21:50 -07:00
|
|
|
)
|
2021-05-20 14:04:07 -07:00
|
|
|
.unwrap();
|
|
|
|
super::process_instruction(&Pubkey::default(), &instruction.data, &mut invoke_context)
|
2019-05-07 17:08:49 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_stake_process_instruction() {
|
2019-10-31 11:07:27 -07:00
|
|
|
assert_eq!(
|
2021-06-15 09:04:00 -07:00
|
|
|
process_instruction(&instruction::initialize(
|
2019-10-31 11:07:27 -07:00
|
|
|
&Pubkey::default(),
|
|
|
|
&Authorized::default(),
|
|
|
|
&Lockup::default()
|
|
|
|
)),
|
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2021-06-15 09:04:00 -07:00
|
|
|
process_instruction(&instruction::authorize(
|
2019-10-31 11:07:27 -07:00
|
|
|
&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(
|
2021-06-15 09:04:00 -07:00
|
|
|
&instruction::split(
|
2019-10-31 11:07:27 -07:00
|
|
|
&Pubkey::default(),
|
|
|
|
&Pubkey::default(),
|
|
|
|
100,
|
2020-11-16 12:42:59 -08:00
|
|
|
&invalid_stake_state_pubkey(),
|
2021-06-05 16:45:45 -07:00
|
|
|
)[2]
|
2020-01-20 12:33:27 -08:00
|
|
|
),
|
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
2020-06-10 17:22:47 -07:00
|
|
|
assert_eq!(
|
|
|
|
process_instruction(
|
2021-06-15 09:04:00 -07:00
|
|
|
&instruction::merge(
|
2020-11-16 12:42:59 -08:00
|
|
|
&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(
|
2021-06-15 09:04:00 -07:00
|
|
|
&instruction::split_with_seed(
|
2020-01-20 12:33:27 -08:00
|
|
|
&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!(
|
2021-06-15 09:04:00 -07:00
|
|
|
process_instruction(&instruction::delegate_stake(
|
2019-09-29 21:18:15 -07:00
|
|
|
&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!(
|
2021-06-15 09:04:00 -07:00
|
|
|
process_instruction(&instruction::withdraw(
|
2019-09-29 21:18:15 -07:00
|
|
|
&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!(
|
2021-06-15 09:04:00 -07:00
|
|
|
process_instruction(&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!(
|
2021-06-15 09:04:00 -07:00
|
|
|
process_instruction(&instruction::set_lockup(
|
2020-01-28 20:59:53 -08:00
|
|
|
&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!(
|
2021-06-15 09:04:00 -07:00
|
|
|
process_instruction(&instruction::initialize(
|
2020-12-22 10:20:38 -08:00
|
|
|
&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!(
|
2021-06-15 09:04:00 -07:00
|
|
|
process_instruction(&instruction::authorize(
|
2020-12-22 10:20:38 -08:00
|
|
|
&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(
|
2021-06-15 09:04:00 -07:00
|
|
|
&instruction::split(
|
2020-12-22 10:20:38 -08:00
|
|
|
&spoofed_stake_state_pubkey(),
|
|
|
|
&Pubkey::default(),
|
|
|
|
100,
|
|
|
|
&Pubkey::default(),
|
2021-06-05 16:45:45 -07:00
|
|
|
)[2]
|
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(
|
2021-06-15 09:04:00 -07:00
|
|
|
&instruction::split(
|
2020-12-22 10:20:38 -08:00
|
|
|
&Pubkey::default(),
|
|
|
|
&Pubkey::default(),
|
|
|
|
100,
|
|
|
|
&spoofed_stake_state_pubkey(),
|
2021-06-05 16:45:45 -07:00
|
|
|
)[2]
|
2020-12-22 10:20:38 -08:00
|
|
|
),
|
|
|
|
Err(InstructionError::IncorrectProgramId),
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
process_instruction(
|
2021-06-15 09:04:00 -07:00
|
|
|
&instruction::merge(
|
2020-12-22 10:20:38 -08:00
|
|
|
&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(
|
2021-06-15 09:04:00 -07:00
|
|
|
&instruction::merge(
|
2020-12-22 10:20:38 -08:00
|
|
|
&Pubkey::default(),
|
|
|
|
&spoofed_stake_state_pubkey(),
|
|
|
|
&Pubkey::default(),
|
|
|
|
)[0]
|
|
|
|
),
|
|
|
|
Err(InstructionError::IncorrectProgramId),
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
process_instruction(
|
2021-06-15 09:04:00 -07:00
|
|
|
&instruction::split_with_seed(
|
2020-12-22 10:20:38 -08:00
|
|
|
&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!(
|
2021-06-15 09:04:00 -07:00
|
|
|
process_instruction(&instruction::delegate_stake(
|
2020-12-22 10:20:38 -08:00
|
|
|
&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!(
|
2021-06-15 09:04:00 -07:00
|
|
|
process_instruction(&instruction::withdraw(
|
2020-12-22 10:20:38 -08:00
|
|
|
&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!(
|
2021-06-15 09:04:00 -07:00
|
|
|
process_instruction(&instruction::deactivate_stake(
|
2020-12-22 10:20:38 -08:00
|
|
|
&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!(
|
2021-06-15 09:04:00 -07:00
|
|
|
process_instruction(&instruction::set_lockup(
|
2020-12-22 10:20:38 -08:00
|
|
|
&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(),
|
2019-09-26 13:29:29 -07:00
|
|
|
&serialize(&StakeInstruction::Initialize(
|
|
|
|
Authorized::default(),
|
|
|
|
Lockup::default()
|
|
|
|
))
|
|
|
|
.unwrap(),
|
2021-04-19 09:48:48 -07:00
|
|
|
&mut MockInvokeContext::new(vec![])
|
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
|
2021-04-19 09:48:48 -07:00
|
|
|
let stake_address = Pubkey::default();
|
|
|
|
let stake_account = create_default_stake_account();
|
|
|
|
let keyed_accounts = vec![KeyedAccount::new(&stake_address, false, &stake_account)];
|
2019-10-31 11:07:27 -07:00
|
|
|
assert_eq!(
|
|
|
|
super::process_instruction(
|
|
|
|
&Pubkey::default(),
|
|
|
|
&serialize(&StakeInstruction::Initialize(
|
|
|
|
Authorized::default(),
|
|
|
|
Lockup::default()
|
|
|
|
))
|
|
|
|
.unwrap(),
|
2021-04-19 09:48:48 -07:00
|
|
|
&mut MockInvokeContext::new(keyed_accounts)
|
2019-10-31 11:07:27 -07:00
|
|
|
),
|
|
|
|
Err(InstructionError::NotEnoughAccountKeys),
|
|
|
|
);
|
|
|
|
|
|
|
|
// rent fails to deserialize
|
2021-04-19 09:48:48 -07:00
|
|
|
let stake_address = Pubkey::default();
|
|
|
|
let stake_account = create_default_stake_account();
|
|
|
|
let rent_address = sysvar::rent::id();
|
|
|
|
let rent_account = create_default_account();
|
|
|
|
let keyed_accounts = vec![
|
|
|
|
KeyedAccount::new(&stake_address, false, &stake_account),
|
|
|
|
KeyedAccount::new(&rent_address, false, &rent_account),
|
|
|
|
];
|
2019-10-31 11:07:27 -07:00
|
|
|
assert_eq!(
|
|
|
|
super::process_instruction(
|
|
|
|
&Pubkey::default(),
|
|
|
|
&serialize(&StakeInstruction::Initialize(
|
|
|
|
Authorized::default(),
|
|
|
|
Lockup::default()
|
|
|
|
))
|
|
|
|
.unwrap(),
|
2021-04-19 09:48:48 -07:00
|
|
|
&mut MockInvokeContext::new(keyed_accounts)
|
2019-10-31 11:07:27 -07:00
|
|
|
),
|
|
|
|
Err(InstructionError::InvalidArgument),
|
|
|
|
);
|
|
|
|
|
|
|
|
// fails to deserialize stake state
|
2021-04-19 09:48:48 -07:00
|
|
|
let stake_address = Pubkey::default();
|
|
|
|
let stake_account = create_default_stake_account();
|
|
|
|
let rent_address = sysvar::rent::id();
|
|
|
|
let rent_account = RefCell::new(account::create_account_shared_data_for_test(
|
|
|
|
&Rent::default(),
|
|
|
|
));
|
|
|
|
let keyed_accounts = vec![
|
|
|
|
KeyedAccount::new(&stake_address, false, &stake_account),
|
|
|
|
KeyedAccount::new(&rent_address, false, &rent_account),
|
|
|
|
];
|
2019-10-31 11:07:27 -07:00
|
|
|
assert_eq!(
|
|
|
|
super::process_instruction(
|
|
|
|
&Pubkey::default(),
|
|
|
|
&serialize(&StakeInstruction::Initialize(
|
|
|
|
Authorized::default(),
|
|
|
|
Lockup::default()
|
|
|
|
))
|
|
|
|
.unwrap(),
|
2021-04-19 09:48:48 -07:00
|
|
|
&mut MockInvokeContext::new(keyed_accounts)
|
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
|
2021-04-19 09:48:48 -07:00
|
|
|
let stake_address = Pubkey::default();
|
|
|
|
let stake_account = create_default_stake_account();
|
|
|
|
let keyed_accounts = vec![KeyedAccount::new(&stake_address, false, &stake_account)];
|
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(),
|
2019-09-11 09:48:29 -07:00
|
|
|
&serialize(&StakeInstruction::DelegateStake).unwrap(),
|
2021-04-19 09:48:48 -07:00
|
|
|
&mut MockInvokeContext::new(keyed_accounts)
|
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
|
2021-04-19 09:48:48 -07:00
|
|
|
let stake_address = Pubkey::default();
|
|
|
|
let stake_account = create_default_stake_account();
|
|
|
|
let keyed_accounts = vec![KeyedAccount::new(&stake_address, false, &stake_account)];
|
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(),
|
2019-09-11 09:48:29 -07:00
|
|
|
&serialize(&StakeInstruction::DelegateStake).unwrap(),
|
2021-04-19 09:48:48 -07:00
|
|
|
&mut MockInvokeContext::new(keyed_accounts)
|
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
|
2021-04-19 09:48:48 -07:00
|
|
|
let stake_address = Pubkey::default();
|
|
|
|
let stake_account = create_default_stake_account();
|
|
|
|
let vote_address = Pubkey::default();
|
2020-11-16 12:42:59 -08:00
|
|
|
let mut bad_vote_account = create_default_account();
|
2021-04-23 10:34:22 -07:00
|
|
|
bad_vote_account
|
|
|
|
.get_mut()
|
|
|
|
.set_owner(solana_vote_program::id());
|
2021-04-19 09:48:48 -07:00
|
|
|
let clock_address = sysvar::clock::id();
|
|
|
|
let clock_account = RefCell::new(account::create_account_shared_data_for_test(
|
|
|
|
&sysvar::clock::Clock::default(),
|
|
|
|
));
|
|
|
|
let stake_history_address = sysvar::stake_history::id();
|
|
|
|
let stake_history_account = RefCell::new(account::create_account_shared_data_for_test(
|
|
|
|
&sysvar::stake_history::StakeHistory::default(),
|
|
|
|
));
|
2021-06-15 09:04:00 -07:00
|
|
|
let config_address = stake_config::id();
|
|
|
|
let config_account =
|
|
|
|
RefCell::new(config::create_account(0, &stake_config::Config::default()));
|
2021-04-19 09:48:48 -07:00
|
|
|
let keyed_accounts = vec![
|
|
|
|
KeyedAccount::new(&stake_address, true, &stake_account),
|
|
|
|
KeyedAccount::new(&vote_address, false, &bad_vote_account),
|
|
|
|
KeyedAccount::new(&clock_address, false, &clock_account),
|
|
|
|
KeyedAccount::new(&stake_history_address, false, &stake_history_account),
|
|
|
|
KeyedAccount::new(&config_address, false, &config_account),
|
|
|
|
];
|
2019-05-07 17:08:49 -07:00
|
|
|
assert_eq!(
|
|
|
|
super::process_instruction(
|
|
|
|
&Pubkey::default(),
|
2019-09-11 09:48:29 -07:00
|
|
|
&serialize(&StakeInstruction::DelegateStake).unwrap(),
|
2021-04-19 09:48:48 -07:00
|
|
|
&mut MockInvokeContext::new(keyed_accounts)
|
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
|
2021-04-19 09:48:48 -07:00
|
|
|
let stake_address = Pubkey::default();
|
|
|
|
let stake_account = create_default_stake_account();
|
|
|
|
let vote_address = Pubkey::default();
|
|
|
|
let vote_account = create_default_account();
|
|
|
|
let rewards_address = sysvar::rewards::id();
|
|
|
|
let rewards_account = RefCell::new(account::create_account_shared_data_for_test(
|
|
|
|
&sysvar::rewards::Rewards::new(0.0),
|
|
|
|
));
|
|
|
|
let stake_history_address = sysvar::stake_history::id();
|
|
|
|
let stake_history_account = RefCell::new(account::create_account_shared_data_for_test(
|
|
|
|
&StakeHistory::default(),
|
|
|
|
));
|
|
|
|
let keyed_accounts = vec![
|
|
|
|
KeyedAccount::new(&stake_address, false, &stake_account),
|
|
|
|
KeyedAccount::new(&vote_address, false, &vote_account),
|
|
|
|
KeyedAccount::new(&rewards_address, false, &rewards_account),
|
|
|
|
KeyedAccount::new(&stake_history_address, false, &stake_history_account),
|
|
|
|
];
|
2019-06-21 22:28:34 -07:00
|
|
|
assert_eq!(
|
|
|
|
super::process_instruction(
|
|
|
|
&Pubkey::default(),
|
|
|
|
&serialize(&StakeInstruction::Withdraw(42)).unwrap(),
|
2021-04-19 09:48:48 -07:00
|
|
|
&mut MockInvokeContext::new(keyed_accounts)
|
2019-06-21 22:28:34 -07:00
|
|
|
),
|
|
|
|
Err(InstructionError::InvalidArgument),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Tests correct number of accounts are provided in withdraw
|
2021-04-19 09:48:48 -07:00
|
|
|
let stake_address = Pubkey::default();
|
|
|
|
let stake_account = create_default_stake_account();
|
|
|
|
let keyed_accounts = vec![KeyedAccount::new(&stake_address, false, &stake_account)];
|
2019-06-21 22:28:34 -07:00
|
|
|
assert_eq!(
|
|
|
|
super::process_instruction(
|
|
|
|
&Pubkey::default(),
|
|
|
|
&serialize(&StakeInstruction::Withdraw(42)).unwrap(),
|
2021-04-19 09:48:48 -07:00
|
|
|
&mut MockInvokeContext::new(keyed_accounts)
|
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
|
2021-04-19 09:48:48 -07:00
|
|
|
let stake_address = Pubkey::default();
|
|
|
|
let stake_account = create_default_stake_account();
|
|
|
|
let rewards_address = sysvar::rewards::id();
|
|
|
|
let rewards_account = RefCell::new(account::create_account_shared_data_for_test(
|
|
|
|
&sysvar::rewards::Rewards::new(0.0),
|
|
|
|
));
|
|
|
|
let keyed_accounts = vec![
|
|
|
|
KeyedAccount::new(&stake_address, false, &stake_account),
|
|
|
|
KeyedAccount::new(&rewards_address, false, &rewards_account),
|
|
|
|
];
|
2019-06-21 23:45:03 -07:00
|
|
|
assert_eq!(
|
|
|
|
super::process_instruction(
|
|
|
|
&Pubkey::default(),
|
|
|
|
&serialize(&StakeInstruction::Deactivate).unwrap(),
|
2021-04-19 09:48:48 -07:00
|
|
|
&mut MockInvokeContext::new(keyed_accounts)
|
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(),
|
|
|
|
&serialize(&StakeInstruction::Deactivate).unwrap(),
|
2021-04-19 09:48:48 -07:00
|
|
|
&mut MockInvokeContext::new(vec![])
|
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
|
|
|
}
|
|
|
|
}
|