2018-01-27 10:48:53 +03:00
|
|
|
// Copyright 2018 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-10-13 23:57:01 +03:00
|
|
|
use consensus::HeaderInfo;
|
2018-07-01 01:36:38 +03:00
|
|
|
use consensus::{
|
2018-10-16 15:19:09 +03:00
|
|
|
BLOCK_TIME_SEC, COINBASE_MATURITY, CUT_THROUGH_HORIZON, SECOND_POW_EDGE_BITS, DIFFICULTY_ADJUST_WINDOW, INITIAL_DIFFICULTY, MEDIAN_TIME_WINDOW, PROOFSIZE,
|
2018-10-16 02:14:23 +03:00
|
|
|
BASE_EDGE_BITS,
|
2018-07-01 01:36:38 +03:00
|
|
|
};
|
2018-10-13 23:57:01 +03:00
|
|
|
use pow::{self, CuckatooContext, EdgeType, PoWContext};
|
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
|
2017-09-29 21:44:25 +03:00
|
|
|
use std::sync::RwLock;
|
2017-08-09 19:40:23 +03:00
|
|
|
|
|
|
|
/// Define these here, as they should be developer-set, not really tweakable
|
|
|
|
/// by users
|
|
|
|
|
2018-10-16 02:14:23 +03:00
|
|
|
/// Automated testing edge_bits
|
|
|
|
pub const AUTOMATED_TESTING_MIN_EDGE_BITS: u8 = 9;
|
2017-08-09 19:40:23 +03:00
|
|
|
|
2017-08-10 03:54:10 +03:00
|
|
|
/// Automated testing proof size
|
2017-09-29 21:44:25 +03:00
|
|
|
pub const AUTOMATED_TESTING_PROOF_SIZE: usize = 4;
|
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-09-04 12:59:55 +03:00
|
|
|
/// Old coinbase maturity
|
|
|
|
/// TODO: obsolete for mainnet together with maturity code below
|
|
|
|
pub const OLD_COINBASE_MATURITY: u64 = 1_000;
|
|
|
|
/// soft-fork around Sep 17 2018 on testnet3
|
|
|
|
pub const COINBASE_MATURITY_FORK_HEIGHT: u64 = 100_000;
|
|
|
|
|
2018-02-10 01:32:16 +03:00
|
|
|
/// Testing cut through horizon in blocks
|
|
|
|
pub const TESTING_CUT_THROUGH_HORIZON: u32 = 20;
|
|
|
|
|
2018-01-27 10:48:53 +03:00
|
|
|
/// Testing initial block difficulty
|
|
|
|
pub const TESTING_INITIAL_DIFFICULTY: u64 = 1;
|
|
|
|
|
2018-03-26 12:48:46 +03:00
|
|
|
/// Testnet 2 initial block difficulty, high to see how it goes
|
|
|
|
pub const TESTNET2_INITIAL_DIFFICULTY: u64 = 1000;
|
2018-01-27 10:48:53 +03:00
|
|
|
|
2018-07-08 00:17:11 +03:00
|
|
|
/// Testnet 3 initial block difficulty, moderately high, taking into account
|
2018-07-02 02:49:49 +03:00
|
|
|
/// a 30x Cuckoo adjustment factor
|
2018-07-08 00:17:11 +03:00
|
|
|
pub const TESTNET3_INITIAL_DIFFICULTY: u64 = 30000;
|
2018-07-02 02:49:49 +03:00
|
|
|
|
2018-10-10 12:09:44 +03:00
|
|
|
/// Testnet 4 initial block difficulty
|
|
|
|
pub const TESTNET4_INITIAL_DIFFICULTY: u64 = 1;
|
|
|
|
|
2018-10-13 19:34:16 +03:00
|
|
|
/// Trigger compaction check on average every 1440 blocks (i.e. one day) for FAST_SYNC_NODE,
|
|
|
|
/// roll the dice on every block to decide,
|
|
|
|
/// all blocks lower than (BodyHead.height - CUT_THROUGH_HORIZON) will be removed.
|
|
|
|
pub const COMPACTION_CHECK: u64 = 1440;
|
|
|
|
|
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.
|
2017-11-13 18:24:49 +03:00
|
|
|
#[derive(Debug, Clone, 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-03-04 03:19:54 +03:00
|
|
|
/// First test network
|
2017-11-16 00:49:15 +03:00
|
|
|
Testnet1,
|
2018-01-19 20:48:18 +03:00
|
|
|
/// Second test network
|
|
|
|
Testnet2,
|
2018-07-06 20:40:43 +03:00
|
|
|
/// Third test network
|
2018-07-02 02:49:49 +03:00
|
|
|
Testnet3,
|
2018-10-10 12:09:44 +03:00
|
|
|
/// Fourth test network
|
|
|
|
Testnet4,
|
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
|
|
|
}
|
|
|
|
|
2017-11-16 01:30:48 +03:00
|
|
|
impl Default for ChainTypes {
|
|
|
|
fn default() -> ChainTypes {
|
2018-10-10 12:09:44 +03:00
|
|
|
ChainTypes::Testnet4
|
2017-11-16 01:30:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-28 13:53:14 +03:00
|
|
|
/// PoW test mining and verifier context
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
|
|
|
pub enum PoWContextTypes {
|
|
|
|
/// Classic Cuckoo
|
|
|
|
Cuckoo,
|
|
|
|
/// Bleeding edge Cuckatoo
|
|
|
|
Cuckatoo,
|
|
|
|
}
|
|
|
|
|
2017-08-09 19:40:23 +03:00
|
|
|
lazy_static!{
|
2017-11-01 02:32:33 +03:00
|
|
|
/// The mining parameter mode
|
2017-11-16 00:49:15 +03:00
|
|
|
pub static ref CHAIN_TYPE: RwLock<ChainTypes> =
|
|
|
|
RwLock::new(ChainTypes::Mainnet);
|
2018-09-28 13:53:14 +03:00
|
|
|
|
|
|
|
/// PoW context type to instantiate
|
|
|
|
pub static ref POW_CONTEXT_TYPE: RwLock<PoWContextTypes> =
|
|
|
|
RwLock::new(PoWContextTypes::Cuckoo);
|
2017-08-09 19:40:23 +03:00
|
|
|
}
|
|
|
|
|
2017-08-10 03:54:10 +03:00
|
|
|
/// Set the mining mode
|
2017-11-16 00:49:15 +03:00
|
|
|
pub fn set_mining_mode(mode: ChainTypes) {
|
|
|
|
let mut param_ref = CHAIN_TYPE.write().unwrap();
|
2017-09-29 21:44:25 +03:00
|
|
|
*param_ref = mode;
|
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>(
|
|
|
|
edge_bits: u8,
|
|
|
|
proof_size: usize,
|
|
|
|
max_sols: u32,
|
|
|
|
) -> Result<Box<impl PoWContext<T>>, pow::Error>
|
|
|
|
where
|
|
|
|
T: EdgeType,
|
|
|
|
{
|
2018-10-15 22:24:36 +03:00
|
|
|
CuckatooContext::<T>::new(edge_bits, proof_size, max_sols)
|
2018-10-10 12:09:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the type of the pos
|
|
|
|
pub fn pow_type() -> PoWContextTypes {
|
|
|
|
PoWContextTypes::Cuckatoo
|
2018-09-28 13:53:14 +03:00
|
|
|
}
|
|
|
|
|
2018-10-16 02:14:23 +03:00
|
|
|
/// The minimum acceptable edge_bits
|
|
|
|
pub fn min_edge_bits() -> u8 {
|
2017-11-16 00:49:15 +03:00
|
|
|
let param_ref = CHAIN_TYPE.read().unwrap();
|
2017-08-09 19:40:23 +03:00
|
|
|
match *param_ref {
|
2018-10-16 02:14:23 +03:00
|
|
|
ChainTypes::AutomatedTesting => AUTOMATED_TESTING_MIN_EDGE_BITS,
|
|
|
|
ChainTypes::UserTesting => USER_TESTING_MIN_EDGE_BITS,
|
|
|
|
ChainTypes::Testnet1 => USER_TESTING_MIN_EDGE_BITS,
|
2018-10-16 15:19:09 +03:00
|
|
|
_ => SECOND_POW_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 {
|
2018-06-29 20:41:28 +03:00
|
|
|
let param_ref = CHAIN_TYPE.read().unwrap();
|
|
|
|
match *param_ref {
|
2018-10-16 02:14:23 +03:00
|
|
|
ChainTypes::AutomatedTesting => AUTOMATED_TESTING_MIN_EDGE_BITS,
|
|
|
|
ChainTypes::UserTesting => USER_TESTING_MIN_EDGE_BITS,
|
|
|
|
ChainTypes::Testnet1 => 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 {
|
2017-11-16 00:49:15 +03:00
|
|
|
let param_ref = CHAIN_TYPE.read().unwrap();
|
2017-08-09 19:40:23 +03:00
|
|
|
match *param_ref {
|
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-09-04 12:59:55 +03:00
|
|
|
/// Coinbase maturity for coinbases to be spent at given height
|
|
|
|
pub fn coinbase_maturity(height: u64) -> u64 {
|
2017-11-16 00:49:15 +03:00
|
|
|
let param_ref = CHAIN_TYPE.read().unwrap();
|
2017-10-04 20:44:22 +03:00
|
|
|
match *param_ref {
|
2017-11-16 00:49:15 +03:00
|
|
|
ChainTypes::AutomatedTesting => AUTOMATED_TESTING_COINBASE_MATURITY,
|
|
|
|
ChainTypes::UserTesting => USER_TESTING_COINBASE_MATURITY,
|
2018-09-19 01:12:57 +03:00
|
|
|
_ => if height < COINBASE_MATURITY_FORK_HEIGHT {
|
|
|
|
OLD_COINBASE_MATURITY
|
|
|
|
} else {
|
|
|
|
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 {
|
|
|
|
let param_ref = CHAIN_TYPE.read().unwrap();
|
|
|
|
match *param_ref {
|
|
|
|
ChainTypes::AutomatedTesting => TESTING_INITIAL_DIFFICULTY,
|
|
|
|
ChainTypes::UserTesting => TESTING_INITIAL_DIFFICULTY,
|
|
|
|
ChainTypes::Testnet1 => TESTING_INITIAL_DIFFICULTY,
|
|
|
|
ChainTypes::Testnet2 => TESTNET2_INITIAL_DIFFICULTY,
|
2018-07-04 00:25:57 +03:00
|
|
|
ChainTypes::Testnet3 => TESTNET3_INITIAL_DIFFICULTY,
|
2018-10-10 12:09:44 +03:00
|
|
|
ChainTypes::Testnet4 => TESTNET4_INITIAL_DIFFICULTY,
|
2018-01-27 10:48:53 +03:00
|
|
|
ChainTypes::Mainnet => INITIAL_DIFFICULTY,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
let param_ref = CHAIN_TYPE.read().unwrap();
|
|
|
|
match *param_ref {
|
|
|
|
ChainTypes::AutomatedTesting => TESTING_CUT_THROUGH_HORIZON,
|
|
|
|
ChainTypes::UserTesting => TESTING_CUT_THROUGH_HORIZON,
|
2018-07-08 00:17:11 +03:00
|
|
|
_ => CUT_THROUGH_HORIZON,
|
2018-02-10 01:32:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-10 03:54:10 +03:00
|
|
|
/// Are we in automated testing mode?
|
2017-08-09 19:40:23 +03:00
|
|
|
pub fn is_automated_testing_mode() -> bool {
|
2017-11-16 00:49:15 +03:00
|
|
|
let param_ref = CHAIN_TYPE.read().unwrap();
|
|
|
|
ChainTypes::AutomatedTesting == *param_ref
|
2017-11-13 18:24:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Are we in user testing mode?
|
|
|
|
pub fn is_user_testing_mode() -> bool {
|
2017-11-16 00:49:15 +03:00
|
|
|
let param_ref = CHAIN_TYPE.read().unwrap();
|
|
|
|
ChainTypes::UserTesting == *param_ref
|
2017-08-09 19:40:23 +03:00
|
|
|
}
|
2017-08-12 00:05:59 +03:00
|
|
|
|
2017-11-16 00:49:15 +03:00
|
|
|
/// Are we in production mode (a live public network)?
|
2017-08-22 21:23:54 +03:00
|
|
|
pub fn is_production_mode() -> bool {
|
2017-11-16 00:49:15 +03:00
|
|
|
let param_ref = CHAIN_TYPE.read().unwrap();
|
2018-07-01 01:36:38 +03:00
|
|
|
ChainTypes::Testnet1 == *param_ref
|
|
|
|
|| ChainTypes::Testnet2 == *param_ref
|
2018-07-02 02:49:49 +03:00
|
|
|
|| ChainTypes::Testnet3 == *param_ref
|
2018-10-10 12:09:44 +03:00
|
|
|
|| ChainTypes::Testnet4 == *param_ref
|
2018-03-04 03:19:54 +03:00
|
|
|
|| ChainTypes::Mainnet == *param_ref
|
2017-08-22 21:23:54 +03:00
|
|
|
}
|
|
|
|
|
2017-09-29 21:44:25 +03:00
|
|
|
/// Helper function to get a nonce known to create a valid POW on
|
2017-08-12 00:05:59 +03:00
|
|
|
/// the genesis block, to prevent it taking ages. Should be fine for now
|
2017-09-29 21:44:25 +03:00
|
|
|
/// as the genesis block POW solution turns out to be the same for every new
|
2018-01-27 10:48:53 +03:00
|
|
|
/// block chain at the moment
|
2017-08-12 00:05:59 +03:00
|
|
|
pub fn get_genesis_nonce() -> u64 {
|
2017-11-16 00:49:15 +03:00
|
|
|
let param_ref = CHAIN_TYPE.read().unwrap();
|
2017-08-12 00:05:59 +03:00
|
|
|
match *param_ref {
|
2017-09-29 21:44:25 +03:00
|
|
|
// won't make a difference
|
2017-11-16 00:49:15 +03:00
|
|
|
ChainTypes::AutomatedTesting => 0,
|
2018-10-16 02:14:23 +03:00
|
|
|
// Magic nonce for current genesis block at cuckatoo15
|
2017-11-16 00:49:15 +03:00
|
|
|
ChainTypes::UserTesting => 27944,
|
2018-10-16 02:14:23 +03:00
|
|
|
// Magic nonce for genesis block for testnet2 (cuckatoo29)
|
2017-11-16 00:49:15 +03:00
|
|
|
_ => panic!("Pre-set"),
|
2017-11-13 18:24:49 +03:00
|
|
|
}
|
2017-08-22 21:23:54 +03:00
|
|
|
}
|
2018-01-27 10:48:53 +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
|
|
|
|
let needed_block_count = (MEDIAN_TIME_WINDOW + DIFFICULTY_ADJUST_WINDOW) as usize;
|
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
|
|
|
|
|
|
|
// Sort blocks from earliest to latest (to keep conceptually easier)
|
|
|
|
last_n.reverse();
|
|
|
|
// 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.
|
|
|
|
let block_count_difference = needed_block_count - last_n.len();
|
|
|
|
if block_count_difference > 0 {
|
|
|
|
// Collect any real data we have
|
2018-10-13 23:57:01 +03:00
|
|
|
let mut live_intervals: Vec<HeaderInfo> = last_n
|
2018-03-04 03:19:54 +03:00
|
|
|
.iter()
|
2018-10-13 23:57:01 +03:00
|
|
|
.map(|b| HeaderInfo::from_ts_diff(b.timestamp, b.difficulty))
|
2018-01-27 10:48:53 +03:00
|
|
|
.collect();
|
|
|
|
for i in (1..live_intervals.len()).rev() {
|
2018-01-31 13:56:41 +03:00
|
|
|
// prevents issues with very fast automated test chains
|
2018-10-13 23:57:01 +03:00
|
|
|
if live_intervals[i - 1].timestamp > live_intervals[i].timestamp {
|
|
|
|
live_intervals[i].timestamp = 0;
|
2018-01-31 13:56:41 +03:00
|
|
|
} else {
|
2018-10-13 23:57:01 +03:00
|
|
|
live_intervals[i].timestamp =
|
|
|
|
live_intervals[i].timestamp - live_intervals[i - 1].timestamp;
|
2018-01-31 13:56:41 +03:00
|
|
|
}
|
2018-01-27 10:48:53 +03:00
|
|
|
}
|
|
|
|
// Remove genesis "interval"
|
|
|
|
if live_intervals.len() > 1 {
|
|
|
|
live_intervals.remove(0);
|
|
|
|
} else {
|
|
|
|
//if it's just genesis, adjust the interval
|
2018-10-13 23:57:01 +03:00
|
|
|
live_intervals[0].timestamp = BLOCK_TIME_SEC;
|
2018-01-27 10:48:53 +03:00
|
|
|
}
|
|
|
|
let mut interval_index = live_intervals.len() - 1;
|
2018-10-13 23:57:01 +03:00
|
|
|
let mut last_ts = last_n.first().unwrap().timestamp;
|
|
|
|
let last_diff = live_intervals[live_intervals.len() - 1].difficulty;
|
2018-07-05 15:18:09 +03:00
|
|
|
// fill in simulated blocks with values from the previous real block
|
|
|
|
|
2018-01-27 10:48:53 +03:00
|
|
|
for _ in 0..block_count_difference {
|
2018-10-13 23:57:01 +03:00
|
|
|
last_ts = last_ts.saturating_sub(live_intervals[live_intervals.len() - 1].timestamp);
|
|
|
|
last_n.insert(0, HeaderInfo::from_ts_diff(last_ts, last_diff.clone()));
|
2018-01-27 10:48:53 +03:00
|
|
|
interval_index = match interval_index {
|
2018-03-04 03:19:54 +03:00
|
|
|
0 => live_intervals.len() - 1,
|
2018-01-27 10:48:53 +03:00
|
|
|
_ => interval_index - 1,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
last_n
|
|
|
|
}
|