From c46343f27953f4a941942fe4d9a7650e36e280fa Mon Sep 17 00:00:00 2001 From: Antioch Peverell Date: Sat, 30 Nov 2019 23:14:35 +0000 Subject: [PATCH] cleanup HeaderVersion, "newtype" no need for constructor (#3150) --- core/src/consensus.rs | 10 ++-- core/src/core/block.rs | 7 --- core/src/global.rs | 8 +-- core/tests/consensus.rs | 129 +++++++++++++--------------------------- 4 files changed, 51 insertions(+), 103 deletions(-) diff --git a/core/src/consensus.rs b/core/src/consensus.rs index 0c7a2eb13..05c56fe23 100644 --- a/core/src/consensus.rs +++ b/core/src/consensus.rs @@ -141,17 +141,17 @@ pub fn header_version(height: u64) -> HeaderVersion { match chain_type { global::ChainTypes::Floonet => { if height < FLOONET_FIRST_HARD_FORK { - (HeaderVersion::new(1)) + (HeaderVersion(1)) } else if height < FLOONET_SECOND_HARD_FORK { - (HeaderVersion::new(2)) + (HeaderVersion(2)) } else if height < 3 * HARD_FORK_INTERVAL { - (HeaderVersion::new(3)) + (HeaderVersion(3)) } else { - HeaderVersion::new(hf_interval) + HeaderVersion(hf_interval) } } // everything else just like mainnet - _ => HeaderVersion::new(hf_interval), + _ => HeaderVersion(hf_interval), } } diff --git a/core/src/core/block.rs b/core/src/core/block.rs index c99a45039..e1e70f449 100644 --- a/core/src/core/block.rs +++ b/core/src/core/block.rs @@ -185,13 +185,6 @@ impl Default for HeaderVersion { } } -impl HeaderVersion { - /// Constructor taking the provided version. - pub fn new(version: u16) -> HeaderVersion { - HeaderVersion(version) - } -} - impl From for u16 { fn from(v: HeaderVersion) -> u16 { v.0 diff --git a/core/src/global.rs b/core/src/global.rs index f45798d6b..27a2a11c5 100644 --- a/core/src/global.rs +++ b/core/src/global.rs @@ -176,20 +176,20 @@ where match chain_type { // Mainnet has Cuckaroo(d)29 for AR and Cuckatoo31+ for AF ChainTypes::Mainnet if edge_bits > 29 => new_cuckatoo_ctx(edge_bits, proof_size, max_sols), - ChainTypes::Mainnet if valid_header_version(height, HeaderVersion::new(3)) => { + ChainTypes::Mainnet if valid_header_version(height, HeaderVersion(3)) => { new_cuckaroom_ctx(edge_bits, proof_size) } - ChainTypes::Mainnet if valid_header_version(height, HeaderVersion::new(2)) => { + ChainTypes::Mainnet if valid_header_version(height, HeaderVersion(2)) => { new_cuckarood_ctx(edge_bits, proof_size) } ChainTypes::Mainnet => new_cuckaroo_ctx(edge_bits, proof_size), // Same for Floonet ChainTypes::Floonet if edge_bits > 29 => new_cuckatoo_ctx(edge_bits, proof_size, max_sols), - ChainTypes::Floonet if valid_header_version(height, HeaderVersion::new(3)) => { + ChainTypes::Floonet if valid_header_version(height, HeaderVersion(3)) => { new_cuckaroom_ctx(edge_bits, proof_size) } - ChainTypes::Floonet if valid_header_version(height, HeaderVersion::new(2)) => { + ChainTypes::Floonet if valid_header_version(height, HeaderVersion(2)) => { new_cuckarood_ctx(edge_bits, proof_size) } ChainTypes::Floonet => new_cuckaroo_ctx(edge_bits, proof_size), diff --git a/core/tests/consensus.rs b/core/tests/consensus.rs index 2f71ab230..f0a86f256 100644 --- a/core/tests/consensus.rs +++ b/core/tests/consensus.rs @@ -623,134 +623,89 @@ fn hard_forks() { { global::set_mining_mode(global::ChainTypes::Mainnet); assert_eq!(global::is_floonet(), false); - assert!(valid_header_version(0, HeaderVersion::new(1))); - assert!(valid_header_version(10, HeaderVersion::new(1))); - assert!(!valid_header_version(10, HeaderVersion::new(2))); - assert!(valid_header_version( - YEAR_HEIGHT / 2 - 1, - HeaderVersion::new(1) - )); - assert!(valid_header_version(YEAR_HEIGHT / 2, HeaderVersion::new(2))); - assert!(valid_header_version( - YEAR_HEIGHT / 2 + 1, - HeaderVersion::new(2) - )); - assert!(!valid_header_version( - YEAR_HEIGHT / 2, - HeaderVersion::new(1) - )); - assert!(!valid_header_version(YEAR_HEIGHT, HeaderVersion::new(1))); + assert!(valid_header_version(0, HeaderVersion(1))); + assert!(valid_header_version(10, HeaderVersion(1))); + assert!(!valid_header_version(10, HeaderVersion(2))); + assert!(valid_header_version(YEAR_HEIGHT / 2 - 1, HeaderVersion(1))); + assert!(valid_header_version(YEAR_HEIGHT / 2, HeaderVersion(2))); + assert!(valid_header_version(YEAR_HEIGHT / 2 + 1, HeaderVersion(2))); + assert!(!valid_header_version(YEAR_HEIGHT / 2, HeaderVersion(1))); + assert!(!valid_header_version(YEAR_HEIGHT, HeaderVersion(1))); - assert!(valid_header_version(YEAR_HEIGHT - 1, HeaderVersion::new(2))); - assert!(valid_header_version(YEAR_HEIGHT, HeaderVersion::new(3))); - assert!(valid_header_version(YEAR_HEIGHT + 1, HeaderVersion::new(3))); - assert!(!valid_header_version(YEAR_HEIGHT, HeaderVersion::new(2))); - assert!(!valid_header_version( - YEAR_HEIGHT * 3 / 2, - HeaderVersion::new(2) - )); + assert!(valid_header_version(YEAR_HEIGHT - 1, HeaderVersion(2))); + assert!(valid_header_version(YEAR_HEIGHT, HeaderVersion(3))); + assert!(valid_header_version(YEAR_HEIGHT + 1, HeaderVersion(3))); + assert!(!valid_header_version(YEAR_HEIGHT, HeaderVersion(2))); + assert!(!valid_header_version(YEAR_HEIGHT * 3 / 2, HeaderVersion(2))); // v4 not active yet - assert!(!valid_header_version( - YEAR_HEIGHT * 3 / 2, - HeaderVersion::new(4) - )); - assert!(!valid_header_version( - YEAR_HEIGHT * 3 / 2, - HeaderVersion::new(3) - )); - assert!(!valid_header_version( - YEAR_HEIGHT * 3 / 2, - HeaderVersion::new(2) - )); - assert!(!valid_header_version( - YEAR_HEIGHT * 3 / 2, - HeaderVersion::new(1) - )); - assert!(!valid_header_version( - YEAR_HEIGHT * 2, - HeaderVersion::new(3) - )); + assert!(!valid_header_version(YEAR_HEIGHT * 3 / 2, HeaderVersion(4))); + assert!(!valid_header_version(YEAR_HEIGHT * 3 / 2, HeaderVersion(3))); + assert!(!valid_header_version(YEAR_HEIGHT * 3 / 2, HeaderVersion(2))); + assert!(!valid_header_version(YEAR_HEIGHT * 3 / 2, HeaderVersion(1))); + assert!(!valid_header_version(YEAR_HEIGHT * 2, HeaderVersion(3))); assert!(!valid_header_version( YEAR_HEIGHT * 3 / 2 + 1, - HeaderVersion::new(3) + HeaderVersion(3) )); } // Tests for floonet chain type. { global::set_mining_mode(global::ChainTypes::Floonet); assert_eq!(global::is_floonet(), true); - assert!(valid_header_version(0, HeaderVersion::new(1))); - assert!(valid_header_version(10, HeaderVersion::new(1))); - assert!(!valid_header_version(10, HeaderVersion::new(2))); + assert!(valid_header_version(0, HeaderVersion(1))); + assert!(valid_header_version(10, HeaderVersion(1))); + assert!(!valid_header_version(10, HeaderVersion(2))); assert!(valid_header_version( FLOONET_FIRST_HARD_FORK - 1, - HeaderVersion::new(1) + HeaderVersion(1) )); assert!(valid_header_version( FLOONET_FIRST_HARD_FORK, - HeaderVersion::new(2) + HeaderVersion(2) )); assert!(valid_header_version( FLOONET_FIRST_HARD_FORK + 1, - HeaderVersion::new(2) + HeaderVersion(2) )); assert!(!valid_header_version( FLOONET_FIRST_HARD_FORK, - HeaderVersion::new(1) + HeaderVersion(1) )); - assert!(!valid_header_version(YEAR_HEIGHT, HeaderVersion::new(1))); + assert!(!valid_header_version(YEAR_HEIGHT, HeaderVersion(1))); assert!(valid_header_version( FLOONET_SECOND_HARD_FORK - 1, - HeaderVersion::new(2) + HeaderVersion(2) )); assert!(valid_header_version( FLOONET_SECOND_HARD_FORK, - HeaderVersion::new(3) + HeaderVersion(3) )); assert!(valid_header_version( FLOONET_SECOND_HARD_FORK + 1, - HeaderVersion::new(3) + HeaderVersion(3) )); assert!(!valid_header_version( FLOONET_SECOND_HARD_FORK, - HeaderVersion::new(2) + HeaderVersion(2) )); assert!(!valid_header_version( FLOONET_SECOND_HARD_FORK, - HeaderVersion::new(1) + HeaderVersion(1) )); - assert!(!valid_header_version( - YEAR_HEIGHT - 1, - HeaderVersion::new(2) - )); - assert!(valid_header_version(YEAR_HEIGHT - 1, HeaderVersion::new(3))); - assert!(valid_header_version(YEAR_HEIGHT, HeaderVersion::new(3))); - assert!(valid_header_version(YEAR_HEIGHT + 1, HeaderVersion::new(3))); + assert!(!valid_header_version(YEAR_HEIGHT - 1, HeaderVersion(2))); + assert!(valid_header_version(YEAR_HEIGHT - 1, HeaderVersion(3))); + assert!(valid_header_version(YEAR_HEIGHT, HeaderVersion(3))); + assert!(valid_header_version(YEAR_HEIGHT + 1, HeaderVersion(3))); // v4 not active yet - assert!(!valid_header_version( - YEAR_HEIGHT * 3 / 2, - HeaderVersion::new(4) - )); - assert!(!valid_header_version( - YEAR_HEIGHT * 3 / 2, - HeaderVersion::new(3) - )); - assert!(!valid_header_version( - YEAR_HEIGHT * 3 / 2, - HeaderVersion::new(2) - )); - assert!(!valid_header_version( - YEAR_HEIGHT * 3 / 2, - HeaderVersion::new(1) - )); - assert!(!valid_header_version( - YEAR_HEIGHT * 2, - HeaderVersion::new(3) - )); + assert!(!valid_header_version(YEAR_HEIGHT * 3 / 2, HeaderVersion(4))); + assert!(!valid_header_version(YEAR_HEIGHT * 3 / 2, HeaderVersion(3))); + assert!(!valid_header_version(YEAR_HEIGHT * 3 / 2, HeaderVersion(2))); + assert!(!valid_header_version(YEAR_HEIGHT * 3 / 2, HeaderVersion(1))); + assert!(!valid_header_version(YEAR_HEIGHT * 2, HeaderVersion(3))); assert!(!valid_header_version( YEAR_HEIGHT * 3 / 2 + 1, - HeaderVersion::new(3) + HeaderVersion(3) )); } }