impl TryFrom<u32> for BranchId

This commit is contained in:
Jack Grigg 2019-11-27 13:12:28 +00:00
parent cd326f2b6a
commit e6a8630b35
No known key found for this signature in database
GPG Key ID: 9E8255172BBF9898
1 changed files with 25 additions and 1 deletions

View File

@ -1,5 +1,6 @@
//! Consensus parameters.
use std::convert::TryFrom;
use std::fmt;
/// Zcash consensus parameters.
@ -128,6 +129,21 @@ pub enum BranchId {
Heartwood,
}
impl TryFrom<u32> for BranchId {
type Error = &'static str;
fn try_from(value: u32) -> Result<Self, Self::Error> {
match value {
0 => Ok(BranchId::Sprout),
0x5ba8_1b19 => Ok(BranchId::Overwinter),
0x76b8_09bb => Ok(BranchId::Sapling),
0x2bb4_0e60 => Ok(BranchId::Blossom),
0xf5b9_230b => Ok(BranchId::Heartwood),
_ => Err("Unknown consensus branch ID"),
}
}
}
impl From<BranchId> for u32 {
fn from(consensus_branch_id: BranchId) -> u32 {
match consensus_branch_id {
@ -159,7 +175,9 @@ impl BranchId {
#[cfg(test)]
mod tests {
use super::{BranchId, Parameters, MainNetwork, NetworkUpgrade, UPGRADES_IN_ORDER};
use std::convert::TryFrom;
use super::{BranchId, MainNetwork, NetworkUpgrade, Parameters, UPGRADES_IN_ORDER};
#[test]
fn nu_ordering() {
@ -194,6 +212,12 @@ mod tests {
));
}
#[test]
fn branch_id_from_u32() {
assert_eq!(BranchId::try_from(0), Ok(BranchId::Sprout));
assert!(BranchId::try_from(1).is_err());
}
#[test]
fn branch_id_for_height() {
assert_eq!(BranchId::for_height::<MainNetwork>(0), BranchId::Sprout,);