#![allow(clippy::arithmetic_side_effects)]
use {
agave_feature_set::stake_raise_minimum_delegation_to_1_sol,
assert_matches::assert_matches,
bincode::serialize,
mollusk_svm::{result::Check, Mollusk},
solana_account::{
create_account_shared_data_for_test, state_traits::StateMut, AccountSharedData,
ReadableAccount, WritableAccount,
},
solana_clock::{Clock, Epoch},
solana_epoch_rewards::EpochRewards,
solana_epoch_schedule::EpochSchedule,
solana_instruction::{AccountMeta, Instruction},
solana_program_error::ProgramError,
solana_pubkey::Pubkey,
solana_rent::Rent,
solana_sdk_ids::system_program,
solana_stake_interface::{
config as stake_config,
error::StakeError,
instruction::{
self, authorize_checked, authorize_checked_with_seed, initialize_checked,
set_lockup_checked, AuthorizeCheckedWithSeedArgs, AuthorizeWithSeedArgs, LockupArgs,
StakeInstruction,
},
stake_flags::StakeFlags,
stake_history::{StakeHistory, StakeHistoryEntry},
state::{
warmup_cooldown_rate, Authorized, Delegation, Lockup, Meta, Stake, StakeAuthorize,
StakeStateV2,
},
MINIMUM_DELINQUENT_EPOCHS_FOR_DEACTIVATION,
},
solana_stake_program::{get_minimum_delegation, id},
solana_sysvar::{clock, epoch_rewards, epoch_schedule, rent, rewards},
solana_sysvar_id::SysvarId,
solana_vote_interface::state::{VoteStateV4, VoteStateVersions, MAX_EPOCH_CREDITS_HISTORY},
std::{collections::HashSet, str::FromStr},
test_case::test_case,
};
fn mollusk_bpf() -> Mollusk {
let mut mollusk = Mollusk::new(&id(), "solana_stake_program");
mollusk
.feature_set
.deactivate(&stake_raise_minimum_delegation_to_1_sol::id());
mollusk
}
fn create_default_account() -> AccountSharedData {
AccountSharedData::new(0, 0, &Pubkey::new_unique())
}
fn create_default_stake_account() -> AccountSharedData {
AccountSharedData::new(0, 0, &id())
}
fn create_default_vote_account() -> AccountSharedData {
let space = VoteStateV4::size_of();
let lamports = Rent::default().minimum_balance(space);
let state = VoteStateVersions::new_v4(VoteStateV4::default());
AccountSharedData::new_data_with_space(lamports, &state, space, &solana_sdk_ids::vote::id())
.unwrap()
}
fn default_stake_rent() -> u64 {
Rent::default().minimum_balance(StakeStateV2::size_of())
}
fn invalid_stake_state_pubkey() -> Pubkey {
Pubkey::from_str("BadStake11111111111111111111111111111111111").unwrap()
}
fn invalid_vote_state_pubkey() -> Pubkey {
Pubkey::from_str("BadVote111111111111111111111111111111111111").unwrap()
}
fn spoofed_stake_state_pubkey() -> Pubkey {
Pubkey::from_str("SpoofedStake1111111111111111111111111111111").unwrap()
}
fn spoofed_stake_program_id() -> Pubkey {
Pubkey::from_str("Spoofed111111111111111111111111111111111111").unwrap()
}
fn process_instruction(
mollusk: &Mollusk,
instruction_data: &[u8],
mut transaction_accounts: Vec<(Pubkey, AccountSharedData)>,
instruction_accounts: Vec<AccountMeta>,
expected_result: Result<(), ProgramError>,
) -> Vec<AccountSharedData> {
for ixn_key in instruction_accounts.iter().map(|meta| meta.pubkey) {
if !transaction_accounts
.iter()
.any(|(txn_key, _)| *txn_key == ixn_key)
{
transaction_accounts.push((ixn_key, AccountSharedData::default()));
}
}
let instruction = Instruction {
program_id: id(),
accounts: instruction_accounts,
data: instruction_data.to_vec(),
};
let check = match expected_result {
Ok(()) => Check::success(),
Err(e) => Check::err(e),
};
let result = mollusk.process_and_validate_instruction(
&instruction,
&transaction_accounts
.into_iter()
.map(|(key, account)| (key, account.into()))
.collect::<Vec<_>>(),
&[check],
);
result
.resulting_accounts
.into_iter()
.map(|(_, account)| account.into())
.collect()
}
fn get_default_transaction_accounts(instruction: &Instruction) -> Vec<(Pubkey, AccountSharedData)> {
let mut pubkeys: HashSet<Pubkey> = instruction
.accounts
.iter()
.map(|meta| meta.pubkey)
.collect();
pubkeys.insert(clock::id());
pubkeys.insert(epoch_schedule::id());
pubkeys.insert(StakeHistory::id());
#[allow(deprecated)]
pubkeys
.iter()
.map(|pubkey| {
(
*pubkey,
if clock::check_id(pubkey) {
create_account_shared_data_for_test(&clock::Clock::default())
} else if rewards::check_id(pubkey) {
create_account_shared_data_for_test(&rewards::Rewards::new(0.0))
} else if StakeHistory::check_id(pubkey) {
create_account_shared_data_for_test(&StakeHistory::default())
} else if stake_config::check_id(pubkey) {
config::create_account(0, &stake_config::Config::default())
} else if epoch_schedule::check_id(pubkey) {
create_account_shared_data_for_test(&EpochSchedule::default())
} else if rent::check_id(pubkey) {
create_account_shared_data_for_test(&Rent::default())
} else if *pubkey == invalid_stake_state_pubkey() {
AccountSharedData::new(0, 0, &id())
} else if *pubkey == invalid_vote_state_pubkey() {
AccountSharedData::new(0, 0, &solana_sdk_ids::vote::id())
} else if *pubkey == spoofed_stake_state_pubkey() {
AccountSharedData::new(0, 0, &spoofed_stake_program_id())
} else {
AccountSharedData::new(0, 0, &id())
},
)
})
.collect()
}
fn process_instruction_as_one_arg(
mollusk: &Mollusk,
instruction: &Instruction,
expected_result: Result<(), ProgramError>,
) -> Vec<AccountSharedData> {
let transaction_accounts = get_default_transaction_accounts(instruction);
process_instruction(
mollusk,
&instruction.data,
transaction_accounts,
instruction.accounts.clone(),
expected_result,
)
}
fn new_stake(
stake: u64,
voter_pubkey: &Pubkey,
vote_state: &VoteStateV4,
activation_epoch: Epoch,
) -> Stake {
Stake {
delegation: Delegation::new(voter_pubkey, stake, activation_epoch),
credits_observed: vote_state.credits(),
}
}
fn from<T: ReadableAccount + StateMut<StakeStateV2>>(account: &T) -> Option<StakeStateV2> {
account.state().ok()
}
fn stake_from<T: ReadableAccount + StateMut<StakeStateV2>>(account: &T) -> Option<Stake> {
from(account).and_then(|state: StakeStateV2| state.stake())
}
fn authorized_from(account: &AccountSharedData) -> Option<Authorized> {
from(account).and_then(|state: StakeStateV2| state.authorized())
}
fn just_stake(meta: Meta, stake: u64) -> StakeStateV2 {
StakeStateV2::Stake(
meta,
Stake {
delegation: Delegation {
stake,
..Delegation::default()
},
..Stake::default()
},
StakeFlags::empty(),
)
}
fn is_closed(account: &AccountSharedData) -> bool {
account.lamports() == 0 && *account.owner() == id() && account.data().is_empty()
}
fn get_active_stake_for_tests(
stake_accounts: &[AccountSharedData],
clock: &Clock,
stake_history: &StakeHistory,
) -> u64 {
let mut active_stake = 0;
for account in stake_accounts {
if let Ok(StakeStateV2::Stake(_meta, stake, _stake_flags)) = account.state() {
let stake_status = stake.delegation.stake_activating_and_deactivating(
clock.epoch,
stake_history,
None,
);
active_stake += stake_status.effective;
}
}
active_stake
}
fn create_empty_stake_history_for_test() -> AccountSharedData {
AccountSharedData::create(1, vec![0; 8], solana_sdk_ids::sysvar::id(), false, u64::MAX)
}
fn new_stake_history_entry<'a, I>(
epoch: Epoch,
stakes: I,
history: &StakeHistory,
new_rate_activation_epoch: Option<Epoch>,
) -> StakeHistoryEntry
where
I: Iterator<Item = &'a Delegation>,
{
stakes.fold(StakeHistoryEntry::default(), |sum, stake| {
sum + stake.stake_activating_and_deactivating(epoch, history, new_rate_activation_epoch)
})
}
fn create_stake_history_from_delegations(
bootstrap: Option<u64>,
epochs: std::ops::Range<Epoch>,
delegations: &[Delegation],
new_rate_activation_epoch: Option<Epoch>,
) -> StakeHistory {
let mut stake_history = StakeHistory::default();
let bootstrap_delegation = if let Some(bootstrap) = bootstrap {
vec![Delegation {
activation_epoch: u64::MAX,
stake: bootstrap,
..Delegation::default()
}]
} else {
vec![]
};
for epoch in epochs {
let entry = new_stake_history_entry(
epoch,
delegations.iter().chain(bootstrap_delegation.iter()),
&stake_history,
new_rate_activation_epoch,
);
stake_history.add(epoch, entry);
}
stake_history
}
fn increment_credits(vote_state: &mut VoteStateV4, epoch: Epoch, credits: u64) {
if vote_state.epoch_credits.is_empty() {
vote_state.epoch_credits.push((epoch, 0, 0));
} else if epoch != vote_state.epoch_credits.last().unwrap().0 {
let (_, credits, prev_credits) = *vote_state.epoch_credits.last().unwrap();
if credits != prev_credits {
vote_state.epoch_credits.push((epoch, credits, credits));
} else {
vote_state.epoch_credits.last_mut().unwrap().0 = epoch;
}
if vote_state.epoch_credits.len() > MAX_EPOCH_CREDITS_HISTORY {
vote_state.epoch_credits.remove(0);
}
}
vote_state.epoch_credits.last_mut().unwrap().1 = vote_state
.epoch_credits
.last()
.unwrap()
.1
.saturating_add(credits);
}
mod config {
#[allow(deprecated)]
use {
solana_account::{Account, AccountSharedData},
solana_config_interface::state::ConfigKeys,
solana_stake_interface::config::Config,
};
#[allow(deprecated)]
pub fn create_account(lamports: u64, config: &Config) -> AccountSharedData {
let mut data = bincode::serialize(&ConfigKeys { keys: vec![] }).unwrap();
data.extend_from_slice(&bincode::serialize(config).unwrap());
AccountSharedData::from(Account {
lamports,
data,
owner: solana_sdk_ids::config::id(),
..Account::default()
})
}
}
#[test]
fn test_stake_process_instruction() {
let mollusk = mollusk_bpf();
process_instruction_as_one_arg(
&mollusk,
&instruction::initialize(
&Pubkey::new_unique(),
&Authorized::default(),
&Lockup::default(),
),
Err(ProgramError::InvalidAccountData),
);
process_instruction_as_one_arg(
&mollusk,
&instruction::authorize(
&Pubkey::new_unique(),
&Pubkey::new_unique(),
&Pubkey::new_unique(),
StakeAuthorize::Staker,
None,
),
Err(ProgramError::InvalidAccountData),
);
process_instruction_as_one_arg(
&mollusk,
&instruction::split(
&Pubkey::new_unique(),
&Pubkey::new_unique(),
100,
&invalid_stake_state_pubkey(),
)[2],
Err(ProgramError::InvalidAccountData),
);
process_instruction_as_one_arg(
&mollusk,
&instruction::merge(
&Pubkey::new_unique(),
&invalid_stake_state_pubkey(),
&Pubkey::new_unique(),
)[0],
Err(ProgramError::InvalidAccountData),
);
process_instruction_as_one_arg(
&mollusk,
&instruction::split_with_seed(
&Pubkey::new_unique(),
&Pubkey::new_unique(),
100,
&invalid_stake_state_pubkey(),
&Pubkey::new_unique(),
"seed",
)[1],
Err(ProgramError::InvalidAccountData),
);
process_instruction_as_one_arg(
&mollusk,
&instruction::delegate_stake(
&Pubkey::new_unique(),
&Pubkey::new_unique(),
&invalid_vote_state_pubkey(),
),
Err(ProgramError::InvalidAccountData),
);
process_instruction_as_one_arg(
&mollusk,
&instruction::withdraw(
&Pubkey::new_unique(),
&Pubkey::new_unique(),
&Pubkey::new_unique(),
100,
None,
),
Err(ProgramError::MissingRequiredSignature),
);
process_instruction_as_one_arg(
&mollusk,
&instruction::deactivate_stake(&Pubkey::new_unique(), &Pubkey::new_unique()),
Err(ProgramError::InvalidAccountData),
);
process_instruction_as_one_arg(
&mollusk,
&instruction::set_lockup(
&Pubkey::new_unique(),
&LockupArgs::default(),
&Pubkey::new_unique(),
),
Err(ProgramError::InvalidAccountData),
);
process_instruction_as_one_arg(
&mollusk,
&instruction::deactivate_delinquent_stake(
&Pubkey::new_unique(),
&Pubkey::new_unique(),
&invalid_vote_state_pubkey(),
),
Err(ProgramError::IncorrectProgramId),
);
process_instruction_as_one_arg(
&mollusk,
&instruction::deactivate_delinquent_stake(
&Pubkey::new_unique(),
&invalid_vote_state_pubkey(),
&Pubkey::new_unique(),
),
Err(ProgramError::InvalidAccountData),
);
process_instruction_as_one_arg(
&mollusk,
&instruction::deactivate_delinquent_stake(
&Pubkey::new_unique(),
&invalid_vote_state_pubkey(),
&invalid_vote_state_pubkey(),
),
Err(ProgramError::InvalidAccountData),
);
process_instruction_as_one_arg(
&mollusk,
&instruction::move_stake(
&Pubkey::new_unique(),
&Pubkey::new_unique(),
&Pubkey::new_unique(),
100,
),
Err(ProgramError::InvalidAccountData),
);
process_instruction_as_one_arg(
&mollusk,
&instruction::move_lamports(
&Pubkey::new_unique(),
&Pubkey::new_unique(),
&Pubkey::new_unique(),
100,
),
Err(ProgramError::InvalidAccountData),
);
}
#[test]
fn test_stake_process_instruction_decode_bail() {
let mollusk = mollusk_bpf();
let stake_address = Pubkey::new_unique();
let stake_account = create_default_stake_account();
let rent_address = rent::id();
let rent = Rent::default();
let rent_account = create_account_shared_data_for_test(&rent);
let stake_history_address = StakeHistory::id();
let stake_history_account = create_account_shared_data_for_test(&StakeHistory::default());
let vote_address = Pubkey::new_unique();
let vote_account = AccountSharedData::new(0, 0, &solana_sdk_ids::vote::id());
let clock_address = clock::id();
let clock_account = create_account_shared_data_for_test(&clock::Clock::default());
#[allow(deprecated)]
let config_address = stake_config::id();
#[allow(deprecated)]
let config_account = config::create_account(0, &stake_config::Config::default());
let rent_exempt_reserve = rent.minimum_balance(StakeStateV2::size_of());
let minimum_delegation = crate::get_minimum_delegation();
let withdrawal_amount = rent_exempt_reserve + minimum_delegation;
process_instruction(
&mollusk,
&serialize(&StakeInstruction::Initialize(
Authorized::default(),
Lockup::default(),
))
.unwrap(),
Vec::new(),
Vec::new(),
Err(ProgramError::NotEnoughAccountKeys),
);
process_instruction(
&mollusk,
&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: true,
},
AccountMeta {
pubkey: rent_address,
is_signer: false,
is_writable: false,
},
],
Err(ProgramError::InvalidAccountData),
);
process_instruction(
&mollusk,
&serialize(&StakeInstruction::DelegateStake).unwrap(),
vec![(stake_address, stake_account.clone())],
vec![AccountMeta {
pubkey: stake_address,
is_signer: false,
is_writable: true,
}],
Err(ProgramError::NotEnoughAccountKeys),
);
process_instruction(
&mollusk,
&serialize(&StakeInstruction::DelegateStake).unwrap(),
vec![(stake_address, stake_account.clone())],
vec![AccountMeta {
pubkey: stake_address,
is_signer: false,
is_writable: true,
}],
Err(ProgramError::NotEnoughAccountKeys),
);
process_instruction(
&mollusk,
&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: true,
},
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,
},
],
Err(ProgramError::InvalidAccountData),
);
process_instruction(
&mollusk,
&serialize(&StakeInstruction::Withdraw(withdrawal_amount)).unwrap(),
vec![(stake_address, stake_account.clone())],
vec![AccountMeta {
pubkey: stake_address,
is_signer: false,
is_writable: true,
}],
Err(ProgramError::NotEnoughAccountKeys),
);
process_instruction(
&mollusk,
&serialize(&StakeInstruction::Deactivate).unwrap(),
Vec::new(),
Vec::new(),
Err(ProgramError::NotEnoughAccountKeys),
);
process_instruction(
&mollusk,
&serialize(&StakeInstruction::DeactivateDelinquent).unwrap(),
Vec::new(),
Vec::new(),
Err(ProgramError::NotEnoughAccountKeys),
);
process_instruction(
&mollusk,
&serialize(&StakeInstruction::DeactivateDelinquent).unwrap(),
vec![(stake_address, stake_account.clone())],
vec![AccountMeta {
pubkey: stake_address,
is_signer: false,
is_writable: true,
}],
Err(ProgramError::NotEnoughAccountKeys),
);
process_instruction(
&mollusk,
&serialize(&StakeInstruction::DeactivateDelinquent).unwrap(),
vec![(stake_address, stake_account), (vote_address, vote_account)],
vec![
AccountMeta {
pubkey: stake_address,
is_signer: false,
is_writable: true,
},
AccountMeta {
pubkey: vote_address,
is_signer: false,
is_writable: false,
},
],
Err(ProgramError::NotEnoughAccountKeys),
);
}
#[test]
fn test_stake_checked_instructions() {
let mollusk = mollusk_bpf();
let stake_address = Pubkey::new_unique();
let staker = Pubkey::new_unique();
let staker_account = create_default_account();
let withdrawer = Pubkey::new_unique();
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 = clock::id();
let clock_account = create_account_shared_data_for_test(&Clock::default());
let custodian = Pubkey::new_unique();
let custodian_account = create_default_account();
let rent = Rent::default();
let rent_address = rent::id();
let rent_account = create_account_shared_data_for_test(&rent);
let rent_exempt_reserve = rent.minimum_balance(StakeStateV2::size_of());
let minimum_delegation = crate::get_minimum_delegation();
let mut instruction = initialize_checked(&stake_address, &Authorized { staker, withdrawer });
instruction.accounts[3] = AccountMeta::new_readonly(withdrawer, false);
process_instruction_as_one_arg(
&mollusk,
&instruction,
Err(ProgramError::MissingRequiredSignature),
);
let stake_account = AccountSharedData::new(
rent_exempt_reserve + minimum_delegation,
StakeStateV2::size_of(),
&id(),
);
process_instruction(
&mollusk,
&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: true,
},
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,
},
],
Ok(()),
);
let mut instruction = authorize_checked(
&stake_address,
&authorized_address,
&staker,
StakeAuthorize::Staker,
None,
);
instruction.accounts[3] = AccountMeta::new_readonly(staker, false);
process_instruction_as_one_arg(
&mollusk,
&instruction,
Err(ProgramError::MissingRequiredSignature),
);
let mut instruction = authorize_checked(
&stake_address,
&authorized_address,
&withdrawer,
StakeAuthorize::Withdrawer,
None,
);
instruction.accounts[3] = AccountMeta::new_readonly(withdrawer, false);
process_instruction_as_one_arg(
&mollusk,
&instruction,
Err(ProgramError::MissingRequiredSignature),
);
let stake_account = AccountSharedData::new_data_with_space(
42,
&StakeStateV2::Initialized(Meta::auto(&authorized_address)),
StakeStateV2::size_of(),
&id(),
)
.unwrap();
process_instruction(
&mollusk,
&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: true,
},
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,
},
],
Ok(()),
);
process_instruction(
&mollusk,
&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: true,
},
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,
},
],
Ok(()),
);
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);
process_instruction_as_one_arg(
&mollusk,
&instruction,
Err(ProgramError::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);
process_instruction_as_one_arg(
&mollusk,
&instruction,
Err(ProgramError::MissingRequiredSignature),
);
let stake_account = AccountSharedData::new_data_with_space(
42,
&StakeStateV2::Initialized(Meta::auto(&address_with_seed)),
StakeStateV2::size_of(),
&id(),
)
.unwrap();
process_instruction(
&mollusk,
&serialize(&StakeInstruction::AuthorizeCheckedWithSeed(
AuthorizeCheckedWithSeedArgs {
stake_authorize: StakeAuthorize::Staker,
authority_seed: seed.to_string(),
authority_owner: authorized_owner,
},
))
.unwrap(),
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: true,
},
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,
},
],
Ok(()),
);
process_instruction(
&mollusk,
&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.clone()),
(withdrawer, new_authorized_account),
],
vec![
AccountMeta {
pubkey: address_with_seed,
is_signer: false,
is_writable: true,
},
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,
},
],
Ok(()),
);
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);
process_instruction_as_one_arg(
&mollusk,
&instruction,
Err(ProgramError::MissingRequiredSignature),
);
let stake_account = AccountSharedData::new_data_with_space(
42,
&StakeStateV2::Initialized(Meta::auto(&withdrawer)),
StakeStateV2::size_of(),
&id(),
)
.unwrap();
process_instruction(
&mollusk,
&instruction.data,
vec![
(clock_address, clock_account),
(stake_address, stake_account),
(withdrawer, withdrawer_account),
(custodian, custodian_account),
],
vec![
AccountMeta {
pubkey: stake_address,
is_signer: false,
is_writable: true,
},
AccountMeta {
pubkey: withdrawer,
is_signer: true,
is_writable: false,
},
AccountMeta {
pubkey: custodian,
is_signer: true,
is_writable: false,
},
],
Ok(()),
);
}
#[test]
fn test_stake_initialize() {
let mollusk = mollusk_bpf();
let rent = Rent::default();
let rent_exempt_reserve = rent.minimum_balance(StakeStateV2::size_of());
let stake_lamports = rent_exempt_reserve;
let stake_address = solana_pubkey::new_rand();
let stake_account = AccountSharedData::new(stake_lamports, StakeStateV2::size_of(), &id());
let custodian_address = solana_pubkey::new_rand();
let lockup = Lockup {
epoch: 1,
unix_timestamp: 0,
custodian: custodian_address,
};
let instruction_data = serialize(&StakeInstruction::Initialize(
Authorized::auto(&stake_address),
lockup,
))
.unwrap();
let mut transaction_accounts = vec![
(stake_address, stake_account.clone()),
(rent::id(), create_account_shared_data_for_test(&rent)),
];
let instruction_accounts = vec![
AccountMeta {
pubkey: stake_address,
is_signer: false,
is_writable: true,
},
AccountMeta {
pubkey: rent::id(),
is_signer: false,
is_writable: false,
},
];
let accounts = process_instruction(
&mollusk,
&instruction_data,
transaction_accounts.clone(),
instruction_accounts.clone(),
Ok(()),
);
assert_eq!(
from(&accounts[0]).unwrap(),
StakeStateV2::Initialized(Meta {
authorized: Authorized::auto(&stake_address),
rent_exempt_reserve,
lockup,
}),
);
transaction_accounts[0] = (stake_address, accounts[0].clone());
process_instruction(
&mollusk,
&instruction_data,
transaction_accounts.clone(),
instruction_accounts.clone(),
Err(ProgramError::InvalidAccountData),
);
transaction_accounts[0] = (stake_address, stake_account);
transaction_accounts[1] = (
rent::id(),
create_account_shared_data_for_test(&Rent {
lamports_per_byte_year: rent.lamports_per_byte_year + 1,
..rent
}),
);
process_instruction(
&mollusk,
&instruction_data,
transaction_accounts.clone(),
instruction_accounts.clone(),
Err(ProgramError::InsufficientFunds),
);
let stake_account = AccountSharedData::new(stake_lamports, StakeStateV2::size_of() + 1, &id());
transaction_accounts[0] = (stake_address, stake_account);
process_instruction(
&mollusk,
&instruction_data,
transaction_accounts.clone(),
instruction_accounts.clone(),
Err(ProgramError::InvalidAccountData),
);
let stake_account = AccountSharedData::new(stake_lamports, StakeStateV2::size_of() - 1, &id());
transaction_accounts[0] = (stake_address, stake_account);
process_instruction(
&mollusk,
&instruction_data,
transaction_accounts,
instruction_accounts,
Err(ProgramError::InvalidAccountData),
);
}
#[test]
fn test_authorize() {
let mollusk = mollusk_bpf();
let authority_address = solana_pubkey::new_rand();
let authority_address_2 = solana_pubkey::new_rand();
let stake_address = solana_pubkey::new_rand();
let stake_lamports = 42;
let stake_account = AccountSharedData::new_data_with_space(
stake_lamports,
&StakeStateV2::default(),
StakeStateV2::size_of(),
&id(),
)
.unwrap();
let to_address = solana_pubkey::new_rand();
let to_account = AccountSharedData::new(1, 0, &system_program::id());
let mut transaction_accounts = vec![
(stake_address, stake_account),
(to_address, to_account),
(authority_address, AccountSharedData::default()),
(
clock::id(),
create_account_shared_data_for_test(&Clock::default()),
),
(
StakeHistory::id(),
create_account_shared_data_for_test(&StakeHistory::default()),
),
(
epoch_schedule::id(),
create_account_shared_data_for_test(&EpochSchedule::default()),
),
];
let mut instruction_accounts = vec![
AccountMeta {
pubkey: stake_address,
is_signer: true,
is_writable: true,
},
AccountMeta {
pubkey: clock::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: authority_address,
is_signer: false,
is_writable: false,
},
];
process_instruction(
&mollusk,
&serialize(&StakeInstruction::Authorize(
authority_address,
StakeAuthorize::Staker,
))
.unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Err(ProgramError::InvalidAccountData),
);
let stake_account = AccountSharedData::new_data_with_space(
stake_lamports,
&StakeStateV2::Initialized(Meta::auto(&stake_address)),
StakeStateV2::size_of(),
&id(),
)
.unwrap();
transaction_accounts[0] = (stake_address, stake_account);
let accounts = process_instruction(
&mollusk,
&serialize(&StakeInstruction::Authorize(
authority_address,
StakeAuthorize::Staker,
))
.unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Ok(()),
);
transaction_accounts[0] = (stake_address, accounts[0].clone());
let accounts = process_instruction(
&mollusk,
&serialize(&StakeInstruction::Authorize(
authority_address,
StakeAuthorize::Withdrawer,
))
.unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Ok(()),
);
transaction_accounts[0] = (stake_address, accounts[0].clone());
if let StakeStateV2::Initialized(Meta { authorized, .. }) = from(&accounts[0]).unwrap() {
assert_eq!(authorized.staker, authority_address);
assert_eq!(authorized.withdrawer, authority_address);
} else {
panic!();
}
process_instruction(
&mollusk,
&serialize(&StakeInstruction::Authorize(
authority_address_2,
StakeAuthorize::Staker,
))
.unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Err(ProgramError::MissingRequiredSignature),
);
instruction_accounts[0].is_signer = false;
instruction_accounts[2].is_signer = true;
let accounts = process_instruction(
&mollusk,
&serialize(&StakeInstruction::Authorize(
authority_address_2,
StakeAuthorize::Staker,
))
.unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Ok(()),
);
if let StakeStateV2::Initialized(Meta { authorized, .. }) = from(&accounts[0]).unwrap() {
assert_eq!(authorized.staker, authority_address_2);
} else {
panic!();
}
let mut instruction_accounts = vec![
AccountMeta {
pubkey: stake_address,
is_signer: false,
is_writable: true,
},
AccountMeta {
pubkey: to_address,
is_signer: false,
is_writable: true,
},
AccountMeta {
pubkey: clock::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: StakeHistory::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: authority_address,
is_signer: true,
is_writable: false,
},
];
let accounts = process_instruction(
&mollusk,
&serialize(&StakeInstruction::Withdraw(stake_lamports)).unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Ok(()),
);
assert!(is_closed(&accounts[0]));
instruction_accounts[4].is_signer = false;
process_instruction(
&mollusk,
&serialize(&StakeInstruction::Withdraw(stake_lamports)).unwrap(),
transaction_accounts,
instruction_accounts,
Err(ProgramError::MissingRequiredSignature),
);
}
#[test]
fn test_authorize_override() {
let mollusk = mollusk_bpf();
let authority_address = solana_pubkey::new_rand();
let mallory_address = solana_pubkey::new_rand();
let stake_address = solana_pubkey::new_rand();
let stake_lamports = 42;
let stake_account = AccountSharedData::new_data_with_space(
stake_lamports,
&StakeStateV2::Initialized(Meta::auto(&stake_address)),
StakeStateV2::size_of(),
&id(),
)
.unwrap();
let mut transaction_accounts = vec![
(stake_address, stake_account),
(authority_address, AccountSharedData::default()),
(
clock::id(),
create_account_shared_data_for_test(&Clock::default()),
),
];
let mut instruction_accounts = vec![
AccountMeta {
pubkey: stake_address,
is_signer: true,
is_writable: true,
},
AccountMeta {
pubkey: clock::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: authority_address,
is_signer: false,
is_writable: false,
},
];
let accounts = process_instruction(
&mollusk,
&serialize(&StakeInstruction::Authorize(
authority_address,
StakeAuthorize::Staker,
))
.unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Ok(()),
);
transaction_accounts[0] = (stake_address, accounts[0].clone());
instruction_accounts[0].is_signer = false;
instruction_accounts[2].is_signer = true;
let accounts = process_instruction(
&mollusk,
&serialize(&StakeInstruction::Authorize(
mallory_address,
StakeAuthorize::Staker,
))
.unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Ok(()),
);
transaction_accounts[0] = (stake_address, accounts[0].clone());
process_instruction(
&mollusk,
&serialize(&StakeInstruction::Authorize(
authority_address,
StakeAuthorize::Staker,
))
.unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Err(ProgramError::MissingRequiredSignature),
);
instruction_accounts[0].is_signer = true;
instruction_accounts[2].is_signer = false;
let accounts = process_instruction(
&mollusk,
&serialize(&StakeInstruction::Authorize(
authority_address,
StakeAuthorize::Withdrawer,
))
.unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Ok(()),
);
transaction_accounts[0] = (stake_address, accounts[0].clone());
instruction_accounts[0].is_signer = false;
instruction_accounts[2] = AccountMeta {
pubkey: mallory_address,
is_signer: true,
is_writable: false,
};
process_instruction(
&mollusk,
&serialize(&StakeInstruction::Authorize(
authority_address,
StakeAuthorize::Withdrawer,
))
.unwrap(),
transaction_accounts,
instruction_accounts,
Err(ProgramError::MissingRequiredSignature),
);
}
#[test]
fn test_authorize_with_seed() {
let mollusk = mollusk_bpf();
let authority_base_address = solana_pubkey::new_rand();
let authority_address = solana_pubkey::new_rand();
let seed = "42";
let stake_address = Pubkey::create_with_seed(&authority_base_address, seed, &id()).unwrap();
let stake_lamports = 42;
let stake_account = AccountSharedData::new_data_with_space(
stake_lamports,
&StakeStateV2::Initialized(Meta::auto(&stake_address)),
StakeStateV2::size_of(),
&id(),
)
.unwrap();
let mut transaction_accounts = vec![
(stake_address, stake_account),
(authority_base_address, AccountSharedData::default()),
(
clock::id(),
create_account_shared_data_for_test(&Clock::default()),
),
];
let mut instruction_accounts = vec![
AccountMeta {
pubkey: stake_address,
is_signer: true,
is_writable: true,
},
AccountMeta {
pubkey: authority_base_address,
is_signer: true,
is_writable: false,
},
AccountMeta {
pubkey: clock::id(),
is_signer: false,
is_writable: false,
},
];
process_instruction(
&mollusk,
&serialize(&StakeInstruction::AuthorizeWithSeed(
AuthorizeWithSeedArgs {
new_authorized_pubkey: authority_address,
stake_authorize: StakeAuthorize::Staker,
authority_seed: "".to_string(),
authority_owner: id(),
},
))
.unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Err(ProgramError::MissingRequiredSignature),
);
instruction_accounts[1].pubkey = authority_address;
let instruction_data = serialize(&StakeInstruction::AuthorizeWithSeed(
AuthorizeWithSeedArgs {
new_authorized_pubkey: authority_address,
stake_authorize: StakeAuthorize::Staker,
authority_seed: seed.to_string(),
authority_owner: id(),
},
))
.unwrap();
process_instruction(
&mollusk,
&instruction_data,
transaction_accounts.clone(),
instruction_accounts.clone(),
Err(ProgramError::MissingRequiredSignature),
);
instruction_accounts[1].pubkey = authority_base_address;
let accounts = process_instruction(
&mollusk,
&instruction_data,
transaction_accounts.clone(),
instruction_accounts.clone(),
Ok(()),
);
transaction_accounts[0] = (stake_address, accounts[0].clone());
let instruction_data = serialize(&StakeInstruction::AuthorizeWithSeed(
AuthorizeWithSeedArgs {
new_authorized_pubkey: authority_address,
stake_authorize: StakeAuthorize::Withdrawer,
authority_seed: seed.to_string(),
authority_owner: id(),
},
))
.unwrap();
let accounts = process_instruction(
&mollusk,
&instruction_data,
transaction_accounts.clone(),
instruction_accounts.clone(),
Ok(()),
);
transaction_accounts[0] = (stake_address, accounts[0].clone());
process_instruction(
&mollusk,
&instruction_data,
transaction_accounts,
instruction_accounts,
Err(ProgramError::MissingRequiredSignature),
);
}
#[test]
fn test_authorize_delegated_stake() {
let mollusk = mollusk_bpf();
let authority_address = solana_pubkey::new_rand();
let stake_address = solana_pubkey::new_rand();
let minimum_delegation = crate::get_minimum_delegation();
let stake_account = AccountSharedData::new_data_with_space(
minimum_delegation + default_stake_rent(),
&StakeStateV2::Initialized(Meta::auto(&stake_address)),
StakeStateV2::size_of(),
&id(),
)
.unwrap();
let vote_address = solana_pubkey::new_rand();
let vote_account = create_default_vote_account();
let vote_address_2 = solana_pubkey::new_rand();
let vote_account_2 = create_default_vote_account();
#[allow(deprecated)]
let mut transaction_accounts = vec![
(stake_address, stake_account),
(vote_address, vote_account),
(vote_address_2, vote_account_2),
(
authority_address,
AccountSharedData::new(42, 0, &system_program::id()),
),
(
clock::id(),
create_account_shared_data_for_test(&Clock::default()),
),
(
StakeHistory::id(),
create_account_shared_data_for_test(&StakeHistory::default()),
),
(
stake_config::id(),
config::create_account(0, &stake_config::Config::default()),
),
(
epoch_schedule::id(),
create_account_shared_data_for_test(&EpochSchedule::default()),
),
];
#[allow(deprecated)]
let mut instruction_accounts = vec![
AccountMeta {
pubkey: stake_address,
is_signer: true,
is_writable: true,
},
AccountMeta {
pubkey: vote_address,
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: clock::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: StakeHistory::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: stake_config::id(),
is_signer: false,
is_writable: false,
},
];
let accounts = process_instruction(
&mollusk,
&serialize(&StakeInstruction::DelegateStake).unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Ok(()),
);
transaction_accounts[0] = (stake_address, accounts[0].clone());
let accounts = process_instruction(
&mollusk,
&serialize(&StakeInstruction::Deactivate).unwrap(),
transaction_accounts.clone(),
vec![
AccountMeta {
pubkey: stake_address,
is_signer: true,
is_writable: true,
},
AccountMeta {
pubkey: clock::id(),
is_signer: false,
is_writable: false,
},
],
Ok(()),
);
transaction_accounts[0] = (stake_address, accounts[0].clone());
let accounts = process_instruction(
&mollusk,
&serialize(&StakeInstruction::Authorize(
authority_address,
StakeAuthorize::Staker,
))
.unwrap(),
transaction_accounts.clone(),
vec![
AccountMeta {
pubkey: stake_address,
is_signer: true,
is_writable: true,
},
AccountMeta {
pubkey: clock::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: authority_address,
is_signer: false,
is_writable: false,
},
],
Ok(()),
);
transaction_accounts[0] = (stake_address, accounts[0].clone());
assert_eq!(
authorized_from(&accounts[0]).unwrap().staker,
authority_address
);
instruction_accounts[0].is_signer = false;
instruction_accounts[1].pubkey = vote_address_2;
process_instruction(
&mollusk,
&serialize(&StakeInstruction::DelegateStake).unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Err(ProgramError::MissingRequiredSignature),
);
instruction_accounts.push(AccountMeta {
pubkey: authority_address,
is_signer: true,
is_writable: false,
});
let accounts = process_instruction(
&mollusk,
&serialize(&StakeInstruction::DelegateStake).unwrap(),
transaction_accounts.clone(),
instruction_accounts,
Ok(()),
);
transaction_accounts[0] = (stake_address, accounts[0].clone());
assert_eq!(
stake_from(&accounts[0]).unwrap().delegation.voter_pubkey,
vote_address_2,
);
process_instruction(
&mollusk,
&serialize(&StakeInstruction::Deactivate).unwrap(),
transaction_accounts,
vec![
AccountMeta {
pubkey: stake_address,
is_signer: false,
is_writable: true,
},
AccountMeta {
pubkey: clock::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: authority_address,
is_signer: true,
is_writable: false,
},
],
Ok(()),
);
}
#[test]
fn test_stake_delegate() {
let mollusk = mollusk_bpf();
let mut vote_state = VoteStateV4::default();
for _ in 0..1000 {
increment_credits(&mut vote_state, 0, 1);
}
let vote_state_credits = vote_state.credits();
let vote_address = solana_pubkey::new_rand();
let vote_address_2 = solana_pubkey::new_rand();
let mut vote_account = create_default_vote_account();
let mut vote_account_2 = create_default_vote_account();
vote_account
.set_state(&VoteStateVersions::new_v4(vote_state.clone()))
.unwrap();
vote_account_2
.set_state(&VoteStateVersions::new_v4(vote_state))
.unwrap();
let minimum_delegation = crate::get_minimum_delegation();
let delegated_stake = minimum_delegation;
let stake_address = solana_pubkey::new_rand();
let mut stake_account = AccountSharedData::new_data_with_space(
delegated_stake + default_stake_rent(),
&StakeStateV2::Initialized(Meta {
authorized: Authorized {
staker: stake_address,
withdrawer: stake_address,
},
..Meta::default()
}),
StakeStateV2::size_of(),
&id(),
)
.unwrap();
let mut clock = Clock {
epoch: 1,
..Clock::default()
};
#[allow(deprecated)]
let mut transaction_accounts = vec![
(stake_address, stake_account.clone()),
(vote_address, vote_account),
(vote_address_2, vote_account_2.clone()),
(clock::id(), create_account_shared_data_for_test(&clock)),
(StakeHistory::id(), create_empty_stake_history_for_test()),
(
stake_config::id(),
config::create_account(0, &stake_config::Config::default()),
),
(
epoch_schedule::id(),
create_account_shared_data_for_test(&EpochSchedule::default()),
),
];
#[allow(deprecated)]
let mut instruction_accounts = vec![
AccountMeta {
pubkey: stake_address,
is_signer: true,
is_writable: true,
},
AccountMeta {
pubkey: vote_address,
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: clock::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: StakeHistory::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: stake_config::id(),
is_signer: false,
is_writable: false,
},
];
instruction_accounts[0].is_signer = false;
process_instruction(
&mollusk,
&serialize(&StakeInstruction::DelegateStake).unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Err(ProgramError::MissingRequiredSignature),
);
instruction_accounts[0].is_signer = true;
let accounts = process_instruction(
&mollusk,
&serialize(&StakeInstruction::DelegateStake).unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Ok(()),
);
assert_eq!(
stake_from(&accounts[0]).unwrap(),
Stake {
delegation: Delegation {
voter_pubkey: vote_address,
stake: delegated_stake,
activation_epoch: clock.epoch,
deactivation_epoch: u64::MAX,
..Delegation::default()
},
credits_observed: vote_state_credits,
}
);
clock.epoch += 1;
transaction_accounts[0] = (stake_address, accounts[0].clone());
transaction_accounts[3] = (clock::id(), create_account_shared_data_for_test(&clock));
process_instruction(
&mollusk,
&serialize(&StakeInstruction::DelegateStake).unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Err(StakeError::TooSoonToRedelegate.into()),
);
let accounts = process_instruction(
&mollusk,
&serialize(&StakeInstruction::Deactivate).unwrap(),
transaction_accounts.clone(),
vec![
AccountMeta {
pubkey: stake_address,
is_signer: true,
is_writable: true,
},
AccountMeta {
pubkey: clock::id(),
is_signer: false,
is_writable: false,
},
],
Ok(()),
);
transaction_accounts[0] = (stake_address, accounts[0].clone());
instruction_accounts[1].pubkey = vote_address_2;
process_instruction(
&mollusk,
&serialize(&StakeInstruction::DelegateStake).unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Err(StakeError::TooSoonToRedelegate.into()),
);
instruction_accounts[1].pubkey = vote_address;
let accounts_2 = process_instruction(
&mollusk,
&serialize(&StakeInstruction::DelegateStake).unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Ok(()),
);
let stake = stake_from(&accounts_2[0]).unwrap();
assert_eq!(stake.delegation.deactivation_epoch, u64::MAX);
transaction_accounts[0] = (stake_address, accounts_2[0].clone());
instruction_accounts[1].pubkey = vote_address_2;
process_instruction(
&mollusk,
&serialize(&StakeInstruction::DelegateStake).unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Err(StakeError::TooSoonToRedelegate.into()),
);
clock.epoch += 1;
transaction_accounts[3] = (clock::id(), create_account_shared_data_for_test(&clock));
transaction_accounts[0] = (stake_address, accounts[0].clone());
let accounts = process_instruction(
&mollusk,
&serialize(&StakeInstruction::DelegateStake).unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Ok(()),
);
instruction_accounts[1].pubkey = vote_address;
assert_eq!(
stake_from(&accounts[0]).unwrap(),
Stake {
delegation: Delegation {
voter_pubkey: vote_address_2,
stake: delegated_stake,
activation_epoch: clock.epoch,
deactivation_epoch: u64::MAX,
..Delegation::default()
},
credits_observed: vote_state_credits,
}
);
transaction_accounts[1] = (vote_address_2, vote_account_2);
transaction_accounts[1]
.1
.set_owner(solana_pubkey::new_rand());
process_instruction(
&mollusk,
&serialize(&StakeInstruction::DelegateStake).unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Err(ProgramError::IncorrectProgramId),
);
let stake_state = StakeStateV2::RewardsPool;
stake_account.set_state(&stake_state).unwrap();
transaction_accounts[0] = (stake_address, stake_account);
process_instruction(
&mollusk,
&serialize(&StakeInstruction::DelegateStake).unwrap(),
transaction_accounts,
instruction_accounts,
Err(ProgramError::IncorrectProgramId),
);
}
#[test]
fn test_redelegate_consider_balance_changes() {
let mollusk = mollusk_bpf();
let mut clock = Clock::default();
let rent = Rent::default();
let rent_exempt_reserve = rent.minimum_balance(StakeStateV2::size_of());
let initial_lamports = 4242424242;
let stake_lamports = rent_exempt_reserve + initial_lamports;
let recipient_address = solana_pubkey::new_rand();
let authority_address = solana_pubkey::new_rand();
let vote_address = solana_pubkey::new_rand();
let vote_account = create_default_vote_account();
let stake_address = solana_pubkey::new_rand();
let stake_account = AccountSharedData::new_data_with_space(
stake_lamports,
&StakeStateV2::Initialized(Meta {
rent_exempt_reserve,
..Meta::auto(&authority_address)
}),
StakeStateV2::size_of(),
&id(),
)
.unwrap();
#[allow(deprecated)]
let mut transaction_accounts = vec![
(stake_address, stake_account),
(vote_address, vote_account),
(
recipient_address,
AccountSharedData::new(1, 0, &system_program::id()),
),
(authority_address, AccountSharedData::default()),
(clock::id(), create_account_shared_data_for_test(&clock)),
(StakeHistory::id(), create_empty_stake_history_for_test()),
(
stake_config::id(),
config::create_account(0, &stake_config::Config::default()),
),
(
epoch_schedule::id(),
create_account_shared_data_for_test(&EpochSchedule::default()),
),
];
#[allow(deprecated)]
let delegate_instruction_accounts = vec![
AccountMeta {
pubkey: stake_address,
is_signer: false,
is_writable: true,
},
AccountMeta {
pubkey: vote_address,
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: clock::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: StakeHistory::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: stake_config::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: authority_address,
is_signer: true,
is_writable: false,
},
];
let deactivate_instruction_accounts = vec![
AccountMeta {
pubkey: stake_address,
is_signer: false,
is_writable: true,
},
AccountMeta {
pubkey: clock::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: authority_address,
is_signer: true,
is_writable: false,
},
];
let accounts = process_instruction(
&mollusk,
&serialize(&StakeInstruction::DelegateStake).unwrap(),
transaction_accounts.clone(),
delegate_instruction_accounts.clone(),
Ok(()),
);
transaction_accounts[0] = (stake_address, accounts[0].clone());
clock.epoch += 1;
transaction_accounts[2] = (clock::id(), create_account_shared_data_for_test(&clock));
let accounts = process_instruction(
&mollusk,
&serialize(&StakeInstruction::Deactivate).unwrap(),
transaction_accounts.clone(),
deactivate_instruction_accounts.clone(),
Ok(()),
);
transaction_accounts[0] = (stake_address, accounts[0].clone());
clock.epoch += 1;
transaction_accounts[2] = (clock::id(), create_account_shared_data_for_test(&clock));
let withdraw_lamports = initial_lamports / 2;
let accounts = process_instruction(
&mollusk,
&serialize(&StakeInstruction::Withdraw(withdraw_lamports)).unwrap(),
transaction_accounts.clone(),
vec![
AccountMeta {
pubkey: stake_address,
is_signer: false,
is_writable: true,
},
AccountMeta {
pubkey: recipient_address,
is_signer: false,
is_writable: true,
},
AccountMeta {
pubkey: clock::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: StakeHistory::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: authority_address,
is_signer: true,
is_writable: false,
},
],
Ok(()),
);
let expected_balance = rent_exempt_reserve + initial_lamports - withdraw_lamports;
assert_eq!(accounts[0].lamports(), expected_balance);
transaction_accounts[0] = (stake_address, accounts[0].clone());
clock.epoch += 1;
transaction_accounts[2] = (clock::id(), create_account_shared_data_for_test(&clock));
let accounts = process_instruction(
&mollusk,
&serialize(&StakeInstruction::DelegateStake).unwrap(),
transaction_accounts.clone(),
delegate_instruction_accounts.clone(),
Ok(()),
);
assert_eq!(
stake_from(&accounts[0]).unwrap().delegation.stake,
accounts[0].lamports() - rent_exempt_reserve,
);
transaction_accounts[0] = (stake_address, accounts[0].clone());
clock.epoch += 1;
transaction_accounts[2] = (clock::id(), create_account_shared_data_for_test(&clock));
let accounts = process_instruction(
&mollusk,
&serialize(&StakeInstruction::Deactivate).unwrap(),
transaction_accounts.clone(),
deactivate_instruction_accounts,
Ok(()),
);
transaction_accounts[0] = (stake_address, accounts[0].clone());
transaction_accounts[0]
.1
.checked_add_lamports(withdraw_lamports)
.unwrap();
clock.epoch += 1;
transaction_accounts[2] = (clock::id(), create_account_shared_data_for_test(&clock));
let accounts = process_instruction(
&mollusk,
&serialize(&StakeInstruction::DelegateStake).unwrap(),
transaction_accounts,
delegate_instruction_accounts,
Ok(()),
);
assert_eq!(
stake_from(&accounts[0]).unwrap().delegation.stake,
accounts[0].lamports() - rent_exempt_reserve,
);
}
#[test]
fn test_split() {
let mollusk = mollusk_bpf();
let stake_history = StakeHistory::default();
let current_epoch = 100;
let clock = Clock {
epoch: current_epoch,
..Clock::default()
};
let stake_address = solana_pubkey::new_rand();
let minimum_delegation = crate::get_minimum_delegation();
let delegated_stake = minimum_delegation * 2;
let total_lamports = delegated_stake + default_stake_rent();
let split_to_address = solana_pubkey::new_rand();
let split_to_account = AccountSharedData::new_data_with_space(
default_stake_rent(),
&StakeStateV2::Uninitialized,
StakeStateV2::size_of(),
&id(),
)
.unwrap();
let mut transaction_accounts = vec![
(stake_address, AccountSharedData::default()),
(split_to_address, split_to_account.clone()),
(
rent::id(),
create_account_shared_data_for_test(&Rent::default()),
),
(
StakeHistory::id(),
create_account_shared_data_for_test(&stake_history),
),
(clock::id(), create_account_shared_data_for_test(&clock)),
(
epoch_schedule::id(),
create_account_shared_data_for_test(&EpochSchedule::default()),
),
];
let instruction_accounts = vec![
AccountMeta {
pubkey: stake_address,
is_signer: true,
is_writable: true,
},
AccountMeta {
pubkey: split_to_address,
is_signer: false,
is_writable: true,
},
];
let meta = Meta {
rent_exempt_reserve: default_stake_rent(),
..Meta::auto(&stake_address)
};
for (state, expected_error) in [
(
StakeStateV2::Initialized(meta),
ProgramError::InsufficientFunds,
),
(
just_stake(meta, delegated_stake),
StakeError::InsufficientDelegation.into(),
),
] {
let stake_account = AccountSharedData::new_data_with_space(
total_lamports,
&state,
StakeStateV2::size_of(),
&id(),
)
.unwrap();
let expected_active_stake = get_active_stake_for_tests(
&[stake_account.clone(), split_to_account.clone()],
&clock,
&stake_history,
);
transaction_accounts[0] = (stake_address, stake_account);
process_instruction(
&mollusk,
&serialize(&StakeInstruction::Split(delegated_stake + 1)).unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Err(expected_error),
);
let accounts = process_instruction(
&mollusk,
&serialize(&StakeInstruction::Split(delegated_stake / 2)).unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Ok(()),
);
assert_eq!(
accounts[0].lamports() + accounts[1].lamports(),
delegated_stake + default_stake_rent() * 2,
);
assert_eq!(
expected_active_stake,
get_active_stake_for_tests(&accounts[0..2], &clock, &stake_history)
);
assert_eq!(from(&accounts[0]).unwrap(), from(&accounts[1]).unwrap());
match state {
StakeStateV2::Initialized(_meta) => {
assert_eq!(from(&accounts[0]).unwrap(), state);
}
StakeStateV2::Stake(_meta, _stake, _) => {
let stake_0 = from(&accounts[0]).unwrap().stake();
assert_eq!(stake_0.unwrap().delegation.stake, delegated_stake / 2);
}
_ => unreachable!(),
}
}
let split_to_account = AccountSharedData::new_data_with_space(
default_stake_rent(),
&StakeStateV2::Uninitialized,
StakeStateV2::size_of(),
&solana_pubkey::new_rand(),
)
.unwrap();
transaction_accounts[1] = (split_to_address, split_to_account);
process_instruction(
&mollusk,
&serialize(&StakeInstruction::Split(delegated_stake / 2)).unwrap(),
transaction_accounts,
instruction_accounts,
Err(ProgramError::InvalidAccountOwner),
);
}
#[test]
fn test_withdraw_stake() {
let mollusk = mollusk_bpf();
let recipient_address = solana_pubkey::new_rand();
let authority_address = solana_pubkey::new_rand();
let custodian_address = solana_pubkey::new_rand();
let stake_address = solana_pubkey::new_rand();
let minimum_delegation = crate::get_minimum_delegation();
let stake_lamports = minimum_delegation;
let stake_account = AccountSharedData::new_data_with_space(
stake_lamports,
&StakeStateV2::Uninitialized,
StakeStateV2::size_of(),
&id(),
)
.unwrap();
let vote_address = solana_pubkey::new_rand();
let vote_account = create_default_vote_account();
#[allow(deprecated)]
let mut transaction_accounts = vec![
(stake_address, stake_account),
(vote_address, vote_account),
(recipient_address, AccountSharedData::default()),
(
authority_address,
AccountSharedData::new(42, 0, &system_program::id()),
),
(custodian_address, AccountSharedData::default()),
(
clock::id(),
create_account_shared_data_for_test(&Clock::default()),
),
(
rent::id(),
create_account_shared_data_for_test(&Rent::free()),
),
(
StakeHistory::id(),
create_account_shared_data_for_test(&StakeHistory::default()),
),
(
stake_config::id(),
config::create_account(0, &stake_config::Config::default()),
),
(
epoch_schedule::id(),
create_account_shared_data_for_test(&EpochSchedule::default()),
),
];
let mut instruction_accounts = vec![
AccountMeta {
pubkey: stake_address,
is_signer: false,
is_writable: true,
},
AccountMeta {
pubkey: recipient_address,
is_signer: false,
is_writable: true,
},
AccountMeta {
pubkey: clock::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: StakeHistory::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: stake_address,
is_signer: true,
is_writable: false,
},
];
instruction_accounts[4].is_signer = false;
process_instruction(
&mollusk,
&serialize(&StakeInstruction::Withdraw(stake_lamports)).unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Err(ProgramError::MissingRequiredSignature),
);
instruction_accounts[4].is_signer = true;
instruction_accounts[1].pubkey = stake_address;
process_instruction(
&mollusk,
&serialize(&StakeInstruction::Withdraw(stake_lamports)).unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Err(ProgramError::InvalidArgument),
);
instruction_accounts[1].pubkey = recipient_address;
let accounts = process_instruction(
&mollusk,
&serialize(&StakeInstruction::Withdraw(stake_lamports)).unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Ok(()),
);
assert_eq!(accounts[0].lamports(), 0);
assert!(is_closed(&accounts[0]));
let lockup = Lockup {
unix_timestamp: 0,
epoch: 0,
custodian: custodian_address,
};
let accounts = process_instruction(
&mollusk,
&serialize(&StakeInstruction::Initialize(
Authorized::auto(&stake_address),
lockup,
))
.unwrap(),
transaction_accounts.clone(),
vec![
AccountMeta {
pubkey: stake_address,
is_signer: true,
is_writable: true,
},
AccountMeta {
pubkey: rent::id(),
is_signer: false,
is_writable: false,
},
],
Ok(()),
);
transaction_accounts[0] = (stake_address, accounts[0].clone());
process_instruction(
&mollusk,
&serialize(&StakeInstruction::Withdraw(stake_lamports + 1)).unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Err(ProgramError::InsufficientFunds),
);
#[allow(deprecated)]
let accounts = process_instruction(
&mollusk,
&serialize(&StakeInstruction::DelegateStake).unwrap(),
transaction_accounts.clone(),
vec![
AccountMeta {
pubkey: stake_address,
is_signer: true,
is_writable: true,
},
AccountMeta {
pubkey: vote_address,
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: clock::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: StakeHistory::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: stake_config::id(),
is_signer: false,
is_writable: false,
},
],
Ok(()),
);
transaction_accounts[0] = (stake_address, accounts[0].clone());
transaction_accounts[0].1.checked_add_lamports(10).unwrap();
process_instruction(
&mollusk,
&serialize(&StakeInstruction::Withdraw(10)).unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Ok(()),
);
process_instruction(
&mollusk,
&serialize(&StakeInstruction::Withdraw(11)).unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Err(ProgramError::InsufficientFunds),
);
let accounts = process_instruction(
&mollusk,
&serialize(&StakeInstruction::Deactivate).unwrap(),
transaction_accounts.clone(),
vec![
AccountMeta {
pubkey: stake_address,
is_signer: true,
is_writable: true,
},
AccountMeta {
pubkey: clock::id(),
is_signer: false,
is_writable: false,
},
],
Ok(()),
);
transaction_accounts[0] = (stake_address, accounts[0].clone());
let clock = Clock {
epoch: 100,
..Clock::default()
};
transaction_accounts[5] = (clock::id(), create_account_shared_data_for_test(&clock));
process_instruction(
&mollusk,
&serialize(&StakeInstruction::Withdraw(stake_lamports + 11)).unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Err(ProgramError::InsufficientFunds),
);
let accounts = process_instruction(
&mollusk,
&serialize(&StakeInstruction::Withdraw(stake_lamports + 10)).unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Ok(()),
);
assert_eq!(accounts[0].lamports(), 0);
assert!(is_closed(&accounts[0]));
let rent = Rent::default();
let rent_exempt_reserve = rent.minimum_balance(StakeStateV2::size_of());
let stake_account = AccountSharedData::new_data_with_space(
1_000_000_000,
&StakeStateV2::Initialized(Meta {
rent_exempt_reserve,
authorized: Authorized {
staker: authority_address,
withdrawer: authority_address,
},
lockup: Lockup::default(),
}),
StakeStateV2::size_of(),
&id(),
)
.unwrap();
transaction_accounts[0] = (stake_address, stake_account.clone());
transaction_accounts[2] = (recipient_address, stake_account);
instruction_accounts[4].pubkey = authority_address;
process_instruction(
&mollusk,
&serialize(&StakeInstruction::Withdraw(u64::MAX - 10)).unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Err(ProgramError::InsufficientFunds),
);
let stake_account = AccountSharedData::new_data_with_space(
stake_lamports,
&StakeStateV2::RewardsPool,
StakeStateV2::size_of(),
&id(),
)
.unwrap();
transaction_accounts[0] = (stake_address, stake_account);
process_instruction(
&mollusk,
&serialize(&StakeInstruction::Withdraw(stake_lamports)).unwrap(),
transaction_accounts,
instruction_accounts,
Err(ProgramError::InvalidAccountData),
);
}
#[test]
fn test_withdraw_stake_before_warmup() {
let mollusk = mollusk_bpf();
let recipient_address = solana_pubkey::new_rand();
let stake_address = solana_pubkey::new_rand();
let minimum_delegation = crate::get_minimum_delegation();
let delegated_stake = minimum_delegation;
let total_lamports = delegated_stake + default_stake_rent() + 33;
let stake_account = AccountSharedData::new_data_with_space(
total_lamports,
&StakeStateV2::Initialized(Meta::auto(&stake_address)),
StakeStateV2::size_of(),
&id(),
)
.unwrap();
let vote_address = solana_pubkey::new_rand();
let vote_account = create_default_vote_account();
let mut clock = Clock {
epoch: 16,
..Clock::default()
};
#[allow(deprecated)]
let mut transaction_accounts = vec![
(stake_address, stake_account),
(vote_address, vote_account),
(recipient_address, AccountSharedData::default()),
(clock::id(), create_account_shared_data_for_test(&clock)),
(
StakeHistory::id(),
create_account_shared_data_for_test(&StakeHistory::default()),
),
(
stake_config::id(),
config::create_account(0, &stake_config::Config::default()),
),
(
epoch_schedule::id(),
create_account_shared_data_for_test(&EpochSchedule::default()),
),
];
let instruction_accounts = vec![
AccountMeta {
pubkey: stake_address,
is_signer: false,
is_writable: true,
},
AccountMeta {
pubkey: recipient_address,
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: clock::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: StakeHistory::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: stake_address,
is_signer: true,
is_writable: false,
},
];
#[allow(deprecated)]
let accounts = process_instruction(
&mollusk,
&serialize(&StakeInstruction::DelegateStake).unwrap(),
transaction_accounts.clone(),
vec![
AccountMeta {
pubkey: stake_address,
is_signer: true,
is_writable: true,
},
AccountMeta {
pubkey: vote_address,
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: clock::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: StakeHistory::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: stake_config::id(),
is_signer: false,
is_writable: false,
},
],
Ok(()),
);
transaction_accounts[0] = (stake_address, accounts[0].clone());
let stake_history = create_stake_history_from_delegations(
None,
0..clock.epoch,
&[stake_from(&accounts[0]).unwrap().delegation],
None,
);
transaction_accounts[4] = (
StakeHistory::id(),
create_account_shared_data_for_test(&stake_history),
);
clock.epoch = 0;
transaction_accounts[3] = (clock::id(), create_account_shared_data_for_test(&clock));
process_instruction(
&mollusk,
&serialize(&StakeInstruction::Withdraw(
total_lamports - delegated_stake + 1,
))
.unwrap(),
transaction_accounts,
instruction_accounts,
Err(ProgramError::InsufficientFunds),
);
}
#[test]
fn test_withdraw_lockup() {
let mollusk = mollusk_bpf();
let recipient_address = solana_pubkey::new_rand();
let custodian_address = solana_pubkey::new_rand();
let stake_address = solana_pubkey::new_rand();
let total_lamports = 100;
let mut meta = Meta {
lockup: Lockup {
unix_timestamp: 0,
epoch: 1,
custodian: custodian_address,
},
..Meta::auto(&stake_address)
};
let stake_account = AccountSharedData::new_data_with_space(
total_lamports,
&StakeStateV2::Initialized(meta),
StakeStateV2::size_of(),
&id(),
)
.unwrap();
let mut clock = Clock::default();
let mut transaction_accounts = vec![
(stake_address, stake_account.clone()),
(recipient_address, AccountSharedData::default()),
(custodian_address, AccountSharedData::default()),
(clock::id(), create_account_shared_data_for_test(&clock)),
(
StakeHistory::id(),
create_account_shared_data_for_test(&StakeHistory::default()),
),
(
epoch_schedule::id(),
create_account_shared_data_for_test(&EpochSchedule::default()),
),
];
let mut instruction_accounts = vec![
AccountMeta {
pubkey: stake_address,
is_signer: false,
is_writable: true,
},
AccountMeta {
pubkey: recipient_address,
is_signer: false,
is_writable: true,
},
AccountMeta {
pubkey: clock::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: StakeHistory::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: stake_address,
is_signer: true,
is_writable: false,
},
];
process_instruction(
&mollusk,
&serialize(&StakeInstruction::Withdraw(total_lamports)).unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Err(StakeError::LockupInForce.into()),
);
instruction_accounts.push(AccountMeta {
pubkey: custodian_address,
is_signer: true,
is_writable: false,
});
let accounts = process_instruction(
&mollusk,
&serialize(&StakeInstruction::Withdraw(total_lamports)).unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Ok(()),
);
assert!(is_closed(&accounts[0]));
instruction_accounts[5].pubkey = stake_address;
meta.lockup.custodian = stake_address;
let stake_account_self_as_custodian = AccountSharedData::new_data_with_space(
total_lamports,
&StakeStateV2::Initialized(meta),
StakeStateV2::size_of(),
&id(),
)
.unwrap();
transaction_accounts[0] = (stake_address, stake_account_self_as_custodian);
let accounts = process_instruction(
&mollusk,
&serialize(&StakeInstruction::Withdraw(total_lamports)).unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Ok(()),
);
assert!(is_closed(&accounts[0]));
transaction_accounts[0] = (stake_address, stake_account);
instruction_accounts.pop();
clock.epoch += 1;
transaction_accounts[3] = (clock::id(), create_account_shared_data_for_test(&clock));
let accounts = process_instruction(
&mollusk,
&serialize(&StakeInstruction::Withdraw(total_lamports)).unwrap(),
transaction_accounts,
instruction_accounts,
Ok(()),
);
assert!(is_closed(&accounts[0]));
}
#[test]
fn test_withdraw_rent_exempt() {
let mollusk = mollusk_bpf();
let recipient_address = solana_pubkey::new_rand();
let custodian_address = solana_pubkey::new_rand();
let stake_address = solana_pubkey::new_rand();
let rent = Rent::default();
let rent_exempt_reserve = rent.minimum_balance(StakeStateV2::size_of());
let minimum_delegation = crate::get_minimum_delegation();
let stake_lamports = 7 * minimum_delegation;
let stake_account = AccountSharedData::new_data_with_space(
stake_lamports + rent_exempt_reserve,
&StakeStateV2::Initialized(Meta {
rent_exempt_reserve,
..Meta::auto(&stake_address)
}),
StakeStateV2::size_of(),
&id(),
)
.unwrap();
let transaction_accounts = vec![
(stake_address, stake_account),
(recipient_address, AccountSharedData::default()),
(custodian_address, AccountSharedData::default()),
(
clock::id(),
create_account_shared_data_for_test(&Clock::default()),
),
(
StakeHistory::id(),
create_account_shared_data_for_test(&StakeHistory::default()),
),
(
epoch_schedule::id(),
create_account_shared_data_for_test(&EpochSchedule::default()),
),
];
let instruction_accounts = vec![
AccountMeta {
pubkey: stake_address,
is_signer: false,
is_writable: true,
},
AccountMeta {
pubkey: recipient_address,
is_signer: false,
is_writable: true,
},
AccountMeta {
pubkey: clock::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: StakeHistory::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: stake_address,
is_signer: true,
is_writable: false,
},
];
process_instruction(
&mollusk,
&serialize(&StakeInstruction::Withdraw(stake_lamports)).unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Ok(()),
);
process_instruction(
&mollusk,
&serialize(&StakeInstruction::Withdraw(stake_lamports + 1)).unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Err(ProgramError::InsufficientFunds),
);
process_instruction(
&mollusk,
&serialize(&StakeInstruction::Withdraw(
stake_lamports + rent_exempt_reserve,
))
.unwrap(),
transaction_accounts,
instruction_accounts,
Ok(()),
);
}
#[test]
fn test_deactivate() {
let mollusk = mollusk_bpf();
let stake_address = solana_pubkey::new_rand();
let minimum_delegation = crate::get_minimum_delegation();
let stake_account = AccountSharedData::new_data_with_space(
minimum_delegation + default_stake_rent(),
&StakeStateV2::Initialized(Meta::auto(&stake_address)),
StakeStateV2::size_of(),
&id(),
)
.unwrap();
let vote_address = solana_pubkey::new_rand();
let vote_account = create_default_vote_account();
#[allow(deprecated)]
let mut transaction_accounts = vec![
(stake_address, stake_account),
(vote_address, vote_account),
(
clock::id(),
create_account_shared_data_for_test(&Clock::default()),
),
(
StakeHistory::id(),
create_account_shared_data_for_test(&StakeHistory::default()),
),
(
stake_config::id(),
config::create_account(0, &stake_config::Config::default()),
),
(
epoch_schedule::id(),
create_account_shared_data_for_test(&EpochSchedule::default()),
),
];
let mut instruction_accounts = vec![
AccountMeta {
pubkey: stake_address,
is_signer: true,
is_writable: true,
},
AccountMeta {
pubkey: clock::id(),
is_signer: false,
is_writable: false,
},
];
instruction_accounts[0].is_signer = false;
process_instruction(
&mollusk,
&serialize(&StakeInstruction::Deactivate).unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Err(ProgramError::InvalidAccountData),
);
instruction_accounts[0].is_signer = true;
process_instruction(
&mollusk,
&serialize(&StakeInstruction::Deactivate).unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Err(ProgramError::InvalidAccountData),
);
#[allow(deprecated)]
let accounts = process_instruction(
&mollusk,
&serialize(&StakeInstruction::DelegateStake).unwrap(),
transaction_accounts.clone(),
vec![
AccountMeta {
pubkey: stake_address,
is_signer: true,
is_writable: true,
},
AccountMeta {
pubkey: vote_address,
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: clock::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: StakeHistory::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: stake_config::id(),
is_signer: false,
is_writable: false,
},
],
Ok(()),
);
transaction_accounts[0] = (stake_address, accounts[0].clone());
let accounts = process_instruction(
&mollusk,
&serialize(&StakeInstruction::Deactivate).unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Ok(()),
);
transaction_accounts[0] = (stake_address, accounts[0].clone());
process_instruction(
&mollusk,
&serialize(&StakeInstruction::Deactivate).unwrap(),
transaction_accounts,
instruction_accounts,
Err(StakeError::AlreadyDeactivated.into()),
);
}
#[test]
fn test_set_lockup() {
let mollusk = mollusk_bpf();
let custodian_address = solana_pubkey::new_rand();
let authorized_address = solana_pubkey::new_rand();
let stake_address = solana_pubkey::new_rand();
let minimum_delegation = crate::get_minimum_delegation();
let stake_lamports = minimum_delegation;
let stake_account = AccountSharedData::new_data_with_space(
stake_lamports,
&StakeStateV2::Uninitialized,
StakeStateV2::size_of(),
&id(),
)
.unwrap();
let vote_address = solana_pubkey::new_rand();
let vote_account = create_default_vote_account();
let instruction_data = serialize(&StakeInstruction::SetLockup(LockupArgs {
unix_timestamp: Some(1),
epoch: Some(1),
custodian: Some(custodian_address),
}))
.unwrap();
#[allow(deprecated)]
let mut transaction_accounts = vec![
(stake_address, stake_account),
(vote_address, vote_account),
(authorized_address, AccountSharedData::default()),
(custodian_address, AccountSharedData::default()),
(
clock::id(),
create_account_shared_data_for_test(&Clock::default()),
),
(
rent::id(),
create_account_shared_data_for_test(&Rent::free()),
),
(
StakeHistory::id(),
create_account_shared_data_for_test(&StakeHistory::default()),
),
(
stake_config::id(),
config::create_account(0, &stake_config::Config::default()),
),
(
epoch_schedule::id(),
create_account_shared_data_for_test(&EpochSchedule::default()),
),
];
let mut instruction_accounts = vec![
AccountMeta {
pubkey: stake_address,
is_signer: false,
is_writable: true,
},
AccountMeta {
pubkey: clock::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: custodian_address,
is_signer: true,
is_writable: false,
},
];
process_instruction(
&mollusk,
&instruction_data,
transaction_accounts.clone(),
instruction_accounts.clone(),
Err(ProgramError::InvalidAccountData),
);
let lockup = Lockup {
unix_timestamp: 1,
epoch: 1,
custodian: custodian_address,
};
let accounts = process_instruction(
&mollusk,
&serialize(&StakeInstruction::Initialize(
Authorized::auto(&stake_address),
lockup,
))
.unwrap(),
transaction_accounts.clone(),
vec![
AccountMeta {
pubkey: stake_address,
is_signer: true,
is_writable: true,
},
AccountMeta {
pubkey: rent::id(),
is_signer: false,
is_writable: false,
},
],
Ok(()),
);
transaction_accounts[0] = (stake_address, accounts[0].clone());
instruction_accounts[2].is_signer = false;
process_instruction(
&mollusk,
&instruction_data,
transaction_accounts.clone(),
instruction_accounts.clone(),
Err(ProgramError::MissingRequiredSignature),
);
instruction_accounts[2].is_signer = true;
process_instruction(
&mollusk,
&instruction_data,
transaction_accounts.clone(),
instruction_accounts.clone(),
Ok(()),
);
#[allow(deprecated)]
let accounts = process_instruction(
&mollusk,
&serialize(&StakeInstruction::DelegateStake).unwrap(),
transaction_accounts.clone(),
vec![
AccountMeta {
pubkey: stake_address,
is_signer: true,
is_writable: true,
},
AccountMeta {
pubkey: vote_address,
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: clock::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: StakeHistory::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: stake_config::id(),
is_signer: false,
is_writable: false,
},
],
Ok(()),
);
transaction_accounts[0] = (stake_address, accounts[0].clone());
instruction_accounts[2].is_signer = false;
process_instruction(
&mollusk,
&instruction_data,
transaction_accounts.clone(),
instruction_accounts.clone(),
Err(ProgramError::MissingRequiredSignature),
);
instruction_accounts[2].is_signer = true;
process_instruction(
&mollusk,
&instruction_data,
transaction_accounts.clone(),
instruction_accounts.clone(),
Ok(()),
);
let instruction_data = serialize(&StakeInstruction::SetLockup(LockupArgs {
unix_timestamp: Some(2),
epoch: None,
custodian: None,
}))
.unwrap();
instruction_accounts[0].is_signer = true;
instruction_accounts[2].is_signer = false;
process_instruction(
&mollusk,
&instruction_data,
transaction_accounts.clone(),
instruction_accounts.clone(),
Err(ProgramError::MissingRequiredSignature),
);
instruction_accounts[0].is_signer = false;
instruction_accounts[2].is_signer = true;
process_instruction(
&mollusk,
&instruction_data,
transaction_accounts.clone(),
instruction_accounts.clone(),
Ok(()),
);
let clock = Clock {
unix_timestamp: i64::MAX,
epoch: Epoch::MAX,
..Clock::default()
};
transaction_accounts[4] = (clock::id(), create_account_shared_data_for_test(&clock));
process_instruction(
&mollusk,
&instruction_data,
transaction_accounts.clone(),
instruction_accounts.clone(),
Err(ProgramError::MissingRequiredSignature),
);
instruction_accounts[0].is_signer = true;
instruction_accounts[2].is_signer = false;
process_instruction(
&mollusk,
&instruction_data,
transaction_accounts.clone(),
instruction_accounts.clone(),
Ok(()),
);
let accounts = process_instruction(
&mollusk,
&serialize(&StakeInstruction::Authorize(
authorized_address,
StakeAuthorize::Withdrawer,
))
.unwrap(),
transaction_accounts.clone(),
vec![
AccountMeta {
pubkey: stake_address,
is_signer: true,
is_writable: true,
},
AccountMeta {
pubkey: clock::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: authorized_address,
is_signer: false,
is_writable: false,
},
],
Ok(()),
);
transaction_accounts[0] = (stake_address, accounts[0].clone());
process_instruction(
&mollusk,
&instruction_data,
transaction_accounts,
instruction_accounts,
Err(ProgramError::MissingRequiredSignature),
);
}
#[test]
fn test_initialize_minimum_balance() {
let mollusk = mollusk_bpf();
let rent = Rent::default();
let rent_exempt_reserve = rent.minimum_balance(StakeStateV2::size_of());
let stake_address = solana_pubkey::new_rand();
let instruction_data = serialize(&StakeInstruction::Initialize(
Authorized::auto(&stake_address),
Lockup::default(),
))
.unwrap();
let instruction_accounts = vec![
AccountMeta {
pubkey: stake_address,
is_signer: false,
is_writable: true,
},
AccountMeta {
pubkey: rent::id(),
is_signer: false,
is_writable: false,
},
];
for (lamports, expected_result) in [
(rent_exempt_reserve, Ok(())),
(
rent_exempt_reserve - 1,
Err(ProgramError::InsufficientFunds),
),
] {
let stake_account = AccountSharedData::new(lamports, StakeStateV2::size_of(), &id());
process_instruction(
&mollusk,
&instruction_data,
vec![
(stake_address, stake_account),
(rent::id(), create_account_shared_data_for_test(&rent)),
],
instruction_accounts.clone(),
expected_result,
);
}
}
#[test]
fn test_delegate_minimum_stake_delegation() {
let mollusk = mollusk_bpf();
let minimum_delegation = crate::get_minimum_delegation();
let rent = Rent::default();
let rent_exempt_reserve = rent.minimum_balance(StakeStateV2::size_of());
let stake_address = solana_pubkey::new_rand();
let meta = Meta {
rent_exempt_reserve,
..Meta::auto(&stake_address)
};
let vote_address = solana_pubkey::new_rand();
let vote_account = create_default_vote_account();
#[allow(deprecated)]
let instruction_accounts = vec![
AccountMeta {
pubkey: stake_address,
is_signer: true,
is_writable: true,
},
AccountMeta {
pubkey: vote_address,
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: clock::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: StakeHistory::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: stake_config::id(),
is_signer: false,
is_writable: false,
},
];
for (stake_delegation, expected_result) in &[
(minimum_delegation, Ok(())),
(
minimum_delegation - 1,
Err(StakeError::InsufficientDelegation),
),
] {
for stake_state in &[
StakeStateV2::Initialized(meta),
just_stake(meta, *stake_delegation),
] {
let stake_account = AccountSharedData::new_data_with_space(
stake_delegation + rent_exempt_reserve,
stake_state,
StakeStateV2::size_of(),
&id(),
)
.unwrap();
#[allow(deprecated)]
process_instruction(
&mollusk,
&serialize(&StakeInstruction::DelegateStake).unwrap(),
vec![
(stake_address, stake_account),
(vote_address, vote_account.clone()),
(
clock::id(),
create_account_shared_data_for_test(&Clock::default()),
),
(
StakeHistory::id(),
create_account_shared_data_for_test(&StakeHistory::default()),
),
(
stake_config::id(),
config::create_account(0, &stake_config::Config::default()),
),
(
epoch_schedule::id(),
create_account_shared_data_for_test(&EpochSchedule::default()),
),
],
instruction_accounts.clone(),
expected_result.clone().map_err(|e| e.into()),
);
}
}
}
#[test]
fn test_split_minimum_stake_delegation() {
let mollusk = mollusk_bpf();
let minimum_delegation = crate::get_minimum_delegation();
let rent = Rent::default();
let rent_exempt_reserve = rent.minimum_balance(StakeStateV2::size_of());
let stake_history = StakeHistory::default();
let current_epoch = 100;
let clock = Clock {
epoch: current_epoch,
..Clock::default()
};
let source_address = Pubkey::new_unique();
let source_meta = Meta {
rent_exempt_reserve,
..Meta::auto(&source_address)
};
let dest_address = Pubkey::new_unique();
let dest_account = AccountSharedData::new_data_with_space(
rent_exempt_reserve,
&StakeStateV2::Uninitialized,
StakeStateV2::size_of(),
&id(),
)
.unwrap();
let instruction_accounts = vec![
AccountMeta {
pubkey: source_address,
is_signer: true,
is_writable: true,
},
AccountMeta {
pubkey: dest_address,
is_signer: false,
is_writable: true,
},
];
for (source_delegation, split_amount, expected_result) in [
(minimum_delegation * 2, minimum_delegation, Ok(())),
(
minimum_delegation * 2,
minimum_delegation - 1,
Err(StakeError::InsufficientDelegation.into()),
),
(
(minimum_delegation * 2) - 1,
minimum_delegation,
Err(StakeError::InsufficientDelegation.into()),
),
(
(minimum_delegation - 1) * 2,
minimum_delegation - 1,
Err(StakeError::InsufficientDelegation.into()),
),
] {
let source_account = AccountSharedData::new_data_with_space(
source_delegation + rent_exempt_reserve,
&just_stake(source_meta, source_delegation),
StakeStateV2::size_of(),
&id(),
)
.unwrap();
let expected_active_stake = get_active_stake_for_tests(
&[source_account.clone(), dest_account.clone()],
&clock,
&stake_history,
);
let accounts = process_instruction(
&mollusk,
&serialize(&StakeInstruction::Split(split_amount)).unwrap(),
vec![
(source_address, source_account),
(dest_address, dest_account.clone()),
(rent::id(), create_account_shared_data_for_test(&rent)),
(
StakeHistory::id(),
create_account_shared_data_for_test(&stake_history),
),
(clock::id(), create_account_shared_data_for_test(&clock)),
(
epoch_schedule::id(),
create_account_shared_data_for_test(&EpochSchedule::default()),
),
],
instruction_accounts.clone(),
expected_result.clone(),
);
assert_eq!(
expected_active_stake,
get_active_stake_for_tests(&accounts[0..2], &clock, &stake_history)
);
}
}
#[test]
fn test_split_full_amount_minimum_stake_delegation() {
let mollusk = mollusk_bpf();
let minimum_delegation = crate::get_minimum_delegation();
let rent = Rent::default();
let rent_exempt_reserve = rent.minimum_balance(StakeStateV2::size_of());
let stake_history = StakeHistory::default();
let current_epoch = 100;
let clock = Clock {
epoch: current_epoch,
..Clock::default()
};
let source_address = Pubkey::new_unique();
let source_meta = Meta {
rent_exempt_reserve,
..Meta::auto(&source_address)
};
let dest_address = Pubkey::new_unique();
let dest_account = AccountSharedData::new_data_with_space(
0,
&StakeStateV2::Uninitialized,
StakeStateV2::size_of(),
&id(),
)
.unwrap();
let instruction_accounts = vec![
AccountMeta {
pubkey: source_address,
is_signer: true,
is_writable: true,
},
AccountMeta {
pubkey: dest_address,
is_signer: false,
is_writable: true,
},
];
for (reserve, expected_result) in [
(rent_exempt_reserve, Ok(())),
(
rent_exempt_reserve - 1,
Err(ProgramError::InsufficientFunds),
),
] {
for (stake_delegation, source_stake_state) in &[
(0, StakeStateV2::Initialized(source_meta)),
(
minimum_delegation,
just_stake(source_meta, minimum_delegation),
),
] {
let source_account = AccountSharedData::new_data_with_space(
stake_delegation + reserve,
source_stake_state,
StakeStateV2::size_of(),
&id(),
)
.unwrap();
let expected_active_stake = get_active_stake_for_tests(
&[source_account.clone(), dest_account.clone()],
&clock,
&stake_history,
);
let accounts = process_instruction(
&mollusk,
&serialize(&StakeInstruction::Split(source_account.lamports())).unwrap(),
vec![
(source_address, source_account),
(dest_address, dest_account.clone()),
(rent::id(), create_account_shared_data_for_test(&rent)),
(
StakeHistory::id(),
create_account_shared_data_for_test(&stake_history),
),
(clock::id(), create_account_shared_data_for_test(&clock)),
(
epoch_schedule::id(),
create_account_shared_data_for_test(&EpochSchedule::default()),
),
],
instruction_accounts.clone(),
expected_result.clone(),
);
assert_eq!(
expected_active_stake,
get_active_stake_for_tests(&accounts[0..2], &clock, &stake_history)
);
}
}
}
#[test]
fn test_initialized_split_destination_minimum_balance() {
let mollusk = mollusk_bpf();
let rent = Rent::default();
let rent_exempt_reserve = rent.minimum_balance(StakeStateV2::size_of());
let source_address = Pubkey::new_unique();
let destination_address = Pubkey::new_unique();
let instruction_accounts = vec![
AccountMeta {
pubkey: source_address,
is_signer: true,
is_writable: true,
},
AccountMeta {
pubkey: destination_address,
is_signer: false,
is_writable: true,
},
];
for (destination_starting_balance, split_amount, expected_result) in [
(rent_exempt_reserve, 0, Err(ProgramError::InsufficientFunds)),
(rent_exempt_reserve, 1, Ok(())),
(rent_exempt_reserve - 1, 1, Ok(())),
(
rent_exempt_reserve - 2,
1,
Err(ProgramError::InsufficientFunds),
),
(1, rent_exempt_reserve - 1, Ok(())),
(
1,
rent_exempt_reserve - 2,
Err(ProgramError::InsufficientFunds),
),
(0, rent_exempt_reserve, Ok(())),
(
0,
rent_exempt_reserve - 1,
Err(ProgramError::InsufficientFunds),
),
] {
let source_balance = rent_exempt_reserve + split_amount;
let source_meta = Meta {
rent_exempt_reserve,
..Meta::auto(&source_address)
};
let source_account = AccountSharedData::new_data_with_space(
source_balance,
&StakeStateV2::Initialized(source_meta),
StakeStateV2::size_of(),
&id(),
)
.unwrap();
let destination_account = AccountSharedData::new_data_with_space(
destination_starting_balance,
&StakeStateV2::Uninitialized,
StakeStateV2::size_of(),
&id(),
)
.unwrap();
process_instruction(
&mollusk,
&serialize(&StakeInstruction::Split(split_amount)).unwrap(),
vec![
(source_address, source_account),
(destination_address, destination_account),
(rent::id(), create_account_shared_data_for_test(&rent)),
],
instruction_accounts.clone(),
expected_result.clone(),
);
}
}
#[test]
fn test_staked_split_destination_minimum_balance() {
let mollusk = mollusk_bpf();
let minimum_delegation = crate::get_minimum_delegation();
let rent = Rent::default();
let rent_exempt_reserve = rent.minimum_balance(StakeStateV2::size_of());
let current_epoch = 100;
let clock = Clock {
epoch: current_epoch,
..Clock::default()
};
let source_address = Pubkey::new_unique();
let destination_address = Pubkey::new_unique();
let instruction_accounts = vec![
AccountMeta {
pubkey: source_address,
is_signer: true,
is_writable: true,
},
AccountMeta {
pubkey: destination_address,
is_signer: false,
is_writable: true,
},
];
for (destination_starting_balance, split_amount, expected_result) in [
(
rent_exempt_reserve + minimum_delegation,
0,
Err(ProgramError::InsufficientFunds),
),
(
rent_exempt_reserve + minimum_delegation,
minimum_delegation - 1,
Err(StakeError::InsufficientDelegation.into()),
),
(
rent_exempt_reserve + minimum_delegation - 1,
1,
Err(StakeError::InsufficientDelegation.into()),
),
(
rent_exempt_reserve + minimum_delegation - 2,
1,
Err(StakeError::InsufficientDelegation.into()),
),
(rent_exempt_reserve, minimum_delegation, Ok(())),
(
rent_exempt_reserve,
minimum_delegation - 1,
Err(StakeError::InsufficientDelegation.into()),
),
(
rent_exempt_reserve - 1,
minimum_delegation + 1,
Err(ProgramError::InsufficientFunds),
),
(
rent_exempt_reserve - 1,
minimum_delegation,
Err(ProgramError::InsufficientFunds),
),
(
1,
rent_exempt_reserve + minimum_delegation - 1,
Err(ProgramError::InsufficientFunds),
),
(
1,
rent_exempt_reserve + minimum_delegation - 2,
Err(ProgramError::InsufficientFunds),
),
(
0,
rent_exempt_reserve + minimum_delegation,
Err(ProgramError::InsufficientFunds),
),
(
0,
rent_exempt_reserve + minimum_delegation - 1,
Err(ProgramError::InsufficientFunds),
),
] {
let source_balance = rent_exempt_reserve + minimum_delegation + split_amount;
let source_meta = Meta {
rent_exempt_reserve,
..Meta::auto(&source_address)
};
let source_stake_delegation = source_balance - rent_exempt_reserve;
let source_account = AccountSharedData::new_data_with_space(
source_balance,
&just_stake(source_meta, source_stake_delegation),
StakeStateV2::size_of(),
&id(),
)
.unwrap();
let destination_account = AccountSharedData::new_data_with_space(
destination_starting_balance,
&StakeStateV2::Uninitialized,
StakeStateV2::size_of(),
&id(),
)
.unwrap();
let expected_active_stake = get_active_stake_for_tests(
&[source_account.clone(), destination_account.clone()],
&clock,
&StakeHistory::default(),
);
let accounts = process_instruction(
&mollusk,
&serialize(&StakeInstruction::Split(split_amount)).unwrap(),
vec![
(source_address, source_account.clone()),
(destination_address, destination_account),
(rent::id(), create_account_shared_data_for_test(&rent)),
(StakeHistory::id(), create_empty_stake_history_for_test()),
(clock::id(), create_account_shared_data_for_test(&clock)),
(
epoch_schedule::id(),
create_account_shared_data_for_test(&EpochSchedule::default()),
),
],
instruction_accounts.clone(),
expected_result.clone(),
);
assert_eq!(
expected_active_stake,
get_active_stake_for_tests(&accounts[0..2], &clock, &StakeHistory::default())
);
if expected_result.is_ok() {
assert_matches!(accounts[0].state().unwrap(), StakeStateV2::Stake(_, _, _));
if let StakeStateV2::Stake(_, destination_stake, _) = accounts[1].state().unwrap() {
let destination_initial_rent_deficit =
rent_exempt_reserve.saturating_sub(destination_starting_balance);
let expected_destination_stake_delegation =
split_amount - destination_initial_rent_deficit;
assert_eq!(
expected_destination_stake_delegation,
destination_stake.delegation.stake
);
assert!(destination_stake.delegation.stake >= minimum_delegation,);
} else {
panic!("destination state must be StakeStake::Stake after successful split when source is also StakeStateV2::Stake!");
}
}
}
}
#[test]
fn test_withdraw_minimum_stake_delegation() {
let mollusk = mollusk_bpf();
let minimum_delegation = crate::get_minimum_delegation();
let rent_exempt_reserve = default_stake_rent();
let stake_address = solana_pubkey::new_rand();
let meta = Meta {
rent_exempt_reserve,
..Meta::auto(&stake_address)
};
let recipient_address = solana_pubkey::new_rand();
let instruction_accounts = vec![
AccountMeta {
pubkey: stake_address,
is_signer: false,
is_writable: true,
},
AccountMeta {
pubkey: recipient_address,
is_signer: false,
is_writable: true,
},
AccountMeta {
pubkey: clock::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: StakeHistory::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: stake_address,
is_signer: true,
is_writable: false,
},
];
let starting_stake_delegation = minimum_delegation;
for (ending_stake_delegation, expected_result) in [
(minimum_delegation, Ok(())),
(minimum_delegation - 1, Err(ProgramError::InsufficientFunds)),
] {
for (stake_delegation, stake_state) in &[
(0, StakeStateV2::Initialized(meta)),
(minimum_delegation, just_stake(meta, minimum_delegation)),
] {
let rewards_balance = 123;
let stake_account = AccountSharedData::new_data_with_space(
stake_delegation + rent_exempt_reserve + rewards_balance,
stake_state,
StakeStateV2::size_of(),
&id(),
)
.unwrap();
let withdraw_amount =
(starting_stake_delegation + rewards_balance) - ending_stake_delegation;
#[allow(deprecated)]
process_instruction(
&mollusk,
&serialize(&StakeInstruction::Withdraw(withdraw_amount)).unwrap(),
vec![
(stake_address, stake_account),
(
recipient_address,
AccountSharedData::new(rent_exempt_reserve, 0, &system_program::id()),
),
(
clock::id(),
create_account_shared_data_for_test(&Clock::default()),
),
(
rent::id(),
create_account_shared_data_for_test(&Rent::default()),
),
(
StakeHistory::id(),
create_account_shared_data_for_test(&StakeHistory::default()),
),
(
stake_config::id(),
config::create_account(0, &stake_config::Config::default()),
),
(
epoch_schedule::id(),
create_account_shared_data_for_test(&EpochSchedule::default()),
),
],
instruction_accounts.clone(),
expected_result.clone(),
);
}
}
}
#[test]
fn test_behavior_withdrawal_then_redelegate_with_less_than_minimum_stake_delegation() {
let mollusk = mollusk_bpf();
let minimum_delegation = crate::get_minimum_delegation();
let rent = Rent::default();
let rent_exempt_reserve = rent.minimum_balance(StakeStateV2::size_of());
let stake_address = solana_pubkey::new_rand();
let stake_account = AccountSharedData::new(
rent_exempt_reserve + minimum_delegation,
StakeStateV2::size_of(),
&id(),
);
let vote_address = solana_pubkey::new_rand();
let vote_account = create_default_vote_account();
let recipient_address = solana_pubkey::new_rand();
let mut clock = Clock::default();
#[allow(deprecated)]
let mut transaction_accounts = vec![
(stake_address, stake_account),
(vote_address, vote_account),
(
recipient_address,
AccountSharedData::new(rent_exempt_reserve, 0, &system_program::id()),
),
(clock::id(), create_account_shared_data_for_test(&clock)),
(StakeHistory::id(), create_empty_stake_history_for_test()),
(
stake_config::id(),
config::create_account(0, &stake_config::Config::default()),
),
(
epoch_schedule::id(),
create_account_shared_data_for_test(&EpochSchedule::default()),
),
(rent::id(), create_account_shared_data_for_test(&rent)),
];
#[allow(deprecated)]
let instruction_accounts = vec![
AccountMeta {
pubkey: stake_address,
is_signer: true,
is_writable: true,
},
AccountMeta {
pubkey: vote_address,
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: clock::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: StakeHistory::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: stake_config::id(),
is_signer: false,
is_writable: false,
},
];
let accounts = process_instruction(
&mollusk,
&serialize(&StakeInstruction::Initialize(
Authorized::auto(&stake_address),
Lockup::default(),
))
.unwrap(),
transaction_accounts.clone(),
vec![
AccountMeta {
pubkey: stake_address,
is_signer: true,
is_writable: true,
},
AccountMeta {
pubkey: rent::id(),
is_signer: false,
is_writable: false,
},
],
Ok(()),
);
transaction_accounts[0] = (stake_address, accounts[0].clone());
let accounts = process_instruction(
&mollusk,
&serialize(&StakeInstruction::DelegateStake).unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Ok(()),
);
transaction_accounts[0] = (stake_address, accounts[0].clone());
transaction_accounts[1] = (vote_address, accounts[1].clone());
clock.epoch += 1;
transaction_accounts[3] = (clock::id(), create_account_shared_data_for_test(&clock));
let accounts = process_instruction(
&mollusk,
&serialize(&StakeInstruction::Deactivate).unwrap(),
transaction_accounts.clone(),
vec![
AccountMeta {
pubkey: stake_address,
is_signer: true,
is_writable: true,
},
AccountMeta {
pubkey: clock::id(),
is_signer: false,
is_writable: false,
},
],
Ok(()),
);
transaction_accounts[0] = (stake_address, accounts[0].clone());
clock.epoch += 1;
transaction_accounts[3] = (clock::id(), create_account_shared_data_for_test(&clock));
let withdraw_amount = accounts[0].lamports() - (rent_exempt_reserve + minimum_delegation - 1);
let accounts = process_instruction(
&mollusk,
&serialize(&StakeInstruction::Withdraw(withdraw_amount)).unwrap(),
transaction_accounts.clone(),
vec![
AccountMeta {
pubkey: stake_address,
is_signer: false,
is_writable: true,
},
AccountMeta {
pubkey: recipient_address,
is_signer: false,
is_writable: true,
},
AccountMeta {
pubkey: clock::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: StakeHistory::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: stake_address,
is_signer: true,
is_writable: false,
},
],
Ok(()),
);
transaction_accounts[0] = (stake_address, accounts[0].clone());
process_instruction(
&mollusk,
&serialize(&StakeInstruction::DelegateStake).unwrap(),
transaction_accounts,
instruction_accounts,
Err(StakeError::InsufficientDelegation.into()),
);
}
#[test]
fn test_split_source_uninitialized() {
let mollusk = mollusk_bpf();
let rent = Rent::default();
let rent_exempt_reserve = rent.minimum_balance(StakeStateV2::size_of());
let minimum_delegation = crate::get_minimum_delegation();
let stake_lamports = (rent_exempt_reserve + minimum_delegation) * 2;
let stake_address = solana_pubkey::new_rand();
let stake_account = AccountSharedData::new_data_with_space(
stake_lamports,
&StakeStateV2::Uninitialized,
StakeStateV2::size_of(),
&id(),
)
.unwrap();
let split_to_address = solana_pubkey::new_rand();
let split_to_account = AccountSharedData::new_data_with_space(
0,
&StakeStateV2::Uninitialized,
StakeStateV2::size_of(),
&id(),
)
.unwrap();
let transaction_accounts = vec![
(stake_address, stake_account),
(split_to_address, split_to_account),
];
let instruction_accounts = vec![
AccountMeta {
pubkey: stake_address,
is_signer: true,
is_writable: true,
},
AccountMeta {
pubkey: split_to_address,
is_signer: false,
is_writable: true,
},
];
process_instruction(
&mollusk,
&serialize(&StakeInstruction::Split(0)).unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Err(ProgramError::InsufficientFunds),
);
{
let mut instruction_accounts = instruction_accounts.clone();
instruction_accounts[1].pubkey = stake_address;
process_instruction(
&mollusk,
&serialize(&StakeInstruction::Split(stake_lamports)).unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Err(ProgramError::InvalidArgument),
);
process_instruction(
&mollusk,
&serialize(&StakeInstruction::Split(stake_lamports / 2)).unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Err(ProgramError::InvalidArgument),
);
process_instruction(
&mollusk,
&serialize(&StakeInstruction::Split(stake_lamports + 1)).unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Err(ProgramError::InvalidArgument),
);
}
let accounts = process_instruction(
&mollusk,
&serialize(&StakeInstruction::Split(stake_lamports / 2)).unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Ok(()),
);
assert_eq!(accounts[0].lamports(), accounts[1].lamports());
let accounts = process_instruction(
&mollusk,
&serialize(&StakeInstruction::Split(stake_lamports)).unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Ok(()),
);
assert_eq!(accounts[0].lamports(), 0);
assert_eq!(accounts[1].lamports(), stake_lamports);
assert_eq!(accounts[0].data().len(), 0);
assert_eq!(accounts[1].data().len(), StakeStateV2::size_of());
{
let mut instruction_accounts = instruction_accounts.clone();
instruction_accounts[0].is_signer = false;
process_instruction(
&mollusk,
&serialize(&StakeInstruction::Split(stake_lamports / 2)).unwrap(),
transaction_accounts,
instruction_accounts,
Err(ProgramError::MissingRequiredSignature),
);
}
}
#[test]
fn test_split_split_not_uninitialized() {
let mollusk = mollusk_bpf();
let stake_lamports = 42;
let stake_address = solana_pubkey::new_rand();
let stake_account = AccountSharedData::new_data_with_space(
stake_lamports,
&just_stake(Meta::auto(&stake_address), stake_lamports),
StakeStateV2::size_of(),
&id(),
)
.unwrap();
let split_to_address = solana_pubkey::new_rand();
let instruction_accounts = vec![
AccountMeta {
pubkey: stake_address,
is_signer: true,
is_writable: true,
},
AccountMeta {
pubkey: split_to_address,
is_signer: false,
is_writable: true,
},
];
for split_to_state in &[
StakeStateV2::Initialized(Meta::default()),
StakeStateV2::Stake(Meta::default(), Stake::default(), StakeFlags::default()),
StakeStateV2::RewardsPool,
] {
let split_to_account = AccountSharedData::new_data_with_space(
0,
split_to_state,
StakeStateV2::size_of(),
&id(),
)
.unwrap();
process_instruction(
&mollusk,
&serialize(&StakeInstruction::Split(stake_lamports / 2)).unwrap(),
vec![
(stake_address, stake_account.clone()),
(split_to_address, split_to_account),
],
instruction_accounts.clone(),
Err(ProgramError::InvalidAccountData),
);
}
}
#[test]
fn test_split_more_than_staked() {
let mollusk = mollusk_bpf();
let rent = Rent::default();
let rent_exempt_reserve = rent.minimum_balance(StakeStateV2::size_of());
let stake_history = StakeHistory::default();
let current_epoch = 100;
let minimum_delegation = crate::get_minimum_delegation();
let stake_lamports = (rent_exempt_reserve + minimum_delegation) * 2;
let stake_address = solana_pubkey::new_rand();
let stake_account = AccountSharedData::new_data_with_space(
stake_lamports,
&just_stake(
Meta {
rent_exempt_reserve,
..Meta::auto(&stake_address)
},
stake_lamports / 2 - 1,
),
StakeStateV2::size_of(),
&id(),
)
.unwrap();
let split_to_address = solana_pubkey::new_rand();
let split_to_account = AccountSharedData::new_data_with_space(
rent_exempt_reserve,
&StakeStateV2::Uninitialized,
StakeStateV2::size_of(),
&id(),
)
.unwrap();
let transaction_accounts = vec![
(stake_address, stake_account),
(split_to_address, split_to_account),
(rent::id(), create_account_shared_data_for_test(&rent)),
(
StakeHistory::id(),
create_account_shared_data_for_test(&stake_history),
),
(
clock::id(),
create_account_shared_data_for_test(&Clock {
epoch: current_epoch,
..Clock::default()
}),
),
(
epoch_schedule::id(),
create_account_shared_data_for_test(&EpochSchedule::default()),
),
];
let instruction_accounts = vec![
AccountMeta {
pubkey: stake_address,
is_signer: true,
is_writable: true,
},
AccountMeta {
pubkey: split_to_address,
is_signer: false,
is_writable: true,
},
];
process_instruction(
&mollusk,
&serialize(&StakeInstruction::Split(stake_lamports / 2)).unwrap(),
transaction_accounts,
instruction_accounts,
Err(StakeError::InsufficientDelegation.into()),
);
}
#[test]
fn test_split_with_rent() {
let mollusk = mollusk_bpf();
let rent = Rent::default();
let rent_exempt_reserve = rent.minimum_balance(StakeStateV2::size_of());
let stake_history = StakeHistory::default();
let current_epoch = 100;
let clock = Clock {
epoch: current_epoch,
..Clock::default()
};
let minimum_delegation = crate::get_minimum_delegation();
let stake_address = solana_pubkey::new_rand();
let split_to_address = solana_pubkey::new_rand();
let split_to_account = AccountSharedData::new_data_with_space(
0,
&StakeStateV2::Uninitialized,
StakeStateV2::size_of(),
&id(),
)
.unwrap();
let instruction_accounts = vec![
AccountMeta {
pubkey: stake_address,
is_signer: true,
is_writable: true,
},
AccountMeta {
pubkey: split_to_address,
is_signer: false,
is_writable: true,
},
];
let meta = Meta {
authorized: Authorized::auto(&stake_address),
rent_exempt_reserve,
..Meta::default()
};
for (minimum_balance, state) in &[
(rent_exempt_reserve, StakeStateV2::Initialized(meta)),
(
rent_exempt_reserve + minimum_delegation,
just_stake(meta, minimum_delegation * 2 + rent_exempt_reserve),
),
] {
let stake_lamports = minimum_balance * 2;
let stake_account = AccountSharedData::new_data_with_space(
stake_lamports,
state,
StakeStateV2::size_of(),
&id(),
)
.unwrap();
let expected_active_stake = get_active_stake_for_tests(
&[stake_account.clone(), split_to_account.clone()],
&clock,
&stake_history,
);
let mut transaction_accounts = vec![
(stake_address, stake_account),
(split_to_address, split_to_account.clone()),
(rent::id(), create_account_shared_data_for_test(&rent)),
(
StakeHistory::id(),
create_account_shared_data_for_test(&stake_history),
),
(clock::id(), create_account_shared_data_for_test(&clock)),
(
epoch_schedule::id(),
create_account_shared_data_for_test(&EpochSchedule::default()),
),
];
process_instruction(
&mollusk,
&serialize(&StakeInstruction::Split(minimum_balance - 1)).unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Err(ProgramError::InsufficientFunds),
);
process_instruction(
&mollusk,
&serialize(&StakeInstruction::Split(
stake_lamports - minimum_balance + 1,
))
.unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Err(ProgramError::InsufficientFunds),
);
transaction_accounts[1].1.set_lamports(*minimum_balance);
let accounts = process_instruction(
&mollusk,
&serialize(&StakeInstruction::Split(stake_lamports - minimum_balance)).unwrap(),
transaction_accounts,
instruction_accounts.clone(),
Ok(()),
);
assert_eq!(
expected_active_stake,
get_active_stake_for_tests(&accounts[0..2], &clock, &stake_history)
);
if let StakeStateV2::Stake(meta, stake, stake_flags) = state {
assert_eq!(
accounts[1].state(),
Ok(StakeStateV2::Stake(
*meta,
Stake {
delegation: Delegation {
stake: stake_lamports - minimum_balance,
..stake.delegation
},
..*stake
},
*stake_flags,
))
);
assert_eq!(accounts[0].lamports(), *minimum_balance,);
assert_eq!(accounts[1].lamports(), stake_lamports,);
}
}
}
#[test]
fn test_split_to_account_with_rent_exempt_reserve() {
let mollusk = mollusk_bpf();
let rent = Rent::default();
let rent_exempt_reserve = rent.minimum_balance(StakeStateV2::size_of());
let current_epoch = 100;
let clock = Clock {
epoch: current_epoch,
..Clock::default()
};
let minimum_delegation = crate::get_minimum_delegation();
let stake_lamports = (rent_exempt_reserve + minimum_delegation) * 2;
let stake_address = solana_pubkey::new_rand();
let meta = Meta {
authorized: Authorized::auto(&stake_address),
rent_exempt_reserve,
..Meta::default()
};
let state = just_stake(meta, stake_lamports - rent_exempt_reserve);
let stake_account = AccountSharedData::new_data_with_space(
stake_lamports,
&state,
StakeStateV2::size_of(),
&id(),
)
.unwrap();
let split_to_address = solana_pubkey::new_rand();
let instruction_accounts = vec![
AccountMeta {
pubkey: stake_address,
is_signer: true,
is_writable: true,
},
AccountMeta {
pubkey: split_to_address,
is_signer: false,
is_writable: true,
},
];
let transaction_accounts = |initial_balance: u64| -> Vec<(Pubkey, AccountSharedData)> {
let split_to_account = AccountSharedData::new_data_with_space(
initial_balance,
&StakeStateV2::Uninitialized,
StakeStateV2::size_of(),
&id(),
)
.unwrap();
vec![
(stake_address, stake_account.clone()),
(split_to_address, split_to_account),
(rent::id(), create_account_shared_data_for_test(&rent)),
(StakeHistory::id(), create_empty_stake_history_for_test()),
(clock::id(), create_account_shared_data_for_test(&clock)),
(
epoch_schedule::id(),
create_account_shared_data_for_test(&EpochSchedule::default()),
),
]
};
let split_lamport_balances = vec![0, rent_exempt_reserve - 1];
for initial_balance in split_lamport_balances {
let transaction_accounts = transaction_accounts(initial_balance);
process_instruction(
&mollusk,
&serialize(&StakeInstruction::Split(stake_lamports + 1)).unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Err(ProgramError::InsufficientFunds),
);
process_instruction(
&mollusk,
&serialize(&StakeInstruction::Split(stake_lamports / 2)).unwrap(),
transaction_accounts,
instruction_accounts.clone(),
Err(ProgramError::InsufficientFunds),
);
}
let split_lamport_balances = vec![
rent_exempt_reserve,
rent_exempt_reserve + minimum_delegation - 1,
rent_exempt_reserve + minimum_delegation,
];
for initial_balance in split_lamport_balances {
let transaction_accounts = transaction_accounts(initial_balance);
let expected_active_stake = get_active_stake_for_tests(
&[
transaction_accounts[0].1.clone(),
transaction_accounts[1].1.clone(),
],
&clock,
&StakeHistory::default(),
);
process_instruction(
&mollusk,
&serialize(&StakeInstruction::Split(stake_lamports + 1)).unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Err(ProgramError::InsufficientFunds),
);
let accounts = process_instruction(
&mollusk,
&serialize(&StakeInstruction::Split(stake_lamports / 2)).unwrap(),
transaction_accounts,
instruction_accounts.clone(),
Ok(()),
);
assert_eq!(
accounts[0].lamports() + accounts[1].lamports(),
stake_lamports + initial_balance,
);
assert_eq!(
expected_active_stake,
get_active_stake_for_tests(&accounts[0..2], &clock, &StakeHistory::default())
);
if let StakeStateV2::Stake(meta, stake, stake_flags) = state {
let expected_stake =
stake_lamports / 2 - (rent_exempt_reserve.saturating_sub(initial_balance));
assert_eq!(
Ok(StakeStateV2::Stake(
meta,
Stake {
delegation: Delegation {
stake: stake_lamports / 2
- (rent_exempt_reserve.saturating_sub(initial_balance)),
..stake.delegation
},
..stake
},
stake_flags
)),
accounts[1].state(),
);
assert_eq!(
accounts[1].lamports(),
expected_stake
+ rent_exempt_reserve
+ initial_balance.saturating_sub(rent_exempt_reserve),
);
assert_eq!(
Ok(StakeStateV2::Stake(
meta,
Stake {
delegation: Delegation {
stake: stake_lamports / 2 - rent_exempt_reserve,
..stake.delegation
},
..stake
},
stake_flags,
)),
accounts[0].state(),
);
}
}
}
#[test]
fn test_split_from_larger_sized_account() {
let mollusk = mollusk_bpf();
let rent = Rent::default();
let source_larger_rent_exempt_reserve = rent.minimum_balance(StakeStateV2::size_of() + 100);
let split_rent_exempt_reserve = rent.minimum_balance(StakeStateV2::size_of());
let current_epoch = 100;
let clock = Clock {
epoch: current_epoch,
..Clock::default()
};
let minimum_delegation = crate::get_minimum_delegation();
let stake_lamports = (source_larger_rent_exempt_reserve + minimum_delegation) * 2;
let stake_address = solana_pubkey::new_rand();
let meta = Meta {
authorized: Authorized::auto(&stake_address),
rent_exempt_reserve: source_larger_rent_exempt_reserve,
..Meta::default()
};
let state = just_stake(meta, stake_lamports - source_larger_rent_exempt_reserve);
let stake_account = AccountSharedData::new_data_with_space(
stake_lamports,
&state,
StakeStateV2::size_of() + 100,
&id(),
)
.unwrap();
let split_to_address = solana_pubkey::new_rand();
let instruction_accounts = vec![
AccountMeta {
pubkey: stake_address,
is_signer: true,
is_writable: true,
},
AccountMeta {
pubkey: split_to_address,
is_signer: false,
is_writable: true,
},
];
let transaction_accounts = |initial_balance: u64| -> Vec<(Pubkey, AccountSharedData)> {
let split_to_account = AccountSharedData::new_data_with_space(
initial_balance,
&StakeStateV2::Uninitialized,
StakeStateV2::size_of(),
&id(),
)
.unwrap();
vec![
(stake_address, stake_account.clone()),
(split_to_address, split_to_account),
(rent::id(), create_account_shared_data_for_test(&rent)),
(StakeHistory::id(), create_empty_stake_history_for_test()),
(clock::id(), create_account_shared_data_for_test(&clock)),
(
epoch_schedule::id(),
create_account_shared_data_for_test(&EpochSchedule::default()),
),
]
};
let split_lamport_balances = vec![0, split_rent_exempt_reserve - 1];
for initial_balance in split_lamport_balances {
process_instruction(
&mollusk,
&serialize(&StakeInstruction::Split(stake_lamports / 2)).unwrap(),
transaction_accounts(initial_balance),
instruction_accounts.clone(),
Err(ProgramError::InsufficientFunds),
);
}
let split_lamport_balances = vec![
split_rent_exempt_reserve,
split_rent_exempt_reserve + minimum_delegation - 1,
split_rent_exempt_reserve + minimum_delegation,
];
for initial_balance in split_lamport_balances {
let transaction_accounts = transaction_accounts(initial_balance);
let expected_active_stake = get_active_stake_for_tests(
&[
transaction_accounts[0].1.clone(),
transaction_accounts[1].1.clone(),
],
&clock,
&StakeHistory::default(),
);
process_instruction(
&mollusk,
&serialize(&StakeInstruction::Split(stake_lamports + 1)).unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Err(ProgramError::InsufficientFunds),
);
let accounts = process_instruction(
&mollusk,
&serialize(&StakeInstruction::Split(stake_lamports / 2)).unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Ok(()),
);
assert_eq!(
accounts[0].lamports() + accounts[1].lamports(),
stake_lamports + initial_balance
);
assert_eq!(
expected_active_stake,
get_active_stake_for_tests(&accounts[0..2], &clock, &StakeHistory::default())
);
if let StakeStateV2::Stake(meta, stake, stake_flags) = state {
let expected_split_meta = Meta {
authorized: Authorized::auto(&stake_address),
rent_exempt_reserve: split_rent_exempt_reserve,
..Meta::default()
};
let expected_stake =
stake_lamports / 2 - (split_rent_exempt_reserve.saturating_sub(initial_balance));
assert_eq!(
Ok(StakeStateV2::Stake(
expected_split_meta,
Stake {
delegation: Delegation {
stake: expected_stake,
..stake.delegation
},
..stake
},
stake_flags,
)),
accounts[1].state()
);
assert_eq!(
accounts[1].lamports(),
expected_stake
+ split_rent_exempt_reserve
+ initial_balance.saturating_sub(split_rent_exempt_reserve)
);
assert_eq!(
Ok(StakeStateV2::Stake(
meta,
Stake {
delegation: Delegation {
stake: stake_lamports / 2 - source_larger_rent_exempt_reserve,
..stake.delegation
},
..stake
},
stake_flags,
)),
accounts[0].state()
);
}
}
}
#[test]
fn test_split_from_smaller_sized_account() {
let mollusk = mollusk_bpf();
let rent = Rent::default();
let source_smaller_rent_exempt_reserve = rent.minimum_balance(StakeStateV2::size_of());
let split_rent_exempt_reserve = rent.minimum_balance(StakeStateV2::size_of() + 100);
let stake_history = StakeHistory::default();
let current_epoch = 100;
let stake_lamports = split_rent_exempt_reserve + 1;
let stake_address = solana_pubkey::new_rand();
let meta = Meta {
authorized: Authorized::auto(&stake_address),
rent_exempt_reserve: source_smaller_rent_exempt_reserve,
..Meta::default()
};
let state = just_stake(meta, stake_lamports - source_smaller_rent_exempt_reserve);
let stake_account = AccountSharedData::new_data_with_space(
stake_lamports,
&state,
StakeStateV2::size_of(),
&id(),
)
.unwrap();
let split_to_address = solana_pubkey::new_rand();
let instruction_accounts = vec![
AccountMeta {
pubkey: stake_address,
is_signer: true,
is_writable: true,
},
AccountMeta {
pubkey: split_to_address,
is_signer: false,
is_writable: true,
},
];
let split_amount = stake_lamports - (source_smaller_rent_exempt_reserve + 1); let split_lamport_balances = vec![
0,
1,
split_rent_exempt_reserve,
split_rent_exempt_reserve + 1,
];
for initial_balance in split_lamport_balances {
let split_to_account = AccountSharedData::new_data_with_space(
initial_balance,
&StakeStateV2::Uninitialized,
StakeStateV2::size_of() + 100,
&id(),
)
.unwrap();
let transaction_accounts = vec![
(stake_address, stake_account.clone()),
(split_to_address, split_to_account),
(rent::id(), create_account_shared_data_for_test(&rent)),
(
StakeHistory::id(),
create_account_shared_data_for_test(&stake_history),
),
(
clock::id(),
create_account_shared_data_for_test(&Clock {
epoch: current_epoch,
..Clock::default()
}),
),
(
epoch_schedule::id(),
create_account_shared_data_for_test(&EpochSchedule::default()),
),
];
process_instruction(
&mollusk,
&serialize(&StakeInstruction::Split(split_amount)).unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Err(ProgramError::InvalidAccountData),
);
process_instruction(
&mollusk,
&serialize(&StakeInstruction::Split(stake_lamports)).unwrap(),
transaction_accounts,
instruction_accounts.clone(),
Err(ProgramError::InvalidAccountData),
);
}
}
#[test]
fn test_split_100_percent_of_source() {
let mollusk = mollusk_bpf();
let rent = Rent::default();
let rent_exempt_reserve = rent.minimum_balance(StakeStateV2::size_of());
let stake_history = StakeHistory::default();
let current_epoch = 100;
let clock = Clock {
epoch: current_epoch,
..Clock::default()
};
let minimum_delegation = crate::get_minimum_delegation();
let stake_lamports = rent_exempt_reserve + minimum_delegation;
let stake_address = solana_pubkey::new_rand();
let meta = Meta {
authorized: Authorized::auto(&stake_address),
rent_exempt_reserve,
..Meta::default()
};
let split_to_address = solana_pubkey::new_rand();
let split_to_account = AccountSharedData::new_data_with_space(
0,
&StakeStateV2::Uninitialized,
StakeStateV2::size_of(),
&id(),
)
.unwrap();
let instruction_accounts = vec![
AccountMeta {
pubkey: stake_address,
is_signer: true,
is_writable: true,
},
AccountMeta {
pubkey: split_to_address,
is_signer: false,
is_writable: true,
},
];
for state in &[
StakeStateV2::Initialized(meta),
just_stake(meta, stake_lamports - rent_exempt_reserve),
] {
let stake_account = AccountSharedData::new_data_with_space(
stake_lamports,
&state,
StakeStateV2::size_of(),
&id(),
)
.unwrap();
let expected_active_stake = get_active_stake_for_tests(
&[stake_account.clone(), split_to_account.clone()],
&clock,
&stake_history,
);
let transaction_accounts = vec![
(stake_address, stake_account),
(split_to_address, split_to_account.clone()),
(rent::id(), create_account_shared_data_for_test(&rent)),
(
StakeHistory::id(),
create_account_shared_data_for_test(&stake_history),
),
(clock::id(), create_account_shared_data_for_test(&clock)),
(
epoch_schedule::id(),
create_account_shared_data_for_test(&EpochSchedule::default()),
),
];
let accounts = process_instruction(
&mollusk,
&serialize(&StakeInstruction::Split(stake_lamports)).unwrap(),
transaction_accounts,
instruction_accounts.clone(),
Ok(()),
);
assert_eq!(
accounts[0].lamports() + accounts[1].lamports(),
stake_lamports
);
assert_eq!(
expected_active_stake,
get_active_stake_for_tests(&accounts[0..2], &clock, &stake_history)
);
match state {
StakeStateV2::Initialized(_) => {
assert_eq!(Ok(*state), accounts[1].state());
assert!(is_closed(&accounts[0]));
}
StakeStateV2::Stake(meta, stake, stake_flags) => {
assert_eq!(
Ok(StakeStateV2::Stake(
*meta,
Stake {
delegation: Delegation {
stake: stake_lamports - rent_exempt_reserve,
..stake.delegation
},
..*stake
},
*stake_flags
)),
accounts[1].state()
);
assert!(is_closed(&accounts[0]));
}
_ => unreachable!(),
}
}
}
#[test]
fn test_split_100_percent_of_source_to_account_with_lamports() {
let mollusk = mollusk_bpf();
let rent = Rent::default();
let rent_exempt_reserve = rent.minimum_balance(StakeStateV2::size_of());
let stake_history = StakeHistory::default();
let current_epoch = 100;
let clock = Clock {
epoch: current_epoch,
..Clock::default()
};
let minimum_delegation = crate::get_minimum_delegation();
let stake_lamports = rent_exempt_reserve + minimum_delegation;
let stake_address = solana_pubkey::new_rand();
let meta = Meta {
authorized: Authorized::auto(&stake_address),
rent_exempt_reserve,
..Meta::default()
};
let state = just_stake(meta, stake_lamports - rent_exempt_reserve);
let stake_account = AccountSharedData::new_data_with_space(
stake_lamports,
&state,
StakeStateV2::size_of(),
&id(),
)
.unwrap();
let split_to_address = solana_pubkey::new_rand();
let instruction_accounts = vec![
AccountMeta {
pubkey: stake_address,
is_signer: true,
is_writable: true,
},
AccountMeta {
pubkey: split_to_address,
is_signer: false,
is_writable: true,
},
];
let split_lamport_balances = vec![
0,
rent_exempt_reserve - 1,
rent_exempt_reserve,
rent_exempt_reserve + minimum_delegation - 1,
rent_exempt_reserve + minimum_delegation,
];
for initial_balance in split_lamport_balances {
let split_to_account = AccountSharedData::new_data_with_space(
initial_balance,
&StakeStateV2::Uninitialized,
StakeStateV2::size_of(),
&id(),
)
.unwrap();
let expected_active_stake = get_active_stake_for_tests(
&[stake_account.clone(), split_to_account.clone()],
&clock,
&stake_history,
);
let transaction_accounts = vec![
(stake_address, stake_account.clone()),
(split_to_address, split_to_account),
(rent::id(), create_account_shared_data_for_test(&rent)),
(
StakeHistory::id(),
create_account_shared_data_for_test(&stake_history),
),
(clock::id(), create_account_shared_data_for_test(&clock)),
(
epoch_schedule::id(),
create_account_shared_data_for_test(&EpochSchedule::default()),
),
];
let accounts = process_instruction(
&mollusk,
&serialize(&StakeInstruction::Split(stake_lamports)).unwrap(),
transaction_accounts,
instruction_accounts.clone(),
Ok(()),
);
assert_eq!(
accounts[0].lamports() + accounts[1].lamports(),
stake_lamports + initial_balance
);
assert_eq!(
expected_active_stake,
get_active_stake_for_tests(&accounts[0..2], &clock, &stake_history)
);
if let StakeStateV2::Stake(meta, stake, stake_flags) = state {
assert_eq!(
Ok(StakeStateV2::Stake(
meta,
Stake {
delegation: Delegation {
stake: stake_lamports - rent_exempt_reserve,
..stake.delegation
},
..stake
},
stake_flags,
)),
accounts[1].state()
);
assert!(is_closed(&accounts[0]));
}
}
}
#[test]
fn test_split_rent_exemptness() {
let mollusk = mollusk_bpf();
let rent = Rent::default();
let source_rent_exempt_reserve = rent.minimum_balance(StakeStateV2::size_of() + 100);
let split_rent_exempt_reserve = rent.minimum_balance(StakeStateV2::size_of());
let stake_history = StakeHistory::default();
let current_epoch = 100;
let clock = Clock {
epoch: current_epoch,
..Clock::default()
};
let minimum_delegation = crate::get_minimum_delegation();
let stake_lamports = source_rent_exempt_reserve + minimum_delegation;
let stake_address = solana_pubkey::new_rand();
let meta = Meta {
authorized: Authorized::auto(&stake_address),
rent_exempt_reserve: source_rent_exempt_reserve,
..Meta::default()
};
let split_to_address = solana_pubkey::new_rand();
let instruction_accounts = vec![
AccountMeta {
pubkey: stake_address,
is_signer: true,
is_writable: true,
},
AccountMeta {
pubkey: split_to_address,
is_signer: false,
is_writable: true,
},
];
for state in &[
StakeStateV2::Initialized(meta),
just_stake(meta, stake_lamports - source_rent_exempt_reserve),
] {
let stake_account = AccountSharedData::new_data_with_space(
stake_lamports,
&state,
StakeStateV2::size_of(),
&id(),
)
.unwrap();
let split_to_account = AccountSharedData::new_data_with_space(
0,
&StakeStateV2::Uninitialized,
StakeStateV2::size_of() + 10000,
&id(),
)
.unwrap();
let transaction_accounts = vec![
(stake_address, stake_account),
(split_to_address, split_to_account),
(rent::id(), create_account_shared_data_for_test(&rent)),
(
StakeHistory::id(),
create_account_shared_data_for_test(&stake_history),
),
(clock::id(), create_account_shared_data_for_test(&clock)),
(
epoch_schedule::id(),
create_account_shared_data_for_test(&EpochSchedule::default()),
),
];
process_instruction(
&mollusk,
&serialize(&StakeInstruction::Split(stake_lamports)).unwrap(),
transaction_accounts,
instruction_accounts.clone(),
Err(ProgramError::InvalidAccountData),
);
let stake_account = AccountSharedData::new_data_with_space(
stake_lamports,
&state,
StakeStateV2::size_of() + 100,
&id(),
)
.unwrap();
let split_to_account = AccountSharedData::new_data_with_space(
0,
&StakeStateV2::Uninitialized,
StakeStateV2::size_of(),
&id(),
)
.unwrap();
let expected_active_stake = get_active_stake_for_tests(
&[stake_account.clone(), split_to_account.clone()],
&clock,
&stake_history,
);
let transaction_accounts = vec![
(stake_address, stake_account),
(split_to_address, split_to_account),
(rent::id(), create_account_shared_data_for_test(&rent)),
(
StakeHistory::id(),
create_account_shared_data_for_test(&stake_history),
),
(
clock::id(),
create_account_shared_data_for_test(&Clock {
epoch: current_epoch,
..Clock::default()
}),
),
(
epoch_schedule::id(),
create_account_shared_data_for_test(&EpochSchedule::default()),
),
];
let accounts = process_instruction(
&mollusk,
&serialize(&StakeInstruction::Split(stake_lamports)).unwrap(),
transaction_accounts,
instruction_accounts.clone(),
Ok(()),
);
assert_eq!(accounts[1].lamports(), stake_lamports);
assert_eq!(
expected_active_stake,
get_active_stake_for_tests(&accounts[0..2], &clock, &stake_history)
);
let expected_split_meta = Meta {
authorized: Authorized::auto(&stake_address),
rent_exempt_reserve: split_rent_exempt_reserve,
..Meta::default()
};
match state {
StakeStateV2::Initialized(_) => {
assert_eq!(
Ok(StakeStateV2::Initialized(expected_split_meta)),
accounts[1].state()
);
assert!(is_closed(&accounts[0]));
}
StakeStateV2::Stake(_meta, stake, stake_flags) => {
let expected_stake = stake_lamports - source_rent_exempt_reserve;
assert_eq!(
Ok(StakeStateV2::Stake(
expected_split_meta,
Stake {
delegation: Delegation {
stake: expected_stake,
..stake.delegation
},
..*stake
},
*stake_flags,
)),
accounts[1].state()
);
assert_eq!(
accounts[1].lamports(),
expected_stake + source_rent_exempt_reserve,
);
assert!(is_closed(&accounts[0]));
}
_ => unreachable!(),
}
}
}
#[test]
fn test_split_require_rent_exempt_destination() {
let mollusk = mollusk_bpf();
let rent = Rent::default();
let rent_exempt_reserve = rent.minimum_balance(StakeStateV2::size_of());
let current_epoch = 100;
let clock = Clock {
epoch: current_epoch,
..Clock::default()
};
let minimum_delegation = crate::get_minimum_delegation();
let delegation_amount = 3 * minimum_delegation;
let source_lamports = rent_exempt_reserve + delegation_amount;
let source_address = Pubkey::new_unique();
let destination_address = Pubkey::new_unique();
let meta = Meta {
authorized: Authorized::auto(&source_address),
rent_exempt_reserve,
..Meta::default()
};
let instruction_accounts = vec![
AccountMeta {
pubkey: source_address,
is_signer: true,
is_writable: true,
},
AccountMeta {
pubkey: destination_address,
is_signer: false,
is_writable: true,
},
];
let expected_result = Err(ProgramError::InsufficientFunds);
for (split_amount, expected_result) in [
(2 * minimum_delegation, expected_result),
(source_lamports, Ok(())),
] {
for (state, expected_result) in &[
(StakeStateV2::Initialized(meta), Ok(())),
(just_stake(meta, delegation_amount), expected_result),
] {
let source_account = AccountSharedData::new_data_with_space(
source_lamports,
&state,
StakeStateV2::size_of(),
&id(),
)
.unwrap();
let transaction_accounts = |initial_balance: u64| -> Vec<(Pubkey, AccountSharedData)> {
let destination_account = AccountSharedData::new_data_with_space(
initial_balance,
&StakeStateV2::Uninitialized,
StakeStateV2::size_of(),
&id(),
)
.unwrap();
vec![
(source_address, source_account.clone()),
(destination_address, destination_account),
(rent::id(), create_account_shared_data_for_test(&rent)),
(StakeHistory::id(), create_empty_stake_history_for_test()),
(clock::id(), create_account_shared_data_for_test(&clock)),
(
epoch_schedule::id(),
create_account_shared_data_for_test(&EpochSchedule::default()),
),
]
};
let split_lamport_balances = vec![0, rent_exempt_reserve - 1];
for initial_balance in split_lamport_balances {
let transaction_accounts = transaction_accounts(initial_balance);
let expected_active_stake = get_active_stake_for_tests(
&[source_account.clone(), transaction_accounts[1].1.clone()],
&clock,
&StakeHistory::default(),
);
let result_accounts = process_instruction(
&mollusk,
&serialize(&StakeInstruction::Split(split_amount)).unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
if initial_balance + split_amount < rent_exempt_reserve {
Err(ProgramError::InsufficientFunds)
} else {
expected_result.clone()
},
);
let result_active_stake = get_active_stake_for_tests(
&result_accounts[0..2],
&clock,
&StakeHistory::default(),
);
if expected_active_stake > 0 && result_accounts[0].lamports() > 0
&& expected_result.is_ok()
{
assert_ne!(expected_active_stake, result_active_stake);
} else {
assert_eq!(expected_active_stake, result_active_stake);
}
}
let split_lamport_balances = vec![rent_exempt_reserve, rent_exempt_reserve + 1];
for initial_balance in split_lamport_balances {
let transaction_accounts = transaction_accounts(initial_balance);
let expected_active_stake = get_active_stake_for_tests(
&[source_account.clone(), transaction_accounts[1].1.clone()],
&clock,
&StakeHistory::default(),
);
let accounts = process_instruction(
&mollusk,
&serialize(&StakeInstruction::Split(split_amount)).unwrap(),
transaction_accounts,
instruction_accounts.clone(),
Ok(()),
);
assert_eq!(
accounts[0].lamports() + accounts[1].lamports(),
source_lamports + initial_balance
);
assert_eq!(
expected_active_stake,
get_active_stake_for_tests(&accounts[0..2], &clock, &StakeHistory::default())
);
if let StakeStateV2::Stake(meta, stake, stake_flags) = state {
if accounts[0].lamports() == 0 {
assert!(is_closed(&accounts[0]));
assert_eq!(
Ok(StakeStateV2::Stake(
*meta,
Stake {
delegation: Delegation {
stake: delegation_amount,
..stake.delegation
},
..*stake
},
*stake_flags,
)),
accounts[1].state()
);
} else {
assert_eq!(
Ok(StakeStateV2::Stake(
*meta,
Stake {
delegation: Delegation {
stake: minimum_delegation,
..stake.delegation
},
..*stake
},
*stake_flags,
)),
accounts[0].state()
);
assert_eq!(
Ok(StakeStateV2::Stake(
*meta,
Stake {
delegation: Delegation {
stake: split_amount,
..stake.delegation
},
..*stake
},
*stake_flags,
)),
accounts[1].state()
);
}
}
}
}
}
}
#[test]
fn test_merge() {
let mollusk = mollusk_bpf();
let stake_address = solana_pubkey::new_rand();
let merge_from_address = solana_pubkey::new_rand();
let authorized_address = solana_pubkey::new_rand();
let meta = Meta::auto(&authorized_address);
let stake_lamports = 42;
let mut instruction_accounts = vec![
AccountMeta {
pubkey: stake_address,
is_signer: false,
is_writable: true,
},
AccountMeta {
pubkey: merge_from_address,
is_signer: false,
is_writable: true,
},
AccountMeta {
pubkey: clock::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: StakeHistory::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: authorized_address,
is_signer: true,
is_writable: false,
},
];
for state in &[
StakeStateV2::Initialized(meta),
just_stake(meta, stake_lamports),
] {
let stake_account = AccountSharedData::new_data_with_space(
stake_lamports,
state,
StakeStateV2::size_of(),
&id(),
)
.unwrap();
for merge_from_state in &[
StakeStateV2::Initialized(meta),
just_stake(meta, stake_lamports),
] {
let merge_from_account = AccountSharedData::new_data_with_space(
stake_lamports,
merge_from_state,
StakeStateV2::size_of(),
&id(),
)
.unwrap();
let transaction_accounts = vec![
(stake_address, stake_account.clone()),
(merge_from_address, merge_from_account),
(authorized_address, AccountSharedData::default()),
(
clock::id(),
create_account_shared_data_for_test(&Clock::default()),
),
(
StakeHistory::id(),
create_account_shared_data_for_test(&StakeHistory::default()),
),
(
epoch_schedule::id(),
create_account_shared_data_for_test(&EpochSchedule::default()),
),
];
instruction_accounts[4].is_signer = false;
process_instruction(
&mollusk,
&serialize(&StakeInstruction::Merge).unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Err(ProgramError::MissingRequiredSignature),
);
instruction_accounts[4].is_signer = true;
let accounts = process_instruction(
&mollusk,
&serialize(&StakeInstruction::Merge).unwrap(),
transaction_accounts,
instruction_accounts.clone(),
Ok(()),
);
assert_eq!(accounts[0].lamports(), stake_lamports * 2);
assert_eq!(accounts[1].lamports(), 0);
match state {
StakeStateV2::Initialized(meta) => {
assert_eq!(accounts[0].state(), Ok(StakeStateV2::Initialized(*meta)),);
}
StakeStateV2::Stake(meta, stake, stake_flags) => {
let expected_stake = stake.delegation.stake
+ merge_from_state
.stake()
.map(|stake| stake.delegation.stake)
.unwrap_or_else(|| {
stake_lamports
- merge_from_state.meta().unwrap().rent_exempt_reserve
});
assert_eq!(
accounts[0].state(),
Ok(StakeStateV2::Stake(
*meta,
Stake {
delegation: Delegation {
stake: expected_stake,
..stake.delegation
},
..*stake
},
*stake_flags,
)),
);
}
_ => unreachable!(),
}
assert!(is_closed(&accounts[1]));
}
}
}
#[test]
fn test_merge_self_fails() {
let mollusk = mollusk_bpf();
let stake_address = solana_pubkey::new_rand();
let authorized_address = solana_pubkey::new_rand();
let rent = Rent::default();
let rent_exempt_reserve = rent.minimum_balance(StakeStateV2::size_of());
let stake_amount = 4242424242;
let stake_lamports = rent_exempt_reserve + stake_amount;
let meta = Meta {
rent_exempt_reserve,
..Meta::auto(&authorized_address)
};
let stake = Stake {
delegation: Delegation {
stake: stake_amount,
activation_epoch: 0,
..Delegation::default()
},
..Stake::default()
};
let stake_account = AccountSharedData::new_data_with_space(
stake_lamports,
&StakeStateV2::Stake(meta, stake, StakeFlags::empty()),
StakeStateV2::size_of(),
&id(),
)
.unwrap();
let transaction_accounts = vec![
(stake_address, stake_account),
(authorized_address, AccountSharedData::default()),
(
clock::id(),
create_account_shared_data_for_test(&Clock::default()),
),
(
StakeHistory::id(),
create_account_shared_data_for_test(&StakeHistory::default()),
),
];
let instruction_accounts = vec![
AccountMeta {
pubkey: stake_address,
is_signer: false,
is_writable: true,
},
AccountMeta {
pubkey: stake_address,
is_signer: false,
is_writable: true,
},
AccountMeta {
pubkey: clock::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: StakeHistory::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: authorized_address,
is_signer: true,
is_writable: false,
},
];
process_instruction(
&mollusk,
&serialize(&StakeInstruction::Merge).unwrap(),
transaction_accounts,
instruction_accounts,
Err(ProgramError::InvalidArgument),
);
}
#[test]
fn test_merge_incorrect_authorized_staker() {
let mollusk = mollusk_bpf();
let stake_address = solana_pubkey::new_rand();
let merge_from_address = solana_pubkey::new_rand();
let authorized_address = solana_pubkey::new_rand();
let wrong_authorized_address = solana_pubkey::new_rand();
let stake_lamports = 42;
let mut instruction_accounts = vec![
AccountMeta {
pubkey: stake_address,
is_signer: false,
is_writable: true,
},
AccountMeta {
pubkey: merge_from_address,
is_signer: false,
is_writable: true,
},
AccountMeta {
pubkey: clock::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: StakeHistory::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: authorized_address,
is_signer: true,
is_writable: false,
},
];
for state in &[
StakeStateV2::Initialized(Meta::auto(&authorized_address)),
just_stake(Meta::auto(&authorized_address), stake_lamports),
] {
let stake_account = AccountSharedData::new_data_with_space(
stake_lamports,
state,
StakeStateV2::size_of(),
&id(),
)
.unwrap();
for merge_from_state in &[
StakeStateV2::Initialized(Meta::auto(&wrong_authorized_address)),
just_stake(Meta::auto(&wrong_authorized_address), stake_lamports),
] {
let merge_from_account = AccountSharedData::new_data_with_space(
stake_lamports,
merge_from_state,
StakeStateV2::size_of(),
&id(),
)
.unwrap();
let transaction_accounts = vec![
(stake_address, stake_account.clone()),
(merge_from_address, merge_from_account),
(authorized_address, AccountSharedData::default()),
(wrong_authorized_address, AccountSharedData::default()),
(
clock::id(),
create_account_shared_data_for_test(&Clock::default()),
),
(
StakeHistory::id(),
create_account_shared_data_for_test(&StakeHistory::default()),
),
(
epoch_schedule::id(),
create_account_shared_data_for_test(&EpochSchedule::default()),
),
];
instruction_accounts[4].pubkey = wrong_authorized_address;
process_instruction(
&mollusk,
&serialize(&StakeInstruction::Merge).unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
Err(ProgramError::MissingRequiredSignature),
);
instruction_accounts[4].pubkey = authorized_address;
process_instruction(
&mollusk,
&serialize(&StakeInstruction::Merge).unwrap(),
transaction_accounts,
instruction_accounts.clone(),
Err(StakeError::MergeMismatch.into()),
);
}
}
}
#[test]
fn test_merge_invalid_account_data() {
let mollusk = mollusk_bpf();
let stake_address = solana_pubkey::new_rand();
let merge_from_address = solana_pubkey::new_rand();
let authorized_address = solana_pubkey::new_rand();
let stake_lamports = 42;
let instruction_accounts = vec![
AccountMeta {
pubkey: stake_address,
is_signer: false,
is_writable: true,
},
AccountMeta {
pubkey: merge_from_address,
is_signer: false,
is_writable: true,
},
AccountMeta {
pubkey: clock::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: StakeHistory::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: authorized_address,
is_signer: true,
is_writable: false,
},
];
for state in &[
StakeStateV2::Uninitialized,
StakeStateV2::RewardsPool,
StakeStateV2::Initialized(Meta::auto(&authorized_address)),
just_stake(Meta::auto(&authorized_address), stake_lamports),
] {
let stake_account = AccountSharedData::new_data_with_space(
stake_lamports,
state,
StakeStateV2::size_of(),
&id(),
)
.unwrap();
for merge_from_state in &[StakeStateV2::Uninitialized, StakeStateV2::RewardsPool] {
let merge_from_account = AccountSharedData::new_data_with_space(
stake_lamports,
merge_from_state,
StakeStateV2::size_of(),
&id(),
)
.unwrap();
let transaction_accounts = vec![
(stake_address, stake_account.clone()),
(merge_from_address, merge_from_account),
(authorized_address, AccountSharedData::default()),
(
clock::id(),
create_account_shared_data_for_test(&Clock::default()),
),
(
StakeHistory::id(),
create_account_shared_data_for_test(&StakeHistory::default()),
),
(
epoch_schedule::id(),
create_account_shared_data_for_test(&EpochSchedule::default()),
),
];
process_instruction(
&mollusk,
&serialize(&StakeInstruction::Merge).unwrap(),
transaction_accounts,
instruction_accounts.clone(),
Err(ProgramError::InvalidAccountData),
);
}
}
}
#[test]
fn test_merge_fake_stake_source() {
let mollusk = mollusk_bpf();
let stake_address = solana_pubkey::new_rand();
let merge_from_address = solana_pubkey::new_rand();
let authorized_address = solana_pubkey::new_rand();
let stake_lamports = 42;
let stake_account = AccountSharedData::new_data_with_space(
stake_lamports,
&just_stake(Meta::auto(&authorized_address), stake_lamports),
StakeStateV2::size_of(),
&id(),
)
.unwrap();
let merge_from_account = AccountSharedData::new_data_with_space(
stake_lamports,
&just_stake(Meta::auto(&authorized_address), stake_lamports),
StakeStateV2::size_of(),
&solana_pubkey::new_rand(),
)
.unwrap();
let transaction_accounts = vec![
(stake_address, stake_account),
(merge_from_address, merge_from_account),
(authorized_address, AccountSharedData::default()),
(
clock::id(),
create_account_shared_data_for_test(&Clock::default()),
),
(
StakeHistory::id(),
create_account_shared_data_for_test(&StakeHistory::default()),
),
];
let instruction_accounts = vec![
AccountMeta {
pubkey: stake_address,
is_signer: false,
is_writable: true,
},
AccountMeta {
pubkey: merge_from_address,
is_signer: false,
is_writable: true,
},
AccountMeta {
pubkey: clock::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: StakeHistory::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: authorized_address,
is_signer: true,
is_writable: false,
},
];
process_instruction(
&mollusk,
&serialize(&StakeInstruction::Merge).unwrap(),
transaction_accounts,
instruction_accounts,
Err(ProgramError::InvalidAccountOwner),
);
}
#[test]
fn test_merge_active_stake() {
let mollusk = mollusk_bpf();
let stake_address = solana_pubkey::new_rand();
let merge_from_address = solana_pubkey::new_rand();
let authorized_address = solana_pubkey::new_rand();
let base_lamports = 4242424242;
let rent = Rent::default();
let rent_exempt_reserve = rent.minimum_balance(StakeStateV2::size_of());
let stake_amount = base_lamports;
let stake_lamports = rent_exempt_reserve + stake_amount;
let merge_from_amount = base_lamports;
let merge_from_lamports = rent_exempt_reserve + merge_from_amount;
let meta = Meta {
rent_exempt_reserve,
..Meta::auto(&authorized_address)
};
let mut stake = Stake {
delegation: Delegation {
stake: stake_amount,
activation_epoch: 0,
..Delegation::default()
},
..Stake::default()
};
let stake_account = AccountSharedData::new_data_with_space(
stake_lamports,
&StakeStateV2::Stake(meta, stake, StakeFlags::empty()),
StakeStateV2::size_of(),
&id(),
)
.unwrap();
let merge_from_activation_epoch = 2;
let mut merge_from_stake = Stake {
delegation: Delegation {
stake: merge_from_amount,
activation_epoch: merge_from_activation_epoch,
..stake.delegation
},
..stake
};
let merge_from_account = AccountSharedData::new_data_with_space(
merge_from_lamports,
&StakeStateV2::Stake(meta, merge_from_stake, StakeFlags::empty()),
StakeStateV2::size_of(),
&id(),
)
.unwrap();
let mut clock = Clock::default();
let mut stake_history = StakeHistory::default();
let mut effective = base_lamports;
let mut activating = stake_amount;
let mut deactivating = 0;
stake_history.add(
clock.epoch,
StakeHistoryEntry {
effective,
activating,
deactivating,
},
);
let mut transaction_accounts = vec![
(stake_address, stake_account),
(merge_from_address, merge_from_account),
(authorized_address, AccountSharedData::default()),
(clock::id(), create_account_shared_data_for_test(&clock)),
(
StakeHistory::id(),
create_account_shared_data_for_test(&stake_history),
),
(
epoch_schedule::id(),
create_account_shared_data_for_test(&EpochSchedule::default()),
),
];
let instruction_accounts = vec![
AccountMeta {
pubkey: stake_address,
is_signer: false,
is_writable: true,
},
AccountMeta {
pubkey: merge_from_address,
is_signer: false,
is_writable: true,
},
AccountMeta {
pubkey: clock::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: StakeHistory::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: authorized_address,
is_signer: true,
is_writable: false,
},
];
fn try_merge(
mollusk: &Mollusk,
transaction_accounts: Vec<(Pubkey, AccountSharedData)>,
mut instruction_accounts: Vec<AccountMeta>,
expected_result: Result<(), ProgramError>,
) {
for iteration in 0..2 {
if iteration == 1 {
instruction_accounts.swap(0, 1);
}
let accounts = process_instruction(
mollusk,
&serialize(&StakeInstruction::Merge).unwrap(),
transaction_accounts.clone(),
instruction_accounts.clone(),
expected_result.clone(),
);
if expected_result.is_ok() {
assert!(is_closed(&accounts[1 - iteration]));
}
}
}
try_merge(
&mollusk,
transaction_accounts.clone(),
instruction_accounts.clone(),
Ok(()),
);
let new_warmup_cooldown_rate_epoch = Some(0);
loop {
clock.epoch += 1;
if clock.epoch == merge_from_activation_epoch {
activating += merge_from_amount;
}
let delta = activating.min(
(effective as f64 * warmup_cooldown_rate(clock.epoch, new_warmup_cooldown_rate_epoch))
as u64,
);
effective += delta;
activating -= delta;
stake_history.add(
clock.epoch - 1,
StakeHistoryEntry {
effective,
activating,
deactivating,
},
);
transaction_accounts[3] = (clock::id(), create_account_shared_data_for_test(&clock));
transaction_accounts[4] = (
StakeHistory::id(),
create_account_shared_data_for_test(&stake_history),
);
if stake_amount == stake.stake(clock.epoch, &stake_history, new_warmup_cooldown_rate_epoch)
&& merge_from_amount
== merge_from_stake.stake(
clock.epoch,
&stake_history,
new_warmup_cooldown_rate_epoch,
)
{
break;
}
try_merge(
&mollusk,
transaction_accounts.clone(),
instruction_accounts.clone(),
Err(ProgramError::from(StakeError::MergeTransientStake)),
);
}
try_merge(
&mollusk,
transaction_accounts.clone(),
instruction_accounts.clone(),
Ok(()),
);
let merge_from_deactivation_epoch = clock.epoch + 1;
let stake_deactivation_epoch = clock.epoch + 2;
loop {
clock.epoch += 1;
let delta = deactivating.min(
(effective as f64 * warmup_cooldown_rate(clock.epoch, new_warmup_cooldown_rate_epoch))
as u64,
);
effective -= delta;
deactivating -= delta;
if clock.epoch == stake_deactivation_epoch {
deactivating += stake_amount;
stake = Stake {
delegation: Delegation {
deactivation_epoch: stake_deactivation_epoch,
..stake.delegation
},
..stake
};
transaction_accounts[0]
.1
.set_state(&StakeStateV2::Stake(meta, stake, StakeFlags::empty()))
.unwrap();
}
if clock.epoch == merge_from_deactivation_epoch {
deactivating += merge_from_amount;
merge_from_stake = Stake {
delegation: Delegation {
deactivation_epoch: merge_from_deactivation_epoch,
..merge_from_stake.delegation
},
..merge_from_stake
};
transaction_accounts[1]
.1
.set_state(&StakeStateV2::Stake(
meta,
merge_from_stake,
StakeFlags::empty(),
))
.unwrap();
}
stake_history.add(
clock.epoch - 1,
StakeHistoryEntry {
effective,
activating,
deactivating,
},
);
transaction_accounts[3] = (clock::id(), create_account_shared_data_for_test(&clock));
transaction_accounts[4] = (
StakeHistory::id(),
create_account_shared_data_for_test(&stake_history),
);
if 0 == stake.stake(clock.epoch, &stake_history, new_warmup_cooldown_rate_epoch)
&& 0 == merge_from_stake.stake(
clock.epoch,
&stake_history,
new_warmup_cooldown_rate_epoch,
)
{
break;
}
try_merge(
&mollusk,
transaction_accounts.clone(),
instruction_accounts.clone(),
Err(ProgramError::from(StakeError::MergeTransientStake)),
);
}
try_merge(&mollusk, transaction_accounts, instruction_accounts, Ok(()));
}
#[test]
fn test_stake_get_minimum_delegation() {
let mollusk = mollusk_bpf();
let stake_address = Pubkey::new_unique();
let stake_account = create_default_stake_account();
let minimum_delegation = crate::get_minimum_delegation();
let instruction_data = serialize(&StakeInstruction::GetMinimumDelegation).unwrap();
let transaction_accounts = vec![(stake_address, stake_account)]
.into_iter()
.map(|(key, account)| (key, account.into()))
.collect::<Vec<_>>();
let instruction_accounts = vec![AccountMeta {
pubkey: stake_address,
is_signer: false,
is_writable: true,
}];
let instruction = Instruction {
program_id: id(),
accounts: instruction_accounts,
data: instruction_data,
};
mollusk.process_and_validate_instruction(
&instruction,
&transaction_accounts,
&[
Check::success(),
Check::return_data(&minimum_delegation.to_le_bytes()),
],
);
}
#[test]
fn test_stake_process_instruction_error_ordering() {
let mollusk = mollusk_bpf();
let rent = Rent::default();
let rent_exempt_reserve = rent.minimum_balance(StakeStateV2::size_of());
let rent_address = rent::id();
let rent_account = create_account_shared_data_for_test(&rent);
let good_stake_address = Pubkey::new_unique();
let good_stake_account =
AccountSharedData::new(rent_exempt_reserve, StakeStateV2::size_of(), &id());
let good_instruction = instruction::initialize(
&good_stake_address,
&Authorized::auto(&good_stake_address),
&Lockup::default(),
);
let good_transaction_accounts = vec![
(good_stake_address, good_stake_account),
(rent_address, rent_account),
];
let good_instruction_accounts = vec![
AccountMeta {
pubkey: good_stake_address,
is_signer: false,
is_writable: true,
},
AccountMeta {
pubkey: rent_address,
is_signer: false,
is_writable: false,
},
];
let good_accounts = (good_transaction_accounts, good_instruction_accounts);
let bad_instruction = Instruction::new_with_bincode(id(), &usize::MAX, Vec::default());
let bad_transaction_accounts = Vec::default();
let bad_instruction_accounts = Vec::default();
let bad_accounts = (bad_transaction_accounts, bad_instruction_accounts);
for (instruction, (transaction_accounts, instruction_accounts), expected_result) in [
(&good_instruction, &good_accounts, Ok(())),
(
&bad_instruction,
&good_accounts,
Err(ProgramError::InvalidInstructionData),
),
(
&good_instruction,
&bad_accounts,
Err(ProgramError::NotEnoughAccountKeys),
),
(
&bad_instruction,
&bad_accounts,
Err(ProgramError::InvalidInstructionData),
),
] {
process_instruction(
&mollusk,
&instruction.data,
transaction_accounts.clone(),
instruction_accounts.clone(),
expected_result,
);
}
}
#[test]
fn test_deactivate_delinquent() {
let mollusk = mollusk_bpf();
let reference_vote_address = Pubkey::new_unique();
let vote_address = Pubkey::new_unique();
let stake_address = Pubkey::new_unique();
let initial_stake_state = StakeStateV2::Stake(
Meta::default(),
new_stake(
1,
&vote_address,
&VoteStateV4::default(),
1,
),
StakeFlags::empty(),
);
let stake_account = AccountSharedData::new_data_with_space(
1,
&initial_stake_state,
StakeStateV2::size_of(),
&id(),
)
.unwrap();
let mut vote_account = AccountSharedData::new_data_with_space(
1,
&VoteStateVersions::new_v4(VoteStateV4::default()),
VoteStateV4::size_of(),
&solana_sdk_ids::vote::id(),
)
.unwrap();
let mut reference_vote_account = AccountSharedData::new_data_with_space(
1,
&VoteStateVersions::new_v4(VoteStateV4::default()),
VoteStateV4::size_of(),
&solana_sdk_ids::vote::id(),
)
.unwrap();
let current_epoch = 20;
let process_instruction_deactivate_delinquent =
|stake_address: &Pubkey,
stake_account: &AccountSharedData,
vote_account: &AccountSharedData,
reference_vote_account: &AccountSharedData,
expected_result| {
process_instruction(
&mollusk,
&serialize(&StakeInstruction::DeactivateDelinquent).unwrap(),
vec![
(*stake_address, stake_account.clone()),
(vote_address, vote_account.clone()),
(reference_vote_address, reference_vote_account.clone()),
(
clock::id(),
create_account_shared_data_for_test(&Clock {
epoch: current_epoch,
..Clock::default()
}),
),
(
StakeHistory::id(),
create_account_shared_data_for_test(&StakeHistory::default()),
),
],
vec![
AccountMeta {
pubkey: *stake_address,
is_signer: false,
is_writable: true,
},
AccountMeta {
pubkey: vote_address,
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: reference_vote_address,
is_signer: false,
is_writable: false,
},
],
expected_result,
)
};
process_instruction_deactivate_delinquent(
&stake_address,
&stake_account,
&vote_account,
&reference_vote_account,
Err(StakeError::InsufficientReferenceVotes.into()),
);
let mut reference_vote_state = VoteStateV4::default();
for epoch in 0..MINIMUM_DELINQUENT_EPOCHS_FOR_DEACTIVATION / 2 {
increment_credits(&mut reference_vote_state, epoch as Epoch, 1);
}
reference_vote_account
.serialize_data(&VoteStateVersions::new_v4(reference_vote_state))
.unwrap();
process_instruction_deactivate_delinquent(
&stake_address,
&stake_account,
&vote_account,
&reference_vote_account,
Err(StakeError::InsufficientReferenceVotes.into()),
);
let mut reference_vote_state = VoteStateV4::default();
for epoch in 0..=current_epoch {
increment_credits(&mut reference_vote_state, epoch, 1);
}
assert_eq!(
reference_vote_state.epoch_credits[current_epoch as usize - 2].0,
current_epoch - 2
);
reference_vote_state
.epoch_credits
.remove(current_epoch as usize - 2);
assert_eq!(
reference_vote_state.epoch_credits[current_epoch as usize - 2].0,
current_epoch - 1
);
reference_vote_account
.serialize_data(&VoteStateVersions::new_v4(reference_vote_state))
.unwrap();
process_instruction_deactivate_delinquent(
&stake_address,
&stake_account,
&vote_account,
&reference_vote_account,
Err(StakeError::InsufficientReferenceVotes.into()),
);
let mut reference_vote_state = VoteStateV4::default();
for epoch in 0..=current_epoch {
increment_credits(&mut reference_vote_state, epoch, 1);
}
reference_vote_account
.serialize_data(&VoteStateVersions::new_v4(reference_vote_state))
.unwrap();
let post_stake_account = &process_instruction_deactivate_delinquent(
&stake_address,
&stake_account,
&vote_account,
&reference_vote_account,
Ok(()),
)[0];
assert_eq!(
stake_from(post_stake_account)
.unwrap()
.delegation
.deactivation_epoch,
current_epoch
);
let mut vote_state = VoteStateV4::default();
for epoch in 0..MINIMUM_DELINQUENT_EPOCHS_FOR_DEACTIVATION / 2 {
increment_credits(&mut vote_state, epoch as Epoch, 1);
}
vote_account
.serialize_data(&VoteStateVersions::new_v4(vote_state))
.unwrap();
let post_stake_account = &process_instruction_deactivate_delinquent(
&stake_address,
&stake_account,
&vote_account,
&reference_vote_account,
Ok(()),
)[0];
assert_eq!(
stake_from(post_stake_account)
.unwrap()
.delegation
.deactivation_epoch,
current_epoch
);
let unrelated_vote_address = Pubkey::new_unique();
let unrelated_stake_address = Pubkey::new_unique();
let mut unrelated_stake_account = stake_account.clone();
assert_ne!(unrelated_vote_address, vote_address);
unrelated_stake_account
.serialize_data(&StakeStateV2::Stake(
Meta::default(),
new_stake(
1,
&unrelated_vote_address,
&VoteStateV4::default(),
1,
),
StakeFlags::empty(),
))
.unwrap();
process_instruction_deactivate_delinquent(
&unrelated_stake_address,
&unrelated_stake_account,
&vote_account,
&reference_vote_account,
Err(StakeError::VoteAddressMismatch.into()),
);
let mut vote_state = VoteStateV4::default();
increment_credits(
&mut vote_state,
current_epoch - MINIMUM_DELINQUENT_EPOCHS_FOR_DEACTIVATION as Epoch,
1,
);
vote_account
.serialize_data(&VoteStateVersions::new_v4(vote_state))
.unwrap();
process_instruction_deactivate_delinquent(
&stake_address,
&stake_account,
&vote_account,
&reference_vote_account,
Ok(()),
);
let mut vote_state = VoteStateV4::default();
increment_credits(
&mut vote_state,
current_epoch - (MINIMUM_DELINQUENT_EPOCHS_FOR_DEACTIVATION - 1) as Epoch,
1,
);
vote_account
.serialize_data(&VoteStateVersions::new_v4(vote_state))
.unwrap();
process_instruction_deactivate_delinquent(
&stake_address,
&stake_account,
&vote_account,
&reference_vote_account,
Err(StakeError::MinimumDelinquentEpochsForDeactivationNotMet.into()),
);
}
#[test]
fn test_stake_process_instruction_with_epoch_rewards_active() {
let mollusk = mollusk_bpf();
let process_instruction_as_one_arg = |mollusk: &Mollusk,
instruction: &Instruction,
expected_result: Result<(), ProgramError>|
-> Vec<AccountSharedData> {
let mut transaction_accounts = get_default_transaction_accounts(instruction);
let epoch_rewards_sysvar = EpochRewards {
active: true,
..EpochRewards::default()
};
transaction_accounts.push((
epoch_rewards::id(),
create_account_shared_data_for_test(&epoch_rewards_sysvar),
));
process_instruction(
mollusk,
&instruction.data,
transaction_accounts,
instruction.accounts.clone(),
expected_result,
)
};
process_instruction_as_one_arg(
&mollusk,
&instruction::initialize(
&Pubkey::new_unique(),
&Authorized::default(),
&Lockup::default(),
),
Err(StakeError::EpochRewardsActive.into()),
);
process_instruction_as_one_arg(
&mollusk,
&instruction::authorize(
&Pubkey::new_unique(),
&Pubkey::new_unique(),
&Pubkey::new_unique(),
StakeAuthorize::Staker,
None,
),
Err(StakeError::EpochRewardsActive.into()),
);
process_instruction_as_one_arg(
&mollusk,
&instruction::delegate_stake(
&Pubkey::new_unique(),
&Pubkey::new_unique(),
&invalid_vote_state_pubkey(),
),
Err(StakeError::EpochRewardsActive.into()),
);
process_instruction_as_one_arg(
&mollusk,
&instruction::split(
&Pubkey::new_unique(),
&Pubkey::new_unique(),
100,
&invalid_stake_state_pubkey(),
)[2],
Err(StakeError::EpochRewardsActive.into()),
);
process_instruction_as_one_arg(
&mollusk,
&instruction::withdraw(
&Pubkey::new_unique(),
&Pubkey::new_unique(),
&Pubkey::new_unique(),
100,
None,
),
Err(StakeError::EpochRewardsActive.into()),
);
process_instruction_as_one_arg(
&mollusk,
&instruction::deactivate_stake(&Pubkey::new_unique(), &Pubkey::new_unique()),
Err(StakeError::EpochRewardsActive.into()),
);
process_instruction_as_one_arg(
&mollusk,
&instruction::set_lockup(
&Pubkey::new_unique(),
&LockupArgs::default(),
&Pubkey::new_unique(),
),
Err(StakeError::EpochRewardsActive.into()),
);
process_instruction_as_one_arg(
&mollusk,
&instruction::merge(
&Pubkey::new_unique(),
&invalid_stake_state_pubkey(),
&Pubkey::new_unique(),
)[0],
Err(StakeError::EpochRewardsActive.into()),
);
process_instruction_as_one_arg(
&mollusk,
&instruction::authorize_with_seed(
&Pubkey::new_unique(),
&Pubkey::new_unique(),
"seed".to_string(),
&Pubkey::new_unique(),
&Pubkey::new_unique(),
StakeAuthorize::Staker,
None,
),
Err(StakeError::EpochRewardsActive.into()),
);
process_instruction_as_one_arg(
&mollusk,
&instruction::initialize_checked(&Pubkey::new_unique(), &Authorized::default()),
Err(StakeError::EpochRewardsActive.into()),
);
process_instruction_as_one_arg(
&mollusk,
&instruction::authorize_checked(
&Pubkey::new_unique(),
&Pubkey::new_unique(),
&Pubkey::new_unique(),
StakeAuthorize::Staker,
None,
),
Err(StakeError::EpochRewardsActive.into()),
);
process_instruction_as_one_arg(
&mollusk,
&instruction::authorize_checked_with_seed(
&Pubkey::new_unique(),
&Pubkey::new_unique(),
"seed".to_string(),
&Pubkey::new_unique(),
&Pubkey::new_unique(),
StakeAuthorize::Staker,
None,
),
Err(StakeError::EpochRewardsActive.into()),
);
process_instruction_as_one_arg(
&mollusk,
&instruction::set_lockup_checked(
&Pubkey::new_unique(),
&LockupArgs::default(),
&Pubkey::new_unique(),
),
Err(StakeError::EpochRewardsActive.into()),
);
process_instruction_as_one_arg(
&mollusk,
&instruction::deactivate_delinquent_stake(
&Pubkey::new_unique(),
&invalid_vote_state_pubkey(),
&Pubkey::new_unique(),
),
Err(StakeError::EpochRewardsActive.into()),
);
process_instruction_as_one_arg(
&mollusk,
&instruction::move_stake(
&Pubkey::new_unique(),
&Pubkey::new_unique(),
&Pubkey::new_unique(),
100,
),
Err(StakeError::EpochRewardsActive.into()),
);
process_instruction_as_one_arg(
&mollusk,
&instruction::move_lamports(
&Pubkey::new_unique(),
&Pubkey::new_unique(),
&Pubkey::new_unique(),
100,
),
Err(StakeError::EpochRewardsActive.into()),
);
process_instruction_as_one_arg(&mollusk, &instruction::get_minimum_delegation(), Ok(()));
}
#[derive(Debug, Clone, Copy)]
enum VoteStateVersion {
V0_23_5,
V1_14_11,
V3,
V4,
}
impl VoteStateVersion {
fn default_vote_state(&self) -> VoteStateVersions {
match self {
Self::V0_23_5 => VoteStateVersions::V0_23_5(Box::default()),
Self::V1_14_11 => VoteStateVersions::V1_14_11(Box::default()),
Self::V3 => VoteStateVersions::V3(Box::default()),
Self::V4 => VoteStateVersions::V4(Box::default()),
}
}
}
fn setup_delegate_test_with_vote_account(
vote_account: AccountSharedData,
) -> (Mollusk, Vec<AccountMeta>, Vec<(Pubkey, AccountSharedData)>) {
let mollusk = mollusk_bpf();
let vote_address = Pubkey::new_unique();
let stake_address = Pubkey::new_unique();
let minimum_delegation = crate::get_minimum_delegation();
let stake_account = AccountSharedData::new_data_with_space(
minimum_delegation + default_stake_rent(),
&StakeStateV2::Initialized(Meta {
authorized: Authorized {
staker: stake_address,
withdrawer: stake_address,
},
..Meta::default()
}),
StakeStateV2::size_of(),
&id(),
)
.unwrap();
let clock = Clock {
epoch: 1,
..Clock::default()
};
#[allow(deprecated)]
let transaction_accounts = vec![
(stake_address, stake_account),
(vote_address, vote_account),
(clock::id(), create_account_shared_data_for_test(&clock)),
(StakeHistory::id(), create_empty_stake_history_for_test()),
(
stake_config::id(),
config::create_account(0, &stake_config::Config::default()),
),
];
#[allow(deprecated)]
let instruction_accounts = vec![
AccountMeta {
pubkey: stake_address,
is_signer: true,
is_writable: true,
},
AccountMeta {
pubkey: vote_address,
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: clock::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: StakeHistory::id(),
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: stake_config::id(),
is_signer: false,
is_writable: false,
},
];
(mollusk, instruction_accounts, transaction_accounts)
}
fn setup_deactivate_delinquent_test_with_vote_account(
vote_account: AccountSharedData,
) -> (Mollusk, Vec<AccountMeta>, Vec<(Pubkey, AccountSharedData)>) {
let mollusk = mollusk_bpf();
let reference_vote_address = Pubkey::new_unique();
let vote_address = Pubkey::new_unique();
let stake_address = Pubkey::new_unique();
let initial_stake_state = StakeStateV2::Stake(
Meta::default(),
new_stake(
1,
&vote_address,
&VoteStateV4::default(),
1,
),
StakeFlags::empty(),
);
let stake_account = AccountSharedData::new_data_with_space(
1,
&initial_stake_state,
StakeStateV2::size_of(),
&id(),
)
.unwrap();
let mut reference_vote_state = VoteStateV4::default();
let current_epoch = 20;
for epoch in 0..=current_epoch {
increment_credits(&mut reference_vote_state, epoch, 1);
}
let reference_vote_account = AccountSharedData::new_data_with_space(
Rent::default().minimum_balance(VoteStateV4::size_of()),
&VoteStateVersions::new_v4(reference_vote_state),
VoteStateV4::size_of(),
&solana_sdk_ids::vote::id(),
)
.unwrap();
let transaction_accounts = vec![
(stake_address, stake_account),
(vote_address, vote_account),
(reference_vote_address, reference_vote_account),
(
clock::id(),
create_account_shared_data_for_test(&Clock {
epoch: current_epoch,
..Clock::default()
}),
),
];
let instruction_accounts = vec![
AccountMeta {
pubkey: stake_address,
is_signer: false,
is_writable: true,
},
AccountMeta {
pubkey: vote_address,
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: reference_vote_address,
is_signer: false,
is_writable: false,
},
];
(mollusk, instruction_accounts, transaction_accounts)
}
#[test]
fn test_delegate_incorrect_vote_owner() {
let wrong_owner = Pubkey::new_unique();
let vote_state = VoteStateVersions::new_v4(VoteStateV4::default());
let vote_account = AccountSharedData::new_data_with_space(
Rent::default().minimum_balance(VoteStateV4::size_of()),
&vote_state,
VoteStateV4::size_of(),
&wrong_owner,
)
.unwrap();
let (mollusk, instruction_accounts, transaction_accounts) =
setup_delegate_test_with_vote_account(vote_account);
process_instruction(
&mollusk,
&serialize(&StakeInstruction::DelegateStake).unwrap(),
transaction_accounts,
instruction_accounts,
Err(ProgramError::IncorrectProgramId), );
}
#[test_case(VoteStateVersion::V0_23_5, Err(ProgramError::InvalidAccountData))]
#[test_case(VoteStateVersion::V1_14_11, Ok(()))]
#[test_case(VoteStateVersion::V3, Ok(()))]
#[test_case(VoteStateVersion::V4, Ok(()))]
fn test_delegate_deserialize_vote_state(
vote_state_version: VoteStateVersion,
expected_result: Result<(), ProgramError>,
) {
let vote_state = vote_state_version.default_vote_state();
let vote_account = AccountSharedData::new_data_with_space(
Rent::default().minimum_balance(VoteStateV4::size_of()),
&vote_state,
VoteStateV4::size_of(),
&solana_sdk_ids::vote::id(),
)
.unwrap();
let (mollusk, instruction_accounts, transaction_accounts) =
setup_delegate_test_with_vote_account(vote_account);
process_instruction(
&mollusk,
&serialize(&StakeInstruction::DelegateStake).unwrap(),
transaction_accounts,
instruction_accounts,
expected_result,
);
}
#[test]
fn test_deactivate_delinquent_incorrect_vote_owner() {
let wrong_owner = Pubkey::new_unique();
let vote_state = VoteStateVersions::new_v4(VoteStateV4::default());
let vote_account = AccountSharedData::new_data_with_space(
Rent::default().minimum_balance(VoteStateV4::size_of()),
&vote_state,
VoteStateV4::size_of(),
&wrong_owner,
)
.unwrap();
let (mollusk, instruction_accounts, transaction_accounts) =
setup_deactivate_delinquent_test_with_vote_account(vote_account);
process_instruction(
&mollusk,
&serialize(&StakeInstruction::DeactivateDelinquent).unwrap(),
transaction_accounts,
instruction_accounts,
Err(ProgramError::IncorrectProgramId), );
}
#[test_case(VoteStateVersion::V0_23_5, Err(ProgramError::InvalidAccountData))]
#[test_case(VoteStateVersion::V1_14_11, Ok(()))]
#[test_case(VoteStateVersion::V3, Ok(()))]
#[test_case(VoteStateVersion::V4, Ok(()))]
fn test_deactivate_delinquent_deserialize_vote_state(
vote_state_version: VoteStateVersion,
expected_result: Result<(), ProgramError>,
) {
let vote_state = vote_state_version.default_vote_state();
let vote_account = AccountSharedData::new_data_with_space(
Rent::default().minimum_balance(VoteStateV4::size_of()),
&vote_state,
VoteStateV4::size_of(),
&solana_sdk_ids::vote::id(),
)
.unwrap();
let (mollusk, instruction_accounts, transaction_accounts) =
setup_deactivate_delinquent_test_with_vote_account(vote_account);
process_instruction(
&mollusk,
&serialize(&StakeInstruction::DeactivateDelinquent).unwrap(),
transaction_accounts,
instruction_accounts,
expected_result,
);
}