1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//! Shared Types between ledger and chain
//!
//! Common types that are found in the header but are used or stored in the
//! ledger for either validation or some state tracking

use crate::key::Hash;
use strum_macros::{Display, EnumString, IntoStaticStr};

pub type HeaderId = Hash; // TODO: change to DigestOf<Blake2b256, Header>

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ChainLength(pub(crate) u32);

impl From<u32> for ChainLength {
    fn from(n: u32) -> ChainLength {
        ChainLength(n)
    }
}

impl From<ChainLength> for u32 {
    fn from(chain_length: ChainLength) -> u32 {
        chain_length.0
    }
}

impl ChainLength {
    #[inline]
    #[must_use = "this returns the result of the operation, without modifying the original"]
    pub fn increase(self) -> Self {
        ChainLength(self.0.checked_add(1).unwrap())
    }

    #[inline]
    #[must_use = "this returns the result of the operation, without modifying the original"]
    pub fn nth_ancestor(self, depth: u32) -> Option<ChainLength> {
        self.0.checked_sub(depth).map(ChainLength)
    }
}

impl std::fmt::Display for ChainLength {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

#[derive(
    Debug, Clone, Copy, Display, EnumString, IntoStaticStr, PartialEq, Eq, PartialOrd, Ord, Hash,
)]
pub enum ConsensusType {
    #[strum(to_string = "bft")]
    Bft = 1,
    #[strum(to_string = "genesis")]
    GenesisPraos = 2,
}

// alias for old name
pub type ConsensusVersion = ConsensusType;

impl ConsensusType {
    pub fn from_u16(v: u16) -> Option<Self> {
        match v {
            1 => Some(ConsensusType::Bft),
            2 => Some(ConsensusType::GenesisPraos),
            _ => None,
        }
    }
}

#[cfg(any(test, feature = "property-test-api"))]
mod tests {
    use super::*;
    use quickcheck::{Arbitrary, Gen};

    impl Arbitrary for ConsensusType {
        fn arbitrary<G: Gen>(g: &mut G) -> Self {
            ConsensusType::from_u16(u16::arbitrary(g) % 2 + 1).unwrap()
        }
    }

    impl Arbitrary for ChainLength {
        fn arbitrary<G: Gen>(g: &mut G) -> Self {
            ChainLength(Arbitrary::arbitrary(g))
        }
    }
}