Cuckoo-miner better merged into Grin (#80)

* Adding cuckoo-miner into grin as a proper dependency now
* Defaulting to using cuckoo-miner
* Updates to tests to use cuckoo_miner by default, (using cuckoo16)
This commit is contained in:
Yeastplume
2017-07-18 20:57:09 +00:00
committed by Ignotus Peverell
parent 40090fcdbc
commit 42376e68bc
17 changed files with 223 additions and 133 deletions
+1 -1
View File
@@ -45,7 +45,7 @@ pub const DEFAULT_SIZESHIFT: u8 = 30;
/// Lower Cuckoo size shift for tests and testnet
/// This should be changed to correspond with the
/// loaded plugin if using cuckoo-miner
pub const TEST_SIZESHIFT: u8 = 12;
pub const TEST_SIZESHIFT: u8 = 16;
/// Default Cuckoo Cycle easiness, high enough to have good likeliness to find
/// a solution.
+3 -1
View File
@@ -18,6 +18,7 @@
//! miner will be much faster in almost every environment.
use std::collections::HashSet;
use std::collections::HashMap;
use std::cmp;
use crypto::digest::Digest;
@@ -162,7 +163,8 @@ pub struct Miner {
impl MiningWorker for Miner {
/// Creates a new miner
fn new(ease: u32, sizeshift: u32) -> Miner {
fn new(ease: u32,
sizeshift: u32) -> Miner {
let size = 1 << sizeshift;
let graph = vec![0; size + 1];
let easiness = (ease as u64) * (size as u64) / 100;
+14 -9
View File
@@ -24,6 +24,7 @@
mod siphash;
pub mod cuckoo;
use std::collections::HashMap;
use time;
@@ -42,7 +43,8 @@ use pow::cuckoo::{Cuckoo, Miner, Error};
pub trait MiningWorker {
//This only sets parameters and does initialisation work now
fn new(ease: u32, sizeshift: u32) -> Self;
fn new(ease: u32,
sizeshift: u32) -> Self;
//Actually perform a mining attempt on the given input and
//return a proof if found
@@ -63,14 +65,15 @@ pub fn verify_size(bh: &BlockHeader, cuckoo_sz: u32) -> bool {
/// Uses the much easier Cuckoo20 (mostly for
/// tests).
pub fn pow20(bh: &mut BlockHeader, diff: Difficulty) -> Result<(), Error> {
pow_size(bh, diff, 20)
pub fn pow20<T: MiningWorker>(miner:&mut T, bh: &mut BlockHeader, diff: Difficulty) -> Result<(), Error> {
pow_size(miner, bh, diff, 20)
}
/// Runs a naive single-threaded proof of work computation over the provided
/// block, until the required difficulty target is reached. May take a
/// while for a low target...
pub fn pow_size(bh: &mut BlockHeader, diff: Difficulty, sizeshift: u32) -> Result<(), Error> {
/// Runs a proof of work computation over the provided block using the provided Mining Worker,
/// until the required difficulty target is reached. May take a while for a low target...
pub fn pow_size<T: MiningWorker>(miner:&mut T, bh: &mut BlockHeader,
diff: Difficulty, sizeshift: u32) -> Result<(), Error> {
let start_nonce = bh.nonce;
// try to find a cuckoo cycle on that header hash
@@ -81,7 +84,8 @@ pub fn pow_size(bh: &mut BlockHeader, diff: Difficulty, sizeshift: u32) -> Resul
// if we found a cycle (not guaranteed) and the proof hash is higher that the
// diff, we're all good
if let Ok(proof) = Miner::new(EASINESS, sizeshift).mine(&pow_hash[..]) {
if let Ok(proof) = miner.mine(&pow_hash[..]) {
if proof.to_difficulty() >= diff {
bh.pow = proof;
return Ok(());
@@ -109,7 +113,8 @@ mod test {
fn genesis_pow() {
let mut b = genesis::genesis();
b.header.nonce = 310;
pow_size(&mut b.header, Difficulty::from_num(MINIMUM_DIFFICULTY), 12).unwrap();
let mut internal_miner = cuckoo::Miner::new(EASINESS, 12);
pow_size(&mut internal_miner, &mut b.header, Difficulty::from_num(MINIMUM_DIFFICULTY), 12).unwrap();
assert!(b.header.nonce != 310);
assert!(b.header.pow.to_difficulty() >= Difficulty::from_num(MINIMUM_DIFFICULTY));
assert!(verify_size(&b.header, 12));