From 39f4fb60ef3fde7bfed78486074fc6255d01d5f0 Mon Sep 17 00:00:00 2001 From: Matthew Little Date: Thu, 17 Dec 2020 14:01:58 +0100 Subject: [PATCH 1/3] feat: allow small test genesis file to be used in mocknet mode --- testnet/stacks-node/src/genesis_data.rs | 10 ++----- testnet/stacks-node/src/node.rs | 36 ++++++++++++++++-------- testnet/stacks-node/src/run_loop/neon.rs | 9 ++++-- 3 files changed, 33 insertions(+), 22 deletions(-) diff --git a/testnet/stacks-node/src/genesis_data.rs b/testnet/stacks-node/src/genesis_data.rs index 0f118c40eb..bb7a2cbb27 100644 --- a/testnet/stacks-node/src/genesis_data.rs +++ b/testnet/stacks-node/src/genesis_data.rs @@ -1,16 +1,10 @@ -use stx_genesis::GenesisData; - // Uses the full production genesis chainstate.txt data when compiled regularly, .e.g. `cargo build`. // Uses a small test chainstate.txt file when tests are ran regularly, .e.g. `cargo test`. // The production file can be used in tests by specifying the `prod-genesis-chainstate` feature // flag, .e.g. `cargo test --features prod-genesis-chainstate` #[cfg(any(not(test), feature = "prod-genesis-chainstate"))] -lazy_static! { - pub static ref GENESIS_DATA: GenesisData = GenesisData::new(false); -} +pub const USE_TEST_GENESIS_CHAINSTATE: bool = false; #[cfg(all(test, not(feature = "prod-genesis-chainstate")))] -lazy_static! { - pub static ref GENESIS_DATA: GenesisData = GenesisData::new(true); -} +pub const USE_TEST_GENESIS_CHAINSTATE: bool = true; diff --git a/testnet/stacks-node/src/node.rs b/testnet/stacks-node/src/node.rs index 789d10c121..227553024e 100644 --- a/testnet/stacks-node/src/node.rs +++ b/testnet/stacks-node/src/node.rs @@ -1,13 +1,10 @@ -use super::{ - genesis_data::GENESIS_DATA, BurnchainController, BurnchainTip, Config, EventDispatcher, - Keychain, Tenure, -}; -use crate::run_loop::RegisteredKey; +use super::{BurnchainController, BurnchainTip, Config, EventDispatcher, Keychain, Tenure}; +use crate::{genesis_data::USE_TEST_GENESIS_CHAINSTATE, run_loop::RegisteredKey}; -use std::collections::HashSet; use std::convert::TryFrom; use std::default::Default; use std::net::SocketAddr; +use std::{collections::HashSet, env}; use std::{thread, thread::JoinHandle, time}; use stacks::chainstate::burn::db::sortdb::SortitionDB; @@ -87,9 +84,11 @@ pub struct Node { nonce: u64, } -pub fn get_account_lockups() -> Box> { +pub fn get_account_lockups( + use_test_chainstate_data: bool, +) -> Box> { Box::new( - GENESIS_DATA + stx_genesis::GenesisData::new(use_test_chainstate_data) .read_lockups() .map(|item| ChainstateAccountLockup { address: item.address, @@ -99,9 +98,11 @@ pub fn get_account_lockups() -> Box ) } -pub fn get_account_balances() -> Box> { +pub fn get_account_balances( + use_test_chainstate_data: bool, +) -> Box> { Box::new( - GENESIS_DATA + stx_genesis::GenesisData::new(use_test_chainstate_data) .read_balances() .map(|item| ChainstateAccountBalance { address: item.address, @@ -181,6 +182,13 @@ fn spawn_peer( impl Node { /// Instantiate and initialize a new node, given a config pub fn new(config: Config, boot_block_exec: Box ()>) -> Self { + let use_test_genesis_data = if config.burnchain.mode == "mocknet" { + // When running in mocknet mode allow the small test genesis chainstate data to be enabled. + env::var("BLOCKSTACK_USE_TEST_GENESIS_CHAINSTATE") == Ok("1".to_string()) + } else { + USE_TEST_GENESIS_CHAINSTATE + }; + let keychain = Keychain::default(config.node.seed.clone()); let initial_balances = config @@ -195,8 +203,12 @@ impl Node { first_burnchain_block_height: 0, first_burnchain_block_timestamp: 0, post_flight_callback: Some(boot_block_exec), - get_bulk_initial_lockups: Some(Box::new(get_account_lockups)), - get_bulk_initial_balances: Some(Box::new(get_account_balances)), + get_bulk_initial_lockups: Some(Box::new(move || { + get_account_lockups(use_test_genesis_data) + })), + get_bulk_initial_balances: Some(Box::new(move || { + get_account_balances(use_test_genesis_data) + })), }; let chain_state_result = StacksChainState::open_and_exec( diff --git a/testnet/stacks-node/src/run_loop/neon.rs b/testnet/stacks-node/src/run_loop/neon.rs index e607c9e715..8492dc045d 100644 --- a/testnet/stacks-node/src/run_loop/neon.rs +++ b/testnet/stacks-node/src/run_loop/neon.rs @@ -1,4 +1,5 @@ use crate::{ + genesis_data::USE_TEST_GENESIS_CHAINSTATE, neon_node, node::{get_account_balances, get_account_lockups}, BitcoinRegtestController, BurnchainController, Config, EventDispatcher, Keychain, @@ -198,8 +199,12 @@ impl RunLoop { first_burnchain_block_hash: coordinator_burnchain_config.first_block_hash, first_burnchain_block_height: coordinator_burnchain_config.first_block_height as u32, first_burnchain_block_timestamp: coordinator_burnchain_config.first_block_timestamp, - get_bulk_initial_lockups: Some(Box::new(get_account_lockups)), - get_bulk_initial_balances: Some(Box::new(get_account_balances)), + get_bulk_initial_lockups: Some(Box::new(|| { + get_account_lockups(USE_TEST_GENESIS_CHAINSTATE) + })), + get_bulk_initial_balances: Some(Box::new(|| { + get_account_balances(USE_TEST_GENESIS_CHAINSTATE) + })), }; let (chain_state_db, receipts) = StacksChainState::open_and_exec( From 39a1c9e952855d38a379fd21fca3f9963cc962d9 Mon Sep 17 00:00:00 2001 From: Matthew Little Date: Thu, 17 Dec 2020 17:03:23 +0100 Subject: [PATCH 2/3] fix: env var should only be used to enable test chainstate --- testnet/stacks-node/src/node.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/testnet/stacks-node/src/node.rs b/testnet/stacks-node/src/node.rs index 227553024e..46db889962 100644 --- a/testnet/stacks-node/src/node.rs +++ b/testnet/stacks-node/src/node.rs @@ -184,7 +184,11 @@ impl Node { pub fn new(config: Config, boot_block_exec: Box ()>) -> Self { let use_test_genesis_data = if config.burnchain.mode == "mocknet" { // When running in mocknet mode allow the small test genesis chainstate data to be enabled. - env::var("BLOCKSTACK_USE_TEST_GENESIS_CHAINSTATE") == Ok("1".to_string()) + if env::var("BLOCKSTACK_USE_TEST_GENESIS_CHAINSTATE") == Ok("1".to_string()) { + true + } else { + USE_TEST_GENESIS_CHAINSTATE + } } else { USE_TEST_GENESIS_CHAINSTATE }; From e51b3b03b02c981bbe7d7d0c0155e7174db19251 Mon Sep 17 00:00:00 2001 From: Matthew Little Date: Fri, 18 Dec 2020 16:08:42 +0100 Subject: [PATCH 3/3] feat: add `use_test_genesis_chainstate` option to node config --- testnet/stacks-node/src/config.rs | 4 ++++ testnet/stacks-node/src/node.rs | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/testnet/stacks-node/src/config.rs b/testnet/stacks-node/src/config.rs index 23db914c2f..dfbd1bf635 100644 --- a/testnet/stacks-node/src/config.rs +++ b/testnet/stacks-node/src/config.rs @@ -421,6 +421,7 @@ impl Config { pox_sync_sample_secs: node .pox_sync_sample_secs .unwrap_or(default_node_config.pox_sync_sample_secs), + use_test_genesis_chainstate: node.use_test_genesis_chainstate, }; node_config.set_bootstrap_node(node.bootstrap_node); if let Some(deny_nodes) = node.deny_nodes { @@ -913,6 +914,7 @@ pub struct NodeConfig { pub wait_time_for_microblocks: u64, pub prometheus_bind: Option, pub pox_sync_sample_secs: u64, + pub use_test_genesis_chainstate: Option, } impl NodeConfig { @@ -950,6 +952,7 @@ impl NodeConfig { wait_time_for_microblocks: 5000, prometheus_bind: None, pox_sync_sample_secs: 30, + use_test_genesis_chainstate: None, } } @@ -1081,6 +1084,7 @@ pub struct NodeConfigFile { pub wait_time_for_microblocks: Option, pub prometheus_bind: Option, pub pox_sync_sample_secs: Option, + pub use_test_genesis_chainstate: Option, } #[derive(Clone, Deserialize, Default)] diff --git a/testnet/stacks-node/src/node.rs b/testnet/stacks-node/src/node.rs index 46db889962..57a79b9101 100644 --- a/testnet/stacks-node/src/node.rs +++ b/testnet/stacks-node/src/node.rs @@ -184,8 +184,13 @@ impl Node { pub fn new(config: Config, boot_block_exec: Box ()>) -> Self { let use_test_genesis_data = if config.burnchain.mode == "mocknet" { // When running in mocknet mode allow the small test genesis chainstate data to be enabled. + // First check env var, then config file, then use default. if env::var("BLOCKSTACK_USE_TEST_GENESIS_CHAINSTATE") == Ok("1".to_string()) { true + } else if let Some(use_test_genesis_chainstate) = + config.node.use_test_genesis_chainstate + { + use_test_genesis_chainstate } else { USE_TEST_GENESIS_CHAINSTATE }