Skip to content
Open
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions program/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ pub enum SinglePoolInstruction {
/// exist. If the pool does not have the account, `InitializePoolOnRamp` must
/// be called to create it.
///
/// If the validator vote account has been closed, this instruction instead
/// winds the pool down: it moves all on-ramp stake and lamports into the main
/// pool account and then deactivates the main account, so all pool value can be
/// withdrawn. This may take several epochs of calls. If a vote account is
/// recreated at the same address, the next `ReplenishPool` redelegates the pool
/// and it resumes normal operation.
///
/// 0. `[]` Validator vote account
/// 1. `[]` Pool account
/// 2. `[w]` Pool stake account
Expand Down
178 changes: 177 additions & 1 deletion program/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,13 @@ impl Processor {
let clock = Clock::get()?;
let stake_history = StakeHistorySysvar(clock.epoch);

check_vote_account(vote_account_info)?;
// a vote account can only stop being owned by the vote program by being closed. the address is
// bound to the pool by `check_pool_address` below, so a wind-down cannot be triggered while the
// real vote account exists
let vote_account_is_closed = vote_account_info.owner != &vote_program::id();
if !vote_account_is_closed {
check_vote_account(vote_account_info)?;
}
check_pool_address(program_id, vote_account_info.key, pool_info.key)?;

SinglePool::from_account_info(pool_info, program_id)?;
Expand All @@ -797,6 +803,24 @@ impl Processor {
)?;
check_stake_program(stake_program_info.key)?;

if vote_account_is_closed {
let stake_authority_seeds = &[
POOL_STAKE_AUTHORITY_PREFIX,
pool_info.key.as_ref(),
&[stake_authority_bump_seed],
];

return Self::wind_down_pool(
pool_stake_info,
pool_onramp_info,
pool_stake_authority_info,
&[&stake_authority_seeds[..]],
&rent,
&clock,
&stake_history,
);
}

let minimum_delegation = stake::tools::get_minimum_delegation()?;

// we expect these numbers to be equal but get them separately in case of future changes
Expand Down Expand Up @@ -954,6 +978,158 @@ impl Processor {
Ok(())
}

/// `ReplenishPool` when the vote account has been closed. Nothing can be delegated anymore, so
/// instead of growing the pool we consolidate all on-ramp value into the main pool account and
/// deactivate it. If the main account is not fully active, the on-ramp is deactivated first, so
/// its lamports can be moved once both are inactive. Once the main account is fully inactive,
/// `WithdrawStake` splits on lamports, so every user's share of pool NAV becomes redeemable.
/// This takes up to a few epochs and, like normal replenish, each call does whatever is
/// possible now and otherwise succeeds as a no-op.
///
/// If a vote account is later recreated at the same address, the normal replenish path
/// redelegates the deactivated pool and the pool resumes normal operation.
fn wind_down_pool<'a>(
pool_stake_info: &AccountInfo<'a>,
pool_onramp_info: &AccountInfo<'a>,
pool_stake_authority_info: &AccountInfo<'a>,
stake_authority_signers: &[&[&[u8]]],
rent: &Rent,
clock: &Clock,
stake_history: &StakeHistorySysvar,
) -> ProgramResult {
let onramp_rent_exempt_reserve = rent.minimum_balance(pool_onramp_info.data_len());

let (_, pool_stake_state) = get_stake_state(pool_stake_info)?;
let pool_stake_status = pool_stake_state
.delegation
.stake_activating_and_deactivating_v2(
clock.epoch,
stake_history,
PERPETUAL_NEW_WARMUP_COOLDOWN_RATE_EPOCH,
);
// read once: nothing below changes the main account's activation status until the final
// deactivate. `MoveStake` and `MoveLamports` only add to it
let pool_stake_is_fully_active = is_stake_fully_active(&pool_stake_status);
let pool_stake_is_fully_inactive = pool_stake_status == StakeActivationStatus::default();

// on-ramp status, re-read after each stake operation. `None` means `Initialized`
let get_onramp_state =
|| -> Result<Option<(Stake, StakeActivationStatus)>, ProgramError> {
match deserialize_stake(pool_onramp_info) {
Ok(StakeStateV2::Initialized(_)) => Ok(None),
Ok(StakeStateV2::Stake(_, stake, _)) => {
let status = stake.delegation.stake_activating_and_deactivating_v2(
clock.epoch,
stake_history,
PERPETUAL_NEW_WARMUP_COOLDOWN_RATE_EPOCH,
);
Ok(Some((stake, status)))
}
_ => Err(SinglePoolError::OnRampDoesntExist.into()),
}
};

// if both accounts are fully active, move all on-ramp stake to the main account.
// MoveStake does not need the vote account, only that both delegate to the same one
if pool_stake_is_fully_active {
if let Some((_, onramp_status)) = get_onramp_state()? {
if is_stake_fully_active(&onramp_status) {
invoke_signed(
&stake::instruction::move_stake(
pool_onramp_info.key,
pool_stake_info.key,
pool_stake_authority_info.key,
onramp_status.effective,
),
&[
pool_onramp_info.clone(),
pool_stake_info.clone(),
pool_stake_authority_info.clone(),
],
stake_authority_signers,
)?;
}
}
}

// a closure rather than a value, so each call reflects any stake operation above
let current_onramp_non_rent_lamports = || {
pool_onramp_info
.lamports()
.saturating_sub(onramp_rent_exempt_reserve)
};

// move any undelegated on-ramp lamports to the main account with `MoveLamports`.
// each account must be either fully active or fully inactive, and only lamports that are
// neither delegated nor rent can move. a transient on-ramp is handled on a later call
let onramp_movable_lamports = match get_onramp_state()? {
// never delegated, or emptied by `MoveStake` above: all of it
None => current_onramp_non_rent_lamports(),
// fully deactivated: all of it
Some((_, status)) if status == StakeActivationStatus::default() => {
current_onramp_non_rent_lamports()
}
// fully active: only lamports beyond the delegation
Some((stake, status)) if is_stake_fully_active(&status) => {
current_onramp_non_rent_lamports().saturating_sub(stake.delegation.stake)
}
// activating or deactivating: nothing yet
Some(_) => 0,
};

if onramp_movable_lamports > 0
&& (pool_stake_is_fully_active || pool_stake_is_fully_inactive)
{
invoke_signed(
&stake::instruction::move_lamports(
pool_onramp_info.key,
pool_stake_info.key,
pool_stake_authority_info.key,
onramp_movable_lamports,
),
&[
pool_onramp_info.clone(),
pool_stake_info.clone(),
pool_stake_authority_info.clone(),
],
stake_authority_signers,
)?;
}

// if the main account is not fully active, on-ramp stake can never be moved into it.
// deactivate the on-ramp instead, so its lamports can be moved once both are inactive
if !pool_stake_is_fully_active {
if let Some((stake, _)) = get_onramp_state()? {
// `u64::MAX` means the on-ramp has not been deactivated yet
if stake.delegation.deactivation_epoch == u64::MAX {
invoke_signed(
&stake::instruction::deactivate_stake(
pool_onramp_info.key,
pool_stake_authority_info.key,
),
&[pool_onramp_info.clone(), pool_stake_authority_info.clone()],
stake_authority_signers,
)?;
}
}
}

// once the on-ramp holds nothing but rent, deactivate the main account. we wait otherwise,
// because a deactivating main account cannot receive stake or lamports from the on-ramp
if pool_stake_is_fully_active && current_onramp_non_rent_lamports() == 0 {
invoke_signed(
&stake::instruction::deactivate_stake(
pool_stake_info.key,
pool_stake_authority_info.key,
),
&[pool_stake_info.clone(), pool_stake_authority_info.clone()],
stake_authority_signers,
)?;
}

Ok(())
}

fn process_deposit_stake(program_id: &Pubkey, accounts: &[AccountInfo]) -> ProgramResult {
let account_info_iter = &mut accounts.iter();
let pool_info = next_account_info(account_info_iter)?;
Expand Down
Loading