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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use super::cstruct;
use crate::chaintypes::ConsensusType;
use std::num::NonZeroUsize;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AnyBlockVersion {
    Supported(BlockVersion),
    Unsupported(u8),
}

impl AnyBlockVersion {
    pub fn try_into_block_version(self) -> Option<BlockVersion> {
        match self {
            AnyBlockVersion::Supported(version) => Some(version),
            AnyBlockVersion::Unsupported(_) => None,
        }
    }
}

impl PartialEq<BlockVersion> for AnyBlockVersion {
    fn eq(&self, other: &BlockVersion) -> bool {
        match self {
            AnyBlockVersion::Supported(version) => version == other,
            AnyBlockVersion::Unsupported(_) => false,
        }
    }
}

impl From<u8> for AnyBlockVersion {
    fn from(n: u8) -> Self {
        match BlockVersion::from_u8(n) {
            Some(supported) => AnyBlockVersion::Supported(supported),
            None => AnyBlockVersion::Unsupported(n),
        }
    }
}

impl From<AnyBlockVersion> for u8 {
    fn from(block_version: AnyBlockVersion) -> u8 {
        match block_version {
            AnyBlockVersion::Supported(version) => version as u8,
            AnyBlockVersion::Unsupported(n) => n,
        }
    }
}

impl From<BlockVersion> for AnyBlockVersion {
    fn from(version: BlockVersion) -> Self {
        AnyBlockVersion::Supported(version)
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BlockVersion {
    Genesis,
    Ed25519Signed,
    KesVrfproof,
}

impl BlockVersion {
    pub fn from_u8(v: u8) -> Option<Self> {
        match v {
            cstruct::VERSION_UNSIGNED => Some(BlockVersion::Genesis),
            cstruct::VERSION_BFT => Some(BlockVersion::Ed25519Signed),
            cstruct::VERSION_GP => Some(BlockVersion::KesVrfproof),
            _ => None,
        }
    }

    pub fn to_u8(self) -> u8 {
        match self {
            BlockVersion::Genesis => cstruct::VERSION_UNSIGNED,
            BlockVersion::Ed25519Signed => cstruct::VERSION_BFT,
            BlockVersion::KesVrfproof => cstruct::VERSION_GP,
        }
    }

    pub const fn get_size(self) -> NonZeroUsize {
        const SIZE: [NonZeroUsize; 3] = [
            unsafe { NonZeroUsize::new_unchecked(cstruct::HEADER_COMMON_SIZE) },
            unsafe { NonZeroUsize::new_unchecked(cstruct::HEADER_BFT_SIZE) },
            unsafe { NonZeroUsize::new_unchecked(cstruct::HEADER_GP_SIZE) },
        ];
        SIZE[self as usize]
    }

    pub const fn get_auth_size(self) -> NonZeroUsize {
        const SIZE: [NonZeroUsize; 3] = [
            unsafe { NonZeroUsize::new_unchecked(cstruct::HEADER_COMMON_SIZE) },
            unsafe { NonZeroUsize::new_unchecked(cstruct::HEADER_BFT_AUTHED_SIZE) },
            unsafe { NonZeroUsize::new_unchecked(cstruct::HEADER_GP_AUTHED_SIZE) },
        ];
        SIZE[self as usize]
    }

    pub fn to_consensus_type(self) -> Option<ConsensusType> {
        match self {
            BlockVersion::Genesis => None,
            BlockVersion::Ed25519Signed => Some(ConsensusType::Bft),
            BlockVersion::KesVrfproof => Some(ConsensusType::GenesisPraos),
        }
    }
}

#[cfg(test)]
mod tests {

    use crate::chaintypes::ConsensusType;
    use crate::header::{AnyBlockVersion, BlockVersion};
    use quickcheck::TestResult;
    use quickcheck_macros::quickcheck;

    #[test]
    pub fn try_into_block_version() {
        assert_eq!(
            AnyBlockVersion::Supported(BlockVersion::Genesis).try_into_block_version(),
            Some(BlockVersion::Genesis)
        );
        assert_eq!(
            AnyBlockVersion::Supported(BlockVersion::Ed25519Signed).try_into_block_version(),
            Some(BlockVersion::Ed25519Signed)
        );
        assert_eq!(
            AnyBlockVersion::Supported(BlockVersion::KesVrfproof).try_into_block_version(),
            Some(BlockVersion::KesVrfproof)
        );
        assert_eq!(
            AnyBlockVersion::Unsupported(0).try_into_block_version(),
            None
        );
    }

    #[test]
    pub fn equality() {
        assert_eq!(
            AnyBlockVersion::Supported(BlockVersion::Genesis),
            BlockVersion::Genesis
        );
        assert_eq!(
            AnyBlockVersion::Supported(BlockVersion::Ed25519Signed),
            BlockVersion::Ed25519Signed
        );
        assert_eq!(
            AnyBlockVersion::Supported(BlockVersion::KesVrfproof),
            BlockVersion::KesVrfproof
        );
        assert!(AnyBlockVersion::Unsupported(0) != BlockVersion::KesVrfproof);
        assert!(AnyBlockVersion::Unsupported(0) != BlockVersion::Ed25519Signed);
        assert!(AnyBlockVersion::Unsupported(0) != BlockVersion::KesVrfproof);
    }

    #[quickcheck]
    pub fn conversion_u8(block_version: AnyBlockVersion) -> TestResult {
        let bytes: u8 = block_version.into();
        let new_block_version: AnyBlockVersion = AnyBlockVersion::from(bytes);
        TestResult::from_bool(block_version == new_block_version)
    }

    #[quickcheck]
    pub fn from_block_version(block_version: BlockVersion) -> TestResult {
        let right_version = AnyBlockVersion::Supported(block_version);
        let left_version: AnyBlockVersion = block_version.into();
        TestResult::from_bool(left_version == right_version)
    }

    #[test]
    pub fn to_consensus_type() {
        assert_eq!(BlockVersion::Genesis.to_consensus_type(), None);
        assert_eq!(
            BlockVersion::Ed25519Signed.to_consensus_type(),
            Some(ConsensusType::Bft)
        );
        assert_eq!(
            BlockVersion::KesVrfproof.to_consensus_type(),
            Some(ConsensusType::GenesisPraos)
        );
    }
}