|
| 1 | +#![allow(clippy::arithmetic_side_effects)] |
| 2 | + |
| 3 | +mod helpers; |
| 4 | + |
| 5 | +use { |
| 6 | + helpers::{ |
| 7 | + context::StakeTestContext, |
| 8 | + instruction_builders::{DeactivateConfig, DelegateConfig}, |
| 9 | + lifecycle::StakeLifecycle, |
| 10 | + stake_tracker::MolluskStakeExt, |
| 11 | + utils::{create_vote_account, increment_vote_account_credits, parse_stake_account}, |
| 12 | + }, |
| 13 | + mollusk_svm::result::Check, |
| 14 | + solana_account::{AccountSharedData, WritableAccount}, |
| 15 | + solana_program_error::ProgramError, |
| 16 | + solana_pubkey::Pubkey, |
| 17 | + solana_stake_interface::{ |
| 18 | + error::StakeError, |
| 19 | + state::{Delegation, Stake, StakeStateV2}, |
| 20 | + }, |
| 21 | + solana_stake_program::id, |
| 22 | +}; |
| 23 | + |
| 24 | +#[tokio::test] |
| 25 | +async fn test_delegate() { |
| 26 | + let mut ctx = StakeTestContext::with_delegation(); |
| 27 | + let vote_account = *ctx.vote_account.as_ref().unwrap(); |
| 28 | + let mut vote_account_data = ctx.vote_account_data.as_ref().unwrap().clone(); |
| 29 | + let min_delegation = ctx.minimum_delegation.unwrap(); |
| 30 | + |
| 31 | + let vote_state_credits = 100u64; |
| 32 | + increment_vote_account_credits(&mut vote_account_data, 0, vote_state_credits); |
| 33 | + |
| 34 | + let (stake, mut stake_account) = ctx |
| 35 | + .stake_account(StakeLifecycle::Initialized) |
| 36 | + .staked_amount(min_delegation) |
| 37 | + .build(); |
| 38 | + |
| 39 | + // Delegate stake |
| 40 | + let result = ctx |
| 41 | + .process_with(DelegateConfig { |
| 42 | + stake: (&stake, &stake_account), |
| 43 | + vote: (&vote_account, &vote_account_data), |
| 44 | + }) |
| 45 | + .checks(&[ |
| 46 | + Check::success(), |
| 47 | + Check::all_rent_exempt(), |
| 48 | + Check::account(&stake) |
| 49 | + .lamports(ctx.rent_exempt_reserve + min_delegation) |
| 50 | + .owner(&id()) |
| 51 | + .space(StakeStateV2::size_of()) |
| 52 | + .build(), |
| 53 | + ]) |
| 54 | + .test_missing_signers(true) |
| 55 | + .execute(); |
| 56 | + stake_account = result.resulting_accounts[0].1.clone().into(); |
| 57 | + |
| 58 | + // Verify that delegate() looks right |
| 59 | + let clock = ctx.mollusk.sysvars.clock.clone(); |
| 60 | + let (_, stake_data, _) = parse_stake_account(&stake_account); |
| 61 | + assert_eq!( |
| 62 | + stake_data.unwrap(), |
| 63 | + Stake { |
| 64 | + delegation: Delegation { |
| 65 | + voter_pubkey: vote_account, |
| 66 | + stake: min_delegation, |
| 67 | + activation_epoch: clock.epoch, |
| 68 | + deactivation_epoch: u64::MAX, |
| 69 | + ..Delegation::default() |
| 70 | + }, |
| 71 | + credits_observed: vote_state_credits, |
| 72 | + } |
| 73 | + ); |
| 74 | + |
| 75 | + // Advance epoch to activate the stake |
| 76 | + let activation_epoch = ctx.mollusk.sysvars.clock.epoch; |
| 77 | + ctx.tracker.as_mut().unwrap().track_delegation( |
| 78 | + &stake, |
| 79 | + min_delegation, |
| 80 | + activation_epoch, |
| 81 | + &vote_account, |
| 82 | + ); |
| 83 | + |
| 84 | + let slots_per_epoch = ctx.mollusk.sysvars.epoch_schedule.slots_per_epoch; |
| 85 | + let current_slot = ctx.mollusk.sysvars.clock.slot; |
| 86 | + ctx.mollusk.warp_to_slot_with_stake_tracking( |
| 87 | + ctx.tracker.as_ref().unwrap(), |
| 88 | + current_slot + slots_per_epoch, |
| 89 | + Some(0), |
| 90 | + ); |
| 91 | + |
| 92 | + // Verify that delegate fails as stake is active and not deactivating |
| 93 | + ctx.process_with(DelegateConfig { |
| 94 | + stake: (&stake, &stake_account), |
| 95 | + vote: (&vote_account, ctx.vote_account_data.as_ref().unwrap()), |
| 96 | + }) |
| 97 | + .checks(&[Check::err(StakeError::TooSoonToRedelegate.into())]) |
| 98 | + .test_missing_signers(false) |
| 99 | + .execute(); |
| 100 | + |
| 101 | + // Deactivate |
| 102 | + let result = ctx |
| 103 | + .process_with(DeactivateConfig { |
| 104 | + stake: (&stake, &stake_account), |
| 105 | + override_signer: None, |
| 106 | + }) |
| 107 | + .execute(); |
| 108 | + stake_account = result.resulting_accounts[0].1.clone().into(); |
| 109 | + |
| 110 | + // Create second vote account |
| 111 | + let (vote_account2, vote_account2_data) = ctx.create_second_vote_account(); |
| 112 | + |
| 113 | + // Verify that delegate to a different vote account fails during deactivation |
| 114 | + ctx.process_with(DelegateConfig { |
| 115 | + stake: (&stake, &stake_account), |
| 116 | + vote: (&vote_account2, &vote_account2_data), |
| 117 | + }) |
| 118 | + .checks(&[Check::err(StakeError::TooSoonToRedelegate.into())]) |
| 119 | + .test_missing_signers(false) |
| 120 | + .execute(); |
| 121 | + |
| 122 | + // Verify that delegate succeeds to same vote account when stake is deactivating |
| 123 | + let result = ctx |
| 124 | + .process_with(DelegateConfig { |
| 125 | + stake: (&stake, &stake_account), |
| 126 | + vote: (&vote_account, ctx.vote_account_data.as_ref().unwrap()), |
| 127 | + }) |
| 128 | + .execute(); |
| 129 | + stake_account = result.resulting_accounts[0].1.clone().into(); |
| 130 | + |
| 131 | + // Verify that deactivation has been cleared |
| 132 | + let (_, stake_data, _) = parse_stake_account(&stake_account); |
| 133 | + assert_eq!(stake_data.unwrap().delegation.deactivation_epoch, u64::MAX); |
| 134 | + |
| 135 | + // Verify that delegate to a different vote account fails if stake is still active |
| 136 | + ctx.process_with(DelegateConfig { |
| 137 | + stake: (&stake, &stake_account), |
| 138 | + vote: (&vote_account2, &vote_account2_data), |
| 139 | + }) |
| 140 | + .checks(&[Check::err(StakeError::TooSoonToRedelegate.into())]) |
| 141 | + .test_missing_signers(false) |
| 142 | + .execute(); |
| 143 | + |
| 144 | + // Advance epoch again using tracker |
| 145 | + let current_slot = ctx.mollusk.sysvars.clock.slot; |
| 146 | + let slots_per_epoch = ctx.mollusk.sysvars.epoch_schedule.slots_per_epoch; |
| 147 | + ctx.mollusk.warp_to_slot_with_stake_tracking( |
| 148 | + ctx.tracker.as_ref().unwrap(), |
| 149 | + current_slot + slots_per_epoch, |
| 150 | + Some(0), |
| 151 | + ); |
| 152 | + |
| 153 | + // Delegate still fails after stake is fully activated; redelegate is not supported |
| 154 | + let (vote_account2, vote_account2_data) = ctx.create_second_vote_account(); |
| 155 | + |
| 156 | + ctx.process_with(DelegateConfig { |
| 157 | + stake: (&stake, &stake_account), |
| 158 | + vote: (&vote_account2, &vote_account2_data), |
| 159 | + }) |
| 160 | + .checks(&[Check::err(StakeError::TooSoonToRedelegate.into())]) |
| 161 | + .test_missing_signers(false) |
| 162 | + .execute(); |
| 163 | +} |
| 164 | + |
| 165 | +#[tokio::test] |
| 166 | +async fn test_delegate_fake_vote_account() { |
| 167 | + let mut ctx = StakeTestContext::with_delegation(); |
| 168 | + |
| 169 | + // Create fake vote account (not owned by vote program) |
| 170 | + let fake_vote_account = Pubkey::new_unique(); |
| 171 | + let mut fake_vote_data = create_vote_account(); |
| 172 | + fake_vote_data.set_owner(Pubkey::new_unique()); // Wrong owner |
| 173 | + |
| 174 | + let min_delegation = ctx.minimum_delegation.unwrap(); |
| 175 | + let (stake, stake_account) = ctx |
| 176 | + .stake_account(StakeLifecycle::Initialized) |
| 177 | + .staked_amount(min_delegation) |
| 178 | + .build(); |
| 179 | + |
| 180 | + // Try to delegate to fake vote account |
| 181 | + ctx.process_with(DelegateConfig { |
| 182 | + stake: (&stake, &stake_account), |
| 183 | + vote: (&fake_vote_account, &fake_vote_data), |
| 184 | + }) |
| 185 | + .checks(&[Check::err(ProgramError::IncorrectProgramId)]) |
| 186 | + .test_missing_signers(false) |
| 187 | + .execute(); |
| 188 | +} |
| 189 | + |
| 190 | +#[tokio::test] |
| 191 | +async fn test_delegate_non_stake_account() { |
| 192 | + let ctx = StakeTestContext::with_delegation(); |
| 193 | + |
| 194 | + // Create a rewards pool account (program-owned but not a stake account) |
| 195 | + let rewards_pool = Pubkey::new_unique(); |
| 196 | + let rewards_pool_data = AccountSharedData::new_data_with_space( |
| 197 | + ctx.rent_exempt_reserve, |
| 198 | + &StakeStateV2::RewardsPool, |
| 199 | + StakeStateV2::size_of(), |
| 200 | + &id(), |
| 201 | + ) |
| 202 | + .unwrap(); |
| 203 | + |
| 204 | + ctx.process_with(DelegateConfig { |
| 205 | + stake: (&rewards_pool, &rewards_pool_data), |
| 206 | + vote: ( |
| 207 | + ctx.vote_account.as_ref().unwrap(), |
| 208 | + ctx.vote_account_data.as_ref().unwrap(), |
| 209 | + ), |
| 210 | + }) |
| 211 | + .checks(&[Check::err(ProgramError::InvalidAccountData)]) |
| 212 | + .test_missing_signers(false) |
| 213 | + .execute(); |
| 214 | +} |
0 commit comments