use chia_protocol::Bytes32;
use crate::storage::StorageError;
use crate::types::CoinId;
#[derive(Debug, Clone, PartialEq, thiserror::Error)]
pub enum CoinStoreError {
#[error("block height {got} does not follow current height {expected}")]
HeightMismatch { expected: u64, got: u64 },
#[error("parent hash mismatch: expected {expected:?}, got {got:?}")]
ParentHashMismatch { expected: Bytes32, got: Bytes32 },
#[error("state root mismatch: expected {expected:?}, computed {computed:?}")]
StateRootMismatch {
expected: Bytes32,
computed: Bytes32,
},
#[error("coin not found: {0:?}")]
CoinNotFound(CoinId),
#[error("coin already exists: {0:?}")]
CoinAlreadyExists(CoinId),
#[error("double spend: coin {0:?} already spent")]
DoubleSpend(CoinId),
#[error("spend count mismatch: expected {expected} updates, got {actual}")]
SpendCountMismatch { expected: usize, actual: usize },
#[error("invalid reward coin count: expected {expected}, got {got}")]
InvalidRewardCoinCount { expected: String, got: usize },
#[error("hint too long: {length} bytes exceeds maximum {max}")]
HintTooLong { length: usize, max: usize },
#[error("genesis already initialized")]
GenesisAlreadyInitialized,
#[error("coinstate not initialized (call init_genesis first)")]
NotInitialized,
#[error("cannot rollback: target height {target} above current height {current}")]
RollbackAboveTip { target: i64, current: u64 },
#[error("puzzle hash batch size {size} exceeds maximum {max}")]
PuzzleHashBatchTooLarge { size: usize, max: usize },
#[error("storage error: {0}")]
StorageError(String),
#[error("serialization error: {0}")]
SerializationError(String),
#[error("deserialization error: {0}")]
DeserializationError(String),
}
impl CoinStoreError {
pub fn from_bincode_deserialize(err: bincode::Error) -> Self {
CoinStoreError::DeserializationError(err.to_string())
}
}
impl From<StorageError> for CoinStoreError {
fn from(err: StorageError) -> Self {
CoinStoreError::StorageError(err.to_string())
}
}
impl From<crate::hints::HintError> for CoinStoreError {
fn from(err: crate::hints::HintError) -> Self {
match err {
crate::hints::HintError::HintTooLong { length, max } => {
CoinStoreError::HintTooLong { length, max }
}
}
}
}
impl From<bincode::Error> for CoinStoreError {
fn from(err: bincode::Error) -> Self {
CoinStoreError::SerializationError(err.to_string())
}
}
#[cfg(feature = "lmdb-storage")]
impl From<heed::Error> for CoinStoreError {
fn from(err: heed::Error) -> Self {
CoinStoreError::StorageError(err.to_string())
}
}
#[cfg(feature = "rocksdb-storage")]
impl From<rocksdb::Error> for CoinStoreError {
fn from(err: rocksdb::Error) -> Self {
CoinStoreError::StorageError(err.to_string())
}
}