casper_types/digest/
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
//! Errors in constructing and validating indexed Merkle proofs, chunks with indexed Merkle proofs.

use alloc::string::String;
use core::fmt::{self, Display, Formatter};
#[cfg(feature = "std")]
use std::error::Error as StdError;

use super::{ChunkWithProof, Digest};
use crate::bytesrepr;

/// Possible hashing errors.
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
    /// The digest length was an incorrect size.
    IncorrectDigestLength(usize),
    /// There was a decoding error.
    Base16DecodeError(base16::DecodeError),
}

impl Display for Error {
    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
        match self {
            Error::IncorrectDigestLength(length) => {
                write!(
                    formatter,
                    "incorrect digest length {}, expected length {}.",
                    length,
                    Digest::LENGTH
                )
            }
            Error::Base16DecodeError(error) => {
                write!(formatter, "base16 decode error: {}", error)
            }
        }
    }
}

#[cfg(feature = "std")]
impl StdError for Error {
    fn source(&self) -> Option<&(dyn StdError + 'static)> {
        match self {
            Error::IncorrectDigestLength(_) => None,
            Error::Base16DecodeError(error) => Some(error),
        }
    }
}

/// Error validating a Merkle proof of a chunk.
#[derive(Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum MerkleVerificationError {
    /// Index out of bounds.
    IndexOutOfBounds {
        /// Count.
        count: u64,
        /// Index.
        index: u64,
    },

    /// Unexpected proof length.
    UnexpectedProofLength {
        /// Count.
        count: u64,
        /// Index.
        index: u64,
        /// Expected proof length.
        expected_proof_length: u8,
        /// Actual proof length.
        actual_proof_length: usize,
    },
}

impl Display for MerkleVerificationError {
    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
        match self {
            MerkleVerificationError::IndexOutOfBounds { count, index } => {
                write!(
                    formatter,
                    "index out of bounds - count: {}, index: {}",
                    count, index
                )
            }
            MerkleVerificationError::UnexpectedProofLength {
                count,
                index,
                expected_proof_length,
                actual_proof_length,
            } => {
                write!(
                    formatter,
                    "unexpected proof length - count: {}, index: {}, expected length: {}, actual \
                    length: {}",
                    count, index, expected_proof_length, actual_proof_length
                )
            }
        }
    }
}

#[cfg(feature = "std")]
impl StdError for MerkleVerificationError {}

/// Error validating a chunk with proof.
#[derive(Debug)]
#[non_exhaustive]
pub enum ChunkWithProofVerificationError {
    /// Indexed Merkle proof verification error.
    MerkleVerificationError(MerkleVerificationError),

    /// Empty Merkle proof for trie with chunk.
    ChunkWithProofHasEmptyMerkleProof {
        /// Chunk with empty Merkle proof.
        chunk_with_proof: ChunkWithProof,
    },
    /// Unexpected Merkle root hash.
    UnexpectedRootHash,
    /// Bytesrepr error.
    Bytesrepr(bytesrepr::Error),

    /// First digest in indexed Merkle proof did not match hash of chunk.
    FirstDigestInMerkleProofDidNotMatchHashOfChunk {
        /// First digest in indexed Merkle proof.
        first_digest_in_indexed_merkle_proof: Digest,
        /// Hash of chunk.
        hash_of_chunk: Digest,
    },
}

impl Display for ChunkWithProofVerificationError {
    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
        match self {
            ChunkWithProofVerificationError::MerkleVerificationError(error) => {
                write!(formatter, "{}", error)
            }
            ChunkWithProofVerificationError::ChunkWithProofHasEmptyMerkleProof {
                chunk_with_proof,
            } => {
                write!(
                    formatter,
                    "chunk with proof has empty merkle proof: {:?}",
                    chunk_with_proof
                )
            }
            ChunkWithProofVerificationError::UnexpectedRootHash => {
                write!(formatter, "merkle proof has an unexpected root hash")
            }
            ChunkWithProofVerificationError::Bytesrepr(error) => {
                write!(
                    formatter,
                    "bytesrepr error computing chunkable hash: {}",
                    error
                )
            }
            ChunkWithProofVerificationError::FirstDigestInMerkleProofDidNotMatchHashOfChunk {
                first_digest_in_indexed_merkle_proof,
                hash_of_chunk,
            } => {
                write!(
                    formatter,
                    "first digest in merkle proof did not match hash of chunk - first digest: \
                    {:?}, hash of chunk: {:?}",
                    first_digest_in_indexed_merkle_proof, hash_of_chunk
                )
            }
        }
    }
}

impl From<MerkleVerificationError> for ChunkWithProofVerificationError {
    fn from(error: MerkleVerificationError) -> Self {
        ChunkWithProofVerificationError::MerkleVerificationError(error)
    }
}

#[cfg(feature = "std")]
impl StdError for ChunkWithProofVerificationError {
    fn source(&self) -> Option<&(dyn StdError + 'static)> {
        match self {
            ChunkWithProofVerificationError::MerkleVerificationError(error) => Some(error),
            ChunkWithProofVerificationError::Bytesrepr(error) => Some(error),
            ChunkWithProofVerificationError::ChunkWithProofHasEmptyMerkleProof { .. }
            | ChunkWithProofVerificationError::UnexpectedRootHash
            | ChunkWithProofVerificationError::FirstDigestInMerkleProofDidNotMatchHashOfChunk {
                ..
            } => None,
        }
    }
}

/// Error during the construction of a Merkle proof.
#[derive(Debug, Eq, PartialEq, Clone)]
#[non_exhaustive]
pub enum MerkleConstructionError {
    /// Chunk index was out of bounds.
    IndexOutOfBounds {
        /// Total chunks count.
        count: u64,
        /// Requested index.
        index: u64,
    },
    /// Too many Merkle tree leaves.
    TooManyLeaves {
        /// Total chunks count.
        count: String,
    },
}

impl Display for MerkleConstructionError {
    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
        match self {
            MerkleConstructionError::IndexOutOfBounds { count, index } => {
                write!(
                    formatter,
                    "could not construct merkle proof - index out of bounds - count: {}, index: {}",
                    count, index
                )
            }
            MerkleConstructionError::TooManyLeaves { count } => {
                write!(
                    formatter,
                    "could not construct merkle proof - too many leaves - count: {}, max: {} \
                    (u64::MAX)",
                    count,
                    u64::MAX
                )
            }
        }
    }
}

#[cfg(feature = "std")]
impl StdError for MerkleConstructionError {}