2017-08-09 19:40:23 +03:00
|
|
|
// Copyright 2017 The Grin Developers
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
/// 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
|
|
|
use consensus::PROOFSIZE;
|
|
|
|
use consensus::DEFAULT_SIZESHIFT;
|
2017-10-04 20:44:22 +03:00
|
|
|
use consensus::COINBASE_MATURITY;
|
2017-08-09 19:40:23 +03:00
|
|
|
|
|
|
|
/// Define these here, as they should be developer-set, not really tweakable
|
|
|
|
/// by users
|
|
|
|
|
2017-08-10 03:54:10 +03:00
|
|
|
/// Automated testing sizeshift
|
2017-09-29 21:44:25 +03:00
|
|
|
pub const AUTOMATED_TESTING_SIZESHIFT: u8 = 10;
|
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
|
|
|
|
2017-08-10 03:54:10 +03:00
|
|
|
/// User testing sizeshift
|
2017-09-29 21:44:25 +03:00
|
|
|
pub const USER_TESTING_SIZESHIFT: u8 = 16;
|
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-01-19 20:48:18 +03:00
|
|
|
/// The target is the 32-bytes hash block hashes must be lower than.
|
|
|
|
pub const MAX_PROOF_TARGET: [u8; 8] = [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff];
|
|
|
|
|
|
|
|
/// We want to slow this right down for user testing at cuckoo 16, so pick a smaller max
|
|
|
|
pub const MAX_PROOF_TARGET_TESTING: [u8; 8] = [0x05, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff];
|
|
|
|
|
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,
|
|
|
|
|
2017-11-16 00:49:15 +03:00
|
|
|
/// First test network
|
|
|
|
Testnet1,
|
|
|
|
|
2018-01-19 20:48:18 +03:00
|
|
|
/// Second test network
|
|
|
|
Testnet2,
|
|
|
|
|
2017-11-16 00:49:15 +03:00
|
|
|
/// Main production network
|
|
|
|
Mainnet,
|
2017-08-09 19:40:23 +03:00
|
|
|
}
|
|
|
|
|
2017-11-16 01:30:48 +03:00
|
|
|
impl Default for ChainTypes {
|
|
|
|
fn default() -> ChainTypes {
|
|
|
|
ChainTypes::UserTesting
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
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
|
|
|
}
|
|
|
|
|
2017-08-10 03:54:10 +03:00
|
|
|
/// The sizeshift
|
2017-08-09 19:40:23 +03:00
|
|
|
pub fn sizeshift() -> 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 {
|
2017-11-16 00:49:15 +03:00
|
|
|
ChainTypes::AutomatedTesting => AUTOMATED_TESTING_SIZESHIFT,
|
|
|
|
ChainTypes::UserTesting => USER_TESTING_SIZESHIFT,
|
2017-11-17 02:17:56 +03:00
|
|
|
ChainTypes::Testnet1 => USER_TESTING_SIZESHIFT,
|
2018-01-19 20:48:18 +03:00
|
|
|
ChainTypes::Testnet2 => DEFAULT_SIZESHIFT,
|
2017-11-16 00:49:15 +03:00
|
|
|
ChainTypes::Mainnet => DEFAULT_SIZESHIFT,
|
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,
|
|
|
|
ChainTypes::Testnet1 => PROOFSIZE,
|
2018-01-19 20:48:18 +03:00
|
|
|
ChainTypes::Testnet2 => PROOFSIZE,
|
2017-11-16 00:49:15 +03:00
|
|
|
ChainTypes::Mainnet => PROOFSIZE,
|
2017-08-09 19:40:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-04 20:44:22 +03:00
|
|
|
/// Coinbase maturity
|
|
|
|
pub fn coinbase_maturity() -> 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,
|
|
|
|
ChainTypes::Testnet1 => COINBASE_MATURITY,
|
2018-01-19 20:48:18 +03:00
|
|
|
ChainTypes::Testnet2 => COINBASE_MATURITY,
|
2017-11-16 00:49:15 +03:00
|
|
|
ChainTypes::Mainnet => COINBASE_MATURITY,
|
2017-10-04 20:44:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-19 20:48:18 +03:00
|
|
|
/// Max Proof Target
|
|
|
|
pub fn max_proof_target() -> [u8; 8] {
|
|
|
|
let param_ref = CHAIN_TYPE.read().unwrap();
|
|
|
|
match *param_ref {
|
|
|
|
ChainTypes::AutomatedTesting => MAX_PROOF_TARGET_TESTING,
|
|
|
|
ChainTypes::UserTesting => MAX_PROOF_TARGET_TESTING,
|
|
|
|
ChainTypes::Testnet1 => MAX_PROOF_TARGET_TESTING,
|
|
|
|
ChainTypes::Testnet2 => MAX_PROOF_TARGET,
|
|
|
|
ChainTypes::Mainnet => MAX_PROOF_TARGET,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
ChainTypes::Testnet1 == *param_ref ||
|
2018-01-19 20:48:18 +03:00
|
|
|
ChainTypes::Testnet2 == *param_ref ||
|
2017-11-16 00:49:15 +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
|
|
|
|
/// block chain
|
2017-08-12 00:05:59 +03:00
|
|
|
/// at the moment
|
|
|
|
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,
|
2017-09-29 21:44:25 +03:00
|
|
|
// Magic nonce for current genesis block at cuckoo16
|
2017-11-16 00:49:15 +03:00
|
|
|
ChainTypes::UserTesting => 27944,
|
2018-01-19 20:48:18 +03:00
|
|
|
// Magic nonce for genesis block for testnet2 (cuckoo30)
|
2017-08-22 21:23:54 +03:00
|
|
|
|
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
|
|
|
}
|