split wallet staking commands (#6168)

* split wallet staking commands

* elide real home

* unit->UNIT for usage

* unit->UNIT, don't try to run SUBCOMMANDS: ;)

* more fixup

* fixups

* actually check

* shellcheck

* preserve #6158 after rebase

* fixup

* test

* too hard

* remove test
This commit is contained in:
Rob Walker
2019-09-29 21:18:15 -07:00
committed by GitHub
parent e5a7d08966
commit 4f4618441c
15 changed files with 1457 additions and 805 deletions

View File

@@ -110,9 +110,9 @@ pub enum StakeInstruction {
pub fn create_stake_account_with_lockup(
from_pubkey: &Pubkey,
stake_pubkey: &Pubkey,
lamports: u64,
authorized: &Authorized,
lockup: &Lockup,
lamports: u64,
) -> Vec<Instruction> {
vec![
system_instruction::create_account(
@@ -133,15 +133,15 @@ pub fn create_stake_account_with_lockup(
pub fn create_stake_account(
from_pubkey: &Pubkey,
stake_pubkey: &Pubkey,
lamports: u64,
authorized: &Authorized,
lamports: u64,
) -> Vec<Instruction> {
create_stake_account_with_lockup(
from_pubkey,
stake_pubkey,
lamports,
authorized,
&Lockup::default(),
lamports,
)
}
@@ -149,11 +149,15 @@ pub fn create_stake_account_and_delegate_stake(
from_pubkey: &Pubkey,
stake_pubkey: &Pubkey,
vote_pubkey: &Pubkey,
lamports: u64,
authorized: &Authorized,
lamports: u64,
) -> Vec<Instruction> {
let mut instructions = create_stake_account(from_pubkey, stake_pubkey, lamports, authorized);
instructions.push(delegate_stake(stake_pubkey, vote_pubkey));
let mut instructions = create_stake_account(from_pubkey, stake_pubkey, authorized, lamports);
instructions.push(delegate_stake(
stake_pubkey,
&authorized.staker,
vote_pubkey,
));
instructions
}
@@ -206,32 +210,54 @@ pub fn redeem_vote_credits(stake_pubkey: &Pubkey, vote_pubkey: &Pubkey) -> Instr
Instruction::new(id(), &StakeInstruction::RedeemVoteCredits, account_metas)
}
pub fn delegate_stake(stake_pubkey: &Pubkey, vote_pubkey: &Pubkey) -> Instruction {
let account_metas = vec![
AccountMeta::new(*stake_pubkey, true),
AccountMeta::new_credit_only(*vote_pubkey, false),
AccountMeta::new_credit_only(sysvar::clock::id(), false),
AccountMeta::new_credit_only(crate::config::id(), false),
];
pub fn delegate_stake(
stake_pubkey: &Pubkey,
authorized_pubkey: &Pubkey,
vote_pubkey: &Pubkey,
) -> Instruction {
let account_metas = metas_for_authorized_signer(
stake_pubkey,
authorized_pubkey,
&[
AccountMeta::new_credit_only(*vote_pubkey, false),
AccountMeta::new_credit_only(sysvar::clock::id(), false),
AccountMeta::new_credit_only(crate::config::id(), false),
],
);
Instruction::new(id(), &StakeInstruction::DelegateStake, account_metas)
}
pub fn withdraw(stake_pubkey: &Pubkey, to_pubkey: &Pubkey, lamports: u64) -> Instruction {
let account_metas = vec![
AccountMeta::new(*stake_pubkey, true),
AccountMeta::new_credit_only(*to_pubkey, false),
AccountMeta::new_credit_only(sysvar::clock::id(), false),
AccountMeta::new_credit_only(sysvar::stake_history::id(), false),
];
pub fn withdraw(
stake_pubkey: &Pubkey,
authorized_pubkey: &Pubkey,
to_pubkey: &Pubkey,
lamports: u64,
) -> Instruction {
let account_metas = metas_for_authorized_signer(
stake_pubkey,
authorized_pubkey,
&[
AccountMeta::new_credit_only(*to_pubkey, false),
AccountMeta::new_credit_only(sysvar::clock::id(), false),
AccountMeta::new_credit_only(sysvar::stake_history::id(), false),
],
);
Instruction::new(id(), &StakeInstruction::Withdraw(lamports), account_metas)
}
pub fn deactivate_stake(stake_pubkey: &Pubkey, vote_pubkey: &Pubkey) -> Instruction {
let account_metas = vec![
AccountMeta::new(*stake_pubkey, true),
AccountMeta::new_credit_only(*vote_pubkey, false),
AccountMeta::new_credit_only(sysvar::clock::id(), false),
];
pub fn deactivate_stake(
stake_pubkey: &Pubkey,
authorized_pubkey: &Pubkey,
vote_pubkey: &Pubkey,
) -> Instruction {
let account_metas = metas_for_authorized_signer(
stake_pubkey,
authorized_pubkey,
&[
AccountMeta::new_credit_only(*vote_pubkey, false),
AccountMeta::new_credit_only(sysvar::clock::id(), false),
],
);
Instruction::new(id(), &StakeInstruction::Deactivate, account_metas)
}
@@ -361,15 +387,28 @@ mod tests {
Err(InstructionError::InvalidAccountData),
);
assert_eq!(
process_instruction(&delegate_stake(&Pubkey::default(), &Pubkey::default())),
process_instruction(&delegate_stake(
&Pubkey::default(),
&Pubkey::default(),
&Pubkey::default()
)),
Err(InstructionError::InvalidAccountData),
);
assert_eq!(
process_instruction(&withdraw(&Pubkey::default(), &Pubkey::new_rand(), 100)),
process_instruction(&withdraw(
&Pubkey::default(),
&Pubkey::default(),
&Pubkey::default(),
100
)),
Err(InstructionError::InvalidAccountData),
);
assert_eq!(
process_instruction(&deactivate_stake(&Pubkey::default(), &Pubkey::default())),
process_instruction(&deactivate_stake(
&Pubkey::default(),
&Pubkey::default(),
&Pubkey::default()
)),
Err(InstructionError::InvalidAccountData),
);
}

View File

@@ -101,8 +101,8 @@ fn test_stake_account_delegate() {
&mint_pubkey,
&staker_pubkey,
&vote_pubkey,
1_000_000,
&authorized,
1_000_000,
));
bank_client
.send_message(&[&mint_keypair, &staker_keypair], message)
@@ -120,6 +120,7 @@ fn test_stake_account_delegate() {
// Test that we cannot withdraw staked lamports
let message = Message::new_with_payer(
vec![stake_instruction::withdraw(
&staker_pubkey,
&staker_pubkey,
&Pubkey::new_rand(),
1_000_000,
@@ -187,6 +188,7 @@ fn test_stake_account_delegate() {
// Deactivate the stake
let message = Message::new_with_payer(
vec![stake_instruction::deactivate_stake(
&staker_pubkey,
&staker_pubkey,
&vote_pubkey,
)],
@@ -199,6 +201,7 @@ fn test_stake_account_delegate() {
// Test that we cannot withdraw staked lamports due to cooldown period
let message = Message::new_with_payer(
vec![stake_instruction::withdraw(
&staker_pubkey,
&staker_pubkey,
&Pubkey::new_rand(),
1_000_000,
@@ -220,6 +223,7 @@ fn test_stake_account_delegate() {
let message = Message::new_with_payer(
vec![stake_instruction::withdraw(
&staker_pubkey,
&staker_pubkey,
&Pubkey::new_rand(),
1_000_000,
@@ -233,6 +237,7 @@ fn test_stake_account_delegate() {
let message = Message::new_with_payer(
vec![stake_instruction::withdraw(
&staker_pubkey,
&staker_pubkey,
&Pubkey::new_rand(),
250_000,
@@ -257,6 +262,7 @@ fn test_stake_account_delegate() {
// Test that we can withdraw now
let message = Message::new_with_payer(
vec![stake_instruction::withdraw(
&staker_pubkey,
&staker_pubkey,
&Pubkey::new_rand(),
750_000,