Remove vote pubkey from deactivate_stake (#6257)

* Remove vote pubkey from deactivate_stake

* Fix test

* Update docs
This commit is contained in:
Tyera Eulberg
2019-10-07 16:07:01 -06:00
committed by GitHub
parent 4a071b06bd
commit 79987e788e
8 changed files with 33 additions and 98 deletions

View File

@ -100,10 +100,9 @@ pub enum StakeInstruction {
/// Deactivates the stake in the account
/// requires Authorized::staker signature
///
/// Expects 3 Accounts:
/// Expects 2 Accounts:
/// 0 - Delegate StakeAccount
/// 1 - VoteAccount to which the Stake is delegated
/// 2 - Syscall Account that carries epoch
/// 1 - Syscall Account that carries epoch
///
Deactivate,
}
@ -250,18 +249,11 @@ pub fn withdraw(
Instruction::new(id(), &StakeInstruction::Withdraw(lamports), account_metas)
}
pub fn deactivate_stake(
stake_pubkey: &Pubkey,
authorized_pubkey: &Pubkey,
vote_pubkey: &Pubkey,
) -> Instruction {
pub fn deactivate_stake(stake_pubkey: &Pubkey, authorized_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(sysvar::clock::id(), false)],
);
Instruction::new(id(), &StakeInstruction::Deactivate, account_metas)
}
@ -340,17 +332,11 @@ pub fn process_instruction(
)
}
StakeInstruction::Deactivate => {
if rest.len() < 2 {
if rest.is_empty() {
return Err(InstructionError::InvalidInstructionData);
}
let (vote, rest) = rest.split_at_mut(1);
let vote = &mut vote[0];
me.deactivate_stake(
vote,
&sysvar::clock::from_keyed_account(&rest[0])?,
&rest[1..],
)
me.deactivate_stake(&sysvar::clock::from_keyed_account(&rest[0])?, &rest[1..])
}
}
}
@ -419,11 +405,7 @@ mod tests {
Err(InstructionError::InvalidAccountData),
);
assert_eq!(
process_instruction(&deactivate_stake(
&Pubkey::default(),
&Pubkey::default(),
&Pubkey::default()
)),
process_instruction(&deactivate_stake(&Pubkey::default(), &Pubkey::default())),
Err(InstructionError::InvalidAccountData),
);
}
@ -600,7 +582,6 @@ mod tests {
super::process_instruction(
&Pubkey::default(),
&mut [
KeyedAccount::new(&Pubkey::default(), false, &mut Account::default()),
KeyedAccount::new(&Pubkey::default(), false, &mut Account::default()),
KeyedAccount::new(
&sysvar::rewards::id(),
@ -617,14 +598,11 @@ mod tests {
assert_eq!(
super::process_instruction(
&Pubkey::default(),
&mut [
KeyedAccount::new(&Pubkey::default(), false, &mut Account::default()),
KeyedAccount::new(
&sysvar::clock::id(),
false,
&mut sysvar::rewards::create_account(1, 0.0, 0.0)
),
],
&mut [KeyedAccount::new(
&sysvar::clock::id(),
false,
&mut sysvar::rewards::create_account(1, 0.0, 0.0)
),],
&serialize(&StakeInstruction::Deactivate).unwrap(),
),
Err(InstructionError::InvalidInstructionData),

View File

@ -430,7 +430,6 @@ pub trait StakeAccount {
) -> Result<(), InstructionError>;
fn deactivate_stake(
&mut self,
vote_account: &KeyedAccount,
clock: &sysvar::clock::Clock,
other_signers: &[KeyedAccount],
) -> Result<(), InstructionError>;
@ -516,7 +515,6 @@ impl<'a> StakeAccount for KeyedAccount<'a> {
}
fn deactivate_stake(
&mut self,
_vote_account: &KeyedAccount, // TODO: used in slashing
clock: &sysvar::clock::Clock,
other_signers: &[KeyedAccount],
) -> Result<(), InstructionError> {
@ -1156,15 +1154,10 @@ mod tests {
..sysvar::clock::Clock::default()
};
let vote_pubkey = Pubkey::new_rand();
let mut vote_account =
vote_state::create_account(&vote_pubkey, &Pubkey::new_rand(), 0, 100);
let vote_keyed_account = KeyedAccount::new(&vote_pubkey, false, &mut vote_account);
// signed keyed account but not staked yet
let mut stake_keyed_account = KeyedAccount::new(&stake_pubkey, true, &mut stake_account);
assert_eq!(
stake_keyed_account.deactivate_stake(&vote_keyed_account, &clock, &[]),
stake_keyed_account.deactivate_stake(&clock, &[]),
Err(InstructionError::InvalidAccountData)
);
@ -1187,16 +1180,13 @@ mod tests {
// unsigned keyed account
let mut stake_keyed_account = KeyedAccount::new(&stake_pubkey, false, &mut stake_account);
assert_eq!(
stake_keyed_account.deactivate_stake(&vote_keyed_account, &clock, &[]),
stake_keyed_account.deactivate_stake(&clock, &[]),
Err(InstructionError::MissingRequiredSignature)
);
// Deactivate after staking
let mut stake_keyed_account = KeyedAccount::new(&stake_pubkey, true, &mut stake_account);
assert_eq!(
stake_keyed_account.deactivate_stake(&vote_keyed_account, &clock, &[]),
Ok(())
);
assert_eq!(stake_keyed_account.deactivate_stake(&clock, &[]), Ok(()));
}
#[test]
@ -1317,10 +1307,7 @@ mod tests {
);
// deactivate the stake before withdrawal
assert_eq!(
stake_keyed_account.deactivate_stake(&vote_keyed_account, &clock, &[]),
Ok(())
);
assert_eq!(stake_keyed_account.deactivate_stake(&clock, &[]), Ok(()));
// simulate time passing
clock.epoch += 100;
@ -1896,11 +1883,7 @@ mod tests {
let new_staker_keyed_account =
KeyedAccount::new(&new_staker_pubkey, true, &mut new_staker_account);
assert_eq!(
stake_keyed_account.deactivate_stake(
&vote_keyed_account,
&clock,
&[new_staker_keyed_account]
),
stake_keyed_account.deactivate_stake(&clock, &[new_staker_keyed_account]),
Ok(())
);
}

View File

@ -190,7 +190,6 @@ fn test_stake_account_delegate() {
vec![stake_instruction::deactivate_stake(
&staker_pubkey,
&staker_pubkey,
&vote_pubkey,
)],
Some(&mint_pubkey),
);