2021-06-15 09:04:00 -07:00
|
|
|
use {
|
|
|
|
crate::{config, stake_state::StakeAccount},
|
|
|
|
log::*,
|
2021-11-17 10:35:07 -08:00
|
|
|
solana_program_runtime::invoke_context::{get_sysvar, InvokeContext},
|
2021-06-15 09:04:00 -07:00
|
|
|
solana_sdk::{
|
|
|
|
feature_set,
|
|
|
|
instruction::InstructionError,
|
|
|
|
keyed_account::{from_keyed_account, get_signers, keyed_account_at_index},
|
|
|
|
program_utils::limited_deserialize,
|
2021-06-30 17:34:50 -07:00
|
|
|
stake::{
|
|
|
|
instruction::StakeInstruction,
|
|
|
|
program::id,
|
|
|
|
state::{Authorized, Lockup},
|
|
|
|
},
|
2021-06-15 09:04:00 -07:00
|
|
|
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(
|
2021-10-08 02:41:07 -07:00
|
|
|
first_instruction_account: usize,
|
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> {
|
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);
|
|
|
|
|
2021-10-08 02:41:07 -07:00
|
|
|
let me = &keyed_account_at_index(keyed_accounts, first_instruction_account)?;
|
2020-12-22 10:20:38 -08:00
|
|
|
if me.owner()? != id() {
|
2021-08-31 14:51:26 -07:00
|
|
|
return Err(InstructionError::InvalidAccountOwner);
|
2020-12-22 10:20:38 -08:00
|
|
|
}
|
|
|
|
|
2021-10-21 11:57:42 -07:00
|
|
|
let signers = get_signers(&keyed_accounts[first_instruction_account..]);
|
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-10-08 02:41:07 -07:00
|
|
|
&from_keyed_account::<Rent>(keyed_account_at_index(
|
|
|
|
keyed_accounts,
|
|
|
|
first_instruction_account + 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-10-08 02:41:07 -07:00
|
|
|
let clock = from_keyed_account::<Clock>(keyed_account_at_index(
|
|
|
|
keyed_accounts,
|
|
|
|
first_instruction_account + 1,
|
|
|
|
)?)?;
|
|
|
|
let _current_authority =
|
|
|
|
keyed_account_at_index(keyed_accounts, first_instruction_account + 2)?;
|
|
|
|
let custodian =
|
|
|
|
keyed_account_at_index(keyed_accounts, first_instruction_account + 3)
|
|
|
|
.ok()
|
|
|
|
.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-06-30 17:34:50 -07:00
|
|
|
custodian,
|
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-10-08 02:41:07 -07:00
|
|
|
let authority_base =
|
|
|
|
keyed_account_at_index(keyed_accounts, first_instruction_account + 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-10-08 02:41:07 -07:00
|
|
|
let clock = from_keyed_account::<Clock>(keyed_account_at_index(
|
|
|
|
keyed_accounts,
|
|
|
|
first_instruction_account + 2,
|
|
|
|
)?)?;
|
|
|
|
let custodian =
|
|
|
|
keyed_account_at_index(keyed_accounts, first_instruction_account + 3)
|
|
|
|
.ok()
|
|
|
|
.map(|ka| ka.unsigned_key());
|
2021-02-05 22:40:07 -08:00
|
|
|
|
|
|
|
me.authorize_with_seed(
|
2021-06-18 06:34:46 -07:00
|
|
|
authority_base,
|
2021-02-05 22:40:07 -08:00
|
|
|
&args.authority_seed,
|
|
|
|
&args.authority_owner,
|
|
|
|
&args.new_authorized_pubkey,
|
|
|
|
args.stake_authorize,
|
|
|
|
require_custodian_for_locked_stake_authorize,
|
|
|
|
&clock,
|
2021-06-30 17:34:50 -07:00
|
|
|
custodian,
|
2021-02-05 22:40:07 -08:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
me.authorize_with_seed(
|
2021-06-18 06:34:46 -07:00
|
|
|
authority_base,
|
2021-02-05 22:40:07 -08:00
|
|
|
&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-10-08 02:41:07 -07:00
|
|
|
let vote = keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?;
|
2019-06-17 19:34:21 -07:00
|
|
|
|
2020-01-29 17:59:14 -08:00
|
|
|
me.delegate(
|
2021-06-18 06:34:46 -07:00
|
|
|
vote,
|
2021-10-08 02:41:07 -07:00
|
|
|
&from_keyed_account::<Clock>(keyed_account_at_index(
|
|
|
|
keyed_accounts,
|
|
|
|
first_instruction_account + 2,
|
|
|
|
)?)?,
|
|
|
|
&from_keyed_account::<StakeHistory>(keyed_account_at_index(
|
|
|
|
keyed_accounts,
|
|
|
|
first_instruction_account + 3,
|
|
|
|
)?)?,
|
|
|
|
&config::from_keyed_account(keyed_account_at_index(
|
|
|
|
keyed_accounts,
|
|
|
|
first_instruction_account + 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-10-08 02:41:07 -07:00
|
|
|
let split_stake =
|
|
|
|
&keyed_account_at_index(keyed_accounts, first_instruction_account + 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-10-08 02:41:07 -07:00
|
|
|
let source_stake =
|
|
|
|
&keyed_account_at_index(keyed_accounts, first_instruction_account + 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-10-08 02:41:07 -07:00
|
|
|
&from_keyed_account::<Clock>(keyed_account_at_index(
|
|
|
|
keyed_accounts,
|
|
|
|
first_instruction_account + 2,
|
|
|
|
)?)?,
|
|
|
|
&from_keyed_account::<StakeHistory>(keyed_account_at_index(
|
|
|
|
keyed_accounts,
|
|
|
|
first_instruction_account + 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-06-21 22:28:34 -07:00
|
|
|
StakeInstruction::Withdraw(lamports) => {
|
2021-10-08 02:41:07 -07:00
|
|
|
let to = &keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?;
|
2019-06-21 22:28:34 -07:00
|
|
|
me.withdraw(
|
|
|
|
lamports,
|
2019-10-14 15:02:24 -07:00
|
|
|
to,
|
2021-10-08 02:41:07 -07:00
|
|
|
&from_keyed_account::<Clock>(keyed_account_at_index(
|
|
|
|
keyed_accounts,
|
|
|
|
first_instruction_account + 2,
|
|
|
|
)?)?,
|
|
|
|
&from_keyed_account::<StakeHistory>(keyed_account_at_index(
|
|
|
|
keyed_accounts,
|
|
|
|
first_instruction_account + 3,
|
|
|
|
)?)?,
|
|
|
|
keyed_account_at_index(keyed_accounts, first_instruction_account + 4)?,
|
|
|
|
keyed_account_at_index(keyed_accounts, first_instruction_account + 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-10-08 02:41:07 -07:00
|
|
|
&from_keyed_account::<Clock>(keyed_account_at_index(
|
|
|
|
keyed_accounts,
|
|
|
|
first_instruction_account + 1,
|
|
|
|
)?)?,
|
2019-10-14 15:02:24 -07:00
|
|
|
&signers,
|
|
|
|
),
|
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())
|
|
|
|
}
|
2021-06-30 17:34:50 -07:00
|
|
|
StakeInstruction::InitializeChecked => {
|
|
|
|
if invoke_context.is_feature_active(&feature_set::vote_stake_checked_instructions::id())
|
|
|
|
{
|
|
|
|
let authorized = Authorized {
|
2021-10-08 02:41:07 -07:00
|
|
|
staker: *keyed_account_at_index(keyed_accounts, first_instruction_account + 2)?
|
|
|
|
.unsigned_key(),
|
|
|
|
withdrawer: *keyed_account_at_index(
|
|
|
|
keyed_accounts,
|
|
|
|
first_instruction_account + 3,
|
|
|
|
)?
|
|
|
|
.signer_key()
|
|
|
|
.ok_or(InstructionError::MissingRequiredSignature)?,
|
2021-06-30 17:34:50 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
me.initialize(
|
|
|
|
&authorized,
|
|
|
|
&Lockup::default(),
|
2021-10-08 02:41:07 -07:00
|
|
|
&from_keyed_account::<Rent>(keyed_account_at_index(
|
|
|
|
keyed_accounts,
|
|
|
|
first_instruction_account + 1,
|
|
|
|
)?)?,
|
2021-06-30 17:34:50 -07:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
Err(InstructionError::InvalidInstructionData)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
StakeInstruction::AuthorizeChecked(stake_authorize) => {
|
|
|
|
if invoke_context.is_feature_active(&feature_set::vote_stake_checked_instructions::id())
|
|
|
|
{
|
2021-10-08 02:41:07 -07:00
|
|
|
let clock = from_keyed_account::<Clock>(keyed_account_at_index(
|
|
|
|
keyed_accounts,
|
|
|
|
first_instruction_account + 1,
|
|
|
|
)?)?;
|
|
|
|
let _current_authority =
|
|
|
|
keyed_account_at_index(keyed_accounts, first_instruction_account + 2)?;
|
|
|
|
let authorized_pubkey =
|
|
|
|
&keyed_account_at_index(keyed_accounts, first_instruction_account + 3)?
|
|
|
|
.signer_key()
|
|
|
|
.ok_or(InstructionError::MissingRequiredSignature)?;
|
|
|
|
let custodian =
|
|
|
|
keyed_account_at_index(keyed_accounts, first_instruction_account + 4)
|
|
|
|
.ok()
|
|
|
|
.map(|ka| ka.unsigned_key());
|
2021-06-30 17:34:50 -07:00
|
|
|
|
|
|
|
me.authorize(
|
|
|
|
&signers,
|
|
|
|
authorized_pubkey,
|
|
|
|
stake_authorize,
|
|
|
|
true,
|
|
|
|
&clock,
|
|
|
|
custodian,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
Err(InstructionError::InvalidInstructionData)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
StakeInstruction::AuthorizeCheckedWithSeed(args) => {
|
|
|
|
if invoke_context.is_feature_active(&feature_set::vote_stake_checked_instructions::id())
|
|
|
|
{
|
2021-10-08 02:41:07 -07:00
|
|
|
let authority_base =
|
|
|
|
keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?;
|
|
|
|
let clock = from_keyed_account::<Clock>(keyed_account_at_index(
|
|
|
|
keyed_accounts,
|
|
|
|
first_instruction_account + 2,
|
|
|
|
)?)?;
|
|
|
|
let authorized_pubkey =
|
|
|
|
&keyed_account_at_index(keyed_accounts, first_instruction_account + 3)?
|
|
|
|
.signer_key()
|
|
|
|
.ok_or(InstructionError::MissingRequiredSignature)?;
|
|
|
|
let custodian =
|
|
|
|
keyed_account_at_index(keyed_accounts, first_instruction_account + 4)
|
|
|
|
.ok()
|
|
|
|
.map(|ka| ka.unsigned_key());
|
2021-06-30 17:34:50 -07:00
|
|
|
|
|
|
|
me.authorize_with_seed(
|
|
|
|
authority_base,
|
|
|
|
&args.authority_seed,
|
|
|
|
&args.authority_owner,
|
|
|
|
authorized_pubkey,
|
|
|
|
args.stake_authorize,
|
|
|
|
true,
|
|
|
|
&clock,
|
|
|
|
custodian,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
Err(InstructionError::InvalidInstructionData)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
StakeInstruction::SetLockupChecked(lockup_checked) => {
|
|
|
|
if invoke_context.is_feature_active(&feature_set::vote_stake_checked_instructions::id())
|
|
|
|
{
|
2021-10-08 02:41:07 -07:00
|
|
|
let custodian = if let Ok(custodian) =
|
|
|
|
keyed_account_at_index(keyed_accounts, first_instruction_account + 2)
|
|
|
|
{
|
2021-06-30 17:34:50 -07:00
|
|
|
Some(
|
|
|
|
*custodian
|
|
|
|
.signer_key()
|
|
|
|
.ok_or(InstructionError::MissingRequiredSignature)?,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
let lockup = LockupArgs {
|
|
|
|
unix_timestamp: lockup_checked.unix_timestamp,
|
|
|
|
epoch: lockup_checked.epoch,
|
|
|
|
custodian,
|
|
|
|
};
|
|
|
|
let clock = Some(get_sysvar::<Clock>(invoke_context, &sysvar::clock::id())?);
|
|
|
|
me.set_lockup(&lockup, &signers, clock.as_ref())
|
|
|
|
} else {
|
|
|
|
Err(InstructionError::InvalidInstructionData)
|
|
|
|
}
|
|
|
|
}
|
2019-04-01 16:45:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2021-07-06 12:24:10 -07:00
|
|
|
use crate::stake_state::{Meta, StakeState};
|
2019-04-01 16:45:53 -07:00
|
|
|
use bincode::serialize;
|
2021-11-04 13:47:32 -07:00
|
|
|
use solana_program_runtime::invoke_context::{
|
|
|
|
mock_process_instruction, prepare_mock_invoke_context, ThisInvokeContext,
|
|
|
|
};
|
2020-10-26 09:14:57 -07:00
|
|
|
use solana_sdk::{
|
2021-11-04 13:47:32 -07:00
|
|
|
account::{self, AccountSharedData},
|
|
|
|
feature_set::FeatureSet,
|
2021-07-06 12:24:10 -07:00
|
|
|
instruction::{AccountMeta, Instruction},
|
2021-10-10 13:29:18 -07:00
|
|
|
pubkey::Pubkey,
|
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},
|
|
|
|
},
|
2021-10-21 11:57:42 -07:00
|
|
|
sysvar::{stake_history::StakeHistory, Sysvar},
|
2020-10-26 09:14:57 -07:00
|
|
|
};
|
2021-11-04 13:47:32 -07:00
|
|
|
use std::{cell::RefCell, rc::Rc, str::FromStr, sync::Arc};
|
2020-01-22 09:11:56 -08:00
|
|
|
|
2021-11-04 13:47:32 -07:00
|
|
|
fn create_default_account() -> Rc<RefCell<AccountSharedData>> {
|
|
|
|
AccountSharedData::new_ref(0, 0, &Pubkey::new_unique())
|
2020-01-22 09:11:56 -08:00
|
|
|
}
|
2019-04-01 16:45:53 -07:00
|
|
|
|
2021-11-04 13:47:32 -07:00
|
|
|
fn create_default_stake_account() -> Rc<RefCell<AccountSharedData>> {
|
|
|
|
AccountSharedData::new_ref(0, 0, &id())
|
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()
|
|
|
|
}
|
|
|
|
|
2021-10-08 02:41:07 -07:00
|
|
|
fn process_instruction(
|
|
|
|
instruction_data: &[u8],
|
2021-11-04 13:47:32 -07:00
|
|
|
keyed_accounts: &[(bool, bool, Pubkey, Rc<RefCell<AccountSharedData>>)],
|
2021-10-08 02:41:07 -07:00
|
|
|
) -> Result<(), InstructionError> {
|
2021-11-04 13:47:32 -07:00
|
|
|
mock_process_instruction(
|
|
|
|
&id(),
|
|
|
|
Vec::new(),
|
2021-10-08 02:41:07 -07:00
|
|
|
instruction_data,
|
2021-11-04 13:47:32 -07:00
|
|
|
keyed_accounts,
|
|
|
|
super::process_instruction,
|
2021-10-08 02:41:07 -07:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn process_instruction_as_one_arg(instruction: &Instruction) -> Result<(), InstructionError> {
|
2021-11-04 13:47:32 -07:00
|
|
|
let accounts = instruction.accounts.iter().map(|meta| {
|
|
|
|
Rc::new(RefCell::new(if sysvar::clock::check_id(&meta.pubkey) {
|
|
|
|
account::create_account_shared_data_for_test(&sysvar::clock::Clock::default())
|
|
|
|
} else if sysvar::rewards::check_id(&meta.pubkey) {
|
|
|
|
account::create_account_shared_data_for_test(&sysvar::rewards::Rewards::new(0.0))
|
|
|
|
} else if sysvar::stake_history::check_id(&meta.pubkey) {
|
|
|
|
account::create_account_shared_data_for_test(&StakeHistory::default())
|
|
|
|
} else if stake_config::check_id(&meta.pubkey) {
|
|
|
|
config::create_account(0, &stake_config::Config::default())
|
|
|
|
} else if sysvar::rent::check_id(&meta.pubkey) {
|
|
|
|
account::create_account_shared_data_for_test(&Rent::default())
|
|
|
|
} else if meta.pubkey == invalid_stake_state_pubkey() {
|
|
|
|
AccountSharedData::new(0, 0, &id())
|
|
|
|
} else if meta.pubkey == invalid_vote_state_pubkey() {
|
|
|
|
AccountSharedData::new(0, 0, &solana_vote_program::id())
|
|
|
|
} else if meta.pubkey == spoofed_stake_state_pubkey() {
|
|
|
|
AccountSharedData::new(0, 0, &spoofed_stake_program_id())
|
|
|
|
} else {
|
|
|
|
AccountSharedData::new(0, 0, &id())
|
|
|
|
}))
|
|
|
|
});
|
|
|
|
let keyed_accounts: Vec<_> = instruction
|
2019-06-17 19:34:21 -07:00
|
|
|
.accounts
|
|
|
|
.iter()
|
2021-11-04 13:47:32 -07:00
|
|
|
.zip(accounts)
|
|
|
|
.map(|(meta, account)| (meta.is_signer, meta.is_writable, meta.pubkey, account))
|
2019-06-17 19:34:21 -07:00
|
|
|
.collect();
|
2021-11-04 13:47:32 -07:00
|
|
|
let mut preparation = prepare_mock_invoke_context(&[], &instruction.data, &keyed_accounts);
|
|
|
|
let processor_account = AccountSharedData::new_ref(0, 0, &solana_sdk::native_loader::id());
|
|
|
|
let program_indices = vec![preparation.accounts.len()];
|
|
|
|
preparation.accounts.push((id(), processor_account));
|
|
|
|
let mut data = Vec::with_capacity(sysvar::clock::Clock::size_of());
|
|
|
|
bincode::serialize_into(&mut data, &sysvar::clock::Clock::default()).unwrap();
|
|
|
|
let sysvars = [(sysvar::clock::id(), data)];
|
|
|
|
let mut invoke_context = ThisInvokeContext::new_mock_with_sysvars_and_features(
|
|
|
|
&preparation.accounts,
|
|
|
|
&[],
|
|
|
|
&sysvars,
|
|
|
|
Arc::new(FeatureSet::all_enabled()),
|
|
|
|
);
|
|
|
|
invoke_context.push(
|
|
|
|
&preparation.message,
|
|
|
|
&preparation.message.instructions[0],
|
|
|
|
&program_indices,
|
|
|
|
Some(&preparation.account_indices),
|
|
|
|
)?;
|
|
|
|
super::process_instruction(1, &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-10-08 02:41:07 -07:00
|
|
|
process_instruction_as_one_arg(&instruction::initialize(
|
2021-11-04 13:47:32 -07:00
|
|
|
&Pubkey::new_unique(),
|
2019-10-31 11:07:27 -07:00
|
|
|
&Authorized::default(),
|
|
|
|
&Lockup::default()
|
|
|
|
)),
|
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2021-10-08 02:41:07 -07:00
|
|
|
process_instruction_as_one_arg(&instruction::authorize(
|
2021-11-04 13:47:32 -07:00
|
|
|
&Pubkey::new_unique(),
|
|
|
|
&Pubkey::new_unique(),
|
|
|
|
&Pubkey::new_unique(),
|
2021-01-26 09:44:44 -08:00
|
|
|
StakeAuthorize::Staker,
|
|
|
|
None,
|
2019-10-31 11:07:27 -07:00
|
|
|
)),
|
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2021-10-08 02:41:07 -07:00
|
|
|
process_instruction_as_one_arg(
|
2021-06-15 09:04:00 -07:00
|
|
|
&instruction::split(
|
2021-11-04 13:47:32 -07:00
|
|
|
&Pubkey::new_unique(),
|
|
|
|
&Pubkey::new_unique(),
|
2019-10-31 11:07:27 -07:00
|
|
|
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!(
|
2021-10-08 02:41:07 -07:00
|
|
|
process_instruction_as_one_arg(
|
2021-06-15 09:04:00 -07:00
|
|
|
&instruction::merge(
|
2021-11-04 13:47:32 -07:00
|
|
|
&Pubkey::new_unique(),
|
2020-11-16 12:42:59 -08:00
|
|
|
&invalid_stake_state_pubkey(),
|
2021-11-04 13:47:32 -07:00
|
|
|
&Pubkey::new_unique(),
|
2020-11-16 12:42:59 -08:00
|
|
|
)[0]
|
2020-06-10 17:22:47 -07:00
|
|
|
),
|
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
2020-01-20 12:33:27 -08:00
|
|
|
assert_eq!(
|
2021-10-08 02:41:07 -07:00
|
|
|
process_instruction_as_one_arg(
|
2021-06-15 09:04:00 -07:00
|
|
|
&instruction::split_with_seed(
|
2021-11-04 13:47:32 -07:00
|
|
|
&Pubkey::new_unique(),
|
|
|
|
&Pubkey::new_unique(),
|
2020-01-20 12:33:27 -08:00
|
|
|
100,
|
2020-11-16 12:42:59 -08:00
|
|
|
&invalid_stake_state_pubkey(),
|
2021-11-04 13:47:32 -07:00
|
|
|
&Pubkey::new_unique(),
|
2020-01-20 12:33:27 -08:00
|
|
|
"seed"
|
2019-10-31 11:07:27 -07:00
|
|
|
)[1]
|
|
|
|
),
|
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
2019-05-07 17:08:49 -07:00
|
|
|
assert_eq!(
|
2021-10-08 02:41:07 -07:00
|
|
|
process_instruction_as_one_arg(&instruction::delegate_stake(
|
2021-11-04 13:47:32 -07:00
|
|
|
&Pubkey::new_unique(),
|
|
|
|
&Pubkey::new_unique(),
|
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-10-08 02:41:07 -07:00
|
|
|
process_instruction_as_one_arg(&instruction::withdraw(
|
2021-11-04 13:47:32 -07:00
|
|
|
&Pubkey::new_unique(),
|
|
|
|
&Pubkey::new_unique(),
|
|
|
|
&Pubkey::new_unique(),
|
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-10-08 02:41:07 -07:00
|
|
|
process_instruction_as_one_arg(&instruction::deactivate_stake(
|
2021-11-04 13:47:32 -07:00
|
|
|
&Pubkey::new_unique(),
|
|
|
|
&Pubkey::new_unique()
|
2021-06-15 09:04:00 -07:00
|
|
|
)),
|
2019-06-21 23:45:03 -07:00
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
2020-01-28 20:59:53 -08:00
|
|
|
assert_eq!(
|
2021-10-08 02:41:07 -07:00
|
|
|
process_instruction_as_one_arg(&instruction::set_lockup(
|
2021-11-04 13:47:32 -07:00
|
|
|
&Pubkey::new_unique(),
|
2020-03-02 12:28:43 -08:00
|
|
|
&LockupArgs::default(),
|
2021-11-04 13:47:32 -07:00
|
|
|
&Pubkey::new_unique()
|
2020-01-28 20:59:53 -08:00
|
|
|
)),
|
|
|
|
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-10-08 02:41:07 -07:00
|
|
|
process_instruction_as_one_arg(&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-10-08 02:41:07 -07:00
|
|
|
process_instruction_as_one_arg(&instruction::authorize(
|
2020-12-22 10:20:38 -08:00
|
|
|
&spoofed_stake_state_pubkey(),
|
2021-11-04 13:47:32 -07:00
|
|
|
&Pubkey::new_unique(),
|
|
|
|
&Pubkey::new_unique(),
|
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!(
|
2021-10-08 02:41:07 -07:00
|
|
|
process_instruction_as_one_arg(
|
2021-06-15 09:04:00 -07:00
|
|
|
&instruction::split(
|
2020-12-22 10:20:38 -08:00
|
|
|
&spoofed_stake_state_pubkey(),
|
2021-11-04 13:47:32 -07:00
|
|
|
&Pubkey::new_unique(),
|
2020-12-22 10:20:38 -08:00
|
|
|
100,
|
2021-11-04 13:47:32 -07:00
|
|
|
&Pubkey::new_unique(),
|
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!(
|
2021-10-08 02:41:07 -07:00
|
|
|
process_instruction_as_one_arg(
|
2021-06-15 09:04:00 -07:00
|
|
|
&instruction::split(
|
2021-11-04 13:47:32 -07:00
|
|
|
&Pubkey::new_unique(),
|
|
|
|
&Pubkey::new_unique(),
|
2020-12-22 10:20:38 -08:00
|
|
|
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!(
|
2021-10-08 02:41:07 -07:00
|
|
|
process_instruction_as_one_arg(
|
2021-06-15 09:04:00 -07:00
|
|
|
&instruction::merge(
|
2020-12-22 10:20:38 -08:00
|
|
|
&spoofed_stake_state_pubkey(),
|
2021-11-04 13:47:32 -07:00
|
|
|
&Pubkey::new_unique(),
|
|
|
|
&Pubkey::new_unique(),
|
2020-12-22 10:20:38 -08:00
|
|
|
)[0]
|
|
|
|
),
|
2021-02-26 14:21:34 -08:00
|
|
|
Err(InstructionError::InvalidAccountOwner),
|
2020-12-22 10:20:38 -08:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2021-10-08 02:41:07 -07:00
|
|
|
process_instruction_as_one_arg(
|
2021-06-15 09:04:00 -07:00
|
|
|
&instruction::merge(
|
2021-11-04 13:47:32 -07:00
|
|
|
&Pubkey::new_unique(),
|
2020-12-22 10:20:38 -08:00
|
|
|
&spoofed_stake_state_pubkey(),
|
2021-11-04 13:47:32 -07:00
|
|
|
&Pubkey::new_unique(),
|
2020-12-22 10:20:38 -08:00
|
|
|
)[0]
|
|
|
|
),
|
|
|
|
Err(InstructionError::IncorrectProgramId),
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2021-10-08 02:41:07 -07:00
|
|
|
process_instruction_as_one_arg(
|
2021-06-15 09:04:00 -07:00
|
|
|
&instruction::split_with_seed(
|
2020-12-22 10:20:38 -08:00
|
|
|
&spoofed_stake_state_pubkey(),
|
2021-11-04 13:47:32 -07:00
|
|
|
&Pubkey::new_unique(),
|
2020-12-22 10:20:38 -08:00
|
|
|
100,
|
2021-11-04 13:47:32 -07:00
|
|
|
&Pubkey::new_unique(),
|
|
|
|
&Pubkey::new_unique(),
|
2020-12-22 10:20:38 -08:00
|
|
|
"seed"
|
|
|
|
)[1]
|
|
|
|
),
|
2021-02-26 14:21:34 -08:00
|
|
|
Err(InstructionError::InvalidAccountOwner),
|
2020-12-22 10:20:38 -08:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2021-10-08 02:41:07 -07:00
|
|
|
process_instruction_as_one_arg(&instruction::delegate_stake(
|
2020-12-22 10:20:38 -08:00
|
|
|
&spoofed_stake_state_pubkey(),
|
2021-11-04 13:47:32 -07:00
|
|
|
&Pubkey::new_unique(),
|
|
|
|
&Pubkey::new_unique(),
|
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!(
|
2021-10-08 02:41:07 -07:00
|
|
|
process_instruction_as_one_arg(&instruction::withdraw(
|
2020-12-22 10:20:38 -08:00
|
|
|
&spoofed_stake_state_pubkey(),
|
2021-11-04 13:47:32 -07:00
|
|
|
&Pubkey::new_unique(),
|
|
|
|
&Pubkey::new_unique(),
|
2020-12-22 10:20:38 -08:00
|
|
|
100,
|
|
|
|
None,
|
|
|
|
)),
|
2021-02-26 14:21:34 -08:00
|
|
|
Err(InstructionError::InvalidAccountOwner),
|
2020-12-22 10:20:38 -08:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2021-10-08 02:41:07 -07:00
|
|
|
process_instruction_as_one_arg(&instruction::deactivate_stake(
|
2020-12-22 10:20:38 -08:00
|
|
|
&spoofed_stake_state_pubkey(),
|
2021-11-04 13:47:32 -07:00
|
|
|
&Pubkey::new_unique()
|
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!(
|
2021-10-08 02:41:07 -07:00
|
|
|
process_instruction_as_one_arg(&instruction::set_lockup(
|
2020-12-22 10:20:38 -08:00
|
|
|
&spoofed_stake_state_pubkey(),
|
|
|
|
&LockupArgs::default(),
|
2021-11-04 13:47:32 -07:00
|
|
|
&Pubkey::new_unique()
|
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
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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!(
|
2021-10-08 02:41:07 -07:00
|
|
|
process_instruction(
|
2019-09-26 13:29:29 -07:00
|
|
|
&serialize(&StakeInstruction::Initialize(
|
|
|
|
Authorized::default(),
|
|
|
|
Lockup::default()
|
|
|
|
))
|
|
|
|
.unwrap(),
|
2021-10-08 02:41:07 -07:00
|
|
|
&[],
|
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-11-04 13:47:32 -07:00
|
|
|
let stake_address = Pubkey::new_unique();
|
2021-04-19 09:48:48 -07:00
|
|
|
let stake_account = create_default_stake_account();
|
2021-11-04 13:47:32 -07:00
|
|
|
let keyed_accounts = [(false, false, stake_address, stake_account)];
|
2019-10-31 11:07:27 -07:00
|
|
|
assert_eq!(
|
2021-10-08 02:41:07 -07:00
|
|
|
process_instruction(
|
2019-10-31 11:07:27 -07:00
|
|
|
&serialize(&StakeInstruction::Initialize(
|
|
|
|
Authorized::default(),
|
|
|
|
Lockup::default()
|
|
|
|
))
|
|
|
|
.unwrap(),
|
2021-10-08 02:41:07 -07:00
|
|
|
&keyed_accounts,
|
2019-10-31 11:07:27 -07:00
|
|
|
),
|
|
|
|
Err(InstructionError::NotEnoughAccountKeys),
|
|
|
|
);
|
|
|
|
|
|
|
|
// rent fails to deserialize
|
2021-11-04 13:47:32 -07:00
|
|
|
let stake_address = Pubkey::new_unique();
|
2021-04-19 09:48:48 -07:00
|
|
|
let stake_account = create_default_stake_account();
|
|
|
|
let rent_address = sysvar::rent::id();
|
|
|
|
let rent_account = create_default_account();
|
2021-10-08 02:41:07 -07:00
|
|
|
let keyed_accounts = [
|
2021-11-04 13:47:32 -07:00
|
|
|
(false, false, stake_address, stake_account),
|
|
|
|
(false, false, rent_address, rent_account),
|
2021-04-19 09:48:48 -07:00
|
|
|
];
|
2019-10-31 11:07:27 -07:00
|
|
|
assert_eq!(
|
2021-10-08 02:41:07 -07:00
|
|
|
process_instruction(
|
2019-10-31 11:07:27 -07:00
|
|
|
&serialize(&StakeInstruction::Initialize(
|
|
|
|
Authorized::default(),
|
|
|
|
Lockup::default()
|
|
|
|
))
|
|
|
|
.unwrap(),
|
2021-10-08 02:41:07 -07:00
|
|
|
&keyed_accounts,
|
2019-10-31 11:07:27 -07:00
|
|
|
),
|
|
|
|
Err(InstructionError::InvalidArgument),
|
|
|
|
);
|
|
|
|
|
|
|
|
// fails to deserialize stake state
|
2021-11-04 13:47:32 -07:00
|
|
|
let stake_address = Pubkey::new_unique();
|
2021-04-19 09:48:48 -07:00
|
|
|
let stake_account = create_default_stake_account();
|
|
|
|
let rent_address = sysvar::rent::id();
|
2021-11-04 13:47:32 -07:00
|
|
|
let rent_account = Rc::new(RefCell::new(account::create_account_shared_data_for_test(
|
2021-04-19 09:48:48 -07:00
|
|
|
&Rent::default(),
|
2021-11-04 13:47:32 -07:00
|
|
|
)));
|
2021-10-08 02:41:07 -07:00
|
|
|
let keyed_accounts = [
|
2021-11-04 13:47:32 -07:00
|
|
|
(false, false, stake_address, stake_account),
|
|
|
|
(false, false, rent_address, rent_account),
|
2021-04-19 09:48:48 -07:00
|
|
|
];
|
2019-10-31 11:07:27 -07:00
|
|
|
assert_eq!(
|
2021-10-08 02:41:07 -07:00
|
|
|
process_instruction(
|
2019-10-31 11:07:27 -07:00
|
|
|
&serialize(&StakeInstruction::Initialize(
|
|
|
|
Authorized::default(),
|
|
|
|
Lockup::default()
|
|
|
|
))
|
|
|
|
.unwrap(),
|
2021-10-08 02:41:07 -07:00
|
|
|
&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-11-04 13:47:32 -07:00
|
|
|
let stake_address = Pubkey::new_unique();
|
2021-04-19 09:48:48 -07:00
|
|
|
let stake_account = create_default_stake_account();
|
2021-11-04 13:47:32 -07:00
|
|
|
let keyed_accounts = [(false, false, stake_address, stake_account)];
|
2019-04-01 16:45:53 -07:00
|
|
|
assert_eq!(
|
2021-10-08 02:41:07 -07:00
|
|
|
process_instruction(
|
2019-09-11 09:48:29 -07:00
|
|
|
&serialize(&StakeInstruction::DelegateStake).unwrap(),
|
2021-10-08 02:41:07 -07:00
|
|
|
&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-11-04 13:47:32 -07:00
|
|
|
let stake_address = Pubkey::new_unique();
|
2021-04-19 09:48:48 -07:00
|
|
|
let stake_account = create_default_stake_account();
|
2021-11-04 13:47:32 -07:00
|
|
|
let keyed_accounts = [(false, false, stake_address, stake_account)];
|
2019-04-01 16:45:53 -07:00
|
|
|
assert_eq!(
|
2021-10-08 02:41:07 -07:00
|
|
|
process_instruction(
|
2019-09-11 09:48:29 -07:00
|
|
|
&serialize(&StakeInstruction::DelegateStake).unwrap(),
|
2021-10-08 02:41:07 -07:00
|
|
|
&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-11-04 13:47:32 -07:00
|
|
|
let stake_address = Pubkey::new_unique();
|
2021-04-19 09:48:48 -07:00
|
|
|
let stake_account = create_default_stake_account();
|
2021-11-04 13:47:32 -07:00
|
|
|
let vote_address = Pubkey::new_unique();
|
|
|
|
let vote_account = AccountSharedData::new_ref(0, 0, &solana_vote_program::id());
|
2021-04-19 09:48:48 -07:00
|
|
|
let clock_address = sysvar::clock::id();
|
2021-11-04 13:47:32 -07:00
|
|
|
let clock_account = Rc::new(RefCell::new(account::create_account_shared_data_for_test(
|
2021-04-19 09:48:48 -07:00
|
|
|
&sysvar::clock::Clock::default(),
|
2021-11-04 13:47:32 -07:00
|
|
|
)));
|
2021-04-19 09:48:48 -07:00
|
|
|
let stake_history_address = sysvar::stake_history::id();
|
2021-11-04 13:47:32 -07:00
|
|
|
let stake_history_account =
|
|
|
|
Rc::new(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();
|
2021-11-04 13:47:32 -07:00
|
|
|
let config_account = Rc::new(RefCell::new(config::create_account(
|
|
|
|
0,
|
|
|
|
&stake_config::Config::default(),
|
|
|
|
)));
|
2021-10-08 02:41:07 -07:00
|
|
|
let keyed_accounts = [
|
2021-11-04 13:47:32 -07:00
|
|
|
(true, false, stake_address, stake_account),
|
|
|
|
(false, false, vote_address, vote_account),
|
|
|
|
(false, false, clock_address, clock_account),
|
|
|
|
(false, false, stake_history_address, stake_history_account),
|
|
|
|
(false, false, config_address, config_account),
|
2021-04-19 09:48:48 -07:00
|
|
|
];
|
2019-05-07 17:08:49 -07:00
|
|
|
assert_eq!(
|
2021-10-08 02:41:07 -07:00
|
|
|
process_instruction(
|
2019-09-11 09:48:29 -07:00
|
|
|
&serialize(&StakeInstruction::DelegateStake).unwrap(),
|
2021-10-08 02:41:07 -07:00
|
|
|
&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-11-04 13:47:32 -07:00
|
|
|
let stake_address = Pubkey::new_unique();
|
2021-04-19 09:48:48 -07:00
|
|
|
let stake_account = create_default_stake_account();
|
2021-11-04 13:47:32 -07:00
|
|
|
let vote_address = Pubkey::new_unique();
|
2021-04-19 09:48:48 -07:00
|
|
|
let vote_account = create_default_account();
|
|
|
|
let rewards_address = sysvar::rewards::id();
|
2021-11-04 13:47:32 -07:00
|
|
|
let rewards_account = Rc::new(RefCell::new(account::create_account_shared_data_for_test(
|
2021-04-19 09:48:48 -07:00
|
|
|
&sysvar::rewards::Rewards::new(0.0),
|
2021-11-04 13:47:32 -07:00
|
|
|
)));
|
2021-04-19 09:48:48 -07:00
|
|
|
let stake_history_address = sysvar::stake_history::id();
|
2021-11-04 13:47:32 -07:00
|
|
|
let stake_history_account = Rc::new(RefCell::new(
|
|
|
|
account::create_account_shared_data_for_test(&StakeHistory::default()),
|
2021-04-19 09:48:48 -07:00
|
|
|
));
|
2021-10-08 02:41:07 -07:00
|
|
|
let keyed_accounts = [
|
2021-11-04 13:47:32 -07:00
|
|
|
(false, false, stake_address, stake_account),
|
|
|
|
(false, false, vote_address, vote_account),
|
|
|
|
(false, false, rewards_address, rewards_account),
|
|
|
|
(false, false, stake_history_address, stake_history_account),
|
2021-04-19 09:48:48 -07:00
|
|
|
];
|
2019-06-21 22:28:34 -07:00
|
|
|
assert_eq!(
|
2021-10-08 02:41:07 -07:00
|
|
|
process_instruction(
|
2019-06-21 22:28:34 -07:00
|
|
|
&serialize(&StakeInstruction::Withdraw(42)).unwrap(),
|
2021-10-08 02:41:07 -07:00
|
|
|
&keyed_accounts,
|
2019-06-21 22:28:34 -07:00
|
|
|
),
|
|
|
|
Err(InstructionError::InvalidArgument),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Tests correct number of accounts are provided in withdraw
|
2021-11-04 13:47:32 -07:00
|
|
|
let stake_address = Pubkey::new_unique();
|
2021-04-19 09:48:48 -07:00
|
|
|
let stake_account = create_default_stake_account();
|
2021-11-04 13:47:32 -07:00
|
|
|
let keyed_accounts = [(false, false, stake_address, stake_account)];
|
2019-06-21 22:28:34 -07:00
|
|
|
assert_eq!(
|
2021-10-08 02:41:07 -07:00
|
|
|
process_instruction(
|
2019-06-21 22:28:34 -07:00
|
|
|
&serialize(&StakeInstruction::Withdraw(42)).unwrap(),
|
2021-10-08 02:41:07 -07:00
|
|
|
&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-11-04 13:47:32 -07:00
|
|
|
let stake_address = Pubkey::new_unique();
|
2021-04-19 09:48:48 -07:00
|
|
|
let stake_account = create_default_stake_account();
|
|
|
|
let rewards_address = sysvar::rewards::id();
|
2021-11-04 13:47:32 -07:00
|
|
|
let rewards_account = Rc::new(RefCell::new(account::create_account_shared_data_for_test(
|
2021-04-19 09:48:48 -07:00
|
|
|
&sysvar::rewards::Rewards::new(0.0),
|
2021-11-04 13:47:32 -07:00
|
|
|
)));
|
2021-10-08 02:41:07 -07:00
|
|
|
let keyed_accounts = [
|
2021-11-04 13:47:32 -07:00
|
|
|
(false, false, stake_address, stake_account),
|
|
|
|
(false, false, rewards_address, rewards_account),
|
2021-04-19 09:48:48 -07:00
|
|
|
];
|
2019-06-21 23:45:03 -07:00
|
|
|
assert_eq!(
|
2021-10-08 02:41:07 -07:00
|
|
|
process_instruction(
|
2019-06-21 23:45:03 -07:00
|
|
|
&serialize(&StakeInstruction::Deactivate).unwrap(),
|
2021-10-08 02:41:07 -07:00
|
|
|
&keyed_accounts,
|
2019-06-21 23:45:03 -07:00
|
|
|
),
|
|
|
|
Err(InstructionError::InvalidArgument),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Tests correct number of accounts are provided in deactivate
|
|
|
|
assert_eq!(
|
2021-11-04 13:47:32 -07:00
|
|
|
process_instruction(&serialize(&StakeInstruction::Deactivate).unwrap(), &[]),
|
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
|
|
|
}
|
2021-07-06 12:24:10 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_stake_checked_instructions() {
|
|
|
|
let stake_address = Pubkey::new_unique();
|
|
|
|
let staker = Pubkey::new_unique();
|
|
|
|
let withdrawer = Pubkey::new_unique();
|
|
|
|
|
|
|
|
// Test InitializeChecked with non-signing withdrawer
|
|
|
|
let mut instruction =
|
|
|
|
initialize_checked(&stake_address, &Authorized { staker, withdrawer });
|
|
|
|
instruction.accounts[3] = AccountMeta::new_readonly(withdrawer, false);
|
|
|
|
assert_eq!(
|
2021-10-08 02:41:07 -07:00
|
|
|
process_instruction_as_one_arg(&instruction),
|
2021-07-06 12:24:10 -07:00
|
|
|
Err(InstructionError::MissingRequiredSignature),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test InitializeChecked with withdrawer signer
|
|
|
|
let stake_account = AccountSharedData::new_ref(
|
|
|
|
1_000_000_000,
|
|
|
|
std::mem::size_of::<crate::stake_state::StakeState>(),
|
|
|
|
&id(),
|
|
|
|
);
|
|
|
|
let rent_address = sysvar::rent::id();
|
2021-11-04 13:47:32 -07:00
|
|
|
let rent_account = Rc::new(RefCell::new(account::create_account_shared_data_for_test(
|
2021-07-06 12:24:10 -07:00
|
|
|
&Rent::default(),
|
2021-11-04 13:47:32 -07:00
|
|
|
)));
|
2021-07-06 12:24:10 -07:00
|
|
|
let staker_account = create_default_account();
|
|
|
|
let withdrawer_account = create_default_account();
|
|
|
|
|
2021-11-04 13:47:32 -07:00
|
|
|
let keyed_accounts = [
|
|
|
|
(false, false, stake_address, stake_account),
|
|
|
|
(false, false, rent_address, rent_account),
|
|
|
|
(false, false, staker, staker_account),
|
|
|
|
(true, false, withdrawer, withdrawer_account.clone()),
|
2021-07-06 12:24:10 -07:00
|
|
|
];
|
|
|
|
assert_eq!(
|
2021-10-08 02:41:07 -07:00
|
|
|
process_instruction(
|
2021-07-06 12:24:10 -07:00
|
|
|
&serialize(&StakeInstruction::InitializeChecked).unwrap(),
|
2021-10-08 02:41:07 -07:00
|
|
|
&keyed_accounts,
|
2021-07-06 12:24:10 -07:00
|
|
|
),
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test AuthorizeChecked with non-signing authority
|
|
|
|
let authorized_address = Pubkey::new_unique();
|
|
|
|
let mut instruction = authorize_checked(
|
|
|
|
&stake_address,
|
|
|
|
&authorized_address,
|
|
|
|
&staker,
|
|
|
|
StakeAuthorize::Staker,
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
instruction.accounts[3] = AccountMeta::new_readonly(staker, false);
|
|
|
|
assert_eq!(
|
2021-10-08 02:41:07 -07:00
|
|
|
process_instruction_as_one_arg(&instruction),
|
2021-07-06 12:24:10 -07:00
|
|
|
Err(InstructionError::MissingRequiredSignature),
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut instruction = authorize_checked(
|
|
|
|
&stake_address,
|
|
|
|
&authorized_address,
|
|
|
|
&withdrawer,
|
|
|
|
StakeAuthorize::Withdrawer,
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
instruction.accounts[3] = AccountMeta::new_readonly(withdrawer, false);
|
|
|
|
assert_eq!(
|
2021-10-08 02:41:07 -07:00
|
|
|
process_instruction_as_one_arg(&instruction),
|
2021-07-06 12:24:10 -07:00
|
|
|
Err(InstructionError::MissingRequiredSignature),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test AuthorizeChecked with authority signer
|
2021-11-04 13:47:32 -07:00
|
|
|
let stake_account = Rc::new(
|
|
|
|
AccountSharedData::new_ref_data_with_space(
|
|
|
|
42,
|
|
|
|
&StakeState::Initialized(Meta::auto(&authorized_address)),
|
|
|
|
std::mem::size_of::<StakeState>(),
|
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
);
|
2021-07-06 12:24:10 -07:00
|
|
|
let clock_address = sysvar::clock::id();
|
2021-11-04 13:47:32 -07:00
|
|
|
let clock_account = Rc::new(RefCell::new(account::create_account_shared_data_for_test(
|
2021-07-06 12:24:10 -07:00
|
|
|
&Clock::default(),
|
2021-11-04 13:47:32 -07:00
|
|
|
)));
|
2021-07-06 12:24:10 -07:00
|
|
|
let authorized_account = create_default_account();
|
|
|
|
let new_authorized_account = create_default_account();
|
|
|
|
|
2021-10-08 02:41:07 -07:00
|
|
|
let mut keyed_accounts = [
|
2021-11-04 13:47:32 -07:00
|
|
|
(false, false, stake_address, stake_account),
|
|
|
|
(false, false, clock_address, clock_account.clone()),
|
|
|
|
(true, false, authorized_address, authorized_account.clone()),
|
|
|
|
(true, false, staker, new_authorized_account.clone()),
|
2021-07-06 12:24:10 -07:00
|
|
|
];
|
|
|
|
assert_eq!(
|
2021-10-08 02:41:07 -07:00
|
|
|
process_instruction(
|
2021-07-06 12:24:10 -07:00
|
|
|
&serialize(&StakeInstruction::AuthorizeChecked(StakeAuthorize::Staker)).unwrap(),
|
2021-10-08 02:41:07 -07:00
|
|
|
&keyed_accounts,
|
2021-07-06 12:24:10 -07:00
|
|
|
),
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
|
2021-11-04 13:47:32 -07:00
|
|
|
keyed_accounts[3] = (true, false, withdrawer, new_authorized_account.clone());
|
2021-07-06 12:24:10 -07:00
|
|
|
assert_eq!(
|
2021-10-08 02:41:07 -07:00
|
|
|
process_instruction(
|
2021-07-06 12:24:10 -07:00
|
|
|
&serialize(&StakeInstruction::AuthorizeChecked(
|
|
|
|
StakeAuthorize::Withdrawer
|
|
|
|
))
|
|
|
|
.unwrap(),
|
2021-10-08 02:41:07 -07:00
|
|
|
&keyed_accounts,
|
2021-07-06 12:24:10 -07:00
|
|
|
),
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test AuthorizeCheckedWithSeed with non-signing authority
|
|
|
|
let authorized_owner = Pubkey::new_unique();
|
|
|
|
let seed = "test seed";
|
|
|
|
let address_with_seed =
|
|
|
|
Pubkey::create_with_seed(&authorized_owner, seed, &authorized_owner).unwrap();
|
|
|
|
let mut instruction = authorize_checked_with_seed(
|
|
|
|
&stake_address,
|
|
|
|
&authorized_owner,
|
|
|
|
seed.to_string(),
|
|
|
|
&authorized_owner,
|
|
|
|
&staker,
|
|
|
|
StakeAuthorize::Staker,
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
instruction.accounts[3] = AccountMeta::new_readonly(staker, false);
|
|
|
|
assert_eq!(
|
2021-10-08 02:41:07 -07:00
|
|
|
process_instruction_as_one_arg(&instruction),
|
2021-07-06 12:24:10 -07:00
|
|
|
Err(InstructionError::MissingRequiredSignature),
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut instruction = authorize_checked_with_seed(
|
|
|
|
&stake_address,
|
|
|
|
&authorized_owner,
|
|
|
|
seed.to_string(),
|
|
|
|
&authorized_owner,
|
|
|
|
&staker,
|
|
|
|
StakeAuthorize::Withdrawer,
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
instruction.accounts[3] = AccountMeta::new_readonly(staker, false);
|
|
|
|
assert_eq!(
|
2021-10-08 02:41:07 -07:00
|
|
|
process_instruction_as_one_arg(&instruction),
|
2021-07-06 12:24:10 -07:00
|
|
|
Err(InstructionError::MissingRequiredSignature),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test AuthorizeCheckedWithSeed with authority signer
|
2021-11-04 13:47:32 -07:00
|
|
|
let stake_account = Rc::new(
|
|
|
|
AccountSharedData::new_ref_data_with_space(
|
|
|
|
42,
|
|
|
|
&StakeState::Initialized(Meta::auto(&address_with_seed)),
|
|
|
|
std::mem::size_of::<StakeState>(),
|
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
);
|
2021-10-08 02:41:07 -07:00
|
|
|
let mut keyed_accounts = [
|
2021-11-04 13:47:32 -07:00
|
|
|
(false, false, address_with_seed, stake_account),
|
|
|
|
(true, false, authorized_owner, authorized_account),
|
|
|
|
(false, false, clock_address, clock_account),
|
|
|
|
(true, false, staker, new_authorized_account.clone()),
|
2021-07-06 12:24:10 -07:00
|
|
|
];
|
|
|
|
assert_eq!(
|
2021-10-08 02:41:07 -07:00
|
|
|
process_instruction(
|
2021-07-06 12:24:10 -07:00
|
|
|
&serialize(&StakeInstruction::AuthorizeCheckedWithSeed(
|
|
|
|
AuthorizeCheckedWithSeedArgs {
|
|
|
|
stake_authorize: StakeAuthorize::Staker,
|
|
|
|
authority_seed: seed.to_string(),
|
|
|
|
authority_owner: authorized_owner,
|
|
|
|
}
|
|
|
|
))
|
|
|
|
.unwrap(),
|
2021-10-08 02:41:07 -07:00
|
|
|
&keyed_accounts,
|
2021-07-06 12:24:10 -07:00
|
|
|
),
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
|
2021-11-04 13:47:32 -07:00
|
|
|
keyed_accounts[3] = (true, false, withdrawer, new_authorized_account);
|
2021-07-06 12:24:10 -07:00
|
|
|
assert_eq!(
|
2021-10-08 02:41:07 -07:00
|
|
|
process_instruction(
|
2021-07-06 12:24:10 -07:00
|
|
|
&serialize(&StakeInstruction::AuthorizeCheckedWithSeed(
|
|
|
|
AuthorizeCheckedWithSeedArgs {
|
|
|
|
stake_authorize: StakeAuthorize::Withdrawer,
|
|
|
|
authority_seed: seed.to_string(),
|
|
|
|
authority_owner: authorized_owner,
|
|
|
|
}
|
|
|
|
))
|
|
|
|
.unwrap(),
|
2021-10-08 02:41:07 -07:00
|
|
|
&keyed_accounts,
|
2021-07-06 12:24:10 -07:00
|
|
|
),
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test SetLockupChecked with non-signing lockup custodian
|
|
|
|
let custodian = Pubkey::new_unique();
|
|
|
|
let mut instruction = set_lockup_checked(
|
|
|
|
&stake_address,
|
|
|
|
&LockupArgs {
|
|
|
|
unix_timestamp: None,
|
|
|
|
epoch: Some(1),
|
|
|
|
custodian: Some(custodian),
|
|
|
|
},
|
|
|
|
&withdrawer,
|
|
|
|
);
|
|
|
|
instruction.accounts[2] = AccountMeta::new_readonly(custodian, false);
|
|
|
|
assert_eq!(
|
2021-10-08 02:41:07 -07:00
|
|
|
process_instruction_as_one_arg(&instruction),
|
2021-07-06 12:24:10 -07:00
|
|
|
Err(InstructionError::MissingRequiredSignature),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test SetLockupChecked with lockup custodian signer
|
2021-11-04 13:47:32 -07:00
|
|
|
let stake_account = Rc::new(
|
|
|
|
AccountSharedData::new_ref_data_with_space(
|
|
|
|
42,
|
|
|
|
&StakeState::Initialized(Meta::auto(&withdrawer)),
|
|
|
|
std::mem::size_of::<StakeState>(),
|
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
);
|
2021-07-06 12:24:10 -07:00
|
|
|
let custodian_account = create_default_account();
|
|
|
|
|
2021-10-08 02:41:07 -07:00
|
|
|
let keyed_accounts = [
|
2021-11-04 13:47:32 -07:00
|
|
|
(false, false, stake_address, stake_account),
|
|
|
|
(true, false, withdrawer, withdrawer_account),
|
|
|
|
(true, false, custodian, custodian_account),
|
2021-07-06 12:24:10 -07:00
|
|
|
];
|
2021-11-04 13:47:32 -07:00
|
|
|
let mut preparation = prepare_mock_invoke_context(&[], &instruction.data, &keyed_accounts);
|
|
|
|
let processor_account = AccountSharedData::new_ref(0, 0, &solana_sdk::native_loader::id());
|
|
|
|
let program_indices = vec![preparation.accounts.len()];
|
|
|
|
preparation.accounts.push((id(), processor_account));
|
2021-10-21 11:57:42 -07:00
|
|
|
let mut data = Vec::with_capacity(sysvar::clock::Clock::size_of());
|
|
|
|
bincode::serialize_into(&mut data, &sysvar::clock::Clock::default()).unwrap();
|
2021-11-04 13:47:32 -07:00
|
|
|
let sysvars = [(sysvar::clock::id(), data)];
|
|
|
|
let mut invoke_context = ThisInvokeContext::new_mock_with_sysvars_and_features(
|
|
|
|
&preparation.accounts,
|
|
|
|
&[],
|
|
|
|
&sysvars,
|
|
|
|
Arc::new(FeatureSet::all_enabled()),
|
|
|
|
);
|
|
|
|
invoke_context
|
|
|
|
.push(
|
|
|
|
&preparation.message,
|
|
|
|
&preparation.message.instructions[0],
|
|
|
|
&program_indices,
|
|
|
|
Some(&preparation.account_indices),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2021-07-06 12:24:10 -07:00
|
|
|
assert_eq!(
|
2021-11-04 13:47:32 -07:00
|
|
|
super::process_instruction(1, &instruction.data, &mut invoke_context),
|
2021-07-06 12:24:10 -07:00
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
}
|
2019-04-01 16:45:53 -07:00
|
|
|
}
|