merkle_trees/
errors.rs

1use failure::{Backtrace, Context, Fail};
2use std::fmt;
3
4#[derive(Clone, Eq, PartialEq, Debug, Fail)]
5pub enum MerkleTreeErrorKind {
6    /// Occurs when the hash is not found in the database. Relevant to databases implementing `HashValueDb`
7    /// trait. The hash is usually the merkle tree hash
8    #[fail(display = "Expected to find hash {:?} in the database.", hash)]
9    HashNotFoundInDB { hash: Vec<u8> },
10
11    #[fail(display = "Expected to find leaf index {:?} in the database.", index)]
12    LeafIndexNotFoundInDB { index: u64 },
13
14    #[fail(display = "Expected to find node index {:?} in the database.", index)]
15    NodeIndexNotFoundInDB { index: u64 },
16
17    #[fail(display = "Incorrect flag {:?} for RLP node", flag)]
18    IncorrectFlagForRLPNode { flag: u8 },
19
20    #[fail(display = "Cannot deserialize using RLP. Error: {:?}", msg)]
21    CannotDeserializeWithRLP { msg: String },
22
23    #[fail(display = "Incorrect node type. Error: {:?}", msg)]
24    IncorrectNodeType { msg: String },
25
26    #[fail(display = "Querying an empty tree")]
27    CannotQueryEmptyTree,
28
29    #[fail(display = "Trie does not have any key with given prefix")]
30    NoKeyWithPrefixInTrie,
31
32    #[fail(display = "Need equal number of keys and values, no of keys={}, no of values={}", num_keys, num_values)]
33    UnequalNoOfKeysAndValues {num_keys: usize, num_values: usize},
34
35    #[fail(display = "Not found in tree")]
36    NotFoundInTree,
37
38    #[fail(display = "Provide at least one leaf")]
39    NoLeafProvided,
40
41    #[fail(
42        display = "Tree size should be at least {} but was {}",
43        expected, given
44    )]
45    TreeSmallerThanExpected { expected: u64, given: u64 },
46
47    #[fail(
48        display = "Inclusion proof should be of size {} but was of size {}",
49        expected, given
50    )]
51    ShorterInclusionProof { expected: u8, given: u8 },
52
53    #[fail(display = "Error while inserting subtree: {:?}", msg)]
54    ErrorInsertingSubtree { msg: String },
55
56    #[fail(display = "End should be ahead of start, end={}, start={}", to, from)]
57    IncorrectSpan { from: u64, to: u64 },
58
59    #[fail(
60        display = "Trying to get a consistency proof of larger tree from a shorter tree, new tree size = {}, old tree size = {}",
61        new, old
62    )]
63    ConsistencyProofIncorrectTreeSize { old: u64, new: u64 },
64
65    #[fail(
66        display = "Consistency proof is needed only when the trees involved have at least one leaf"
67    )]
68    ConsistencyProofWithEmptyTree,
69
70    #[fail(
71        display = "Consistency proof should be of size {} but was of size {}",
72        expected, given
73    )]
74    ShorterConsistencyProof { expected: u8, given: u8 },
75
76    #[fail(display = "Old root does not match the root calculated from consistency proof")]
77    InconsistentOldRoot,
78
79    #[fail(display = "New root does not match the root calculated from consistency proof")]
80    InconsistentNewRoot,
81}
82
83#[derive(Debug)]
84pub struct MerkleTreeError {
85    inner: Context<MerkleTreeErrorKind>,
86}
87
88impl MerkleTreeError {
89    pub fn kind(&self) -> MerkleTreeErrorKind {
90        self.inner.get_context().clone()
91    }
92
93    pub fn from_kind(kind: MerkleTreeErrorKind) -> Self {
94        Self {
95            inner: Context::new("").context(kind),
96        }
97    }
98}
99
100impl From<MerkleTreeErrorKind> for MerkleTreeError {
101    fn from(kind: MerkleTreeErrorKind) -> Self {
102        Self {
103            inner: Context::new(kind),
104        }
105    }
106}
107
108impl From<Context<MerkleTreeErrorKind>> for MerkleTreeError {
109    fn from(inner: Context<MerkleTreeErrorKind>) -> Self {
110        Self { inner }
111    }
112}
113
114impl Fail for MerkleTreeError {
115    fn cause(&self) -> Option<&dyn Fail> {
116        self.inner.cause()
117    }
118
119    fn backtrace(&self) -> Option<&Backtrace> {
120        self.inner.backtrace()
121    }
122}
123
124impl fmt::Display for MerkleTreeError {
125    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
126        fmt::Display::fmt(&self.inner, f)
127    }
128}