Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: stake withdraw logic #3468

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 12 additions & 16 deletions programs/stake/src/stake_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -872,24 +872,20 @@ pub fn withdraw(
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 is_staked // line coverage for branch coverage
&& lamports_and_reserve > stake_account.get_lamports()
{
return Err(InstructionError::InsufficientFunds);
}

if lamports != stake_account.get_lamports() // not a full withdrawal
&& lamports_and_reserve > stake_account.get_lamports()
{
assert!(!is_staked);
return Err(InstructionError::InsufficientFunds);
}

// Deinitialize state upon zero balance
if lamports == stake_account.get_lamports() {
// if the stake is active, we mustn't allow the account to go away
if is_staked {
return Err(InstructionError::InsufficientFunds);
}

// Deinitialize state upon zero balance
stake_account.set_state(&StakeStateV2::Uninitialized)?;
} else {
// Don't allow withdrawing the reserved rent balance or active stake
let lamports_and_reserve = checked_add(lamports, reserve)?;
if lamports_and_reserve > stake_account.get_lamports() {
return Err(InstructionError::InsufficientFunds);
}
}

stake_account.checked_sub_lamports(lamports)?;
Expand Down
Loading