Stake program update (#15308)

This commit is contained in:
sakridge
2021-02-12 17:15:48 -08:00
committed by GitHub
parent 32ec9147bb
commit 563231132f

View File

@ -54,6 +54,13 @@ impl Default for StakeState {
} }
} }
fn checked_add(a: u64, b: u64) -> Result<u64, InstructionError> {
match a.checked_add(b) {
Some(sum) => Ok(sum),
None => Err(InstructionError::InsufficientFunds),
}
}
impl StakeState { impl StakeState {
pub fn get_rent_exempt_reserve(rent: &Rent) -> u64 { pub fn get_rent_exempt_reserve(rent: &Rent) -> u64 {
rent.minimum_balance(std::mem::size_of::<StakeState>()) rent.minimum_balance(std::mem::size_of::<StakeState>())
@ -1207,7 +1214,8 @@ impl<'a> StakeAccount for KeyedAccount<'a> {
stake.delegation.stake stake.delegation.stake
}; };
(meta.lockup, staked + meta.rent_exempt_reserve, staked != 0) let staked_and_reserve = checked_add(staked, meta.rent_exempt_reserve)?;
(meta.lockup, staked_and_reserve, staked != 0)
} }
StakeState::Initialized(meta) => { StakeState::Initialized(meta) => {
meta.authorized meta.authorized
@ -1231,15 +1239,16 @@ impl<'a> StakeAccount for KeyedAccount<'a> {
return Err(StakeError::LockupInForce.into()); return Err(StakeError::LockupInForce.into());
} }
let lamports_and_reserve = checked_add(lamports, reserve)?;
// if the stake is active, we mustn't allow the account to go away // if the stake is active, we mustn't allow the account to go away
if is_staked // line coverage for branch coverage if is_staked // line coverage for branch coverage
&& lamports + reserve > self.lamports()? && lamports_and_reserve > self.lamports()?
{ {
return Err(InstructionError::InsufficientFunds); return Err(InstructionError::InsufficientFunds);
} }
if lamports != self.lamports()? // not a full withdrawal if lamports != self.lamports()? // not a full withdrawal
&& lamports + reserve > self.lamports()? && lamports_and_reserve > self.lamports()?
{ {
assert!(!is_staked); assert!(!is_staked);
return Err(InstructionError::InsufficientFunds); return Err(InstructionError::InsufficientFunds);