light_concurrent_merkle_tree/
event.rs

1use borsh::{BorshDeserialize, BorshSerialize};
2#[derive(BorshDeserialize, BorshSerialize, Debug)]
3pub struct MerkleTreeEvents {
4    pub events: Vec<MerkleTreeEvent>,
5}
6
7/// Event containing the Merkle path of the given
8/// [`StateMerkleTree`](light_merkle_tree_program::state::StateMerkleTree)
9/// change. Indexers can use this type of events to re-build a non-sparse
10/// version of state Merkle tree.
11#[derive(BorshDeserialize, BorshSerialize, Debug, PartialEq)]
12#[repr(C)]
13pub enum MerkleTreeEvent {
14    V1(ChangelogEvent),
15    V2(NullifierEvent),
16    V3(IndexedMerkleTreeEvent),
17}
18
19/// Node of the Merkle path with an index representing the position in a
20/// non-sparse Merkle tree.
21#[derive(BorshDeserialize, BorshSerialize, Debug, Eq, PartialEq)]
22pub struct PathNode {
23    pub node: [u8; 32],
24    pub index: u32,
25}
26
27/// Version 1 of the [`ChangelogEvent`](light_merkle_tree_program::state::ChangelogEvent).
28#[derive(BorshDeserialize, BorshSerialize, Debug, PartialEq)]
29pub struct ChangelogEvent {
30    /// Public key of the tree.
31    pub id: [u8; 32],
32    // Merkle paths.
33    pub paths: Vec<Vec<PathNode>>,
34    /// Number of successful operations on the on-chain tree.
35    pub seq: u64,
36    /// Changelog event index.
37    pub index: u32,
38}
39
40#[derive(BorshDeserialize, BorshSerialize, Debug, PartialEq)]
41pub struct NullifierEvent {
42    /// Public key of the tree.
43    pub id: [u8; 32],
44    /// Indices of leaves that were nullified.
45    /// Nullified means updated with [0u8;32].
46    pub nullified_leaves_indices: Vec<u64>,
47    /// Number of successful operations on the on-chain tree.
48    /// seq corresponds to leaves[0].
49    /// seq + 1 corresponds to leaves[1].
50    pub seq: u64,
51}
52
53#[derive(Debug, Default, Clone, Copy, BorshSerialize, BorshDeserialize, Eq, PartialEq)]
54pub struct RawIndexedElement<I>
55where
56    I: Clone,
57{
58    pub value: [u8; 32],
59    pub next_index: I,
60    pub next_value: [u8; 32],
61    pub index: I,
62}
63
64#[derive(BorshDeserialize, BorshSerialize, Debug, Clone, PartialEq)]
65pub struct IndexedMerkleTreeUpdate<I>
66where
67    I: Clone,
68{
69    pub new_low_element: RawIndexedElement<I>,
70    /// Leaf hash in new_low_element.index.
71    pub new_low_element_hash: [u8; 32],
72    pub new_high_element: RawIndexedElement<I>,
73    /// Leaf hash in new_high_element.index,
74    /// is equivalent with next_index.
75    pub new_high_element_hash: [u8; 32],
76}
77
78#[derive(BorshDeserialize, BorshSerialize, Debug, PartialEq)]
79pub struct IndexedMerkleTreeEvent {
80    /// Public key of the tree.
81    pub id: [u8; 32],
82    pub updates: Vec<IndexedMerkleTreeUpdate<usize>>,
83    /// Number of successful operations on the on-chain tree.
84    /// seq corresponds to leaves[0].
85    /// seq + 1 corresponds to leaves[1].
86    pub seq: u64,
87}