light_merkle_tree_program/verifier_invoked_instructions/
insert_two_leaves_transaction.rs

1use anchor_lang::prelude::*;
2
3use crate::{
4    transaction_merkle_tree::state::{TransactionMerkleTree, TwoLeavesBytesPda},
5    utils::constants::{LEAVES_SEED, TRANSACTION_MERKLE_TREE_SEED},
6    RegisteredVerifier,
7};
8
9#[derive(Accounts)]
10#[instruction(
11    leaf_left: [u8;32],
12    leaf_right: [u8;32],
13    encrypted_utxos: [u8;256],
14)]
15pub struct InsertTwoLeaves<'info> {
16    /// CHECK:` should only be accessed by a registered verifier.
17    #[account(mut, seeds=[__program_id.to_bytes().as_ref()],bump,seeds::program=registered_verifier_pda.pubkey)]
18    pub authority: Signer<'info>,
19    // /// CHECK:` Leaves account should be checked by invoking verifier.
20    #[account(init, seeds= [&leaf_left, LEAVES_SEED], bump, payer=authority, space= 8 + 3 * 32 + 256 + 8 + 8)]
21    pub two_leaves_pda: Account<'info, TwoLeavesBytesPda>,
22    #[account(mut, seeds = [
23        TRANSACTION_MERKLE_TREE_SEED,
24        transaction_merkle_tree.load().unwrap().merkle_tree_nr.to_le_bytes().as_ref()
25    ], bump)]
26    pub transaction_merkle_tree: AccountLoader<'info, TransactionMerkleTree>,
27    pub system_program: Program<'info, System>,
28    #[account(seeds=[&registered_verifier_pda.pubkey.to_bytes()],  bump)]
29    pub registered_verifier_pda: Account<'info, RegisteredVerifier>,
30}
31
32pub fn process_insert_two_leaves(
33    ctx: Context<InsertTwoLeaves>,
34    leaf_left: [u8; 32],
35    leaf_right: [u8; 32],
36    encrypted_utxos: [u8; 256],
37) -> Result<()> {
38    //save leaves into pda account
39    ctx.accounts.two_leaves_pda.node_left = leaf_left;
40    ctx.accounts.two_leaves_pda.node_right = leaf_right;
41    let mut merkle_tree = ctx.accounts.transaction_merkle_tree.load_mut()?;
42    ctx.accounts.two_leaves_pda.left_leaf_index = merkle_tree.next_queued_index;
43
44    ctx.accounts.two_leaves_pda.merkle_tree_pubkey = ctx.accounts.transaction_merkle_tree.key();
45    ctx.accounts.two_leaves_pda.encrypted_utxos = encrypted_utxos;
46
47    // Increase next index by 2 because we're inserting 2 leaves at once.
48    merkle_tree.next_queued_index += 2;
49    Ok(())
50}