use super::Severity;
use thiserror::Error;
#[derive(Error, Debug, PartialEq, Eq)]
pub enum VdfRejectReason {
#[error("proof bytes are malformed")]
MalformedProof,
#[error("proof does not match the challenge")]
ChallengeMismatch,
#[error("VDF engine error: {0}")]
EngineError(String),
#[error("discriminant creation failed")]
DiscriminantFailed,
}
#[derive(Error, Debug, PartialEq, Eq)]
pub enum VdfError {
#[error("Failed to create VDF lock file: {0}")]
LockFileError(String),
#[error("Failed to acquire VDF lock: {0}")]
LockAcquireError(String),
#[error("Failed to create VDF discriminant")]
DiscriminantError,
#[error("Failed to generate VDF proof")]
ProofGenerationError,
#[error("VDF operation is unsupported on this platform")]
UnsupportedPlatform,
#[error("VDF proof is structurally invalid or too large")]
InvalidProof,
}
impl VdfError {
pub fn code(&self) -> &'static str {
match self {
Self::LockFileError(_) => "KIN-VDF-001",
Self::LockAcquireError(_) => "KIN-VDF-002",
Self::DiscriminantError => "KIN-VDF-003",
Self::ProofGenerationError => "KIN-VDF-004",
Self::UnsupportedPlatform => "KIN-VDF-005",
Self::InvalidProof => "KIN-VDF-006",
}
}
pub fn error_type_uri(&self) -> String {
format!("{}/errors/{}", crate::constants::DOCS_URL, self.code())
}
pub fn severity(&self) -> Severity {
match self {
Self::LockFileError(_) | Self::LockAcquireError(_) => Severity::Error,
Self::DiscriminantError | Self::ProofGenerationError | Self::InvalidProof => {
Severity::Error
}
Self::UnsupportedPlatform => Severity::Critical,
}
}
pub fn is_retryable(&self) -> bool {
matches!(self, Self::LockAcquireError(_))
}
pub fn user_message(&self) -> String {
match self {
Self::LockFileError(_) => "Failed to create VDF lock file.".to_string(),
Self::LockAcquireError(_) => "Failed to acquire VDF lock.".to_string(),
Self::DiscriminantError => "Failed to create VDF discriminant.".to_string(),
Self::ProofGenerationError => "Failed to generate VDF proof.".to_string(),
Self::UnsupportedPlatform => {
"VDF operations are not supported on this platform.".to_string()
}
Self::InvalidProof => "The VDF proof is structurally invalid or too large.".to_string(),
}
}
}