diff --git a/core/src/consensus.rs b/core/src/consensus.rs index 4cae47dcd..804e5cd03 100644 --- a/core/src/consensus.rs +++ b/core/src/consensus.rs @@ -62,29 +62,31 @@ pub const COINBASE_MATURITY: u64 = DAY_HEIGHT; /// function of block height (time). Starts at 90% losing a percent /// approximately every week. Represented as an integer between 0 and 100. pub fn secondary_pow_ratio(height: u64) -> u64 { - if global::is_mainnet() { - 90u64.saturating_sub(height / (2 * YEAR_HEIGHT / 90)) - } else { + if global::is_testnet() { if height < T4_CUCKAROO_HARDFORK { // Maintaining pre hardfork testnet4 behavior 90u64.saturating_sub(height / WEEK_HEIGHT) } else { 90u64.saturating_sub(height / (2 * YEAR_HEIGHT / 90)) } + } else { + // Mainnet (or testing mainnet code). + 90u64.saturating_sub(height / (2 * YEAR_HEIGHT / 90)) } } /// The AR scale damping factor to use. Dependent on block height /// to account for pre HF behavior on testnet4. fn ar_scale_damp_factor(height: u64) -> u64 { - if global::is_mainnet() { - AR_SCALE_DAMP_FACTOR - } else { + if global::is_testnet() { if height < T4_CUCKAROO_HARDFORK { DIFFICULTY_DAMP_FACTOR } else { AR_SCALE_DAMP_FACTOR } + } else { + // Mainnet (or testing mainnet code). + AR_SCALE_DAMP_FACTOR } } @@ -332,7 +334,8 @@ where /// the hardfork. fn ar_count(height: u64, diff_data: &[HeaderInfo]) -> u64 { let mut to_skip = 1; - if !global::is_mainnet() && height < T4_CUCKAROO_HARDFORK { + if global::is_testnet() && height < T4_CUCKAROO_HARDFORK { + // Maintain behavior of testnet4 pre-HF. to_skip = 0; } 100 * diff_data diff --git a/core/src/core/transaction.rs b/core/src/core/transaction.rs index b046453fb..10ba740dd 100644 --- a/core/src/core/transaction.rs +++ b/core/src/core/transaction.rs @@ -1295,14 +1295,14 @@ pub fn kernel_sig_msg( lock_height: u64, features: KernelFeatures, ) -> Result { - let msg = if global::is_mainnet() { - let hash = (fee, lock_height, features).hash(); - secp::Message::from_slice(&hash.as_bytes())? - } else { + let msg = if global::is_testnet() { let mut bytes = [0; 32]; BigEndian::write_u64(&mut bytes[16..24], fee); BigEndian::write_u64(&mut bytes[24..], lock_height); secp::Message::from_slice(&bytes)? + } else { + let hash = (fee, lock_height, features).hash(); + secp::Message::from_slice(&hash.as_bytes())? }; Ok(msg) } diff --git a/core/src/global.rs b/core/src/global.rs index 30a38374d..153bea402 100644 --- a/core/src/global.rs +++ b/core/src/global.rs @@ -284,7 +284,8 @@ pub fn is_user_testing_mode() -> bool { ChainTypes::UserTesting == *param_ref } -/// Are we in production mode (a live public network)? +/// Are we in production mode? +/// Production defined as a live public network, testnet[n] or mainnet. pub fn is_production_mode() -> bool { let param_ref = CHAIN_TYPE.read(); ChainTypes::Testnet1 == *param_ref @@ -294,10 +295,13 @@ pub fn is_production_mode() -> bool { || ChainTypes::Mainnet == *param_ref } -/// Are we in mainnet? -pub fn is_mainnet() -> bool { +/// Are we in one of our (many) testnets? +pub fn is_testnet() -> bool { let param_ref = CHAIN_TYPE.read(); - ChainTypes::Mainnet == *param_ref + ChainTypes::Testnet1 == *param_ref + || ChainTypes::Testnet2 == *param_ref + || ChainTypes::Testnet3 == *param_ref + || ChainTypes::Testnet4 == *param_ref } /// Helper function to get a nonce known to create a valid POW on diff --git a/core/tests/consensus.rs b/core/tests/consensus.rs index 1dc59a94c..8178c456f 100644 --- a/core/tests/consensus.rs +++ b/core/tests/consensus.rs @@ -384,9 +384,18 @@ fn next_target_adjustment() { let diff_min = Difficulty::min(); // Check we don't get stuck on difficulty <= MIN_DIFFICULTY (at 4x faster blocks at least) - let mut hi = HeaderInfo::from_diff_scaling(diff_min, MIN_DIFFICULTY as u32); + let mut hi = HeaderInfo::from_diff_scaling(diff_min, AR_SCALE_DAMP_FACTOR as u32); hi.is_secondary = false; - let hinext = next_difficulty(1, repeat(15, hi.clone(), DIFFICULTY_ADJUST_WINDOW, None)); + let hinext = next_difficulty( + 1, + repeat( + BLOCK_TIME_SEC / 4, + hi.clone(), + DIFFICULTY_ADJUST_WINDOW, + None, + ), + ); + assert_ne!(hinext.difficulty, diff_min); // Check we don't get stuck on scale MIN_DIFFICULTY, when primary frequency is too high @@ -476,7 +485,7 @@ fn test_secondary_pow_ratio() { // Tests for mainnet chain type. { global::set_mining_mode(global::ChainTypes::Mainnet); - assert_eq!(global::is_mainnet(), true); + assert_eq!(global::is_testnet(), false); assert_eq!(secondary_pow_ratio(1), 90); assert_eq!(secondary_pow_ratio(89), 90); @@ -518,7 +527,7 @@ fn test_secondary_pow_ratio() { // Tests for testnet4 chain type (covers pre and post hardfork). { global::set_mining_mode(global::ChainTypes::Testnet4); - assert_eq!(global::is_mainnet(), false); + assert_eq!(global::is_testnet(), true); assert_eq!(secondary_pow_ratio(1), 90); assert_eq!(secondary_pow_ratio(89), 90); @@ -566,7 +575,7 @@ fn test_secondary_pow_scale() { // testnet4 testing { global::set_mining_mode(global::ChainTypes::Testnet4); - assert_eq!(global::is_mainnet(), false); + assert_eq!(global::is_testnet(), true); // all primary, factor should increase so it becomes easier to find a high // difficulty block @@ -640,7 +649,7 @@ fn test_secondary_pow_scale() { // mainnet testing { global::set_mining_mode(global::ChainTypes::Mainnet); - assert_eq!(global::is_mainnet(), true); + assert_eq!(global::is_testnet(), false); // all primary, factor should increase so it becomes easier to find a high // difficulty block