2021-06-15 09:04:00 -07:00
|
|
|
use {
|
2022-04-06 13:58:09 -07:00
|
|
|
crate::{
|
|
|
|
config,
|
|
|
|
stake_state::{
|
2022-03-25 09:11:51 -07:00
|
|
|
authorize, authorize_with_seed, deactivate, deactivate_delinquent, delegate,
|
2022-06-28 10:39:00 -07:00
|
|
|
initialize, merge, redelegate, set_lockup, split, withdraw,
|
2022-04-06 13:58:09 -07:00
|
|
|
},
|
|
|
|
},
|
2021-06-15 09:04:00 -07:00
|
|
|
log::*,
|
2022-02-03 04:03:50 -08:00
|
|
|
solana_program_runtime::{
|
2023-04-05 06:50:34 -07:00
|
|
|
declare_process_instruction, invoke_context::InvokeContext,
|
|
|
|
sysvar_cache::get_sysvar_with_account_check,
|
2022-02-03 04:03:50 -08:00
|
|
|
},
|
2021-06-15 09:04:00 -07:00
|
|
|
solana_sdk::{
|
2023-04-03 08:23:24 -07:00
|
|
|
clock::Clock,
|
2021-06-15 09:04:00 -07:00
|
|
|
feature_set,
|
|
|
|
instruction::InstructionError,
|
|
|
|
program_utils::limited_deserialize,
|
2022-04-10 00:55:37 -07:00
|
|
|
pubkey::Pubkey,
|
2021-06-30 17:34:50 -07:00
|
|
|
stake::{
|
2022-04-06 13:58:09 -07:00
|
|
|
instruction::{LockupArgs, StakeInstruction},
|
2021-06-30 17:34:50 -07:00
|
|
|
program::id,
|
|
|
|
state::{Authorized, Lockup},
|
|
|
|
},
|
2022-09-06 02:31:40 -07:00
|
|
|
transaction_context::{IndexOfAccount, InstructionContext, TransactionContext},
|
2021-06-15 09:04:00 -07:00
|
|
|
},
|
2019-08-15 14:35:48 -07:00
|
|
|
};
|
2020-08-21 11:28:01 -07:00
|
|
|
|
2022-04-10 00:55:37 -07:00
|
|
|
fn get_optional_pubkey<'a>(
|
|
|
|
transaction_context: &'a TransactionContext,
|
|
|
|
instruction_context: &'a InstructionContext,
|
2022-09-06 02:31:40 -07:00
|
|
|
instruction_account_index: IndexOfAccount,
|
2022-04-10 00:55:37 -07:00
|
|
|
should_be_signer: bool,
|
|
|
|
) -> Result<Option<&'a Pubkey>, InstructionError> {
|
|
|
|
Ok(
|
2022-06-16 09:46:17 -07:00
|
|
|
if instruction_account_index < instruction_context.get_number_of_instruction_accounts() {
|
|
|
|
if should_be_signer
|
|
|
|
&& !instruction_context.is_instruction_account_signer(instruction_account_index)?
|
|
|
|
{
|
2022-04-10 00:55:37 -07:00
|
|
|
return Err(InstructionError::MissingRequiredSignature);
|
|
|
|
}
|
2022-06-16 09:46:17 -07:00
|
|
|
Some(
|
|
|
|
transaction_context.get_key_of_account_at_index(
|
|
|
|
instruction_context.get_index_of_instruction_account_in_transaction(
|
|
|
|
instruction_account_index,
|
|
|
|
)?,
|
|
|
|
)?,
|
|
|
|
)
|
2022-04-10 00:55:37 -07:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-04-05 06:50:34 -07:00
|
|
|
declare_process_instruction!(750);
|
|
|
|
pub fn process_instruction_inner(
|
|
|
|
invoke_context: &mut InvokeContext,
|
|
|
|
) -> Result<(), InstructionError> {
|
2022-03-24 04:57:51 -07:00
|
|
|
let transaction_context = &invoke_context.transaction_context;
|
|
|
|
let instruction_context = transaction_context.get_current_instruction_context()?;
|
2022-04-01 06:48:05 -07:00
|
|
|
let data = instruction_context.get_instruction_data();
|
2021-04-19 09:48:48 -07:00
|
|
|
|
2019-04-01 16:45:53 -07:00
|
|
|
trace!("process_instruction: {:?}", data);
|
|
|
|
|
2022-04-11 14:26:36 -07:00
|
|
|
let get_stake_account = || {
|
|
|
|
let me = instruction_context.try_borrow_instruction_account(transaction_context, 0)?;
|
|
|
|
if *me.get_owner() != id() {
|
|
|
|
return Err(InstructionError::InvalidAccountOwner);
|
|
|
|
}
|
|
|
|
Ok(me)
|
|
|
|
};
|
2020-12-22 10:20:38 -08:00
|
|
|
|
2022-09-05 07:29:02 -07:00
|
|
|
let signers = instruction_context.get_signers(transaction_context)?;
|
2022-04-11 14:26:36 -07:00
|
|
|
match limited_deserialize(data) {
|
|
|
|
Ok(StakeInstruction::Initialize(authorized, lockup)) => {
|
|
|
|
let mut me = get_stake_account()?;
|
2022-03-24 11:30:42 -07:00
|
|
|
let rent = get_sysvar_with_account_check::rent(invoke_context, instruction_context, 1)?;
|
2022-04-10 00:55:37 -07:00
|
|
|
initialize(
|
|
|
|
&mut me,
|
|
|
|
&authorized,
|
|
|
|
&lockup,
|
|
|
|
&rent,
|
|
|
|
&invoke_context.feature_set,
|
|
|
|
)
|
2022-02-03 04:03:50 -08:00
|
|
|
}
|
2022-04-11 14:26:36 -07:00
|
|
|
Ok(StakeInstruction::Authorize(authorized_pubkey, stake_authorize)) => {
|
|
|
|
let mut me = get_stake_account()?;
|
2021-12-03 03:15:22 -08:00
|
|
|
let require_custodian_for_locked_stake_authorize = invoke_context
|
|
|
|
.feature_set
|
|
|
|
.is_active(&feature_set::require_custodian_for_locked_stake_authorize::id());
|
2021-02-05 22:40:07 -08:00
|
|
|
|
|
|
|
if require_custodian_for_locked_stake_authorize {
|
2022-03-24 11:30:42 -07:00
|
|
|
let clock =
|
|
|
|
get_sysvar_with_account_check::clock(invoke_context, instruction_context, 1)?;
|
2022-04-08 03:43:55 -07:00
|
|
|
instruction_context.check_number_of_instruction_accounts(3)?;
|
2022-06-16 09:46:17 -07:00
|
|
|
let custodian_pubkey =
|
|
|
|
get_optional_pubkey(transaction_context, instruction_context, 3, false)?;
|
2021-02-05 22:40:07 -08:00
|
|
|
|
2022-04-06 13:58:09 -07:00
|
|
|
authorize(
|
2022-04-10 00:55:37 -07:00
|
|
|
&mut me,
|
2021-02-05 22:40:07 -08:00
|
|
|
&signers,
|
|
|
|
&authorized_pubkey,
|
|
|
|
stake_authorize,
|
|
|
|
require_custodian_for_locked_stake_authorize,
|
|
|
|
&clock,
|
2022-04-10 00:55:37 -07:00
|
|
|
custodian_pubkey,
|
2021-02-05 22:40:07 -08:00
|
|
|
)
|
|
|
|
} else {
|
2022-04-06 13:58:09 -07:00
|
|
|
authorize(
|
2022-04-10 00:55:37 -07:00
|
|
|
&mut me,
|
2021-02-05 22:40:07 -08:00
|
|
|
&signers,
|
|
|
|
&authorized_pubkey,
|
|
|
|
stake_authorize,
|
|
|
|
require_custodian_for_locked_stake_authorize,
|
|
|
|
&Clock::default(),
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
}
|
2020-08-21 11:28:01 -07:00
|
|
|
}
|
2022-04-11 14:26:36 -07:00
|
|
|
Ok(StakeInstruction::AuthorizeWithSeed(args)) => {
|
|
|
|
let mut me = get_stake_account()?;
|
2022-03-29 10:06:50 -07:00
|
|
|
instruction_context.check_number_of_instruction_accounts(2)?;
|
2021-12-03 03:15:22 -08:00
|
|
|
let require_custodian_for_locked_stake_authorize = invoke_context
|
|
|
|
.feature_set
|
|
|
|
.is_active(&feature_set::require_custodian_for_locked_stake_authorize::id());
|
2021-02-05 22:40:07 -08:00
|
|
|
if require_custodian_for_locked_stake_authorize {
|
2022-03-24 11:30:42 -07:00
|
|
|
let clock =
|
|
|
|
get_sysvar_with_account_check::clock(invoke_context, instruction_context, 2)?;
|
2022-06-16 09:46:17 -07:00
|
|
|
let custodian_pubkey =
|
|
|
|
get_optional_pubkey(transaction_context, instruction_context, 3, false)?;
|
2021-02-05 22:40:07 -08:00
|
|
|
|
2022-04-06 13:58:09 -07:00
|
|
|
authorize_with_seed(
|
2022-04-10 00:55:37 -07:00
|
|
|
transaction_context,
|
|
|
|
instruction_context,
|
|
|
|
&mut me,
|
2022-06-16 09:46:17 -07:00
|
|
|
1,
|
2021-02-05 22:40:07 -08:00
|
|
|
&args.authority_seed,
|
|
|
|
&args.authority_owner,
|
|
|
|
&args.new_authorized_pubkey,
|
|
|
|
args.stake_authorize,
|
|
|
|
require_custodian_for_locked_stake_authorize,
|
|
|
|
&clock,
|
2022-04-10 00:55:37 -07:00
|
|
|
custodian_pubkey,
|
2021-02-05 22:40:07 -08:00
|
|
|
)
|
|
|
|
} else {
|
2022-04-06 13:58:09 -07:00
|
|
|
authorize_with_seed(
|
2022-04-10 00:55:37 -07:00
|
|
|
transaction_context,
|
|
|
|
instruction_context,
|
|
|
|
&mut me,
|
2022-06-16 09:46:17 -07:00
|
|
|
1,
|
2021-02-05 22:40:07 -08:00
|
|
|
&args.authority_seed,
|
|
|
|
&args.authority_owner,
|
|
|
|
&args.new_authorized_pubkey,
|
|
|
|
args.stake_authorize,
|
|
|
|
require_custodian_for_locked_stake_authorize,
|
|
|
|
&Clock::default(),
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
}
|
2020-04-21 21:05:49 -07:00
|
|
|
}
|
2022-04-11 14:26:36 -07:00
|
|
|
Ok(StakeInstruction::DelegateStake) => {
|
|
|
|
let me = get_stake_account()?;
|
2022-03-29 10:06:50 -07:00
|
|
|
instruction_context.check_number_of_instruction_accounts(2)?;
|
2022-03-24 11:30:42 -07:00
|
|
|
let clock =
|
|
|
|
get_sysvar_with_account_check::clock(invoke_context, instruction_context, 2)?;
|
2022-03-16 03:30:01 -07:00
|
|
|
let stake_history = get_sysvar_with_account_check::stake_history(
|
2022-02-03 04:03:50 -08:00
|
|
|
invoke_context,
|
2022-03-24 11:30:42 -07:00
|
|
|
instruction_context,
|
|
|
|
3,
|
2022-02-03 04:03:50 -08:00
|
|
|
)?;
|
2022-03-29 10:06:50 -07:00
|
|
|
instruction_context.check_number_of_instruction_accounts(5)?;
|
2022-04-10 00:55:37 -07:00
|
|
|
drop(me);
|
2022-03-16 03:30:01 -07:00
|
|
|
let config_account =
|
2022-04-10 00:55:37 -07:00
|
|
|
instruction_context.try_borrow_instruction_account(transaction_context, 4)?;
|
|
|
|
if !config::check_id(config_account.get_key()) {
|
2022-02-25 12:05:05 -08:00
|
|
|
return Err(InstructionError::InvalidArgument);
|
|
|
|
}
|
2022-04-10 00:55:37 -07:00
|
|
|
let config = config::from(&config_account).ok_or(InstructionError::InvalidArgument)?;
|
|
|
|
drop(config_account);
|
|
|
|
delegate(
|
2022-06-28 10:39:00 -07:00
|
|
|
invoke_context,
|
2022-04-10 00:55:37 -07:00
|
|
|
transaction_context,
|
|
|
|
instruction_context,
|
2022-06-16 09:46:17 -07:00
|
|
|
0,
|
|
|
|
1,
|
2022-04-10 00:55:37 -07:00
|
|
|
&clock,
|
|
|
|
&stake_history,
|
|
|
|
&config,
|
|
|
|
&signers,
|
2022-05-04 03:17:29 -07:00
|
|
|
&invoke_context.feature_set,
|
2022-04-10 00:55:37 -07:00
|
|
|
)
|
2019-04-01 16:45:53 -07:00
|
|
|
}
|
2022-04-11 14:26:36 -07:00
|
|
|
Ok(StakeInstruction::Split(lamports)) => {
|
|
|
|
let me = get_stake_account()?;
|
2022-03-29 10:06:50 -07:00
|
|
|
instruction_context.check_number_of_instruction_accounts(2)?;
|
2022-04-10 00:55:37 -07:00
|
|
|
drop(me);
|
|
|
|
split(
|
|
|
|
invoke_context,
|
|
|
|
transaction_context,
|
|
|
|
instruction_context,
|
2022-06-16 09:46:17 -07:00
|
|
|
0,
|
2022-04-10 00:55:37 -07:00
|
|
|
lamports,
|
2022-06-16 09:46:17 -07:00
|
|
|
1,
|
2022-04-10 00:55:37 -07:00
|
|
|
&signers,
|
|
|
|
)
|
2019-10-31 11:07:27 -07:00
|
|
|
}
|
2022-04-11 14:26:36 -07:00
|
|
|
Ok(StakeInstruction::Merge) => {
|
|
|
|
let me = get_stake_account()?;
|
2022-03-29 10:06:50 -07:00
|
|
|
instruction_context.check_number_of_instruction_accounts(2)?;
|
2022-03-24 11:30:42 -07:00
|
|
|
let clock =
|
|
|
|
get_sysvar_with_account_check::clock(invoke_context, instruction_context, 2)?;
|
2022-03-16 03:30:01 -07:00
|
|
|
let stake_history = get_sysvar_with_account_check::stake_history(
|
2022-02-03 04:03:50 -08:00
|
|
|
invoke_context,
|
2022-03-24 11:30:42 -07:00
|
|
|
instruction_context,
|
|
|
|
3,
|
2022-02-03 04:03:50 -08:00
|
|
|
)?;
|
2022-04-10 00:55:37 -07:00
|
|
|
drop(me);
|
2022-04-06 13:58:09 -07:00
|
|
|
merge(
|
2021-01-21 09:59:24 -08:00
|
|
|
invoke_context,
|
2022-04-10 00:55:37 -07:00
|
|
|
transaction_context,
|
|
|
|
instruction_context,
|
2022-06-16 09:46:17 -07:00
|
|
|
0,
|
|
|
|
1,
|
2022-02-03 04:03:50 -08:00
|
|
|
&clock,
|
|
|
|
&stake_history,
|
2020-06-10 17:22:47 -07:00
|
|
|
&signers,
|
|
|
|
)
|
|
|
|
}
|
2022-04-11 14:26:36 -07:00
|
|
|
Ok(StakeInstruction::Withdraw(lamports)) => {
|
|
|
|
let me = get_stake_account()?;
|
2022-03-29 10:06:50 -07:00
|
|
|
instruction_context.check_number_of_instruction_accounts(2)?;
|
2022-03-24 11:30:42 -07:00
|
|
|
let clock =
|
|
|
|
get_sysvar_with_account_check::clock(invoke_context, instruction_context, 2)?;
|
2022-03-16 03:30:01 -07:00
|
|
|
let stake_history = get_sysvar_with_account_check::stake_history(
|
2022-02-03 04:03:50 -08:00
|
|
|
invoke_context,
|
2022-03-24 11:30:42 -07:00
|
|
|
instruction_context,
|
|
|
|
3,
|
2022-02-03 04:03:50 -08:00
|
|
|
)?;
|
2022-03-29 10:06:50 -07:00
|
|
|
instruction_context.check_number_of_instruction_accounts(5)?;
|
2022-04-10 00:55:37 -07:00
|
|
|
drop(me);
|
2022-04-06 13:58:09 -07:00
|
|
|
withdraw(
|
2022-04-10 00:55:37 -07:00
|
|
|
transaction_context,
|
|
|
|
instruction_context,
|
2022-06-16 09:46:17 -07:00
|
|
|
0,
|
2019-06-21 22:28:34 -07:00
|
|
|
lamports,
|
2022-06-16 09:46:17 -07:00
|
|
|
1,
|
2022-02-03 04:03:50 -08:00
|
|
|
&clock,
|
|
|
|
&stake_history,
|
2022-06-16 09:46:17 -07:00
|
|
|
4,
|
2022-04-10 00:55:37 -07:00
|
|
|
if instruction_context.get_number_of_instruction_accounts() >= 6 {
|
2022-06-16 09:46:17 -07:00
|
|
|
Some(5)
|
2022-04-10 00:55:37 -07:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
},
|
2022-04-01 22:11:10 -07:00
|
|
|
&invoke_context.feature_set,
|
2019-06-21 22:28:34 -07:00
|
|
|
)
|
|
|
|
}
|
2022-04-11 14:26:36 -07:00
|
|
|
Ok(StakeInstruction::Deactivate) => {
|
|
|
|
let mut me = get_stake_account()?;
|
2022-03-24 11:30:42 -07:00
|
|
|
let clock =
|
|
|
|
get_sysvar_with_account_check::clock(invoke_context, instruction_context, 1)?;
|
2022-04-10 00:55:37 -07:00
|
|
|
deactivate(&mut me, &clock, &signers)
|
2022-02-03 04:03:50 -08:00
|
|
|
}
|
2022-04-11 14:26:36 -07:00
|
|
|
Ok(StakeInstruction::SetLockup(lockup)) => {
|
|
|
|
let mut me = get_stake_account()?;
|
2022-01-21 17:27:53 -08:00
|
|
|
let clock = invoke_context.get_sysvar_cache().get_clock()?;
|
2022-04-10 00:55:37 -07:00
|
|
|
set_lockup(&mut me, &lockup, &signers, &clock)
|
2021-05-20 14:04:07 -07:00
|
|
|
}
|
2022-04-11 14:26:36 -07:00
|
|
|
Ok(StakeInstruction::InitializeChecked) => {
|
|
|
|
let mut me = get_stake_account()?;
|
2021-12-03 03:15:22 -08:00
|
|
|
if invoke_context
|
|
|
|
.feature_set
|
|
|
|
.is_active(&feature_set::vote_stake_checked_instructions::id())
|
2021-06-30 17:34:50 -07:00
|
|
|
{
|
2022-03-29 10:06:50 -07:00
|
|
|
instruction_context.check_number_of_instruction_accounts(4)?;
|
2022-04-10 00:55:37 -07:00
|
|
|
let staker_pubkey = transaction_context.get_key_of_account_at_index(
|
2022-06-16 09:46:17 -07:00
|
|
|
instruction_context.get_index_of_instruction_account_in_transaction(2)?,
|
2022-04-10 00:55:37 -07:00
|
|
|
)?;
|
|
|
|
let withdrawer_pubkey = transaction_context.get_key_of_account_at_index(
|
2022-06-16 09:46:17 -07:00
|
|
|
instruction_context.get_index_of_instruction_account_in_transaction(3)?,
|
2022-04-10 00:55:37 -07:00
|
|
|
)?;
|
2022-06-16 09:46:17 -07:00
|
|
|
if !instruction_context.is_instruction_account_signer(3)? {
|
2022-04-10 00:55:37 -07:00
|
|
|
return Err(InstructionError::MissingRequiredSignature);
|
|
|
|
}
|
|
|
|
|
2021-06-30 17:34:50 -07:00
|
|
|
let authorized = Authorized {
|
2022-04-10 00:55:37 -07:00
|
|
|
staker: *staker_pubkey,
|
|
|
|
withdrawer: *withdrawer_pubkey,
|
2021-06-30 17:34:50 -07:00
|
|
|
};
|
|
|
|
|
2022-03-24 11:30:42 -07:00
|
|
|
let rent =
|
|
|
|
get_sysvar_with_account_check::rent(invoke_context, instruction_context, 1)?;
|
2022-04-06 13:58:09 -07:00
|
|
|
initialize(
|
2022-04-10 00:55:37 -07:00
|
|
|
&mut me,
|
2022-04-01 22:11:10 -07:00
|
|
|
&authorized,
|
|
|
|
&Lockup::default(),
|
|
|
|
&rent,
|
|
|
|
&invoke_context.feature_set,
|
|
|
|
)
|
2021-06-30 17:34:50 -07:00
|
|
|
} else {
|
|
|
|
Err(InstructionError::InvalidInstructionData)
|
|
|
|
}
|
|
|
|
}
|
2022-04-11 14:26:36 -07:00
|
|
|
Ok(StakeInstruction::AuthorizeChecked(stake_authorize)) => {
|
|
|
|
let mut me = get_stake_account()?;
|
2021-12-03 03:15:22 -08:00
|
|
|
if invoke_context
|
|
|
|
.feature_set
|
|
|
|
.is_active(&feature_set::vote_stake_checked_instructions::id())
|
2021-06-30 17:34:50 -07:00
|
|
|
{
|
2022-03-24 11:30:42 -07:00
|
|
|
let clock =
|
|
|
|
get_sysvar_with_account_check::clock(invoke_context, instruction_context, 1)?;
|
2022-03-29 10:06:50 -07:00
|
|
|
instruction_context.check_number_of_instruction_accounts(4)?;
|
2022-04-10 00:55:37 -07:00
|
|
|
let authorized_pubkey = transaction_context.get_key_of_account_at_index(
|
2022-06-16 09:46:17 -07:00
|
|
|
instruction_context.get_index_of_instruction_account_in_transaction(3)?,
|
2022-04-10 00:55:37 -07:00
|
|
|
)?;
|
2022-06-16 09:46:17 -07:00
|
|
|
if !instruction_context.is_instruction_account_signer(3)? {
|
2022-04-10 00:55:37 -07:00
|
|
|
return Err(InstructionError::MissingRequiredSignature);
|
|
|
|
}
|
2022-06-16 09:46:17 -07:00
|
|
|
let custodian_pubkey =
|
|
|
|
get_optional_pubkey(transaction_context, instruction_context, 4, false)?;
|
2021-06-30 17:34:50 -07:00
|
|
|
|
2022-04-06 13:58:09 -07:00
|
|
|
authorize(
|
2022-04-10 00:55:37 -07:00
|
|
|
&mut me,
|
2021-06-30 17:34:50 -07:00
|
|
|
&signers,
|
|
|
|
authorized_pubkey,
|
|
|
|
stake_authorize,
|
|
|
|
true,
|
|
|
|
&clock,
|
2022-04-10 00:55:37 -07:00
|
|
|
custodian_pubkey,
|
2021-06-30 17:34:50 -07:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
Err(InstructionError::InvalidInstructionData)
|
|
|
|
}
|
|
|
|
}
|
2022-04-11 14:26:36 -07:00
|
|
|
Ok(StakeInstruction::AuthorizeCheckedWithSeed(args)) => {
|
|
|
|
let mut me = get_stake_account()?;
|
2021-12-03 03:15:22 -08:00
|
|
|
if invoke_context
|
|
|
|
.feature_set
|
|
|
|
.is_active(&feature_set::vote_stake_checked_instructions::id())
|
2021-06-30 17:34:50 -07:00
|
|
|
{
|
2022-03-29 10:06:50 -07:00
|
|
|
instruction_context.check_number_of_instruction_accounts(2)?;
|
2022-03-24 11:30:42 -07:00
|
|
|
let clock =
|
|
|
|
get_sysvar_with_account_check::clock(invoke_context, instruction_context, 2)?;
|
2022-03-29 10:06:50 -07:00
|
|
|
instruction_context.check_number_of_instruction_accounts(4)?;
|
2022-04-10 00:55:37 -07:00
|
|
|
let authorized_pubkey = transaction_context.get_key_of_account_at_index(
|
2022-06-16 09:46:17 -07:00
|
|
|
instruction_context.get_index_of_instruction_account_in_transaction(3)?,
|
2022-04-10 00:55:37 -07:00
|
|
|
)?;
|
2022-06-16 09:46:17 -07:00
|
|
|
if !instruction_context.is_instruction_account_signer(3)? {
|
2022-04-10 00:55:37 -07:00
|
|
|
return Err(InstructionError::MissingRequiredSignature);
|
|
|
|
}
|
2022-06-16 09:46:17 -07:00
|
|
|
let custodian_pubkey =
|
|
|
|
get_optional_pubkey(transaction_context, instruction_context, 4, false)?;
|
2021-06-30 17:34:50 -07:00
|
|
|
|
2022-04-06 13:58:09 -07:00
|
|
|
authorize_with_seed(
|
2022-04-10 00:55:37 -07:00
|
|
|
transaction_context,
|
|
|
|
instruction_context,
|
|
|
|
&mut me,
|
2022-06-16 09:46:17 -07:00
|
|
|
1,
|
2021-06-30 17:34:50 -07:00
|
|
|
&args.authority_seed,
|
|
|
|
&args.authority_owner,
|
|
|
|
authorized_pubkey,
|
|
|
|
args.stake_authorize,
|
|
|
|
true,
|
|
|
|
&clock,
|
2022-04-10 00:55:37 -07:00
|
|
|
custodian_pubkey,
|
2021-06-30 17:34:50 -07:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
Err(InstructionError::InvalidInstructionData)
|
|
|
|
}
|
|
|
|
}
|
2022-04-11 14:26:36 -07:00
|
|
|
Ok(StakeInstruction::SetLockupChecked(lockup_checked)) => {
|
|
|
|
let mut me = get_stake_account()?;
|
2021-12-03 03:15:22 -08:00
|
|
|
if invoke_context
|
|
|
|
.feature_set
|
|
|
|
.is_active(&feature_set::vote_stake_checked_instructions::id())
|
2021-06-30 17:34:50 -07:00
|
|
|
{
|
2022-06-16 09:46:17 -07:00
|
|
|
let custodian_pubkey =
|
|
|
|
get_optional_pubkey(transaction_context, instruction_context, 2, true)?;
|
2021-06-30 17:34:50 -07:00
|
|
|
|
|
|
|
let lockup = LockupArgs {
|
|
|
|
unix_timestamp: lockup_checked.unix_timestamp,
|
|
|
|
epoch: lockup_checked.epoch,
|
2022-04-10 00:55:37 -07:00
|
|
|
custodian: custodian_pubkey.cloned(),
|
2021-06-30 17:34:50 -07:00
|
|
|
};
|
2022-01-21 17:27:53 -08:00
|
|
|
let clock = invoke_context.get_sysvar_cache().get_clock()?;
|
2022-04-10 00:55:37 -07:00
|
|
|
set_lockup(&mut me, &lockup, &signers, &clock)
|
2021-06-30 17:34:50 -07:00
|
|
|
} else {
|
|
|
|
Err(InstructionError::InvalidInstructionData)
|
|
|
|
}
|
|
|
|
}
|
2022-04-11 14:26:36 -07:00
|
|
|
Ok(StakeInstruction::GetMinimumDelegation) => {
|
2022-04-01 22:11:10 -07:00
|
|
|
let feature_set = invoke_context.feature_set.as_ref();
|
|
|
|
if !feature_set.is_active(
|
|
|
|
&feature_set::add_get_minimum_delegation_instruction_to_stake_program::id(),
|
|
|
|
) {
|
2022-04-11 14:26:36 -07:00
|
|
|
// Retain previous behavior of always checking that the first account
|
|
|
|
// is a stake account until the feature is activated
|
|
|
|
let _ = get_stake_account()?;
|
2022-04-01 22:11:10 -07:00
|
|
|
return Err(InstructionError::InvalidInstructionData);
|
|
|
|
}
|
|
|
|
|
|
|
|
let minimum_delegation = crate::get_minimum_delegation(feature_set);
|
|
|
|
let minimum_delegation = Vec::from(minimum_delegation.to_le_bytes());
|
|
|
|
invoke_context
|
|
|
|
.transaction_context
|
|
|
|
.set_return_data(id(), minimum_delegation)
|
|
|
|
}
|
2022-03-25 09:11:51 -07:00
|
|
|
Ok(StakeInstruction::DeactivateDelinquent) => {
|
|
|
|
let mut me = get_stake_account()?;
|
|
|
|
if invoke_context
|
|
|
|
.feature_set
|
|
|
|
.is_active(&feature_set::stake_deactivate_delinquent_instruction::id())
|
|
|
|
{
|
|
|
|
instruction_context.check_number_of_instruction_accounts(3)?;
|
|
|
|
|
|
|
|
let clock = invoke_context.get_sysvar_cache().get_clock()?;
|
|
|
|
deactivate_delinquent(
|
|
|
|
transaction_context,
|
|
|
|
instruction_context,
|
|
|
|
&mut me,
|
2022-06-16 09:46:17 -07:00
|
|
|
1,
|
|
|
|
2,
|
2022-03-25 09:11:51 -07:00
|
|
|
clock.epoch,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
Err(InstructionError::InvalidInstructionData)
|
|
|
|
}
|
|
|
|
}
|
2022-06-28 10:39:00 -07:00
|
|
|
Ok(StakeInstruction::Redelegate) => {
|
|
|
|
let mut me = get_stake_account()?;
|
|
|
|
if invoke_context
|
|
|
|
.feature_set
|
|
|
|
.is_active(&feature_set::stake_redelegate_instruction::id())
|
|
|
|
{
|
|
|
|
instruction_context.check_number_of_instruction_accounts(3)?;
|
|
|
|
let config_account =
|
|
|
|
instruction_context.try_borrow_instruction_account(transaction_context, 3)?;
|
|
|
|
if !config::check_id(config_account.get_key()) {
|
|
|
|
return Err(InstructionError::InvalidArgument);
|
|
|
|
}
|
|
|
|
let config =
|
|
|
|
config::from(&config_account).ok_or(InstructionError::InvalidArgument)?;
|
|
|
|
drop(config_account);
|
|
|
|
|
|
|
|
redelegate(
|
|
|
|
invoke_context,
|
|
|
|
transaction_context,
|
|
|
|
instruction_context,
|
|
|
|
&mut me,
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
&config,
|
|
|
|
&signers,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
Err(InstructionError::InvalidInstructionData)
|
|
|
|
}
|
|
|
|
}
|
2022-07-27 19:44:52 -07:00
|
|
|
// In order to prevent consensus issues, any new StakeInstruction variant added before the
|
|
|
|
// `add_get_minimum_delegation_instruction_to_stake_program` is activated needs to check
|
|
|
|
// the validity of the stake account by calling the `get_stake_account()` method outside
|
|
|
|
// its own feature gate, as per the following pattern:
|
|
|
|
// ```
|
|
|
|
// Ok(StakeInstruction::Variant) -> {
|
|
|
|
// let mut me = get_stake_account()?;
|
|
|
|
// if invoke_context
|
|
|
|
// .feature_set
|
|
|
|
// .is_active(&feature_set::stake_variant_feature::id()) { .. }
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
// TODO: Remove this comment when `add_get_minimum_delegation_instruction_to_stake_program`
|
|
|
|
// is cleaned up
|
2022-04-11 14:26:36 -07:00
|
|
|
Err(err) => {
|
|
|
|
if !invoke_context.feature_set.is_active(
|
|
|
|
&feature_set::add_get_minimum_delegation_instruction_to_stake_program::id(),
|
|
|
|
) {
|
|
|
|
let _ = get_stake_account()?;
|
|
|
|
}
|
|
|
|
Err(err)
|
|
|
|
}
|
2019-04-01 16:45:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2021-12-03 09:00:31 -08:00
|
|
|
use {
|
|
|
|
super::*,
|
2022-03-09 14:48:10 -08:00
|
|
|
crate::stake_state::{
|
2022-03-25 09:11:51 -07:00
|
|
|
authorized_from, create_stake_history_from_delegations, from, new_stake, stake_from,
|
|
|
|
Delegation, Meta, Stake, StakeState,
|
2022-03-09 14:48:10 -08:00
|
|
|
},
|
2022-05-04 03:17:29 -07:00
|
|
|
assert_matches::assert_matches,
|
2021-12-03 09:00:31 -08:00
|
|
|
bincode::serialize,
|
2022-03-31 01:46:35 -07:00
|
|
|
solana_program_runtime::{
|
|
|
|
invoke_context::mock_process_instruction, sysvar_cache::SysvarCache,
|
|
|
|
},
|
2021-12-03 09:00:31 -08:00
|
|
|
solana_sdk::{
|
2023-04-03 08:23:24 -07:00
|
|
|
account::{
|
|
|
|
create_account_shared_data_for_test, AccountSharedData, ReadableAccount,
|
|
|
|
WritableAccount,
|
|
|
|
},
|
2022-03-09 14:48:10 -08:00
|
|
|
account_utils::StateMut,
|
|
|
|
clock::{Epoch, UnixTimestamp},
|
2022-04-01 22:11:10 -07:00
|
|
|
feature_set::FeatureSet,
|
2021-12-03 09:00:31 -08:00
|
|
|
instruction::{AccountMeta, Instruction},
|
|
|
|
pubkey::Pubkey,
|
|
|
|
rent::Rent,
|
|
|
|
stake::{
|
|
|
|
config as stake_config,
|
2022-04-06 13:58:09 -07:00
|
|
|
instruction::{
|
|
|
|
self, authorize_checked, authorize_checked_with_seed, initialize_checked,
|
|
|
|
set_lockup_checked, AuthorizeCheckedWithSeedArgs, AuthorizeWithSeedArgs,
|
|
|
|
LockupArgs, StakeError,
|
|
|
|
},
|
2022-06-28 10:39:00 -07:00
|
|
|
state::{Authorized, Lockup, StakeActivationStatus, StakeAuthorize},
|
2022-03-25 09:11:51 -07:00
|
|
|
MINIMUM_DELINQUENT_EPOCHS_FOR_DEACTIVATION,
|
2021-12-03 09:00:31 -08:00
|
|
|
},
|
2022-04-06 03:04:35 -07:00
|
|
|
stake_history::{StakeHistory, StakeHistoryEntry},
|
2023-04-03 08:23:24 -07:00
|
|
|
system_program,
|
|
|
|
sysvar::{clock, rent, rewards, stake_history},
|
2021-06-15 09:04:00 -07:00
|
|
|
},
|
2022-03-09 14:48:10 -08:00
|
|
|
solana_vote_program::vote_state::{self, VoteState, VoteStateVersions},
|
2022-06-28 10:39:00 -07:00
|
|
|
std::{
|
|
|
|
borrow::{Borrow, BorrowMut},
|
|
|
|
collections::HashSet,
|
|
|
|
str::FromStr,
|
|
|
|
sync::Arc,
|
|
|
|
},
|
2022-07-06 14:15:01 -07:00
|
|
|
test_case::test_case,
|
2020-10-26 09:14:57 -07:00
|
|
|
};
|
2020-01-22 09:11:56 -08:00
|
|
|
|
2022-07-06 14:15:01 -07:00
|
|
|
/// The "new" behavior enables all features
|
2023-04-03 08:23:24 -07:00
|
|
|
fn feature_set_new_behavior() -> Arc<FeatureSet> {
|
|
|
|
Arc::new(FeatureSet::all_enabled())
|
2022-07-06 14:15:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The "old" behavior is before the stake minimum delegation was raised
|
2023-04-03 08:23:24 -07:00
|
|
|
fn feature_set_old_behavior() -> Arc<FeatureSet> {
|
2022-07-06 14:15:01 -07:00
|
|
|
let mut feature_set = feature_set_new_behavior();
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::get_mut(&mut feature_set)
|
|
|
|
.unwrap()
|
|
|
|
.deactivate(&feature_set::stake_raise_minimum_delegation_to_1_sol::id());
|
2022-07-06 14:15:01 -07:00
|
|
|
feature_set
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The "old old" behavior is both before the stake minimum delegation was raised *and* before
|
|
|
|
/// undelegated stake accounts could have zero lamports beyond rent
|
2023-04-03 08:23:24 -07:00
|
|
|
fn feature_set_old_old_behavior() -> Arc<FeatureSet> {
|
2022-07-06 14:15:01 -07:00
|
|
|
let mut feature_set = feature_set_old_behavior();
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::get_mut(&mut feature_set)
|
|
|
|
.unwrap()
|
|
|
|
.deactivate(&feature_set::stake_allow_zero_undelegated_amount::id());
|
2022-07-06 14:15:01 -07:00
|
|
|
feature_set
|
|
|
|
}
|
|
|
|
|
2021-12-17 05:01:12 -08:00
|
|
|
fn create_default_account() -> AccountSharedData {
|
|
|
|
AccountSharedData::new(0, 0, &Pubkey::new_unique())
|
2020-01-22 09:11:56 -08:00
|
|
|
}
|
2019-04-01 16:45:53 -07:00
|
|
|
|
2021-12-17 05:01:12 -08:00
|
|
|
fn create_default_stake_account() -> AccountSharedData {
|
|
|
|
AccountSharedData::new(0, 0, &id())
|
2020-12-22 10:20:38 -08:00
|
|
|
}
|
|
|
|
|
2020-11-16 12:42:59 -08:00
|
|
|
fn invalid_stake_state_pubkey() -> Pubkey {
|
|
|
|
Pubkey::from_str("BadStake11111111111111111111111111111111111").unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn invalid_vote_state_pubkey() -> Pubkey {
|
|
|
|
Pubkey::from_str("BadVote111111111111111111111111111111111111").unwrap()
|
|
|
|
}
|
|
|
|
|
2020-12-22 10:20:38 -08:00
|
|
|
fn spoofed_stake_state_pubkey() -> Pubkey {
|
|
|
|
Pubkey::from_str("SpoofedStake1111111111111111111111111111111").unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn spoofed_stake_program_id() -> Pubkey {
|
|
|
|
Pubkey::from_str("Spoofed111111111111111111111111111111111111").unwrap()
|
|
|
|
}
|
|
|
|
|
2021-10-08 02:41:07 -07:00
|
|
|
fn process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
feature_set: Arc<FeatureSet>,
|
2021-10-08 02:41:07 -07:00
|
|
|
instruction_data: &[u8],
|
2021-12-17 05:01:12 -08:00
|
|
|
transaction_accounts: Vec<(Pubkey, AccountSharedData)>,
|
|
|
|
instruction_accounts: Vec<AccountMeta>,
|
|
|
|
expected_result: Result<(), InstructionError>,
|
|
|
|
) -> Vec<AccountSharedData> {
|
2021-11-04 13:47:32 -07:00
|
|
|
mock_process_instruction(
|
|
|
|
&id(),
|
|
|
|
Vec::new(),
|
2021-10-08 02:41:07 -07:00
|
|
|
instruction_data,
|
2021-12-17 05:01:12 -08:00
|
|
|
transaction_accounts,
|
|
|
|
instruction_accounts,
|
|
|
|
expected_result,
|
2021-11-04 13:47:32 -07:00
|
|
|
super::process_instruction,
|
2023-04-03 08:23:24 -07:00
|
|
|
|invoke_context| {
|
|
|
|
invoke_context.feature_set = Arc::clone(&feature_set);
|
|
|
|
},
|
2021-10-08 02:41:07 -07:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-12-17 05:01:12 -08:00
|
|
|
fn process_instruction_as_one_arg(
|
2023-04-03 08:23:24 -07:00
|
|
|
feature_set: Arc<FeatureSet>,
|
2021-12-17 05:01:12 -08:00
|
|
|
instruction: &Instruction,
|
|
|
|
expected_result: Result<(), InstructionError>,
|
|
|
|
) -> Vec<AccountSharedData> {
|
2022-01-31 08:53:50 -08:00
|
|
|
let mut pubkeys: HashSet<Pubkey> = instruction
|
2019-06-17 19:34:21 -07:00
|
|
|
.accounts
|
|
|
|
.iter()
|
2022-01-31 08:53:50 -08:00
|
|
|
.map(|meta| meta.pubkey)
|
|
|
|
.collect();
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkeys.insert(clock::id());
|
2022-01-31 08:53:50 -08:00
|
|
|
let transaction_accounts = pubkeys
|
|
|
|
.iter()
|
|
|
|
.map(|pubkey| {
|
2021-12-17 05:01:12 -08:00
|
|
|
(
|
2022-01-31 08:53:50 -08:00
|
|
|
*pubkey,
|
2023-04-03 08:23:24 -07:00
|
|
|
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 stake_history::check_id(pubkey) {
|
|
|
|
create_account_shared_data_for_test(&StakeHistory::default())
|
2022-01-31 08:53:50 -08:00
|
|
|
} else if stake_config::check_id(pubkey) {
|
2021-12-17 05:01:12 -08:00
|
|
|
config::create_account(0, &stake_config::Config::default())
|
2023-04-03 08:23:24 -07:00
|
|
|
} else if rent::check_id(pubkey) {
|
|
|
|
create_account_shared_data_for_test(&Rent::default())
|
2022-01-31 08:53:50 -08:00
|
|
|
} else if *pubkey == invalid_stake_state_pubkey() {
|
2021-12-17 05:01:12 -08:00
|
|
|
AccountSharedData::new(0, 0, &id())
|
2022-01-31 08:53:50 -08:00
|
|
|
} else if *pubkey == invalid_vote_state_pubkey() {
|
2021-12-17 05:01:12 -08:00
|
|
|
AccountSharedData::new(0, 0, &solana_vote_program::id())
|
2022-01-31 08:53:50 -08:00
|
|
|
} else if *pubkey == spoofed_stake_state_pubkey() {
|
2021-12-17 05:01:12 -08:00
|
|
|
AccountSharedData::new(0, 0, &spoofed_stake_program_id())
|
|
|
|
} else {
|
|
|
|
AccountSharedData::new(0, 0, &id())
|
|
|
|
},
|
|
|
|
)
|
|
|
|
})
|
2019-06-17 19:34:21 -07:00
|
|
|
.collect();
|
2022-01-31 08:53:50 -08:00
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2021-12-17 05:01:12 -08:00
|
|
|
&instruction.data,
|
|
|
|
transaction_accounts,
|
|
|
|
instruction.accounts.clone(),
|
|
|
|
expected_result,
|
|
|
|
)
|
2019-05-07 17:08:49 -07:00
|
|
|
}
|
|
|
|
|
2022-03-09 14:48:10 -08:00
|
|
|
fn just_stake(meta: Meta, stake: u64) -> StakeState {
|
|
|
|
StakeState::Stake(
|
|
|
|
meta,
|
|
|
|
Stake {
|
|
|
|
delegation: Delegation {
|
|
|
|
stake,
|
|
|
|
..Delegation::default()
|
|
|
|
},
|
|
|
|
..Stake::default()
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-07-06 14:15:01 -07:00
|
|
|
#[test_case(feature_set_old_behavior(); "old_behavior")]
|
|
|
|
#[test_case(feature_set_new_behavior(); "new_behavior")]
|
2023-04-03 08:23:24 -07:00
|
|
|
fn test_stake_process_instruction(feature_set: Arc<FeatureSet>) {
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction_as_one_arg(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2021-12-17 05:01:12 -08:00
|
|
|
&instruction::initialize(
|
2021-11-04 13:47:32 -07:00
|
|
|
&Pubkey::new_unique(),
|
2019-10-31 11:07:27 -07:00
|
|
|
&Authorized::default(),
|
2021-12-17 05:01:12 -08:00
|
|
|
&Lockup::default(),
|
|
|
|
),
|
2019-10-31 11:07:27 -07:00
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction_as_one_arg(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2021-12-17 05:01:12 -08:00
|
|
|
&instruction::authorize(
|
2021-11-04 13:47:32 -07:00
|
|
|
&Pubkey::new_unique(),
|
|
|
|
&Pubkey::new_unique(),
|
|
|
|
&Pubkey::new_unique(),
|
2021-01-26 09:44:44 -08:00
|
|
|
StakeAuthorize::Staker,
|
|
|
|
None,
|
2021-12-17 05:01:12 -08:00
|
|
|
),
|
2019-10-31 11:07:27 -07:00
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction_as_one_arg(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2021-12-17 05:01:12 -08:00
|
|
|
&instruction::split(
|
|
|
|
&Pubkey::new_unique(),
|
|
|
|
&Pubkey::new_unique(),
|
|
|
|
100,
|
|
|
|
&invalid_stake_state_pubkey(),
|
|
|
|
)[2],
|
2020-01-20 12:33:27 -08:00
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction_as_one_arg(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2021-12-17 05:01:12 -08:00
|
|
|
&instruction::merge(
|
|
|
|
&Pubkey::new_unique(),
|
|
|
|
&invalid_stake_state_pubkey(),
|
|
|
|
&Pubkey::new_unique(),
|
|
|
|
)[0],
|
2020-06-10 17:22:47 -07:00
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction_as_one_arg(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2021-12-17 05:01:12 -08:00
|
|
|
&instruction::split_with_seed(
|
|
|
|
&Pubkey::new_unique(),
|
|
|
|
&Pubkey::new_unique(),
|
|
|
|
100,
|
|
|
|
&invalid_stake_state_pubkey(),
|
|
|
|
&Pubkey::new_unique(),
|
|
|
|
"seed",
|
|
|
|
)[1],
|
2019-10-31 11:07:27 -07:00
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction_as_one_arg(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2021-12-17 05:01:12 -08:00
|
|
|
&instruction::delegate_stake(
|
2021-11-04 13:47:32 -07:00
|
|
|
&Pubkey::new_unique(),
|
|
|
|
&Pubkey::new_unique(),
|
2020-11-16 12:42:59 -08:00
|
|
|
&invalid_vote_state_pubkey(),
|
2021-12-17 05:01:12 -08:00
|
|
|
),
|
2019-05-07 17:08:49 -07:00
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction_as_one_arg(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2021-12-17 05:01:12 -08:00
|
|
|
&instruction::withdraw(
|
2021-11-04 13:47:32 -07:00
|
|
|
&Pubkey::new_unique(),
|
|
|
|
&Pubkey::new_unique(),
|
|
|
|
&Pubkey::new_unique(),
|
2020-04-20 17:16:50 -07:00
|
|
|
100,
|
|
|
|
None,
|
2021-12-17 05:01:12 -08:00
|
|
|
),
|
2019-06-21 22:28:34 -07:00
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction_as_one_arg(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2021-12-17 05:01:12 -08:00
|
|
|
&instruction::deactivate_stake(&Pubkey::new_unique(), &Pubkey::new_unique()),
|
2019-06-21 23:45:03 -07:00
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction_as_one_arg(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2021-12-17 05:01:12 -08:00
|
|
|
&instruction::set_lockup(
|
2021-11-04 13:47:32 -07:00
|
|
|
&Pubkey::new_unique(),
|
2020-03-02 12:28:43 -08:00
|
|
|
&LockupArgs::default(),
|
2021-12-17 05:01:12 -08:00
|
|
|
&Pubkey::new_unique(),
|
|
|
|
),
|
2020-01-28 20:59:53 -08:00
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
2022-03-25 09:11:51 -07:00
|
|
|
process_instruction_as_one_arg(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-25 09:11:51 -07:00
|
|
|
&instruction::deactivate_delinquent_stake(
|
|
|
|
&Pubkey::new_unique(),
|
|
|
|
&Pubkey::new_unique(),
|
|
|
|
&invalid_vote_state_pubkey(),
|
|
|
|
),
|
|
|
|
Err(InstructionError::IncorrectProgramId),
|
|
|
|
);
|
|
|
|
process_instruction_as_one_arg(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-25 09:11:51 -07:00
|
|
|
&instruction::deactivate_delinquent_stake(
|
|
|
|
&Pubkey::new_unique(),
|
|
|
|
&invalid_vote_state_pubkey(),
|
|
|
|
&Pubkey::new_unique(),
|
|
|
|
),
|
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
|
|
|
process_instruction_as_one_arg(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-25 09:11:51 -07:00
|
|
|
&instruction::deactivate_delinquent_stake(
|
|
|
|
&Pubkey::new_unique(),
|
|
|
|
&invalid_vote_state_pubkey(),
|
|
|
|
&invalid_vote_state_pubkey(),
|
|
|
|
),
|
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
2019-05-07 17:08:49 -07:00
|
|
|
}
|
|
|
|
|
2022-07-06 14:15:01 -07:00
|
|
|
#[test_case(feature_set_old_behavior(); "old_behavior")]
|
|
|
|
#[test_case(feature_set_new_behavior(); "new_behavior")]
|
2023-04-03 08:23:24 -07:00
|
|
|
fn test_spoofed_stake_accounts(feature_set: Arc<FeatureSet>) {
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction_as_one_arg(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2021-12-17 05:01:12 -08:00
|
|
|
&instruction::initialize(
|
2020-12-22 10:20:38 -08:00
|
|
|
&spoofed_stake_state_pubkey(),
|
|
|
|
&Authorized::default(),
|
2021-12-17 05:01:12 -08:00
|
|
|
&Lockup::default(),
|
|
|
|
),
|
2021-02-26 14:21:34 -08:00
|
|
|
Err(InstructionError::InvalidAccountOwner),
|
2020-12-22 10:20:38 -08:00
|
|
|
);
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction_as_one_arg(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2021-12-17 05:01:12 -08:00
|
|
|
&instruction::authorize(
|
2020-12-22 10:20:38 -08:00
|
|
|
&spoofed_stake_state_pubkey(),
|
2021-11-04 13:47:32 -07:00
|
|
|
&Pubkey::new_unique(),
|
|
|
|
&Pubkey::new_unique(),
|
2021-01-26 09:44:44 -08:00
|
|
|
StakeAuthorize::Staker,
|
|
|
|
None,
|
2021-12-17 05:01:12 -08:00
|
|
|
),
|
2021-02-26 14:21:34 -08:00
|
|
|
Err(InstructionError::InvalidAccountOwner),
|
2020-12-22 10:20:38 -08:00
|
|
|
);
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction_as_one_arg(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2021-12-17 05:01:12 -08:00
|
|
|
&instruction::split(
|
|
|
|
&spoofed_stake_state_pubkey(),
|
|
|
|
&Pubkey::new_unique(),
|
|
|
|
100,
|
|
|
|
&Pubkey::new_unique(),
|
|
|
|
)[2],
|
2021-02-26 14:21:34 -08:00
|
|
|
Err(InstructionError::InvalidAccountOwner),
|
2020-12-22 10:20:38 -08:00
|
|
|
);
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction_as_one_arg(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2021-12-17 05:01:12 -08:00
|
|
|
&instruction::split(
|
|
|
|
&Pubkey::new_unique(),
|
|
|
|
&Pubkey::new_unique(),
|
|
|
|
100,
|
|
|
|
&spoofed_stake_state_pubkey(),
|
|
|
|
)[2],
|
2020-12-22 10:20:38 -08:00
|
|
|
Err(InstructionError::IncorrectProgramId),
|
|
|
|
);
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction_as_one_arg(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2021-12-17 05:01:12 -08:00
|
|
|
&instruction::merge(
|
|
|
|
&spoofed_stake_state_pubkey(),
|
|
|
|
&Pubkey::new_unique(),
|
|
|
|
&Pubkey::new_unique(),
|
|
|
|
)[0],
|
2021-02-26 14:21:34 -08:00
|
|
|
Err(InstructionError::InvalidAccountOwner),
|
2020-12-22 10:20:38 -08:00
|
|
|
);
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction_as_one_arg(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2021-12-17 05:01:12 -08:00
|
|
|
&instruction::merge(
|
|
|
|
&Pubkey::new_unique(),
|
|
|
|
&spoofed_stake_state_pubkey(),
|
|
|
|
&Pubkey::new_unique(),
|
|
|
|
)[0],
|
2020-12-22 10:20:38 -08:00
|
|
|
Err(InstructionError::IncorrectProgramId),
|
|
|
|
);
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction_as_one_arg(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2021-12-17 05:01:12 -08:00
|
|
|
&instruction::split_with_seed(
|
|
|
|
&spoofed_stake_state_pubkey(),
|
|
|
|
&Pubkey::new_unique(),
|
|
|
|
100,
|
|
|
|
&Pubkey::new_unique(),
|
|
|
|
&Pubkey::new_unique(),
|
|
|
|
"seed",
|
|
|
|
)[1],
|
2021-02-26 14:21:34 -08:00
|
|
|
Err(InstructionError::InvalidAccountOwner),
|
2020-12-22 10:20:38 -08:00
|
|
|
);
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction_as_one_arg(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2021-12-17 05:01:12 -08:00
|
|
|
&instruction::delegate_stake(
|
2020-12-22 10:20:38 -08:00
|
|
|
&spoofed_stake_state_pubkey(),
|
2021-11-04 13:47:32 -07:00
|
|
|
&Pubkey::new_unique(),
|
|
|
|
&Pubkey::new_unique(),
|
2021-12-17 05:01:12 -08:00
|
|
|
),
|
2021-02-26 14:21:34 -08:00
|
|
|
Err(InstructionError::InvalidAccountOwner),
|
2020-12-22 10:20:38 -08:00
|
|
|
);
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction_as_one_arg(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2021-12-17 05:01:12 -08:00
|
|
|
&instruction::withdraw(
|
2020-12-22 10:20:38 -08:00
|
|
|
&spoofed_stake_state_pubkey(),
|
2021-11-04 13:47:32 -07:00
|
|
|
&Pubkey::new_unique(),
|
|
|
|
&Pubkey::new_unique(),
|
2020-12-22 10:20:38 -08:00
|
|
|
100,
|
|
|
|
None,
|
2021-12-17 05:01:12 -08:00
|
|
|
),
|
2021-02-26 14:21:34 -08:00
|
|
|
Err(InstructionError::InvalidAccountOwner),
|
2020-12-22 10:20:38 -08:00
|
|
|
);
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction_as_one_arg(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2021-12-17 05:01:12 -08:00
|
|
|
&instruction::deactivate_stake(&spoofed_stake_state_pubkey(), &Pubkey::new_unique()),
|
2021-02-26 14:21:34 -08:00
|
|
|
Err(InstructionError::InvalidAccountOwner),
|
2020-12-22 10:20:38 -08:00
|
|
|
);
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction_as_one_arg(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2021-12-17 05:01:12 -08:00
|
|
|
&instruction::set_lockup(
|
2020-12-22 10:20:38 -08:00
|
|
|
&spoofed_stake_state_pubkey(),
|
|
|
|
&LockupArgs::default(),
|
2021-12-17 05:01:12 -08:00
|
|
|
&Pubkey::new_unique(),
|
|
|
|
),
|
2021-02-26 14:21:34 -08:00
|
|
|
Err(InstructionError::InvalidAccountOwner),
|
2020-12-22 10:20:38 -08:00
|
|
|
);
|
2022-03-25 09:11:51 -07:00
|
|
|
process_instruction_as_one_arg(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-25 09:11:51 -07:00
|
|
|
&instruction::deactivate_delinquent_stake(
|
|
|
|
&spoofed_stake_state_pubkey(),
|
|
|
|
&Pubkey::new_unique(),
|
|
|
|
&Pubkey::new_unique(),
|
|
|
|
),
|
|
|
|
Err(InstructionError::InvalidAccountOwner),
|
|
|
|
);
|
2022-06-28 10:39:00 -07:00
|
|
|
process_instruction_as_one_arg(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-06-28 10:39:00 -07:00
|
|
|
&instruction::redelegate(
|
|
|
|
&spoofed_stake_state_pubkey(),
|
|
|
|
&Pubkey::new_unique(),
|
|
|
|
&Pubkey::new_unique(),
|
|
|
|
&Pubkey::new_unique(),
|
|
|
|
)[2],
|
|
|
|
Err(InstructionError::InvalidAccountOwner),
|
|
|
|
);
|
2020-12-22 10:20:38 -08:00
|
|
|
}
|
|
|
|
|
2022-07-06 14:15:01 -07:00
|
|
|
#[test_case(feature_set_old_behavior(); "old_behavior")]
|
|
|
|
#[test_case(feature_set_new_behavior(); "new_behavior")]
|
2023-04-03 08:23:24 -07:00
|
|
|
fn test_stake_process_instruction_decode_bail(feature_set: Arc<FeatureSet>) {
|
2019-04-01 16:45:53 -07:00
|
|
|
// these will not call stake_state, have bogus contents
|
2021-12-17 05:01:12 -08:00
|
|
|
let stake_address = Pubkey::new_unique();
|
|
|
|
let stake_account = create_default_stake_account();
|
2023-04-03 08:23:24 -07:00
|
|
|
let rent_address = rent::id();
|
2022-03-16 14:36:23 -07:00
|
|
|
let rent = Rent::default();
|
2023-04-03 08:23:24 -07:00
|
|
|
let rent_account = create_account_shared_data_for_test(&rent);
|
|
|
|
let rewards_address = rewards::id();
|
|
|
|
let rewards_account = create_account_shared_data_for_test(&rewards::Rewards::new(0.0));
|
|
|
|
let stake_history_address = stake_history::id();
|
|
|
|
let stake_history_account = create_account_shared_data_for_test(&StakeHistory::default());
|
2021-12-17 05:01:12 -08:00
|
|
|
let vote_address = Pubkey::new_unique();
|
|
|
|
let vote_account = AccountSharedData::new(0, 0, &solana_vote_program::id());
|
2023-04-03 08:23:24 -07:00
|
|
|
let clock_address = clock::id();
|
|
|
|
let clock_account = create_account_shared_data_for_test(&clock::Clock::default());
|
2021-12-17 05:01:12 -08:00
|
|
|
let config_address = stake_config::id();
|
|
|
|
let config_account = config::create_account(0, &stake_config::Config::default());
|
2022-04-19 10:04:12 -07:00
|
|
|
let rent_exempt_reserve = rent.minimum_balance(StakeState::size_of());
|
2022-05-07 16:01:05 -07:00
|
|
|
let minimum_delegation = crate::get_minimum_delegation(&feature_set);
|
2022-04-01 22:11:10 -07:00
|
|
|
let withdrawal_amount = rent_exempt_reserve + minimum_delegation;
|
2019-04-01 16:45:53 -07:00
|
|
|
|
2019-09-11 09:48:29 -07:00
|
|
|
// gets the "is_empty()" check
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2021-12-17 05:01:12 -08:00
|
|
|
&serialize(&StakeInstruction::Initialize(
|
|
|
|
Authorized::default(),
|
|
|
|
Lockup::default(),
|
|
|
|
))
|
|
|
|
.unwrap(),
|
|
|
|
Vec::new(),
|
|
|
|
Vec::new(),
|
2019-10-14 15:02:24 -07:00
|
|
|
Err(InstructionError::NotEnoughAccountKeys),
|
2019-09-11 09:48:29 -07:00
|
|
|
);
|
|
|
|
|
2019-10-31 11:07:27 -07:00
|
|
|
// no account for rent
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2021-12-17 05:01:12 -08:00
|
|
|
&serialize(&StakeInstruction::Initialize(
|
|
|
|
Authorized::default(),
|
|
|
|
Lockup::default(),
|
|
|
|
))
|
|
|
|
.unwrap(),
|
|
|
|
vec![(stake_address, stake_account.clone())],
|
|
|
|
vec![AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2021-12-17 05:01:12 -08:00
|
|
|
}],
|
2019-10-31 11:07:27 -07:00
|
|
|
Err(InstructionError::NotEnoughAccountKeys),
|
|
|
|
);
|
|
|
|
|
|
|
|
// fails to deserialize stake state
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2021-12-17 05:01:12 -08:00
|
|
|
&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,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2021-12-17 05:01:12 -08:00
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: rent_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
2019-10-31 11:07:27 -07:00
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
|
|
|
|
2019-09-11 09:48:29 -07:00
|
|
|
// gets the first check in delegate, wrong number of accounts
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2021-12-17 05:01:12 -08:00
|
|
|
&serialize(&StakeInstruction::DelegateStake).unwrap(),
|
|
|
|
vec![(stake_address, stake_account.clone())],
|
|
|
|
vec![AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2021-12-17 05:01:12 -08:00
|
|
|
}],
|
2019-10-14 15:02:24 -07:00
|
|
|
Err(InstructionError::NotEnoughAccountKeys),
|
2019-04-01 16:45:53 -07:00
|
|
|
);
|
|
|
|
|
2019-05-07 17:08:49 -07:00
|
|
|
// gets the sub-check for number of args
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2021-12-17 05:01:12 -08:00
|
|
|
&serialize(&StakeInstruction::DelegateStake).unwrap(),
|
|
|
|
vec![(stake_address, stake_account.clone())],
|
|
|
|
vec![AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2021-12-17 05:01:12 -08:00
|
|
|
}],
|
2019-10-14 15:02:24 -07:00
|
|
|
Err(InstructionError::NotEnoughAccountKeys),
|
2019-04-01 16:45:53 -07:00
|
|
|
);
|
|
|
|
|
2019-06-17 19:34:21 -07:00
|
|
|
// gets the check non-deserialize-able account in delegate_stake
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2021-12-17 05:01:12 -08:00
|
|
|
&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,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2021-12-17 05:01:12 -08:00
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: vote_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: clock_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_history_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: config_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
2019-05-07 17:08:49 -07:00
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
|
|
|
|
2019-07-12 16:38:15 -07:00
|
|
|
// Tests 3rd keyed account is of correct type (Clock instead of rewards) in withdraw
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-16 14:36:23 -07:00
|
|
|
&serialize(&StakeInstruction::Withdraw(withdrawal_amount)).unwrap(),
|
2021-12-17 05:01:12 -08:00
|
|
|
vec![
|
|
|
|
(stake_address, stake_account.clone()),
|
2022-03-25 09:11:51 -07:00
|
|
|
(vote_address, vote_account.clone()),
|
2021-12-17 05:01:12 -08:00
|
|
|
(rewards_address, rewards_account.clone()),
|
|
|
|
(stake_history_address, stake_history_account),
|
|
|
|
],
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2021-12-17 05:01:12 -08:00
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: vote_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: rewards_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_history_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
2019-06-21 22:28:34 -07:00
|
|
|
Err(InstructionError::InvalidArgument),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Tests correct number of accounts are provided in withdraw
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-16 14:36:23 -07:00
|
|
|
&serialize(&StakeInstruction::Withdraw(withdrawal_amount)).unwrap(),
|
2021-12-17 05:01:12 -08:00
|
|
|
vec![(stake_address, stake_account.clone())],
|
|
|
|
vec![AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2021-12-17 05:01:12 -08:00
|
|
|
}],
|
2019-10-14 15:02:24 -07:00
|
|
|
Err(InstructionError::NotEnoughAccountKeys),
|
2019-06-21 22:28:34 -07:00
|
|
|
);
|
2019-06-21 23:45:03 -07:00
|
|
|
|
2019-07-12 16:38:15 -07:00
|
|
|
// Tests 2nd keyed account is of correct type (Clock instead of rewards) in deactivate
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2021-12-17 05:01:12 -08:00
|
|
|
&serialize(&StakeInstruction::Deactivate).unwrap(),
|
|
|
|
vec![
|
2022-03-25 09:11:51 -07:00
|
|
|
(stake_address, stake_account.clone()),
|
2021-12-17 05:01:12 -08:00
|
|
|
(rewards_address, rewards_account),
|
|
|
|
],
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2021-12-17 05:01:12 -08:00
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: rewards_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
2019-06-21 23:45:03 -07:00
|
|
|
Err(InstructionError::InvalidArgument),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Tests correct number of accounts are provided in deactivate
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2021-12-17 05:01:12 -08:00
|
|
|
&serialize(&StakeInstruction::Deactivate).unwrap(),
|
|
|
|
Vec::new(),
|
|
|
|
Vec::new(),
|
2019-10-14 15:02:24 -07:00
|
|
|
Err(InstructionError::NotEnoughAccountKeys),
|
2019-06-21 23:45:03 -07:00
|
|
|
);
|
2022-03-25 09:11:51 -07:00
|
|
|
|
|
|
|
// Tests correct number of accounts are provided in deactivate_delinquent
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-25 09:11:51 -07:00
|
|
|
&serialize(&StakeInstruction::DeactivateDelinquent).unwrap(),
|
|
|
|
Vec::new(),
|
|
|
|
Vec::new(),
|
|
|
|
Err(InstructionError::NotEnoughAccountKeys),
|
|
|
|
);
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-25 09:11:51 -07:00
|
|
|
&serialize(&StakeInstruction::DeactivateDelinquent).unwrap(),
|
|
|
|
vec![(stake_address, stake_account.clone())],
|
|
|
|
vec![AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-03-25 09:11:51 -07:00
|
|
|
}],
|
|
|
|
Err(InstructionError::NotEnoughAccountKeys),
|
|
|
|
);
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-25 09:11:51 -07:00
|
|
|
&serialize(&StakeInstruction::DeactivateDelinquent).unwrap(),
|
|
|
|
vec![(stake_address, stake_account), (vote_address, vote_account)],
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-03-25 09:11:51 -07:00
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: vote_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
Err(InstructionError::NotEnoughAccountKeys),
|
|
|
|
);
|
2019-04-01 16:45:53 -07:00
|
|
|
}
|
2021-07-06 12:24:10 -07:00
|
|
|
|
2022-07-06 14:15:01 -07:00
|
|
|
#[test_case(feature_set_old_behavior(); "old_behavior")]
|
|
|
|
#[test_case(feature_set_new_behavior(); "new_behavior")]
|
2023-04-03 08:23:24 -07:00
|
|
|
fn test_stake_checked_instructions(feature_set: Arc<FeatureSet>) {
|
2021-07-06 12:24:10 -07:00
|
|
|
let stake_address = Pubkey::new_unique();
|
|
|
|
let staker = Pubkey::new_unique();
|
2021-12-17 05:01:12 -08:00
|
|
|
let staker_account = create_default_account();
|
2021-07-06 12:24:10 -07:00
|
|
|
let withdrawer = Pubkey::new_unique();
|
2021-12-17 05:01:12 -08:00
|
|
|
let withdrawer_account = create_default_account();
|
|
|
|
let authorized_address = Pubkey::new_unique();
|
|
|
|
let authorized_account = create_default_account();
|
|
|
|
let new_authorized_account = create_default_account();
|
2023-04-03 08:23:24 -07:00
|
|
|
let clock_address = clock::id();
|
|
|
|
let clock_account = create_account_shared_data_for_test(&Clock::default());
|
2021-12-17 05:01:12 -08:00
|
|
|
let custodian = Pubkey::new_unique();
|
|
|
|
let custodian_account = create_default_account();
|
2022-03-16 14:36:23 -07:00
|
|
|
let rent = Rent::default();
|
2023-04-03 08:23:24 -07:00
|
|
|
let rent_address = rent::id();
|
|
|
|
let rent_account = create_account_shared_data_for_test(&rent);
|
2022-04-19 10:04:12 -07:00
|
|
|
let rent_exempt_reserve = rent.minimum_balance(StakeState::size_of());
|
2022-05-07 16:01:05 -07:00
|
|
|
let minimum_delegation = crate::get_minimum_delegation(&feature_set);
|
2021-07-06 12:24:10 -07:00
|
|
|
|
|
|
|
// Test InitializeChecked with non-signing withdrawer
|
|
|
|
let mut instruction =
|
|
|
|
initialize_checked(&stake_address, &Authorized { staker, withdrawer });
|
|
|
|
instruction.accounts[3] = AccountMeta::new_readonly(withdrawer, false);
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction_as_one_arg(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2021-12-17 05:01:12 -08:00
|
|
|
&instruction,
|
2021-07-06 12:24:10 -07:00
|
|
|
Err(InstructionError::MissingRequiredSignature),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test InitializeChecked with withdrawer signer
|
2021-12-17 05:01:12 -08:00
|
|
|
let stake_account = AccountSharedData::new(
|
2022-04-01 22:11:10 -07:00
|
|
|
rent_exempt_reserve + minimum_delegation,
|
2022-05-07 16:01:05 -07:00
|
|
|
StakeState::size_of(),
|
2021-07-06 12:24:10 -07:00
|
|
|
&id(),
|
|
|
|
);
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2021-12-17 05:01:12 -08:00
|
|
|
&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,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2021-12-17 05:01:12 -08:00
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: rent_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: staker,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: withdrawer,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
2021-07-06 12:24:10 -07:00
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test AuthorizeChecked with non-signing authority
|
|
|
|
let mut instruction = authorize_checked(
|
|
|
|
&stake_address,
|
|
|
|
&authorized_address,
|
|
|
|
&staker,
|
|
|
|
StakeAuthorize::Staker,
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
instruction.accounts[3] = AccountMeta::new_readonly(staker, false);
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction_as_one_arg(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2021-12-17 05:01:12 -08:00
|
|
|
&instruction,
|
2021-07-06 12:24:10 -07:00
|
|
|
Err(InstructionError::MissingRequiredSignature),
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut instruction = authorize_checked(
|
|
|
|
&stake_address,
|
|
|
|
&authorized_address,
|
|
|
|
&withdrawer,
|
|
|
|
StakeAuthorize::Withdrawer,
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
instruction.accounts[3] = AccountMeta::new_readonly(withdrawer, false);
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction_as_one_arg(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2021-12-17 05:01:12 -08:00
|
|
|
&instruction,
|
2021-07-06 12:24:10 -07:00
|
|
|
Err(InstructionError::MissingRequiredSignature),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test AuthorizeChecked with authority signer
|
2021-12-17 05:01:12 -08:00
|
|
|
let stake_account = AccountSharedData::new_data_with_space(
|
|
|
|
42,
|
|
|
|
&StakeState::Initialized(Meta::auto(&authorized_address)),
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2021-12-17 05:01:12 -08:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2021-12-17 05:01:12 -08:00
|
|
|
&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,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2021-12-17 05:01:12 -08:00
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: clock_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: authorized_address,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: staker,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
2021-07-06 12:24:10 -07:00
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2021-12-17 05:01:12 -08:00
|
|
|
&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,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2021-12-17 05:01:12 -08:00
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: clock_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: authorized_address,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: withdrawer,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
2021-07-06 12:24:10 -07:00
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test AuthorizeCheckedWithSeed with non-signing authority
|
|
|
|
let authorized_owner = Pubkey::new_unique();
|
|
|
|
let seed = "test seed";
|
|
|
|
let address_with_seed =
|
|
|
|
Pubkey::create_with_seed(&authorized_owner, seed, &authorized_owner).unwrap();
|
|
|
|
let mut instruction = authorize_checked_with_seed(
|
|
|
|
&stake_address,
|
|
|
|
&authorized_owner,
|
|
|
|
seed.to_string(),
|
|
|
|
&authorized_owner,
|
|
|
|
&staker,
|
|
|
|
StakeAuthorize::Staker,
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
instruction.accounts[3] = AccountMeta::new_readonly(staker, false);
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction_as_one_arg(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2021-12-17 05:01:12 -08:00
|
|
|
&instruction,
|
2021-07-06 12:24:10 -07:00
|
|
|
Err(InstructionError::MissingRequiredSignature),
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut instruction = authorize_checked_with_seed(
|
|
|
|
&stake_address,
|
|
|
|
&authorized_owner,
|
|
|
|
seed.to_string(),
|
|
|
|
&authorized_owner,
|
|
|
|
&staker,
|
|
|
|
StakeAuthorize::Withdrawer,
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
instruction.accounts[3] = AccountMeta::new_readonly(staker, false);
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction_as_one_arg(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2021-12-17 05:01:12 -08:00
|
|
|
&instruction,
|
2021-07-06 12:24:10 -07:00
|
|
|
Err(InstructionError::MissingRequiredSignature),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test AuthorizeCheckedWithSeed with authority signer
|
2021-12-17 05:01:12 -08:00
|
|
|
let stake_account = AccountSharedData::new_data_with_space(
|
|
|
|
42,
|
|
|
|
&StakeState::Initialized(Meta::auto(&address_with_seed)),
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2021-12-17 05:01:12 -08:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2021-12-17 05:01:12 -08:00
|
|
|
&serialize(&StakeInstruction::AuthorizeCheckedWithSeed(
|
|
|
|
AuthorizeCheckedWithSeedArgs {
|
|
|
|
stake_authorize: StakeAuthorize::Staker,
|
|
|
|
authority_seed: seed.to_string(),
|
|
|
|
authority_owner: authorized_owner,
|
|
|
|
},
|
|
|
|
))
|
2021-11-04 13:47:32 -07:00
|
|
|
.unwrap(),
|
2021-12-17 05:01:12 -08:00
|
|
|
vec![
|
|
|
|
(address_with_seed, stake_account.clone()),
|
|
|
|
(authorized_owner, authorized_account.clone()),
|
|
|
|
(clock_address, clock_account.clone()),
|
|
|
|
(staker, new_authorized_account.clone()),
|
|
|
|
],
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: address_with_seed,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2021-12-17 05:01:12 -08:00
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: authorized_owner,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: clock_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: staker,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
2021-07-06 12:24:10 -07:00
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2021-12-17 05:01:12 -08:00
|
|
|
&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),
|
2022-01-31 08:53:50 -08:00
|
|
|
(clock_address, clock_account.clone()),
|
2021-12-17 05:01:12 -08:00
|
|
|
(withdrawer, new_authorized_account),
|
|
|
|
],
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: address_with_seed,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2021-12-17 05:01:12 -08:00
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: authorized_owner,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: clock_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: withdrawer,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
2021-07-06 12:24:10 -07:00
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test SetLockupChecked with non-signing lockup custodian
|
|
|
|
let mut instruction = set_lockup_checked(
|
|
|
|
&stake_address,
|
|
|
|
&LockupArgs {
|
|
|
|
unix_timestamp: None,
|
|
|
|
epoch: Some(1),
|
|
|
|
custodian: Some(custodian),
|
|
|
|
},
|
|
|
|
&withdrawer,
|
|
|
|
);
|
|
|
|
instruction.accounts[2] = AccountMeta::new_readonly(custodian, false);
|
2021-12-17 05:01:12 -08:00
|
|
|
process_instruction_as_one_arg(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2021-12-17 05:01:12 -08:00
|
|
|
&instruction,
|
2021-07-06 12:24:10 -07:00
|
|
|
Err(InstructionError::MissingRequiredSignature),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test SetLockupChecked with lockup custodian signer
|
2021-12-17 05:01:12 -08:00
|
|
|
let stake_account = AccountSharedData::new_data_with_space(
|
|
|
|
42,
|
|
|
|
&StakeState::Initialized(Meta::auto(&withdrawer)),
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2021-12-17 05:01:12 -08:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2021-07-06 12:24:10 -07:00
|
|
|
|
2022-01-31 08:53:50 -08:00
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2021-12-17 05:01:12 -08:00
|
|
|
&instruction.data,
|
|
|
|
vec![
|
2022-01-31 08:53:50 -08:00
|
|
|
(clock_address, clock_account),
|
2021-12-17 05:01:12 -08:00
|
|
|
(stake_address, stake_account),
|
|
|
|
(withdrawer, withdrawer_account),
|
|
|
|
(custodian, custodian_account),
|
|
|
|
],
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2021-12-17 05:01:12 -08:00
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: withdrawer,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: custodian,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
2021-07-06 12:24:10 -07:00
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
}
|
2022-03-09 14:48:10 -08:00
|
|
|
|
2022-07-06 14:15:01 -07:00
|
|
|
#[test_case(feature_set_old_behavior(); "old_behavior")]
|
|
|
|
#[test_case(feature_set_new_behavior(); "new_behavior")]
|
2023-04-03 08:23:24 -07:00
|
|
|
fn test_stake_initialize(feature_set: Arc<FeatureSet>) {
|
2022-03-16 14:36:23 -07:00
|
|
|
let rent = Rent::default();
|
2022-04-19 10:04:12 -07:00
|
|
|
let rent_exempt_reserve = rent.minimum_balance(StakeState::size_of());
|
2022-05-07 16:01:05 -07:00
|
|
|
let stake_lamports = rent_exempt_reserve;
|
2022-03-09 14:48:10 -08:00
|
|
|
let stake_address = solana_sdk::pubkey::new_rand();
|
2022-04-19 10:04:12 -07:00
|
|
|
let stake_account = AccountSharedData::new(stake_lamports, StakeState::size_of(), &id());
|
2022-03-09 14:48:10 -08:00
|
|
|
let custodian_address = solana_sdk::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()),
|
2023-04-03 08:23:24 -07:00
|
|
|
(rent::id(), create_account_shared_data_for_test(&rent)),
|
2022-03-09 14:48:10 -08:00
|
|
|
];
|
|
|
|
let instruction_accounts = vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-03-09 14:48:10 -08:00
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: rent::id(),
|
2022-03-09 14:48:10 -08:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
// should pass
|
|
|
|
let accounts = process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&instruction_data,
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
// check that we see what we expect
|
|
|
|
assert_eq!(
|
|
|
|
from(&accounts[0]).unwrap(),
|
|
|
|
StakeState::Initialized(Meta {
|
2022-03-16 14:36:23 -07:00
|
|
|
authorized: Authorized::auto(&stake_address),
|
|
|
|
rent_exempt_reserve,
|
2022-03-09 14:48:10 -08:00
|
|
|
lockup,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
// 2nd time fails, can't move it from anything other than uninit->init
|
|
|
|
transaction_accounts[0] = (stake_address, accounts[0].clone());
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&instruction_data,
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
|
|
|
transaction_accounts[0] = (stake_address, stake_account);
|
|
|
|
|
2022-05-07 16:01:05 -07:00
|
|
|
// not enough balance for rent
|
2022-03-09 14:48:10 -08:00
|
|
|
transaction_accounts[1] = (
|
2023-04-03 08:23:24 -07:00
|
|
|
rent::id(),
|
|
|
|
create_account_shared_data_for_test(&Rent {
|
2022-03-16 14:36:23 -07:00
|
|
|
lamports_per_byte_year: rent.lamports_per_byte_year + 1,
|
|
|
|
..rent
|
2022-03-09 14:48:10 -08:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&instruction_data,
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Err(InstructionError::InsufficientFunds),
|
|
|
|
);
|
|
|
|
|
|
|
|
// incorrect account sizes
|
|
|
|
let stake_account =
|
2022-04-19 10:04:12 -07:00
|
|
|
AccountSharedData::new(stake_lamports, StakeState::size_of() + 1, &id());
|
2022-03-09 14:48:10 -08:00
|
|
|
transaction_accounts[0] = (stake_address, stake_account);
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&instruction_data,
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
|
|
|
|
|
|
|
let stake_account =
|
2022-04-19 10:04:12 -07:00
|
|
|
AccountSharedData::new(stake_lamports, StakeState::size_of() - 1, &id());
|
2022-03-09 14:48:10 -08:00
|
|
|
transaction_accounts[0] = (stake_address, stake_account);
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&instruction_data,
|
|
|
|
transaction_accounts,
|
|
|
|
instruction_accounts,
|
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-07-06 14:15:01 -07:00
|
|
|
#[test_case(feature_set_old_behavior(); "old_behavior")]
|
|
|
|
#[test_case(feature_set_new_behavior(); "new_behavior")]
|
2023-04-03 08:23:24 -07:00
|
|
|
fn test_authorize(feature_set: Arc<FeatureSet>) {
|
2022-03-09 14:48:10 -08:00
|
|
|
let authority_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let authority_address_2 = solana_sdk::pubkey::new_rand();
|
|
|
|
let stake_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let stake_lamports = 42;
|
|
|
|
let stake_account = AccountSharedData::new_data_with_space(
|
|
|
|
stake_lamports,
|
|
|
|
&StakeState::default(),
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-03-09 14:48:10 -08:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let to_address = solana_sdk::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()),
|
|
|
|
(
|
2023-04-03 08:23:24 -07:00
|
|
|
clock::id(),
|
|
|
|
create_account_shared_data_for_test(&Clock::default()),
|
2022-03-09 14:48:10 -08:00
|
|
|
),
|
|
|
|
(
|
2023-04-03 08:23:24 -07:00
|
|
|
stake_history::id(),
|
|
|
|
create_account_shared_data_for_test(&StakeHistory::default()),
|
2022-03-09 14:48:10 -08:00
|
|
|
),
|
|
|
|
];
|
|
|
|
let mut instruction_accounts = vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: true,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-03-09 14:48:10 -08:00
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: clock::id(),
|
2022-03-09 14:48:10 -08:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
2022-03-15 07:53:11 -07:00
|
|
|
AccountMeta {
|
|
|
|
pubkey: authority_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
2022-03-09 14:48:10 -08:00
|
|
|
];
|
|
|
|
|
|
|
|
// should fail, uninit
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::Authorize(
|
|
|
|
authority_address,
|
|
|
|
StakeAuthorize::Staker,
|
|
|
|
))
|
|
|
|
.unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
|
|
|
|
|
|
|
// should pass
|
|
|
|
let stake_account = AccountSharedData::new_data_with_space(
|
|
|
|
stake_lamports,
|
|
|
|
&StakeState::Initialized(Meta::auto(&stake_address)),
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-03-09 14:48:10 -08:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
transaction_accounts[0] = (stake_address, stake_account);
|
|
|
|
let accounts = process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&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(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&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 StakeState::Initialized(Meta { authorized, .. }) = from(&accounts[0]).unwrap() {
|
|
|
|
assert_eq!(authorized.staker, authority_address);
|
|
|
|
assert_eq!(authorized.withdrawer, authority_address);
|
|
|
|
} else {
|
|
|
|
panic!();
|
|
|
|
}
|
|
|
|
|
|
|
|
// A second authorization signed by the stake account should fail
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::Authorize(
|
|
|
|
authority_address_2,
|
|
|
|
StakeAuthorize::Staker,
|
|
|
|
))
|
|
|
|
.unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Err(InstructionError::MissingRequiredSignature),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test a second authorization by the new authority_address
|
|
|
|
instruction_accounts[0].is_signer = false;
|
2022-03-15 07:53:11 -07:00
|
|
|
instruction_accounts[2].is_signer = true;
|
2022-03-09 14:48:10 -08:00
|
|
|
let accounts = process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::Authorize(
|
|
|
|
authority_address_2,
|
|
|
|
StakeAuthorize::Staker,
|
|
|
|
))
|
|
|
|
.unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
if let StakeState::Initialized(Meta { authorized, .. }) = from(&accounts[0]).unwrap() {
|
|
|
|
assert_eq!(authorized.staker, authority_address_2);
|
|
|
|
} else {
|
|
|
|
panic!();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test a successful action by the currently authorized withdrawer
|
|
|
|
let mut instruction_accounts = vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-03-09 14:48:10 -08:00
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: to_address,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-03-09 14:48:10 -08:00
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: clock::id(),
|
2022-03-09 14:48:10 -08:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: stake_history::id(),
|
2022-03-09 14:48:10 -08:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: authority_address,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
let accounts = process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::Withdraw(stake_lamports)).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
assert_eq!(from(&accounts[0]).unwrap(), StakeState::Uninitialized);
|
|
|
|
|
|
|
|
// Test that withdrawal to account fails without authorized withdrawer
|
|
|
|
instruction_accounts[4].is_signer = false;
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::Withdraw(stake_lamports)).unwrap(),
|
|
|
|
transaction_accounts,
|
|
|
|
instruction_accounts,
|
|
|
|
Err(InstructionError::MissingRequiredSignature),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-07-06 14:15:01 -07:00
|
|
|
#[test_case(feature_set_old_behavior(); "old_behavior")]
|
|
|
|
#[test_case(feature_set_new_behavior(); "new_behavior")]
|
2023-04-03 08:23:24 -07:00
|
|
|
fn test_authorize_override(feature_set: Arc<FeatureSet>) {
|
2022-03-09 14:48:10 -08:00
|
|
|
let authority_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let mallory_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let stake_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let stake_lamports = 42;
|
|
|
|
let stake_account = AccountSharedData::new_data_with_space(
|
|
|
|
stake_lamports,
|
|
|
|
&StakeState::Initialized(Meta::auto(&stake_address)),
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-03-09 14:48:10 -08:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let mut transaction_accounts = vec![
|
|
|
|
(stake_address, stake_account),
|
|
|
|
(authority_address, AccountSharedData::default()),
|
|
|
|
(
|
2023-04-03 08:23:24 -07:00
|
|
|
clock::id(),
|
|
|
|
create_account_shared_data_for_test(&Clock::default()),
|
2022-03-09 14:48:10 -08:00
|
|
|
),
|
|
|
|
];
|
|
|
|
let mut instruction_accounts = vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: true,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-03-09 14:48:10 -08:00
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: clock::id(),
|
2022-03-09 14:48:10 -08:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
2022-03-15 07:53:11 -07:00
|
|
|
AccountMeta {
|
|
|
|
pubkey: authority_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
2022-03-09 14:48:10 -08:00
|
|
|
];
|
|
|
|
|
|
|
|
// Authorize a staker pubkey and move the withdrawer key into cold storage.
|
|
|
|
let accounts = process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::Authorize(
|
|
|
|
authority_address,
|
|
|
|
StakeAuthorize::Staker,
|
|
|
|
))
|
|
|
|
.unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
transaction_accounts[0] = (stake_address, accounts[0].clone());
|
|
|
|
|
|
|
|
// Attack! The stake key (a hot key) is stolen and used to authorize a new staker.
|
|
|
|
instruction_accounts[0].is_signer = false;
|
2022-03-15 07:53:11 -07:00
|
|
|
instruction_accounts[2].is_signer = true;
|
2022-03-09 14:48:10 -08:00
|
|
|
let accounts = process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::Authorize(
|
|
|
|
mallory_address,
|
|
|
|
StakeAuthorize::Staker,
|
|
|
|
))
|
|
|
|
.unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
transaction_accounts[0] = (stake_address, accounts[0].clone());
|
|
|
|
|
|
|
|
// Verify the original staker no longer has access.
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::Authorize(
|
|
|
|
authority_address,
|
|
|
|
StakeAuthorize::Staker,
|
|
|
|
))
|
|
|
|
.unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Err(InstructionError::MissingRequiredSignature),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Verify the withdrawer (pulled from cold storage) can save the day.
|
|
|
|
instruction_accounts[0].is_signer = true;
|
2022-03-15 07:53:11 -07:00
|
|
|
instruction_accounts[2].is_signer = false;
|
2022-03-09 14:48:10 -08:00
|
|
|
let accounts = process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::Authorize(
|
|
|
|
authority_address,
|
|
|
|
StakeAuthorize::Withdrawer,
|
|
|
|
))
|
|
|
|
.unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
transaction_accounts[0] = (stake_address, accounts[0].clone());
|
|
|
|
|
|
|
|
// Attack! Verify the staker cannot be used to authorize a withdraw.
|
|
|
|
instruction_accounts[0].is_signer = false;
|
2022-03-15 07:53:11 -07:00
|
|
|
instruction_accounts[2] = AccountMeta {
|
2022-03-09 14:48:10 -08:00
|
|
|
pubkey: mallory_address,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
2022-03-15 07:53:11 -07:00
|
|
|
};
|
2022-03-09 14:48:10 -08:00
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::Authorize(
|
|
|
|
authority_address,
|
|
|
|
StakeAuthorize::Withdrawer,
|
|
|
|
))
|
|
|
|
.unwrap(),
|
|
|
|
transaction_accounts,
|
|
|
|
instruction_accounts,
|
|
|
|
Err(InstructionError::MissingRequiredSignature),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-07-06 14:15:01 -07:00
|
|
|
#[test_case(feature_set_old_behavior(); "old_behavior")]
|
|
|
|
#[test_case(feature_set_new_behavior(); "new_behavior")]
|
2023-04-03 08:23:24 -07:00
|
|
|
fn test_authorize_with_seed(feature_set: Arc<FeatureSet>) {
|
2022-03-09 14:48:10 -08:00
|
|
|
let authority_base_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let authority_address = solana_sdk::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,
|
|
|
|
&StakeState::Initialized(Meta::auto(&stake_address)),
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-03-09 14:48:10 -08:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let mut transaction_accounts = vec![
|
|
|
|
(stake_address, stake_account),
|
|
|
|
(authority_base_address, AccountSharedData::default()),
|
|
|
|
(
|
2023-04-03 08:23:24 -07:00
|
|
|
clock::id(),
|
|
|
|
create_account_shared_data_for_test(&Clock::default()),
|
2022-03-09 14:48:10 -08:00
|
|
|
),
|
|
|
|
];
|
|
|
|
let mut instruction_accounts = vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: true,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-03-09 14:48:10 -08:00
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: authority_base_address,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: clock::id(),
|
2022-03-09 14:48:10 -08:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
// Wrong seed
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&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(InstructionError::MissingRequiredSignature),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Wrong base
|
|
|
|
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(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&instruction_data,
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Err(InstructionError::MissingRequiredSignature),
|
|
|
|
);
|
|
|
|
instruction_accounts[1].pubkey = authority_base_address;
|
|
|
|
|
|
|
|
// Set stake authority
|
|
|
|
let accounts = process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&instruction_data,
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
transaction_accounts[0] = (stake_address, accounts[0].clone());
|
|
|
|
|
|
|
|
// Set withdraw authority
|
|
|
|
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(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&instruction_data,
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
transaction_accounts[0] = (stake_address, accounts[0].clone());
|
|
|
|
|
|
|
|
// No longer withdraw authority
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&instruction_data,
|
|
|
|
transaction_accounts,
|
|
|
|
instruction_accounts,
|
|
|
|
Err(InstructionError::MissingRequiredSignature),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-07-06 14:15:01 -07:00
|
|
|
#[test_case(feature_set_old_behavior(); "old_behavior")]
|
|
|
|
#[test_case(feature_set_new_behavior(); "new_behavior")]
|
2023-04-03 08:23:24 -07:00
|
|
|
fn test_authorize_delegated_stake(feature_set: Arc<FeatureSet>) {
|
2022-03-09 14:48:10 -08:00
|
|
|
let authority_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let stake_address = solana_sdk::pubkey::new_rand();
|
2022-05-07 16:01:05 -07:00
|
|
|
let minimum_delegation = crate::get_minimum_delegation(&feature_set);
|
2022-04-15 11:30:45 -07:00
|
|
|
let stake_lamports = minimum_delegation;
|
2022-03-09 14:48:10 -08:00
|
|
|
let stake_account = AccountSharedData::new_data_with_space(
|
|
|
|
stake_lamports,
|
|
|
|
&StakeState::Initialized(Meta::auto(&stake_address)),
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-03-09 14:48:10 -08:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let vote_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let vote_account =
|
|
|
|
vote_state::create_account(&vote_address, &solana_sdk::pubkey::new_rand(), 0, 100);
|
|
|
|
let vote_address_2 = solana_sdk::pubkey::new_rand();
|
|
|
|
let mut vote_account_2 =
|
|
|
|
vote_state::create_account(&vote_address_2, &solana_sdk::pubkey::new_rand(), 0, 100);
|
|
|
|
vote_account_2.set_state(&VoteState::default()).unwrap();
|
|
|
|
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()),
|
|
|
|
),
|
|
|
|
(
|
2023-04-03 08:23:24 -07:00
|
|
|
clock::id(),
|
|
|
|
create_account_shared_data_for_test(&Clock::default()),
|
2022-03-09 14:48:10 -08:00
|
|
|
),
|
|
|
|
(
|
2023-04-03 08:23:24 -07:00
|
|
|
stake_history::id(),
|
|
|
|
create_account_shared_data_for_test(&StakeHistory::default()),
|
2022-03-09 14:48:10 -08:00
|
|
|
),
|
|
|
|
(
|
|
|
|
stake_config::id(),
|
|
|
|
config::create_account(0, &stake_config::Config::default()),
|
|
|
|
),
|
|
|
|
];
|
|
|
|
let mut instruction_accounts = vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: true,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-03-09 14:48:10 -08:00
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: vote_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: clock::id(),
|
2022-03-09 14:48:10 -08:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: stake_history::id(),
|
2022-03-09 14:48:10 -08:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_config::id(),
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
// delegate stake
|
|
|
|
let accounts = process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::DelegateStake).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
transaction_accounts[0] = (stake_address, accounts[0].clone());
|
|
|
|
|
|
|
|
// deactivate, so we can re-delegate
|
|
|
|
let accounts = process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::Deactivate).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: true,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-03-09 14:48:10 -08:00
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: clock::id(),
|
2022-03-09 14:48:10 -08:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
transaction_accounts[0] = (stake_address, accounts[0].clone());
|
|
|
|
|
|
|
|
// authorize
|
|
|
|
let accounts = process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::Authorize(
|
|
|
|
authority_address,
|
|
|
|
StakeAuthorize::Staker,
|
|
|
|
))
|
|
|
|
.unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: true,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-03-09 14:48:10 -08:00
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: clock::id(),
|
2022-03-09 14:48:10 -08:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
2022-03-15 07:53:11 -07:00
|
|
|
AccountMeta {
|
|
|
|
pubkey: authority_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
2022-03-09 14:48:10 -08:00
|
|
|
],
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
transaction_accounts[0] = (stake_address, accounts[0].clone());
|
|
|
|
assert_eq!(
|
|
|
|
authorized_from(&accounts[0]).unwrap().staker,
|
|
|
|
authority_address
|
|
|
|
);
|
|
|
|
|
|
|
|
// Random other account should fail
|
|
|
|
instruction_accounts[0].is_signer = false;
|
|
|
|
instruction_accounts[1].pubkey = vote_address_2;
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::DelegateStake).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Err(InstructionError::MissingRequiredSignature),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Authorized staker should succeed
|
|
|
|
instruction_accounts.push(AccountMeta {
|
|
|
|
pubkey: authority_address,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
});
|
|
|
|
let accounts = process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&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,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test another staking action
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::Deactivate).unwrap(),
|
|
|
|
transaction_accounts,
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-03-09 14:48:10 -08:00
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: clock::id(),
|
2022-03-09 14:48:10 -08:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: authority_address,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-07-06 14:15:01 -07:00
|
|
|
#[test_case(feature_set_old_behavior(); "old_behavior")]
|
|
|
|
#[test_case(feature_set_new_behavior(); "new_behavior")]
|
2023-04-03 08:23:24 -07:00
|
|
|
fn test_stake_delegate(feature_set: Arc<FeatureSet>) {
|
2022-03-09 14:48:10 -08:00
|
|
|
let mut vote_state = VoteState::default();
|
|
|
|
for i in 0..1000 {
|
2022-08-03 23:12:59 -07:00
|
|
|
vote_state::process_slot_vote_unchecked(&mut vote_state, i);
|
2022-03-09 14:48:10 -08:00
|
|
|
}
|
|
|
|
let vote_state_credits = vote_state.credits();
|
|
|
|
let vote_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let vote_address_2 = solana_sdk::pubkey::new_rand();
|
|
|
|
let mut vote_account =
|
|
|
|
vote_state::create_account(&vote_address, &solana_sdk::pubkey::new_rand(), 0, 100);
|
|
|
|
let mut vote_account_2 =
|
|
|
|
vote_state::create_account(&vote_address_2, &solana_sdk::pubkey::new_rand(), 0, 100);
|
|
|
|
vote_account
|
|
|
|
.set_state(&VoteStateVersions::new_current(vote_state.clone()))
|
|
|
|
.unwrap();
|
|
|
|
vote_account_2
|
|
|
|
.set_state(&VoteStateVersions::new_current(vote_state))
|
|
|
|
.unwrap();
|
2022-05-07 16:01:05 -07:00
|
|
|
let minimum_delegation = crate::get_minimum_delegation(&feature_set);
|
2022-04-15 11:30:45 -07:00
|
|
|
let stake_lamports = minimum_delegation;
|
2022-03-09 14:48:10 -08:00
|
|
|
let stake_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let mut stake_account = AccountSharedData::new_data_with_space(
|
|
|
|
stake_lamports,
|
|
|
|
&StakeState::Initialized(Meta {
|
|
|
|
authorized: Authorized {
|
|
|
|
staker: stake_address,
|
|
|
|
withdrawer: stake_address,
|
|
|
|
},
|
|
|
|
..Meta::default()
|
|
|
|
}),
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-03-09 14:48:10 -08:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let mut clock = Clock {
|
|
|
|
epoch: 1,
|
|
|
|
..Clock::default()
|
|
|
|
};
|
|
|
|
let mut transaction_accounts = vec![
|
|
|
|
(stake_address, stake_account.clone()),
|
|
|
|
(vote_address, vote_account),
|
|
|
|
(vote_address_2, vote_account_2.clone()),
|
2023-04-03 08:23:24 -07:00
|
|
|
(clock::id(), create_account_shared_data_for_test(&clock)),
|
2022-03-09 14:48:10 -08:00
|
|
|
(
|
2023-04-03 08:23:24 -07:00
|
|
|
stake_history::id(),
|
|
|
|
create_account_shared_data_for_test(&StakeHistory::default()),
|
2022-03-09 14:48:10 -08:00
|
|
|
),
|
|
|
|
(
|
|
|
|
stake_config::id(),
|
|
|
|
config::create_account(0, &stake_config::Config::default()),
|
|
|
|
),
|
|
|
|
];
|
|
|
|
let mut instruction_accounts = vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: true,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-03-09 14:48:10 -08:00
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: vote_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: clock::id(),
|
2022-03-09 14:48:10 -08:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: stake_history::id(),
|
2022-03-09 14:48:10 -08:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_config::id(),
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
// should fail, unsigned stake account
|
|
|
|
instruction_accounts[0].is_signer = false;
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::DelegateStake).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Err(InstructionError::MissingRequiredSignature),
|
|
|
|
);
|
|
|
|
instruction_accounts[0].is_signer = true;
|
|
|
|
|
|
|
|
// should pass
|
|
|
|
let accounts = process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::DelegateStake).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
// verify that delegate() looks right, compare against hand-rolled
|
|
|
|
assert_eq!(
|
|
|
|
stake_from(&accounts[0]).unwrap(),
|
|
|
|
Stake {
|
|
|
|
delegation: Delegation {
|
|
|
|
voter_pubkey: vote_address,
|
|
|
|
stake: stake_lamports,
|
|
|
|
activation_epoch: clock.epoch,
|
|
|
|
deactivation_epoch: std::u64::MAX,
|
|
|
|
..Delegation::default()
|
|
|
|
},
|
|
|
|
credits_observed: vote_state_credits,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
// verify that delegate fails as stake is active and not deactivating
|
|
|
|
clock.epoch += 1;
|
|
|
|
transaction_accounts[0] = (stake_address, accounts[0].clone());
|
2023-04-03 08:23:24 -07:00
|
|
|
transaction_accounts[3] = (clock::id(), create_account_shared_data_for_test(&clock));
|
2022-03-09 14:48:10 -08:00
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::DelegateStake).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Err(StakeError::TooSoonToRedelegate.into()),
|
|
|
|
);
|
|
|
|
|
|
|
|
// deactivate
|
|
|
|
let accounts = process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::Deactivate).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: true,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-03-09 14:48:10 -08:00
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: clock::id(),
|
2022-03-09 14:48:10 -08:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
|
|
|
|
// verify that delegate to a different vote account fails
|
|
|
|
// during deactivation
|
|
|
|
transaction_accounts[0] = (stake_address, accounts[0].clone());
|
|
|
|
instruction_accounts[1].pubkey = vote_address_2;
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::DelegateStake).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Err(StakeError::TooSoonToRedelegate.into()),
|
|
|
|
);
|
|
|
|
instruction_accounts[1].pubkey = vote_address;
|
|
|
|
|
|
|
|
// verify that delegate succeeds to same vote account
|
|
|
|
// when stake is deactivating
|
|
|
|
let accounts_2 = process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::DelegateStake).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
// verify that deactivation has been cleared
|
|
|
|
let stake = stake_from(&accounts_2[0]).unwrap();
|
|
|
|
assert_eq!(stake.delegation.deactivation_epoch, std::u64::MAX);
|
|
|
|
|
|
|
|
// verify that delegate to a different vote account fails
|
|
|
|
// if stake is still active
|
|
|
|
transaction_accounts[0] = (stake_address, accounts_2[0].clone());
|
|
|
|
instruction_accounts[1].pubkey = vote_address_2;
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::DelegateStake).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Err(StakeError::TooSoonToRedelegate.into()),
|
|
|
|
);
|
|
|
|
|
|
|
|
// without stake history, cool down is instantaneous
|
|
|
|
clock.epoch += 1;
|
2023-04-03 08:23:24 -07:00
|
|
|
transaction_accounts[3] = (clock::id(), create_account_shared_data_for_test(&clock));
|
2022-03-09 14:48:10 -08:00
|
|
|
|
|
|
|
// verify that delegate can be called to new vote account, 2nd is redelegate
|
|
|
|
transaction_accounts[0] = (stake_address, accounts[0].clone());
|
|
|
|
let accounts = process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::DelegateStake).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
instruction_accounts[1].pubkey = vote_address;
|
|
|
|
// verify that delegate() looks right, compare against hand-rolled
|
|
|
|
assert_eq!(
|
|
|
|
stake_from(&accounts[0]).unwrap(),
|
|
|
|
Stake {
|
|
|
|
delegation: Delegation {
|
|
|
|
voter_pubkey: vote_address_2,
|
|
|
|
stake: stake_lamports,
|
|
|
|
activation_epoch: clock.epoch,
|
|
|
|
deactivation_epoch: std::u64::MAX,
|
|
|
|
..Delegation::default()
|
|
|
|
},
|
|
|
|
credits_observed: vote_state_credits,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
// signed but faked vote account
|
|
|
|
transaction_accounts[1] = (vote_address_2, vote_account_2);
|
|
|
|
transaction_accounts[1]
|
|
|
|
.1
|
|
|
|
.set_owner(solana_sdk::pubkey::new_rand());
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::DelegateStake).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Err(solana_sdk::instruction::InstructionError::IncorrectProgramId),
|
|
|
|
);
|
|
|
|
|
|
|
|
// verify that non-stakes fail delegate()
|
|
|
|
let stake_state = StakeState::RewardsPool;
|
|
|
|
stake_account.set_state(&stake_state).unwrap();
|
|
|
|
transaction_accounts[0] = (stake_address, stake_account);
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::DelegateStake).unwrap(),
|
|
|
|
transaction_accounts,
|
|
|
|
instruction_accounts,
|
|
|
|
Err(solana_sdk::instruction::InstructionError::IncorrectProgramId),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-07-06 14:15:01 -07:00
|
|
|
#[test_case(feature_set_old_behavior(); "old_behavior")]
|
|
|
|
#[test_case(feature_set_new_behavior(); "new_behavior")]
|
2023-04-03 08:23:24 -07:00
|
|
|
fn test_redelegate_consider_balance_changes(feature_set: Arc<FeatureSet>) {
|
2022-03-09 14:48:10 -08:00
|
|
|
let mut clock = Clock::default();
|
|
|
|
let rent = Rent::default();
|
2022-04-19 10:04:12 -07:00
|
|
|
let rent_exempt_reserve = rent.minimum_balance(StakeState::size_of());
|
2022-03-09 14:48:10 -08:00
|
|
|
let initial_lamports = 4242424242;
|
|
|
|
let stake_lamports = rent_exempt_reserve + initial_lamports;
|
|
|
|
let recipient_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let authority_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let vote_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let vote_account =
|
|
|
|
vote_state::create_account(&vote_address, &solana_sdk::pubkey::new_rand(), 0, 100);
|
|
|
|
let stake_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let stake_account = AccountSharedData::new_data_with_space(
|
|
|
|
stake_lamports,
|
|
|
|
&StakeState::Initialized(Meta {
|
|
|
|
rent_exempt_reserve,
|
|
|
|
..Meta::auto(&authority_address)
|
|
|
|
}),
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-03-09 14:48:10 -08:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
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()),
|
2023-04-03 08:23:24 -07:00
|
|
|
(clock::id(), create_account_shared_data_for_test(&clock)),
|
2022-03-09 14:48:10 -08:00
|
|
|
(
|
2023-04-03 08:23:24 -07:00
|
|
|
stake_history::id(),
|
|
|
|
create_account_shared_data_for_test(&StakeHistory::default()),
|
2022-03-09 14:48:10 -08:00
|
|
|
),
|
|
|
|
(
|
|
|
|
stake_config::id(),
|
|
|
|
config::create_account(0, &stake_config::Config::default()),
|
|
|
|
),
|
|
|
|
];
|
|
|
|
let delegate_instruction_accounts = vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-03-09 14:48:10 -08:00
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: vote_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: clock::id(),
|
2022-03-09 14:48:10 -08:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: stake_history::id(),
|
2022-03-09 14:48:10 -08:00
|
|
|
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,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-03-09 14:48:10 -08:00
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: clock::id(),
|
2022-03-09 14:48:10 -08:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: authority_address,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
let accounts = process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::DelegateStake).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
delegate_instruction_accounts.clone(),
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
transaction_accounts[0] = (stake_address, accounts[0].clone());
|
|
|
|
|
|
|
|
clock.epoch += 1;
|
2023-04-03 08:23:24 -07:00
|
|
|
transaction_accounts[2] = (clock::id(), create_account_shared_data_for_test(&clock));
|
2022-03-09 14:48:10 -08:00
|
|
|
let accounts = process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::Deactivate).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
deactivate_instruction_accounts.clone(),
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
transaction_accounts[0] = (stake_address, accounts[0].clone());
|
|
|
|
|
|
|
|
// Once deactivated, we withdraw stake to new account
|
|
|
|
clock.epoch += 1;
|
2023-04-03 08:23:24 -07:00
|
|
|
transaction_accounts[2] = (clock::id(), create_account_shared_data_for_test(&clock));
|
2022-03-09 14:48:10 -08:00
|
|
|
let withdraw_lamports = initial_lamports / 2;
|
|
|
|
let accounts = process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::Withdraw(withdraw_lamports)).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-03-09 14:48:10 -08:00
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: recipient_address,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-03-09 14:48:10 -08:00
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: clock::id(),
|
2022-03-09 14:48:10 -08:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: stake_history::id(),
|
2022-03-09 14:48:10 -08:00
|
|
|
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;
|
2023-04-03 08:23:24 -07:00
|
|
|
transaction_accounts[2] = (clock::id(), create_account_shared_data_for_test(&clock));
|
2022-03-09 14:48:10 -08:00
|
|
|
let accounts = process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&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;
|
2023-04-03 08:23:24 -07:00
|
|
|
transaction_accounts[2] = (clock::id(), create_account_shared_data_for_test(&clock));
|
2022-03-09 14:48:10 -08:00
|
|
|
let accounts = process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::Deactivate).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
deactivate_instruction_accounts,
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
transaction_accounts[0] = (stake_address, accounts[0].clone());
|
|
|
|
|
|
|
|
// Out of band deposit
|
|
|
|
transaction_accounts[0]
|
|
|
|
.1
|
|
|
|
.checked_add_lamports(withdraw_lamports)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
clock.epoch += 1;
|
2023-04-03 08:23:24 -07:00
|
|
|
transaction_accounts[2] = (clock::id(), create_account_shared_data_for_test(&clock));
|
2022-03-09 14:48:10 -08:00
|
|
|
let accounts = process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&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,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-07-06 14:15:01 -07:00
|
|
|
#[test_case(feature_set_old_behavior(); "old_behavior")]
|
|
|
|
#[test_case(feature_set_new_behavior(); "new_behavior")]
|
2023-04-03 08:23:24 -07:00
|
|
|
fn test_split(feature_set: Arc<FeatureSet>) {
|
2022-03-09 14:48:10 -08:00
|
|
|
let stake_address = solana_sdk::pubkey::new_rand();
|
2022-05-07 16:01:05 -07:00
|
|
|
let minimum_delegation = crate::get_minimum_delegation(&feature_set);
|
2022-04-01 22:11:10 -07:00
|
|
|
let stake_lamports = minimum_delegation * 2;
|
2022-03-09 14:48:10 -08:00
|
|
|
let split_to_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let split_to_account = AccountSharedData::new_data_with_space(
|
|
|
|
0,
|
|
|
|
&StakeState::Uninitialized,
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-03-09 14:48:10 -08:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let mut transaction_accounts = vec![
|
|
|
|
(stake_address, AccountSharedData::default()),
|
|
|
|
(split_to_address, split_to_account),
|
2023-04-03 08:23:24 -07:00
|
|
|
(
|
|
|
|
rent::id(),
|
|
|
|
create_account_shared_data_for_test(&Rent {
|
|
|
|
lamports_per_byte_year: 0,
|
|
|
|
..Rent::default()
|
|
|
|
}),
|
|
|
|
),
|
2022-03-09 14:48:10 -08:00
|
|
|
];
|
|
|
|
let instruction_accounts = vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: true,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-03-09 14:48:10 -08:00
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: split_to_address,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-03-09 14:48:10 -08:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2022-05-07 16:01:05 -07:00
|
|
|
let feature_set = Arc::new(feature_set);
|
2022-03-31 01:46:35 -07:00
|
|
|
|
2022-03-09 14:48:10 -08:00
|
|
|
for state in [
|
|
|
|
StakeState::Initialized(Meta::auto(&stake_address)),
|
|
|
|
just_stake(Meta::auto(&stake_address), stake_lamports),
|
|
|
|
] {
|
|
|
|
let stake_account = AccountSharedData::new_data_with_space(
|
|
|
|
stake_lamports,
|
|
|
|
&state,
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-03-09 14:48:10 -08:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
transaction_accounts[0] = (stake_address, stake_account);
|
|
|
|
|
|
|
|
// should fail, split more than available
|
2023-04-03 08:23:24 -07:00
|
|
|
process_instruction(
|
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::Split(stake_lamports + 1)).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Err(InstructionError::InsufficientFunds),
|
|
|
|
);
|
|
|
|
|
|
|
|
// should pass
|
2023-04-03 08:23:24 -07:00
|
|
|
let accounts = process_instruction(
|
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::Split(stake_lamports / 2)).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
// no lamport leakage
|
|
|
|
assert_eq!(
|
|
|
|
accounts[0].lamports() + accounts[1].lamports(),
|
|
|
|
stake_lamports
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(from(&accounts[0]).unwrap(), from(&accounts[1]).unwrap());
|
|
|
|
match state {
|
|
|
|
StakeState::Initialized(_meta) => {
|
|
|
|
assert_eq!(from(&accounts[0]).unwrap(), state);
|
|
|
|
}
|
|
|
|
StakeState::Stake(_meta, _stake) => {
|
|
|
|
let stake_0 = from(&accounts[0]).unwrap().stake();
|
|
|
|
assert_eq!(stake_0.unwrap().delegation.stake, stake_lamports / 2);
|
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// should fail, fake owner of destination
|
|
|
|
let split_to_account = AccountSharedData::new_data_with_space(
|
|
|
|
0,
|
|
|
|
&StakeState::Uninitialized,
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-03-09 14:48:10 -08:00
|
|
|
&solana_sdk::pubkey::new_rand(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
transaction_accounts[1] = (split_to_address, split_to_account);
|
2023-04-03 08:23:24 -07:00
|
|
|
process_instruction(
|
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::Split(stake_lamports / 2)).unwrap(),
|
|
|
|
transaction_accounts,
|
|
|
|
instruction_accounts,
|
|
|
|
Err(InstructionError::IncorrectProgramId),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-07-06 14:15:01 -07:00
|
|
|
#[test_case(feature_set_old_behavior(); "old_behavior")]
|
|
|
|
#[test_case(feature_set_new_behavior(); "new_behavior")]
|
2023-04-03 08:23:24 -07:00
|
|
|
fn test_withdraw_stake(feature_set: Arc<FeatureSet>) {
|
2022-03-09 14:48:10 -08:00
|
|
|
let recipient_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let authority_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let custodian_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let stake_address = solana_sdk::pubkey::new_rand();
|
2022-05-07 16:01:05 -07:00
|
|
|
let minimum_delegation = crate::get_minimum_delegation(&feature_set);
|
2022-04-01 22:11:10 -07:00
|
|
|
let stake_lamports = minimum_delegation;
|
2022-03-09 14:48:10 -08:00
|
|
|
let stake_account = AccountSharedData::new_data_with_space(
|
|
|
|
stake_lamports,
|
|
|
|
&StakeState::Uninitialized,
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-03-09 14:48:10 -08:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let vote_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let mut vote_account =
|
|
|
|
vote_state::create_account(&vote_address, &solana_sdk::pubkey::new_rand(), 0, 100);
|
|
|
|
vote_account
|
|
|
|
.set_state(&VoteStateVersions::new_current(VoteState::default()))
|
|
|
|
.unwrap();
|
|
|
|
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()),
|
|
|
|
(
|
2023-04-03 08:23:24 -07:00
|
|
|
clock::id(),
|
|
|
|
create_account_shared_data_for_test(&Clock::default()),
|
2022-03-09 14:48:10 -08:00
|
|
|
),
|
|
|
|
(
|
2023-04-03 08:23:24 -07:00
|
|
|
rent::id(),
|
|
|
|
create_account_shared_data_for_test(&Rent::free()),
|
2022-03-09 14:48:10 -08:00
|
|
|
),
|
|
|
|
(
|
2023-04-03 08:23:24 -07:00
|
|
|
stake_history::id(),
|
|
|
|
create_account_shared_data_for_test(&StakeHistory::default()),
|
2022-03-09 14:48:10 -08:00
|
|
|
),
|
|
|
|
(
|
|
|
|
stake_config::id(),
|
|
|
|
config::create_account(0, &stake_config::Config::default()),
|
|
|
|
),
|
|
|
|
];
|
|
|
|
let mut instruction_accounts = vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-03-09 14:48:10 -08:00
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: recipient_address,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-03-09 14:48:10 -08:00
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: clock::id(),
|
2022-03-09 14:48:10 -08:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: stake_history::id(),
|
2022-03-09 14:48:10 -08:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
// should fail, no signer
|
|
|
|
instruction_accounts[4].is_signer = false;
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::Withdraw(stake_lamports)).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Err(InstructionError::MissingRequiredSignature),
|
|
|
|
);
|
|
|
|
instruction_accounts[4].is_signer = true;
|
|
|
|
|
|
|
|
// should pass, signed keyed account and uninitialized
|
|
|
|
let accounts = process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::Withdraw(stake_lamports)).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
assert_eq!(accounts[0].lamports(), 0);
|
|
|
|
assert_eq!(from(&accounts[0]).unwrap(), StakeState::Uninitialized);
|
|
|
|
|
|
|
|
// initialize stake
|
|
|
|
let lockup = Lockup {
|
|
|
|
unix_timestamp: 0,
|
|
|
|
epoch: 0,
|
|
|
|
custodian: custodian_address,
|
|
|
|
};
|
|
|
|
let accounts = process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::Initialize(
|
|
|
|
Authorized::auto(&stake_address),
|
|
|
|
lockup,
|
|
|
|
))
|
|
|
|
.unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: true,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-03-09 14:48:10 -08:00
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: rent::id(),
|
2022-03-09 14:48:10 -08:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
transaction_accounts[0] = (stake_address, accounts[0].clone());
|
|
|
|
|
|
|
|
// should fail, signed keyed account and locked up, more than available
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::Withdraw(stake_lamports + 1)).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Err(InstructionError::InsufficientFunds),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Stake some lamports (available lamports for withdrawals will reduce to zero)
|
|
|
|
let accounts = process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::DelegateStake).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: true,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-03-09 14:48:10 -08:00
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: vote_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: clock::id(),
|
2022-03-09 14:48:10 -08:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: stake_history::id(),
|
2022-03-09 14:48:10 -08:00
|
|
|
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());
|
|
|
|
|
|
|
|
// simulate rewards
|
|
|
|
transaction_accounts[0].1.checked_add_lamports(10).unwrap();
|
|
|
|
|
|
|
|
// withdrawal before deactivate works for rewards amount
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::Withdraw(10)).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
|
|
|
|
// withdrawal of rewards fails if not in excess of stake
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::Withdraw(11)).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Err(InstructionError::InsufficientFunds),
|
|
|
|
);
|
|
|
|
|
|
|
|
// deactivate the stake before withdrawal
|
|
|
|
let accounts = process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::Deactivate).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: true,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-03-09 14:48:10 -08:00
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: clock::id(),
|
2022-03-09 14:48:10 -08:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
transaction_accounts[0] = (stake_address, accounts[0].clone());
|
|
|
|
|
|
|
|
// simulate time passing
|
|
|
|
let clock = Clock {
|
|
|
|
epoch: 100,
|
|
|
|
..Clock::default()
|
|
|
|
};
|
2023-04-03 08:23:24 -07:00
|
|
|
transaction_accounts[5] = (clock::id(), create_account_shared_data_for_test(&clock));
|
2022-03-09 14:48:10 -08:00
|
|
|
|
|
|
|
// Try to withdraw more than what's available
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::Withdraw(stake_lamports + 11)).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Err(InstructionError::InsufficientFunds),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Try to withdraw all lamports
|
|
|
|
let accounts = process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::Withdraw(stake_lamports + 10)).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
assert_eq!(accounts[0].lamports(), 0);
|
|
|
|
assert_eq!(from(&accounts[0]).unwrap(), StakeState::Uninitialized);
|
|
|
|
|
|
|
|
// overflow
|
|
|
|
let rent = Rent::default();
|
2022-04-19 10:04:12 -07:00
|
|
|
let rent_exempt_reserve = rent.minimum_balance(StakeState::size_of());
|
2022-03-09 14:48:10 -08:00
|
|
|
let stake_account = AccountSharedData::new_data_with_space(
|
|
|
|
1_000_000_000,
|
|
|
|
&StakeState::Initialized(Meta {
|
|
|
|
rent_exempt_reserve,
|
|
|
|
authorized: Authorized {
|
|
|
|
staker: authority_address,
|
|
|
|
withdrawer: authority_address,
|
|
|
|
},
|
|
|
|
lockup: Lockup::default(),
|
|
|
|
}),
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-03-09 14:48:10 -08:00
|
|
|
&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(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::Withdraw(u64::MAX - 10)).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Err(InstructionError::InsufficientFunds),
|
|
|
|
);
|
|
|
|
|
|
|
|
// should fail, invalid state
|
|
|
|
let stake_account = AccountSharedData::new_data_with_space(
|
|
|
|
stake_lamports,
|
|
|
|
&StakeState::RewardsPool,
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-03-09 14:48:10 -08:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
transaction_accounts[0] = (stake_address, stake_account);
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::Withdraw(stake_lamports)).unwrap(),
|
|
|
|
transaction_accounts,
|
|
|
|
instruction_accounts,
|
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-07-06 14:15:01 -07:00
|
|
|
#[test_case(feature_set_old_behavior(); "old_behavior")]
|
|
|
|
#[test_case(feature_set_new_behavior(); "new_behavior")]
|
2023-04-03 08:23:24 -07:00
|
|
|
fn test_withdraw_stake_before_warmup(feature_set: Arc<FeatureSet>) {
|
2022-03-09 14:48:10 -08:00
|
|
|
let recipient_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let stake_address = solana_sdk::pubkey::new_rand();
|
2022-05-07 16:01:05 -07:00
|
|
|
let minimum_delegation = crate::get_minimum_delegation(&feature_set);
|
2022-04-15 11:30:45 -07:00
|
|
|
let stake_lamports = minimum_delegation;
|
|
|
|
let total_lamports = stake_lamports + 33;
|
2022-03-09 14:48:10 -08:00
|
|
|
let stake_account = AccountSharedData::new_data_with_space(
|
|
|
|
total_lamports,
|
|
|
|
&StakeState::Initialized(Meta::auto(&stake_address)),
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-03-09 14:48:10 -08:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let vote_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let mut vote_account =
|
|
|
|
vote_state::create_account(&vote_address, &solana_sdk::pubkey::new_rand(), 0, 100);
|
|
|
|
vote_account
|
|
|
|
.set_state(&VoteStateVersions::new_current(VoteState::default()))
|
|
|
|
.unwrap();
|
|
|
|
let mut clock = Clock {
|
|
|
|
epoch: 16,
|
|
|
|
..Clock::default()
|
|
|
|
};
|
|
|
|
let mut transaction_accounts = vec![
|
|
|
|
(stake_address, stake_account),
|
|
|
|
(vote_address, vote_account),
|
|
|
|
(recipient_address, AccountSharedData::default()),
|
2023-04-03 08:23:24 -07:00
|
|
|
(clock::id(), create_account_shared_data_for_test(&clock)),
|
2022-03-09 14:48:10 -08:00
|
|
|
(
|
2023-04-03 08:23:24 -07:00
|
|
|
stake_history::id(),
|
|
|
|
create_account_shared_data_for_test(&StakeHistory::default()),
|
2022-03-09 14:48:10 -08:00
|
|
|
),
|
|
|
|
(
|
|
|
|
stake_config::id(),
|
|
|
|
config::create_account(0, &stake_config::Config::default()),
|
|
|
|
),
|
|
|
|
];
|
|
|
|
let instruction_accounts = vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-03-09 14:48:10 -08:00
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: recipient_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: clock::id(),
|
2022-03-09 14:48:10 -08:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: stake_history::id(),
|
2022-03-09 14:48:10 -08:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
// Stake some lamports (available lamports for withdrawals will reduce to zero)
|
|
|
|
let accounts = process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::DelegateStake).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: true,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-03-09 14:48:10 -08:00
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: vote_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: clock::id(),
|
2022-03-09 14:48:10 -08:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: stake_history::id(),
|
2022-03-09 14:48:10 -08:00
|
|
|
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());
|
|
|
|
|
|
|
|
// Try to withdraw stake
|
|
|
|
let stake_history = create_stake_history_from_delegations(
|
|
|
|
None,
|
|
|
|
0..clock.epoch,
|
|
|
|
&[stake_from(&accounts[0]).unwrap().delegation],
|
|
|
|
);
|
|
|
|
transaction_accounts[4] = (
|
2023-04-03 08:23:24 -07:00
|
|
|
stake_history::id(),
|
|
|
|
create_account_shared_data_for_test(&stake_history),
|
2022-03-09 14:48:10 -08:00
|
|
|
);
|
|
|
|
clock.epoch = 0;
|
2023-04-03 08:23:24 -07:00
|
|
|
transaction_accounts[3] = (clock::id(), create_account_shared_data_for_test(&clock));
|
2022-03-09 14:48:10 -08:00
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::Withdraw(
|
|
|
|
total_lamports - stake_lamports + 1,
|
|
|
|
))
|
|
|
|
.unwrap(),
|
|
|
|
transaction_accounts,
|
|
|
|
instruction_accounts,
|
|
|
|
Err(InstructionError::InsufficientFunds),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-07-06 14:15:01 -07:00
|
|
|
#[test_case(feature_set_old_behavior(); "old_behavior")]
|
|
|
|
#[test_case(feature_set_new_behavior(); "new_behavior")]
|
2023-04-03 08:23:24 -07:00
|
|
|
fn test_withdraw_lockup(feature_set: Arc<FeatureSet>) {
|
2022-03-09 14:48:10 -08:00
|
|
|
let recipient_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let custodian_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let stake_address = solana_sdk::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,
|
|
|
|
&StakeState::Initialized(meta),
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-03-09 14:48:10 -08:00
|
|
|
&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()),
|
2023-04-03 08:23:24 -07:00
|
|
|
(clock::id(), create_account_shared_data_for_test(&clock)),
|
2022-03-09 14:48:10 -08:00
|
|
|
(
|
2023-04-03 08:23:24 -07:00
|
|
|
stake_history::id(),
|
|
|
|
create_account_shared_data_for_test(&StakeHistory::default()),
|
2022-03-09 14:48:10 -08:00
|
|
|
),
|
|
|
|
];
|
|
|
|
let mut instruction_accounts = vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-03-09 14:48:10 -08:00
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: recipient_address,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-03-09 14:48:10 -08:00
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: clock::id(),
|
2022-03-09 14:48:10 -08:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: stake_history::id(),
|
2022-03-09 14:48:10 -08:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
// should fail, lockup is still in force
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::Withdraw(total_lamports)).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Err(StakeError::LockupInForce.into()),
|
|
|
|
);
|
|
|
|
|
|
|
|
// should pass
|
|
|
|
instruction_accounts.push(AccountMeta {
|
|
|
|
pubkey: custodian_address,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
});
|
|
|
|
let accounts = process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::Withdraw(total_lamports)).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
assert_eq!(from(&accounts[0]).unwrap(), StakeState::Uninitialized);
|
|
|
|
|
|
|
|
// should pass, custodian is the same as the withdraw authority
|
|
|
|
instruction_accounts[5].pubkey = stake_address;
|
|
|
|
meta.lockup.custodian = stake_address;
|
|
|
|
let stake_account_self_as_custodian = AccountSharedData::new_data_with_space(
|
|
|
|
total_lamports,
|
|
|
|
&StakeState::Initialized(meta),
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-03-09 14:48:10 -08:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
transaction_accounts[0] = (stake_address, stake_account_self_as_custodian);
|
|
|
|
let accounts = process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::Withdraw(total_lamports)).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
assert_eq!(from(&accounts[0]).unwrap(), StakeState::Uninitialized);
|
|
|
|
transaction_accounts[0] = (stake_address, stake_account);
|
|
|
|
|
|
|
|
// should pass, lockup has expired
|
|
|
|
instruction_accounts.pop();
|
|
|
|
clock.epoch += 1;
|
2023-04-03 08:23:24 -07:00
|
|
|
transaction_accounts[3] = (clock::id(), create_account_shared_data_for_test(&clock));
|
2022-03-09 14:48:10 -08:00
|
|
|
let accounts = process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::Withdraw(total_lamports)).unwrap(),
|
|
|
|
transaction_accounts,
|
|
|
|
instruction_accounts,
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
assert_eq!(from(&accounts[0]).unwrap(), StakeState::Uninitialized);
|
|
|
|
}
|
|
|
|
|
2022-07-06 14:15:01 -07:00
|
|
|
#[test_case(feature_set_old_behavior(); "old_behavior")]
|
|
|
|
#[test_case(feature_set_new_behavior(); "new_behavior")]
|
2023-04-03 08:23:24 -07:00
|
|
|
fn test_withdraw_rent_exempt(feature_set: Arc<FeatureSet>) {
|
2022-03-09 14:48:10 -08:00
|
|
|
let recipient_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let custodian_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let stake_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let rent = Rent::default();
|
2022-04-19 10:04:12 -07:00
|
|
|
let rent_exempt_reserve = rent.minimum_balance(StakeState::size_of());
|
2022-05-07 16:01:05 -07:00
|
|
|
let minimum_delegation = crate::get_minimum_delegation(&feature_set);
|
2022-04-01 22:11:10 -07:00
|
|
|
let stake_lamports = 7 * minimum_delegation;
|
2022-03-09 14:48:10 -08:00
|
|
|
let stake_account = AccountSharedData::new_data_with_space(
|
|
|
|
stake_lamports + rent_exempt_reserve,
|
|
|
|
&StakeState::Initialized(Meta {
|
|
|
|
rent_exempt_reserve,
|
|
|
|
..Meta::auto(&stake_address)
|
|
|
|
}),
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-03-09 14:48:10 -08:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let transaction_accounts = vec![
|
|
|
|
(stake_address, stake_account),
|
|
|
|
(recipient_address, AccountSharedData::default()),
|
|
|
|
(custodian_address, AccountSharedData::default()),
|
|
|
|
(
|
2023-04-03 08:23:24 -07:00
|
|
|
clock::id(),
|
|
|
|
create_account_shared_data_for_test(&Clock::default()),
|
2022-03-09 14:48:10 -08:00
|
|
|
),
|
|
|
|
(
|
2023-04-03 08:23:24 -07:00
|
|
|
stake_history::id(),
|
|
|
|
create_account_shared_data_for_test(&StakeHistory::default()),
|
2022-03-09 14:48:10 -08:00
|
|
|
),
|
|
|
|
];
|
|
|
|
let instruction_accounts = vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-03-09 14:48:10 -08:00
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: recipient_address,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-03-09 14:48:10 -08:00
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: clock::id(),
|
2022-03-09 14:48:10 -08:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: stake_history::id(),
|
2022-03-09 14:48:10 -08:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2022-05-04 03:17:29 -07:00
|
|
|
// should pass, withdrawing initialized account down to minimum balance
|
2022-03-09 14:48:10 -08:00
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::Withdraw(stake_lamports)).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
2022-05-04 03:17:29 -07:00
|
|
|
Ok(()),
|
2022-03-09 14:48:10 -08:00
|
|
|
);
|
|
|
|
|
|
|
|
// should fail, withdrawal that would leave less than rent-exempt reserve
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-05-04 03:17:29 -07:00
|
|
|
&serialize(&StakeInstruction::Withdraw(stake_lamports + 1)).unwrap(),
|
2022-03-09 14:48:10 -08:00
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Err(InstructionError::InsufficientFunds),
|
|
|
|
);
|
|
|
|
|
|
|
|
// should pass, withdrawal of complete account
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::Withdraw(
|
|
|
|
stake_lamports + rent_exempt_reserve,
|
|
|
|
))
|
|
|
|
.unwrap(),
|
|
|
|
transaction_accounts,
|
|
|
|
instruction_accounts,
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-07-06 14:15:01 -07:00
|
|
|
#[test_case(feature_set_old_behavior(); "old_behavior")]
|
|
|
|
#[test_case(feature_set_new_behavior(); "new_behavior")]
|
2023-04-03 08:23:24 -07:00
|
|
|
fn test_deactivate(feature_set: Arc<FeatureSet>) {
|
2022-03-09 14:48:10 -08:00
|
|
|
let stake_address = solana_sdk::pubkey::new_rand();
|
2022-05-07 16:01:05 -07:00
|
|
|
let minimum_delegation = crate::get_minimum_delegation(&feature_set);
|
2022-04-15 11:30:45 -07:00
|
|
|
let stake_lamports = minimum_delegation;
|
2022-03-09 14:48:10 -08:00
|
|
|
let stake_account = AccountSharedData::new_data_with_space(
|
|
|
|
stake_lamports,
|
|
|
|
&StakeState::Initialized(Meta::auto(&stake_address)),
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-03-09 14:48:10 -08:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let vote_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let mut vote_account =
|
|
|
|
vote_state::create_account(&vote_address, &solana_sdk::pubkey::new_rand(), 0, 100);
|
|
|
|
vote_account
|
|
|
|
.set_state(&VoteStateVersions::new_current(VoteState::default()))
|
|
|
|
.unwrap();
|
|
|
|
let mut transaction_accounts = vec![
|
|
|
|
(stake_address, stake_account),
|
|
|
|
(vote_address, vote_account),
|
|
|
|
(
|
2023-04-03 08:23:24 -07:00
|
|
|
clock::id(),
|
|
|
|
create_account_shared_data_for_test(&Clock::default()),
|
2022-03-09 14:48:10 -08:00
|
|
|
),
|
|
|
|
(
|
2023-04-03 08:23:24 -07:00
|
|
|
stake_history::id(),
|
|
|
|
create_account_shared_data_for_test(&StakeHistory::default()),
|
2022-03-09 14:48:10 -08:00
|
|
|
),
|
|
|
|
(
|
|
|
|
stake_config::id(),
|
|
|
|
config::create_account(0, &stake_config::Config::default()),
|
|
|
|
),
|
|
|
|
];
|
|
|
|
let mut instruction_accounts = vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: true,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-03-09 14:48:10 -08:00
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: clock::id(),
|
2022-03-09 14:48:10 -08:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
// should fail, not signed
|
|
|
|
instruction_accounts[0].is_signer = false;
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::Deactivate).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
|
|
|
instruction_accounts[0].is_signer = true;
|
|
|
|
|
|
|
|
// should fail, not staked yet
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::Deactivate).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Staking
|
|
|
|
let accounts = process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::DelegateStake).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: true,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-03-09 14:48:10 -08:00
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: vote_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: clock::id(),
|
2022-03-09 14:48:10 -08:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: stake_history::id(),
|
2022-03-09 14:48:10 -08:00
|
|
|
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());
|
|
|
|
|
|
|
|
// should pass
|
|
|
|
let accounts = process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::Deactivate).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
transaction_accounts[0] = (stake_address, accounts[0].clone());
|
|
|
|
|
|
|
|
// should fail, only works once
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::Deactivate).unwrap(),
|
|
|
|
transaction_accounts,
|
|
|
|
instruction_accounts,
|
|
|
|
Err(StakeError::AlreadyDeactivated.into()),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-07-06 14:15:01 -07:00
|
|
|
#[test_case(feature_set_old_behavior(); "old_behavior")]
|
|
|
|
#[test_case(feature_set_new_behavior(); "new_behavior")]
|
2023-04-03 08:23:24 -07:00
|
|
|
fn test_set_lockup(feature_set: Arc<FeatureSet>) {
|
2022-03-09 14:48:10 -08:00
|
|
|
let custodian_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let authorized_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let stake_address = solana_sdk::pubkey::new_rand();
|
2022-05-07 16:01:05 -07:00
|
|
|
let minimum_delegation = crate::get_minimum_delegation(&feature_set);
|
2022-04-01 22:11:10 -07:00
|
|
|
let stake_lamports = minimum_delegation;
|
2022-03-09 14:48:10 -08:00
|
|
|
let stake_account = AccountSharedData::new_data_with_space(
|
|
|
|
stake_lamports,
|
|
|
|
&StakeState::Uninitialized,
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-03-09 14:48:10 -08:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let vote_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let mut vote_account =
|
|
|
|
vote_state::create_account(&vote_address, &solana_sdk::pubkey::new_rand(), 0, 100);
|
|
|
|
vote_account
|
|
|
|
.set_state(&VoteStateVersions::new_current(VoteState::default()))
|
|
|
|
.unwrap();
|
|
|
|
let instruction_data = serialize(&StakeInstruction::SetLockup(LockupArgs {
|
|
|
|
unix_timestamp: Some(1),
|
|
|
|
epoch: Some(1),
|
|
|
|
custodian: Some(custodian_address),
|
|
|
|
}))
|
|
|
|
.unwrap();
|
|
|
|
let mut transaction_accounts = vec![
|
|
|
|
(stake_address, stake_account),
|
|
|
|
(vote_address, vote_account),
|
|
|
|
(authorized_address, AccountSharedData::default()),
|
|
|
|
(custodian_address, AccountSharedData::default()),
|
|
|
|
(
|
2023-04-03 08:23:24 -07:00
|
|
|
clock::id(),
|
|
|
|
create_account_shared_data_for_test(&Clock::default()),
|
2022-03-09 14:48:10 -08:00
|
|
|
),
|
|
|
|
(
|
2023-04-03 08:23:24 -07:00
|
|
|
rent::id(),
|
|
|
|
create_account_shared_data_for_test(&Rent::free()),
|
2022-03-09 14:48:10 -08:00
|
|
|
),
|
|
|
|
(
|
2023-04-03 08:23:24 -07:00
|
|
|
stake_history::id(),
|
|
|
|
create_account_shared_data_for_test(&StakeHistory::default()),
|
2022-03-09 14:48:10 -08:00
|
|
|
),
|
|
|
|
(
|
|
|
|
stake_config::id(),
|
|
|
|
config::create_account(0, &stake_config::Config::default()),
|
|
|
|
),
|
|
|
|
];
|
|
|
|
let mut instruction_accounts = vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-03-09 14:48:10 -08:00
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: clock::id(),
|
2022-03-09 14:48:10 -08:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: custodian_address,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
// should fail, wrong state
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&instruction_data,
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
|
|
|
|
|
|
|
// initialize stake
|
|
|
|
let lockup = Lockup {
|
|
|
|
unix_timestamp: 1,
|
|
|
|
epoch: 1,
|
|
|
|
custodian: custodian_address,
|
|
|
|
};
|
|
|
|
let accounts = process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::Initialize(
|
|
|
|
Authorized::auto(&stake_address),
|
|
|
|
lockup,
|
|
|
|
))
|
|
|
|
.unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: true,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-03-09 14:48:10 -08:00
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: rent::id(),
|
2022-03-09 14:48:10 -08:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
transaction_accounts[0] = (stake_address, accounts[0].clone());
|
|
|
|
|
|
|
|
// should fail, not signed
|
|
|
|
instruction_accounts[2].is_signer = false;
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&instruction_data,
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Err(InstructionError::MissingRequiredSignature),
|
|
|
|
);
|
|
|
|
instruction_accounts[2].is_signer = true;
|
|
|
|
|
|
|
|
// should pass
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&instruction_data,
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Staking
|
|
|
|
let accounts = process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::DelegateStake).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: true,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-03-09 14:48:10 -08:00
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: vote_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: clock::id(),
|
2022-03-09 14:48:10 -08:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: stake_history::id(),
|
2022-03-09 14:48:10 -08:00
|
|
|
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());
|
|
|
|
|
|
|
|
// should fail, not signed
|
|
|
|
instruction_accounts[2].is_signer = false;
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&instruction_data,
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Err(InstructionError::MissingRequiredSignature),
|
|
|
|
);
|
|
|
|
instruction_accounts[2].is_signer = true;
|
|
|
|
|
|
|
|
// should pass
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&instruction_data,
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Lockup in force
|
|
|
|
let instruction_data = serialize(&StakeInstruction::SetLockup(LockupArgs {
|
|
|
|
unix_timestamp: Some(2),
|
|
|
|
epoch: None,
|
|
|
|
custodian: None,
|
|
|
|
}))
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// should fail, authorized withdrawer cannot change it
|
|
|
|
instruction_accounts[0].is_signer = true;
|
|
|
|
instruction_accounts[2].is_signer = false;
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&instruction_data,
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Err(InstructionError::MissingRequiredSignature),
|
|
|
|
);
|
|
|
|
instruction_accounts[0].is_signer = false;
|
|
|
|
instruction_accounts[2].is_signer = true;
|
|
|
|
|
|
|
|
// should pass, custodian can change it
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&instruction_data,
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Lockup expired
|
|
|
|
let clock = Clock {
|
|
|
|
unix_timestamp: UnixTimestamp::MAX,
|
|
|
|
epoch: Epoch::MAX,
|
|
|
|
..Clock::default()
|
|
|
|
};
|
2023-04-03 08:23:24 -07:00
|
|
|
transaction_accounts[4] = (clock::id(), create_account_shared_data_for_test(&clock));
|
2022-03-09 14:48:10 -08:00
|
|
|
|
|
|
|
// should fail, custodian cannot change it
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&instruction_data,
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Err(InstructionError::MissingRequiredSignature),
|
|
|
|
);
|
|
|
|
|
|
|
|
// should pass, authorized withdrawer can change it
|
|
|
|
instruction_accounts[0].is_signer = true;
|
|
|
|
instruction_accounts[2].is_signer = false;
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&instruction_data,
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Change authorized withdrawer
|
|
|
|
let accounts = process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&serialize(&StakeInstruction::Authorize(
|
|
|
|
authorized_address,
|
|
|
|
StakeAuthorize::Withdrawer,
|
|
|
|
))
|
|
|
|
.unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: true,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-03-09 14:48:10 -08:00
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: clock::id(),
|
2022-03-09 14:48:10 -08:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
2022-03-15 07:53:11 -07:00
|
|
|
AccountMeta {
|
|
|
|
pubkey: authorized_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
2022-03-09 14:48:10 -08:00
|
|
|
],
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
transaction_accounts[0] = (stake_address, accounts[0].clone());
|
|
|
|
|
|
|
|
// should fail, previous authorized withdrawer cannot change the lockup anymore
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-03-09 14:48:10 -08:00
|
|
|
&instruction_data,
|
|
|
|
transaction_accounts,
|
|
|
|
instruction_accounts,
|
|
|
|
Err(InstructionError::MissingRequiredSignature),
|
|
|
|
);
|
|
|
|
}
|
2022-04-01 16:08:55 -07:00
|
|
|
|
2022-05-04 03:17:29 -07:00
|
|
|
/// Ensure that `initialize()` respects the minimum balance requirements
|
|
|
|
/// - Assert 1: accounts with a balance equal-to the rent exemption initialize OK
|
|
|
|
/// - Assert 2: accounts with a balance less-than the rent exemption do not initialize
|
2022-07-06 14:15:01 -07:00
|
|
|
#[test_case(feature_set_old_behavior(); "old_behavior")]
|
|
|
|
#[test_case(feature_set_new_behavior(); "new_behavior")]
|
2023-04-03 08:23:24 -07:00
|
|
|
fn test_initialize_minimum_balance(feature_set: Arc<FeatureSet>) {
|
2022-04-01 16:08:55 -07:00
|
|
|
let rent = Rent::default();
|
2022-04-19 10:04:12 -07:00
|
|
|
let rent_exempt_reserve = rent.minimum_balance(StakeState::size_of());
|
2022-04-01 16:08:55 -07:00
|
|
|
let stake_address = solana_sdk::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,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-04-01 16:08:55 -07:00
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: rent::id(),
|
2022-04-01 16:08:55 -07:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
];
|
2022-05-04 03:17:29 -07:00
|
|
|
for (lamports, expected_result) in [
|
|
|
|
(rent_exempt_reserve, Ok(())),
|
2022-04-01 16:08:55 -07:00
|
|
|
(
|
2022-05-04 03:17:29 -07:00
|
|
|
rent_exempt_reserve - 1,
|
2022-04-01 16:08:55 -07:00
|
|
|
Err(InstructionError::InsufficientFunds),
|
|
|
|
),
|
|
|
|
] {
|
2022-05-04 03:17:29 -07:00
|
|
|
let stake_account = AccountSharedData::new(lamports, StakeState::size_of(), &id());
|
2022-04-01 16:08:55 -07:00
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-04-01 16:08:55 -07:00
|
|
|
&instruction_data,
|
|
|
|
vec![
|
|
|
|
(stake_address, stake_account),
|
2023-04-03 08:23:24 -07:00
|
|
|
(rent::id(), create_account_shared_data_for_test(&rent)),
|
2022-04-01 16:08:55 -07:00
|
|
|
],
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
expected_result,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-01 22:11:10 -07:00
|
|
|
/// Ensure that `delegate()` respects the minimum delegation requirements
|
2022-05-07 16:01:05 -07:00
|
|
|
/// - Assert 1: delegating an amount equal-to the minimum succeeds
|
2022-05-04 03:17:29 -07:00
|
|
|
/// - Assert 2: delegating an amount less-than the minimum fails
|
2022-04-01 16:08:55 -07:00
|
|
|
/// Also test both asserts above over both StakeState::{Initialized and Stake}, since the logic
|
|
|
|
/// is slightly different for the variants.
|
|
|
|
///
|
|
|
|
/// NOTE: Even though new stake accounts must have a minimum balance that is at least
|
2022-05-07 16:01:05 -07:00
|
|
|
/// the minimum delegation (plus rent exempt reserve), the old behavior allowed
|
2022-04-01 16:08:55 -07:00
|
|
|
/// withdrawing below the minimum delegation, then re-delegating successfully (see
|
|
|
|
/// `test_behavior_withdrawal_then_redelegate_with_less_than_minimum_stake_delegation()` for
|
|
|
|
/// more information.)
|
2022-07-06 14:15:01 -07:00
|
|
|
#[test_case(feature_set_old_behavior(); "old_behavior")]
|
|
|
|
#[test_case(feature_set_new_behavior(); "new_behavior")]
|
2023-04-03 08:23:24 -07:00
|
|
|
fn test_delegate_minimum_stake_delegation(feature_set: Arc<FeatureSet>) {
|
2022-04-01 22:11:10 -07:00
|
|
|
let minimum_delegation = crate::get_minimum_delegation(&feature_set);
|
2022-04-01 16:08:55 -07:00
|
|
|
let rent = Rent::default();
|
2022-04-19 10:04:12 -07:00
|
|
|
let rent_exempt_reserve = rent.minimum_balance(StakeState::size_of());
|
2022-04-01 16:08:55 -07:00
|
|
|
let stake_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let meta = Meta {
|
|
|
|
rent_exempt_reserve,
|
|
|
|
..Meta::auto(&stake_address)
|
|
|
|
};
|
|
|
|
let vote_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let vote_account =
|
|
|
|
vote_state::create_account(&vote_address, &solana_sdk::pubkey::new_rand(), 0, 100);
|
|
|
|
let instruction_accounts = vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: true,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-04-01 16:08:55 -07:00
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: vote_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: clock::id(),
|
2022-04-01 16:08:55 -07:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: stake_history::id(),
|
2022-04-01 16:08:55 -07:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_config::id(),
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
];
|
2022-05-07 16:01:05 -07:00
|
|
|
for (stake_delegation, expected_result) in &[
|
2022-04-01 22:11:10 -07:00
|
|
|
(minimum_delegation, Ok(())),
|
2022-06-01 18:29:57 -07:00
|
|
|
(
|
|
|
|
minimum_delegation - 1,
|
|
|
|
Err(StakeError::InsufficientDelegation),
|
|
|
|
),
|
2022-04-01 16:08:55 -07:00
|
|
|
] {
|
|
|
|
for stake_state in &[
|
|
|
|
StakeState::Initialized(meta),
|
2022-05-07 16:01:05 -07:00
|
|
|
just_stake(meta, *stake_delegation),
|
2022-04-01 16:08:55 -07:00
|
|
|
] {
|
|
|
|
let stake_account = AccountSharedData::new_data_with_space(
|
|
|
|
stake_delegation + rent_exempt_reserve,
|
|
|
|
stake_state,
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-04-01 16:08:55 -07:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-04-01 16:08:55 -07:00
|
|
|
&serialize(&StakeInstruction::DelegateStake).unwrap(),
|
|
|
|
vec![
|
|
|
|
(stake_address, stake_account),
|
|
|
|
(vote_address, vote_account.clone()),
|
|
|
|
(
|
2023-04-03 08:23:24 -07:00
|
|
|
clock::id(),
|
|
|
|
create_account_shared_data_for_test(&Clock::default()),
|
2022-04-01 16:08:55 -07:00
|
|
|
),
|
|
|
|
(
|
2023-04-03 08:23:24 -07:00
|
|
|
stake_history::id(),
|
|
|
|
create_account_shared_data_for_test(&StakeHistory::default()),
|
2022-04-01 16:08:55 -07:00
|
|
|
),
|
|
|
|
(
|
|
|
|
stake_config::id(),
|
|
|
|
config::create_account(0, &stake_config::Config::default()),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
instruction_accounts.clone(),
|
2022-05-04 03:17:29 -07:00
|
|
|
expected_result.clone().map_err(|e| e.into()),
|
2022-04-01 16:08:55 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-01 22:11:10 -07:00
|
|
|
/// Ensure that `split()` respects the minimum delegation requirements. This applies to
|
2022-04-01 16:08:55 -07:00
|
|
|
/// both the source and destination acounts. Thus, we have four permutations possible based on
|
|
|
|
/// if each account's post-split delegation is equal-to (EQ) or less-than (LT) the minimum:
|
|
|
|
///
|
|
|
|
/// source | dest | result
|
|
|
|
/// --------+------+--------
|
|
|
|
/// EQ | EQ | Ok
|
|
|
|
/// EQ | LT | Err
|
|
|
|
/// LT | EQ | Err
|
|
|
|
/// LT | LT | Err
|
2022-07-06 14:15:01 -07:00
|
|
|
#[test_case(feature_set_old_behavior(); "old_behavior")]
|
|
|
|
#[test_case(feature_set_new_behavior(); "new_behavior")]
|
2023-04-03 08:23:24 -07:00
|
|
|
fn test_split_minimum_stake_delegation(feature_set: Arc<FeatureSet>) {
|
2022-04-01 22:11:10 -07:00
|
|
|
let minimum_delegation = crate::get_minimum_delegation(&feature_set);
|
2022-04-01 16:08:55 -07:00
|
|
|
let rent = Rent::default();
|
2022-04-19 10:04:12 -07:00
|
|
|
let rent_exempt_reserve = rent.minimum_balance(StakeState::size_of());
|
2022-04-01 16:08:55 -07:00
|
|
|
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,
|
|
|
|
&StakeState::Uninitialized,
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-04-01 16:08:55 -07:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let instruction_accounts = vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: source_address,
|
|
|
|
is_signer: true,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-04-01 16:08:55 -07:00
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: dest_address,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-04-01 16:08:55 -07:00
|
|
|
},
|
|
|
|
];
|
2022-05-04 03:17:29 -07:00
|
|
|
for (source_reserve, dest_reserve, expected_result) in [
|
|
|
|
(rent_exempt_reserve, rent_exempt_reserve, Ok(())),
|
2022-04-01 16:08:55 -07:00
|
|
|
(
|
2022-05-04 03:17:29 -07:00
|
|
|
rent_exempt_reserve,
|
|
|
|
rent_exempt_reserve - 1,
|
2022-04-01 16:08:55 -07:00
|
|
|
Err(InstructionError::InsufficientFunds),
|
|
|
|
),
|
|
|
|
(
|
2022-05-04 03:17:29 -07:00
|
|
|
rent_exempt_reserve - 1,
|
|
|
|
rent_exempt_reserve,
|
2022-04-01 16:08:55 -07:00
|
|
|
Err(InstructionError::InsufficientFunds),
|
|
|
|
),
|
|
|
|
(
|
2022-05-04 03:17:29 -07:00
|
|
|
rent_exempt_reserve - 1,
|
|
|
|
rent_exempt_reserve - 1,
|
2022-04-01 16:08:55 -07:00
|
|
|
Err(InstructionError::InsufficientFunds),
|
|
|
|
),
|
|
|
|
] {
|
|
|
|
// The source account's starting balance is equal to *both* the source and dest
|
|
|
|
// accounts' *final* balance
|
2022-05-04 03:17:29 -07:00
|
|
|
let mut source_starting_balance = source_reserve + dest_reserve;
|
|
|
|
for (delegation, source_stake_state) in &[
|
|
|
|
(0, StakeState::Initialized(source_meta)),
|
|
|
|
(
|
|
|
|
minimum_delegation,
|
|
|
|
just_stake(
|
|
|
|
source_meta,
|
|
|
|
minimum_delegation * 2 + source_starting_balance - rent_exempt_reserve,
|
|
|
|
),
|
|
|
|
),
|
2022-04-01 16:08:55 -07:00
|
|
|
] {
|
2022-05-04 03:17:29 -07:00
|
|
|
source_starting_balance += delegation * 2;
|
2022-04-01 16:08:55 -07:00
|
|
|
let source_account = AccountSharedData::new_data_with_space(
|
|
|
|
source_starting_balance,
|
|
|
|
source_stake_state,
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-04-01 16:08:55 -07:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-05-04 03:17:29 -07:00
|
|
|
&serialize(&StakeInstruction::Split(dest_reserve + delegation)).unwrap(),
|
2022-04-01 16:08:55 -07:00
|
|
|
vec![
|
|
|
|
(source_address, source_account),
|
|
|
|
(dest_address, dest_account.clone()),
|
2023-04-03 08:23:24 -07:00
|
|
|
(rent::id(), create_account_shared_data_for_test(&rent)),
|
2022-04-01 16:08:55 -07:00
|
|
|
],
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
expected_result.clone(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-01 22:11:10 -07:00
|
|
|
/// Ensure that splitting the full amount from an account respects the minimum delegation
|
2022-04-01 16:08:55 -07:00
|
|
|
/// requirements. This ensures that we are future-proofing/testing any raises to the minimum
|
|
|
|
/// delegation.
|
|
|
|
/// - Assert 1: splitting the full amount from an account that has at least the minimum
|
|
|
|
/// delegation is OK
|
|
|
|
/// - Assert 2: splitting the full amount from an account that has less than the minimum
|
|
|
|
/// delegation is not OK
|
2022-07-06 14:15:01 -07:00
|
|
|
#[test_case(feature_set_old_behavior(); "old_behavior")]
|
|
|
|
#[test_case(feature_set_new_behavior(); "new_behavior")]
|
2023-04-03 08:23:24 -07:00
|
|
|
fn test_split_full_amount_minimum_stake_delegation(feature_set: Arc<FeatureSet>) {
|
2022-04-01 22:11:10 -07:00
|
|
|
let minimum_delegation = crate::get_minimum_delegation(&feature_set);
|
2022-04-01 16:08:55 -07:00
|
|
|
let rent = Rent::default();
|
2022-04-19 10:04:12 -07:00
|
|
|
let rent_exempt_reserve = rent.minimum_balance(StakeState::size_of());
|
2022-04-01 16:08:55 -07:00
|
|
|
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,
|
|
|
|
&StakeState::Uninitialized,
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-04-01 16:08:55 -07:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let instruction_accounts = vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: source_address,
|
|
|
|
is_signer: true,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-04-01 16:08:55 -07:00
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: dest_address,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-04-01 16:08:55 -07:00
|
|
|
},
|
|
|
|
];
|
2022-05-04 03:17:29 -07:00
|
|
|
for (reserve, expected_result) in [
|
|
|
|
(rent_exempt_reserve, Ok(())),
|
2022-04-01 16:08:55 -07:00
|
|
|
(
|
2022-05-04 03:17:29 -07:00
|
|
|
rent_exempt_reserve - 1,
|
2022-04-01 16:08:55 -07:00
|
|
|
Err(InstructionError::InsufficientFunds),
|
|
|
|
),
|
|
|
|
] {
|
2022-05-04 03:17:29 -07:00
|
|
|
for (stake_delegation, source_stake_state) in &[
|
|
|
|
(0, StakeState::Initialized(source_meta)),
|
|
|
|
(
|
|
|
|
minimum_delegation,
|
|
|
|
just_stake(source_meta, minimum_delegation),
|
|
|
|
),
|
2022-04-01 16:08:55 -07:00
|
|
|
] {
|
|
|
|
let source_account = AccountSharedData::new_data_with_space(
|
2022-05-04 03:17:29 -07:00
|
|
|
stake_delegation + reserve,
|
2022-04-01 16:08:55 -07:00
|
|
|
source_stake_state,
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-04-01 16:08:55 -07:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-04-01 16:08:55 -07:00
|
|
|
&serialize(&StakeInstruction::Split(source_account.lamports())).unwrap(),
|
|
|
|
vec![
|
|
|
|
(source_address, source_account),
|
|
|
|
(dest_address, dest_account.clone()),
|
2023-04-03 08:23:24 -07:00
|
|
|
(rent::id(), create_account_shared_data_for_test(&rent)),
|
2022-04-01 16:08:55 -07:00
|
|
|
],
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
expected_result.clone(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-04 03:17:29 -07:00
|
|
|
/// Ensure that `split()` correctly handles prefunded destination accounts from
|
|
|
|
/// initialized stakes. When a destination account already has funds, ensure
|
|
|
|
/// the minimum split amount reduces accordingly.
|
2022-07-06 14:15:01 -07:00
|
|
|
#[test_case(feature_set_old_behavior(); "old_behavior")]
|
|
|
|
#[test_case(feature_set_new_behavior(); "new_behavior")]
|
2023-04-03 08:23:24 -07:00
|
|
|
fn test_initialized_split_destination_minimum_balance(feature_set: Arc<FeatureSet>) {
|
2022-05-04 03:17:29 -07:00
|
|
|
let rent = Rent::default();
|
|
|
|
let rent_exempt_reserve = rent.minimum_balance(StakeState::size_of());
|
|
|
|
let source_address = Pubkey::new_unique();
|
2022-05-07 16:01:05 -07:00
|
|
|
let destination_address = Pubkey::new_unique();
|
2022-05-04 03:17:29 -07:00
|
|
|
let instruction_accounts = vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: source_address,
|
|
|
|
is_signer: true,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-05-04 03:17:29 -07:00
|
|
|
},
|
|
|
|
AccountMeta {
|
2022-05-07 16:01:05 -07:00
|
|
|
pubkey: destination_address,
|
2022-05-04 03:17:29 -07:00
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-05-04 03:17:29 -07:00
|
|
|
},
|
|
|
|
];
|
|
|
|
for (destination_starting_balance, split_amount, expected_result) in [
|
|
|
|
// split amount must be non zero
|
|
|
|
(
|
|
|
|
rent_exempt_reserve,
|
|
|
|
0,
|
|
|
|
Err(InstructionError::InsufficientFunds),
|
|
|
|
),
|
|
|
|
// any split amount is OK when destination account is already fully funded
|
|
|
|
(rent_exempt_reserve, 1, Ok(())),
|
|
|
|
// if destination is only short by 1 lamport, then split amount can be 1 lamport
|
|
|
|
(rent_exempt_reserve - 1, 1, Ok(())),
|
2022-05-07 16:01:05 -07:00
|
|
|
// destination short by 2 lamports, then 1 isn't enough (non-zero split amount)
|
2022-05-04 03:17:29 -07:00
|
|
|
(
|
|
|
|
rent_exempt_reserve - 2,
|
|
|
|
1,
|
|
|
|
Err(InstructionError::InsufficientFunds),
|
|
|
|
),
|
|
|
|
// destination has smallest non-zero balance, so can split the minimum balance
|
|
|
|
// requirements minus what destination already has
|
|
|
|
(1, rent_exempt_reserve - 1, Ok(())),
|
|
|
|
// destination has smallest non-zero balance, but cannot split less than the minimum
|
|
|
|
// balance requirements minus what destination already has
|
|
|
|
(
|
|
|
|
1,
|
|
|
|
rent_exempt_reserve - 2,
|
|
|
|
Err(InstructionError::InsufficientFunds),
|
|
|
|
),
|
2022-05-07 16:01:05 -07:00
|
|
|
// destination has zero lamports, so split must be at least rent exempt reserve
|
2022-05-04 03:17:29 -07:00
|
|
|
(0, rent_exempt_reserve, Ok(())),
|
|
|
|
// destination has zero lamports, but split amount is less than rent exempt reserve
|
|
|
|
(
|
|
|
|
0,
|
|
|
|
rent_exempt_reserve - 1,
|
|
|
|
Err(InstructionError::InsufficientFunds),
|
|
|
|
),
|
|
|
|
] {
|
2022-05-07 16:01:05 -07:00
|
|
|
// Set the source's starting balance to something large to ensure its post-split
|
|
|
|
// balance meets all the requirements
|
|
|
|
let source_balance = u64::MAX;
|
|
|
|
let source_meta = Meta {
|
|
|
|
rent_exempt_reserve,
|
|
|
|
..Meta::auto(&source_address)
|
|
|
|
};
|
2022-05-04 03:17:29 -07:00
|
|
|
let source_account = AccountSharedData::new_data_with_space(
|
|
|
|
source_balance,
|
2022-05-07 16:01:05 -07:00
|
|
|
&StakeState::Initialized(source_meta),
|
2022-05-04 03:17:29 -07:00
|
|
|
StakeState::size_of(),
|
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2022-05-07 16:01:05 -07:00
|
|
|
let destination_account = AccountSharedData::new_data_with_space(
|
2022-05-04 03:17:29 -07:00
|
|
|
destination_starting_balance,
|
|
|
|
&StakeState::Uninitialized,
|
|
|
|
StakeState::size_of(),
|
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-05-04 03:17:29 -07:00
|
|
|
&serialize(&StakeInstruction::Split(split_amount)).unwrap(),
|
|
|
|
vec![
|
|
|
|
(source_address, source_account),
|
2022-05-07 16:01:05 -07:00
|
|
|
(destination_address, destination_account),
|
2023-04-03 08:23:24 -07:00
|
|
|
(rent::id(), create_account_shared_data_for_test(&rent)),
|
2022-05-04 03:17:29 -07:00
|
|
|
],
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
expected_result.clone(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-07 16:01:05 -07:00
|
|
|
/// Ensure that `split()` correctly handles prefunded destination accounts from staked stakes.
|
|
|
|
/// When a destination account already has funds, ensure the minimum split amount reduces
|
|
|
|
/// accordingly.
|
2022-07-06 14:15:01 -07:00
|
|
|
#[test_case(feature_set_old_behavior(), &[Ok(()), Ok(())]; "old_behavior")]
|
|
|
|
#[test_case(feature_set_new_behavior(), &[ Err(InstructionError::InsufficientFunds), Err(InstructionError::InsufficientFunds) ] ; "new_behavior")]
|
|
|
|
fn test_staked_split_destination_minimum_balance(
|
2023-04-03 08:23:24 -07:00
|
|
|
feature_set: Arc<FeatureSet>,
|
2022-05-07 16:01:05 -07:00
|
|
|
expected_results: &[Result<(), InstructionError>],
|
|
|
|
) {
|
|
|
|
let minimum_delegation = crate::get_minimum_delegation(&feature_set);
|
2022-04-01 16:08:55 -07:00
|
|
|
let rent = Rent::default();
|
2022-04-19 10:04:12 -07:00
|
|
|
let rent_exempt_reserve = rent.minimum_balance(StakeState::size_of());
|
2022-04-01 16:08:55 -07:00
|
|
|
let source_address = Pubkey::new_unique();
|
2022-05-07 16:01:05 -07:00
|
|
|
let destination_address = Pubkey::new_unique();
|
2022-04-01 16:08:55 -07:00
|
|
|
let instruction_accounts = vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: source_address,
|
|
|
|
is_signer: true,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-04-01 16:08:55 -07:00
|
|
|
},
|
|
|
|
AccountMeta {
|
2022-05-07 16:01:05 -07:00
|
|
|
pubkey: destination_address,
|
2022-04-01 16:08:55 -07:00
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-04-01 16:08:55 -07:00
|
|
|
},
|
|
|
|
];
|
|
|
|
for (destination_starting_balance, split_amount, expected_result) in [
|
|
|
|
// split amount must be non zero
|
|
|
|
(
|
2022-04-01 22:11:10 -07:00
|
|
|
rent_exempt_reserve + minimum_delegation,
|
2022-04-01 16:08:55 -07:00
|
|
|
0,
|
|
|
|
Err(InstructionError::InsufficientFunds),
|
|
|
|
),
|
2022-05-07 16:01:05 -07:00
|
|
|
// destination is fully funded:
|
|
|
|
// - old behavior: any split amount is OK
|
|
|
|
// - new behavior: split amount must be at least the minimum delegation
|
|
|
|
(
|
|
|
|
rent_exempt_reserve + minimum_delegation,
|
|
|
|
1,
|
|
|
|
expected_results[0].clone(),
|
|
|
|
),
|
|
|
|
// if destination is only short by 1 lamport, then...
|
|
|
|
// - old behavior: split amount can be 1 lamport
|
|
|
|
// - new behavior: split amount must be at least the minimum delegation
|
|
|
|
(
|
|
|
|
rent_exempt_reserve + minimum_delegation - 1,
|
|
|
|
1,
|
|
|
|
expected_results[1].clone(),
|
|
|
|
),
|
2022-04-01 16:08:55 -07:00
|
|
|
// destination short by 2 lamports, so 1 isn't enough (non-zero split amount)
|
|
|
|
(
|
2022-04-01 22:11:10 -07:00
|
|
|
rent_exempt_reserve + minimum_delegation - 2,
|
2022-04-01 16:08:55 -07:00
|
|
|
1,
|
|
|
|
Err(InstructionError::InsufficientFunds),
|
|
|
|
),
|
|
|
|
// destination is rent exempt, so split enough for minimum delegation
|
2022-04-01 22:11:10 -07:00
|
|
|
(rent_exempt_reserve, minimum_delegation, Ok(())),
|
2022-04-01 16:08:55 -07:00
|
|
|
// destination is rent exempt, but split amount less than minimum delegation
|
|
|
|
(
|
|
|
|
rent_exempt_reserve,
|
2022-05-04 03:17:29 -07:00
|
|
|
minimum_delegation.saturating_sub(1), // when minimum is 0, this blows up!
|
2022-04-01 16:08:55 -07:00
|
|
|
Err(InstructionError::InsufficientFunds),
|
|
|
|
),
|
|
|
|
// destination is not rent exempt, so split enough for rent and minimum delegation
|
2022-04-01 22:11:10 -07:00
|
|
|
(rent_exempt_reserve - 1, minimum_delegation + 1, Ok(())),
|
2022-04-01 16:08:55 -07:00
|
|
|
// destination is not rent exempt, but split amount only for minimum delegation
|
|
|
|
(
|
|
|
|
rent_exempt_reserve - 1,
|
2022-04-01 22:11:10 -07:00
|
|
|
minimum_delegation,
|
2022-04-01 16:08:55 -07:00
|
|
|
Err(InstructionError::InsufficientFunds),
|
|
|
|
),
|
|
|
|
// destination has smallest non-zero balance, so can split the minimum balance
|
|
|
|
// requirements minus what destination already has
|
2022-04-01 22:11:10 -07:00
|
|
|
(1, rent_exempt_reserve + minimum_delegation - 1, Ok(())),
|
2022-04-01 16:08:55 -07:00
|
|
|
// destination has smallest non-zero balance, but cannot split less than the minimum
|
|
|
|
// balance requirements minus what destination already has
|
|
|
|
(
|
|
|
|
1,
|
2022-04-01 22:11:10 -07:00
|
|
|
rent_exempt_reserve + minimum_delegation - 2,
|
2022-04-01 16:08:55 -07:00
|
|
|
Err(InstructionError::InsufficientFunds),
|
|
|
|
),
|
|
|
|
// destination has zero lamports, so split must be at least rent exempt reserve plus
|
|
|
|
// minimum delegation
|
2022-04-01 22:11:10 -07:00
|
|
|
(0, rent_exempt_reserve + minimum_delegation, Ok(())),
|
2022-04-01 16:08:55 -07:00
|
|
|
// destination has zero lamports, but split amount is less than rent exempt reserve
|
|
|
|
// plus minimum delegation
|
|
|
|
(
|
|
|
|
0,
|
2022-04-01 22:11:10 -07:00
|
|
|
rent_exempt_reserve + minimum_delegation - 1,
|
2022-04-01 16:08:55 -07:00
|
|
|
Err(InstructionError::InsufficientFunds),
|
|
|
|
),
|
|
|
|
] {
|
2022-05-07 16:01:05 -07:00
|
|
|
// Set the source's starting balance to something large to ensure its post-split
|
|
|
|
// balance meets all the requirements
|
|
|
|
let source_balance = u64::MAX;
|
|
|
|
let source_meta = Meta {
|
|
|
|
rent_exempt_reserve,
|
|
|
|
..Meta::auto(&source_address)
|
|
|
|
};
|
|
|
|
let source_stake_delegation = source_balance - rent_exempt_reserve;
|
2022-05-04 03:17:29 -07:00
|
|
|
let source_account = AccountSharedData::new_data_with_space(
|
|
|
|
source_balance,
|
2022-05-07 16:01:05 -07:00
|
|
|
&just_stake(source_meta, source_stake_delegation),
|
2022-05-04 03:17:29 -07:00
|
|
|
StakeState::size_of(),
|
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2022-05-07 16:01:05 -07:00
|
|
|
let destination_account = AccountSharedData::new_data_with_space(
|
2022-05-04 03:17:29 -07:00
|
|
|
destination_starting_balance,
|
|
|
|
&StakeState::Uninitialized,
|
|
|
|
StakeState::size_of(),
|
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let accounts = process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-05-04 03:17:29 -07:00
|
|
|
&serialize(&StakeInstruction::Split(split_amount)).unwrap(),
|
|
|
|
vec![
|
2022-05-07 16:01:05 -07:00
|
|
|
(source_address, source_account.clone()),
|
|
|
|
(destination_address, destination_account),
|
2023-04-03 08:23:24 -07:00
|
|
|
(rent::id(), create_account_shared_data_for_test(&rent)),
|
2022-05-04 03:17:29 -07:00
|
|
|
],
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
expected_result.clone(),
|
|
|
|
);
|
|
|
|
// For the expected OK cases, when the source's StakeState is Stake, then the
|
|
|
|
// destination's StakeState *must* also end up as Stake as well. Additionally,
|
|
|
|
// check to ensure the destination's delegation amount is correct. If the
|
|
|
|
// destination is already rent exempt, then the destination's stake delegation
|
|
|
|
// *must* equal the split amount. Otherwise, the split amount must first be used to
|
|
|
|
// make the destination rent exempt, and then the leftover lamports are delegated.
|
|
|
|
if expected_result.is_ok() {
|
|
|
|
assert_matches!(accounts[0].state().unwrap(), StakeState::Stake(_, _));
|
|
|
|
if let StakeState::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 StakeState::Stake!");
|
2022-04-01 16:08:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-01 22:11:10 -07:00
|
|
|
/// Ensure that `withdraw()` respects the minimum delegation requirements
|
2022-04-01 16:08:55 -07:00
|
|
|
/// - Assert 1: withdrawing so remaining stake is equal-to the minimum is OK
|
|
|
|
/// - Assert 2: withdrawing so remaining stake is less-than the minimum is not OK
|
2022-07-06 14:15:01 -07:00
|
|
|
#[test_case(feature_set_old_behavior(); "old_behavior")]
|
|
|
|
#[test_case(feature_set_new_behavior(); "new_behavior")]
|
2023-04-03 08:23:24 -07:00
|
|
|
fn test_withdraw_minimum_stake_delegation(feature_set: Arc<FeatureSet>) {
|
2022-04-01 22:11:10 -07:00
|
|
|
let minimum_delegation = crate::get_minimum_delegation(&feature_set);
|
2022-04-01 16:08:55 -07:00
|
|
|
let rent = Rent::default();
|
2022-04-19 10:04:12 -07:00
|
|
|
let rent_exempt_reserve = rent.minimum_balance(StakeState::size_of());
|
2022-04-01 16:08:55 -07:00
|
|
|
let stake_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let meta = Meta {
|
|
|
|
rent_exempt_reserve,
|
|
|
|
..Meta::auto(&stake_address)
|
|
|
|
};
|
|
|
|
let recipient_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let instruction_accounts = vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-04-01 16:08:55 -07:00
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: recipient_address,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-04-01 16:08:55 -07:00
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: clock::id(),
|
2022-04-01 16:08:55 -07:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: stake_history::id(),
|
2022-04-01 16:08:55 -07:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
];
|
2022-04-01 22:11:10 -07:00
|
|
|
let starting_stake_delegation = minimum_delegation;
|
2022-04-01 16:08:55 -07:00
|
|
|
for (ending_stake_delegation, expected_result) in [
|
2022-04-01 22:11:10 -07:00
|
|
|
(minimum_delegation, Ok(())),
|
2022-04-01 16:08:55 -07:00
|
|
|
(
|
2022-04-01 22:11:10 -07:00
|
|
|
minimum_delegation - 1,
|
2022-04-01 16:08:55 -07:00
|
|
|
Err(InstructionError::InsufficientFunds),
|
|
|
|
),
|
|
|
|
] {
|
2022-05-04 03:17:29 -07:00
|
|
|
for (stake_delegation, stake_state) in &[
|
|
|
|
(0, StakeState::Initialized(meta)),
|
|
|
|
(minimum_delegation, just_stake(meta, minimum_delegation)),
|
2022-04-01 16:08:55 -07:00
|
|
|
] {
|
|
|
|
let rewards_balance = 123;
|
|
|
|
let stake_account = AccountSharedData::new_data_with_space(
|
2022-05-04 03:17:29 -07:00
|
|
|
stake_delegation + rent_exempt_reserve + rewards_balance,
|
2022-04-01 16:08:55 -07:00
|
|
|
stake_state,
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-04-01 16:08:55 -07:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let withdraw_amount =
|
|
|
|
(starting_stake_delegation + rewards_balance) - ending_stake_delegation;
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-04-01 16:08:55 -07:00
|
|
|
&serialize(&StakeInstruction::Withdraw(withdraw_amount)).unwrap(),
|
|
|
|
vec![
|
|
|
|
(stake_address, stake_account),
|
|
|
|
(
|
|
|
|
recipient_address,
|
|
|
|
AccountSharedData::new(rent_exempt_reserve, 0, &system_program::id()),
|
|
|
|
),
|
|
|
|
(
|
2023-04-03 08:23:24 -07:00
|
|
|
clock::id(),
|
|
|
|
create_account_shared_data_for_test(&Clock::default()),
|
2022-04-01 16:08:55 -07:00
|
|
|
),
|
|
|
|
(
|
2023-04-03 08:23:24 -07:00
|
|
|
rent::id(),
|
|
|
|
create_account_shared_data_for_test(&Rent::free()),
|
2022-04-01 16:08:55 -07:00
|
|
|
),
|
|
|
|
(
|
2023-04-03 08:23:24 -07:00
|
|
|
stake_history::id(),
|
|
|
|
create_account_shared_data_for_test(&StakeHistory::default()),
|
2022-04-01 16:08:55 -07:00
|
|
|
),
|
|
|
|
(
|
|
|
|
stake_config::id(),
|
|
|
|
config::create_account(0, &stake_config::Config::default()),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
expected_result.clone(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-07 16:01:05 -07:00
|
|
|
/// The stake program's old behavior allowed delegations below the minimum stake delegation
|
|
|
|
/// (see also `test_delegate_minimum_stake_delegation()`). This was not the desired behavior,
|
|
|
|
/// and has been fixed in the new behavior. This test ensures the behavior is not changed
|
|
|
|
/// inadvertently.
|
2022-04-01 16:08:55 -07:00
|
|
|
///
|
|
|
|
/// This test:
|
|
|
|
/// 1. Initialises a stake account (with sufficient balance for both rent and minimum delegation)
|
|
|
|
/// 2. Delegates the minimum amount
|
|
|
|
/// 3. Deactives the delegation
|
|
|
|
/// 4. Withdraws from the account such that the ending balance is *below* rent + minimum delegation
|
|
|
|
/// 5. Re-delegates, now with less than the minimum delegation, but it still succeeds
|
2022-07-06 14:15:01 -07:00
|
|
|
//
|
|
|
|
// The "old old" behavior relies on `validate_delegated_amount()` *not* checking if the
|
|
|
|
// stake amount meets the minimum delegation. Once the
|
|
|
|
// `stake_allow_zero_undelegated_amount` feature is activated, `the expected_result`
|
|
|
|
// parameter can be removed and consolidated.
|
|
|
|
#[test_case(feature_set_old_old_behavior(), Ok(()); "old_old_behavior")]
|
|
|
|
#[test_case(feature_set_new_behavior(), Err(StakeError::InsufficientDelegation.into()); "new_behavior")]
|
|
|
|
fn test_behavior_withdrawal_then_redelegate_with_less_than_minimum_stake_delegation(
|
2023-04-03 08:23:24 -07:00
|
|
|
feature_set: Arc<FeatureSet>,
|
2022-05-07 16:01:05 -07:00
|
|
|
expected_result: Result<(), InstructionError>,
|
|
|
|
) {
|
2022-04-01 22:11:10 -07:00
|
|
|
let minimum_delegation = crate::get_minimum_delegation(&feature_set);
|
2022-04-01 16:08:55 -07:00
|
|
|
let rent = Rent::default();
|
2022-04-19 10:04:12 -07:00
|
|
|
let rent_exempt_reserve = rent.minimum_balance(StakeState::size_of());
|
2022-04-01 16:08:55 -07:00
|
|
|
let stake_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let stake_account = AccountSharedData::new(
|
2022-04-01 22:11:10 -07:00
|
|
|
rent_exempt_reserve + minimum_delegation,
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-04-01 16:08:55 -07:00
|
|
|
&id(),
|
|
|
|
);
|
|
|
|
let vote_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let vote_account =
|
|
|
|
vote_state::create_account(&vote_address, &solana_sdk::pubkey::new_rand(), 0, 100);
|
|
|
|
let recipient_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let mut clock = Clock::default();
|
|
|
|
let mut transaction_accounts = vec![
|
|
|
|
(stake_address, stake_account),
|
|
|
|
(vote_address, vote_account),
|
|
|
|
(
|
|
|
|
recipient_address,
|
|
|
|
AccountSharedData::new(rent_exempt_reserve, 0, &system_program::id()),
|
|
|
|
),
|
2023-04-03 08:23:24 -07:00
|
|
|
(clock::id(), create_account_shared_data_for_test(&clock)),
|
2022-04-01 16:08:55 -07:00
|
|
|
(
|
2023-04-03 08:23:24 -07:00
|
|
|
stake_history::id(),
|
|
|
|
create_account_shared_data_for_test(&StakeHistory::default()),
|
2022-04-01 16:08:55 -07:00
|
|
|
),
|
|
|
|
(
|
|
|
|
stake_config::id(),
|
|
|
|
config::create_account(0, &stake_config::Config::default()),
|
|
|
|
),
|
2023-04-03 08:23:24 -07:00
|
|
|
(rent::id(), create_account_shared_data_for_test(&rent)),
|
2022-04-01 16:08:55 -07:00
|
|
|
];
|
|
|
|
let instruction_accounts = vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: true,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-04-01 16:08:55 -07:00
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: vote_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: clock::id(),
|
2022-04-01 16:08:55 -07:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: stake_history::id(),
|
2022-04-01 16:08:55 -07:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_config::id(),
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
let accounts = process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-04-01 16:08:55 -07:00
|
|
|
&serialize(&StakeInstruction::Initialize(
|
|
|
|
Authorized::auto(&stake_address),
|
|
|
|
Lockup::default(),
|
|
|
|
))
|
|
|
|
.unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: true,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-04-01 16:08:55 -07:00
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: rent::id(),
|
2022-04-01 16:08:55 -07:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
transaction_accounts[0] = (stake_address, accounts[0].clone());
|
|
|
|
|
|
|
|
let accounts = process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-04-01 16:08:55 -07:00
|
|
|
&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;
|
2023-04-03 08:23:24 -07:00
|
|
|
transaction_accounts[3] = (clock::id(), create_account_shared_data_for_test(&clock));
|
2022-04-01 16:08:55 -07:00
|
|
|
let accounts = process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-04-01 16:08:55 -07:00
|
|
|
&serialize(&StakeInstruction::Deactivate).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: true,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-04-01 16:08:55 -07:00
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: clock::id(),
|
2022-04-01 16:08:55 -07:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
transaction_accounts[0] = (stake_address, accounts[0].clone());
|
|
|
|
|
|
|
|
clock.epoch += 1;
|
2023-04-03 08:23:24 -07:00
|
|
|
transaction_accounts[3] = (clock::id(), create_account_shared_data_for_test(&clock));
|
2022-04-01 16:08:55 -07:00
|
|
|
let withdraw_amount =
|
2022-04-01 22:11:10 -07:00
|
|
|
accounts[0].lamports() - (rent_exempt_reserve + minimum_delegation - 1);
|
2022-05-07 16:01:05 -07:00
|
|
|
let accounts = process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-04-01 16:08:55 -07:00
|
|
|
&serialize(&StakeInstruction::Withdraw(withdraw_amount)).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-04-01 16:08:55 -07:00
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: recipient_address,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-04-01 16:08:55 -07:00
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: clock::id(),
|
2022-04-01 16:08:55 -07:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: stake_history::id(),
|
2022-04-01 16:08:55 -07:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
Ok(()),
|
|
|
|
);
|
2022-04-15 03:15:02 -07:00
|
|
|
transaction_accounts[0] = (stake_address, accounts[0].clone());
|
2022-04-01 16:08:55 -07:00
|
|
|
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-04-01 16:08:55 -07:00
|
|
|
&serialize(&StakeInstruction::DelegateStake).unwrap(),
|
|
|
|
transaction_accounts,
|
|
|
|
instruction_accounts,
|
2022-05-07 16:01:05 -07:00
|
|
|
expected_result,
|
2022-04-01 16:08:55 -07:00
|
|
|
);
|
|
|
|
}
|
2022-04-05 03:36:01 -07:00
|
|
|
|
2022-07-06 14:15:01 -07:00
|
|
|
#[test_case(feature_set_old_behavior(); "old_behavior")]
|
|
|
|
#[test_case(feature_set_new_behavior(); "new_behavior")]
|
2023-04-03 08:23:24 -07:00
|
|
|
fn test_split_source_uninitialized(feature_set: Arc<FeatureSet>) {
|
2022-04-05 03:36:01 -07:00
|
|
|
let rent = Rent::default();
|
2022-04-19 10:04:12 -07:00
|
|
|
let rent_exempt_reserve = rent.minimum_balance(StakeState::size_of());
|
2022-05-07 16:01:05 -07:00
|
|
|
let minimum_delegation = crate::get_minimum_delegation(&feature_set);
|
2022-04-05 03:36:01 -07:00
|
|
|
let stake_lamports = (rent_exempt_reserve + minimum_delegation) * 2;
|
|
|
|
let stake_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let stake_account = AccountSharedData::new_data_with_space(
|
|
|
|
stake_lamports,
|
|
|
|
&StakeState::Uninitialized,
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-04-05 03:36:01 -07:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let split_to_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let split_to_account = AccountSharedData::new_data_with_space(
|
|
|
|
0,
|
|
|
|
&StakeState::Uninitialized,
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-04-05 03:36:01 -07:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let transaction_accounts = vec![
|
|
|
|
(stake_address, stake_account),
|
|
|
|
(split_to_address, split_to_account),
|
|
|
|
];
|
|
|
|
let mut instruction_accounts = vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: true,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-04-05 03:36:01 -07:00
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-04-05 03:36:01 -07:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
// splitting an uninitialized account where the destination is the same as the source
|
|
|
|
{
|
|
|
|
// splitting should work when...
|
|
|
|
// - when split amount is the full balance
|
|
|
|
// - when split amount is zero
|
|
|
|
// - when split amount is non-zero and less than the full balance
|
|
|
|
//
|
|
|
|
// and splitting should fail when the split amount is greater than the balance
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-04-05 03:36:01 -07:00
|
|
|
&serialize(&StakeInstruction::Split(stake_lamports)).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-04-05 03:36:01 -07:00
|
|
|
&serialize(&StakeInstruction::Split(0)).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-04-05 03:36:01 -07:00
|
|
|
&serialize(&StakeInstruction::Split(stake_lamports / 2)).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-04-05 03:36:01 -07:00
|
|
|
&serialize(&StakeInstruction::Split(stake_lamports + 1)).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Err(InstructionError::InsufficientFunds),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// this should work
|
|
|
|
instruction_accounts[1].pubkey = split_to_address;
|
|
|
|
let accounts = process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-04-05 03:36:01 -07:00
|
|
|
&serialize(&StakeInstruction::Split(stake_lamports / 2)).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
assert_eq!(accounts[0].lamports(), accounts[1].lamports());
|
|
|
|
|
|
|
|
// no signers should fail
|
|
|
|
instruction_accounts[0].is_signer = false;
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-04-05 03:36:01 -07:00
|
|
|
&serialize(&StakeInstruction::Split(stake_lamports / 2)).unwrap(),
|
|
|
|
transaction_accounts,
|
|
|
|
instruction_accounts,
|
|
|
|
Err(InstructionError::MissingRequiredSignature),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-07-06 14:15:01 -07:00
|
|
|
#[test_case(feature_set_old_behavior(); "old_behavior")]
|
|
|
|
#[test_case(feature_set_new_behavior(); "new_behavior")]
|
2023-04-03 08:23:24 -07:00
|
|
|
fn test_split_split_not_uninitialized(feature_set: Arc<FeatureSet>) {
|
2022-04-05 03:36:01 -07:00
|
|
|
let stake_lamports = 42;
|
|
|
|
let stake_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let stake_account = AccountSharedData::new_data_with_space(
|
|
|
|
stake_lamports,
|
|
|
|
&just_stake(Meta::auto(&stake_address), stake_lamports),
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-04-05 03:36:01 -07:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let split_to_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let instruction_accounts = vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: true,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-04-05 03:36:01 -07:00
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-04-05 03:36:01 -07:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
for split_to_state in &[
|
|
|
|
StakeState::Initialized(Meta::default()),
|
|
|
|
StakeState::Stake(Meta::default(), Stake::default()),
|
|
|
|
StakeState::RewardsPool,
|
|
|
|
] {
|
|
|
|
let split_to_account = AccountSharedData::new_data_with_space(
|
|
|
|
0,
|
|
|
|
split_to_state,
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-04-05 03:36:01 -07:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-04-05 03:36:01 -07:00
|
|
|
&serialize(&StakeInstruction::Split(stake_lamports / 2)).unwrap(),
|
|
|
|
vec![
|
|
|
|
(stake_address, stake_account.clone()),
|
|
|
|
(split_to_address, split_to_account),
|
|
|
|
],
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-06 14:15:01 -07:00
|
|
|
#[test_case(feature_set_old_behavior(); "old_behavior")]
|
|
|
|
#[test_case(feature_set_new_behavior(); "new_behavior")]
|
2023-04-03 08:23:24 -07:00
|
|
|
fn test_split_more_than_staked(feature_set: Arc<FeatureSet>) {
|
2022-04-05 03:36:01 -07:00
|
|
|
let rent = Rent::default();
|
2022-04-19 10:04:12 -07:00
|
|
|
let rent_exempt_reserve = rent.minimum_balance(StakeState::size_of());
|
2022-05-07 16:01:05 -07:00
|
|
|
let minimum_delegation = crate::get_minimum_delegation(&feature_set);
|
2022-04-05 03:36:01 -07:00
|
|
|
let stake_lamports = (rent_exempt_reserve + minimum_delegation) * 2;
|
|
|
|
let stake_address = solana_sdk::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,
|
|
|
|
),
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-04-05 03:36:01 -07:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let split_to_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let split_to_account = AccountSharedData::new_data_with_space(
|
|
|
|
0,
|
|
|
|
&StakeState::Uninitialized,
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-04-05 03:36:01 -07:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let transaction_accounts = vec![
|
|
|
|
(stake_address, stake_account),
|
|
|
|
(split_to_address, split_to_account),
|
2023-04-03 08:23:24 -07:00
|
|
|
(rent::id(), create_account_shared_data_for_test(&rent)),
|
2022-04-05 03:36:01 -07:00
|
|
|
];
|
|
|
|
let instruction_accounts = vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: true,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-04-05 03:36:01 -07:00
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: split_to_address,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-04-05 03:36:01 -07:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-04-05 03:36:01 -07:00
|
|
|
&serialize(&StakeInstruction::Split(stake_lamports / 2)).unwrap(),
|
|
|
|
transaction_accounts,
|
|
|
|
instruction_accounts,
|
|
|
|
Err(StakeError::InsufficientStake.into()),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-07-06 14:15:01 -07:00
|
|
|
#[test_case(feature_set_old_behavior(); "old_behavior")]
|
|
|
|
#[test_case(feature_set_new_behavior(); "new_behavior")]
|
2023-04-03 08:23:24 -07:00
|
|
|
fn test_split_with_rent(feature_set: Arc<FeatureSet>) {
|
2022-04-05 03:36:01 -07:00
|
|
|
let rent = Rent::default();
|
2022-04-19 10:04:12 -07:00
|
|
|
let rent_exempt_reserve = rent.minimum_balance(StakeState::size_of());
|
2022-05-07 16:01:05 -07:00
|
|
|
let minimum_delegation = crate::get_minimum_delegation(&feature_set);
|
2022-04-05 03:36:01 -07:00
|
|
|
let stake_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let split_to_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let split_to_account = AccountSharedData::new_data_with_space(
|
|
|
|
0,
|
|
|
|
&StakeState::Uninitialized,
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-04-05 03:36:01 -07:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let instruction_accounts = vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: true,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-04-05 03:36:01 -07:00
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: split_to_address,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-04-05 03:36:01 -07:00
|
|
|
},
|
|
|
|
];
|
|
|
|
let meta = Meta {
|
|
|
|
authorized: Authorized::auto(&stake_address),
|
|
|
|
rent_exempt_reserve,
|
|
|
|
..Meta::default()
|
|
|
|
};
|
|
|
|
|
|
|
|
// test splitting both an Initialized stake and a Staked stake
|
2022-05-04 03:17:29 -07:00
|
|
|
for (minimum_balance, state) in &[
|
|
|
|
(rent_exempt_reserve, StakeState::Initialized(meta)),
|
|
|
|
(
|
|
|
|
rent_exempt_reserve + minimum_delegation,
|
|
|
|
just_stake(meta, minimum_delegation * 2 + rent_exempt_reserve),
|
|
|
|
),
|
2022-04-05 03:36:01 -07:00
|
|
|
] {
|
2022-05-04 03:17:29 -07:00
|
|
|
let stake_lamports = minimum_balance * 2;
|
2022-04-05 03:36:01 -07:00
|
|
|
let stake_account = AccountSharedData::new_data_with_space(
|
|
|
|
stake_lamports,
|
|
|
|
state,
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-04-05 03:36:01 -07:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let mut transaction_accounts = vec![
|
|
|
|
(stake_address, stake_account),
|
|
|
|
(split_to_address, split_to_account.clone()),
|
2023-04-03 08:23:24 -07:00
|
|
|
(rent::id(), create_account_shared_data_for_test(&rent)),
|
2022-04-05 03:36:01 -07:00
|
|
|
];
|
|
|
|
|
|
|
|
// not enough to make a non-zero stake account
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-05-04 03:17:29 -07:00
|
|
|
&serialize(&StakeInstruction::Split(minimum_balance - 1)).unwrap(),
|
2022-04-05 03:36:01 -07:00
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Err(InstructionError::InsufficientFunds),
|
|
|
|
);
|
|
|
|
|
|
|
|
// doesn't leave enough for initial stake to be non-zero
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-04-05 03:36:01 -07:00
|
|
|
&serialize(&StakeInstruction::Split(
|
2022-05-04 03:17:29 -07:00
|
|
|
stake_lamports - minimum_balance + 1,
|
2022-04-05 03:36:01 -07:00
|
|
|
))
|
|
|
|
.unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Err(InstructionError::InsufficientFunds),
|
|
|
|
);
|
|
|
|
|
|
|
|
// split account already has way enough lamports
|
2022-05-04 03:17:29 -07:00
|
|
|
transaction_accounts[1].1.set_lamports(*minimum_balance);
|
2022-04-05 03:36:01 -07:00
|
|
|
let accounts = process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-04-05 03:36:01 -07:00
|
|
|
&serialize(&StakeInstruction::Split(stake_lamports - minimum_balance)).unwrap(),
|
|
|
|
transaction_accounts,
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
|
|
|
|
// verify no stake leakage in the case of a stake
|
|
|
|
if let StakeState::Stake(meta, stake) = state {
|
|
|
|
assert_eq!(
|
|
|
|
accounts[1].state(),
|
|
|
|
Ok(StakeState::Stake(
|
|
|
|
*meta,
|
|
|
|
Stake {
|
|
|
|
delegation: Delegation {
|
|
|
|
stake: stake_lamports - minimum_balance,
|
|
|
|
..stake.delegation
|
|
|
|
},
|
|
|
|
..*stake
|
|
|
|
}
|
|
|
|
))
|
|
|
|
);
|
2022-05-04 03:17:29 -07:00
|
|
|
assert_eq!(accounts[0].lamports(), *minimum_balance,);
|
2022-04-05 03:36:01 -07:00
|
|
|
assert_eq!(accounts[1].lamports(), stake_lamports,);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-06 14:15:01 -07:00
|
|
|
#[test_case(feature_set_old_behavior(); "old_behavior")]
|
|
|
|
#[test_case(feature_set_new_behavior(); "new_behavior")]
|
2023-04-03 08:23:24 -07:00
|
|
|
fn test_split_to_account_with_rent_exempt_reserve(feature_set: Arc<FeatureSet>) {
|
2022-04-05 03:36:01 -07:00
|
|
|
let rent = Rent::default();
|
2022-04-19 10:04:12 -07:00
|
|
|
let rent_exempt_reserve = rent.minimum_balance(StakeState::size_of());
|
2022-05-07 16:01:05 -07:00
|
|
|
let minimum_delegation = crate::get_minimum_delegation(&feature_set);
|
2022-04-05 03:36:01 -07:00
|
|
|
let stake_lamports = (rent_exempt_reserve + minimum_delegation) * 2;
|
|
|
|
let stake_address = solana_sdk::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,
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-04-05 03:36:01 -07:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let split_to_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let instruction_accounts = vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: true,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-04-05 03:36:01 -07:00
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: split_to_address,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-04-05 03:36:01 -07:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
// Test various account prefunding, including empty, less than rent_exempt_reserve, exactly
|
|
|
|
// rent_exempt_reserve, and more than rent_exempt_reserve. The empty case is not covered in
|
|
|
|
// test_split, since that test uses a Meta with rent_exempt_reserve = 0
|
|
|
|
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,
|
|
|
|
&StakeState::Uninitialized,
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-04-05 03:36:01 -07:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let transaction_accounts = vec![
|
|
|
|
(stake_address, stake_account.clone()),
|
|
|
|
(split_to_address, split_to_account),
|
2023-04-03 08:23:24 -07:00
|
|
|
(rent::id(), create_account_shared_data_for_test(&rent)),
|
2022-04-05 03:36:01 -07:00
|
|
|
];
|
|
|
|
|
|
|
|
// split more than available fails
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-04-05 03:36:01 -07:00
|
|
|
&serialize(&StakeInstruction::Split(stake_lamports + 1)).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Err(InstructionError::InsufficientFunds),
|
|
|
|
);
|
|
|
|
|
|
|
|
// should work
|
|
|
|
let accounts = process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-04-05 03:36:01 -07:00
|
|
|
&serialize(&StakeInstruction::Split(stake_lamports / 2)).unwrap(),
|
|
|
|
transaction_accounts,
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
// no lamport leakage
|
|
|
|
assert_eq!(
|
|
|
|
accounts[0].lamports() + accounts[1].lamports(),
|
|
|
|
stake_lamports + initial_balance,
|
|
|
|
);
|
|
|
|
|
|
|
|
if let StakeState::Stake(meta, stake) = state {
|
|
|
|
let expected_stake =
|
|
|
|
stake_lamports / 2 - (rent_exempt_reserve.saturating_sub(initial_balance));
|
|
|
|
assert_eq!(
|
|
|
|
Ok(StakeState::Stake(
|
|
|
|
meta,
|
|
|
|
Stake {
|
|
|
|
delegation: Delegation {
|
|
|
|
stake: stake_lamports / 2
|
|
|
|
- (rent_exempt_reserve.saturating_sub(initial_balance)),
|
|
|
|
..stake.delegation
|
|
|
|
},
|
|
|
|
..stake
|
|
|
|
}
|
|
|
|
)),
|
|
|
|
accounts[1].state(),
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
accounts[1].lamports(),
|
|
|
|
expected_stake
|
|
|
|
+ rent_exempt_reserve
|
|
|
|
+ initial_balance.saturating_sub(rent_exempt_reserve),
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
Ok(StakeState::Stake(
|
|
|
|
meta,
|
|
|
|
Stake {
|
|
|
|
delegation: Delegation {
|
|
|
|
stake: stake_lamports / 2 - rent_exempt_reserve,
|
|
|
|
..stake.delegation
|
|
|
|
},
|
|
|
|
..stake
|
|
|
|
}
|
|
|
|
)),
|
|
|
|
accounts[0].state(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-06 14:15:01 -07:00
|
|
|
#[test_case(feature_set_old_behavior(); "old_behavior")]
|
|
|
|
#[test_case(feature_set_new_behavior(); "new_behavior")]
|
2023-04-03 08:23:24 -07:00
|
|
|
fn test_split_from_larger_sized_account(feature_set: Arc<FeatureSet>) {
|
2022-04-05 03:36:01 -07:00
|
|
|
let rent = Rent::default();
|
2022-04-19 10:04:12 -07:00
|
|
|
let source_larger_rent_exempt_reserve = rent.minimum_balance(StakeState::size_of() + 100);
|
|
|
|
let split_rent_exempt_reserve = rent.minimum_balance(StakeState::size_of());
|
2022-05-07 16:01:05 -07:00
|
|
|
let minimum_delegation = crate::get_minimum_delegation(&feature_set);
|
2022-04-05 03:36:01 -07:00
|
|
|
let stake_lamports = (source_larger_rent_exempt_reserve + minimum_delegation) * 2;
|
|
|
|
let stake_address = solana_sdk::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,
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of() + 100,
|
2022-04-05 03:36:01 -07:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let split_to_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let instruction_accounts = vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: true,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-04-05 03:36:01 -07:00
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: split_to_address,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-04-05 03:36:01 -07:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
// Test various account prefunding, including empty, less than rent_exempt_reserve, exactly
|
|
|
|
// rent_exempt_reserve, and more than rent_exempt_reserve. The empty case is not covered in
|
|
|
|
// test_split, since that test uses a Meta with rent_exempt_reserve = 0
|
|
|
|
let split_lamport_balances = vec![
|
|
|
|
0,
|
|
|
|
split_rent_exempt_reserve - 1,
|
|
|
|
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 split_to_account = AccountSharedData::new_data_with_space(
|
|
|
|
initial_balance,
|
|
|
|
&StakeState::Uninitialized,
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-04-05 03:36:01 -07:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let transaction_accounts = vec![
|
|
|
|
(stake_address, stake_account.clone()),
|
|
|
|
(split_to_address, split_to_account),
|
2023-04-03 08:23:24 -07:00
|
|
|
(rent::id(), create_account_shared_data_for_test(&rent)),
|
2022-04-05 03:36:01 -07:00
|
|
|
];
|
|
|
|
|
|
|
|
// split more than available fails
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-04-05 03:36:01 -07:00
|
|
|
&serialize(&StakeInstruction::Split(stake_lamports + 1)).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Err(InstructionError::InsufficientFunds),
|
|
|
|
);
|
|
|
|
|
|
|
|
// should work
|
|
|
|
let accounts = process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-04-05 03:36:01 -07:00
|
|
|
&serialize(&StakeInstruction::Split(stake_lamports / 2)).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
// no lamport leakage
|
|
|
|
assert_eq!(
|
|
|
|
accounts[0].lamports() + accounts[1].lamports(),
|
|
|
|
stake_lamports + initial_balance
|
|
|
|
);
|
|
|
|
|
|
|
|
if let StakeState::Stake(meta, stake) = 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(StakeState::Stake(
|
|
|
|
expected_split_meta,
|
|
|
|
Stake {
|
|
|
|
delegation: Delegation {
|
|
|
|
stake: expected_stake,
|
|
|
|
..stake.delegation
|
|
|
|
},
|
|
|
|
..stake
|
|
|
|
}
|
|
|
|
)),
|
|
|
|
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(StakeState::Stake(
|
|
|
|
meta,
|
|
|
|
Stake {
|
|
|
|
delegation: Delegation {
|
|
|
|
stake: stake_lamports / 2 - source_larger_rent_exempt_reserve,
|
|
|
|
..stake.delegation
|
|
|
|
},
|
|
|
|
..stake
|
|
|
|
}
|
|
|
|
)),
|
|
|
|
accounts[0].state()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-06 14:15:01 -07:00
|
|
|
#[test_case(feature_set_old_behavior(); "old_behavior")]
|
|
|
|
#[test_case(feature_set_new_behavior(); "new_behavior")]
|
2023-04-03 08:23:24 -07:00
|
|
|
fn test_split_from_smaller_sized_account(feature_set: Arc<FeatureSet>) {
|
2022-04-05 03:36:01 -07:00
|
|
|
let rent = Rent::default();
|
2022-04-19 10:04:12 -07:00
|
|
|
let source_smaller_rent_exempt_reserve = rent.minimum_balance(StakeState::size_of());
|
|
|
|
let split_rent_exempt_reserve = rent.minimum_balance(StakeState::size_of() + 100);
|
2022-04-05 03:36:01 -07:00
|
|
|
let stake_lamports = split_rent_exempt_reserve + 1;
|
|
|
|
let stake_address = solana_sdk::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,
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-04-05 03:36:01 -07:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let split_to_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let instruction_accounts = vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: true,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-04-05 03:36:01 -07:00
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: split_to_address,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-04-05 03:36:01 -07:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
let split_amount = stake_lamports - (source_smaller_rent_exempt_reserve + 1); // Enough so that split stake is > 0
|
|
|
|
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,
|
|
|
|
&StakeState::Uninitialized,
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of() + 100,
|
2022-04-05 03:36:01 -07:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let transaction_accounts = vec![
|
|
|
|
(stake_address, stake_account.clone()),
|
|
|
|
(split_to_address, split_to_account),
|
2023-04-03 08:23:24 -07:00
|
|
|
(rent::id(), create_account_shared_data_for_test(&rent)),
|
2022-04-05 03:36:01 -07:00
|
|
|
];
|
|
|
|
|
|
|
|
// should always return error when splitting to larger account
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-04-05 03:36:01 -07:00
|
|
|
&serialize(&StakeInstruction::Split(split_amount)).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Splitting 100% of source should not make a difference
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-04-05 03:36:01 -07:00
|
|
|
&serialize(&StakeInstruction::Split(stake_lamports)).unwrap(),
|
|
|
|
transaction_accounts,
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-06 14:15:01 -07:00
|
|
|
#[test_case(feature_set_old_behavior(); "old_behavior")]
|
|
|
|
#[test_case(feature_set_new_behavior(); "new_behavior")]
|
2023-04-03 08:23:24 -07:00
|
|
|
fn test_split_100_percent_of_source(feature_set: Arc<FeatureSet>) {
|
2022-04-05 03:36:01 -07:00
|
|
|
let rent = Rent::default();
|
2022-04-19 10:04:12 -07:00
|
|
|
let rent_exempt_reserve = rent.minimum_balance(StakeState::size_of());
|
2022-05-07 16:01:05 -07:00
|
|
|
let minimum_delegation = crate::get_minimum_delegation(&feature_set);
|
2022-04-05 03:36:01 -07:00
|
|
|
let stake_lamports = rent_exempt_reserve + minimum_delegation;
|
|
|
|
let stake_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let meta = Meta {
|
|
|
|
authorized: Authorized::auto(&stake_address),
|
|
|
|
rent_exempt_reserve,
|
|
|
|
..Meta::default()
|
|
|
|
};
|
|
|
|
let split_to_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let split_to_account = AccountSharedData::new_data_with_space(
|
|
|
|
0,
|
|
|
|
&StakeState::Uninitialized,
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-04-05 03:36:01 -07:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let instruction_accounts = vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: true,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-04-05 03:36:01 -07:00
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: split_to_address,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-04-05 03:36:01 -07:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
// test splitting both an Initialized stake and a Staked stake
|
|
|
|
for state in &[
|
|
|
|
StakeState::Initialized(meta),
|
|
|
|
just_stake(meta, stake_lamports - rent_exempt_reserve),
|
|
|
|
] {
|
|
|
|
let stake_account = AccountSharedData::new_data_with_space(
|
|
|
|
stake_lamports,
|
|
|
|
&state,
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-04-05 03:36:01 -07:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let transaction_accounts = vec![
|
|
|
|
(stake_address, stake_account),
|
|
|
|
(split_to_address, split_to_account.clone()),
|
2023-04-03 08:23:24 -07:00
|
|
|
(rent::id(), create_account_shared_data_for_test(&rent)),
|
2022-04-05 03:36:01 -07:00
|
|
|
];
|
|
|
|
|
|
|
|
// split 100% over to dest
|
|
|
|
let accounts = process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-04-05 03:36:01 -07:00
|
|
|
&serialize(&StakeInstruction::Split(stake_lamports)).unwrap(),
|
|
|
|
transaction_accounts,
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
|
|
|
|
// no lamport leakage
|
|
|
|
assert_eq!(
|
|
|
|
accounts[0].lamports() + accounts[1].lamports(),
|
|
|
|
stake_lamports
|
|
|
|
);
|
|
|
|
|
|
|
|
match state {
|
|
|
|
StakeState::Initialized(_) => {
|
|
|
|
assert_eq!(Ok(*state), accounts[1].state());
|
|
|
|
assert_eq!(Ok(StakeState::Uninitialized), accounts[0].state());
|
|
|
|
}
|
|
|
|
StakeState::Stake(meta, stake) => {
|
|
|
|
assert_eq!(
|
|
|
|
Ok(StakeState::Stake(
|
|
|
|
*meta,
|
|
|
|
Stake {
|
|
|
|
delegation: Delegation {
|
|
|
|
stake: stake_lamports - rent_exempt_reserve,
|
|
|
|
..stake.delegation
|
|
|
|
},
|
|
|
|
..*stake
|
|
|
|
}
|
|
|
|
)),
|
|
|
|
accounts[1].state()
|
|
|
|
);
|
|
|
|
assert_eq!(Ok(StakeState::Uninitialized), accounts[0].state());
|
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-06 14:15:01 -07:00
|
|
|
#[test_case(feature_set_old_behavior(); "old_behavior")]
|
|
|
|
#[test_case(feature_set_new_behavior(); "new_behavior")]
|
2023-04-03 08:23:24 -07:00
|
|
|
fn test_split_100_percent_of_source_to_account_with_lamports(feature_set: Arc<FeatureSet>) {
|
2022-04-05 03:36:01 -07:00
|
|
|
let rent = Rent::default();
|
2022-04-19 10:04:12 -07:00
|
|
|
let rent_exempt_reserve = rent.minimum_balance(StakeState::size_of());
|
2022-05-07 16:01:05 -07:00
|
|
|
let minimum_delegation = crate::get_minimum_delegation(&feature_set);
|
2022-04-05 03:36:01 -07:00
|
|
|
let stake_lamports = rent_exempt_reserve + minimum_delegation;
|
|
|
|
let stake_address = solana_sdk::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,
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-04-05 03:36:01 -07:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let split_to_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let instruction_accounts = vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: true,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-04-05 03:36:01 -07:00
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: split_to_address,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-04-05 03:36:01 -07:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
// Test various account prefunding, including empty, less than rent_exempt_reserve, exactly
|
|
|
|
// rent_exempt_reserve, and more than rent_exempt_reserve. Technically, the empty case is
|
|
|
|
// covered in test_split_100_percent_of_source, but included here as well for readability
|
|
|
|
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,
|
|
|
|
&StakeState::Uninitialized,
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-04-05 03:36:01 -07:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let transaction_accounts = vec![
|
|
|
|
(stake_address, stake_account.clone()),
|
|
|
|
(split_to_address, split_to_account),
|
2023-04-03 08:23:24 -07:00
|
|
|
(rent::id(), create_account_shared_data_for_test(&rent)),
|
2022-04-05 03:36:01 -07:00
|
|
|
];
|
|
|
|
|
|
|
|
// split 100% over to dest
|
|
|
|
let accounts = process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-04-05 03:36:01 -07:00
|
|
|
&serialize(&StakeInstruction::Split(stake_lamports)).unwrap(),
|
|
|
|
transaction_accounts,
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
|
|
|
|
// no lamport leakage
|
|
|
|
assert_eq!(
|
|
|
|
accounts[0].lamports() + accounts[1].lamports(),
|
|
|
|
stake_lamports + initial_balance
|
|
|
|
);
|
|
|
|
|
|
|
|
if let StakeState::Stake(meta, stake) = state {
|
|
|
|
assert_eq!(
|
|
|
|
Ok(StakeState::Stake(
|
|
|
|
meta,
|
|
|
|
Stake {
|
|
|
|
delegation: Delegation {
|
|
|
|
stake: stake_lamports - rent_exempt_reserve,
|
|
|
|
..stake.delegation
|
|
|
|
},
|
|
|
|
..stake
|
|
|
|
}
|
|
|
|
)),
|
|
|
|
accounts[1].state()
|
|
|
|
);
|
|
|
|
assert_eq!(Ok(StakeState::Uninitialized), accounts[0].state());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-06 14:15:01 -07:00
|
|
|
#[test_case(feature_set_old_behavior(); "old_behavior")]
|
|
|
|
#[test_case(feature_set_new_behavior(); "new_behavior")]
|
2023-04-03 08:23:24 -07:00
|
|
|
fn test_split_rent_exemptness(feature_set: Arc<FeatureSet>) {
|
2022-04-05 03:36:01 -07:00
|
|
|
let rent = Rent::default();
|
2022-04-19 10:04:12 -07:00
|
|
|
let source_rent_exempt_reserve = rent.minimum_balance(StakeState::size_of() + 100);
|
|
|
|
let split_rent_exempt_reserve = rent.minimum_balance(StakeState::size_of());
|
2022-05-07 16:01:05 -07:00
|
|
|
let minimum_delegation = crate::get_minimum_delegation(&feature_set);
|
2022-04-05 03:36:01 -07:00
|
|
|
let stake_lamports = source_rent_exempt_reserve + minimum_delegation;
|
|
|
|
let stake_address = solana_sdk::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_sdk::pubkey::new_rand();
|
|
|
|
let instruction_accounts = vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: true,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-04-05 03:36:01 -07:00
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: split_to_address,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-04-05 03:36:01 -07:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
for state in &[
|
|
|
|
StakeState::Initialized(meta),
|
|
|
|
just_stake(meta, stake_lamports - source_rent_exempt_reserve),
|
|
|
|
] {
|
|
|
|
// Test that splitting to a larger account fails
|
|
|
|
let stake_account = AccountSharedData::new_data_with_space(
|
|
|
|
stake_lamports,
|
|
|
|
&state,
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-04-05 03:36:01 -07:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let split_to_account = AccountSharedData::new_data_with_space(
|
|
|
|
0,
|
|
|
|
&StakeState::Uninitialized,
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of() + 10000,
|
2022-04-05 03:36:01 -07:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let transaction_accounts = vec![
|
|
|
|
(stake_address, stake_account),
|
|
|
|
(split_to_address, split_to_account),
|
2023-04-03 08:23:24 -07:00
|
|
|
(rent::id(), create_account_shared_data_for_test(&rent)),
|
2022-04-05 03:36:01 -07:00
|
|
|
];
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-04-05 03:36:01 -07:00
|
|
|
&serialize(&StakeInstruction::Split(stake_lamports)).unwrap(),
|
|
|
|
transaction_accounts,
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test that splitting from a larger account to a smaller one works.
|
|
|
|
// Split amount should not matter, assuming other fund criteria are met
|
|
|
|
let stake_account = AccountSharedData::new_data_with_space(
|
|
|
|
stake_lamports,
|
|
|
|
&state,
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of() + 100,
|
2022-04-05 03:36:01 -07:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let split_to_account = AccountSharedData::new_data_with_space(
|
|
|
|
0,
|
|
|
|
&StakeState::Uninitialized,
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-04-05 03:36:01 -07:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let transaction_accounts = vec![
|
|
|
|
(stake_address, stake_account),
|
|
|
|
(split_to_address, split_to_account),
|
2023-04-03 08:23:24 -07:00
|
|
|
(rent::id(), create_account_shared_data_for_test(&rent)),
|
2022-04-05 03:36:01 -07:00
|
|
|
];
|
|
|
|
let accounts = process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-04-05 03:36:01 -07:00
|
|
|
&serialize(&StakeInstruction::Split(stake_lamports)).unwrap(),
|
|
|
|
transaction_accounts,
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
assert_eq!(accounts[1].lamports(), stake_lamports);
|
|
|
|
|
|
|
|
let expected_split_meta = Meta {
|
|
|
|
authorized: Authorized::auto(&stake_address),
|
|
|
|
rent_exempt_reserve: split_rent_exempt_reserve,
|
|
|
|
..Meta::default()
|
|
|
|
};
|
|
|
|
match state {
|
|
|
|
StakeState::Initialized(_) => {
|
|
|
|
assert_eq!(
|
|
|
|
Ok(StakeState::Initialized(expected_split_meta)),
|
|
|
|
accounts[1].state()
|
|
|
|
);
|
|
|
|
assert_eq!(Ok(StakeState::Uninitialized), accounts[0].state());
|
|
|
|
}
|
|
|
|
StakeState::Stake(_meta, stake) => {
|
|
|
|
// Expected stake should reflect original stake amount so that extra lamports
|
|
|
|
// from the rent_exempt_reserve inequality do not magically activate
|
|
|
|
let expected_stake = stake_lamports - source_rent_exempt_reserve;
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
Ok(StakeState::Stake(
|
|
|
|
expected_split_meta,
|
|
|
|
Stake {
|
|
|
|
delegation: Delegation {
|
|
|
|
stake: expected_stake,
|
|
|
|
..stake.delegation
|
|
|
|
},
|
|
|
|
..*stake
|
|
|
|
}
|
|
|
|
)),
|
|
|
|
accounts[1].state()
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
accounts[1].lamports(),
|
|
|
|
expected_stake + source_rent_exempt_reserve,
|
|
|
|
);
|
|
|
|
assert_eq!(Ok(StakeState::Uninitialized), accounts[0].state());
|
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-06 03:04:35 -07:00
|
|
|
|
2022-07-06 14:15:01 -07:00
|
|
|
#[test_case(feature_set_old_behavior(); "old_behavior")]
|
|
|
|
#[test_case(feature_set_new_behavior(); "new_behavior")]
|
2023-04-03 08:23:24 -07:00
|
|
|
fn test_merge(feature_set: Arc<FeatureSet>) {
|
2022-04-06 03:04:35 -07:00
|
|
|
let stake_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let merge_from_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let authorized_address = solana_sdk::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,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-04-06 03:04:35 -07:00
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: merge_from_address,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-04-06 03:04:35 -07:00
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: clock::id(),
|
2022-04-06 03:04:35 -07:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: stake_history::id(),
|
2022-04-06 03:04:35 -07:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: authorized_address,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
for state in &[
|
|
|
|
StakeState::Initialized(meta),
|
|
|
|
just_stake(meta, stake_lamports),
|
|
|
|
] {
|
|
|
|
let stake_account = AccountSharedData::new_data_with_space(
|
|
|
|
stake_lamports,
|
|
|
|
state,
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-04-06 03:04:35 -07:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
for merge_from_state in &[
|
|
|
|
StakeState::Initialized(meta),
|
|
|
|
just_stake(meta, stake_lamports),
|
|
|
|
] {
|
|
|
|
let merge_from_account = AccountSharedData::new_data_with_space(
|
|
|
|
stake_lamports,
|
|
|
|
merge_from_state,
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-04-06 03:04:35 -07:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let transaction_accounts = vec![
|
|
|
|
(stake_address, stake_account.clone()),
|
|
|
|
(merge_from_address, merge_from_account),
|
|
|
|
(authorized_address, AccountSharedData::default()),
|
|
|
|
(
|
2023-04-03 08:23:24 -07:00
|
|
|
clock::id(),
|
|
|
|
create_account_shared_data_for_test(&Clock::default()),
|
2022-04-06 03:04:35 -07:00
|
|
|
),
|
|
|
|
(
|
2023-04-03 08:23:24 -07:00
|
|
|
stake_history::id(),
|
|
|
|
create_account_shared_data_for_test(&StakeHistory::default()),
|
2022-04-06 03:04:35 -07:00
|
|
|
),
|
|
|
|
];
|
|
|
|
|
|
|
|
// Authorized staker signature required...
|
|
|
|
instruction_accounts[4].is_signer = false;
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-04-06 03:04:35 -07:00
|
|
|
&serialize(&StakeInstruction::Merge).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Err(InstructionError::MissingRequiredSignature),
|
|
|
|
);
|
|
|
|
instruction_accounts[4].is_signer = true;
|
|
|
|
|
|
|
|
let accounts = process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-04-06 03:04:35 -07:00
|
|
|
&serialize(&StakeInstruction::Merge).unwrap(),
|
|
|
|
transaction_accounts,
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
|
|
|
|
// check lamports
|
|
|
|
assert_eq!(accounts[0].lamports(), stake_lamports * 2);
|
|
|
|
assert_eq!(accounts[1].lamports(), 0);
|
|
|
|
|
|
|
|
// check state
|
|
|
|
match state {
|
|
|
|
StakeState::Initialized(meta) => {
|
|
|
|
assert_eq!(accounts[0].state(), Ok(StakeState::Initialized(*meta)),);
|
|
|
|
}
|
|
|
|
StakeState::Stake(meta, stake) => {
|
|
|
|
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(StakeState::Stake(
|
|
|
|
*meta,
|
|
|
|
Stake {
|
|
|
|
delegation: Delegation {
|
|
|
|
stake: expected_stake,
|
|
|
|
..stake.delegation
|
|
|
|
},
|
|
|
|
..*stake
|
|
|
|
}
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
assert_eq!(accounts[1].state(), Ok(StakeState::Uninitialized));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-06 14:15:01 -07:00
|
|
|
#[test_case(feature_set_old_behavior(); "old_behavior")]
|
|
|
|
#[test_case(feature_set_new_behavior(); "new_behavior")]
|
2023-04-03 08:23:24 -07:00
|
|
|
fn test_merge_self_fails(feature_set: Arc<FeatureSet>) {
|
2022-04-06 03:04:35 -07:00
|
|
|
let stake_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let authorized_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let rent = Rent::default();
|
2022-04-19 10:04:12 -07:00
|
|
|
let rent_exempt_reserve = rent.minimum_balance(StakeState::size_of());
|
2022-04-06 03:04:35 -07:00
|
|
|
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,
|
|
|
|
&StakeState::Stake(meta, stake),
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-04-06 03:04:35 -07:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let transaction_accounts = vec![
|
|
|
|
(stake_address, stake_account),
|
|
|
|
(authorized_address, AccountSharedData::default()),
|
|
|
|
(
|
2023-04-03 08:23:24 -07:00
|
|
|
clock::id(),
|
|
|
|
create_account_shared_data_for_test(&Clock::default()),
|
2022-04-06 03:04:35 -07:00
|
|
|
),
|
|
|
|
(
|
2023-04-03 08:23:24 -07:00
|
|
|
stake_history::id(),
|
|
|
|
create_account_shared_data_for_test(&StakeHistory::default()),
|
2022-04-06 03:04:35 -07:00
|
|
|
),
|
|
|
|
];
|
|
|
|
let instruction_accounts = vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-04-06 03:04:35 -07:00
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-04-06 03:04:35 -07:00
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: clock::id(),
|
2022-04-06 03:04:35 -07:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: stake_history::id(),
|
2022-04-06 03:04:35 -07:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: authorized_address,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-04-06 03:04:35 -07:00
|
|
|
&serialize(&StakeInstruction::Merge).unwrap(),
|
|
|
|
transaction_accounts,
|
|
|
|
instruction_accounts,
|
|
|
|
Err(InstructionError::InvalidArgument),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-07-06 14:15:01 -07:00
|
|
|
#[test_case(feature_set_old_behavior(); "old_behavior")]
|
|
|
|
#[test_case(feature_set_new_behavior(); "new_behavior")]
|
2023-04-03 08:23:24 -07:00
|
|
|
fn test_merge_incorrect_authorized_staker(feature_set: Arc<FeatureSet>) {
|
2022-04-06 03:04:35 -07:00
|
|
|
let stake_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let merge_from_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let authorized_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let wrong_authorized_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let stake_lamports = 42;
|
|
|
|
let mut instruction_accounts = vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-04-06 03:04:35 -07:00
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: merge_from_address,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-04-06 03:04:35 -07:00
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: clock::id(),
|
2022-04-06 03:04:35 -07:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: stake_history::id(),
|
2022-04-06 03:04:35 -07:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: authorized_address,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
for state in &[
|
|
|
|
StakeState::Initialized(Meta::auto(&authorized_address)),
|
|
|
|
just_stake(Meta::auto(&authorized_address), stake_lamports),
|
|
|
|
] {
|
|
|
|
let stake_account = AccountSharedData::new_data_with_space(
|
|
|
|
stake_lamports,
|
|
|
|
state,
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-04-06 03:04:35 -07:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
for merge_from_state in &[
|
|
|
|
StakeState::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,
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-04-06 03:04:35 -07:00
|
|
|
&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()),
|
|
|
|
(
|
2023-04-03 08:23:24 -07:00
|
|
|
clock::id(),
|
|
|
|
create_account_shared_data_for_test(&Clock::default()),
|
2022-04-06 03:04:35 -07:00
|
|
|
),
|
|
|
|
(
|
2023-04-03 08:23:24 -07:00
|
|
|
stake_history::id(),
|
|
|
|
create_account_shared_data_for_test(&StakeHistory::default()),
|
2022-04-06 03:04:35 -07:00
|
|
|
),
|
|
|
|
];
|
|
|
|
|
|
|
|
instruction_accounts[4].pubkey = wrong_authorized_address;
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-04-06 03:04:35 -07:00
|
|
|
&serialize(&StakeInstruction::Merge).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Err(InstructionError::MissingRequiredSignature),
|
|
|
|
);
|
|
|
|
instruction_accounts[4].pubkey = authorized_address;
|
|
|
|
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-04-06 03:04:35 -07:00
|
|
|
&serialize(&StakeInstruction::Merge).unwrap(),
|
|
|
|
transaction_accounts,
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Err(StakeError::MergeMismatch.into()),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-06 14:15:01 -07:00
|
|
|
#[test_case(feature_set_old_behavior(); "old_behavior")]
|
|
|
|
#[test_case(feature_set_new_behavior(); "new_behavior")]
|
2023-04-03 08:23:24 -07:00
|
|
|
fn test_merge_invalid_account_data(feature_set: Arc<FeatureSet>) {
|
2022-04-06 03:04:35 -07:00
|
|
|
let stake_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let merge_from_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let authorized_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let stake_lamports = 42;
|
|
|
|
let instruction_accounts = vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-04-06 03:04:35 -07:00
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: merge_from_address,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-04-06 03:04:35 -07:00
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: clock::id(),
|
2022-04-06 03:04:35 -07:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: stake_history::id(),
|
2022-04-06 03:04:35 -07:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: authorized_address,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
for state in &[
|
|
|
|
StakeState::Uninitialized,
|
|
|
|
StakeState::RewardsPool,
|
|
|
|
StakeState::Initialized(Meta::auto(&authorized_address)),
|
|
|
|
just_stake(Meta::auto(&authorized_address), stake_lamports),
|
|
|
|
] {
|
|
|
|
let stake_account = AccountSharedData::new_data_with_space(
|
|
|
|
stake_lamports,
|
|
|
|
state,
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-04-06 03:04:35 -07:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
for merge_from_state in &[StakeState::Uninitialized, StakeState::RewardsPool] {
|
|
|
|
let merge_from_account = AccountSharedData::new_data_with_space(
|
|
|
|
stake_lamports,
|
|
|
|
merge_from_state,
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-04-06 03:04:35 -07:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let transaction_accounts = vec![
|
|
|
|
(stake_address, stake_account.clone()),
|
|
|
|
(merge_from_address, merge_from_account),
|
|
|
|
(authorized_address, AccountSharedData::default()),
|
|
|
|
(
|
2023-04-03 08:23:24 -07:00
|
|
|
clock::id(),
|
|
|
|
create_account_shared_data_for_test(&Clock::default()),
|
2022-04-06 03:04:35 -07:00
|
|
|
),
|
|
|
|
(
|
2023-04-03 08:23:24 -07:00
|
|
|
stake_history::id(),
|
|
|
|
create_account_shared_data_for_test(&StakeHistory::default()),
|
2022-04-06 03:04:35 -07:00
|
|
|
),
|
|
|
|
];
|
|
|
|
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-04-06 03:04:35 -07:00
|
|
|
&serialize(&StakeInstruction::Merge).unwrap(),
|
|
|
|
transaction_accounts,
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-06 14:15:01 -07:00
|
|
|
#[test_case(feature_set_old_behavior(); "old_behavior")]
|
|
|
|
#[test_case(feature_set_new_behavior(); "new_behavior")]
|
2023-04-03 08:23:24 -07:00
|
|
|
fn test_merge_fake_stake_source(feature_set: Arc<FeatureSet>) {
|
2022-04-06 03:04:35 -07:00
|
|
|
let stake_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let merge_from_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let authorized_address = solana_sdk::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),
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-04-06 03:04:35 -07:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let merge_from_account = AccountSharedData::new_data_with_space(
|
|
|
|
stake_lamports,
|
|
|
|
&just_stake(Meta::auto(&authorized_address), stake_lamports),
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-04-06 03:04:35 -07:00
|
|
|
&solana_sdk::pubkey::new_rand(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let transaction_accounts = vec![
|
|
|
|
(stake_address, stake_account),
|
|
|
|
(merge_from_address, merge_from_account),
|
|
|
|
(authorized_address, AccountSharedData::default()),
|
|
|
|
(
|
2023-04-03 08:23:24 -07:00
|
|
|
clock::id(),
|
|
|
|
create_account_shared_data_for_test(&Clock::default()),
|
2022-04-06 03:04:35 -07:00
|
|
|
),
|
|
|
|
(
|
2023-04-03 08:23:24 -07:00
|
|
|
stake_history::id(),
|
|
|
|
create_account_shared_data_for_test(&StakeHistory::default()),
|
2022-04-06 03:04:35 -07:00
|
|
|
),
|
|
|
|
];
|
|
|
|
let instruction_accounts = vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-04-06 03:04:35 -07:00
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: merge_from_address,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-04-06 03:04:35 -07:00
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: clock::id(),
|
2022-04-06 03:04:35 -07:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: stake_history::id(),
|
2022-04-06 03:04:35 -07:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: authorized_address,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-04-06 03:04:35 -07:00
|
|
|
&serialize(&StakeInstruction::Merge).unwrap(),
|
|
|
|
transaction_accounts,
|
|
|
|
instruction_accounts,
|
|
|
|
Err(InstructionError::IncorrectProgramId),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-07-06 14:15:01 -07:00
|
|
|
#[test_case(feature_set_old_behavior(); "old_behavior")]
|
|
|
|
#[test_case(feature_set_new_behavior(); "new_behavior")]
|
2023-04-03 08:23:24 -07:00
|
|
|
fn test_merge_active_stake(feature_set: Arc<FeatureSet>) {
|
2022-04-06 03:04:35 -07:00
|
|
|
let stake_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let merge_from_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let authorized_address = solana_sdk::pubkey::new_rand();
|
|
|
|
let base_lamports = 4242424242;
|
|
|
|
let rent = Rent::default();
|
2022-04-19 10:04:12 -07:00
|
|
|
let rent_exempt_reserve = rent.minimum_balance(StakeState::size_of());
|
2022-04-06 03:04:35 -07:00
|
|
|
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,
|
|
|
|
&StakeState::Stake(meta, stake),
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-04-06 03:04:35 -07:00
|
|
|
&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,
|
|
|
|
&StakeState::Stake(meta, merge_from_stake),
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-04-06 03:04:35 -07:00
|
|
|
&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()),
|
2023-04-03 08:23:24 -07:00
|
|
|
(clock::id(), create_account_shared_data_for_test(&clock)),
|
2022-04-06 03:04:35 -07:00
|
|
|
(
|
2023-04-03 08:23:24 -07:00
|
|
|
stake_history::id(),
|
|
|
|
create_account_shared_data_for_test(&stake_history),
|
2022-04-06 03:04:35 -07:00
|
|
|
),
|
|
|
|
];
|
|
|
|
let instruction_accounts = vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-04-06 03:04:35 -07:00
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: merge_from_address,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-04-06 03:04:35 -07:00
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: clock::id(),
|
2022-04-06 03:04:35 -07:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: stake_history::id(),
|
2022-04-06 03:04:35 -07:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: authorized_address,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
fn try_merge(
|
2023-04-03 08:23:24 -07:00
|
|
|
feature_set: Arc<FeatureSet>,
|
2022-04-06 03:04:35 -07:00
|
|
|
transaction_accounts: Vec<(Pubkey, AccountSharedData)>,
|
|
|
|
mut instruction_accounts: Vec<AccountMeta>,
|
|
|
|
expected_result: Result<(), InstructionError>,
|
|
|
|
) {
|
|
|
|
for iteration in 0..2 {
|
|
|
|
if iteration == 1 {
|
|
|
|
instruction_accounts.swap(0, 1);
|
|
|
|
}
|
2022-05-07 16:01:05 -07:00
|
|
|
let accounts = process_instruction(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-04-06 03:04:35 -07:00
|
|
|
&serialize(&StakeInstruction::Merge).unwrap(),
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
expected_result.clone(),
|
|
|
|
);
|
|
|
|
if expected_result.is_ok() {
|
|
|
|
assert_eq!(
|
|
|
|
accounts[1 - iteration].state(),
|
|
|
|
Ok(StakeState::Uninitialized)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// stake activation epoch, source initialized succeeds
|
|
|
|
try_merge(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-04-06 03:04:35 -07:00
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
|
|
|
|
// both activating fails
|
|
|
|
loop {
|
|
|
|
clock.epoch += 1;
|
|
|
|
if clock.epoch == merge_from_activation_epoch {
|
|
|
|
activating += merge_from_amount;
|
|
|
|
}
|
|
|
|
let delta =
|
|
|
|
activating.min((effective as f64 * stake.delegation.warmup_cooldown_rate) as u64);
|
|
|
|
effective += delta;
|
|
|
|
activating -= delta;
|
|
|
|
stake_history.add(
|
|
|
|
clock.epoch,
|
|
|
|
StakeHistoryEntry {
|
|
|
|
effective,
|
|
|
|
activating,
|
|
|
|
deactivating,
|
|
|
|
},
|
|
|
|
);
|
2023-04-03 08:23:24 -07:00
|
|
|
transaction_accounts[3] = (clock::id(), create_account_shared_data_for_test(&clock));
|
2022-04-06 03:04:35 -07:00
|
|
|
transaction_accounts[4] = (
|
2023-04-03 08:23:24 -07:00
|
|
|
stake_history::id(),
|
|
|
|
create_account_shared_data_for_test(&stake_history),
|
2022-04-06 03:04:35 -07:00
|
|
|
);
|
|
|
|
if stake_amount == stake.stake(clock.epoch, Some(&stake_history))
|
|
|
|
&& merge_from_amount == merge_from_stake.stake(clock.epoch, Some(&stake_history))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
try_merge(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-04-06 03:04:35 -07:00
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Err(InstructionError::from(StakeError::MergeTransientStake)),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Both fully activated works
|
|
|
|
try_merge(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-04-06 03:04:35 -07:00
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
|
|
|
|
// deactivate setup for deactivation
|
|
|
|
let merge_from_deactivation_epoch = clock.epoch + 1;
|
|
|
|
let stake_deactivation_epoch = clock.epoch + 2;
|
|
|
|
|
|
|
|
// active/deactivating and deactivating/inactive mismatches fail
|
|
|
|
loop {
|
|
|
|
clock.epoch += 1;
|
|
|
|
let delta =
|
|
|
|
deactivating.min((effective as f64 * stake.delegation.warmup_cooldown_rate) 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(&StakeState::Stake(meta, stake))
|
|
|
|
.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(&StakeState::Stake(meta, merge_from_stake))
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
stake_history.add(
|
|
|
|
clock.epoch,
|
|
|
|
StakeHistoryEntry {
|
|
|
|
effective,
|
|
|
|
activating,
|
|
|
|
deactivating,
|
|
|
|
},
|
|
|
|
);
|
2023-04-03 08:23:24 -07:00
|
|
|
transaction_accounts[3] = (clock::id(), create_account_shared_data_for_test(&clock));
|
2022-04-06 03:04:35 -07:00
|
|
|
transaction_accounts[4] = (
|
2023-04-03 08:23:24 -07:00
|
|
|
stake_history::id(),
|
|
|
|
create_account_shared_data_for_test(&stake_history),
|
2022-04-06 03:04:35 -07:00
|
|
|
);
|
|
|
|
if 0 == stake.stake(clock.epoch, Some(&stake_history))
|
|
|
|
&& 0 == merge_from_stake.stake(clock.epoch, Some(&stake_history))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
try_merge(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-04-06 03:04:35 -07:00
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
Err(InstructionError::from(StakeError::MergeTransientStake)),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Both fully deactivated works
|
2022-05-07 16:01:05 -07:00
|
|
|
try_merge(
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::clone(&feature_set),
|
2022-05-07 16:01:05 -07:00
|
|
|
transaction_accounts,
|
|
|
|
instruction_accounts,
|
|
|
|
Ok(()),
|
|
|
|
);
|
2022-04-06 03:04:35 -07:00
|
|
|
}
|
2022-04-07 09:54:15 -07:00
|
|
|
|
2022-07-06 14:15:01 -07:00
|
|
|
#[test_case(feature_set_old_behavior(); "old_behavior")]
|
|
|
|
#[test_case(feature_set_new_behavior(); "new_behavior")]
|
2023-04-03 08:23:24 -07:00
|
|
|
fn test_stake_get_minimum_delegation(feature_set: Arc<FeatureSet>) {
|
2022-04-07 09:54:15 -07:00
|
|
|
let stake_address = Pubkey::new_unique();
|
|
|
|
let stake_account = create_default_stake_account();
|
|
|
|
let instruction_data = serialize(&StakeInstruction::GetMinimumDelegation).unwrap();
|
|
|
|
let transaction_accounts = vec![(stake_address, stake_account)];
|
|
|
|
let instruction_accounts = vec![AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: false,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-04-07 09:54:15 -07:00
|
|
|
}];
|
|
|
|
|
|
|
|
mock_process_instruction(
|
|
|
|
&id(),
|
|
|
|
Vec::new(),
|
|
|
|
&instruction_data,
|
|
|
|
transaction_accounts,
|
|
|
|
instruction_accounts,
|
|
|
|
Ok(()),
|
2023-03-06 08:37:37 -08:00
|
|
|
|invoke_context| {
|
|
|
|
super::process_instruction(invoke_context)?;
|
2022-04-07 09:54:15 -07:00
|
|
|
let expected_minimum_delegation =
|
|
|
|
crate::get_minimum_delegation(&invoke_context.feature_set).to_le_bytes();
|
|
|
|
let actual_minimum_delegation =
|
|
|
|
invoke_context.transaction_context.get_return_data().1;
|
|
|
|
assert_eq!(expected_minimum_delegation, actual_minimum_delegation);
|
|
|
|
Ok(())
|
|
|
|
},
|
2023-04-03 08:23:24 -07:00
|
|
|
|invoke_context| {
|
|
|
|
invoke_context.feature_set = Arc::clone(&feature_set);
|
|
|
|
},
|
2022-04-07 09:54:15 -07:00
|
|
|
);
|
|
|
|
}
|
2022-04-11 14:26:36 -07:00
|
|
|
|
|
|
|
// Ensure that the correct errors are returned when processing instructions
|
|
|
|
//
|
|
|
|
// The GetMinimumDelegation instruction does not take any accounts; so when it was added,
|
|
|
|
// `process_instruction()` needed to be updated to *not* need a stake account passed in, which
|
|
|
|
// changes the error *ordering* conditions. These changes shall only occur when the
|
|
|
|
// `add_get_minimum_delegation_instruction_to_stake_program` feature is enabled, and this test
|
|
|
|
// ensures it.
|
|
|
|
//
|
|
|
|
// For the following combinations of the feature enabled/disabled, if the instruction is
|
|
|
|
// valid/invalid, and if a stake account is passed in or not, assert the result:
|
|
|
|
//
|
|
|
|
// feature | instruction | account || result
|
|
|
|
// ---------+-------------+---------++--------
|
|
|
|
// enabled | good | some || Ok
|
|
|
|
// enabled | bad | some || Err InvalidInstructionData
|
|
|
|
// enabled | good | none || Err NotEnoughAccountKeys
|
|
|
|
// enabled | bad | none || Err InvalidInstructionData
|
|
|
|
// disabled | good | some || Ok
|
|
|
|
// disabled | bad | some || Err InvalidInstructionData
|
|
|
|
// disabled | good | none || Err NotEnoughAccountKeys
|
|
|
|
// disabled | bad | none || Err NotEnoughAccountKeys
|
2022-07-06 14:15:01 -07:00
|
|
|
#[test_case(feature_set_old_behavior(); "old_behavior")]
|
|
|
|
#[test_case(feature_set_new_behavior(); "new_behavior")]
|
2023-04-03 08:23:24 -07:00
|
|
|
fn test_stake_process_instruction_error_ordering(feature_set: Arc<FeatureSet>) {
|
2022-04-11 14:26:36 -07:00
|
|
|
let rent = Rent::default();
|
2023-04-03 08:23:24 -07:00
|
|
|
let rent_address = rent::id();
|
|
|
|
let rent_account = create_account_shared_data_for_test(&rent);
|
2022-04-11 14:26:36 -07:00
|
|
|
|
|
|
|
let good_stake_address = Pubkey::new_unique();
|
2022-05-07 16:01:05 -07:00
|
|
|
let good_stake_account = AccountSharedData::new(u64::MAX, StakeState::size_of(), &id());
|
2022-04-11 14:26:36 -07:00
|
|
|
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,
|
2022-06-29 10:29:12 -07:00
|
|
|
is_writable: true,
|
2022-04-11 14:26:36 -07:00
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: rent_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
let good_accounts = (good_transaction_accounts, good_instruction_accounts);
|
|
|
|
|
|
|
|
// The instruction data needs to deserialize to a bogus StakeInstruction. We likely never
|
|
|
|
// will have `usize::MAX`-number of instructions, so this should be a safe constant to
|
|
|
|
// always map to an invalid stake instruction.
|
|
|
|
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 (
|
|
|
|
is_feature_enabled,
|
|
|
|
instruction,
|
|
|
|
(transaction_accounts, instruction_accounts),
|
|
|
|
expected_result,
|
|
|
|
) in [
|
|
|
|
(true, &good_instruction, &good_accounts, Ok(())),
|
|
|
|
(
|
|
|
|
true,
|
|
|
|
&bad_instruction,
|
|
|
|
&good_accounts,
|
|
|
|
Err(InstructionError::InvalidInstructionData),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
true,
|
|
|
|
&good_instruction,
|
|
|
|
&bad_accounts,
|
|
|
|
Err(InstructionError::NotEnoughAccountKeys),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
true,
|
|
|
|
&bad_instruction,
|
|
|
|
&bad_accounts,
|
|
|
|
Err(InstructionError::InvalidInstructionData),
|
|
|
|
),
|
|
|
|
(false, &good_instruction, &good_accounts, Ok(())),
|
|
|
|
(
|
|
|
|
false,
|
|
|
|
&bad_instruction,
|
|
|
|
&good_accounts,
|
|
|
|
Err(InstructionError::InvalidInstructionData),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
false,
|
|
|
|
&good_instruction,
|
|
|
|
&bad_accounts,
|
|
|
|
Err(InstructionError::NotEnoughAccountKeys),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
false,
|
|
|
|
&bad_instruction,
|
|
|
|
&bad_accounts,
|
|
|
|
Err(InstructionError::NotEnoughAccountKeys),
|
|
|
|
),
|
|
|
|
] {
|
2023-04-03 08:23:24 -07:00
|
|
|
let mut feature_set = Arc::new(FeatureSet::clone(&feature_set));
|
2022-04-15 11:43:04 -07:00
|
|
|
if !is_feature_enabled {
|
2023-04-03 08:23:24 -07:00
|
|
|
Arc::get_mut(&mut feature_set).unwrap().deactivate(
|
2022-04-15 11:43:04 -07:00
|
|
|
&feature_set::add_get_minimum_delegation_instruction_to_stake_program::id(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-04-03 08:23:24 -07:00
|
|
|
process_instruction(
|
|
|
|
feature_set,
|
2022-04-11 14:26:36 -07:00
|
|
|
&instruction.data,
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_accounts.clone(),
|
|
|
|
expected_result,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2022-03-25 09:11:51 -07:00
|
|
|
|
2022-07-06 14:15:01 -07:00
|
|
|
#[test_case(feature_set_old_behavior(); "old_behavior")]
|
|
|
|
#[test_case(feature_set_new_behavior(); "new_behavior")]
|
2023-04-03 08:23:24 -07:00
|
|
|
fn test_deactivate_delinquent(feature_set: Arc<FeatureSet>) {
|
2022-03-25 09:11:51 -07:00
|
|
|
let reference_vote_address = Pubkey::new_unique();
|
|
|
|
let vote_address = Pubkey::new_unique();
|
|
|
|
let stake_address = Pubkey::new_unique();
|
|
|
|
|
|
|
|
let initial_stake_state = StakeState::Stake(
|
|
|
|
Meta::default(),
|
|
|
|
new_stake(
|
|
|
|
1, /* stake */
|
|
|
|
&vote_address,
|
|
|
|
&VoteState::default(),
|
|
|
|
1, /* activation_epoch */
|
|
|
|
&stake_config::Config::default(),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
let stake_account = AccountSharedData::new_data_with_space(
|
|
|
|
1, /* lamports */
|
|
|
|
&initial_stake_state,
|
2022-04-19 10:04:12 -07:00
|
|
|
StakeState::size_of(),
|
2022-03-25 09:11:51 -07:00
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let mut vote_account = AccountSharedData::new_data_with_space(
|
|
|
|
1, /* lamports */
|
|
|
|
&VoteStateVersions::new_current(VoteState::default()),
|
|
|
|
VoteState::size_of(),
|
|
|
|
&solana_vote_program::id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let mut reference_vote_account = AccountSharedData::new_data_with_space(
|
|
|
|
1, /* lamports */
|
|
|
|
&VoteStateVersions::new_current(VoteState::default()),
|
|
|
|
VoteState::size_of(),
|
|
|
|
&solana_vote_program::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| {
|
2023-04-03 08:23:24 -07:00
|
|
|
process_instruction(
|
|
|
|
Arc::clone(&feature_set),
|
2022-03-25 09:11:51 -07:00
|
|
|
&serialize(&StakeInstruction::DeactivateDelinquent).unwrap(),
|
|
|
|
vec![
|
|
|
|
(*stake_address, stake_account.clone()),
|
|
|
|
(vote_address, vote_account.clone()),
|
|
|
|
(reference_vote_address, reference_vote_account.clone()),
|
2023-04-03 08:23:24 -07:00
|
|
|
(
|
|
|
|
clock::id(),
|
|
|
|
create_account_shared_data_for_test(&Clock {
|
|
|
|
epoch: current_epoch,
|
|
|
|
..Clock::default()
|
|
|
|
}),
|
|
|
|
),
|
2022-03-25 09:11:51 -07:00
|
|
|
],
|
|
|
|
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,
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
// `reference_vote_account` has not voted. Instruction will fail
|
|
|
|
process_instruction_deactivate_delinquent(
|
|
|
|
&stake_address,
|
|
|
|
&stake_account,
|
|
|
|
&vote_account,
|
|
|
|
&reference_vote_account,
|
|
|
|
Err(StakeError::InsufficientReferenceVotes.into()),
|
|
|
|
);
|
|
|
|
|
|
|
|
// `reference_vote_account` has not consistently voted for at least
|
|
|
|
// `MINIMUM_DELINQUENT_EPOCHS_FOR_DEACTIVATION`.
|
|
|
|
// Instruction will fail
|
|
|
|
let mut reference_vote_state = VoteState::default();
|
|
|
|
for epoch in 0..MINIMUM_DELINQUENT_EPOCHS_FOR_DEACTIVATION / 2 {
|
2022-06-06 16:37:03 -07:00
|
|
|
reference_vote_state.increment_credits(epoch as Epoch, 1);
|
2022-03-25 09:11:51 -07:00
|
|
|
}
|
|
|
|
reference_vote_account
|
|
|
|
.borrow_mut()
|
|
|
|
.serialize_data(&VoteStateVersions::new_current(reference_vote_state))
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
process_instruction_deactivate_delinquent(
|
|
|
|
&stake_address,
|
|
|
|
&stake_account,
|
|
|
|
&vote_account,
|
|
|
|
&reference_vote_account,
|
|
|
|
Err(StakeError::InsufficientReferenceVotes.into()),
|
|
|
|
);
|
|
|
|
|
|
|
|
// `reference_vote_account` has not consistently voted for the last
|
|
|
|
// `MINIMUM_DELINQUENT_EPOCHS_FOR_DEACTIVATION`.
|
|
|
|
// Instruction will fail
|
|
|
|
let mut reference_vote_state = VoteState::default();
|
|
|
|
for epoch in 0..=current_epoch {
|
2022-06-06 16:37:03 -07:00
|
|
|
reference_vote_state.increment_credits(epoch, 1);
|
2022-03-25 09:11:51 -07:00
|
|
|
}
|
|
|
|
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
|
|
|
|
.borrow_mut()
|
|
|
|
.serialize_data(&VoteStateVersions::new_current(reference_vote_state))
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
process_instruction_deactivate_delinquent(
|
|
|
|
&stake_address,
|
|
|
|
&stake_account,
|
|
|
|
&vote_account,
|
|
|
|
&reference_vote_account,
|
|
|
|
Err(StakeError::InsufficientReferenceVotes.into()),
|
|
|
|
);
|
|
|
|
|
|
|
|
// `reference_vote_account` has consistently voted and `vote_account` has never voted.
|
|
|
|
// Instruction will succeed
|
|
|
|
let mut reference_vote_state = VoteState::default();
|
|
|
|
for epoch in 0..=current_epoch {
|
2022-06-06 16:37:03 -07:00
|
|
|
reference_vote_state.increment_credits(epoch, 1);
|
2022-03-25 09:11:51 -07:00
|
|
|
}
|
|
|
|
reference_vote_account
|
|
|
|
.borrow_mut()
|
|
|
|
.serialize_data(&VoteStateVersions::new_current(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
|
|
|
|
);
|
|
|
|
|
|
|
|
// `reference_vote_account` has consistently voted and `vote_account` has not voted for the
|
|
|
|
// last `MINIMUM_DELINQUENT_EPOCHS_FOR_DEACTIVATION`.
|
|
|
|
// Instruction will succeed
|
|
|
|
|
|
|
|
let mut vote_state = VoteState::default();
|
|
|
|
for epoch in 0..MINIMUM_DELINQUENT_EPOCHS_FOR_DEACTIVATION / 2 {
|
2022-06-06 16:37:03 -07:00
|
|
|
vote_state.increment_credits(epoch as Epoch, 1);
|
2022-03-25 09:11:51 -07:00
|
|
|
}
|
|
|
|
vote_account
|
|
|
|
.serialize_data(&VoteStateVersions::new_current(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
|
|
|
|
);
|
|
|
|
|
|
|
|
// `reference_vote_account` has consistently voted and `vote_account` has not voted for the
|
|
|
|
// last `MINIMUM_DELINQUENT_EPOCHS_FOR_DEACTIVATION`. Try to deactivate an unrelated stake
|
|
|
|
// account. Instruction will fail
|
|
|
|
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(&StakeState::Stake(
|
|
|
|
Meta::default(),
|
|
|
|
new_stake(
|
|
|
|
1, /* stake */
|
|
|
|
&unrelated_vote_address,
|
|
|
|
&VoteState::default(),
|
|
|
|
1, /* activation_epoch */
|
|
|
|
&stake_config::Config::default(),
|
|
|
|
),
|
|
|
|
))
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
process_instruction_deactivate_delinquent(
|
|
|
|
&unrelated_stake_address,
|
|
|
|
&unrelated_stake_account,
|
|
|
|
&vote_account,
|
|
|
|
&reference_vote_account,
|
|
|
|
Err(StakeError::VoteAddressMismatch.into()),
|
|
|
|
);
|
|
|
|
|
|
|
|
// `reference_vote_account` has consistently voted and `vote_account` voted once
|
|
|
|
// `MINIMUM_DELINQUENT_EPOCHS_FOR_DEACTIVATION` ago.
|
|
|
|
// Instruction will succeed
|
|
|
|
let mut vote_state = VoteState::default();
|
2022-06-06 16:37:03 -07:00
|
|
|
vote_state.increment_credits(
|
|
|
|
current_epoch - MINIMUM_DELINQUENT_EPOCHS_FOR_DEACTIVATION as Epoch,
|
|
|
|
1,
|
|
|
|
);
|
2022-03-25 09:11:51 -07:00
|
|
|
vote_account
|
|
|
|
.serialize_data(&VoteStateVersions::new_current(vote_state))
|
|
|
|
.unwrap();
|
|
|
|
process_instruction_deactivate_delinquent(
|
|
|
|
&stake_address,
|
|
|
|
&stake_account,
|
|
|
|
&vote_account,
|
|
|
|
&reference_vote_account,
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
|
|
|
|
// `reference_vote_account` has consistently voted and `vote_account` voted once
|
|
|
|
// `MINIMUM_DELINQUENT_EPOCHS_FOR_DEACTIVATION` - 1 epochs ago
|
|
|
|
// Instruction will fail
|
|
|
|
let mut vote_state = VoteState::default();
|
|
|
|
vote_state.increment_credits(
|
|
|
|
current_epoch - (MINIMUM_DELINQUENT_EPOCHS_FOR_DEACTIVATION - 1) as Epoch,
|
2022-06-06 16:37:03 -07:00
|
|
|
1,
|
2022-03-25 09:11:51 -07:00
|
|
|
);
|
|
|
|
vote_account
|
|
|
|
.serialize_data(&VoteStateVersions::new_current(vote_state))
|
|
|
|
.unwrap();
|
|
|
|
process_instruction_deactivate_delinquent(
|
|
|
|
&stake_address,
|
|
|
|
&stake_account,
|
|
|
|
&vote_account,
|
|
|
|
&reference_vote_account,
|
|
|
|
Err(StakeError::MinimumDelinquentEpochsForDeactivationNotMet.into()),
|
|
|
|
);
|
|
|
|
}
|
2022-06-28 10:39:00 -07:00
|
|
|
|
|
|
|
#[test_case(feature_set_old_behavior(); "old_behavior")]
|
|
|
|
#[test_case(feature_set_new_behavior(); "new_behavior")]
|
2023-04-03 08:23:24 -07:00
|
|
|
fn test_redelegate(feature_set: Arc<FeatureSet>) {
|
2022-06-28 10:39:00 -07:00
|
|
|
let feature_set = Arc::new(feature_set);
|
|
|
|
|
|
|
|
let minimum_delegation = crate::get_minimum_delegation(&feature_set);
|
|
|
|
let rent = Rent::default();
|
|
|
|
let rent_exempt_reserve = rent.minimum_balance(StakeState::size_of());
|
|
|
|
let stake_history = StakeHistory::default();
|
|
|
|
let current_epoch = 100;
|
|
|
|
|
|
|
|
let mut sysvar_cache_override = SysvarCache::default();
|
|
|
|
sysvar_cache_override.set_stake_history(stake_history.clone());
|
|
|
|
sysvar_cache_override.set_rent(rent);
|
|
|
|
sysvar_cache_override.set_clock(Clock {
|
|
|
|
epoch: current_epoch,
|
|
|
|
..Clock::default()
|
|
|
|
});
|
|
|
|
|
|
|
|
let authorized_staker = Pubkey::new_unique();
|
|
|
|
let vote_address = Pubkey::new_unique();
|
|
|
|
let new_vote_address = Pubkey::new_unique();
|
|
|
|
let stake_address = Pubkey::new_unique();
|
|
|
|
let uninitialized_stake_address = Pubkey::new_unique();
|
|
|
|
|
|
|
|
let prepare_stake_account = |activation_epoch, expected_stake_activation_status| {
|
|
|
|
let initial_stake_delegation = minimum_delegation + rent_exempt_reserve;
|
|
|
|
let initial_stake_state = StakeState::Stake(
|
|
|
|
Meta {
|
|
|
|
authorized: Authorized {
|
|
|
|
staker: authorized_staker,
|
|
|
|
withdrawer: Pubkey::new_unique(),
|
|
|
|
},
|
|
|
|
rent_exempt_reserve,
|
|
|
|
..Meta::default()
|
|
|
|
},
|
|
|
|
new_stake(
|
|
|
|
initial_stake_delegation,
|
|
|
|
&vote_address,
|
|
|
|
&VoteState::default(),
|
|
|
|
activation_epoch,
|
|
|
|
&stake_config::Config::default(),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
if let Some(expected_stake_activation_status) = expected_stake_activation_status {
|
|
|
|
assert_eq!(
|
|
|
|
expected_stake_activation_status,
|
|
|
|
initial_stake_state
|
|
|
|
.delegation()
|
|
|
|
.unwrap()
|
|
|
|
.stake_activating_and_deactivating(current_epoch, Some(&stake_history))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
AccountSharedData::new_data_with_space(
|
|
|
|
rent_exempt_reserve + initial_stake_delegation, /* lamports */
|
|
|
|
&initial_stake_state,
|
|
|
|
StakeState::size_of(),
|
|
|
|
&id(),
|
|
|
|
)
|
|
|
|
.unwrap()
|
|
|
|
};
|
|
|
|
|
|
|
|
let new_vote_account = AccountSharedData::new_data_with_space(
|
|
|
|
1, /* lamports */
|
|
|
|
&VoteStateVersions::new_current(VoteState::default()),
|
|
|
|
VoteState::size_of(),
|
|
|
|
&solana_vote_program::id(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let process_instruction_redelegate =
|
|
|
|
|stake_address: &Pubkey,
|
|
|
|
stake_account: &AccountSharedData,
|
|
|
|
authorized_staker: &Pubkey,
|
|
|
|
vote_address: &Pubkey,
|
|
|
|
vote_account: &AccountSharedData,
|
|
|
|
uninitialized_stake_address: &Pubkey,
|
|
|
|
uninitialized_stake_account: &AccountSharedData,
|
|
|
|
expected_result| {
|
2023-04-03 08:23:24 -07:00
|
|
|
process_instruction(
|
|
|
|
Arc::clone(&feature_set),
|
2022-06-28 10:39:00 -07:00
|
|
|
&serialize(&StakeInstruction::Redelegate).unwrap(),
|
|
|
|
vec![
|
|
|
|
(*stake_address, stake_account.clone()),
|
|
|
|
(
|
|
|
|
*uninitialized_stake_address,
|
|
|
|
uninitialized_stake_account.clone(),
|
|
|
|
),
|
|
|
|
(*vote_address, vote_account.clone()),
|
2023-04-03 08:23:24 -07:00
|
|
|
(*authorized_staker, AccountSharedData::default()),
|
2022-06-28 10:39:00 -07:00
|
|
|
(
|
|
|
|
stake_config::id(),
|
|
|
|
config::create_account(0, &stake_config::Config::default()),
|
|
|
|
),
|
2023-04-03 08:23:24 -07:00
|
|
|
(
|
|
|
|
stake_history::id(),
|
|
|
|
create_account_shared_data_for_test(&stake_history),
|
|
|
|
),
|
|
|
|
(rent::id(), create_account_shared_data_for_test(&rent)),
|
|
|
|
(
|
|
|
|
clock::id(),
|
|
|
|
create_account_shared_data_for_test(&Clock {
|
|
|
|
epoch: current_epoch,
|
|
|
|
..Clock::default()
|
|
|
|
}),
|
|
|
|
),
|
2022-06-28 10:39:00 -07:00
|
|
|
],
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: *stake_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: true,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: *uninitialized_stake_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: true,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: *vote_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_config::id(),
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: *authorized_staker,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
expected_result,
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// Failure: incorrect authorized staker
|
|
|
|
//
|
|
|
|
let stake_account = prepare_stake_account(0 /*activation_epoch*/, None);
|
|
|
|
let uninitialized_stake_account =
|
|
|
|
AccountSharedData::new(0 /* lamports */, StakeState::size_of(), &id());
|
|
|
|
|
|
|
|
let _ = process_instruction_redelegate(
|
|
|
|
&stake_address,
|
|
|
|
&stake_account,
|
|
|
|
&Pubkey::new_unique(), // <-- Incorrect authorized staker
|
|
|
|
&new_vote_address,
|
|
|
|
&new_vote_account,
|
|
|
|
&uninitialized_stake_address,
|
|
|
|
&uninitialized_stake_account,
|
|
|
|
Err(InstructionError::MissingRequiredSignature),
|
|
|
|
);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Success: normal case
|
|
|
|
//
|
|
|
|
let output_accounts = process_instruction_redelegate(
|
|
|
|
&stake_address,
|
|
|
|
&stake_account,
|
|
|
|
&authorized_staker,
|
|
|
|
&new_vote_address,
|
|
|
|
&new_vote_account,
|
|
|
|
&uninitialized_stake_address,
|
|
|
|
&uninitialized_stake_account,
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(output_accounts[0].lamports(), rent_exempt_reserve);
|
|
|
|
if let StakeState::Stake(meta, stake) =
|
|
|
|
output_accounts[0].borrow().deserialize_data().unwrap()
|
|
|
|
{
|
|
|
|
assert_eq!(meta.rent_exempt_reserve, rent_exempt_reserve);
|
|
|
|
assert_eq!(
|
|
|
|
stake.delegation.stake,
|
|
|
|
minimum_delegation + rent_exempt_reserve
|
|
|
|
);
|
|
|
|
assert_eq!(stake.delegation.activation_epoch, 0);
|
|
|
|
assert_eq!(stake.delegation.deactivation_epoch, current_epoch);
|
|
|
|
} else {
|
|
|
|
panic!("Invalid output_accounts[0] data");
|
|
|
|
}
|
|
|
|
assert_eq!(
|
|
|
|
output_accounts[1].lamports(),
|
|
|
|
minimum_delegation + rent_exempt_reserve
|
|
|
|
);
|
|
|
|
if let StakeState::Stake(meta, stake) =
|
|
|
|
output_accounts[1].borrow().deserialize_data().unwrap()
|
|
|
|
{
|
|
|
|
assert_eq!(meta.rent_exempt_reserve, rent_exempt_reserve);
|
|
|
|
assert_eq!(stake.delegation.stake, minimum_delegation);
|
|
|
|
assert_eq!(stake.delegation.activation_epoch, current_epoch);
|
|
|
|
assert_eq!(stake.delegation.deactivation_epoch, u64::MAX);
|
|
|
|
} else {
|
|
|
|
panic!("Invalid output_accounts[1] data");
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Variations of rescinding the deactivation of `stake_account`
|
|
|
|
//
|
|
|
|
let deactivated_stake_accounts = [
|
|
|
|
(
|
|
|
|
// Failure: insufficient stake in `stake_account` to even delegate normally
|
|
|
|
{
|
|
|
|
let mut deactivated_stake_account = output_accounts[0].clone();
|
|
|
|
deactivated_stake_account
|
|
|
|
.checked_add_lamports(minimum_delegation - 1)
|
|
|
|
.unwrap();
|
|
|
|
deactivated_stake_account
|
|
|
|
},
|
|
|
|
Err(StakeError::InsufficientDelegation.into()),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
// Failure: `stake_account` holds the "virtual stake" that's cooling now, with the
|
|
|
|
// real stake now warming up in `uninitialized_stake_account`
|
|
|
|
{
|
|
|
|
let mut deactivated_stake_account = output_accounts[0].clone();
|
|
|
|
deactivated_stake_account
|
|
|
|
.checked_add_lamports(minimum_delegation)
|
|
|
|
.unwrap();
|
|
|
|
deactivated_stake_account
|
|
|
|
},
|
|
|
|
Err(StakeError::TooSoonToRedelegate.into()),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
// Success: `stake_account` has been replenished with additional lamports to
|
|
|
|
// fully realize its "virtual stake"
|
|
|
|
{
|
|
|
|
let mut deactivated_stake_account = output_accounts[0].clone();
|
|
|
|
deactivated_stake_account
|
|
|
|
.checked_add_lamports(minimum_delegation + rent_exempt_reserve)
|
|
|
|
.unwrap();
|
|
|
|
deactivated_stake_account
|
|
|
|
},
|
|
|
|
Ok(()),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
// Failure: `stake_account` has been replenished with 1 lamport less than what's
|
|
|
|
// necessary to fully realize its "virtual stake"
|
|
|
|
{
|
|
|
|
let mut deactivated_stake_account = output_accounts[0].clone();
|
|
|
|
deactivated_stake_account
|
|
|
|
.checked_add_lamports(minimum_delegation + rent_exempt_reserve - 1)
|
|
|
|
.unwrap();
|
|
|
|
deactivated_stake_account
|
|
|
|
},
|
|
|
|
Err(StakeError::TooSoonToRedelegate.into()),
|
|
|
|
),
|
|
|
|
];
|
|
|
|
for (deactivated_stake_account, expected_result) in deactivated_stake_accounts {
|
2023-04-03 08:23:24 -07:00
|
|
|
process_instruction(
|
|
|
|
Arc::clone(&feature_set),
|
2022-06-28 10:39:00 -07:00
|
|
|
&serialize(&StakeInstruction::DelegateStake).unwrap(),
|
|
|
|
vec![
|
|
|
|
(stake_address, deactivated_stake_account),
|
|
|
|
(vote_address, new_vote_account.clone()),
|
2023-04-03 08:23:24 -07:00
|
|
|
(authorized_staker, AccountSharedData::default()),
|
2022-06-28 10:39:00 -07:00
|
|
|
(
|
2023-04-03 08:23:24 -07:00
|
|
|
stake_config::id(),
|
|
|
|
config::create_account(0, &stake_config::Config::default()),
|
2022-06-28 10:39:00 -07:00
|
|
|
),
|
|
|
|
(
|
2023-04-03 08:23:24 -07:00
|
|
|
stake_history::id(),
|
|
|
|
create_account_shared_data_for_test(&stake_history),
|
2022-06-28 10:39:00 -07:00
|
|
|
),
|
2023-04-03 08:23:24 -07:00
|
|
|
(rent::id(), create_account_shared_data_for_test(&rent)),
|
2022-06-28 10:39:00 -07:00
|
|
|
(
|
2023-04-03 08:23:24 -07:00
|
|
|
clock::id(),
|
|
|
|
create_account_shared_data_for_test(&Clock {
|
|
|
|
epoch: current_epoch,
|
|
|
|
..Clock::default()
|
|
|
|
}),
|
2022-06-28 10:39:00 -07:00
|
|
|
),
|
|
|
|
],
|
|
|
|
vec![
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: true,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: vote_address,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: clock::id(),
|
2022-06-28 10:39:00 -07:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
2023-04-03 08:23:24 -07:00
|
|
|
pubkey: stake_history::id(),
|
2022-06-28 10:39:00 -07:00
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: stake_config::id(),
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: authorized_staker,
|
|
|
|
is_signer: true,
|
|
|
|
is_writable: false,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
expected_result,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Success: `uninitialized_stake_account` starts with 42 extra lamports
|
|
|
|
//
|
|
|
|
let uninitialized_stake_account_with_extra_lamports =
|
|
|
|
AccountSharedData::new(42 /* lamports */, StakeState::size_of(), &id());
|
|
|
|
let output_accounts = process_instruction_redelegate(
|
|
|
|
&stake_address,
|
|
|
|
&stake_account,
|
|
|
|
&authorized_staker,
|
|
|
|
&new_vote_address,
|
|
|
|
&new_vote_account,
|
|
|
|
&uninitialized_stake_address,
|
|
|
|
&uninitialized_stake_account_with_extra_lamports,
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(output_accounts[0].lamports(), rent_exempt_reserve);
|
|
|
|
assert_eq!(
|
|
|
|
output_accounts[1].lamports(),
|
|
|
|
minimum_delegation + rent_exempt_reserve + 42
|
|
|
|
);
|
|
|
|
if let StakeState::Stake(meta, stake) =
|
|
|
|
output_accounts[1].borrow().deserialize_data().unwrap()
|
|
|
|
{
|
|
|
|
assert_eq!(meta.rent_exempt_reserve, rent_exempt_reserve);
|
|
|
|
assert_eq!(stake.delegation.stake, minimum_delegation + 42);
|
|
|
|
assert_eq!(stake.delegation.activation_epoch, current_epoch);
|
|
|
|
assert_eq!(stake.delegation.deactivation_epoch, u64::MAX);
|
|
|
|
} else {
|
|
|
|
panic!("Invalid output_accounts[1] data");
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Success: `stake_account` is over-allocated and holds a greater than required `rent_exempt_reserve`
|
|
|
|
//
|
|
|
|
let mut stake_account_over_allocated =
|
|
|
|
prepare_stake_account(0 /*activation_epoch:*/, None);
|
|
|
|
if let StakeState::Stake(mut meta, stake) = stake_account_over_allocated
|
|
|
|
.borrow_mut()
|
|
|
|
.deserialize_data()
|
|
|
|
.unwrap()
|
|
|
|
{
|
|
|
|
meta.rent_exempt_reserve += 42;
|
|
|
|
stake_account_over_allocated
|
|
|
|
.set_state(&StakeState::Stake(meta, stake))
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
stake_account_over_allocated
|
|
|
|
.checked_add_lamports(42)
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
stake_account_over_allocated.lamports(),
|
|
|
|
(minimum_delegation + rent_exempt_reserve) + (rent_exempt_reserve + 42),
|
|
|
|
);
|
|
|
|
assert_eq!(uninitialized_stake_account.lamports(), 0);
|
|
|
|
let output_accounts = process_instruction_redelegate(
|
|
|
|
&stake_address,
|
|
|
|
&stake_account_over_allocated,
|
|
|
|
&authorized_staker,
|
|
|
|
&new_vote_address,
|
|
|
|
&new_vote_account,
|
|
|
|
&uninitialized_stake_address,
|
|
|
|
&uninitialized_stake_account,
|
|
|
|
Ok(()),
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(output_accounts[0].lamports(), rent_exempt_reserve + 42);
|
|
|
|
if let StakeState::Stake(meta, _stake) =
|
|
|
|
output_accounts[0].borrow().deserialize_data().unwrap()
|
|
|
|
{
|
|
|
|
assert_eq!(meta.rent_exempt_reserve, rent_exempt_reserve + 42);
|
|
|
|
} else {
|
|
|
|
panic!("Invalid output_accounts[0] data");
|
|
|
|
}
|
|
|
|
assert_eq!(
|
|
|
|
output_accounts[1].lamports(),
|
|
|
|
minimum_delegation + rent_exempt_reserve,
|
|
|
|
);
|
|
|
|
if let StakeState::Stake(meta, stake) =
|
|
|
|
output_accounts[1].borrow().deserialize_data().unwrap()
|
|
|
|
{
|
|
|
|
assert_eq!(meta.rent_exempt_reserve, rent_exempt_reserve);
|
|
|
|
assert_eq!(stake.delegation.stake, minimum_delegation);
|
|
|
|
} else {
|
|
|
|
panic!("Invalid output_accounts[1] data");
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Failure: `uninitialized_stake_account` with invalid program id
|
|
|
|
//
|
|
|
|
let _ = process_instruction_redelegate(
|
|
|
|
&stake_address,
|
|
|
|
&stake_account,
|
|
|
|
&authorized_staker,
|
|
|
|
&new_vote_address,
|
|
|
|
&new_vote_account,
|
|
|
|
&uninitialized_stake_address,
|
|
|
|
&AccountSharedData::new(
|
|
|
|
0, /* lamports */
|
|
|
|
StakeState::size_of(),
|
|
|
|
&Pubkey::new_unique(), // <-- Invalid program id
|
|
|
|
),
|
|
|
|
Err(InstructionError::IncorrectProgramId),
|
|
|
|
);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Failure: `uninitialized_stake_account` with size too small
|
|
|
|
//
|
|
|
|
let _ = process_instruction_redelegate(
|
|
|
|
&stake_address,
|
|
|
|
&stake_account,
|
|
|
|
&authorized_staker,
|
|
|
|
&new_vote_address,
|
|
|
|
&new_vote_account,
|
|
|
|
&uninitialized_stake_address,
|
|
|
|
&AccountSharedData::new(0 /* lamports */, StakeState::size_of() - 1, &id()), // <-- size too small
|
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Failure: `uninitialized_stake_account` with size too large
|
|
|
|
//
|
|
|
|
let _ = process_instruction_redelegate(
|
|
|
|
&stake_address,
|
|
|
|
&stake_account,
|
|
|
|
&authorized_staker,
|
|
|
|
&new_vote_address,
|
|
|
|
&new_vote_account,
|
|
|
|
&uninitialized_stake_address,
|
|
|
|
&AccountSharedData::new(0 /* lamports */, StakeState::size_of() + 1, &id()), // <-- size too large
|
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Failure: `uninitialized_stake_account` with initialized stake account
|
|
|
|
//
|
|
|
|
let _ = process_instruction_redelegate(
|
|
|
|
&stake_address,
|
|
|
|
&stake_account,
|
|
|
|
&authorized_staker,
|
|
|
|
&new_vote_address,
|
|
|
|
&new_vote_account,
|
|
|
|
&uninitialized_stake_address,
|
|
|
|
&stake_account.clone(), // <-- Initialized stake account
|
|
|
|
Err(InstructionError::AccountAlreadyInitialized),
|
|
|
|
);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Failure: invalid `new_vote_account`
|
|
|
|
//
|
|
|
|
let _ = process_instruction_redelegate(
|
|
|
|
&stake_address,
|
|
|
|
&stake_account,
|
|
|
|
&authorized_staker,
|
|
|
|
&new_vote_address,
|
2023-01-05 10:42:19 -08:00
|
|
|
&uninitialized_stake_account, // <-- Invalid vote account
|
2022-06-28 10:39:00 -07:00
|
|
|
&uninitialized_stake_address,
|
|
|
|
&uninitialized_stake_account,
|
|
|
|
Err(InstructionError::IncorrectProgramId),
|
|
|
|
);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Failure: invalid `stake_account`
|
|
|
|
//
|
|
|
|
let _ = process_instruction_redelegate(
|
|
|
|
&stake_address,
|
2023-01-05 10:42:19 -08:00
|
|
|
&uninitialized_stake_account, // <-- Uninitialized stake account
|
2022-06-28 10:39:00 -07:00
|
|
|
&authorized_staker,
|
|
|
|
&new_vote_address,
|
|
|
|
&new_vote_account,
|
|
|
|
&uninitialized_stake_address,
|
|
|
|
&uninitialized_stake_account,
|
|
|
|
Err(InstructionError::InvalidAccountData),
|
|
|
|
);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Failure: stake is inactive, activating or deactivating
|
|
|
|
//
|
|
|
|
let inactive_stake_account = prepare_stake_account(
|
|
|
|
current_epoch + 1, /*activation_epoch*/
|
|
|
|
Some(StakeActivationStatus {
|
|
|
|
effective: 0,
|
|
|
|
activating: 0,
|
|
|
|
deactivating: 0,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
let _ = process_instruction_redelegate(
|
|
|
|
&stake_address,
|
|
|
|
&inactive_stake_account,
|
|
|
|
&authorized_staker,
|
|
|
|
&new_vote_address,
|
|
|
|
&new_vote_account,
|
|
|
|
&uninitialized_stake_address,
|
|
|
|
&uninitialized_stake_account,
|
|
|
|
Err(StakeError::RedelegateTransientOrInactiveStake.into()),
|
|
|
|
);
|
|
|
|
|
|
|
|
let activating_stake_account = prepare_stake_account(
|
|
|
|
current_epoch, /*activation_epoch*/
|
|
|
|
Some(StakeActivationStatus {
|
|
|
|
effective: 0,
|
|
|
|
activating: minimum_delegation + rent_exempt_reserve,
|
|
|
|
deactivating: 0,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
let _ = process_instruction_redelegate(
|
|
|
|
&stake_address,
|
|
|
|
&activating_stake_account,
|
|
|
|
&authorized_staker,
|
|
|
|
&new_vote_address,
|
|
|
|
&new_vote_account,
|
|
|
|
&uninitialized_stake_address,
|
|
|
|
&uninitialized_stake_account,
|
|
|
|
Err(StakeError::RedelegateTransientOrInactiveStake.into()),
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut deactivating_stake_account =
|
|
|
|
prepare_stake_account(0 /*activation_epoch:*/, None);
|
|
|
|
if let StakeState::Stake(meta, mut stake) = deactivating_stake_account
|
|
|
|
.borrow_mut()
|
|
|
|
.deserialize_data()
|
|
|
|
.unwrap()
|
|
|
|
{
|
|
|
|
stake.deactivate(current_epoch).unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
StakeActivationStatus {
|
|
|
|
effective: minimum_delegation + rent_exempt_reserve,
|
|
|
|
activating: 0,
|
|
|
|
deactivating: minimum_delegation + rent_exempt_reserve,
|
|
|
|
},
|
|
|
|
stake
|
|
|
|
.delegation
|
|
|
|
.stake_activating_and_deactivating(current_epoch, Some(&stake_history))
|
|
|
|
);
|
|
|
|
|
|
|
|
deactivating_stake_account
|
|
|
|
.set_state(&StakeState::Stake(meta, stake))
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
let _ = process_instruction_redelegate(
|
|
|
|
&stake_address,
|
|
|
|
&deactivating_stake_account,
|
|
|
|
&authorized_staker,
|
|
|
|
&new_vote_address,
|
|
|
|
&new_vote_account,
|
|
|
|
&uninitialized_stake_address,
|
|
|
|
&uninitialized_stake_account,
|
|
|
|
Err(StakeError::RedelegateTransientOrInactiveStake.into()),
|
|
|
|
);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Failure: `stake_account` has insufficient stake
|
|
|
|
// (less than `minimum_delegation + rent_exempt_reserve`)
|
|
|
|
//
|
|
|
|
let mut stake_account_too_few_lamports = stake_account.clone();
|
|
|
|
if let StakeState::Stake(meta, mut stake) = stake_account_too_few_lamports
|
|
|
|
.borrow_mut()
|
|
|
|
.deserialize_data()
|
|
|
|
.unwrap()
|
|
|
|
{
|
|
|
|
stake.delegation.stake -= 1;
|
|
|
|
assert_eq!(
|
|
|
|
stake.delegation.stake,
|
|
|
|
minimum_delegation + rent_exempt_reserve - 1
|
|
|
|
);
|
|
|
|
stake_account_too_few_lamports
|
|
|
|
.set_state(&StakeState::Stake(meta, stake))
|
|
|
|
.unwrap();
|
|
|
|
} else {
|
|
|
|
panic!("Invalid stake_account");
|
|
|
|
}
|
|
|
|
stake_account_too_few_lamports
|
|
|
|
.checked_sub_lamports(1)
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
stake_account_too_few_lamports.lamports(),
|
|
|
|
minimum_delegation + 2 * rent_exempt_reserve - 1
|
|
|
|
);
|
|
|
|
|
|
|
|
let _ = process_instruction_redelegate(
|
|
|
|
&stake_address,
|
|
|
|
&stake_account_too_few_lamports,
|
|
|
|
&authorized_staker,
|
|
|
|
&new_vote_address,
|
|
|
|
&new_vote_account,
|
|
|
|
&uninitialized_stake_address,
|
|
|
|
&uninitialized_stake_account,
|
|
|
|
Err(StakeError::InsufficientDelegation.into()),
|
|
|
|
);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Failure: redelegate to same vote addresss
|
|
|
|
//
|
|
|
|
let _ = process_instruction_redelegate(
|
|
|
|
&stake_address,
|
|
|
|
&stake_account,
|
|
|
|
&authorized_staker,
|
|
|
|
&vote_address, // <-- Same vote address
|
|
|
|
&new_vote_account,
|
|
|
|
&uninitialized_stake_address,
|
|
|
|
&uninitialized_stake_account,
|
|
|
|
Err(StakeError::RedelegateToSameVoteAccount.into()),
|
|
|
|
);
|
|
|
|
}
|
2019-04-01 16:45:53 -07:00
|
|
|
}
|