light_concurrent_merkle_tree/
errors.rs

1use light_bounded_vec::BoundedVecError;
2use light_hasher::errors::HasherError;
3use thiserror::Error;
4
5#[derive(Debug, Error)]
6pub enum ConcurrentMerkleTreeError {
7    #[error("Integer overflow")]
8    IntegerOverflow,
9    #[error("Invalid height, it has to be greater than 0")]
10    HeightZero,
11    #[error("Invalud height, expected {0}")]
12    InvalidHeight(usize),
13    #[error("Invalid changelog size, it has to be greater than 0. Changelog is used for storing Merkle paths during appends.")]
14    ChangelogZero,
15    #[error("Invalid number of roots, it has to be greater than 0")]
16    RootsZero,
17    #[error("Canopy depth has to be lower than height")]
18    CanopyGeThanHeight,
19    #[error("Merkle tree is full, cannot append more leaves.")]
20    TreeFull,
21    #[error("Number of leaves ({0}) exceeds the changelog capacity ({1}).")]
22    BatchGreaterThanChangelog(usize, usize),
23    #[error("Invalid proof length, expected {0}, got {1}.")]
24    InvalidProofLength(usize, usize),
25    #[error("Invalid Merkle proof, expected root: {0:?}, the provided proof produces root: {1:?}")]
26    InvalidProof([u8; 32], [u8; 32]),
27    #[error("Attempting to update the leaf which was updated by an another newest change.")]
28    CannotUpdateLeaf,
29    #[error("Cannot update the empty leaf")]
30    CannotUpdateEmpty,
31    #[error("The batch of leaves is empty")]
32    EmptyLeaves,
33    #[error("Invalid buffer size, expected {0}, got {1}")]
34    BufferSize(usize, usize),
35    #[error("Hasher error: {0}")]
36    Hasher(#[from] HasherError),
37    #[error("Bounded vector error: {0}")]
38    BoundedVec(#[from] BoundedVecError),
39}
40
41// NOTE(vadorovsky): Unfortunately, we need to do it by hand. `num_derive::ToPrimitive`
42// doesn't support data-carrying enums.
43#[cfg(feature = "solana")]
44impl From<ConcurrentMerkleTreeError> for u32 {
45    fn from(e: ConcurrentMerkleTreeError) -> u32 {
46        match e {
47            ConcurrentMerkleTreeError::IntegerOverflow => 10001,
48            ConcurrentMerkleTreeError::HeightZero => 10002,
49            ConcurrentMerkleTreeError::InvalidHeight(_) => 10003,
50            ConcurrentMerkleTreeError::ChangelogZero => 10004,
51            ConcurrentMerkleTreeError::RootsZero => 10005,
52            ConcurrentMerkleTreeError::CanopyGeThanHeight => 10006,
53            ConcurrentMerkleTreeError::TreeFull => 10007,
54            ConcurrentMerkleTreeError::BatchGreaterThanChangelog(_, _) => 10008,
55            ConcurrentMerkleTreeError::InvalidProofLength(_, _) => 10009,
56            ConcurrentMerkleTreeError::InvalidProof(_, _) => 10010,
57            ConcurrentMerkleTreeError::CannotUpdateLeaf => 10011,
58            ConcurrentMerkleTreeError::CannotUpdateEmpty => 10012,
59            ConcurrentMerkleTreeError::EmptyLeaves => 10013,
60            ConcurrentMerkleTreeError::BufferSize(_, _) => 10014,
61            ConcurrentMerkleTreeError::Hasher(e) => e.into(),
62            ConcurrentMerkleTreeError::BoundedVec(e) => e.into(),
63        }
64    }
65}
66
67#[cfg(feature = "solana")]
68impl From<ConcurrentMerkleTreeError> for solana_program::program_error::ProgramError {
69    fn from(e: ConcurrentMerkleTreeError) -> Self {
70        solana_program::program_error::ProgramError::Custom(e.into())
71    }
72}