spl_account_compression/
error.rs

1use anchor_lang::{
2    prelude::*,
3    solana_program::{msg, program_error::ProgramError},
4};
5use bytemuck::PodCastError;
6use spl_concurrent_merkle_tree::error::ConcurrentMerkleTreeError;
7use std::any::type_name;
8use std::mem::size_of;
9
10/// Errors related to misconfiguration or misuse of the Merkle tree
11#[error_code]
12pub enum AccountCompressionError {
13    /// This error is currently not used.
14    #[msg("Incorrect leaf length. Expected vec of 32 bytes")]
15    IncorrectLeafLength,
16
17    /// A modification to the tree was invalid and a changelog was not emitted.
18    /// The proof may be invalid or out-of-date, or the provided leaf hash was invalid.
19    #[msg("Concurrent merkle tree error")]
20    ConcurrentMerkleTreeError,
21
22    /// An issue was detected with loading the provided account data for this ConcurrentMerkleTree.
23    #[msg("Issue zero copying concurrent merkle tree data")]
24    ZeroCopyError,
25
26    /// See [ConcurrentMerkleTreeHeader](/spl_account_compression/state/struct.ConcurrentMerkleTreeHeader.html) for valid configuration options.
27    #[msg("An unsupported max depth or max buffer size constant was provided")]
28    ConcurrentMerkleTreeConstantsError,
29
30    /// When using Canopy, the stored byte length should a multiple of the node's byte length (32 bytes)
31    #[msg("Expected a different byte length for the merkle tree canopy")]
32    CanopyLengthMismatch,
33
34    /// Incorrect authority account provided
35    #[msg("Provided authority does not match expected tree authority")]
36    IncorrectAuthority,
37
38    /// Incorrect account owner
39    #[msg("Account is owned by a different program, expected it to be owned by this program")]
40    IncorrectAccountOwner,
41
42    /// Incorrect account type
43    #[msg("Account provided has incorrect account type")]
44    IncorrectAccountType,
45
46    /// Tree information cannot be processed because the provided leaf_index
47    /// is out of bounds of tree's maximum leaf capacity
48    #[msg("Leaf index of concurrent merkle tree is out of bounds")]
49    LeafIndexOutOfBounds,
50
51    /// When initializing a canopy of the tree, the underlying tree was allocated without space for the canopy
52    #[msg("Tree was initialized without allocating space for the canopy")]
53    CanopyNotAllocated,
54
55    /// The tree was already initialized
56    #[msg("Tree was already initialized")]
57    TreeAlreadyInitialized,
58
59    /// The tree header was not initialized for batch processing
60    #[msg("Tree header was not initialized for batch processing")]
61    BatchNotInitialized,
62
63    /// The canopy root doesn't match the root of the tree
64    #[msg("Canopy root does not match the root of the tree")]
65    CanopyRootMismatch,
66
67    /// The canopy contains nodes to the right of the rightmost leaf of the tree
68    #[msg("Canopy contains nodes to the right of the rightmost leaf of the tree")]
69    CanopyRightmostLeafMismatch,
70}
71
72impl From<&ConcurrentMerkleTreeError> for AccountCompressionError {
73    fn from(_error: &ConcurrentMerkleTreeError) -> Self {
74        AccountCompressionError::ConcurrentMerkleTreeError
75    }
76}
77
78pub fn error_msg<T>(data_len: usize) -> impl Fn(PodCastError) -> ProgramError {
79    move |_: PodCastError| -> ProgramError {
80        msg!(
81            "Failed to load {}. Size is {}, expected {}",
82            type_name::<T>(),
83            data_len,
84            size_of::<T>(),
85        );
86        ProgramError::InvalidAccountData
87    }
88}