Skip to content

Commit 623a306

Browse files
committed
program: deallocate stake account on zero lamports
1 parent 0d5ab1d commit 623a306

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

program/src/processor.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use {
1818
state::{Authorized, Lockup, Meta, StakeAuthorize, StakeStateV2},
1919
tools::{acceptable_reference_epoch_credits, eligible_for_deactivate_delinquent},
2020
},
21+
system_program,
2122
sysvar::{epoch_rewards::EpochRewards, stake_history::StakeHistorySysvar, Sysvar},
2223
vote::{program as solana_vote_program, state::VoteState},
2324
},
@@ -58,6 +59,11 @@ fn set_stake_state(stake_account_info: &AccountInfo, new_state: &StakeStateV2) -
5859
.map_err(|_| ProgramError::InvalidAccountData)
5960
}
6061

62+
fn deallocate_stake_account(stake_account_info: &AccountInfo) -> ProgramResult {
63+
stake_account_info.assign(&system_program::id());
64+
stake_account_info.realloc(0, false)
65+
}
66+
6167
// dont call this "move" because we have an instruction MoveLamports
6268
fn relocate_lamports(
6369
source_account_info: &AccountInfo,
@@ -582,7 +588,7 @@ impl Processor {
582588

583589
// Deinitialize state upon zero balance
584590
if split_lamports == source_lamport_balance {
585-
set_stake_state(source_stake_account_info, &StakeStateV2::Uninitialized)?;
591+
deallocate_stake_account(source_stake_account_info)?;
586592
}
587593

588594
relocate_lamports(
@@ -667,7 +673,7 @@ impl Processor {
667673
}
668674

669675
// Deinitialize state upon zero balance
670-
set_stake_state(source_stake_account_info, &StakeStateV2::Uninitialized)?;
676+
deallocate_stake_account(source_stake_account_info)?;
671677
} else {
672678
// a partial withdrawal must not deplete the reserve
673679
let withdraw_lamports_and_reserve = checked_add(withdraw_lamports, reserve)?;
@@ -784,7 +790,7 @@ impl Processor {
784790
}
785791

786792
// Source is about to be drained, deinitialize its state
787-
set_stake_state(source_stake_account_info, &StakeStateV2::Uninitialized)?;
793+
deallocate_stake_account(source_stake_account_info)?;
788794

789795
// Drain the source stake account
790796
relocate_lamports(

0 commit comments

Comments
 (0)