use casper_types::bytesrepr;
use crate::{ChunkWithProof, Digest};
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
#[error("Incorrect digest length {0}, expected length {}.", Digest::LENGTH)]
IncorrectDigestLength(usize),
#[error("Base16 decode error {0}.")]
Base16DecodeError(base16::DecodeError),
}
#[derive(thiserror::Error, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum MerkleVerificationError {
#[error("Index out of bounds. Count: {count}, index: {index}")]
IndexOutOfBounds {
count: u64,
index: u64,
},
#[error(
"Unexpected proof length. Count: {count}, index: {index}, \
expected proof length: {expected_proof_length}, \
actual proof length: {actual_proof_length}"
)]
UnexpectedProofLength {
count: u64,
index: u64,
expected_proof_length: u8,
actual_proof_length: usize,
},
}
#[derive(thiserror::Error, Debug)]
#[non_exhaustive]
pub enum ChunkWithProofVerificationError {
#[error(transparent)]
MerkleVerificationError(#[from] MerkleVerificationError),
#[error("Chunk with proof has empty Merkle proof: {chunk_with_proof:?}")]
ChunkWithProofHasEmptyMerkleProof {
chunk_with_proof: ChunkWithProof,
},
#[error("Merkle proof has an unexpected root hash")]
UnexpectedRootHash,
#[error("Bytesrepr error computing chunkable hash: {0}")]
Bytesrepr(bytesrepr::Error),
#[error(
"First digest in Merkle proof did not match hash of chunk. \
First digest in indexed Merkle proof: {first_digest_in_indexed_merkle_proof:?}. \
Hash of chunk: {hash_of_chunk:?}."
)]
FirstDigestInMerkleProofDidNotMatchHashOfChunk {
first_digest_in_indexed_merkle_proof: Digest,
hash_of_chunk: Digest,
},
}
#[derive(thiserror::Error, Debug, Eq, PartialEq, Clone)]
#[non_exhaustive]
pub enum MerkleConstructionError {
#[error(
"Could not construct Merkle proof. Index out of bounds. Count: {count}, index: {index}"
)]
IndexOutOfBounds {
count: u64,
index: u64,
},
#[error(
"Could not construct Merkle proof. Too many leaves. Count: {count}, max: {} (u64::MAX)",
u64::MAX
)]
TooManyLeaves {
count: String,
},
}