casper_hashing/
error.rs

1//! Errors in constructing and validating indexed Merkle proofs, chunks with indexed Merkle proofs.
2use casper_types::bytesrepr;
3
4use crate::{ChunkWithProof, Digest};
5
6/// Possible hashing errors.
7#[derive(Debug, thiserror::Error)]
8#[non_exhaustive]
9pub enum Error {
10    #[error("Incorrect digest length {0}, expected length {}.", Digest::LENGTH)]
11    /// The digest length was an incorrect size.
12    IncorrectDigestLength(usize),
13    /// There was a decoding error.
14    #[error("Base16 decode error {0}.")]
15    Base16DecodeError(base16::DecodeError),
16}
17
18/// Error validating a Merkle proof of a chunk.
19#[derive(thiserror::Error, Debug, PartialEq, Eq)]
20#[non_exhaustive]
21pub enum MerkleVerificationError {
22    /// Index out of bounds.
23    #[error("Index out of bounds. Count: {count}, index: {index}")]
24    IndexOutOfBounds {
25        /// Count.
26        count: u64,
27        /// Index.
28        index: u64,
29    },
30
31    /// Unexpected proof length.
32    #[error(
33        "Unexpected proof length. Count: {count}, index: {index}, \
34         expected proof length: {expected_proof_length}, \
35         actual proof length: {actual_proof_length}"
36    )]
37    UnexpectedProofLength {
38        /// Count.
39        count: u64,
40        /// Index.
41        index: u64,
42        /// Expected proof length.
43        expected_proof_length: u8,
44        /// Actual proof length.
45        actual_proof_length: usize,
46    },
47}
48
49/// Error validating a chunk with proof.
50#[derive(thiserror::Error, Debug)]
51#[non_exhaustive]
52pub enum ChunkWithProofVerificationError {
53    /// Indexed Merkle proof verification error.
54    #[error(transparent)]
55    MerkleVerificationError(#[from] MerkleVerificationError),
56
57    /// Empty Merkle proof for trie with chunk.
58    #[error("Chunk with proof has empty Merkle proof: {chunk_with_proof:?}")]
59    ChunkWithProofHasEmptyMerkleProof {
60        /// Chunk with empty Merkle proof.
61        chunk_with_proof: ChunkWithProof,
62    },
63    /// Unexpected Merkle root hash.
64    #[error("Merkle proof has an unexpected root hash")]
65    UnexpectedRootHash,
66    /// Bytesrepr error.
67    #[error("Bytesrepr error computing chunkable hash: {0}")]
68    Bytesrepr(bytesrepr::Error),
69
70    /// First digest in indexed Merkle proof did not match hash of chunk.
71    #[error(
72        "First digest in Merkle proof did not match hash of chunk. \
73         First digest in indexed Merkle proof: {first_digest_in_indexed_merkle_proof:?}. \
74         Hash of chunk: {hash_of_chunk:?}."
75    )]
76    FirstDigestInMerkleProofDidNotMatchHashOfChunk {
77        /// First digest in indexed Merkle proof.
78        first_digest_in_indexed_merkle_proof: Digest,
79        /// Hash of chunk.
80        hash_of_chunk: Digest,
81    },
82}
83
84/// Error during the construction of a Merkle proof.
85#[derive(thiserror::Error, Debug, Eq, PartialEq, Clone)]
86#[non_exhaustive]
87pub enum MerkleConstructionError {
88    /// Chunk index was out of bounds.
89    #[error(
90        "Could not construct Merkle proof. Index out of bounds. Count: {count}, index: {index}"
91    )]
92    IndexOutOfBounds {
93        /// Total chunks count.
94        count: u64,
95        /// Requested index.
96        index: u64,
97    },
98    /// Too many Merkle tree leaves.
99    #[error(
100        "Could not construct Merkle proof. Too many leaves. Count: {count}, max: {} (u64::MAX)",
101        u64::MAX
102    )]
103    TooManyLeaves {
104        /// Total chunks count.
105        count: String,
106    },
107}