Skip to content

Commit

Permalink
fix: move checks for automated test mode into global.rs (#2956)
Browse files Browse the repository at this point in the history
* fix: move checks for test/production mode into one place - global.rs

* chore: rustfmt
  • Loading branch information
Joseph Goulden authored and antiochp committed Jul 25, 2019
1 parent ecd16d1 commit 2ae76d3
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 44 deletions.
11 changes: 3 additions & 8 deletions chain/src/pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
}

Expand Down
2 changes: 1 addition & 1 deletion chain/tests/chain_test_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 1 addition & 3 deletions chain/tests/test_txhashset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion core/src/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
31 changes: 9 additions & 22 deletions core/src/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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
Expand Down
8 changes: 0 additions & 8 deletions core/src/pow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
1 change: 0 additions & 1 deletion p2p/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down

0 comments on commit 2ae76d3

Please sign in to comment.