|
| 1 | +use { |
| 2 | + super::{ |
| 3 | + instruction_builders::{InstructionConfig, InstructionExecution}, |
| 4 | + lifecycle::StakeLifecycle, |
| 5 | + utils::{add_sysvars, STAKE_RENT_EXEMPTION}, |
| 6 | + }, |
| 7 | + mollusk_svm::{result::Check, Mollusk}, |
| 8 | + solana_account::AccountSharedData, |
| 9 | + solana_instruction::Instruction, |
| 10 | + solana_pubkey::Pubkey, |
| 11 | + solana_stake_program::id, |
| 12 | +}; |
| 13 | + |
| 14 | +/// Builder for creating stake accounts with customizable parameters |
| 15 | +pub struct StakeAccountBuilder { |
| 16 | + lifecycle: StakeLifecycle, |
| 17 | +} |
| 18 | + |
| 19 | +impl StakeAccountBuilder { |
| 20 | + pub fn build(self) -> (Pubkey, AccountSharedData) { |
| 21 | + let stake_pubkey = Pubkey::new_unique(); |
| 22 | + let account = self.lifecycle.create_uninitialized_account(); |
| 23 | + (stake_pubkey, account) |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +/// Consolidated test context for stake account tests |
| 28 | +pub struct StakeTestContext { |
| 29 | + pub mollusk: Mollusk, |
| 30 | + pub rent_exempt_reserve: u64, |
| 31 | + pub staker: Pubkey, |
| 32 | + pub withdrawer: Pubkey, |
| 33 | +} |
| 34 | + |
| 35 | +impl StakeTestContext { |
| 36 | + /// Create a new test context with all standard setup |
| 37 | + pub fn new() -> Self { |
| 38 | + let mollusk = Mollusk::new(&id(), "solana_stake_program"); |
| 39 | + |
| 40 | + Self { |
| 41 | + mollusk, |
| 42 | + rent_exempt_reserve: STAKE_RENT_EXEMPTION, |
| 43 | + staker: Pubkey::new_unique(), |
| 44 | + withdrawer: Pubkey::new_unique(), |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + /// Create a stake account builder for the specified lifecycle stage |
| 49 | + /// |
| 50 | + /// Example: |
| 51 | + /// ``` |
| 52 | + /// let (stake, account) = ctx |
| 53 | + /// .stake_account(StakeLifecycle::Uninitialized) |
| 54 | + /// .build(); |
| 55 | + /// ``` |
| 56 | + pub fn stake_account(&mut self, lifecycle: StakeLifecycle) -> StakeAccountBuilder { |
| 57 | + StakeAccountBuilder { lifecycle } |
| 58 | + } |
| 59 | + |
| 60 | + /// Process an instruction |
| 61 | + pub fn process_with<'b, C: InstructionConfig>( |
| 62 | + &self, |
| 63 | + config: C, |
| 64 | + ) -> InstructionExecution<'_, 'b> { |
| 65 | + InstructionExecution::new( |
| 66 | + config.build_instruction(self), |
| 67 | + config.build_accounts(), |
| 68 | + self, |
| 69 | + ) |
| 70 | + } |
| 71 | + |
| 72 | + /// Process an instruction with optional missing signer testing |
| 73 | + pub(crate) fn process_instruction_maybe_test_signers( |
| 74 | + &self, |
| 75 | + instruction: &Instruction, |
| 76 | + accounts: Vec<(Pubkey, AccountSharedData)>, |
| 77 | + checks: &[Check], |
| 78 | + test_missing_signers: bool, |
| 79 | + ) -> mollusk_svm::result::InstructionResult { |
| 80 | + if test_missing_signers { |
| 81 | + use solana_program_error::ProgramError; |
| 82 | + |
| 83 | + // Test that removing each signer causes failure |
| 84 | + for i in 0..instruction.accounts.len() { |
| 85 | + if instruction.accounts[i].is_signer { |
| 86 | + let mut modified_instruction = instruction.clone(); |
| 87 | + modified_instruction.accounts[i].is_signer = false; |
| 88 | + |
| 89 | + let accounts_with_sysvars = |
| 90 | + add_sysvars(&self.mollusk, &modified_instruction, accounts.clone()); |
| 91 | + |
| 92 | + self.mollusk.process_and_validate_instruction( |
| 93 | + &modified_instruction, |
| 94 | + &accounts_with_sysvars, |
| 95 | + &[Check::err(ProgramError::MissingRequiredSignature)], |
| 96 | + ); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + // Process with all signers present |
| 102 | + let accounts_with_sysvars = add_sysvars(&self.mollusk, instruction, accounts); |
| 103 | + self.mollusk |
| 104 | + .process_and_validate_instruction(instruction, &accounts_with_sysvars, checks) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +impl Default for StakeTestContext { |
| 109 | + fn default() -> Self { |
| 110 | + Self::new() |
| 111 | + } |
| 112 | +} |
0 commit comments