2018-03-05 22:33:44 +03:00
|
|
|
// Copyright 2018 The Grin Developers
|
2016-10-22 21:35:48 +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.
|
|
|
|
|
2016-10-21 03:06:12 +03:00
|
|
|
//! The proof of work needs to strike a balance between fast header
|
|
|
|
//! verification to avoid DoS attacks and difficulty for block verifiers to
|
|
|
|
//! build new blocks. In addition, mining new blocks should also be as
|
|
|
|
//! difficult on high end custom-made hardware (ASICs) as on commodity hardware
|
2016-10-22 01:02:20 +03:00
|
|
|
//! or smartphones. For this reason we use Cuckoo Cycle (see the cuckoo
|
2016-10-21 03:06:12 +03:00
|
|
|
//! module for more information).
|
|
|
|
//!
|
|
|
|
//! Note that this miner implementation is here mostly for tests and
|
|
|
|
//! reference. It's not optimized for speed.
|
|
|
|
|
2017-08-22 21:23:54 +03:00
|
|
|
#![deny(non_upper_case_globals)]
|
|
|
|
#![deny(non_camel_case_types)]
|
|
|
|
#![deny(non_snake_case)]
|
|
|
|
#![deny(unused_mut)]
|
|
|
|
#![warn(missing_docs)]
|
|
|
|
|
|
|
|
extern crate blake2_rfc as blake2;
|
2017-11-01 02:32:33 +03:00
|
|
|
extern crate rand;
|
2017-08-22 21:23:54 +03:00
|
|
|
extern crate serde;
|
2017-11-01 02:32:33 +03:00
|
|
|
extern crate time;
|
2017-08-22 21:23:54 +03:00
|
|
|
|
2017-10-12 19:56:44 +03:00
|
|
|
extern crate grin_util as util;
|
2017-08-22 21:23:54 +03:00
|
|
|
|
2016-11-27 23:31:15 +03:00
|
|
|
pub mod cuckoo;
|
2018-06-01 22:41:26 +03:00
|
|
|
mod siphash;
|
2018-04-24 11:18:24 +03:00
|
|
|
|
|
|
|
use consensus;
|
|
|
|
use core::target::Difficulty;
|
2018-06-01 22:41:26 +03:00
|
|
|
use core::{Block, BlockHeader};
|
2018-04-24 11:18:24 +03:00
|
|
|
use genesis;
|
2018-06-01 22:41:26 +03:00
|
|
|
use global;
|
2018-04-24 11:18:24 +03:00
|
|
|
use pow::cuckoo::{Cuckoo, Error};
|
2017-07-11 20:11:03 +03:00
|
|
|
|
2017-04-10 09:17:23 +03:00
|
|
|
/// Validates the proof of work of a given header, and that the proof of work
|
|
|
|
/// satisfies the requirements of the header.
|
2018-04-24 11:18:24 +03:00
|
|
|
pub fn verify_size(bh: &BlockHeader, cuckoo_sz: u8) -> bool {
|
2018-06-23 00:59:56 +03:00
|
|
|
Cuckoo::from_hash(bh.pre_pow_hash().as_ref(), cuckoo_sz)
|
2018-06-29 20:41:28 +03:00
|
|
|
.verify(&bh.pow, consensus::EASINESS as u64)
|
2016-10-21 03:06:12 +03:00
|
|
|
}
|
|
|
|
|
2018-04-24 11:18:24 +03:00
|
|
|
/// Mines a genesis block using the internal miner
|
|
|
|
pub fn mine_genesis_block() -> Result<Block, Error> {
|
2018-01-19 20:48:18 +03:00
|
|
|
let mut gen = genesis::genesis_testnet2();
|
2018-03-26 14:07:04 +03:00
|
|
|
if global::is_user_testing_mode() || global::is_automated_testing_mode() {
|
2018-03-19 22:23:58 +03:00
|
|
|
gen = genesis::genesis_dev();
|
|
|
|
gen.header.timestamp = time::now();
|
|
|
|
}
|
2018-03-15 22:16:34 +03:00
|
|
|
|
|
|
|
// total_difficulty on the genesis header *is* the difficulty of that block
|
|
|
|
let genesis_difficulty = gen.header.total_difficulty.clone();
|
2017-09-29 21:44:25 +03:00
|
|
|
|
2018-06-29 20:41:28 +03:00
|
|
|
let sz = global::min_sizeshift();
|
2017-08-22 21:23:54 +03:00
|
|
|
let proof_size = global::proofsize();
|
|
|
|
|
2018-04-24 11:18:24 +03:00
|
|
|
pow_size(&mut gen.header, genesis_difficulty, proof_size, sz).unwrap();
|
2017-11-20 20:35:52 +03:00
|
|
|
Ok(gen)
|
2017-08-22 21:23:54 +03:00
|
|
|
}
|
|
|
|
|
2017-09-29 21:44:25 +03:00
|
|
|
/// Runs a proof of work computation over the provided block using the provided
|
2017-11-16 00:49:15 +03:00
|
|
|
/// Mining Worker, until the required difficulty target is reached. May take a
|
|
|
|
/// while for a low target...
|
2018-04-24 11:18:24 +03:00
|
|
|
pub fn pow_size(
|
2017-11-01 02:32:33 +03:00
|
|
|
bh: &mut BlockHeader,
|
|
|
|
diff: Difficulty,
|
2018-04-24 11:18:24 +03:00
|
|
|
proof_size: usize,
|
|
|
|
sz: u8,
|
2017-11-01 02:32:33 +03:00
|
|
|
) -> Result<(), Error> {
|
2017-02-08 00:48:11 +03:00
|
|
|
let start_nonce = bh.nonce;
|
2016-10-21 03:06:12 +03:00
|
|
|
|
2018-03-04 03:19:54 +03:00
|
|
|
// set the nonce for faster solution finding in user testing
|
|
|
|
if bh.height == 0 && global::is_user_testing_mode() {
|
|
|
|
bh.nonce = global::get_genesis_nonce();
|
2017-08-22 21:23:54 +03:00
|
|
|
}
|
|
|
|
|
2018-03-04 03:19:54 +03:00
|
|
|
// try to find a cuckoo cycle on that header hash
|
|
|
|
loop {
|
|
|
|
// if we found a cycle (not guaranteed) and the proof hash is higher that the
|
|
|
|
// diff, we're all good
|
2018-06-23 00:59:56 +03:00
|
|
|
if let Ok(proof) = cuckoo::Miner::new(bh, consensus::EASINESS, proof_size, sz).mine() {
|
2018-06-01 22:41:26 +03:00
|
|
|
if proof.to_difficulty() >= diff {
|
2018-03-04 03:19:54 +03:00
|
|
|
bh.pow = proof.clone();
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise increment the nonce
|
2018-07-11 00:23:15 +03:00
|
|
|
let (res, _) = bh.nonce.overflowing_add(1);
|
|
|
|
bh.nonce = res;
|
2018-03-04 03:19:54 +03:00
|
|
|
|
|
|
|
// and if we're back where we started, update the time (changes the hash as
|
|
|
|
// well)
|
|
|
|
if bh.nonce == start_nonce {
|
|
|
|
bh.timestamp = time::at_utc(time::Timespec { sec: 0, nsec: 0 });
|
|
|
|
}
|
|
|
|
}
|
2016-10-21 03:06:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
2018-04-24 11:18:24 +03:00
|
|
|
use core::target::Difficulty;
|
|
|
|
use genesis;
|
2018-06-01 22:41:26 +03:00
|
|
|
use global;
|
2016-10-21 03:06:12 +03:00
|
|
|
|
2018-04-24 11:18:24 +03:00
|
|
|
/// We'll be generating genesis blocks differently
|
|
|
|
#[ignore]
|
2016-10-21 03:06:12 +03:00
|
|
|
#[test]
|
|
|
|
fn genesis_pow() {
|
2017-11-16 00:49:15 +03:00
|
|
|
let mut b = genesis::genesis_dev();
|
|
|
|
b.header.nonce = 485;
|
2017-09-29 21:44:25 +03:00
|
|
|
pow_size(
|
|
|
|
&mut b.header,
|
2018-01-27 10:48:53 +03:00
|
|
|
Difficulty::one(),
|
2018-04-24 11:18:24 +03:00
|
|
|
global::proofsize(),
|
2018-06-29 20:41:28 +03:00
|
|
|
global::min_sizeshift(),
|
2017-09-29 21:44:25 +03:00
|
|
|
).unwrap();
|
2017-01-09 23:07:38 +03:00
|
|
|
assert!(b.header.nonce != 310);
|
2018-06-01 22:41:26 +03:00
|
|
|
assert!(b.header.pow.to_difficulty() >= Difficulty::one());
|
2018-06-29 20:41:28 +03:00
|
|
|
assert!(verify_size(&b.header, global::min_sizeshift()));
|
2016-10-21 03:06:12 +03:00
|
|
|
}
|
|
|
|
}
|