use alloc::string::String;
use thiserror::Error;
use crate::{
Word,
merkle::smt::{LeafIndex, MAX_LEAF_ENTRIES, SMT_DEPTH},
};
#[derive(Debug, Error)]
pub enum SmtLeafError {
#[error(
"multiple leaf requires all keys to map to the same leaf index but key1 {key_1} and key2 {key_2} map to different indices"
)]
InconsistentMultipleLeafKeys { key_1: Word, key_2: Word },
#[error(
"single leaf key {key} maps to leaf {actual_leaf_index} but was expected to map to leaf {expected_leaf_index}"
)]
InconsistentSingleLeafIndices {
key: Word,
expected_leaf_index: LeafIndex<SMT_DEPTH>,
actual_leaf_index: LeafIndex<SMT_DEPTH>,
},
#[error(
"supplied leaf index {leaf_index_supplied:?} does not match {leaf_index_from_keys:?} for multiple leaf"
)]
InconsistentMultipleLeafIndices {
leaf_index_from_keys: LeafIndex<SMT_DEPTH>,
leaf_index_supplied: LeafIndex<SMT_DEPTH>,
},
#[error("multiple leaf requires at least two entries but only {0} were given")]
MultipleLeafRequiresTwoEntries(usize),
#[error(
"multiple leaf contains {actual} entries but the maximum allowed is {MAX_LEAF_ENTRIES}"
)]
TooManyLeafEntries { actual: usize },
#[error("decoding error: {0}")]
DecodingError(String),
}
#[derive(Debug, Error, PartialEq, Eq)]
pub enum SmtProofError {
#[error("merkle path length {0} does not match SMT depth {SMT_DEPTH}")]
InvalidMerklePathLength(usize),
#[error("key maps to a different leaf index than the proof")]
InvalidKeyForProof,
#[error("value mismatch: expected {expected}, got {actual}")]
ValueMismatch { expected: Word, actual: Word },
#[error("expected merkle root {expected_root} found {actual_root}")]
ConflictingRoots { expected_root: Word, actual_root: Word },
#[error("key-value pair exists in the tree: key {key}, value {value}")]
ValuePresent { key: Word, value: Word },
}