use core::fmt;
use eth_valkyoth_codec::DecodeError;
use crate::mpt::MptNodeDecodeError;
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum MptProofVerificationError {
KeyEncode(DecodeError),
MalformedNode(MptNodeDecodeError),
MissingProofNode,
WrongRoot,
Absent,
ValueMismatch,
TrailingProofNodes,
ProofTooDeep,
}
impl MptProofVerificationError {
#[must_use]
pub const fn code(self) -> &'static str {
match self {
Self::KeyEncode(error) => error.code(),
Self::MalformedNode(error) => error.code(),
Self::MissingProofNode => "ETH_MPT_PROOF_MISSING_NODE",
Self::WrongRoot => "ETH_MPT_PROOF_WRONG_ROOT",
Self::Absent => "ETH_MPT_PROOF_ABSENT",
Self::ValueMismatch => "ETH_MPT_PROOF_VALUE_MISMATCH",
Self::TrailingProofNodes => "ETH_MPT_PROOF_TRAILING_NODES",
Self::ProofTooDeep => "ETH_MPT_PROOF_TOO_DEEP",
}
}
#[must_use]
pub const fn message(self) -> &'static str {
match self {
Self::KeyEncode(_) => "MPT proof key encoding failed",
Self::MalformedNode(_) => "MPT proof node is malformed",
Self::MissingProofNode => "MPT proof is missing a required node",
Self::WrongRoot => "MPT proof node hash does not match the trusted root",
Self::Absent => "MPT proof does not contain the requested key",
Self::ValueMismatch => "MPT proof value does not match the expected value",
Self::TrailingProofNodes => "MPT proof contains unused trailing nodes",
Self::ProofTooDeep => "MPT proof traversal depth limit exceeded",
}
}
#[must_use]
pub const fn category(self) -> MptProofVerificationErrorCategory {
match self {
Self::KeyEncode(_)
| Self::MalformedNode(_)
| Self::MissingProofNode
| Self::ProofTooDeep => MptProofVerificationErrorCategory::Malformed,
Self::WrongRoot | Self::ValueMismatch | Self::TrailingProofNodes => {
MptProofVerificationErrorCategory::WrongRoot
}
Self::Absent => MptProofVerificationErrorCategory::Absent,
}
}
}
impl fmt::Display for MptProofVerificationError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(self.message())
}
}
#[cfg(feature = "std")]
impl std::error::Error for MptProofVerificationError {}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum MptProofVerificationErrorCategory {
Malformed,
Absent,
WrongRoot,
}