2020-10-09 01:37:24 -07:00
|
|
|
use std::sync::Arc;
|
2020-12-02 20:48:34 -08:00
|
|
|
|
|
|
|
use chrono::{DateTime, Utc};
|
2020-10-09 01:37:24 -07:00
|
|
|
use thiserror::Error;
|
|
|
|
|
2020-12-02 20:48:34 -08:00
|
|
|
use zebra_chain::{block, work::difficulty::CompactDifficulty};
|
|
|
|
|
2020-10-09 01:37:24 -07:00
|
|
|
/// A wrapper for type erased errors that is itself clonable and implements the
|
|
|
|
/// Error trait
|
|
|
|
#[derive(Debug, Error, Clone)]
|
|
|
|
#[error(transparent)]
|
|
|
|
pub struct CloneError {
|
|
|
|
source: Arc<dyn std::error::Error + Send + Sync + 'static>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<CommitBlockError> for CloneError {
|
|
|
|
fn from(source: CommitBlockError) -> Self {
|
|
|
|
let source = Arc::new(source);
|
|
|
|
Self { source }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<BoxError> for CloneError {
|
|
|
|
fn from(source: BoxError) -> Self {
|
|
|
|
let source = Arc::from(source);
|
|
|
|
Self { source }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A boxed [`std::error::Error`].
|
|
|
|
pub type BoxError = Box<dyn std::error::Error + Send + Sync + 'static>;
|
|
|
|
|
|
|
|
/// An error describing the reason a block could not be committed to the state.
|
|
|
|
#[derive(Debug, Error)]
|
|
|
|
#[error("block is not contextually valid")]
|
|
|
|
pub struct CommitBlockError(#[from] ValidateContextError);
|
|
|
|
|
|
|
|
/// An error describing why a block failed contextual validation.
|
|
|
|
#[derive(displaydoc::Display, Debug, Error)]
|
|
|
|
#[non_exhaustive]
|
2020-12-02 20:48:34 -08:00
|
|
|
#[allow(missing_docs)]
|
2020-10-09 01:37:24 -07:00
|
|
|
pub enum ValidateContextError {
|
2020-12-02 20:48:34 -08:00
|
|
|
/// block height {candidate_height:?} is lower than the current finalized height {finalized_tip_height:?}
|
2020-10-09 01:37:24 -07:00
|
|
|
#[non_exhaustive]
|
2020-12-02 20:48:34 -08:00
|
|
|
OrphanedBlock {
|
|
|
|
candidate_height: block::Height,
|
|
|
|
finalized_tip_height: block::Height,
|
|
|
|
},
|
2020-11-12 20:26:16 -08:00
|
|
|
|
2020-12-02 20:48:34 -08:00
|
|
|
/// block height {candidate_height:?} is not one greater than its parent block's height {parent_height:?}
|
2020-11-12 20:26:16 -08:00
|
|
|
#[non_exhaustive]
|
2020-12-02 20:48:34 -08:00
|
|
|
NonSequentialBlock {
|
|
|
|
candidate_height: block::Height,
|
|
|
|
parent_height: block::Height,
|
|
|
|
},
|
2020-11-19 00:05:48 -08:00
|
|
|
|
2020-12-02 20:48:34 -08:00
|
|
|
/// block time {candidate_time:?} is less than or equal to the median-time-past for the block {median_time_past:?}
|
2020-12-02 20:28:42 -08:00
|
|
|
#[non_exhaustive]
|
2020-12-02 20:48:34 -08:00
|
|
|
TimeTooEarly {
|
|
|
|
candidate_time: DateTime<Utc>,
|
|
|
|
median_time_past: DateTime<Utc>,
|
|
|
|
},
|
2020-12-02 20:28:42 -08:00
|
|
|
|
2020-12-02 20:48:34 -08:00
|
|
|
/// block time {candidate_time:?} is greater than the median-time-past for the block plus 90 minutes {block_time_max:?}
|
2020-12-02 20:28:42 -08:00
|
|
|
#[non_exhaustive]
|
2020-12-02 20:48:34 -08:00
|
|
|
TimeTooLate {
|
|
|
|
candidate_time: DateTime<Utc>,
|
|
|
|
block_time_max: DateTime<Utc>,
|
|
|
|
},
|
2020-12-02 20:28:42 -08:00
|
|
|
|
2020-12-02 20:48:34 -08:00
|
|
|
/// block difficulty threshold {difficulty_threshold:?} is not equal to the expected difficulty for the block {expected_difficulty:?}
|
2020-11-19 00:05:48 -08:00
|
|
|
#[non_exhaustive]
|
2020-12-02 20:48:34 -08:00
|
|
|
InvalidDifficultyThreshold {
|
|
|
|
difficulty_threshold: CompactDifficulty,
|
|
|
|
expected_difficulty: CompactDifficulty,
|
|
|
|
},
|
2020-10-09 01:37:24 -07:00
|
|
|
}
|