light_program_test/indexer/
state_tree.rs

1use std::fmt::Debug;
2
3use light_client::indexer::{IndexerError, StateMerkleTreeAccounts};
4use light_compressed_account::TreeType;
5use light_concurrent_merkle_tree::light_hasher::Poseidon;
6use light_merkle_tree_reference::MerkleTree;
7
8#[derive(Debug, Clone)]
9pub struct LeafIndexInfo {
10    pub leaf_index: u32,
11    pub leaf: [u8; 32],
12    pub tx_hash: [u8; 32],
13}
14
15#[derive(Debug, Clone)]
16pub struct StateMerkleTreeBundle {
17    pub rollover_fee: i64,
18    pub merkle_tree: Box<MerkleTree<Poseidon>>,
19    pub accounts: StateMerkleTreeAccounts,
20    pub tree_type: TreeType,
21    pub output_queue_elements: Vec<([u8; 32], u64)>,
22    pub input_leaf_indices: Vec<LeafIndexInfo>,
23    pub output_queue_batch_size: Option<usize>,
24    pub num_inserted_batches: usize,
25}
26
27impl StateMerkleTreeBundle {
28    /// Returns true if index is in current queue range.
29    pub fn leaf_index_in_queue_range(&self, index: usize) -> Result<bool, IndexerError> {
30        if let Some(output_queue_batch_size) = self.output_queue_batch_size {
31            let start_offset = self.num_inserted_batches * output_queue_batch_size;
32            // There is always 2 batches.
33            let end_offset = start_offset + (output_queue_batch_size * 2);
34            Ok(start_offset <= index && index < end_offset)
35        } else {
36            Err(IndexerError::CustomError(format!(
37                "Batch size not set for Merkle tree {:?}",
38                self.accounts.merkle_tree
39            )))
40        }
41    }
42}