thread local chain type vs global chain type (#3327)

* Introduce GLOBAL_CHAIN_TYPE and make CHAIN_TYPE thread_local.
This makes testing more explicit and significantly more robust.

* set_local_chain_type() in tests

* cleanup - weird

* get pool tests working with explicit local chain_type config

* core tests working with explicit local chain_type

* p2p tests working with explicit local chain_type

* store tests working

* cleanup, feedback
This commit is contained in:
Antioch Peverell
2020-05-22 12:51:58 +01:00
committed by GitHub
parent 096b6924ce
commit 6faa0e8d75
33 changed files with 192 additions and 114 deletions
+26 -8
View File
@@ -29,18 +29,22 @@ use crate::core::libtx::ProofBuilder;
use crate::core::{global, ser};
use chrono::Duration;
use grin_core as core;
use grin_core::global::ChainTypes;
use keychain::{BlindingFactor, ExtKeychain, Keychain};
use std::sync::Arc;
use util::{secp, RwLock, ToHex};
// Setup test with AutomatedTesting chain_type;
fn test_setup() {
global::set_local_chain_type(global::ChainTypes::AutomatedTesting);
}
fn verifier_cache() -> Arc<RwLock<dyn VerifierCache>> {
Arc::new(RwLock::new(LruVerifierCache::new()))
}
#[test]
fn too_large_block() {
global::set_mining_mode(ChainTypes::AutomatedTesting);
test_setup();
let keychain = ExtKeychain::from_random_seed(false).unwrap();
let builder = ProofBuilder::new(&keychain);
let max_out = global::max_block_weight() / BLOCK_OUTPUT_WEIGHT;
@@ -71,6 +75,7 @@ fn too_large_block() {
// block with no inputs/outputs/kernels
// no fees, no reward, no coinbase
fn very_empty_block() {
test_setup();
let b = Block::with_header(BlockHeader::default());
assert_eq!(
@@ -82,6 +87,7 @@ fn very_empty_block() {
#[test]
// builds a block with a tx spending another and check that cut_through occurred
fn block_with_cut_through() {
test_setup();
let keychain = ExtKeychain::from_random_seed(false).unwrap();
let builder = ProofBuilder::new(&keychain);
let key_id1 = ExtKeychain::derive_key_id(1, 1, 0, 0, 0);
@@ -120,6 +126,7 @@ fn block_with_cut_through() {
#[test]
fn empty_block_with_coinbase_is_valid() {
test_setup();
let keychain = ExtKeychain::from_random_seed(false).unwrap();
let builder = ProofBuilder::new(&keychain);
let prev = BlockHeader::default();
@@ -158,6 +165,7 @@ fn empty_block_with_coinbase_is_valid() {
// invalidates the block and specifically it causes verify_coinbase to fail
// additionally verifying the merkle_inputs_outputs also fails
fn remove_coinbase_output_flag() {
test_setup();
let keychain = ExtKeychain::from_random_seed(false).unwrap();
let builder = ProofBuilder::new(&keychain);
let prev = BlockHeader::default();
@@ -181,6 +189,7 @@ fn remove_coinbase_output_flag() {
// test that flipping the COINBASE flag on the kernel features
// invalidates the block and specifically it causes verify_coinbase to fail
fn remove_coinbase_kernel_flag() {
test_setup();
let keychain = ExtKeychain::from_random_seed(false).unwrap();
let builder = ProofBuilder::new(&keychain);
let prev = BlockHeader::default();
@@ -223,6 +232,7 @@ fn serialize_deserialize_header_version() {
#[test]
fn serialize_deserialize_block_header() {
test_setup();
let keychain = ExtKeychain::from_random_seed(false).unwrap();
let builder = ProofBuilder::new(&keychain);
let prev = BlockHeader::default();
@@ -240,6 +250,7 @@ fn serialize_deserialize_block_header() {
#[test]
fn serialize_deserialize_block() {
test_setup();
let tx1 = tx1i2o();
let keychain = ExtKeychain::from_random_seed(false).unwrap();
let builder = ProofBuilder::new(&keychain);
@@ -260,7 +271,7 @@ fn serialize_deserialize_block() {
#[test]
fn empty_block_serialized_size() {
global::set_mining_mode(ChainTypes::AutomatedTesting);
test_setup();
let keychain = ExtKeychain::from_random_seed(false).unwrap();
let builder = ProofBuilder::new(&keychain);
let prev = BlockHeader::default();
@@ -273,7 +284,7 @@ fn empty_block_serialized_size() {
#[test]
fn block_single_tx_serialized_size() {
global::set_mining_mode(ChainTypes::AutomatedTesting);
test_setup();
let keychain = ExtKeychain::from_random_seed(false).unwrap();
let builder = ProofBuilder::new(&keychain);
let tx1 = tx1i2o();
@@ -287,7 +298,7 @@ fn block_single_tx_serialized_size() {
#[test]
fn empty_compact_block_serialized_size() {
global::set_mining_mode(ChainTypes::AutomatedTesting);
test_setup();
let keychain = ExtKeychain::from_random_seed(false).unwrap();
let builder = ProofBuilder::new(&keychain);
let prev = BlockHeader::default();
@@ -301,7 +312,7 @@ fn empty_compact_block_serialized_size() {
#[test]
fn compact_block_single_tx_serialized_size() {
global::set_mining_mode(ChainTypes::AutomatedTesting);
test_setup();
let keychain = ExtKeychain::from_random_seed(false).unwrap();
let builder = ProofBuilder::new(&keychain);
let tx1 = tx1i2o();
@@ -316,7 +327,7 @@ fn compact_block_single_tx_serialized_size() {
#[test]
fn block_10_tx_serialized_size() {
global::set_mining_mode(global::ChainTypes::AutomatedTesting);
test_setup();
let keychain = ExtKeychain::from_random_seed(false).unwrap();
let builder = ProofBuilder::new(&keychain);
@@ -353,7 +364,7 @@ fn block_10_tx_serialized_size() {
#[test]
fn compact_block_10_tx_serialized_size() {
global::set_mining_mode(ChainTypes::AutomatedTesting);
test_setup();
let keychain = ExtKeychain::from_random_seed(false).unwrap();
let builder = ProofBuilder::new(&keychain);
@@ -373,6 +384,7 @@ fn compact_block_10_tx_serialized_size() {
#[test]
fn compact_block_hash_with_nonce() {
test_setup();
let keychain = ExtKeychain::from_random_seed(false).unwrap();
let builder = ProofBuilder::new(&keychain);
let tx = tx1i2o();
@@ -404,6 +416,7 @@ fn compact_block_hash_with_nonce() {
#[test]
fn convert_block_to_compact_block() {
test_setup();
let keychain = ExtKeychain::from_random_seed(false).unwrap();
let builder = ProofBuilder::new(&keychain);
let tx1 = tx1i2o();
@@ -428,6 +441,7 @@ fn convert_block_to_compact_block() {
#[test]
fn hydrate_empty_compact_block() {
test_setup();
let keychain = ExtKeychain::from_random_seed(false).unwrap();
let builder = ProofBuilder::new(&keychain);
let prev = BlockHeader::default();
@@ -442,6 +456,7 @@ fn hydrate_empty_compact_block() {
#[test]
fn serialize_deserialize_compact_block() {
test_setup();
let keychain = ExtKeychain::from_random_seed(false).unwrap();
let builder = ProofBuilder::new(&keychain);
let tx1 = tx1i2o();
@@ -469,6 +484,7 @@ fn serialize_deserialize_compact_block() {
// Duplicate a range proof from a valid output into another of the same amount
#[test]
fn same_amount_outputs_copy_range_proof() {
test_setup();
let keychain = keychain::ExtKeychain::from_random_seed(false).unwrap();
let builder = ProofBuilder::new(&keychain);
let key_id1 = keychain::ExtKeychain::derive_key_id(1, 1, 0, 0, 0);
@@ -511,6 +527,7 @@ fn same_amount_outputs_copy_range_proof() {
// Swap a range proof with the right private key but wrong amount
#[test]
fn wrong_amount_range_proof() {
test_setup();
let keychain = keychain::ExtKeychain::from_random_seed(false).unwrap();
let builder = ProofBuilder::new(&keychain);
let key_id1 = keychain::ExtKeychain::derive_key_id(1, 1, 0, 0, 0);
@@ -563,6 +580,7 @@ fn wrong_amount_range_proof() {
#[test]
fn validate_header_proof() {
test_setup();
let keychain = ExtKeychain::from_random_seed(false).unwrap();
let builder = ProofBuilder::new(&keychain);
let prev = BlockHeader::default();
+1 -1
View File
@@ -22,7 +22,7 @@ use grin_core::pow::Difficulty;
/// Checks different next_target adjustments and difficulty boundaries
#[test]
fn next_target_adjustment() {
global::set_mining_mode(global::ChainTypes::AutomatedTesting);
global::set_local_chain_type(global::ChainTypes::AutomatedTesting);
let cur_time = Utc::now().timestamp() as u64;
let diff_min = Difficulty::min();
+2 -2
View File
@@ -21,7 +21,7 @@ use grin_core::global;
#[test]
fn test_secondary_pow_ratio() {
// Tests for Floonet chain type (covers pre and post hardfork).
global::set_mining_mode(global::ChainTypes::Floonet);
global::set_local_chain_type(global::ChainTypes::Floonet);
assert_eq!(global::is_floonet(), true);
assert_eq!(secondary_pow_ratio(1), 90);
@@ -63,7 +63,7 @@ fn test_secondary_pow_ratio() {
#[test]
fn hard_forks() {
global::set_mining_mode(global::ChainTypes::Floonet);
global::set_local_chain_type(global::ChainTypes::Floonet);
assert_eq!(global::is_floonet(), true);
assert!(valid_header_version(0, HeaderVersion(1)));
assert!(valid_header_version(10, HeaderVersion(1)));
+6 -9
View File
@@ -245,8 +245,7 @@ fn print_chain_sim(chain_sim: Vec<(HeaderInfo, DiffStats)>) {
/// Checks different next_target adjustments and difficulty boundaries
#[test]
fn adjustment_scenarios() {
// Use production parameters for genesis diff
global::set_mining_mode(global::ChainTypes::Mainnet);
global::set_local_chain_type(global::ChainTypes::Mainnet);
// Genesis block with initial diff
let chain_sim = create_chain_sim(global::initial_block_difficulty());
@@ -318,8 +317,7 @@ fn adjustment_scenarios() {
#[test]
fn test_secondary_pow_ratio() {
global::set_mining_mode(global::ChainTypes::Mainnet);
assert_eq!(global::is_floonet(), false);
global::set_local_chain_type(global::ChainTypes::Mainnet);
assert_eq!(secondary_pow_ratio(1), 90);
assert_eq!(secondary_pow_ratio(89), 90);
@@ -360,12 +358,11 @@ fn test_secondary_pow_ratio() {
#[test]
fn test_secondary_pow_scale() {
global::set_local_chain_type(global::ChainTypes::Mainnet);
let window = DIFFICULTY_ADJUST_WINDOW;
let mut hi = HeaderInfo::from_diff_scaling(Difficulty::from_num(10), 100);
global::set_mining_mode(global::ChainTypes::Mainnet);
assert_eq!(global::is_floonet(), false);
// all primary, factor should increase so it becomes easier to find a high
// difficulty block
hi.is_secondary = false;
@@ -436,8 +433,8 @@ fn test_secondary_pow_scale() {
#[test]
fn hard_forks() {
global::set_mining_mode(global::ChainTypes::Mainnet);
assert_eq!(global::is_floonet(), false);
global::set_local_chain_type(global::ChainTypes::Mainnet);
assert!(valid_header_version(0, HeaderVersion(1)));
assert!(valid_header_version(10, HeaderVersion(1)));
assert!(!valid_header_version(10, HeaderVersion(2)));
+24 -1
View File
@@ -25,7 +25,7 @@ use self::core::core::{
};
use self::core::libtx::build::{self, initial_tx, input, output, with_excess};
use self::core::libtx::ProofBuilder;
use self::core::ser;
use self::core::{global, ser};
use crate::common::{new_block, tx1i1o, tx1i2o, tx2i1o};
use grin_core as core;
use keychain::{BlindingFactor, ExtKeychain, Keychain};
@@ -33,6 +33,11 @@ use std::sync::Arc;
use util::static_secp_instance;
use util::RwLock;
// Setup test with AutomatedTesting chain_type;
fn test_setup() {
global::set_local_chain_type(global::ChainTypes::AutomatedTesting);
}
#[test]
fn simple_tx_ser() {
let tx = tx2i1o();
@@ -61,6 +66,7 @@ fn simple_tx_ser() {
#[test]
fn simple_tx_ser_deser() {
test_setup();
let tx = tx2i1o();
let mut vec = Vec::new();
ser::serialize_default(&mut vec, &tx).expect("serialization failed");
@@ -73,6 +79,7 @@ fn simple_tx_ser_deser() {
#[test]
fn tx_double_ser_deser() {
test_setup();
// checks serializing doesn't mess up the tx and produces consistent results
let btx = tx2i1o();
@@ -91,6 +98,7 @@ fn tx_double_ser_deser() {
#[test]
#[should_panic(expected = "Keychain Error")]
fn test_zero_commit_fails() {
test_setup();
let keychain = ExtKeychain::from_random_seed(false).unwrap();
let builder = ProofBuilder::new(&keychain);
let key_id1 = ExtKeychain::derive_key_id(1, 1, 0, 0, 0);
@@ -111,6 +119,7 @@ fn verifier_cache() -> Arc<RwLock<dyn VerifierCache>> {
#[test]
fn build_tx_kernel() {
test_setup();
let keychain = ExtKeychain::from_random_seed(false).unwrap();
let builder = ProofBuilder::new(&keychain);
let key_id1 = ExtKeychain::derive_key_id(1, 1, 0, 0, 0);
@@ -143,6 +152,7 @@ fn build_tx_kernel() {
// and check it still validates.
#[test]
fn transaction_cut_through() {
test_setup();
let tx1 = tx1i2o();
let tx2 = tx2i1o();
@@ -164,6 +174,7 @@ fn transaction_cut_through() {
// Attempt to deaggregate a multi-kernel transaction in a different way
#[test]
fn multi_kernel_transaction_deaggregation() {
test_setup();
let tx1 = tx1i1o();
let tx2 = tx1i1o();
let tx3 = tx1i1o();
@@ -202,6 +213,7 @@ fn multi_kernel_transaction_deaggregation() {
#[test]
fn multi_kernel_transaction_deaggregation_2() {
test_setup();
let tx1 = tx1i1o();
let tx2 = tx1i1o();
let tx3 = tx1i1o();
@@ -227,6 +239,7 @@ fn multi_kernel_transaction_deaggregation_2() {
#[test]
fn multi_kernel_transaction_deaggregation_3() {
test_setup();
let tx1 = tx1i1o();
let tx2 = tx1i1o();
let tx3 = tx1i1o();
@@ -253,6 +266,7 @@ fn multi_kernel_transaction_deaggregation_3() {
#[test]
fn multi_kernel_transaction_deaggregation_4() {
test_setup();
let tx1 = tx1i1o();
let tx2 = tx1i1o();
let tx3 = tx1i1o();
@@ -288,6 +302,7 @@ fn multi_kernel_transaction_deaggregation_4() {
#[test]
fn multi_kernel_transaction_deaggregation_5() {
test_setup();
let tx1 = tx1i1o();
let tx2 = tx1i1o();
let tx3 = tx1i1o();
@@ -327,6 +342,7 @@ fn multi_kernel_transaction_deaggregation_5() {
// Attempt to deaggregate a multi-kernel transaction
#[test]
fn basic_transaction_deaggregation() {
test_setup();
let tx1 = tx1i2o();
let tx2 = tx2i1o();
@@ -412,6 +428,7 @@ fn tx_hash_diff() {
/// 2 inputs, 2 outputs transaction.
#[test]
fn tx_build_exchange() {
test_setup();
let keychain = ExtKeychain::from_random_seed(false).unwrap();
let builder = ProofBuilder::new(&keychain);
let key_id1 = ExtKeychain::derive_key_id(1, 1, 0, 0, 0);
@@ -457,6 +474,7 @@ fn tx_build_exchange() {
#[test]
fn reward_empty_block() {
test_setup();
let keychain = keychain::ExtKeychain::from_random_seed(false).unwrap();
let builder = ProofBuilder::new(&keychain);
let key_id = ExtKeychain::derive_key_id(1, 1, 0, 0, 0);
@@ -473,6 +491,7 @@ fn reward_empty_block() {
#[test]
fn reward_with_tx_block() {
test_setup();
let keychain = keychain::ExtKeychain::from_random_seed(false).unwrap();
let builder = ProofBuilder::new(&keychain);
let key_id = ExtKeychain::derive_key_id(1, 1, 0, 0, 0);
@@ -500,6 +519,7 @@ fn reward_with_tx_block() {
#[test]
fn simple_block() {
test_setup();
let keychain = keychain::ExtKeychain::from_random_seed(false).unwrap();
let builder = ProofBuilder::new(&keychain);
let key_id = ExtKeychain::derive_key_id(1, 1, 0, 0, 0);
@@ -523,6 +543,7 @@ fn simple_block() {
#[test]
fn test_block_with_timelocked_tx() {
test_setup();
let keychain = keychain::ExtKeychain::from_random_seed(false).unwrap();
let builder = ProofBuilder::new(&keychain);
let key_id1 = ExtKeychain::derive_key_id(1, 1, 0, 0, 0);
@@ -581,6 +602,7 @@ fn test_block_with_timelocked_tx() {
#[test]
pub fn test_verify_1i1o_sig() {
test_setup();
let tx = tx1i1o();
tx.validate(Weighting::AsTransaction, verifier_cache())
.unwrap();
@@ -588,6 +610,7 @@ pub fn test_verify_1i1o_sig() {
#[test]
pub fn test_verify_2i1o_sig() {
test_setup();
let tx = tx2i1o();
tx.validate(Weighting::AsTransaction, verifier_cache())
.unwrap();