account_compression/instructions/
batch_nullify.rs

1use anchor_lang::prelude::*;
2use light_batched_merkle_tree::merkle_tree::{
3    BatchedMerkleTreeAccount, InstructionDataBatchNullifyInputs,
4};
5
6use crate::{
7    emit_indexer_event,
8    utils::check_signer_is_registered_or_authority::{
9        check_signer_is_registered_or_authority, GroupAccounts,
10    },
11    RegisteredProgram,
12};
13
14#[derive(Accounts)]
15pub struct BatchNullify<'info> {
16    /// CHECK: should only be accessed by a registered program or owner.
17    pub authority: Signer<'info>,
18    pub registered_program_pda: Option<Account<'info, RegisteredProgram>>,
19    /// CHECK: when emitting event.
20    pub log_wrapper: UncheckedAccount<'info>,
21    /// CHECK: in state_from_account_info.
22    #[account(mut)]
23    pub merkle_tree: AccountInfo<'info>,
24}
25
26impl<'info> GroupAccounts<'info> for BatchNullify<'info> {
27    fn get_authority(&self) -> &Signer<'info> {
28        &self.authority
29    }
30    fn get_registered_program_pda(&self) -> &Option<Account<'info, RegisteredProgram>> {
31        &self.registered_program_pda
32    }
33}
34
35/// Nullify a batch of leaves from the input queue
36/// to the state Merkle tree.
37/// Nullify means updating the leaf index with a nullifier.
38/// The input queue is part of the state Merkle tree account.
39/// 1. Check Merkle tree account discriminator, tree type, and program ownership.
40/// 2. Check that signer is registered or authority.
41/// 3. Nullify leaves from the input queue to the state Merkle tree.
42///    3.1 Verifies batch zkp and updates root.
43/// 4. Emit indexer event.
44pub fn process_batch_nullify<'a, 'b, 'c: 'info, 'info>(
45    ctx: &'a Context<'a, 'b, 'c, 'info, BatchNullify<'info>>,
46    instruction_data: InstructionDataBatchNullifyInputs,
47) -> Result<()> {
48    // 1. Check Merkle tree account discriminator, tree type, and program ownership.
49    let merkle_tree =
50        &mut BatchedMerkleTreeAccount::state_from_account_info(&ctx.accounts.merkle_tree)
51            .map_err(ProgramError::from)?;
52    // 2. Check that signer is registered or authority.
53    check_signer_is_registered_or_authority::<BatchNullify, BatchedMerkleTreeAccount>(
54        ctx,
55        merkle_tree,
56    )?;
57    // 3. Nullify leaves from the input queue to the state Merkle tree.
58    let event = merkle_tree
59        .update_tree_from_input_queue(instruction_data)
60        .map_err(ProgramError::from)?;
61    // 4. Emit indexer event.
62    emit_indexer_event(event.try_to_vec()?, &ctx.accounts.log_wrapper)
63}