fix: move checks for automated test mode into global.rs (#2956)

* fix: move checks for test/production mode into one place - global.rs

* chore: rustfmt
This commit is contained in:
Joseph Goulden
2019-07-25 10:08:24 +01:00
committed by Antioch Peverell
parent ecd16d114f
commit 2ae76d39bb
7 changed files with 15 additions and 44 deletions
+1 -1
View File
@@ -39,7 +39,7 @@ pub fn genesis_dev() -> core::Block {
core::Block::with_header(core::BlockHeader {
height: 0,
// previous: core::hash::Hash([0xff; 32]),
timestamp: Utc.ymd(1997, 8, 4).and_hms(0, 0, 0),
timestamp: global::get_genesis_timestamp(),
pow: ProofOfWork {
nonce: global::get_genesis_nonce(),
..Default::default()
+9 -22
View File
@@ -31,7 +31,8 @@ use crate::pow::{
/// different sets of parameters for different purposes,
/// e.g. CI, User testing, production values
use crate::util::RwLock;
use chrono::prelude::{TimeZone, Utc};
use chrono::{DateTime, Duration};
/// Define these here, as they should be developer-set, not really tweakable
/// by users
@@ -296,18 +297,6 @@ pub fn txhashset_archive_interval() -> u64 {
}
}
/// Are we in automated testing mode?
pub fn is_automated_testing_mode() -> bool {
let param_ref = CHAIN_TYPE.read();
ChainTypes::AutomatedTesting == *param_ref
}
/// Are we in user testing mode?
pub fn is_user_testing_mode() -> bool {
let param_ref = CHAIN_TYPE.read();
ChainTypes::UserTesting == *param_ref
}
/// Are we in production mode?
/// Production defined as a live public network, testnet[n] or mainnet.
pub fn is_production_mode() -> bool {
@@ -324,12 +313,6 @@ pub fn is_floonet() -> bool {
ChainTypes::Floonet == *param_ref
}
/// Are we for real?
pub fn is_mainnet() -> bool {
let param_ref = CHAIN_TYPE.read();
ChainTypes::Mainnet == *param_ref
}
/// Helper function to get a nonce known to create a valid POW on
/// the genesis block, to prevent it taking ages. Should be fine for now
/// as the genesis block POW solution turns out to be the same for every new
@@ -348,10 +331,14 @@ pub fn get_genesis_nonce() -> u64 {
}
}
/// Short name representing the current chain type ("floo", "main", etc.)
pub fn chain_shortname() -> String {
/// Genesis block timestamp. Dependant on chain type.
pub fn get_genesis_timestamp() -> DateTime<Utc> {
let param_ref = CHAIN_TYPE.read();
param_ref.shortname()
match *param_ref {
ChainTypes::UserTesting => Utc::now(),
ChainTypes::AutomatedTesting => Utc::now() - Duration::minutes(60),
_ => Utc.ymd(1997, 8, 4).and_hms(0, 0, 0),
}
}
/// Converts an iterator of block difficulty data to more a more manageable
-8
View File
@@ -72,9 +72,6 @@ pub fn verify_size(bh: &BlockHeader) -> Result<(), Error> {
/// Mines a genesis block using the internal miner
pub fn mine_genesis_block() -> Result<Block, Error> {
let mut gen = genesis::genesis_dev();
if global::is_user_testing_mode() || global::is_automated_testing_mode() {
gen.header.timestamp = Utc::now();
}
// total_difficulty on the genesis header *is* the difficulty of that block
let genesis_difficulty = gen.header.pow.total_difficulty;
@@ -97,11 +94,6 @@ pub fn pow_size(
) -> Result<(), Error> {
let start_nonce = bh.pow.nonce;
// set the nonce for faster solution finding in user testing
if bh.height == 0 && global::is_user_testing_mode() {
bh.pow.nonce = global::get_genesis_nonce();
}
// try to find a cuckoo cycle on that header hash
loop {
// if we found a cycle (not guaranteed) and the proof hash is higher that the