replace is_mainnet() with !is_testnet() (#2125)

* replace is_mainnet() with !is_testnet()
makes testing "mainnet" code significantly easier (its the default)

* fix next_target_adjustment test based on tromp analysis

* rustfmt

* cleanup wallet db files and add to gitignore

* fix
This commit is contained in:
Antioch Peverell 2018-12-12 16:49:35 +00:00 committed by GitHub
parent ed9c1acec1
commit 305b36dcce
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 21 deletions

View file

@ -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

View file

@ -1295,14 +1295,14 @@ pub fn kernel_sig_msg(
lock_height: u64,
features: KernelFeatures,
) -> Result<secp::Message, Error> {
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)
}

View file

@ -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

View file

@ -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