|
1 | | -use account_compression::state_merkle_tree_from_bytes_zero_copy; |
2 | 1 | use solana_account_info::AccountInfo; |
3 | 2 | use solana_msg::msg; |
4 | 3 | use solana_program_error::ProgramError; |
5 | 4 |
|
6 | | -use crate::error::LightSdkError; |
7 | 5 | pub mod v1 { |
| 6 | + use super::*; |
8 | 7 | use light_account_checks::checks::check_owner; |
| 8 | + use light_concurrent_merkle_tree::zero_copy::ConcurrentMerkleTreeZeroCopy; |
| 9 | + use light_hasher::Poseidon; |
9 | 10 | use light_sdk_types::ACCOUNT_COMPRESSION_PROGRAM_ID; |
10 | 11 |
|
11 | | - use super::*; |
12 | | - |
13 | 12 | /// StateMerkleTreeAccount discriminator |
14 | 13 | pub const STATE_MERKLE_TREE_DISCRIMINATOR: [u8; 8] = [172, 43, 172, 186, 29, 73, 219, 84]; |
| 14 | + pub const STATE_MERKLE_TREE_ACCOUNT_METADATA_LEN: usize = 224; |
15 | 15 |
|
16 | 16 | /// Reads a root from the concurrent state merkle tree by index |
17 | 17 | pub fn read_state_merkle_tree_root( |
18 | 18 | account_info: &AccountInfo, |
19 | 19 | root_index: u16, |
20 | | - ) -> Result<[u8; 32], LightSdkError> { |
| 20 | + ) -> Result<[u8; 32], ProgramError> { |
21 | 21 | if root_index as usize >= 2400 { |
22 | 22 | msg!( |
23 | 23 | "Invalid root index: {} greater than max root index {}", |
24 | 24 | root_index, |
25 | 25 | 2400 |
26 | 26 | ); |
27 | | - return Err(LightSdkError::from(ProgramError::InvalidArgument)); |
| 27 | + return Err(ProgramError::InvalidArgument); |
28 | 28 | } |
29 | 29 | check_owner(&ACCOUNT_COMPRESSION_PROGRAM_ID, account_info)?; |
30 | 30 | let account_data = account_info.try_borrow_data()?; |
31 | 31 |
|
32 | 32 | // Check discriminator |
33 | 33 | if account_data.len() < 8 { |
34 | 34 | msg!("StateMerkleTreeAccount data too short for discriminator"); |
35 | | - return Err(LightSdkError::from(ProgramError::InvalidAccountData)); |
| 35 | + return Err(ProgramError::InvalidAccountData); |
36 | 36 | } |
37 | 37 |
|
38 | 38 | let discriminator = &account_data[0..8]; |
39 | 39 | if discriminator != STATE_MERKLE_TREE_DISCRIMINATOR { |
40 | 40 | msg!("Invalid StateMerkleTreeAccount discriminator"); |
41 | | - return Err(LightSdkError::from(ProgramError::InvalidAccountData)); |
| 41 | + return Err(ProgramError::InvalidAccountData); |
| 42 | + } |
| 43 | + let required_size = STATE_MERKLE_TREE_ACCOUNT_METADATA_LEN; |
| 44 | + if account_data.len() < required_size { |
| 45 | + msg!("StateMerkleTreeAccount data too short for discriminator"); |
| 46 | + return Err(ProgramError::InvalidAccountData); |
42 | 47 | } |
43 | 48 |
|
44 | | - let merkle_tree = state_merkle_tree_from_bytes_zero_copy(&account_data) |
45 | | - .map_err(|_| LightSdkError::from(ProgramError::InvalidAccountData))?; |
| 49 | + let data = &account_data[required_size..]; |
| 50 | + let merkle_tree = ConcurrentMerkleTreeZeroCopy::<Poseidon, 26>::from_bytes_zero_copy(data)?; |
46 | 51 |
|
47 | 52 | Ok(merkle_tree.roots[root_index as usize]) |
48 | 53 | } |
|
0 commit comments