spl_account_compression/
error.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
use anchor_lang::{
    prelude::*,
    solana_program::{msg, program_error::ProgramError},
};
use bytemuck::PodCastError;
use spl_concurrent_merkle_tree::error::ConcurrentMerkleTreeError;
use std::any::type_name;
use std::mem::size_of;

/// Errors related to misconfiguration or misuse of the Merkle tree
#[error_code]
pub enum AccountCompressionError {
    /// This error is currently not used.
    #[msg("Incorrect leaf length. Expected vec of 32 bytes")]
    IncorrectLeafLength,

    /// A modification to the tree was invalid and a changelog was not emitted.
    /// The proof may be invalid or out-of-date, or the provided leaf hash was invalid.
    #[msg("Concurrent merkle tree error")]
    ConcurrentMerkleTreeError,

    /// An issue was detected with loading the provided account data for this ConcurrentMerkleTree.
    #[msg("Issue zero copying concurrent merkle tree data")]
    ZeroCopyError,

    /// See [ConcurrentMerkleTreeHeader](/spl_account_compression/state/struct.ConcurrentMerkleTreeHeader.html) for valid configuration options.
    #[msg("An unsupported max depth or max buffer size constant was provided")]
    ConcurrentMerkleTreeConstantsError,

    /// When using Canopy, the stored byte length should a multiple of the node's byte length (32 bytes)
    #[msg("Expected a different byte length for the merkle tree canopy")]
    CanopyLengthMismatch,

    /// Incorrect authority account provided
    #[msg("Provided authority does not match expected tree authority")]
    IncorrectAuthority,

    /// Incorrect account owner
    #[msg("Account is owned by a different program, expected it to be owned by this program")]
    IncorrectAccountOwner,

    /// Incorrect account type
    #[msg("Account provided has incorrect account type")]
    IncorrectAccountType,

    /// Tree information cannot be processed because the provided leaf_index
    /// is out of bounds of tree's maximum leaf capacity
    #[msg("Leaf index of concurrent merkle tree is out of bounds")]
    LeafIndexOutOfBounds,

    /// When initializing a canopy of the tree, the underlying tree was allocated without space for the canopy
    #[msg("Tree was initialized without allocating space for the canopy")]
    CanopyNotAllocated,

    /// The tree was already initialized
    #[msg("Tree was already initialized")]
    TreeAlreadyInitialized,

    /// The tree header was not initialized for batch processing
    #[msg("Tree header was not initialized for batch processing")]
    BatchNotInitialized,

    /// The canopy root doesn't match the root of the tree
    #[msg("Canopy root does not match the root of the tree")]
    CanopyRootMismatch,

    /// The canopy contains nodes to the right of the rightmost leaf of the tree
    #[msg("Canopy contains nodes to the right of the rightmost leaf of the tree")]
    CanopyRightmostLeafMismatch,
}

impl From<&ConcurrentMerkleTreeError> for AccountCompressionError {
    fn from(_error: &ConcurrentMerkleTreeError) -> Self {
        AccountCompressionError::ConcurrentMerkleTreeError
    }
}

pub fn error_msg<T>(data_len: usize) -> impl Fn(PodCastError) -> ProgramError {
    move |_: PodCastError| -> ProgramError {
        msg!(
            "Failed to load {}. Size is {}, expected {}",
            type_name::<T>(),
            data_len,
            size_of::<T>(),
        );
        ProgramError::InvalidAccountData
    }
}