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

View file

@ -19,7 +19,6 @@ use crate::core::core::hash::Hashed;
use crate::core::core::verifier_cache::VerifierCache;
use crate::core::core::Committed;
use crate::core::core::{Block, BlockHeader, BlockSums};
use crate::core::global;
use crate::core::pow;
use crate::error::{Error, ErrorKind};
use crate::store;
@ -326,10 +325,7 @@ fn validate_header(header: &BlockHeader, ctx: &mut BlockContext<'_>) -> Result<(
return Err(ErrorKind::InvalidBlockVersion(header.version).into());
}
// TODO: remove CI check from here somehow
if header.timestamp > Utc::now() + Duration::seconds(12 * (consensus::BLOCK_TIME_SEC as i64))
&& !global::is_automated_testing_mode()
{
if header.timestamp > Utc::now() + Duration::seconds(12 * (consensus::BLOCK_TIME_SEC as i64)) {
// refuse blocks more than 12 blocks intervals in future (as in bitcoin)
// TODO add warning in p2p code if local time is too different from peers
return Err(ErrorKind::InvalidBlockTime.into());
@ -358,10 +354,9 @@ fn validate_header(header: &BlockHeader, ctx: &mut BlockContext<'_>) -> Result<(
return Err(ErrorKind::InvalidBlockHeight.into());
}
// TODO - get rid of the automated testing mode check here somehow
if header.timestamp <= prev.timestamp && !global::is_automated_testing_mode() {
if header.timestamp <= prev.timestamp {
// prevent time warp attacks and some timestamp manipulations by forcing strict
// time progression (but not in CI mode)
// time progression
return Err(ErrorKind::InvalidBlockTime.into());
}

View file

@ -102,7 +102,7 @@ where
let mut b =
core::core::Block::new(&prev, vec![], next_header_info.clone().difficulty, reward)
.unwrap();
b.header.timestamp = prev.timestamp + Duration::seconds(160);
b.header.timestamp = prev.timestamp + Duration::seconds(60);
b.header.pow.secondary_scaling = next_header_info.secondary_scaling;
chain.set_txhashset_roots(&mut b).unwrap();

View file

@ -17,9 +17,7 @@ use grin_core as core;
use grin_util as util;
use std::collections::HashSet;
use std::fs::{self, File, OpenOptions};
use std::iter::FromIterator;
use std::fs::{self, File};
use std::path::{Path, PathBuf};
use std::sync::Arc;

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()

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

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

View file

@ -12,7 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use crate::conn::{Message, MessageHandler, Response, Tracker};
use crate::core::core::{self, hash::Hash, hash::Hashed, CompactBlock};