use std::cmp::Ordering;
use zebra_chain::block;
use Progress::*;
use TargetHeight::*;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Progress<HeightOrHash> {
BeforeGenesis,
InitialTip(HeightOrHash),
PreviousCheckpoint(HeightOrHash),
FinalCheckpoint,
}
impl Ord for Progress<block::Height> {
fn cmp(&self, other: &Self) -> Ordering {
if self == other {
return Ordering::Equal;
}
match (self, other) {
(BeforeGenesis, _) => Ordering::Less,
(_, BeforeGenesis) => Ordering::Greater,
(FinalCheckpoint, _) => Ordering::Greater,
(_, FinalCheckpoint) => Ordering::Less,
(InitialTip(self_height), InitialTip(other_height))
| (InitialTip(self_height), PreviousCheckpoint(other_height))
| (PreviousCheckpoint(self_height), InitialTip(other_height))
| (PreviousCheckpoint(self_height), PreviousCheckpoint(other_height)) => {
self_height.cmp(other_height)
}
}
}
}
impl PartialOrd for Progress<block::Height> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Progress<block::Height> {
pub fn height(&self) -> Option<block::Height> {
match self {
BeforeGenesis => None,
InitialTip(height) => Some(*height),
PreviousCheckpoint(height) => Some(*height),
FinalCheckpoint => None,
}
}
}
impl<HeightOrHash> Progress<HeightOrHash> {
#[allow(dead_code)]
pub fn is_before_genesis(&self) -> bool {
matches!(self, BeforeGenesis)
}
pub fn is_final_checkpoint(&self) -> bool {
matches!(self, FinalCheckpoint)
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum TargetHeight {
WaitingForBlocks,
Checkpoint(block::Height),
FinishedVerifying,
}
impl PartialOrd for TargetHeight {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
match (self, other) {
(FinishedVerifying, FinishedVerifying) => Some(Ordering::Equal),
(FinishedVerifying, _) => Some(Ordering::Greater),
(_, FinishedVerifying) => Some(Ordering::Less),
(Checkpoint(self_height), Checkpoint(other_height)) => {
self_height.partial_cmp(other_height)
}
(WaitingForBlocks, Checkpoint(_)) => None,
(Checkpoint(_), WaitingForBlocks) => None,
(WaitingForBlocks, WaitingForBlocks) => Some(Ordering::Equal),
}
}
}