|
| 1 | +#![allow(clippy::arithmetic_side_effects)] |
| 2 | + |
| 3 | +mod helpers; |
| 4 | + |
| 5 | +use { |
| 6 | + helpers::{ |
| 7 | + context::StakeTestContext, |
| 8 | + instruction_builders::SplitConfig, |
| 9 | + lifecycle::StakeLifecycle, |
| 10 | + utils::{get_effective_stake, parse_stake_account}, |
| 11 | + }, |
| 12 | + mollusk_svm::result::Check, |
| 13 | + solana_account::{AccountSharedData, WritableAccount}, |
| 14 | + solana_program_error::ProgramError, |
| 15 | + solana_pubkey::Pubkey, |
| 16 | + solana_stake_interface::state::StakeStateV2, |
| 17 | + solana_stake_program::id, |
| 18 | + test_case::test_case, |
| 19 | +}; |
| 20 | + |
| 21 | +#[test_case(StakeLifecycle::Uninitialized; "uninitialized")] |
| 22 | +#[test_case(StakeLifecycle::Initialized; "initialized")] |
| 23 | +#[test_case(StakeLifecycle::Activating; "activating")] |
| 24 | +#[test_case(StakeLifecycle::Active; "active")] |
| 25 | +#[test_case(StakeLifecycle::Deactivating; "deactivating")] |
| 26 | +#[test_case(StakeLifecycle::Deactive; "deactive")] |
| 27 | +fn test_split(split_source_type: StakeLifecycle) { |
| 28 | + let mut ctx = StakeTestContext::new(); |
| 29 | + let staked_amount = ctx.minimum_delegation.unwrap() * 2; |
| 30 | + |
| 31 | + // Create source stake account at the specified lifecycle stage |
| 32 | + let (split_source, mut split_source_account) = ctx |
| 33 | + .stake_account(split_source_type) |
| 34 | + .staked_amount(staked_amount) |
| 35 | + .build(); |
| 36 | + |
| 37 | + // Create destination stake account matching what create_blank_stake_account does: |
| 38 | + // rent-exempt lamports, correct size, stake program owner, uninitialized data |
| 39 | + let split_dest = Pubkey::new_unique(); |
| 40 | + let split_dest_account = |
| 41 | + AccountSharedData::new(ctx.rent_exempt_reserve, StakeStateV2::size_of(), &id()); |
| 42 | + |
| 43 | + // Determine signer based on lifecycle stage |
| 44 | + let signer = if split_source_type == StakeLifecycle::Uninitialized { |
| 45 | + split_source |
| 46 | + } else { |
| 47 | + ctx.staker |
| 48 | + }; |
| 49 | + |
| 50 | + // Fail: split more than available (would violate rent exemption) |
| 51 | + // Note: Behavior differs between program-test and Mollusk: |
| 52 | + // - program-test: Transaction-level rent check returns InsufficientFunds before program runs |
| 53 | + // - Mollusk: Program succeeds for uninitialized (no program-level check), but violates rent |
| 54 | + // For initialized+ accounts, the program itself checks and returns InsufficientFunds |
| 55 | + if split_source_type == StakeLifecycle::Uninitialized { |
| 56 | + // Mollusk: Program succeeds, but resulting accounts violate rent exemption |
| 57 | + let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { |
| 58 | + ctx.process_with(SplitConfig { |
| 59 | + source: (&split_source, &split_source_account), |
| 60 | + destination: (&split_dest, &split_dest_account), |
| 61 | + signer: &signer, |
| 62 | + amount: staked_amount + 1, |
| 63 | + }) |
| 64 | + .checks(&[Check::success(), Check::all_rent_exempt()]) |
| 65 | + .execute() |
| 66 | + })); |
| 67 | + assert!( |
| 68 | + result.is_err(), |
| 69 | + "Expected rent exemption check to fail for uninitialized split" |
| 70 | + ); |
| 71 | + } else { |
| 72 | + // Program-level check returns InsufficientFunds for initialized+ accounts |
| 73 | + ctx.process_with(SplitConfig { |
| 74 | + source: (&split_source, &split_source_account), |
| 75 | + destination: (&split_dest, &split_dest_account), |
| 76 | + signer: &signer, |
| 77 | + amount: staked_amount + 1, |
| 78 | + }) |
| 79 | + .checks(&[Check::err(ProgramError::InsufficientFunds)]) |
| 80 | + .test_missing_signers(false) |
| 81 | + .execute(); |
| 82 | + } |
| 83 | + |
| 84 | + // Test minimum delegation enforcement for active/transitioning stakes |
| 85 | + if split_source_type.split_minimum_enforced() { |
| 86 | + // Zero split fails |
| 87 | + ctx.process_with(SplitConfig { |
| 88 | + source: (&split_source, &split_source_account), |
| 89 | + destination: (&split_dest, &split_dest_account), |
| 90 | + signer: &signer, |
| 91 | + amount: 0, |
| 92 | + }) |
| 93 | + .checks(&[Check::err(ProgramError::InsufficientFunds)]) |
| 94 | + .test_missing_signers(false) |
| 95 | + .execute(); |
| 96 | + |
| 97 | + // Underfunded destination fails |
| 98 | + ctx.process_with(SplitConfig { |
| 99 | + source: (&split_source, &split_source_account), |
| 100 | + destination: (&split_dest, &split_dest_account), |
| 101 | + signer: &signer, |
| 102 | + amount: ctx.minimum_delegation.unwrap() - 1, |
| 103 | + }) |
| 104 | + .checks(&[Check::err(ProgramError::InsufficientFunds)]) |
| 105 | + .test_missing_signers(false) |
| 106 | + .execute(); |
| 107 | + |
| 108 | + // Underfunded source fails |
| 109 | + ctx.process_with(SplitConfig { |
| 110 | + source: (&split_source, &split_source_account), |
| 111 | + destination: (&split_dest, &split_dest_account), |
| 112 | + signer: &signer, |
| 113 | + amount: ctx.minimum_delegation.unwrap() + 1, |
| 114 | + }) |
| 115 | + .checks(&[Check::err(ProgramError::InsufficientFunds)]) |
| 116 | + .test_missing_signers(false) |
| 117 | + .execute(); |
| 118 | + } |
| 119 | + |
| 120 | + // Split to account with wrong owner fails |
| 121 | + let fake_split_dest = Pubkey::new_unique(); |
| 122 | + let mut fake_split_dest_account = split_dest_account.clone(); |
| 123 | + fake_split_dest_account.set_owner(Pubkey::new_unique()); |
| 124 | + |
| 125 | + ctx.process_with(SplitConfig { |
| 126 | + source: (&split_source, &split_source_account), |
| 127 | + destination: (&fake_split_dest, &fake_split_dest_account), |
| 128 | + signer: &signer, |
| 129 | + amount: staked_amount / 2, |
| 130 | + }) |
| 131 | + .checks(&[Check::err(ProgramError::InvalidAccountOwner)]) |
| 132 | + .test_missing_signers(false) |
| 133 | + .execute(); |
| 134 | + |
| 135 | + // Success: split half |
| 136 | + let result = ctx |
| 137 | + .process_with(SplitConfig { |
| 138 | + source: (&split_source, &split_source_account), |
| 139 | + destination: (&split_dest, &split_dest_account), |
| 140 | + signer: &signer, |
| 141 | + amount: staked_amount / 2, |
| 142 | + }) |
| 143 | + .checks(&[ |
| 144 | + Check::success(), |
| 145 | + Check::all_rent_exempt(), |
| 146 | + Check::account(&split_source) |
| 147 | + .lamports(staked_amount / 2 + ctx.rent_exempt_reserve) |
| 148 | + .owner(&id()) |
| 149 | + .space(StakeStateV2::size_of()) |
| 150 | + .build(), |
| 151 | + Check::account(&split_dest) |
| 152 | + .lamports(staked_amount / 2 + ctx.rent_exempt_reserve) |
| 153 | + .owner(&id()) |
| 154 | + .space(StakeStateV2::size_of()) |
| 155 | + .build(), |
| 156 | + ]) |
| 157 | + .test_missing_signers(true) |
| 158 | + .execute(); |
| 159 | + |
| 160 | + split_source_account = result.resulting_accounts[0].1.clone().into(); |
| 161 | + let split_dest_account: AccountSharedData = result.resulting_accounts[1].1.clone().into(); |
| 162 | + |
| 163 | + // Verify metadata is copied for initialized and above |
| 164 | + if split_source_type >= StakeLifecycle::Initialized { |
| 165 | + let (source_meta, source_stake, _) = parse_stake_account(&split_source_account); |
| 166 | + let (dest_meta, dest_stake, _) = parse_stake_account(&split_dest_account); |
| 167 | + assert_eq!(dest_meta, source_meta); |
| 168 | + |
| 169 | + // Verify delegations are set properly for activating/active/deactivating |
| 170 | + if split_source_type >= StakeLifecycle::Activating |
| 171 | + && split_source_type < StakeLifecycle::Deactive |
| 172 | + { |
| 173 | + assert_eq!(source_stake.unwrap().delegation.stake, staked_amount / 2); |
| 174 | + assert_eq!(dest_stake.unwrap().delegation.stake, staked_amount / 2); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + // Verify nothing has been deactivated for active stakes |
| 179 | + if split_source_type >= StakeLifecycle::Active && split_source_type < StakeLifecycle::Deactive { |
| 180 | + assert_eq!( |
| 181 | + get_effective_stake(&ctx.mollusk, &split_source_account), |
| 182 | + staked_amount / 2, |
| 183 | + ); |
| 184 | + |
| 185 | + assert_eq!( |
| 186 | + get_effective_stake(&ctx.mollusk, &split_dest_account), |
| 187 | + staked_amount / 2, |
| 188 | + ); |
| 189 | + } |
| 190 | +} |
0 commit comments