2021-12-03 09:00:31 -08: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::*;
|
2021-06-15 09:04:00 -07:00
|
|
|
use {
|
|
|
|
crate::{config, stake_state::StakeAccount},
|
|
|
|
log::*,
|
2021-12-03 03:15:22 -08:00
|
|
|
solana_program_runtime::invoke_context::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},
|
|
|
|
},
|
2022-01-12 21:36:21 -08:00
|
|
|
sysvar::{clock::Clock, rent::Rent, stake_history::StakeHistory},
|
2021-06-15 09:04:00 -07:00
|
|
|
},
|
2019-08-15 14:35:48 -07:00
|
|
|
};
|
2020-08-21 11:28:01 -07: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-12-02 09:47:16 -08:00
|
|
|
invoke_context: &mut 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-12-03 03:15:22 -08:00
|
|
|
let require_custodian_for_locked_stake_authorize = invoke_context
|
|
|
|
.feature_set
|
|
|
|
.is_active(&feature_set::require_custodian_for_locked_stake_authorize::id());
|
2021-02-05 22:40:07 -08:00
|
|
|
|
|
|
|
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-12-03 03:15:22 -08:00
|
|
|
let require_custodian_for_locked_stake_authorize = invoke_context
|
|
|
|
.feature_set
|
|
|
|
.is_active(&feature_set::require_custodian_for_locked_stake_authorize::id());
|
2021-02-05 22:40:07 -08:00
|
|
|
|
|
|
|
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-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,
|
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)?;
|
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,
|
|
|
|
)
|
|
|
|
}
|
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(),
|
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) => {
|
2022-01-21 17:27:53 -08:00
|
|
|
let clock = invoke_context.get_sysvar_cache().get_clock()?;
|
|
|
|
me.set_lockup(&lockup, &signers, &clock)
|
2021-05-20 14:04:07 -07:00
|
|
|
}
|
2021-06-30 17:34:50 -07:00
|
|
|
StakeInstruction::InitializeChecked => {
|
2021-12-03 03:15:22 -08:00
|
|
|
if invoke_context
|
|
|
|
.feature_set
|
|
|
|
.is_active(&feature_set::vote_stake_checked_instructions::id())
|
2021-06-30 17:34:50 -07:00
|
|
|
{
|
|
|
|
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) => {
|
2021-12-03 03:15:22 -08:00
|
|
|
if invoke_context
|
|
|
|
.feature_set
|
|
|
|
.is_active(&feature_set::vote_stake_checked_instructions::id())
|
2021-06-30 17:34:50 -07:00
|
|
|
{
|
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) => {
|
2021-12-03 03:15:22 -08:00
|
|
|
if invoke_context
|
|
|
|
.feature_set
|
|
|
|
.is_active(&feature_set::vote_stake_checked_instructions::id())
|
2021-06-30 17:34:50 -07:00
|
|
|
{
|
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) => {
|
2021-12-03 03:15:22 -08:00
|
|
|
if invoke_context
|
|
|
|
.feature_set
|
|
|
|
.is_active(&feature_set::vote_stake_checked_instructions::id())
|
2021-06-30 17:34:50 -07:00
|
|
|
{
|
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,
|
|
|
|
};
|
2022-01-21 17:27:53 -08:00
|
|
|
let clock = invoke_context.get_sysvar_cache().get_clock()?;
|
|
|
|
me.set_lockup(&lockup, &signers, &clock)
|
2021-06-30 17:34:50 -07:00
|
|
|
} else {
|
|
|
|
Err(InstructionError::InvalidInstructionData)
|
|
|
|
}
|
|
|
|
}
|
2019-04-01 16:45:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2021-12-03 09:00:31 -08:00
|
|
|
use {
|
|
|
|
super::*,
|
|
|
|
crate::stake_state::{Meta, StakeState},
|
|
|
|
bincode::serialize,
|
2022-01-11 19:19:11 -08:00
|
|
|
solana_program_runtime::{
|
|
|
|
invoke_context::{mock_process_instruction, mock_process_instruction_with_sysvars},
|
|
|
|
sysvar_cache::SysvarCache,
|
2021-12-03 09:00:31 -08:00
|
|
|
},
|
|
|
|
solana_sdk::{
|
|
|
|
account::{self, AccountSharedData},
|
|
|
|
instruction::{AccountMeta, Instruction},
|
|
|
|
pubkey::Pubkey,
|
|
|
|
rent::Rent,
|
|
|
|
stake::{
|
|
|
|
config as stake_config,
|
|
|
|
instruction::{self, LockupArgs},
|
|
|
|
state::{Authorized, Lockup, StakeAuthorize},
|
|
|
|
},
|
2022-01-12 21:36:21 -08:00
|
|
|
sysvar::{self, stake_history::StakeHistory},
|
2021-06-15 09:04:00 -07:00
|
|
|
},
|
2021-12-17 05:01:12 -08:00
|
|
|
std::str::FromStr,
|
2020-10-26 09:14:57 -07:00
|
|
|
};
|
2020-01-22 09:11:56 -08:00
|
|
|
|
2021-12-17 05:01:12 -08:00
|
|
|
fn create_default_account() -> AccountSharedData {
|
|
|
|
AccountSharedData::new(0, 0, &Pubkey::new_unique())
|
2020-01-22 09:11:56 -08:00
|
|
|
}
|
2019-04-01 16:45:53 -07:00
|
|
|
|
2021-12-17 05:01:12 -08:00
|
|
|
fn create_default_stake_account() -> AccountSharedData {
|
|
|
|
AccountSharedData::new(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-12-17 05:01:12 -08:00
|
|
|
transaction_accounts: Vec<(Pubkey, AccountSharedData)>,
|
|
|
|
instruction_accounts: Vec<AccountMeta>,
|
|
|
|
expected_result: Result<(), InstructionError>,
|
|
|
|
) -> Vec<AccountSharedData> {
|
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-12-17 05:01:12 -08:00
|
|
|
transaction_accounts,
|
|
|
|
instruction_accounts,
|
|
|
|
expected_result,
|
2021-11-04 13:47:32 -07:00
|
|
|
super::process_instruction,
|
2021-10-08 02:41:07 -07:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-12-17 05:01:12 -08:00
|
|
|
fn process_instruction_as_one_arg(
|
|
|
|
instruction: &Instruction,
|
|
|
|
expected_result: Result<(), InstructionError>,
|
|
|
|
) -> Vec<AccountSharedData> {
|
|
|
|
let transaction_accounts = instruction
|
2019-06-17 19:34:21 -07:00
|
|
|
.accounts
|
|
|
|
.iter()
|
2021-12-17 05:01:12 -08:00
|
|
|
.map(|meta| {
|
|
|
|
(
|
|
|
|
meta.pubkey,
|
|
|
|
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())
|
|
|
|
},
|
|
|
|
)
|
|
|
|
})
|
2019-06-17 19:34:21 -07:00
|
|
|
.collect();
|
2022-01-11 19:19:11 -08:00
|
|
|
let mut sysvar_cache = SysvarCache::default();
|
2022-01-12 21:36:21 -08:00
|
|
|
sysvar_cache.set_clock(Clock::default());
|
2021-12-17 05:01:12 -08:00
|
|
|
mock_process_instruction_with_sysvars(
|
|
|
|
&id(),
|
|
|
|
Vec::new(),
|
|
|
|
&instruction.data,
|
|
|
|
transaction_accounts,
|
|
|
|
instruction.accounts.clone(),
|
|
|
|
expected_result,
|
2022-01-11 19:19:11 -08:00
|
|
|
&sysvar_cache,
|
2021-12-17 05:01:12 -08:00
|
|
|
super::process_instruction,
|
|
|
|
)
|
2019-05-07 17:08:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_stake_process_instruction() {
|
2021-12-17 05:01:12 -08: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(),
|
2021-12-17 05:01:12 -08:00
|
|
|
&Lockup::default(),
|
|
|
|
),
|
2019-10-31 11:07:27 -07:00
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
2021-12-17 05:01:12 -08: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,
|
2021-12-17 05:01:12 -08:00
|
|
|
),
|
2019-10-31 11:07:27 -07:00
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction_as_one_arg(
|
|
|
|
&instruction::split(
|
|
|
|
&Pubkey::new_unique(),
|
|
|
|
&Pubkey::new_unique(),
|
|
|
|
100,
|
|
|
|
&invalid_stake_state_pubkey(),
|
|
|
|
)[2],
|
2020-01-20 12:33:27 -08:00
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction_as_one_arg(
|
|
|
|
&instruction::merge(
|
|
|
|
&Pubkey::new_unique(),
|
|
|
|
&invalid_stake_state_pubkey(),
|
|
|
|
&Pubkey::new_unique(),
|
|
|
|
)[0],
|
2020-06-10 17:22:47 -07:00
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction_as_one_arg(
|
|
|
|
&instruction::split_with_seed(
|
|
|
|
&Pubkey::new_unique(),
|
|
|
|
&Pubkey::new_unique(),
|
|
|
|
100,
|
|
|
|
&invalid_stake_state_pubkey(),
|
|
|
|
&Pubkey::new_unique(),
|
|
|
|
"seed",
|
|
|
|
)[1],
|
2019-10-31 11:07:27 -07:00
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
2021-12-17 05:01:12 -08: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(),
|
2021-12-17 05:01:12 -08:00
|
|
|
),
|
2019-05-07 17:08:49 -07:00
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
2021-12-17 05:01:12 -08: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,
|
2021-12-17 05:01:12 -08:00
|
|
|
),
|
2019-06-21 22:28:34 -07:00
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction_as_one_arg(
|
|
|
|
&instruction::deactivate_stake(&Pubkey::new_unique(), &Pubkey::new_unique()),
|
2019-06-21 23:45:03 -07:00
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
2021-12-17 05:01:12 -08: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-12-17 05:01:12 -08: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() {
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction_as_one_arg(
|
|
|
|
&instruction::initialize(
|
2020-12-22 10:20:38 -08:00
|
|
|
&spoofed_stake_state_pubkey(),
|
|
|
|
&Authorized::default(),
|
2021-12-17 05:01:12 -08:00
|
|
|
&Lockup::default(),
|
|
|
|
),
|
2021-02-26 14:21:34 -08:00
|
|
|
Err(InstructionError::InvalidAccountOwner),
|
2020-12-22 10:20:38 -08:00
|
|
|
);
|
2021-12-17 05:01:12 -08: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,
|
2021-12-17 05:01:12 -08:00
|
|
|
),
|
2021-02-26 14:21:34 -08:00
|
|
|
Err(InstructionError::InvalidAccountOwner),
|
2020-12-22 10:20:38 -08:00
|
|
|
);
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction_as_one_arg(
|
|
|
|
&instruction::split(
|
|
|
|
&spoofed_stake_state_pubkey(),
|
|
|
|
&Pubkey::new_unique(),
|
|
|
|
100,
|
|
|
|
&Pubkey::new_unique(),
|
|
|
|
)[2],
|
2021-02-26 14:21:34 -08:00
|
|
|
Err(InstructionError::InvalidAccountOwner),
|
2020-12-22 10:20:38 -08:00
|
|
|
);
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction_as_one_arg(
|
|
|
|
&instruction::split(
|
|
|
|
&Pubkey::new_unique(),
|
|
|
|
&Pubkey::new_unique(),
|
|
|
|
100,
|
|
|
|
&spoofed_stake_state_pubkey(),
|
|
|
|
)[2],
|
2020-12-22 10:20:38 -08:00
|
|
|
Err(InstructionError::IncorrectProgramId),
|
|
|
|
);
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction_as_one_arg(
|
|
|
|
&instruction::merge(
|
|
|
|
&spoofed_stake_state_pubkey(),
|
|
|
|
&Pubkey::new_unique(),
|
|
|
|
&Pubkey::new_unique(),
|
|
|
|
)[0],
|
2021-02-26 14:21:34 -08:00
|
|
|
Err(InstructionError::InvalidAccountOwner),
|
2020-12-22 10:20:38 -08:00
|
|
|
);
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction_as_one_arg(
|
|
|
|
&instruction::merge(
|
|
|
|
&Pubkey::new_unique(),
|
|
|
|
&spoofed_stake_state_pubkey(),
|
|
|
|
&Pubkey::new_unique(),
|
|
|
|
)[0],
|
2020-12-22 10:20:38 -08:00
|
|
|
Err(InstructionError::IncorrectProgramId),
|
|
|
|
);
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction_as_one_arg(
|
|
|
|
&instruction::split_with_seed(
|
|
|
|
&spoofed_stake_state_pubkey(),
|
|
|
|
&Pubkey::new_unique(),
|
|
|
|
100,
|
|
|
|
&Pubkey::new_unique(),
|
|
|
|
&Pubkey::new_unique(),
|
|
|
|
"seed",
|
|
|
|
)[1],
|
2021-02-26 14:21:34 -08:00
|
|
|
Err(InstructionError::InvalidAccountOwner),
|
2020-12-22 10:20:38 -08:00
|
|
|
);
|
2021-12-17 05:01:12 -08: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(),
|
2021-12-17 05:01:12 -08:00
|
|
|
),
|
2021-02-26 14:21:34 -08:00
|
|
|
Err(InstructionError::InvalidAccountOwner),
|
2020-12-22 10:20:38 -08:00
|
|
|
);
|
2021-12-17 05:01:12 -08: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-12-17 05:01:12 -08:00
|
|
|
),
|
2021-02-26 14:21:34 -08:00
|
|
|
Err(InstructionError::InvalidAccountOwner),
|
2020-12-22 10:20:38 -08:00
|
|
|
);
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction_as_one_arg(
|
|
|
|
&instruction::deactivate_stake(&spoofed_stake_state_pubkey(), &Pubkey::new_unique()),
|
2021-02-26 14:21:34 -08:00
|
|
|
Err(InstructionError::InvalidAccountOwner),
|
2020-12-22 10:20:38 -08:00
|
|
|
);
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction_as_one_arg(
|
|
|
|
&instruction::set_lockup(
|
2020-12-22 10:20:38 -08:00
|
|
|
&spoofed_stake_state_pubkey(),
|
|
|
|
&LockupArgs::default(),
|
2021-12-17 05:01:12 -08:00
|
|
|
&Pubkey::new_unique(),
|
|
|
|
),
|
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
|
2021-12-17 05:01:12 -08:00
|
|
|
let stake_address = Pubkey::new_unique();
|
|
|
|
let stake_account = create_default_stake_account();
|
|
|
|
let rent_address = sysvar::rent::id();
|
|
|
|
let rent_account = account::create_account_shared_data_for_test(&Rent::default());
|
|
|
|
let rewards_address = sysvar::rewards::id();
|
|
|
|
let rewards_account =
|
|
|
|
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 =
|
|
|
|
account::create_account_shared_data_for_test(&StakeHistory::default());
|
|
|
|
let vote_address = Pubkey::new_unique();
|
|
|
|
let vote_account = AccountSharedData::new(0, 0, &solana_vote_program::id());
|
|
|
|
let clock_address = sysvar::clock::id();
|
|
|
|
let clock_account =
|
|
|
|
account::create_account_shared_data_for_test(&sysvar::clock::Clock::default());
|
|
|
|
let config_address = stake_config::id();
|
|
|
|
let config_account = config::create_account(0, &stake_config::Config::default());
|
2019-04-01 16:45:53 -07:00
|
|
|
|
2019-09-11 09:48:29 -07:00
|
|
|
// gets the "is_empty()" check
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction(
|
|
|
|
&serialize(&StakeInstruction::Initialize(
|
|
|
|
Authorized::default(),
|
|
|
|
Lockup::default(),
|
|
|
|
))
|
|
|
|
.unwrap(),
|
|
|
|
Vec::new(),
|
|
|
|
Vec::new(),
|
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-12-17 05:01:12 -08:00
|
|
|
process_instruction(
|
|
|
|
&serialize(&StakeInstruction::Initialize(
|
|
|
|
Authorized::default(),
|
|
|
|
Lockup::default(),
|
|
|
|
))
|
|
|
|
.unwrap(),
|
|
|
|
vec![(stake_address, stake_account.clone())],
|
|
|
|
vec![AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
}],
|
2019-10-31 11:07:27 -07:00
|
|
|
Err(InstructionError::NotEnoughAccountKeys),
|
|
|
|
);
|
|
|
|
|
|
|
|
// rent fails to deserialize
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction(
|
|
|
|
&serialize(&StakeInstruction::Initialize(
|
|
|
|
Authorized::default(),
|
|
|
|
Lockup::default(),
|
|
|
|
))
|
|
|
|
.unwrap(),
|
|
|
|
vec![
|
|
|
|
(stake_address, stake_account.clone()),
|
|
|
|
(rent_address, create_default_account()),
|
|
|
|
],
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: rent_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
2019-10-31 11:07:27 -07:00
|
|
|
Err(InstructionError::InvalidArgument),
|
|
|
|
);
|
|
|
|
|
|
|
|
// fails to deserialize stake state
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction(
|
|
|
|
&serialize(&StakeInstruction::Initialize(
|
|
|
|
Authorized::default(),
|
|
|
|
Lockup::default(),
|
|
|
|
))
|
|
|
|
.unwrap(),
|
|
|
|
vec![
|
|
|
|
(stake_address, stake_account.clone()),
|
|
|
|
(rent_address, rent_account),
|
|
|
|
],
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: rent_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
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-12-17 05:01:12 -08:00
|
|
|
process_instruction(
|
|
|
|
&serialize(&StakeInstruction::DelegateStake).unwrap(),
|
|
|
|
vec![(stake_address, stake_account.clone())],
|
|
|
|
vec![AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
}],
|
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-12-17 05:01:12 -08:00
|
|
|
process_instruction(
|
|
|
|
&serialize(&StakeInstruction::DelegateStake).unwrap(),
|
|
|
|
vec![(stake_address, stake_account.clone())],
|
|
|
|
vec![AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
}],
|
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-12-17 05:01:12 -08:00
|
|
|
process_instruction(
|
|
|
|
&serialize(&StakeInstruction::DelegateStake).unwrap(),
|
|
|
|
vec![
|
|
|
|
(stake_address, stake_account.clone()),
|
|
|
|
(vote_address, vote_account.clone()),
|
|
|
|
(clock_address, clock_account),
|
|
|
|
(stake_history_address, stake_history_account.clone()),
|
|
|
|
(config_address, config_account),
|
|
|
|
],
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: vote_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: clock_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_history_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: config_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
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-12-17 05:01:12 -08:00
|
|
|
process_instruction(
|
|
|
|
&serialize(&StakeInstruction::Withdraw(42)).unwrap(),
|
|
|
|
vec![
|
|
|
|
(stake_address, stake_account.clone()),
|
|
|
|
(vote_address, vote_account),
|
|
|
|
(rewards_address, rewards_account.clone()),
|
|
|
|
(stake_history_address, stake_history_account),
|
|
|
|
],
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: vote_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: rewards_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_history_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
2019-06-21 22:28:34 -07:00
|
|
|
Err(InstructionError::InvalidArgument),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Tests correct number of accounts are provided in withdraw
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction(
|
|
|
|
&serialize(&StakeInstruction::Withdraw(42)).unwrap(),
|
|
|
|
vec![(stake_address, stake_account.clone())],
|
|
|
|
vec![AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
}],
|
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-12-17 05:01:12 -08:00
|
|
|
process_instruction(
|
|
|
|
&serialize(&StakeInstruction::Deactivate).unwrap(),
|
|
|
|
vec![
|
|
|
|
(stake_address, stake_account),
|
|
|
|
(rewards_address, rewards_account),
|
|
|
|
],
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: rewards_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
2019-06-21 23:45:03 -07:00
|
|
|
Err(InstructionError::InvalidArgument),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Tests correct number of accounts are provided in deactivate
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction(
|
|
|
|
&serialize(&StakeInstruction::Deactivate).unwrap(),
|
|
|
|
Vec::new(),
|
|
|
|
Vec::new(),
|
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();
|
2021-12-17 05:01:12 -08:00
|
|
|
let staker_account = create_default_account();
|
2021-07-06 12:24:10 -07:00
|
|
|
let withdrawer = Pubkey::new_unique();
|
2021-12-17 05:01:12 -08:00
|
|
|
let withdrawer_account = create_default_account();
|
|
|
|
let authorized_address = Pubkey::new_unique();
|
|
|
|
let authorized_account = create_default_account();
|
|
|
|
let new_authorized_account = create_default_account();
|
|
|
|
let clock_address = sysvar::clock::id();
|
|
|
|
let clock_account = account::create_account_shared_data_for_test(&Clock::default());
|
|
|
|
let custodian = Pubkey::new_unique();
|
|
|
|
let custodian_account = create_default_account();
|
2021-07-06 12:24:10 -07:00
|
|
|
|
|
|
|
// Test InitializeChecked with non-signing withdrawer
|
|
|
|
let mut instruction =
|
|
|
|
initialize_checked(&stake_address, &Authorized { staker, withdrawer });
|
|
|
|
instruction.accounts[3] = AccountMeta::new_readonly(withdrawer, false);
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction_as_one_arg(
|
|
|
|
&instruction,
|
2021-07-06 12:24:10 -07:00
|
|
|
Err(InstructionError::MissingRequiredSignature),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test InitializeChecked with withdrawer signer
|
2021-12-17 05:01:12 -08:00
|
|
|
let stake_account = AccountSharedData::new(
|
2021-07-06 12:24:10 -07:00
|
|
|
1_000_000_000,
|
|
|
|
std::mem::size_of::<crate::stake_state::StakeState>(),
|
|
|
|
&id(),
|
|
|
|
);
|
|
|
|
let rent_address = sysvar::rent::id();
|
2021-12-17 05:01:12 -08:00
|
|
|
let rent_account = account::create_account_shared_data_for_test(&Rent::default());
|
|
|
|
process_instruction(
|
|
|
|
&serialize(&StakeInstruction::InitializeChecked).unwrap(),
|
|
|
|
vec![
|
|
|
|
(stake_address, stake_account),
|
|
|
|
(rent_address, rent_account),
|
|
|
|
(staker, staker_account),
|
|
|
|
(withdrawer, withdrawer_account.clone()),
|
|
|
|
],
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: rent_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: staker,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: withdrawer,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
2021-07-06 12:24:10 -07:00
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test AuthorizeChecked with non-signing authority
|
|
|
|
let mut instruction = authorize_checked(
|
|
|
|
&stake_address,
|
|
|
|
&authorized_address,
|
|
|
|
&staker,
|
|
|
|
StakeAuthorize::Staker,
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
instruction.accounts[3] = AccountMeta::new_readonly(staker, false);
|
2021-12-17 05:01:12 -08: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);
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction_as_one_arg(
|
|
|
|
&instruction,
|
2021-07-06 12:24:10 -07:00
|
|
|
Err(InstructionError::MissingRequiredSignature),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test AuthorizeChecked with authority signer
|
2021-12-17 05:01:12 -08:00
|
|
|
let stake_account = AccountSharedData::new_data_with_space(
|
|
|
|
42,
|
|
|
|
&StakeState::Initialized(Meta::auto(&authorized_address)),
|
|
|
|
std::mem::size_of::<StakeState>(),
|
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
process_instruction(
|
|
|
|
&serialize(&StakeInstruction::AuthorizeChecked(StakeAuthorize::Staker)).unwrap(),
|
|
|
|
vec![
|
|
|
|
(stake_address, stake_account.clone()),
|
|
|
|
(clock_address, clock_account.clone()),
|
|
|
|
(authorized_address, authorized_account.clone()),
|
|
|
|
(staker, new_authorized_account.clone()),
|
|
|
|
],
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: clock_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: authorized_address,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: staker,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
2021-07-06 12:24:10 -07:00
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction(
|
|
|
|
&serialize(&StakeInstruction::AuthorizeChecked(
|
|
|
|
StakeAuthorize::Withdrawer,
|
|
|
|
))
|
|
|
|
.unwrap(),
|
|
|
|
vec![
|
|
|
|
(stake_address, stake_account),
|
|
|
|
(clock_address, clock_account.clone()),
|
|
|
|
(authorized_address, authorized_account.clone()),
|
|
|
|
(withdrawer, new_authorized_account.clone()),
|
|
|
|
],
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: clock_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: authorized_address,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: withdrawer,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
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);
|
2021-12-17 05:01:12 -08: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);
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction_as_one_arg(
|
|
|
|
&instruction,
|
2021-07-06 12:24:10 -07:00
|
|
|
Err(InstructionError::MissingRequiredSignature),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test AuthorizeCheckedWithSeed with authority signer
|
2021-12-17 05:01:12 -08:00
|
|
|
let stake_account = AccountSharedData::new_data_with_space(
|
|
|
|
42,
|
|
|
|
&StakeState::Initialized(Meta::auto(&address_with_seed)),
|
|
|
|
std::mem::size_of::<StakeState>(),
|
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
process_instruction(
|
|
|
|
&serialize(&StakeInstruction::AuthorizeCheckedWithSeed(
|
|
|
|
AuthorizeCheckedWithSeedArgs {
|
|
|
|
stake_authorize: StakeAuthorize::Staker,
|
|
|
|
authority_seed: seed.to_string(),
|
|
|
|
authority_owner: authorized_owner,
|
|
|
|
},
|
|
|
|
))
|
2021-11-04 13:47:32 -07:00
|
|
|
.unwrap(),
|
2021-12-17 05:01:12 -08:00
|
|
|
vec![
|
|
|
|
(address_with_seed, stake_account.clone()),
|
|
|
|
(authorized_owner, authorized_account.clone()),
|
|
|
|
(clock_address, clock_account.clone()),
|
|
|
|
(staker, new_authorized_account.clone()),
|
|
|
|
],
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: address_with_seed,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: authorized_owner,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: clock_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: staker,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
2021-07-06 12:24:10 -07:00
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction(
|
|
|
|
&serialize(&StakeInstruction::AuthorizeCheckedWithSeed(
|
|
|
|
AuthorizeCheckedWithSeedArgs {
|
|
|
|
stake_authorize: StakeAuthorize::Withdrawer,
|
|
|
|
authority_seed: seed.to_string(),
|
|
|
|
authority_owner: authorized_owner,
|
|
|
|
},
|
|
|
|
))
|
|
|
|
.unwrap(),
|
|
|
|
vec![
|
|
|
|
(address_with_seed, stake_account),
|
|
|
|
(authorized_owner, authorized_account),
|
|
|
|
(clock_address, clock_account),
|
|
|
|
(withdrawer, new_authorized_account),
|
|
|
|
],
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: address_with_seed,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: authorized_owner,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: clock_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: withdrawer,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
2021-07-06 12:24:10 -07:00
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test SetLockupChecked with non-signing lockup custodian
|
|
|
|
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);
|
2021-12-17 05:01:12 -08: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-12-17 05:01:12 -08:00
|
|
|
let stake_account = AccountSharedData::new_data_with_space(
|
|
|
|
42,
|
|
|
|
&StakeState::Initialized(Meta::auto(&withdrawer)),
|
|
|
|
std::mem::size_of::<StakeState>(),
|
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2021-07-06 12:24:10 -07:00
|
|
|
|
2022-01-11 19:19:11 -08:00
|
|
|
let mut sysvar_cache = SysvarCache::default();
|
2022-01-12 21:36:21 -08:00
|
|
|
sysvar_cache.set_clock(Clock::default());
|
2021-12-17 05:01:12 -08:00
|
|
|
mock_process_instruction_with_sysvars(
|
|
|
|
&id(),
|
|
|
|
Vec::new(),
|
|
|
|
&instruction.data,
|
|
|
|
vec![
|
|
|
|
(stake_address, stake_account),
|
|
|
|
(withdrawer, withdrawer_account),
|
|
|
|
(custodian, custodian_account),
|
|
|
|
],
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: withdrawer,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: custodian,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
2021-07-06 12:24:10 -07:00
|
|
|
Ok(()),
|
2022-01-11 19:19:11 -08:00
|
|
|
&sysvar_cache,
|
2021-12-17 05:01:12 -08:00
|
|
|
super::process_instruction,
|
2021-07-06 12:24:10 -07:00
|
|
|
);
|
|
|
|
}
|
2019-04-01 16:45:53 -07:00
|
|
|
}
|