2020-01-20 14:40:58 +03:00
|
|
|
// Copyright 2020 The Grin Developers
|
2017-08-09 19:40:23 +03:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
//! Values that should be shared across all modules, without necessarily
|
|
|
|
//! having to pass them all over the place, but aren't consensus values.
|
|
|
|
//! should be used sparingly.
|
|
|
|
|
2018-12-08 02:59:40 +03:00
|
|
|
use crate::consensus::{
|
2019-07-13 00:17:38 +03:00
|
|
|
graph_weight, valid_header_version, HeaderInfo, BASE_EDGE_BITS, BLOCK_TIME_SEC,
|
2019-06-12 12:28:14 +03:00
|
|
|
COINBASE_MATURITY, CUT_THROUGH_HORIZON, DAY_HEIGHT, DEFAULT_MIN_EDGE_BITS,
|
|
|
|
DIFFICULTY_ADJUST_WINDOW, INITIAL_DIFFICULTY, MAX_BLOCK_WEIGHT, PROOFSIZE,
|
|
|
|
SECOND_POW_EDGE_BITS, STATE_SYNC_THRESHOLD,
|
2018-07-01 01:36:38 +03:00
|
|
|
};
|
2019-06-12 12:28:14 +03:00
|
|
|
use crate::core::block::HeaderVersion;
|
2019-07-13 00:17:38 +03:00
|
|
|
use crate::pow::{
|
2019-11-27 23:36:36 +03:00
|
|
|
self, new_cuckaroo_ctx, new_cuckarood_ctx, new_cuckaroom_ctx, new_cuckatoo_ctx, EdgeType,
|
|
|
|
PoWContext,
|
2019-07-13 00:17:38 +03:00
|
|
|
};
|
2020-05-22 14:51:58 +03:00
|
|
|
use std::cell::Cell;
|
|
|
|
use util::OneTime;
|
2019-11-14 18:27:30 +03:00
|
|
|
|
2017-08-09 19:40:23 +03:00
|
|
|
/// An enum collecting sets of parameters used throughout the
|
|
|
|
/// code wherever mining is needed. This should allow for
|
|
|
|
/// different sets of parameters for different purposes,
|
|
|
|
/// e.g. CI, User testing, production values
|
|
|
|
/// Define these here, as they should be developer-set, not really tweakable
|
|
|
|
/// by users
|
|
|
|
|
2019-07-06 17:51:03 +03:00
|
|
|
/// The default "local" protocol version for this node.
|
|
|
|
/// We negotiate compatible versions with each peer via Hand/Shake.
|
|
|
|
/// Note: We also use a specific (possible different) protocol version
|
|
|
|
/// for both the backend database and MMR data files.
|
2019-09-19 16:31:46 +03:00
|
|
|
/// This defines the p2p layer protocol version for this node.
|
|
|
|
pub const PROTOCOL_VERSION: u32 = 2;
|
2019-07-06 17:51:03 +03:00
|
|
|
|
2018-10-16 02:14:23 +03:00
|
|
|
/// Automated testing edge_bits
|
2019-09-10 16:14:33 +03:00
|
|
|
pub const AUTOMATED_TESTING_MIN_EDGE_BITS: u8 = 10;
|
2017-08-09 19:40:23 +03:00
|
|
|
|
2017-08-10 03:54:10 +03:00
|
|
|
/// Automated testing proof size
|
2019-09-10 16:14:33 +03:00
|
|
|
pub const AUTOMATED_TESTING_PROOF_SIZE: usize = 8;
|
2017-08-09 19:40:23 +03:00
|
|
|
|
2018-10-16 02:14:23 +03:00
|
|
|
/// User testing edge_bits
|
|
|
|
pub const USER_TESTING_MIN_EDGE_BITS: u8 = 15;
|
2017-08-09 19:40:23 +03:00
|
|
|
|
2017-08-10 03:54:10 +03:00
|
|
|
/// User testing proof size
|
2017-09-29 21:44:25 +03:00
|
|
|
pub const USER_TESTING_PROOF_SIZE: usize = 42;
|
2017-08-09 19:40:23 +03:00
|
|
|
|
2017-10-04 20:44:22 +03:00
|
|
|
/// Automated testing coinbase maturity
|
|
|
|
pub const AUTOMATED_TESTING_COINBASE_MATURITY: u64 = 3;
|
|
|
|
|
|
|
|
/// User testing coinbase maturity
|
|
|
|
pub const USER_TESTING_COINBASE_MATURITY: u64 = 3;
|
|
|
|
|
2018-02-10 01:32:16 +03:00
|
|
|
/// Testing cut through horizon in blocks
|
2019-07-23 11:46:29 +03:00
|
|
|
pub const AUTOMATED_TESTING_CUT_THROUGH_HORIZON: u32 = 20;
|
|
|
|
|
|
|
|
/// Testing cut through horizon in blocks
|
|
|
|
pub const USER_TESTING_CUT_THROUGH_HORIZON: u32 = 70;
|
2018-11-10 06:27:52 +03:00
|
|
|
|
|
|
|
/// Testing state sync threshold in blocks
|
|
|
|
pub const TESTING_STATE_SYNC_THRESHOLD: u32 = 20;
|
2018-02-10 01:32:16 +03:00
|
|
|
|
2018-10-17 18:53:31 +03:00
|
|
|
/// Testing initial graph weight
|
|
|
|
pub const TESTING_INITIAL_GRAPH_WEIGHT: u32 = 1;
|
|
|
|
|
2018-01-27 10:48:53 +03:00
|
|
|
/// Testing initial block difficulty
|
|
|
|
pub const TESTING_INITIAL_DIFFICULTY: u64 = 1;
|
|
|
|
|
2019-02-01 13:44:04 +03:00
|
|
|
/// Testing max_block_weight (artifically low, just enough to support a few txs).
|
|
|
|
pub const TESTING_MAX_BLOCK_WEIGHT: usize = 150;
|
|
|
|
|
2018-10-18 05:04:05 +03:00
|
|
|
/// If a peer's last updated difficulty is 2 hours ago and its difficulty's lower than ours,
|
|
|
|
/// we're sure this peer is a stuck node, and we will kick out such kind of stuck peers.
|
|
|
|
pub const STUCK_PEER_KICK_TIME: i64 = 2 * 3600 * 1000;
|
|
|
|
|
2018-10-22 23:59:40 +03:00
|
|
|
/// If a peer's last seen time is 2 weeks ago we will forget such kind of defunct peers.
|
|
|
|
const PEER_EXPIRATION_DAYS: i64 = 7 * 2;
|
|
|
|
|
|
|
|
/// Constant that expresses defunct peer timeout in seconds to be used in checks.
|
|
|
|
pub const PEER_EXPIRATION_REMOVE_TIME: i64 = PEER_EXPIRATION_DAYS * 24 * 3600;
|
|
|
|
|
2018-10-23 15:01:19 +03:00
|
|
|
/// Trigger compaction check on average every day for all nodes.
|
|
|
|
/// Randomized per node - roll the dice on every block to decide.
|
|
|
|
/// Will compact the txhashset to remove pruned data.
|
|
|
|
/// Will also remove old blocks and associated data from the database.
|
|
|
|
/// For a node configured as "archival_mode = true" only the txhashset will be compacted.
|
2018-10-17 02:14:22 +03:00
|
|
|
pub const COMPACTION_CHECK: u64 = DAY_HEIGHT;
|
2018-10-13 19:34:16 +03:00
|
|
|
|
2019-07-23 15:25:34 +03:00
|
|
|
/// Number of blocks to reuse a txhashset zip for (automated testing and user testing).
|
|
|
|
pub const TESTING_TXHASHSET_ARCHIVE_INTERVAL: u64 = 10;
|
2019-07-23 11:46:29 +03:00
|
|
|
|
|
|
|
/// Number of blocks to reuse a txhashset zip for.
|
|
|
|
pub const TXHASHSET_ARCHIVE_INTERVAL: u64 = 12 * 60;
|
|
|
|
|
2017-11-16 00:49:15 +03:00
|
|
|
/// Types of chain a server can run with, dictates the genesis block and
|
|
|
|
/// and mining parameters used.
|
2020-05-22 14:51:58 +03:00
|
|
|
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq)]
|
2017-11-16 00:49:15 +03:00
|
|
|
pub enum ChainTypes {
|
2017-08-09 19:40:23 +03:00
|
|
|
/// For CI testing
|
|
|
|
AutomatedTesting,
|
|
|
|
/// For User testing
|
|
|
|
UserTesting,
|
2018-12-19 01:03:55 +03:00
|
|
|
/// Protocol testing network
|
|
|
|
Floonet,
|
2018-03-04 03:19:54 +03:00
|
|
|
/// Main production network
|
2017-11-16 00:49:15 +03:00
|
|
|
Mainnet,
|
2017-08-09 19:40:23 +03:00
|
|
|
}
|
|
|
|
|
2018-12-29 01:46:21 +03:00
|
|
|
impl ChainTypes {
|
|
|
|
/// Short name representing the chain type ("floo", "main", etc.)
|
|
|
|
pub fn shortname(&self) -> String {
|
|
|
|
match *self {
|
|
|
|
ChainTypes::AutomatedTesting => "auto".to_owned(),
|
|
|
|
ChainTypes::UserTesting => "user".to_owned(),
|
|
|
|
ChainTypes::Floonet => "floo".to_owned(),
|
|
|
|
ChainTypes::Mainnet => "main".to_owned(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-16 01:30:48 +03:00
|
|
|
impl Default for ChainTypes {
|
|
|
|
fn default() -> ChainTypes {
|
2019-01-30 07:09:30 +03:00
|
|
|
ChainTypes::Mainnet
|
2017-11-16 01:30:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-22 14:51:58 +03:00
|
|
|
lazy_static! {
|
|
|
|
/// Global chain_type that must be initialized once on node startup.
|
|
|
|
/// This is accessed via get_chain_type() which allows the global value
|
|
|
|
/// to be overridden on a per-thread basis (for testing).
|
|
|
|
pub static ref GLOBAL_CHAIN_TYPE: OneTime<ChainTypes> = OneTime::new();
|
2020-05-29 11:56:24 +03:00
|
|
|
|
|
|
|
/// Global feature flag for NRD kernel support.
|
|
|
|
/// If enabled NRD kernels are treated as valid after HF3 (based on header version).
|
|
|
|
/// If disabled NRD kernels are invalid regardless of header version or block height.
|
|
|
|
pub static ref GLOBAL_NRD_FEATURE_ENABLED: OneTime<bool> = OneTime::new();
|
2018-09-28 13:53:14 +03:00
|
|
|
}
|
|
|
|
|
2020-05-22 14:51:58 +03:00
|
|
|
thread_local! {
|
|
|
|
/// Mainnet|Floonet|UserTesting|AutomatedTesting
|
|
|
|
pub static CHAIN_TYPE: Cell<Option<ChainTypes>> = Cell::new(None);
|
2020-05-29 11:56:24 +03:00
|
|
|
|
|
|
|
/// Local feature flag for NRD kernel support.
|
|
|
|
pub static NRD_FEATURE_ENABLED: Cell<Option<bool>> = Cell::new(None);
|
2020-05-22 14:51:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the chain type on a per-thread basis via thread_local storage.
|
|
|
|
pub fn set_local_chain_type(new_type: ChainTypes) {
|
|
|
|
CHAIN_TYPE.with(|chain_type| chain_type.set(Some(new_type)))
|
|
|
|
}
|
2018-09-28 13:53:14 +03:00
|
|
|
|
2020-05-22 14:51:58 +03:00
|
|
|
/// Get the chain type via thread_local, fallback to global chain_type.
|
|
|
|
pub fn get_chain_type() -> ChainTypes {
|
|
|
|
CHAIN_TYPE.with(|chain_type| match chain_type.get() {
|
|
|
|
None => {
|
|
|
|
if GLOBAL_CHAIN_TYPE.is_init() {
|
|
|
|
let chain_type = GLOBAL_CHAIN_TYPE.borrow();
|
|
|
|
set_local_chain_type(chain_type);
|
|
|
|
chain_type
|
|
|
|
} else {
|
|
|
|
panic!("GLOBAL_CHAIN_TYPE and CHAIN_TYPE unset. Consider set_local_chain_type() in tests.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(chain_type) => chain_type,
|
|
|
|
})
|
2017-08-09 19:40:23 +03:00
|
|
|
}
|
|
|
|
|
2020-05-22 14:51:58 +03:00
|
|
|
/// One time initialization of the global chain_type.
|
|
|
|
/// Will panic if we attempt to re-initialize this (via OneTime).
|
|
|
|
pub fn init_global_chain_type(new_type: ChainTypes) {
|
|
|
|
GLOBAL_CHAIN_TYPE.init(new_type)
|
2020-05-29 11:56:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// One time initialization of the global chain_type.
|
|
|
|
/// Will panic if we attempt to re-initialize this (via OneTime).
|
|
|
|
pub fn init_global_nrd_enabled(enabled: bool) {
|
|
|
|
GLOBAL_NRD_FEATURE_ENABLED.init(enabled)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Explicitly enable the NRD global feature flag.
|
|
|
|
pub fn set_local_nrd_enabled(enabled: bool) {
|
|
|
|
NRD_FEATURE_ENABLED.with(|flag| flag.set(Some(enabled)))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Is the NRD feature flag enabled?
|
|
|
|
/// Look at thread local config first. If not set fallback to global config.
|
|
|
|
/// Default to false if global config unset.
|
|
|
|
pub fn is_nrd_enabled() -> bool {
|
|
|
|
NRD_FEATURE_ENABLED.with(|flag| match flag.get() {
|
|
|
|
None => {
|
|
|
|
if GLOBAL_NRD_FEATURE_ENABLED.is_init() {
|
|
|
|
let global_flag = GLOBAL_NRD_FEATURE_ENABLED.borrow();
|
|
|
|
flag.set(Some(global_flag));
|
|
|
|
global_flag
|
|
|
|
} else {
|
|
|
|
// Global config unset, default to false.
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(flag) => flag,
|
|
|
|
})
|
2017-08-09 19:40:23 +03:00
|
|
|
}
|
|
|
|
|
2018-09-28 13:53:14 +03:00
|
|
|
/// Return either a cuckoo context or a cuckatoo context
|
|
|
|
/// Single change point
|
|
|
|
pub fn create_pow_context<T>(
|
2019-06-12 12:28:14 +03:00
|
|
|
height: u64,
|
2018-09-28 13:53:14 +03:00
|
|
|
edge_bits: u8,
|
|
|
|
proof_size: usize,
|
|
|
|
max_sols: u32,
|
2018-11-24 23:33:17 +03:00
|
|
|
) -> Result<Box<dyn PoWContext<T>>, pow::Error>
|
2018-09-28 13:53:14 +03:00
|
|
|
where
|
2018-11-24 23:33:17 +03:00
|
|
|
T: EdgeType + 'static,
|
2018-09-28 13:53:14 +03:00
|
|
|
{
|
2020-05-22 14:51:58 +03:00
|
|
|
let chain_type = get_chain_type();
|
2018-11-24 23:33:17 +03:00
|
|
|
match chain_type {
|
2019-06-12 12:28:14 +03:00
|
|
|
// Mainnet has Cuckaroo(d)29 for AR and Cuckatoo31+ for AF
|
|
|
|
ChainTypes::Mainnet if edge_bits > 29 => new_cuckatoo_ctx(edge_bits, proof_size, max_sols),
|
2019-12-01 02:14:35 +03:00
|
|
|
ChainTypes::Mainnet if valid_header_version(height, HeaderVersion(3)) => {
|
2019-11-27 23:36:36 +03:00
|
|
|
new_cuckaroom_ctx(edge_bits, proof_size)
|
|
|
|
}
|
2019-12-01 02:14:35 +03:00
|
|
|
ChainTypes::Mainnet if valid_header_version(height, HeaderVersion(2)) => {
|
2019-07-13 00:17:38 +03:00
|
|
|
new_cuckarood_ctx(edge_bits, proof_size)
|
|
|
|
}
|
2019-06-12 12:28:14 +03:00
|
|
|
ChainTypes::Mainnet => new_cuckaroo_ctx(edge_bits, proof_size),
|
2018-11-24 23:33:17 +03:00
|
|
|
|
2018-12-19 01:03:55 +03:00
|
|
|
// Same for Floonet
|
2019-06-12 12:28:14 +03:00
|
|
|
ChainTypes::Floonet if edge_bits > 29 => new_cuckatoo_ctx(edge_bits, proof_size, max_sols),
|
2019-12-01 02:14:35 +03:00
|
|
|
ChainTypes::Floonet if valid_header_version(height, HeaderVersion(3)) => {
|
2019-11-27 23:36:36 +03:00
|
|
|
new_cuckaroom_ctx(edge_bits, proof_size)
|
|
|
|
}
|
2019-12-01 02:14:35 +03:00
|
|
|
ChainTypes::Floonet if valid_header_version(height, HeaderVersion(2)) => {
|
2019-07-13 00:17:38 +03:00
|
|
|
new_cuckarood_ctx(edge_bits, proof_size)
|
|
|
|
}
|
2019-06-12 12:28:14 +03:00
|
|
|
ChainTypes::Floonet => new_cuckaroo_ctx(edge_bits, proof_size),
|
2018-11-24 23:33:17 +03:00
|
|
|
|
|
|
|
// Everything else is Cuckatoo only
|
|
|
|
_ => new_cuckatoo_ctx(edge_bits, proof_size, max_sols),
|
|
|
|
}
|
2018-10-10 12:09:44 +03:00
|
|
|
}
|
|
|
|
|
2018-10-16 02:14:23 +03:00
|
|
|
/// The minimum acceptable edge_bits
|
|
|
|
pub fn min_edge_bits() -> u8 {
|
2020-05-22 14:51:58 +03:00
|
|
|
match get_chain_type() {
|
2018-10-16 02:14:23 +03:00
|
|
|
ChainTypes::AutomatedTesting => AUTOMATED_TESTING_MIN_EDGE_BITS,
|
|
|
|
ChainTypes::UserTesting => USER_TESTING_MIN_EDGE_BITS,
|
2018-11-24 23:33:17 +03:00
|
|
|
_ => DEFAULT_MIN_EDGE_BITS,
|
2018-06-29 20:41:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-16 02:14:23 +03:00
|
|
|
/// Reference edge_bits used to compute factor on higher Cuck(at)oo graph sizes,
|
|
|
|
/// while the min_edge_bits can be changed on a soft fork, changing
|
|
|
|
/// base_edge_bits is a hard fork.
|
|
|
|
pub fn base_edge_bits() -> u8 {
|
2020-05-22 14:51:58 +03:00
|
|
|
match get_chain_type() {
|
2018-10-16 02:14:23 +03:00
|
|
|
ChainTypes::AutomatedTesting => AUTOMATED_TESTING_MIN_EDGE_BITS,
|
|
|
|
ChainTypes::UserTesting => USER_TESTING_MIN_EDGE_BITS,
|
|
|
|
_ => BASE_EDGE_BITS,
|
2017-08-09 19:40:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-10 03:54:10 +03:00
|
|
|
/// The proofsize
|
2017-08-09 19:40:23 +03:00
|
|
|
pub fn proofsize() -> usize {
|
2020-05-22 14:51:58 +03:00
|
|
|
match get_chain_type() {
|
2017-11-16 00:49:15 +03:00
|
|
|
ChainTypes::AutomatedTesting => AUTOMATED_TESTING_PROOF_SIZE,
|
|
|
|
ChainTypes::UserTesting => USER_TESTING_PROOF_SIZE,
|
2018-07-08 00:17:11 +03:00
|
|
|
_ => PROOFSIZE,
|
2017-08-09 19:40:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-17 02:14:22 +03:00
|
|
|
/// Coinbase maturity for coinbases to be spent
|
|
|
|
pub fn coinbase_maturity() -> u64 {
|
2020-05-22 14:51:58 +03:00
|
|
|
match get_chain_type() {
|
2017-11-16 00:49:15 +03:00
|
|
|
ChainTypes::AutomatedTesting => AUTOMATED_TESTING_COINBASE_MATURITY,
|
|
|
|
ChainTypes::UserTesting => USER_TESTING_COINBASE_MATURITY,
|
2018-10-17 02:14:22 +03:00
|
|
|
_ => COINBASE_MATURITY,
|
2017-10-04 20:44:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-27 10:48:53 +03:00
|
|
|
/// Initial mining difficulty
|
|
|
|
pub fn initial_block_difficulty() -> u64 {
|
2020-05-22 14:51:58 +03:00
|
|
|
match get_chain_type() {
|
2018-01-27 10:48:53 +03:00
|
|
|
ChainTypes::AutomatedTesting => TESTING_INITIAL_DIFFICULTY,
|
|
|
|
ChainTypes::UserTesting => TESTING_INITIAL_DIFFICULTY,
|
2018-12-19 01:03:55 +03:00
|
|
|
ChainTypes::Floonet => INITIAL_DIFFICULTY,
|
2018-01-27 10:48:53 +03:00
|
|
|
ChainTypes::Mainnet => INITIAL_DIFFICULTY,
|
|
|
|
}
|
|
|
|
}
|
2018-10-17 18:53:31 +03:00
|
|
|
/// Initial mining secondary scale
|
|
|
|
pub fn initial_graph_weight() -> u32 {
|
2020-05-22 14:51:58 +03:00
|
|
|
match get_chain_type() {
|
2018-10-17 18:53:31 +03:00
|
|
|
ChainTypes::AutomatedTesting => TESTING_INITIAL_GRAPH_WEIGHT,
|
|
|
|
ChainTypes::UserTesting => TESTING_INITIAL_GRAPH_WEIGHT,
|
2018-12-19 01:03:55 +03:00
|
|
|
ChainTypes::Floonet => graph_weight(0, SECOND_POW_EDGE_BITS) as u32,
|
2018-11-29 01:05:55 +03:00
|
|
|
ChainTypes::Mainnet => graph_weight(0, SECOND_POW_EDGE_BITS) as u32,
|
2018-10-17 18:53:31 +03:00
|
|
|
}
|
|
|
|
}
|
2018-01-27 10:48:53 +03:00
|
|
|
|
2019-02-01 13:44:04 +03:00
|
|
|
/// Maximum allowed block weight.
|
|
|
|
pub fn max_block_weight() -> usize {
|
2020-05-22 14:51:58 +03:00
|
|
|
match get_chain_type() {
|
2019-02-01 13:44:04 +03:00
|
|
|
ChainTypes::AutomatedTesting => TESTING_MAX_BLOCK_WEIGHT,
|
|
|
|
ChainTypes::UserTesting => TESTING_MAX_BLOCK_WEIGHT,
|
|
|
|
ChainTypes::Floonet => MAX_BLOCK_WEIGHT,
|
|
|
|
ChainTypes::Mainnet => MAX_BLOCK_WEIGHT,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-10 01:32:16 +03:00
|
|
|
/// Horizon at which we can cut-through and do full local pruning
|
|
|
|
pub fn cut_through_horizon() -> u32 {
|
2020-05-22 14:51:58 +03:00
|
|
|
match get_chain_type() {
|
2019-07-23 11:46:29 +03:00
|
|
|
ChainTypes::AutomatedTesting => AUTOMATED_TESTING_CUT_THROUGH_HORIZON,
|
|
|
|
ChainTypes::UserTesting => USER_TESTING_CUT_THROUGH_HORIZON,
|
2018-07-08 00:17:11 +03:00
|
|
|
_ => CUT_THROUGH_HORIZON,
|
2018-02-10 01:32:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-10 06:27:52 +03:00
|
|
|
/// Threshold at which we can request a txhashset (and full blocks from)
|
|
|
|
pub fn state_sync_threshold() -> u32 {
|
2020-05-22 14:51:58 +03:00
|
|
|
match get_chain_type() {
|
2018-11-10 06:27:52 +03:00
|
|
|
ChainTypes::AutomatedTesting => TESTING_STATE_SYNC_THRESHOLD,
|
|
|
|
ChainTypes::UserTesting => TESTING_STATE_SYNC_THRESHOLD,
|
|
|
|
_ => STATE_SYNC_THRESHOLD,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-23 11:46:29 +03:00
|
|
|
/// Number of blocks to reuse a txhashset zip for.
|
|
|
|
pub fn txhashset_archive_interval() -> u64 {
|
2020-05-22 14:51:58 +03:00
|
|
|
match get_chain_type() {
|
2019-07-23 15:25:34 +03:00
|
|
|
ChainTypes::AutomatedTesting => TESTING_TXHASHSET_ARCHIVE_INTERVAL,
|
|
|
|
ChainTypes::UserTesting => TESTING_TXHASHSET_ARCHIVE_INTERVAL,
|
2019-07-23 11:46:29 +03:00
|
|
|
_ => TXHASHSET_ARCHIVE_INTERVAL,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-12 19:49:35 +03:00
|
|
|
/// Are we in production mode?
|
|
|
|
/// Production defined as a live public network, testnet[n] or mainnet.
|
2017-08-22 21:23:54 +03:00
|
|
|
pub fn is_production_mode() -> bool {
|
2020-05-22 14:51:58 +03:00
|
|
|
match get_chain_type() {
|
|
|
|
ChainTypes::Floonet => true,
|
|
|
|
ChainTypes::Mainnet => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
2017-08-22 21:23:54 +03:00
|
|
|
}
|
|
|
|
|
2018-12-29 01:46:21 +03:00
|
|
|
/// Are we in floonet?
|
2019-01-31 12:23:19 +03:00
|
|
|
/// Note: We do not have a corresponding is_mainnet() as we want any tests to be as close
|
|
|
|
/// as possible to "mainnet" configuration as possible.
|
|
|
|
/// We want to avoid missing any mainnet only code paths.
|
2018-12-29 01:46:21 +03:00
|
|
|
pub fn is_floonet() -> bool {
|
2020-05-22 14:51:58 +03:00
|
|
|
match get_chain_type() {
|
|
|
|
ChainTypes::Floonet => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
2018-11-29 01:05:13 +03:00
|
|
|
}
|
|
|
|
|
2018-09-03 14:09:28 +03:00
|
|
|
/// Converts an iterator of block difficulty data to more a more manageable
|
2018-06-14 15:16:14 +03:00
|
|
|
/// vector and pads if needed (which will) only be needed for the first few
|
|
|
|
/// blocks after genesis
|
2018-01-27 10:48:53 +03:00
|
|
|
|
2018-10-13 23:57:01 +03:00
|
|
|
pub fn difficulty_data_to_vector<T>(cursor: T) -> Vec<HeaderInfo>
|
2018-03-04 03:19:54 +03:00
|
|
|
where
|
2018-10-13 23:57:01 +03:00
|
|
|
T: IntoIterator<Item = HeaderInfo>,
|
2018-03-04 03:19:54 +03:00
|
|
|
{
|
2018-01-27 10:48:53 +03:00
|
|
|
// Convert iterator to vector, so we can append to it if necessary
|
2018-10-17 02:55:40 +03:00
|
|
|
let needed_block_count = DIFFICULTY_ADJUST_WINDOW as usize + 1;
|
2018-10-13 23:57:01 +03:00
|
|
|
let mut last_n: Vec<HeaderInfo> = cursor.into_iter().take(needed_block_count).collect();
|
2018-01-27 10:48:53 +03:00
|
|
|
|
|
|
|
// Only needed just after blockchain launch... basically ensures there's
|
|
|
|
// always enough data by simulating perfectly timed pre-genesis
|
|
|
|
// blocks at the genesis difficulty as needed.
|
2018-10-19 22:39:54 +03:00
|
|
|
let n = last_n.len();
|
|
|
|
if needed_block_count > n {
|
|
|
|
let last_ts_delta = if n > 1 {
|
|
|
|
last_n[0].timestamp - last_n[1].timestamp
|
2018-01-27 10:48:53 +03:00
|
|
|
} else {
|
2018-10-19 22:39:54 +03:00
|
|
|
BLOCK_TIME_SEC
|
|
|
|
};
|
|
|
|
let last_diff = last_n[0].difficulty;
|
2018-07-05 15:18:09 +03:00
|
|
|
|
2018-10-19 22:39:54 +03:00
|
|
|
// fill in simulated blocks with values from the previous real block
|
|
|
|
let mut last_ts = last_n.last().unwrap().timestamp;
|
|
|
|
for _ in n..needed_block_count {
|
|
|
|
last_ts = last_ts.saturating_sub(last_ts_delta);
|
2020-02-05 19:02:07 +03:00
|
|
|
last_n.push(HeaderInfo::from_ts_diff(last_ts, last_diff));
|
2018-01-27 10:48:53 +03:00
|
|
|
}
|
|
|
|
}
|
2018-10-19 22:39:54 +03:00
|
|
|
last_n.reverse();
|
2018-01-27 10:48:53 +03:00
|
|
|
last_n
|
|
|
|
}
|