add premined pow solution for cuckoo16/UserTesting (#260)

* add premined pow solution for cuckoo16/UserTesting
faster startup time

* only check premined solution on genesis block (height 0)
This commit is contained in:
AntiochP 2017-11-13 10:24:49 -05:00 committed by GitHub
parent 54dbda6bc2
commit 596bbd9b6e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 88 additions and 74 deletions

View file

@ -68,11 +68,9 @@ impl Chain {
} }
} }
/// Initializes the blockchain and returns a new Chain instance. Does a /// Initializes the blockchain and returns a new Chain instance. Does a check
/// check
/// on the current chain head to make sure it exists and creates one based /// on the current chain head to make sure it exists and creates one based
/// on /// on the genesis block if necessary.
/// the genesis block if necessary.
pub fn init( pub fn init(
db_root: String, db_root: String,
adapter: Arc<ChainAdapter>, adapter: Arc<ChainAdapter>,
@ -96,7 +94,13 @@ impl Chain {
// saving a new tip based on genesis // saving a new tip based on genesis
let tip = Tip::new(gen.hash()); let tip = Tip::new(gen.hash());
chain_store.save_head(&tip)?; chain_store.save_head(&tip)?;
info!(LOGGER, "Saved genesis block with hash {}", gen.hash()); info!(
LOGGER,
"Saved genesis block with hash: {:?}, nonce: {:?}, pow: {:?}",
gen.hash(),
gen.header.nonce,
gen.header.pow,
);
tip tip
} }
Err(e) => return Err(Error::StoreErr(e, "chain init load head".to_owned())), Err(e) => return Err(Error::StoreErr(e, "chain init load head".to_owned())),

View file

@ -48,7 +48,7 @@ pub const AUTOMATED_TESTING_COINBASE_MATURITY: u64 = 3;
pub const USER_TESTING_COINBASE_MATURITY: u64 = 3; pub const USER_TESTING_COINBASE_MATURITY: u64 = 3;
/// Mining parameter modes /// Mining parameter modes
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum MiningParameterMode { pub enum MiningParameterMode {
/// For CI testing /// For CI testing
AutomatedTesting, AutomatedTesting,
@ -105,21 +105,19 @@ pub fn coinbase_maturity() -> u64 {
/// Are we in automated testing mode? /// Are we in automated testing mode?
pub fn is_automated_testing_mode() -> bool { pub fn is_automated_testing_mode() -> bool {
let param_ref = MINING_PARAMETER_MODE.read().unwrap(); let param_ref = MINING_PARAMETER_MODE.read().unwrap();
if let MiningParameterMode::AutomatedTesting = *param_ref { MiningParameterMode::AutomatedTesting == *param_ref
return true;
} else {
return false;
} }
/// Are we in user testing mode?
pub fn is_user_testing_mode() -> bool {
let param_ref = MINING_PARAMETER_MODE.read().unwrap();
MiningParameterMode::UserTesting == *param_ref
} }
/// Are we in production mode? /// Are we in production mode?
pub fn is_production_mode() -> bool { pub fn is_production_mode() -> bool {
let param_ref = MINING_PARAMETER_MODE.read().unwrap(); let param_ref = MINING_PARAMETER_MODE.read().unwrap();
if let MiningParameterMode::Production = *param_ref { MiningParameterMode::Production == *param_ref
return true;
} else {
return false;
}
} }
@ -135,19 +133,27 @@ pub fn get_genesis_nonce() -> u64 {
// won't make a difference // won't make a difference
MiningParameterMode::AutomatedTesting => 0, MiningParameterMode::AutomatedTesting => 0,
// Magic nonce for current genesis block at cuckoo16 // Magic nonce for current genesis block at cuckoo16
MiningParameterMode::UserTesting => 22141, MiningParameterMode::UserTesting => 27944,
// Magic nonce for current genesis at cuckoo30 // Magic nonce for current genesis at cuckoo30
MiningParameterMode::Production => 1429942738856787200, MiningParameterMode::Production => 1429942738856787200,
} }
} }
/// Returns the genesis POW for production, because it takes far too long to /// Returns the genesis POW for cuckoo16 (UserTesting) and cuckoo30 (Production)
/// mine at production values
/// using the internal miner
pub fn get_genesis_pow() -> [u32; 42] { pub fn get_genesis_pow() -> [u32; 42] {
// TODO: This is diff 26, probably just want a 10: mine one let param_ref = MINING_PARAMETER_MODE.read().unwrap();
[ match *param_ref {
// pow solution for genesis block at cuckoo16
MiningParameterMode::UserTesting => [
0x37f, 0x9f6, 0x136d, 0x13d3, 0x155e, 0x16dd, 0x186b, 0x1b11, 0x208e, 0x23cd,
0x24d1, 0x278f, 0x2a1b, 0x2a28, 0x2a44, 0x2ae2, 0x2c37, 0x36af, 0x391d, 0x3c2e,
0x3d9a, 0x3f00, 0x429f, 0x45b2, 0x47ce, 0x47f1, 0x492f, 0x4bd8, 0x4fee, 0x51f0,
0x5207, 0x58e5, 0x5905, 0x5aca, 0x5dfb, 0x628d, 0x7310, 0x75e5, 0x76d4, 0x76df,
0x77bd, 0x7ab9
],
// pow solution for genesis block at cuckoo30
// TODO - likely this is no longer correct (block header changes)
MiningParameterMode::Production => [
7444824, 7444824,
11926557, 11926557,
28520390, 28520390,
@ -190,5 +196,8 @@ pub fn get_genesis_pow() -> [u32; 42] {
487576917, 487576917,
495653489, 495653489,
501862896, 501862896,
] ],
// TODO - for completeness do we provide one here for AutomatedTesting?
_ => panic!("unexpected"),
}
} }

View file

@ -97,9 +97,8 @@ pub fn pow20<T: MiningWorker>(
/// Otherwise, /// Otherwise,
/// uses the internal miner /// uses the internal miner
/// ///
pub fn mine_genesis_block(miner_config: Option<types::MinerConfig>) -> Option<core::core::Block> { pub fn mine_genesis_block(miner_config: Option<types::MinerConfig>) -> Option<core::core::Block> {
info!(util::LOGGER, "Starting miner loop for Genesis Block"); info!(util::LOGGER, "Genesis block not found, initializing...");
let mut gen = genesis::genesis(); let mut gen = genesis::genesis();
let diff = gen.header.difficulty.clone(); let diff = gen.header.difficulty.clone();
@ -132,14 +131,16 @@ pub fn pow_size<T: MiningWorker + ?Sized>(
) -> Result<(), Error> { ) -> Result<(), Error> {
let start_nonce = bh.nonce; let start_nonce = bh.nonce;
// if we're in production mode, try the pre-mined solution first // try the pre-mined solution first for the genesis block (height 0)
if global::is_production_mode() { if bh.height == 0 {
if global::is_production_mode() || global::is_user_testing_mode() {
let p = Proof::new(global::get_genesis_pow().to_vec()); let p = Proof::new(global::get_genesis_pow().to_vec());
if p.clone().to_difficulty() >= diff { if p.clone().to_difficulty() >= diff {
bh.pow = p; bh.pow = p;
return Ok(()); return Ok(());
} }
} }
}
// try to find a cuckoo cycle on that header hash // try to find a cuckoo cycle on that header hash
loop { loop {