hpl_toolkit/compression/
merke_tree_utills.rs

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
use anchor_lang::{
    accounts::program::Program, context::CpiContext, solana_program::account_info::AccountInfo,
    Result, ToAccountInfo,
};
use spl_account_compression::{program::SplAccountCompression, ConcurrentMerkleTree, Node, Noop};

pub trait MerkleTreeUtils {
    fn get_latest_leaf_index(&self) -> u32;
    fn tree_capacity(&self) -> u32;
    fn tree_capacity_is_zero(&self) -> bool;
    fn tree_capacity_is_more_than(&self, val: u32) -> bool;
    fn get_hpl_info(&self) -> (u32, u32, u64);
}

impl<const MAX_DEPTH: usize, const MAX_BUFFER_SIZE: usize> MerkleTreeUtils
    for ConcurrentMerkleTree<MAX_DEPTH, MAX_BUFFER_SIZE>
{
    fn get_latest_leaf_index(&self) -> u32 {
        self.rightmost_proof.index
    }

    fn tree_capacity(&self) -> u32 {
        let max_capacity = 2u32.pow(MAX_DEPTH as u32) - 1;
        max_capacity - self.rightmost_proof.index
    }

    fn tree_capacity_is_zero(&self) -> bool {
        self.tree_capacity() == 0
    }

    fn tree_capacity_is_more_than(&self, val: u32) -> bool {
        self.tree_capacity() > val
    }
    fn get_hpl_info(&self) -> (u32, u32, u64) {
        (
            self.tree_capacity(),
            self.get_latest_leaf_index(),
            self.get_seq(),
        )
    }
}

const HEADER_V1_SIZE: usize = 4 + 4 + 32 + 8 + 6;

pub fn calculate_concurrent_merkle_tree_byte_size(
    max_depth: usize,
    max_buffer_size: usize,
) -> usize {
    let u64_byte_size = 8; // Size of u64
    let u32_byte_size = 4; // Size of u32
    let public_key_byte_size = 32; // Size of public key

    // ChangeLogInternal structure
    let change_log_byte_size =
        public_key_byte_size + max_depth * public_key_byte_size + u32_byte_size + u32_byte_size;

    // Path structure
    let path_byte_size =
        max_depth * public_key_byte_size + public_key_byte_size + u32_byte_size + u32_byte_size;

    // ConcurrentMerkleTree structure
    let concurrent_merkle_tree_byte_size = u64_byte_size * 3 + // sequenceNumber, activeIndex, bufferSize
        change_log_byte_size * max_buffer_size +
        path_byte_size;

    concurrent_merkle_tree_byte_size
}

pub fn calculate_canopy_depth_header_v1(
    max_depth: usize,
    max_buffer_size: usize,
    tree_size: usize,
) -> u8 {
    let canopy_byte_size = tree_size
        - 2
        - HEADER_V1_SIZE
        - calculate_concurrent_merkle_tree_byte_size(max_depth, max_buffer_size);
    let depth = ((canopy_byte_size / 32) as f64 + 2.0).log2() - 1.0;
    depth as u8
}

pub fn init_tree<'info>(
    max_depth: u32,
    max_buffer_size: u32,
    authority: &AccountInfo<'info>,
    merkle_tree: &AccountInfo<'info>,
    compression_program: &Program<'info, SplAccountCompression>,
    noop: &Program<'info, Noop>,
    signer_seeds: Option<&[&[&[u8]]; 1]>,
) -> Result<()> {
    let program = compression_program.to_account_info();
    let accounts = spl_account_compression::cpi::accounts::Initialize {
        authority: authority.to_account_info(),
        merkle_tree: merkle_tree.to_account_info(),
        noop: noop.to_account_info(),
    };
    spl_account_compression::cpi::init_empty_merkle_tree(
        if let Some(signer_seeds) = signer_seeds {
            CpiContext::new_with_signer(program, accounts, signer_seeds)
        } else {
            CpiContext::new(program, accounts)
        },
        max_depth,
        max_buffer_size,
    )
}

pub fn close_tree<'info>(
    recipient: &AccountInfo<'info>,
    authority: &AccountInfo<'info>,
    merkle_tree: &AccountInfo<'info>,
    compression_program: &Program<'info, SplAccountCompression>,
    signer_seeds: Option<&[&[&[u8]]; 1]>,
) -> Result<()> {
    let program = compression_program.to_account_info();
    let accounts = spl_account_compression::cpi::accounts::CloseTree {
        recipient: recipient.to_account_info(),
        authority: authority.to_account_info(),
        merkle_tree: merkle_tree.to_account_info(),
    };
    spl_account_compression::cpi::close_empty_tree(if let Some(signer_seeds) = signer_seeds {
        CpiContext::new_with_signer(program, accounts, signer_seeds)
    } else {
        CpiContext::new(program, accounts)
    })
}

pub fn append_leaf<'info>(
    leaf: Node,
    authority: &AccountInfo<'info>,
    merkle_tree: &AccountInfo<'info>,
    compression_program: &Program<'info, SplAccountCompression>,
    noop: &Program<'info, Noop>,
    signer_seeds: Option<&[&[&[u8]]; 1]>,
) -> Result<()> {
    let program = compression_program.to_account_info();
    let accounts = spl_account_compression::cpi::accounts::Modify {
        authority: authority.to_account_info(),
        merkle_tree: merkle_tree.to_account_info(),
        noop: noop.to_account_info(),
    };

    spl_account_compression::cpi::append(
        if let Some(signer_seeds) = signer_seeds {
            CpiContext::new_with_signer(program, accounts, signer_seeds)
        } else {
            CpiContext::new(program, accounts)
        },
        leaf,
    )
}

pub fn replace_leaf<'info>(
    root: Node,
    previous_leaf: Node,
    new_leaf: Node,
    index: u32,
    authority: &AccountInfo<'info>,
    merkle_tree: &AccountInfo<'info>,
    compression_program: &Program<'info, SplAccountCompression>,
    noop: &Program<'info, Noop>,
    remaining_accounts: Vec<AccountInfo<'info>>,
    signer_seeds: Option<&[&[&[u8]]; 1]>,
) -> Result<()> {
    let program = compression_program.to_account_info();
    let accounts = spl_account_compression::cpi::accounts::Modify {
        authority: authority.to_account_info(),
        merkle_tree: merkle_tree.to_account_info(),
        noop: noop.to_account_info(),
    };

    let ctx = if let Some(signer_seeds) = signer_seeds {
        CpiContext::new_with_signer(program, accounts, signer_seeds)
    } else {
        CpiContext::new(program, accounts)
    }
    .with_remaining_accounts(remaining_accounts);

    spl_account_compression::cpi::replace_leaf(ctx, root, previous_leaf, new_leaf, index)
}

pub fn verify_leaf<'info>(
    root: Node,
    leaf: Node,
    index: u32,
    merkle_tree: &AccountInfo<'info>,
    compression_program: &Program<'info, SplAccountCompression>,
    remaining_accounts: Vec<AccountInfo<'info>>,
) -> Result<()> {
    let program = compression_program.to_account_info();
    let accounts = spl_account_compression::cpi::accounts::VerifyLeaf {
        merkle_tree: merkle_tree.to_account_info(),
    };

    let ctx = CpiContext::new(program, accounts).with_remaining_accounts(remaining_accounts);

    spl_account_compression::cpi::verify_leaf(ctx, root, leaf, index)
}