Add support for stake::split() via create_account_with_seed() (#7879)
* Add split with seed * move to new system_program APIs * de-replicode
This commit is contained in:
@ -128,7 +128,7 @@ pub enum StakeInstruction {
|
||||
Deactivate,
|
||||
}
|
||||
|
||||
pub fn initialize(stake_pubkey: &Pubkey, authorized: &Authorized, lockup: &Lockup) -> Instruction {
|
||||
fn initialize(stake_pubkey: &Pubkey, authorized: &Authorized, lockup: &Lockup) -> Instruction {
|
||||
Instruction::new(
|
||||
id(),
|
||||
&StakeInstruction::Initialize(*authorized, *lockup),
|
||||
@ -181,6 +181,21 @@ pub fn create_account(
|
||||
]
|
||||
}
|
||||
|
||||
fn _split(
|
||||
stake_pubkey: &Pubkey,
|
||||
authorized_pubkey: &Pubkey,
|
||||
lamports: u64,
|
||||
split_stake_pubkey: &Pubkey,
|
||||
) -> Instruction {
|
||||
let account_metas = vec![
|
||||
AccountMeta::new(*stake_pubkey, false),
|
||||
AccountMeta::new(*split_stake_pubkey, false),
|
||||
]
|
||||
.with_signer(authorized_pubkey);
|
||||
|
||||
Instruction::new(id(), &StakeInstruction::Split(lamports), account_metas)
|
||||
}
|
||||
|
||||
pub fn split(
|
||||
stake_pubkey: &Pubkey,
|
||||
authorized_pubkey: &Pubkey,
|
||||
@ -188,21 +203,39 @@ pub fn split(
|
||||
split_stake_pubkey: &Pubkey,
|
||||
) -> Vec<Instruction> {
|
||||
vec![
|
||||
system_instruction::create_account(
|
||||
system_instruction::allocate(split_stake_pubkey, std::mem::size_of::<StakeState>() as u64),
|
||||
system_instruction::assign(split_stake_pubkey, &id()),
|
||||
_split(
|
||||
stake_pubkey,
|
||||
authorized_pubkey,
|
||||
lamports,
|
||||
split_stake_pubkey,
|
||||
0, // creates an ephemeral, uninitialized Stake
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
pub fn split_with_seed(
|
||||
stake_pubkey: &Pubkey,
|
||||
authorized_pubkey: &Pubkey,
|
||||
lamports: u64,
|
||||
split_stake_pubkey: &Pubkey, // derived using create_address_with_seed()
|
||||
base: &Pubkey, // base
|
||||
seed: &str, // seed
|
||||
) -> Vec<Instruction> {
|
||||
vec![
|
||||
system_instruction::allocate_with_seed(
|
||||
split_stake_pubkey,
|
||||
base,
|
||||
seed,
|
||||
std::mem::size_of::<StakeState>() as u64,
|
||||
&id(),
|
||||
),
|
||||
{
|
||||
let account_metas = vec![
|
||||
AccountMeta::new(*stake_pubkey, false),
|
||||
AccountMeta::new(*split_stake_pubkey, false),
|
||||
]
|
||||
.with_signer(authorized_pubkey);
|
||||
Instruction::new(id(), &StakeInstruction::Split(lamports), account_metas)
|
||||
},
|
||||
_split(
|
||||
stake_pubkey,
|
||||
authorized_pubkey,
|
||||
lamports,
|
||||
split_stake_pubkey,
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
@ -478,6 +511,19 @@ mod tests {
|
||||
&Pubkey::default(),
|
||||
100,
|
||||
&Pubkey::default()
|
||||
)[2]
|
||||
),
|
||||
Err(InstructionError::InvalidAccountData),
|
||||
);
|
||||
assert_eq!(
|
||||
process_instruction(
|
||||
&split_with_seed(
|
||||
&Pubkey::default(),
|
||||
&Pubkey::default(),
|
||||
100,
|
||||
&Pubkey::default(),
|
||||
&Pubkey::default(),
|
||||
"seed"
|
||||
)[1]
|
||||
),
|
||||
Err(InstructionError::InvalidAccountData),
|
||||
|
@ -2779,6 +2779,18 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
#[should_panic]
|
||||
fn test_dbg_stake_minimum_balance() {
|
||||
let minimum_balance = Rent::default().minimum_balance(std::mem::size_of::<StakeState>());
|
||||
panic!(
|
||||
"stake minimum_balance: {} lamports, {} SOL",
|
||||
minimum_balance,
|
||||
minimum_balance as f64 / solana_sdk::native_token::LAMPORTS_PER_SOL as f64
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_authorize_delegated_stake() {
|
||||
let stake_pubkey = Pubkey::new_rand();
|
||||
|
Reference in New Issue
Block a user