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
use light_bounded_vec::BoundedVecError;
use light_hasher::errors::HasherError;
use thiserror::Error;

#[derive(Debug, Error)]
pub enum ConcurrentMerkleTreeError {
    #[error("Integer overflow")]
    IntegerOverflow,
    #[error("Invalid height, it has to be greater than 0")]
    HeightZero,
    #[error("Invalid changelog size, it has to be greater than 0. Changelog is used for storing Merkle paths during appends.")]
    ChangelogZero,
    #[error("Invalid number of roots, it has to be greater than 0")]
    RootsZero,
    #[error("Merkle tree is full, cannot append more leaves.")]
    TreeFull,
    #[error("Number of leaves ({0}) exceeds the changelog capacity ({1}).")]
    BatchGreaterThanChangelog(usize, usize),
    #[error("Invalid proof length, expected {0}, got {1}.")]
    InvalidProofLength(usize, usize),
    #[error("Invalid Merkle proof, expected root: {0:?}, the provided proof produces root: {1:?}")]
    InvalidProof([u8; 32], [u8; 32]),
    #[error("Attempting to update the leaf which was updated by an another newest change.")]
    CannotUpdateLeaf,
    #[error("Cannot update the empty leaf")]
    CannotUpdateEmpty,
    #[error("The batch of leaves is empty")]
    EmptyLeaves,
    #[error("Invalid buffer size, expected {0}, got {1}")]
    BufferSize(usize, usize),
    #[error("Hasher error: {0}")]
    Hasher(#[from] HasherError),
    #[error("Bounded vector error: {0}")]
    BoundedVec(#[from] BoundedVecError),
}

// NOTE(vadorovsky): Unfortunately, we need to do it by hand. `num_derive::ToPrimitive`
// doesn't support data-carrying enums.
#[cfg(feature = "solana")]
impl From<ConcurrentMerkleTreeError> for u32 {
    fn from(e: ConcurrentMerkleTreeError) -> u32 {
        match e {
            ConcurrentMerkleTreeError::IntegerOverflow => 10001,
            ConcurrentMerkleTreeError::HeightZero => 10002,
            ConcurrentMerkleTreeError::ChangelogZero => 10003,
            ConcurrentMerkleTreeError::RootsZero => 10004,
            ConcurrentMerkleTreeError::TreeFull => 10005,
            ConcurrentMerkleTreeError::BatchGreaterThanChangelog(_, _) => 10006,
            ConcurrentMerkleTreeError::InvalidProofLength(_, _) => 10007,
            ConcurrentMerkleTreeError::InvalidProof(_, _) => 10008,
            ConcurrentMerkleTreeError::CannotUpdateLeaf => 10009,
            ConcurrentMerkleTreeError::CannotUpdateEmpty => 10010,
            ConcurrentMerkleTreeError::EmptyLeaves => 10011,
            ConcurrentMerkleTreeError::BufferSize(_, _) => 10012,
            ConcurrentMerkleTreeError::Hasher(e) => e.into(),
            ConcurrentMerkleTreeError::BoundedVec(e) => e.into(),
        }
    }
}

#[cfg(feature = "solana")]
impl From<ConcurrentMerkleTreeError> for solana_program::program_error::ProgramError {
    fn from(e: ConcurrentMerkleTreeError) -> Self {
        solana_program::program_error::ProgramError::Custom(e.into())
    }
}