cleanup HeaderVersion, "newtype" no need for constructor (#3150)

This commit is contained in:
Antioch Peverell 2019-11-30 23:14:35 +00:00 committed by GitHub
parent 869cfd2b82
commit c46343f279
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 103 deletions

View file

@ -141,17 +141,17 @@ pub fn header_version(height: u64) -> HeaderVersion {
match chain_type { match chain_type {
global::ChainTypes::Floonet => { global::ChainTypes::Floonet => {
if height < FLOONET_FIRST_HARD_FORK { if height < FLOONET_FIRST_HARD_FORK {
(HeaderVersion::new(1)) (HeaderVersion(1))
} else if height < FLOONET_SECOND_HARD_FORK { } else if height < FLOONET_SECOND_HARD_FORK {
(HeaderVersion::new(2)) (HeaderVersion(2))
} else if height < 3 * HARD_FORK_INTERVAL { } else if height < 3 * HARD_FORK_INTERVAL {
(HeaderVersion::new(3)) (HeaderVersion(3))
} else { } else {
HeaderVersion::new(hf_interval) HeaderVersion(hf_interval)
} }
} }
// everything else just like mainnet // everything else just like mainnet
_ => HeaderVersion::new(hf_interval), _ => HeaderVersion(hf_interval),
} }
} }

View file

@ -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<HeaderVersion> for u16 { impl From<HeaderVersion> for u16 {
fn from(v: HeaderVersion) -> u16 { fn from(v: HeaderVersion) -> u16 {
v.0 v.0

View file

@ -176,20 +176,20 @@ where
match chain_type { match chain_type {
// Mainnet has Cuckaroo(d)29 for AR and Cuckatoo31+ for AF // 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 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) 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) new_cuckarood_ctx(edge_bits, proof_size)
} }
ChainTypes::Mainnet => new_cuckaroo_ctx(edge_bits, proof_size), ChainTypes::Mainnet => new_cuckaroo_ctx(edge_bits, proof_size),
// Same for Floonet // Same for Floonet
ChainTypes::Floonet if edge_bits > 29 => new_cuckatoo_ctx(edge_bits, proof_size, max_sols), 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) 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) new_cuckarood_ctx(edge_bits, proof_size)
} }
ChainTypes::Floonet => new_cuckaroo_ctx(edge_bits, proof_size), ChainTypes::Floonet => new_cuckaroo_ctx(edge_bits, proof_size),

View file

@ -623,134 +623,89 @@ fn hard_forks() {
{ {
global::set_mining_mode(global::ChainTypes::Mainnet); global::set_mining_mode(global::ChainTypes::Mainnet);
assert_eq!(global::is_floonet(), false); assert_eq!(global::is_floonet(), false);
assert!(valid_header_version(0, HeaderVersion::new(1))); assert!(valid_header_version(0, HeaderVersion(1)));
assert!(valid_header_version(10, HeaderVersion::new(1))); assert!(valid_header_version(10, HeaderVersion(1)));
assert!(!valid_header_version(10, HeaderVersion::new(2))); assert!(!valid_header_version(10, HeaderVersion(2)));
assert!(valid_header_version( assert!(valid_header_version(YEAR_HEIGHT / 2 - 1, HeaderVersion(1)));
YEAR_HEIGHT / 2 - 1, assert!(valid_header_version(YEAR_HEIGHT / 2, HeaderVersion(2)));
HeaderVersion::new(1) 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 / 2, HeaderVersion::new(2))); assert!(!valid_header_version(YEAR_HEIGHT, HeaderVersion(1)));
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(YEAR_HEIGHT - 1, HeaderVersion::new(2))); assert!(valid_header_version(YEAR_HEIGHT - 1, HeaderVersion(2)));
assert!(valid_header_version(YEAR_HEIGHT, HeaderVersion::new(3))); assert!(valid_header_version(YEAR_HEIGHT, HeaderVersion(3)));
assert!(valid_header_version(YEAR_HEIGHT + 1, HeaderVersion::new(3))); assert!(valid_header_version(YEAR_HEIGHT + 1, HeaderVersion(3)));
assert!(!valid_header_version(YEAR_HEIGHT, HeaderVersion::new(2))); assert!(!valid_header_version(YEAR_HEIGHT, HeaderVersion(2)));
assert!(!valid_header_version( assert!(!valid_header_version(YEAR_HEIGHT * 3 / 2, HeaderVersion(2)));
YEAR_HEIGHT * 3 / 2,
HeaderVersion::new(2)
));
// v4 not active yet // v4 not active yet
assert!(!valid_header_version( assert!(!valid_header_version(YEAR_HEIGHT * 3 / 2, HeaderVersion(4)));
YEAR_HEIGHT * 3 / 2, assert!(!valid_header_version(YEAR_HEIGHT * 3 / 2, HeaderVersion(3)));
HeaderVersion::new(4) assert!(!valid_header_version(YEAR_HEIGHT * 3 / 2, HeaderVersion(2)));
)); assert!(!valid_header_version(YEAR_HEIGHT * 3 / 2, HeaderVersion(1)));
assert!(!valid_header_version( assert!(!valid_header_version(YEAR_HEIGHT * 2, HeaderVersion(3)));
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( assert!(!valid_header_version(
YEAR_HEIGHT * 3 / 2 + 1, YEAR_HEIGHT * 3 / 2 + 1,
HeaderVersion::new(3) HeaderVersion(3)
)); ));
} }
// Tests for floonet chain type. // Tests for floonet chain type.
{ {
global::set_mining_mode(global::ChainTypes::Floonet); global::set_mining_mode(global::ChainTypes::Floonet);
assert_eq!(global::is_floonet(), true); assert_eq!(global::is_floonet(), true);
assert!(valid_header_version(0, HeaderVersion::new(1))); assert!(valid_header_version(0, HeaderVersion(1)));
assert!(valid_header_version(10, HeaderVersion::new(1))); assert!(valid_header_version(10, HeaderVersion(1)));
assert!(!valid_header_version(10, HeaderVersion::new(2))); assert!(!valid_header_version(10, HeaderVersion(2)));
assert!(valid_header_version( assert!(valid_header_version(
FLOONET_FIRST_HARD_FORK - 1, FLOONET_FIRST_HARD_FORK - 1,
HeaderVersion::new(1) HeaderVersion(1)
)); ));
assert!(valid_header_version( assert!(valid_header_version(
FLOONET_FIRST_HARD_FORK, FLOONET_FIRST_HARD_FORK,
HeaderVersion::new(2) HeaderVersion(2)
)); ));
assert!(valid_header_version( assert!(valid_header_version(
FLOONET_FIRST_HARD_FORK + 1, FLOONET_FIRST_HARD_FORK + 1,
HeaderVersion::new(2) HeaderVersion(2)
)); ));
assert!(!valid_header_version( assert!(!valid_header_version(
FLOONET_FIRST_HARD_FORK, 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( assert!(valid_header_version(
FLOONET_SECOND_HARD_FORK - 1, FLOONET_SECOND_HARD_FORK - 1,
HeaderVersion::new(2) HeaderVersion(2)
)); ));
assert!(valid_header_version( assert!(valid_header_version(
FLOONET_SECOND_HARD_FORK, FLOONET_SECOND_HARD_FORK,
HeaderVersion::new(3) HeaderVersion(3)
)); ));
assert!(valid_header_version( assert!(valid_header_version(
FLOONET_SECOND_HARD_FORK + 1, FLOONET_SECOND_HARD_FORK + 1,
HeaderVersion::new(3) HeaderVersion(3)
)); ));
assert!(!valid_header_version( assert!(!valid_header_version(
FLOONET_SECOND_HARD_FORK, FLOONET_SECOND_HARD_FORK,
HeaderVersion::new(2) HeaderVersion(2)
)); ));
assert!(!valid_header_version( assert!(!valid_header_version(
FLOONET_SECOND_HARD_FORK, FLOONET_SECOND_HARD_FORK,
HeaderVersion::new(1) HeaderVersion(1)
)); ));
assert!(!valid_header_version( assert!(!valid_header_version(YEAR_HEIGHT - 1, HeaderVersion(2)));
YEAR_HEIGHT - 1, assert!(valid_header_version(YEAR_HEIGHT - 1, HeaderVersion(3)));
HeaderVersion::new(2) assert!(valid_header_version(YEAR_HEIGHT, HeaderVersion(3)));
)); assert!(valid_header_version(YEAR_HEIGHT + 1, HeaderVersion(3)));
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)));
// v4 not active yet // v4 not active yet
assert!(!valid_header_version( assert!(!valid_header_version(YEAR_HEIGHT * 3 / 2, HeaderVersion(4)));
YEAR_HEIGHT * 3 / 2, assert!(!valid_header_version(YEAR_HEIGHT * 3 / 2, HeaderVersion(3)));
HeaderVersion::new(4) assert!(!valid_header_version(YEAR_HEIGHT * 3 / 2, HeaderVersion(2)));
)); assert!(!valid_header_version(YEAR_HEIGHT * 3 / 2, HeaderVersion(1)));
assert!(!valid_header_version( assert!(!valid_header_version(YEAR_HEIGHT * 2, HeaderVersion(3)));
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( assert!(!valid_header_version(
YEAR_HEIGHT * 3 / 2 + 1, YEAR_HEIGHT * 3 / 2 + 1,
HeaderVersion::new(3) HeaderVersion(3)
)); ));
} }
} }