From 240a835938918d1a6b589407c5d1d84bece003f8 Mon Sep 17 00:00:00 2001 From: Ilia Zyrin Date: Wed, 25 Nov 2026 22:49:01 +0100 Subject: [PATCH 1/2] program: wind down pool in ReplenishPool when vote account is closed (#588) --- program/src/instruction.rs | 6 + program/src/processor.rs | 166 ++++++++++++++++++++- program/tests/replenish.rs | 288 ++++++++++++++++++++++++++++++++++++- 3 files changed, 458 insertions(+), 2 deletions(-) diff --git a/program/src/instruction.rs b/program/src/instruction.rs index e1f19b22..38e42c71 100644 --- a/program/src/instruction.rs +++ b/program/src/instruction.rs @@ -67,6 +67,12 @@ 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 it, 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 pool is reactivated as normal. + /// /// 0. `[]` Validator vote account /// 1. `[]` Pool account /// 2. `[w]` Pool stake account diff --git a/program/src/processor.rs b/program/src/processor.rs index ec159338..2e8487f8 100644 --- a/program/src/processor.rs +++ b/program/src/processor.rs @@ -783,7 +783,12 @@ 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 still bound to the pool by `check_pool_address`, so this cannot be spoofed for a live pool + 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)?; @@ -797,6 +802,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 @@ -954,6 +977,147 @@ 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. Once the main account is fully inactive, `WithdrawStake` splits on lamports, + /// so every lamport 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, + ); + let pool_is_fully_active = is_stake_fully_active(&pool_stake_status); + let pool_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, 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_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, + )?; + } + } + } + + // move any undelegated on-ramp lamports to the main account. both accounts must be + // fully active or fully inactive. a transient on-ramp is handled on a later call + let onramp_movable_lamports = match get_onramp_state()? { + None => pool_onramp_info + .lamports() + .saturating_sub(onramp_rent_exempt_reserve), + Some((_, status)) if status == StakeActivationStatus::default() => pool_onramp_info + .lamports() + .saturating_sub(onramp_rent_exempt_reserve), + Some((stake, status)) if is_stake_fully_active(&status) => pool_onramp_info + .lamports() + .saturating_sub(stake.delegation.stake) + .saturating_sub(onramp_rent_exempt_reserve), + Some(_) => 0, + }; + + if onramp_movable_lamports > 0 && (pool_is_fully_active || pool_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_is_fully_active { + if let Some((stake, _)) = get_onramp_state()? { + 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 + let onramp_is_empty = pool_onramp_info + .lamports() + .saturating_sub(onramp_rent_exempt_reserve) + == 0; + + if pool_is_fully_active && onramp_is_empty { + 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)?; diff --git a/program/tests/replenish.rs b/program/tests/replenish.rs index df745710..f3ec61c8 100644 --- a/program/tests/replenish.rs +++ b/program/tests/replenish.rs @@ -6,17 +6,81 @@ use { helpers::*, solana_account::ReadableAccount, solana_clock::Clock, + solana_keypair::Keypair, + solana_native_token::LAMPORTS_PER_SOL, solana_program_test::*, + solana_pubkey::Pubkey, solana_signer::Signer, solana_stake_interface::{ - instruction as stake_instruction, stake_history::StakeHistory, state::StakeStateV2, + instruction as stake_instruction, + stake_history::StakeHistory, + state::{StakeActivationStatus, StakeStateV2}, }, solana_sysvar_id::SysvarId, solana_transaction::Transaction, + solana_vote_interface::instruction as vote_instruction, spl_single_pool::{error::SinglePoolError, id, instruction}, test_case::test_matrix, }; +// stake status and lamports for a stake account. `None` status means `Initialized` +async fn get_stake_status( + context: &mut ProgramTestContext, + pubkey: &Pubkey, +) -> (Option, u64) { + let clock = context.banks_client.get_sysvar::().await.unwrap(); + let stake_history_account = context + .banks_client + .get_account(StakeHistory::id()) + .await + .unwrap() + .unwrap(); + let stake_history: StakeHistory = bincode::deserialize(stake_history_account.data()).unwrap(); + + let (_, stake, lamports) = get_stake_account(&mut context.banks_client, pubkey).await; + let status = stake.map(|stake| { + stake + .delegation + .stake_activating_and_deactivating_v2(clock.epoch, &stake_history, Some(0)) + }); + + (status, lamports) +} + +async fn close_vote_account(context: &mut ProgramTestContext, accounts: &SinglePoolAccounts) { + let vote_address = accounts.vote_account.pubkey(); + let vote_lamports = get_account(&mut context.banks_client, &vote_address) + .await + .lamports; + + let transaction = Transaction::new_signed_with_payer( + &[vote_instruction::withdraw( + &vote_address, + &accounts.withdrawer.pubkey(), + vote_lamports, + &context.payer.pubkey(), + )], + Some(&context.payer.pubkey()), + &[&context.payer, &accounts.withdrawer], + context.last_blockhash, + ); + + context + .banks_client + .process_transaction(transaction) + .await + .unwrap(); + + assert!(context + .banks_client + .get_account(vote_address) + .await + .unwrap() + .is_none()); + + refresh_blockhash(context).await; +} + #[test_matrix( [StakeProgramVersion::Stable, StakeProgramVersion::Beta, StakeProgramVersion::Edge], [false, true], @@ -629,3 +693,225 @@ async fn fail_onramp_doesnt_exist(stake_version: StakeProgramVersion, activate: .await .unwrap(); } + +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] +enum ClosedOnRampState { + Empty, + Initialized, + Activating, + Active, +} + +// if the vote account is closed, replenish cannot delegate anything. instead, it must move all on-ramp +// value into the main pool account and deactivate it, so that the entire pool NAV can be withdrawn. +// `pool_excess` adds undelegated lamports (eg mev rewards) to the main account. +// `pool_inactive` starts the wind-down with an already inactive main account, which requires +// deactivating the on-ramp before its lamports can be moved. +#[test_matrix( + [StakeProgramVersion::Stable, StakeProgramVersion::Beta, StakeProgramVersion::Edge], + [ClosedOnRampState::Empty, ClosedOnRampState::Initialized, ClosedOnRampState::Activating, ClosedOnRampState::Active], + [false, true], + [false, true] +)] +#[tokio::test] +async fn closed_vote_account_wind_down( + stake_version: StakeProgramVersion, + onramp_state: ClosedOnRampState, + pool_excess: bool, + pool_inactive: bool, +) { + let Some(program_test) = program_test(stake_version) else { + return; + }; + let mut context = program_test.start_with_context().await; + + let rent = context.banks_client.get_rent().await.unwrap(); + let stake_rent = rent.minimum_balance(StakeStateV2::size_of()); + + let accounts = SinglePoolAccounts::default(); + accounts + .initialize_for_withdraw(&mut context, TEST_STAKE_AMOUNT, None, true) + .await; + + if onramp_state >= ClosedOnRampState::Initialized { + transfer( + &mut context.banks_client, + &context.payer, + &context.last_blockhash, + &accounts.onramp_account, + TEST_STAKE_AMOUNT, + ) + .await; + } + + if onramp_state >= ClosedOnRampState::Activating { + replenish(&mut context, &accounts.vote_account.pubkey()).await; + } + + if onramp_state >= ClosedOnRampState::Active { + advance_epoch(&mut context).await; + } + + if pool_excess { + transfer( + &mut context.banks_client, + &context.payer, + &context.last_blockhash, + &accounts.stake_account, + LAMPORTS_PER_SOL, + ) + .await; + } + + if pool_inactive { + force_deactivate_stake_account(&mut context, &accounts.stake_account).await; + } + + close_vote_account(&mut context, &accounts).await; + + let (_, pre_pool_lamports) = get_stake_status(&mut context, &accounts.stake_account).await; + let (_, pre_onramp_lamports) = get_stake_status(&mut context, &accounts.onramp_account).await; + let pre_nav = pre_pool_lamports + pre_onramp_lamports - stake_rent * 2; + + let alice_tokens = get_token_balance(&mut context.banks_client, &accounts.alice_token).await; + let notional_supply = + get_token_supply(&mut context.banks_client, &accounts.mint).await + LAMPORTS_PER_SOL; + + // replenish on a closed vote account always succeeds, and winds down within a few epochs + for _ in 0..4 { + replenish(&mut context, &accounts.vote_account.pubkey()).await; + advance_epoch(&mut context).await; + } + + // it is still a successful no-op after the wind-down completes + replenish(&mut context, &accounts.vote_account.pubkey()).await; + + // all value is in the main pool account, which is fully inactive + let (pool_status, pool_lamports) = + get_stake_status(&mut context, &accounts.stake_account).await; + assert_eq!(pool_status, Some(StakeActivationStatus::default())); + assert_eq!(pool_lamports, pre_nav + stake_rent); + + let (onramp_status, onramp_lamports) = + get_stake_status(&mut context, &accounts.onramp_account).await; + assert!(matches!( + onramp_status, + None | Some(StakeActivationStatus { + effective: 0, + activating: 0, + deactivating: 0, + }) + )); + assert_eq!(onramp_lamports, stake_rent); + + // alice can withdraw her entire share of the pool NAV, including on-ramp value + let instructions = instruction::withdraw( + &id(), + &accounts.pool, + &accounts.alice_stake.pubkey(), + &accounts.alice.pubkey(), + &accounts.alice_token, + &accounts.alice.pubkey(), + alice_tokens, + ); + let transaction = Transaction::new_signed_with_payer( + &instructions, + Some(&context.payer.pubkey()), + &[&context.payer, &accounts.alice], + context.last_blockhash, + ); + + context + .banks_client + .process_transaction(transaction) + .await + .unwrap(); + + let expected_withdraw = + (alice_tokens as u128 * pre_nav as u128 / notional_supply as u128) as u64; + + let (alice_status, alice_lamports) = + get_stake_status(&mut context, &accounts.alice_stake.pubkey()).await; + assert_eq!(alice_status, Some(StakeActivationStatus::default())); + assert_eq!(alice_lamports, expected_withdraw + stake_rent); + assert_eq!( + get_token_balance(&mut context.banks_client, &accounts.alice_token).await, + 0 + ); +} + +// replenish fails safe if the vote account is closed while stranded on-ramp value has not yet been moved, +// and a pool whose vote account is recreated at the same address resumes normal operation +#[test_matrix( + [StakeProgramVersion::Stable, StakeProgramVersion::Beta, StakeProgramVersion::Edge] +)] +#[tokio::test] +async fn closed_vote_account_recreated(stake_version: StakeProgramVersion) { + let Some(program_test) = program_test(stake_version) else { + return; + }; + let mut context = program_test.start_with_context().await; + + let rent = context.banks_client.get_rent().await.unwrap(); + let stake_rent = rent.minimum_balance(StakeStateV2::size_of()); + + let accounts = SinglePoolAccounts::default(); + accounts + .initialize_for_withdraw(&mut context, TEST_STAKE_AMOUNT, None, true) + .await; + + // activating on-ramp, then the vote account is closed in the same epoch + transfer( + &mut context.banks_client, + &context.payer, + &context.last_blockhash, + &accounts.onramp_account, + TEST_STAKE_AMOUNT, + ) + .await; + replenish(&mut context, &accounts.vote_account.pubkey()).await; + close_vote_account(&mut context, &accounts).await; + + // the activating on-ramp cannot be moved yet, so the main account is left active + replenish(&mut context, &accounts.vote_account.pubkey()).await; + let (pool_status, _) = get_stake_status(&mut context, &accounts.stake_account).await; + let pool_status = pool_status.unwrap(); + assert!(pool_status.effective > 0 && pool_status.deactivating == 0); + + // once the on-ramp is active, its stake is moved and the main account deactivated in one call + advance_epoch(&mut context).await; + replenish(&mut context, &accounts.vote_account.pubkey()).await; + + let (_, onramp_lamports) = get_stake_status(&mut context, &accounts.onramp_account).await; + assert_eq!(onramp_lamports, stake_rent); + let (pool_status, _) = get_stake_status(&mut context, &accounts.stake_account).await; + let pool_status = pool_status.unwrap(); + assert_eq!(pool_status.deactivating, pool_status.effective); + assert!(pool_status.effective > TEST_STAKE_AMOUNT * 2); + + advance_epoch(&mut context).await; + + // recreate a vote account at the same address with a new validator identity + create_vote( + &mut context.banks_client, + &context.payer, + &context.last_blockhash, + &Keypair::new(), + &accounts.voter.pubkey(), + &accounts.withdrawer.pubkey(), + &accounts.vote_account, + ) + .await; + refresh_blockhash(&mut context).await; + + // normal replenish redelegates the pool, and it is fully active the next epoch + replenish(&mut context, &accounts.vote_account.pubkey()).await; + advance_epoch(&mut context).await; + + let (pool_status, pool_lamports) = + get_stake_status(&mut context, &accounts.stake_account).await; + let pool_status = pool_status.unwrap(); + assert_eq!(pool_status.activating, 0); + assert_eq!(pool_status.deactivating, 0); + assert_eq!(pool_status.effective, pool_lamports - stake_rent); +} From e4c9697ab14a0cf694698698380dbe6b0a8e4560 Mon Sep 17 00:00:00 2001 From: Ilia Zyrin Date: Wed, 25 Nov 2026 23:33:11 +0100 Subject: [PATCH 2/2] program: clarify naming and comments in closed-vote wind-down --- program/src/instruction.rs | 7 ++-- program/src/processor.rs | 68 ++++++++++++++++++++++---------------- program/tests/replenish.rs | 29 +++++++++------- 3 files changed, 61 insertions(+), 43 deletions(-) diff --git a/program/src/instruction.rs b/program/src/instruction.rs index 38e42c71..b0a28b2a 100644 --- a/program/src/instruction.rs +++ b/program/src/instruction.rs @@ -69,9 +69,10 @@ pub enum SinglePoolInstruction { /// /// 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 it, 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 pool is reactivated as normal. + /// 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 diff --git a/program/src/processor.rs b/program/src/processor.rs index 2e8487f8..486ed065 100644 --- a/program/src/processor.rs +++ b/program/src/processor.rs @@ -783,8 +783,9 @@ impl Processor { let clock = Clock::get()?; let stake_history = StakeHistorySysvar(clock.epoch); - // a vote account can only stop being owned by the vote program by being closed. the address - // is still bound to the pool by `check_pool_address`, so this cannot be spoofed for a live pool + // 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)?; @@ -979,9 +980,11 @@ impl Processor { /// `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. Once the main account is fully inactive, `WithdrawStake` splits on lamports, - /// so every lamport 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. + /// 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. @@ -1004,8 +1007,10 @@ impl Processor { stake_history, PERPETUAL_NEW_WARMUP_COOLDOWN_RATE_EPOCH, ); - let pool_is_fully_active = is_stake_fully_active(&pool_stake_status); - let pool_is_fully_inactive = pool_stake_status == StakeActivationStatus::default(); + // 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 = @@ -1026,7 +1031,7 @@ impl Processor { // 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_is_fully_active { + if pool_stake_is_fully_active { if let Some((_, onramp_status)) = get_onramp_state()? { if is_stake_fully_active(&onramp_status) { invoke_signed( @@ -1047,23 +1052,34 @@ impl Processor { } } - // move any undelegated on-ramp lamports to the main account. both accounts must be - // fully active or fully inactive. a transient on-ramp is handled on a later call - let onramp_movable_lamports = match get_onramp_state()? { - None => pool_onramp_info + // 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), - Some((_, status)) if status == StakeActivationStatus::default() => pool_onramp_info - .lamports() - .saturating_sub(onramp_rent_exempt_reserve), - Some((stake, status)) if is_stake_fully_active(&status) => pool_onramp_info - .lamports() - .saturating_sub(stake.delegation.stake) - .saturating_sub(onramp_rent_exempt_reserve), + .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_is_fully_active || pool_is_fully_inactive) { + 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, @@ -1082,8 +1098,9 @@ impl Processor { // 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_is_fully_active { + 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( @@ -1099,12 +1116,7 @@ impl Processor { // 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 - let onramp_is_empty = pool_onramp_info - .lamports() - .saturating_sub(onramp_rent_exempt_reserve) - == 0; - - if pool_is_fully_active && onramp_is_empty { + if pool_stake_is_fully_active && current_onramp_non_rent_lamports() == 0 { invoke_signed( &stake::instruction::deactivate_stake( pool_stake_info.key, diff --git a/program/tests/replenish.rs b/program/tests/replenish.rs index f3ec61c8..51eacd1b 100644 --- a/program/tests/replenish.rs +++ b/program/tests/replenish.rs @@ -694,10 +694,11 @@ async fn fail_onramp_doesnt_exist(stake_version: StakeProgramVersion, activate: .unwrap(); } +// ordered: the test builds each state on the setup steps of the ones before it, via `>=` #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] -enum ClosedOnRampState { +enum OnRampStateAtVoteClose { Empty, - Initialized, + Undelegated, Activating, Active, } @@ -709,14 +710,14 @@ enum ClosedOnRampState { // deactivating the on-ramp before its lamports can be moved. #[test_matrix( [StakeProgramVersion::Stable, StakeProgramVersion::Beta, StakeProgramVersion::Edge], - [ClosedOnRampState::Empty, ClosedOnRampState::Initialized, ClosedOnRampState::Activating, ClosedOnRampState::Active], + [OnRampStateAtVoteClose::Empty, OnRampStateAtVoteClose::Undelegated, OnRampStateAtVoteClose::Activating, OnRampStateAtVoteClose::Active], [false, true], [false, true] )] #[tokio::test] async fn closed_vote_account_wind_down( stake_version: StakeProgramVersion, - onramp_state: ClosedOnRampState, + onramp_state: OnRampStateAtVoteClose, pool_excess: bool, pool_inactive: bool, ) { @@ -733,7 +734,7 @@ async fn closed_vote_account_wind_down( .initialize_for_withdraw(&mut context, TEST_STAKE_AMOUNT, None, true) .await; - if onramp_state >= ClosedOnRampState::Initialized { + if onramp_state >= OnRampStateAtVoteClose::Undelegated { transfer( &mut context.banks_client, &context.payer, @@ -744,11 +745,11 @@ async fn closed_vote_account_wind_down( .await; } - if onramp_state >= ClosedOnRampState::Activating { + if onramp_state >= OnRampStateAtVoteClose::Activating { replenish(&mut context, &accounts.vote_account.pubkey()).await; } - if onramp_state >= ClosedOnRampState::Active { + if onramp_state >= OnRampStateAtVoteClose::Active { advance_epoch(&mut context).await; } @@ -777,8 +778,10 @@ async fn closed_vote_account_wind_down( let notional_supply = get_token_supply(&mut context.banks_client, &accounts.mint).await + LAMPORTS_PER_SOL; - // replenish on a closed vote account always succeeds, and winds down within a few epochs - for _ in 0..4 { + // replenish on a closed vote account always succeeds. the slowest case is an activating on-ramp: + // one call waits for it to activate, the next moves its stake and deactivates the main account, + // then the main account cools down for an epoch + for _ in 0..3 { replenish(&mut context, &accounts.vote_account.pubkey()).await; advance_epoch(&mut context).await; } @@ -840,8 +843,9 @@ async fn closed_vote_account_wind_down( ); } -// replenish fails safe if the vote account is closed while stranded on-ramp value has not yet been moved, -// and a pool whose vote account is recreated at the same address resumes normal operation +// vote account is closed while the on-ramp is still activating: replenish waits for it to activate, +// then moves its stake and deactivates the main account. a pool whose vote account is then recreated +// at the same address resumes normal operation #[test_matrix( [StakeProgramVersion::Stable, StakeProgramVersion::Beta, StakeProgramVersion::Edge] )] @@ -877,6 +881,7 @@ async fn closed_vote_account_recreated(stake_version: StakeProgramVersion) { let (pool_status, _) = get_stake_status(&mut context, &accounts.stake_account).await; let pool_status = pool_status.unwrap(); assert!(pool_status.effective > 0 && pool_status.deactivating == 0); + let pre_pool_stake = pool_status.effective; // once the on-ramp is active, its stake is moved and the main account deactivated in one call advance_epoch(&mut context).await; @@ -887,7 +892,7 @@ async fn closed_vote_account_recreated(stake_version: StakeProgramVersion) { let (pool_status, _) = get_stake_status(&mut context, &accounts.stake_account).await; let pool_status = pool_status.unwrap(); assert_eq!(pool_status.deactivating, pool_status.effective); - assert!(pool_status.effective > TEST_STAKE_AMOUNT * 2); + assert_eq!(pool_status.effective, pre_pool_stake + TEST_STAKE_AMOUNT); advance_epoch(&mut context).await;