1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
use anchor_lang::{
    solana_program::{pubkey::Pubkey, system_instruction},
    AnchorDeserialize,
};
use light_hash_set::HashSet;
use num_bigint::ToBigUint;
use num_traits::{Bounded, CheckedAdd, CheckedSub, Unsigned};
use solana_program_test::{BanksClientError, ProgramTestContext};
use solana_sdk::{
    account::Account,
    instruction::{Instruction, InstructionError},
    signature::{Keypair, Signature},
    signer::Signer,
    transaction::Transaction,
};
use std::{fmt, marker::PhantomData, mem, pin::Pin};
pub mod spl;
pub mod system_program;
pub mod test_env;
pub mod test_indexer;

#[derive(Debug, Clone)]
pub struct AccountZeroCopy<'a, T> {
    pub account: Pin<Box<Account>>,
    deserialized: *const T,
    _phantom_data: PhantomData<&'a T>,
}

impl<'a, T> AccountZeroCopy<'a, T> {
    pub async fn new(context: &mut ProgramTestContext, address: Pubkey) -> AccountZeroCopy<'a, T> {
        let account = Box::pin(
            context
                .banks_client
                .get_account(address)
                .await
                .unwrap()
                .unwrap(),
        );
        let deserialized = account.data[8..].as_ptr() as *const T;

        Self {
            account,
            deserialized,
            _phantom_data: PhantomData,
        }
    }

    // Safe method to access `deserialized` ensuring the lifetime is respected
    pub fn deserialized(&self) -> &'a T {
        unsafe { &*self.deserialized }
    }
}

pub async fn get_account<T: AnchorDeserialize>(
    context: &mut ProgramTestContext,
    pubkey: Pubkey,
) -> T {
    let account = context
        .banks_client
        .get_account(pubkey)
        .await
        .unwrap()
        .unwrap();
    T::deserialize(&mut &account.data[8..]).unwrap()
}

/// Fetches the given account, then copies and serializes it as a `HashSet`.
///
/// # Safety
///
/// This is highly unsafe. Ensuring that:
///
/// * The correct account is used.
/// * The account has enough space to be treated as a HashSet with specified
///   parameters.
/// * The account data is aligned.
///
/// Is the caller's responsibility.
pub async unsafe fn get_hash_set<I, T>(
    context: &mut ProgramTestContext,
    pubkey: Pubkey,
) -> HashSet<I>
where
    I: Bounded
        + CheckedAdd
        + CheckedSub
        + Clone
        + Copy
        + fmt::Display
        + From<u8>
        + PartialEq
        + PartialOrd
        + ToBigUint
        + TryFrom<u64>
        + TryFrom<usize>
        + Unsigned,
    f64: From<I>,
    u64: TryFrom<I>,
    usize: TryFrom<I>,
    <usize as TryFrom<I>>::Error: fmt::Debug,
{
    let mut account = context
        .banks_client
        .get_account(pubkey)
        .await
        .unwrap()
        .unwrap();

    HashSet::from_bytes_copy(&mut account.data[8 + mem::size_of::<T>()..]).unwrap()
}

pub async fn airdrop_lamports(
    banks_client: &mut ProgramTestContext,
    destination_pubkey: &Pubkey,
    lamports: u64,
) -> Result<(), Box<dyn std::error::Error>> {
    // Create a transfer instruction
    let transfer_instruction =
        system_instruction::transfer(&banks_client.payer.pubkey(), destination_pubkey, lamports);
    let latest_blockhash = banks_client.get_new_latest_blockhash().await.unwrap();
    // Create and sign a transaction
    let transaction = Transaction::new_signed_with_payer(
        &[transfer_instruction],
        Some(&banks_client.payer.pubkey()),
        &vec![&banks_client.payer],
        latest_blockhash,
    );

    // Send the transaction
    banks_client
        .banks_client
        .process_transaction(transaction)
        .await?;

    Ok(())
}

pub async fn create_and_send_transaction(
    context: &mut ProgramTestContext,
    instruction: &[Instruction],
    payer: &Pubkey,
    signers: &[&Keypair],
) -> Result<Signature, BanksClientError> {
    let transaction = Transaction::new_signed_with_payer(
        instruction,
        Some(payer),
        &signers.to_vec(),
        context.get_new_latest_blockhash().await.unwrap(),
    );
    let signature = transaction.signatures[0];
    context
        .banks_client
        .process_transaction(transaction)
        .await?;
    Ok(signature)
}

#[derive(Debug, Clone, PartialEq)]
pub struct FeeConfig {
    pub state_merkle_tree_rollover: u64,
    pub nullifier_queue_rollover: u64,
    pub address_queue_rollover: u64,
    pub tip: u64,
}

impl Default for FeeConfig {
    fn default() -> Self {
        Self {
            state_merkle_tree_rollover: 149,
            nullifier_queue_rollover: 29,
            address_queue_rollover: 181,
            tip: 1,
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct TransactionParams {
    pub num_input_compressed_accounts: u8,
    pub num_output_compressed_accounts: u8,
    pub num_new_addresses: u8,
    pub compress: i64,
    pub fee_config: FeeConfig,
}

pub async fn create_and_send_transaction_with_event<T>(
    context: &mut ProgramTestContext,
    instruction: &[Instruction],
    payer: &Pubkey,
    signers: &[&Keypair],
    transaction_params: Option<TransactionParams>,
) -> Result<Option<T>, BanksClientError>
where
    T: AnchorDeserialize,
{
    let pre_balance = context
        .banks_client
        .get_account(*payer)
        .await?
        .unwrap()
        .lamports;

    let transaction = Transaction::new_signed_with_payer(
        instruction,
        Some(payer),
        signers,
        context.get_new_latest_blockhash().await?,
    );

    // Simulate the transaction. Currently, in banks-client/server, only
    // simulations are able to track CPIs. Therefore, simulating is the
    // only way to retrieve the event.
    let simulation_result = context
        .banks_client
        .simulate_transaction(transaction.clone())
        .await?;
    // Handle an error nested in the simulation result.
    if let Some(Err(e)) = simulation_result.result {
        return Err(BanksClientError::TransactionError(e));
    }

    // Retrieve the event.
    let event = simulation_result
        .simulation_details
        .and_then(|details| details.inner_instructions)
        .and_then(|instructions| {
            instructions.iter().flatten().find_map(|inner_instruction| {
                T::try_from_slice(inner_instruction.instruction.data.as_slice()).ok()
            })
        });
    // If transaction was successful, execute it.
    if let Some(Ok(())) = simulation_result.result {
        context
            .banks_client
            .process_transaction(transaction)
            .await?;
    }

    // assert correct rollover fee and tip distribution
    if let Some(transaction_params) = transaction_params {
        let post_balance = context
            .banks_client
            .get_account(*payer)
            .await?
            .unwrap()
            .lamports;

        let mut tip = 0;
        for rollover in &[
            transaction_params.num_new_addresses,
            transaction_params.num_input_compressed_accounts,
            transaction_params.num_output_compressed_accounts,
        ] {
            if *rollover != 0 {
                tip += transaction_params.fee_config.tip as i64;
            }
        }

        let expected_post_balance = pre_balance as i64
            - i64::from(transaction_params.num_new_addresses)
                * transaction_params.fee_config.address_queue_rollover as i64
            - i64::from(transaction_params.num_input_compressed_accounts)
                * transaction_params.fee_config.nullifier_queue_rollover as i64
            - i64::from(transaction_params.num_output_compressed_accounts)
                * transaction_params.fee_config.state_merkle_tree_rollover as i64
            - transaction_params.compress
            - 5000
            - tip * (i64::from(transaction_params.num_output_compressed_accounts) / 28 + 1);

        if post_balance as i64 != expected_post_balance {
            println!("transaction_params: {:?}", transaction_params);
            println!("pre_balance: {}", pre_balance);
            println!("post_balance: {}", post_balance);
            println!("expected post_balance: {}", expected_post_balance);
            println!(
                "diff post_balance: {}",
                post_balance as i64 - expected_post_balance
            );
            println!("tip: {}", tip);
            return Err(BanksClientError::TransactionError(
                solana_sdk::transaction::TransactionError::InstructionError(
                    0,
                    InstructionError::Custom(11111),
                ),
            ));
        }
    }
    Ok(event)
}

pub fn create_account_instruction(
    payer: &Pubkey,
    size: usize,
    rent: u64,
    id: &Pubkey,
    keypair: Option<&Keypair>,
) -> Instruction {
    let keypair = match keypair {
        Some(keypair) => keypair.insecure_clone(),
        None => Keypair::new(),
    };
    system_instruction::create_account(payer, &keypair.pubkey(), rent, size as u64, id)
}

/// Asserts that the given `BanksTransactionResultWithMetadata` is an error with a custom error code
/// or a program error.
/// Unfortunately BanksTransactionResultWithMetadata does not reliably expose the custom error code, so
/// we allow program error as well.
// TODO: add generic that parses the error code from the result
pub fn assert_custom_error_or_program_error(
    res: solana_program_test::BanksTransactionResultWithMetadata,
    error_code: u32,
) -> Result<(), solana_sdk::transaction::TransactionError> {
    if !(res.result
        == Err(solana_sdk::transaction::TransactionError::InstructionError(
            0,
            InstructionError::Custom(error_code),
        ))
        || res.result
            == Err(solana_sdk::transaction::TransactionError::InstructionError(
                0,
                InstructionError::ProgramFailedToComplete,
            )))
    {
        println!("result {:?}", res.result);
        println!("error_code {:?}", error_code);
        return Err(res.result.unwrap_err());
    }
    Ok(())
}