Ci mode fixes (#86)
* playing around with changing cuckoo sizes on the fly * modifying tests to use global cuckoo parameters, and checking results * check for pow size * Changing global function names, and removing length from POW serialization
This commit is contained in:
committed by
Ignotus Peverell
parent
d32ab967f0
commit
131ea2f799
@@ -35,15 +35,9 @@ pub const BLOCK_TIME_SEC: u64 = 60;
|
||||
/// Cuckoo-cycle proof size (cycle length)
|
||||
pub const PROOFSIZE: usize = 42;
|
||||
|
||||
|
||||
/// Default Cuckoo Cycle size shift used for mining and validating.
|
||||
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 = 16;
|
||||
|
||||
/// Default Cuckoo Cycle easiness, high enough to have good likeliness to find
|
||||
/// a solution.
|
||||
pub const EASINESS: u32 = 50;
|
||||
|
||||
@@ -24,9 +24,11 @@ use core::{Input, Output, Proof, TxKernel, Transaction, COINBASE_KERNEL, COINBAS
|
||||
use core::transaction::merkle_inputs_outputs;
|
||||
use consensus::REWARD;
|
||||
use consensus::MINIMUM_DIFFICULTY;
|
||||
use consensus::PROOFSIZE;
|
||||
use core::hash::{Hash, Hashed, ZERO_HASH};
|
||||
use core::target::Difficulty;
|
||||
use ser::{self, Readable, Reader, Writeable, Writer};
|
||||
use global;
|
||||
|
||||
bitflags! {
|
||||
/// Options for block validation
|
||||
@@ -62,6 +64,7 @@ pub struct BlockHeader {
|
||||
|
||||
impl Default for BlockHeader {
|
||||
fn default() -> BlockHeader {
|
||||
let proof_size = global::proofsize();
|
||||
BlockHeader {
|
||||
height: 0,
|
||||
previous: ZERO_HASH,
|
||||
@@ -72,7 +75,7 @@ impl Default for BlockHeader {
|
||||
tx_merkle: ZERO_HASH,
|
||||
features: DEFAULT_BLOCK,
|
||||
nonce: 0,
|
||||
pow: Proof::zero(),
|
||||
pow: Proof::zero(proof_size),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -279,7 +282,7 @@ impl Block {
|
||||
height: prev.height + 1,
|
||||
timestamp: time::now(),
|
||||
previous: prev.hash(),
|
||||
total_difficulty: prev.pow.to_difficulty() + prev.total_difficulty.clone(),
|
||||
total_difficulty: prev.pow.clone().to_difficulty() + prev.total_difficulty.clone(),
|
||||
..Default::default()
|
||||
},
|
||||
inputs: inputs,
|
||||
|
||||
+43
-20
@@ -30,13 +30,14 @@ use std::cmp::Ordering;
|
||||
use secp::{self, Secp256k1};
|
||||
use secp::pedersen::*;
|
||||
|
||||
use consensus::PROOFSIZE;
|
||||
pub use self::block::{Block, BlockHeader, DEFAULT_BLOCK};
|
||||
pub use self::transaction::{Transaction, Input, Output, TxKernel, COINBASE_KERNEL,
|
||||
COINBASE_OUTPUT, DEFAULT_OUTPUT};
|
||||
use self::hash::{Hash, Hashed, ZERO_HASH};
|
||||
use ser::{Writeable, Writer, Reader, Readable, Error};
|
||||
|
||||
use global;
|
||||
|
||||
/// Implemented by types that hold inputs and outputs including Pedersen
|
||||
/// commitments. Handles the collection of the commitments as well as their
|
||||
/// summing, taking potential explicit overages of fees into account.
|
||||
@@ -81,15 +82,17 @@ pub trait Committed {
|
||||
}
|
||||
|
||||
/// Proof of work
|
||||
#[derive(Copy)]
|
||||
pub struct Proof(pub [u32; PROOFSIZE]);
|
||||
pub struct Proof {
|
||||
pub nonces:Vec<u32>,
|
||||
pub proof_size: usize,
|
||||
}
|
||||
|
||||
impl fmt::Debug for Proof {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
try!(write!(f, "Cuckoo("));
|
||||
for (i, val) in self.0[..].iter().enumerate() {
|
||||
for (i, val) in self.nonces[..].iter().enumerate() {
|
||||
try!(write!(f, "{:x}", val));
|
||||
if i < PROOFSIZE - 1 {
|
||||
if i < self.nonces.len() - 1 {
|
||||
try!(write!(f, " "));
|
||||
}
|
||||
}
|
||||
@@ -98,39 +101,58 @@ impl fmt::Debug for Proof {
|
||||
}
|
||||
impl PartialOrd for Proof {
|
||||
fn partial_cmp(&self, other: &Proof) -> Option<Ordering> {
|
||||
self.0.partial_cmp(&other.0)
|
||||
self.nonces.partial_cmp(&other.nonces)
|
||||
}
|
||||
}
|
||||
impl PartialEq for Proof {
|
||||
fn eq(&self, other: &Proof) -> bool {
|
||||
self.0[..] == other.0[..]
|
||||
self.nonces[..] == other.nonces[..]
|
||||
}
|
||||
}
|
||||
impl Eq for Proof {}
|
||||
impl Clone for Proof {
|
||||
fn clone(&self) -> Proof {
|
||||
*self
|
||||
let mut out_nonces = Vec::new();
|
||||
for n in self.nonces.iter() {
|
||||
out_nonces.push(*n as u32);
|
||||
}
|
||||
Proof {
|
||||
proof_size: out_nonces.len(),
|
||||
nonces: out_nonces,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Proof {
|
||||
|
||||
/// Builds a proof with all bytes zeroed out
|
||||
pub fn zero() -> Proof {
|
||||
Proof([0; PROOFSIZE])
|
||||
pub fn new(in_nonces:Vec<u32>) -> Proof {
|
||||
Proof {
|
||||
proof_size: in_nonces.len(),
|
||||
nonces: in_nonces,
|
||||
}
|
||||
}
|
||||
|
||||
/// Builds a proof with all bytes zeroed out
|
||||
pub fn zero(proof_size:usize) -> Proof {
|
||||
Proof {
|
||||
proof_size: proof_size,
|
||||
nonces: vec![0;proof_size],
|
||||
}
|
||||
}
|
||||
|
||||
/// Converts the proof to a vector of u64s
|
||||
pub fn to_u64s(&self) -> Vec<u64> {
|
||||
let mut nonces = Vec::with_capacity(PROOFSIZE);
|
||||
for n in self.0.iter() {
|
||||
nonces.push(*n as u64);
|
||||
let mut out_nonces = Vec::with_capacity(self.proof_size);
|
||||
for n in self.nonces.iter() {
|
||||
out_nonces.push(*n as u64);
|
||||
}
|
||||
nonces
|
||||
out_nonces
|
||||
}
|
||||
|
||||
/// Converts the proof to a vector of u32s
|
||||
pub fn to_u32s(&self) -> Vec<u32> {
|
||||
self.0.to_vec()
|
||||
self.clone().nonces
|
||||
}
|
||||
|
||||
/// Converts the proof to a proof-of-work Target so they can be compared.
|
||||
@@ -142,18 +164,19 @@ impl Proof {
|
||||
|
||||
impl Readable for Proof {
|
||||
fn read(reader: &mut Reader) -> Result<Proof, Error> {
|
||||
let mut pow = [0u32; PROOFSIZE];
|
||||
for n in 0..PROOFSIZE {
|
||||
let proof_size = global::proofsize();
|
||||
let mut pow = vec![0u32; proof_size];
|
||||
for n in 0..proof_size {
|
||||
pow[n] = try!(reader.read_u32());
|
||||
}
|
||||
Ok(Proof(pow))
|
||||
Ok(Proof::new(pow))
|
||||
}
|
||||
}
|
||||
|
||||
impl Writeable for Proof {
|
||||
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), Error> {
|
||||
for n in 0..PROOFSIZE {
|
||||
try!(writer.write_u32(self.0[n]));
|
||||
for n in 0..self.proof_size {
|
||||
try!(writer.write_u32(self.nonces[n]));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
+4
-1
@@ -18,12 +18,15 @@ use time;
|
||||
|
||||
use core;
|
||||
use consensus::MINIMUM_DIFFICULTY;
|
||||
use consensus::PROOFSIZE;
|
||||
use core::hash::Hashed;
|
||||
use core::target::Difficulty;
|
||||
use global;
|
||||
|
||||
/// Genesis block definition. It has no rewards, no inputs, no outputs, no
|
||||
/// fees and a height of zero.
|
||||
pub fn genesis() -> core::Block {
|
||||
let proof_size = global::proofsize();
|
||||
core::Block {
|
||||
header: core::BlockHeader {
|
||||
height: 0,
|
||||
@@ -40,7 +43,7 @@ pub fn genesis() -> core::Block {
|
||||
tx_merkle: [].hash(),
|
||||
features: core::DEFAULT_BLOCK,
|
||||
nonce: 0,
|
||||
pow: core::Proof::zero(), // TODO get actual PoW solution
|
||||
pow: core::Proof::zero(proof_size), // TODO get actual PoW solution
|
||||
},
|
||||
inputs: vec![],
|
||||
outputs: vec![],
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
// 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
|
||||
|
||||
use std::sync::{RwLock};
|
||||
use consensus::PROOFSIZE;
|
||||
use consensus::DEFAULT_SIZESHIFT;
|
||||
|
||||
/// Define these here, as they should be developer-set, not really tweakable
|
||||
/// by users
|
||||
|
||||
pub const AUTOMATED_TESTING_SIZESHIFT:u8 = 10;
|
||||
|
||||
pub const AUTOMATED_TESTING_PROOF_SIZE:usize = 4;
|
||||
|
||||
pub const USER_TESTING_SIZESHIFT:u8 = 16;
|
||||
|
||||
pub const USER_TESTING_PROOF_SIZE:usize = 42;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub enum MiningParameterMode {
|
||||
/// For CI testing
|
||||
AutomatedTesting,
|
||||
|
||||
/// For User testing
|
||||
UserTesting,
|
||||
|
||||
/// For production, use the values in consensus.rs
|
||||
Production,
|
||||
}
|
||||
|
||||
lazy_static!{
|
||||
pub static ref MINING_PARAMETER_MODE: RwLock<MiningParameterMode> = RwLock::new(MiningParameterMode::Production);
|
||||
}
|
||||
|
||||
pub fn set_mining_mode(mode:MiningParameterMode){
|
||||
let mut param_ref=MINING_PARAMETER_MODE.write().unwrap();
|
||||
*param_ref=mode;
|
||||
}
|
||||
|
||||
pub fn sizeshift() -> u8 {
|
||||
let param_ref=MINING_PARAMETER_MODE.read().unwrap();
|
||||
match *param_ref {
|
||||
MiningParameterMode::AutomatedTesting => AUTOMATED_TESTING_SIZESHIFT,
|
||||
MiningParameterMode::UserTesting => USER_TESTING_SIZESHIFT,
|
||||
MiningParameterMode::Production => DEFAULT_SIZESHIFT,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn proofsize() -> usize {
|
||||
let param_ref=MINING_PARAMETER_MODE.read().unwrap();
|
||||
match *param_ref {
|
||||
MiningParameterMode::AutomatedTesting => AUTOMATED_TESTING_PROOF_SIZE,
|
||||
MiningParameterMode::UserTesting => USER_TESTING_PROOF_SIZE,
|
||||
MiningParameterMode::Production => PROOFSIZE,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_automated_testing_mode() -> bool {
|
||||
let param_ref=MINING_PARAMETER_MODE.read().unwrap();
|
||||
if let MiningParameterMode::AutomatedTesting=*param_ref {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,6 +33,8 @@ extern crate serde;
|
||||
#[macro_use]
|
||||
extern crate serde_derive;
|
||||
extern crate time;
|
||||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
|
||||
#[macro_use]
|
||||
pub mod macros;
|
||||
@@ -42,3 +44,4 @@ pub mod core;
|
||||
pub mod genesis;
|
||||
pub mod pow;
|
||||
pub mod ser;
|
||||
pub mod global;
|
||||
|
||||
+56
-54
@@ -23,7 +23,6 @@ use std::cmp;
|
||||
use crypto::digest::Digest;
|
||||
use crypto::sha2::Sha256;
|
||||
|
||||
use consensus::PROOFSIZE;
|
||||
use core::Proof;
|
||||
use pow::siphash::siphash24;
|
||||
use pow::MiningWorker;
|
||||
@@ -101,9 +100,9 @@ impl Cuckoo {
|
||||
pub fn verify(&self, proof: Proof, ease: u64) -> bool {
|
||||
let easiness = ease * (self.size as u64) / 100;
|
||||
let nonces = proof.to_u64s();
|
||||
let mut us = [0; PROOFSIZE];
|
||||
let mut vs = [0; PROOFSIZE];
|
||||
for n in 0..PROOFSIZE {
|
||||
let mut us = vec![0; proof.proof_size];
|
||||
let mut vs = vec![0; proof.proof_size];
|
||||
for n in 0..proof.proof_size {
|
||||
if nonces[n] >= easiness || (n != 0 && nonces[n] <= nonces[n - 1]) {
|
||||
return false;
|
||||
}
|
||||
@@ -111,10 +110,10 @@ impl Cuckoo {
|
||||
vs[n] = self.new_node(nonces[n], 1);
|
||||
}
|
||||
let mut i = 0;
|
||||
let mut count = PROOFSIZE;
|
||||
let mut count = proof.proof_size;
|
||||
loop {
|
||||
let mut j = i;
|
||||
for k in 0..PROOFSIZE {
|
||||
for k in 0..proof.proof_size {
|
||||
// find unique other j with same vs[j]
|
||||
if k != i && vs[k] == vs[i] {
|
||||
if j != i {
|
||||
@@ -127,7 +126,7 @@ impl Cuckoo {
|
||||
return false;
|
||||
}
|
||||
i = j;
|
||||
for k in 0..PROOFSIZE {
|
||||
for k in 0..proof.proof_size {
|
||||
// find unique other i with same us[i]
|
||||
if k != j && us[k] == us[j] {
|
||||
if i != j {
|
||||
@@ -154,6 +153,7 @@ impl Cuckoo {
|
||||
/// tests, being impractical with sizes greater than 2^22.
|
||||
pub struct Miner {
|
||||
easiness: u64,
|
||||
proof_size: usize,
|
||||
cuckoo: Option<Cuckoo>,
|
||||
graph: Vec<u32>,
|
||||
sizeshift: u32,
|
||||
@@ -163,7 +163,8 @@ impl MiningWorker for Miner {
|
||||
|
||||
/// Creates a new miner
|
||||
fn new(ease: u32,
|
||||
sizeshift: u32) -> Miner {
|
||||
sizeshift: u32,
|
||||
proof_size: usize) -> Miner {
|
||||
let size = 1 << sizeshift;
|
||||
let graph = vec![0; size + 1];
|
||||
let easiness = (ease as u64) * (size as u64) / 100;
|
||||
@@ -172,6 +173,7 @@ impl MiningWorker for Miner {
|
||||
cuckoo: None,
|
||||
graph: graph,
|
||||
sizeshift: sizeshift,
|
||||
proof_size: proof_size,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -186,7 +188,7 @@ impl MiningWorker for Miner {
|
||||
/// What type of cycle we have found?
|
||||
enum CycleSol {
|
||||
/// A cycle of the right length is a valid proof.
|
||||
ValidProof([u32; PROOFSIZE]),
|
||||
ValidProof(Vec<u32>),
|
||||
/// A cycle of the wrong length is great, but not a proof.
|
||||
InvalidCycle(usize),
|
||||
/// No cycles have been found.
|
||||
@@ -214,7 +216,7 @@ impl Miner {
|
||||
let sol = self.find_sol(nu, &us, nv, &vs);
|
||||
match sol {
|
||||
CycleSol::ValidProof(res) => {
|
||||
return Ok(Proof(res))
|
||||
return Ok(Proof::new(res.to_vec()));
|
||||
},
|
||||
CycleSol::InvalidCycle(_) => continue,
|
||||
CycleSol::NoCycle => {
|
||||
@@ -266,7 +268,7 @@ impl Miner {
|
||||
nu += 1;
|
||||
nv += 1;
|
||||
}
|
||||
if nu + nv + 1 == PROOFSIZE {
|
||||
if nu + nv + 1 == self.proof_size {
|
||||
self.solution(&us, nu as u32, &vs, nv as u32)
|
||||
} else {
|
||||
CycleSol::InvalidCycle(nu + nv + 1)
|
||||
@@ -299,7 +301,7 @@ impl Miner {
|
||||
});
|
||||
}
|
||||
let mut n = 0;
|
||||
let mut sol = [0; PROOFSIZE];
|
||||
let mut sol = vec![0; self.proof_size];
|
||||
for nonce in 0..self.easiness {
|
||||
let edge = self.cuckoo.as_mut().unwrap().new_edge(nonce);
|
||||
if cycle.contains(&edge) {
|
||||
@@ -308,7 +310,7 @@ impl Miner {
|
||||
cycle.remove(&edge);
|
||||
}
|
||||
}
|
||||
return if n == PROOFSIZE {
|
||||
return if n == self.proof_size {
|
||||
CycleSol::ValidProof(sol)
|
||||
} else {
|
||||
CycleSol::NoCycle
|
||||
@@ -329,68 +331,68 @@ mod test {
|
||||
use super::*;
|
||||
use core::Proof;
|
||||
|
||||
static V1: Proof = Proof([0xe13, 0x410c, 0x7974, 0x8317, 0xb016, 0xb992, 0xe3c8, 0x1038a,
|
||||
0x116f0, 0x15ed2, 0x165a2, 0x17793, 0x17dd1, 0x1f885, 0x20932,
|
||||
0x20936, 0x2171b, 0x28968, 0x2b184, 0x30b8e, 0x31d28, 0x35782,
|
||||
0x381ea, 0x38321, 0x3b414, 0x3e14b, 0x43615, 0x49a51, 0x4a319,
|
||||
0x58271, 0x5dbb9, 0x5dbcf, 0x62db4, 0x653d2, 0x655f6, 0x66382,
|
||||
0x7057d, 0x765b0, 0x79c7c, 0x83167, 0x86e7b, 0x8a5f4]);
|
||||
static V2: Proof = Proof([0x33b8, 0x3fd9, 0x8f2b, 0xba0d, 0x11e2d, 0x1d51d, 0x2786e, 0x29625,
|
||||
0x2a862, 0x2a972, 0x2e6d7, 0x319df, 0x37ce7, 0x3f771, 0x4373b,
|
||||
0x439b7, 0x48626, 0x49c7d, 0x4a6f1, 0x4a808, 0x4e518, 0x519e3,
|
||||
0x526bb, 0x54988, 0x564e9, 0x58a6c, 0x5a4dd, 0x63fa2, 0x68ad1,
|
||||
0x69e52, 0x6bf53, 0x70841, 0x76343, 0x763a4, 0x79681, 0x7d006,
|
||||
0x7d633, 0x7eebe, 0x7fe7c, 0x811fa, 0x863c1, 0x8b149]);
|
||||
static V3: Proof = Proof([0x24ae, 0x5180, 0x9f3d, 0xd379, 0x102c9, 0x15787, 0x16df4, 0x19509,
|
||||
0x19a78, 0x235a0, 0x24210, 0x24410, 0x2567f, 0x282c3, 0x2d986,
|
||||
0x2efde, 0x319d7, 0x334d7, 0x336dd, 0x34296, 0x35809, 0x3ad40,
|
||||
0x46d81, 0x48c92, 0x4b374, 0x4c353, 0x4fe4c, 0x50e4f, 0x53202,
|
||||
0x5d167, 0x6527c, 0x6a8b5, 0x6c70d, 0x76d90, 0x794f4, 0x7c411,
|
||||
0x7c5d4, 0x7f59f, 0x7fead, 0x872d8, 0x875b4, 0x95c6b]);
|
||||
static V1:[u32;42] = [0xe13, 0x410c, 0x7974, 0x8317, 0xb016, 0xb992, 0xe3c8, 0x1038a,
|
||||
0x116f0, 0x15ed2, 0x165a2, 0x17793, 0x17dd1, 0x1f885, 0x20932,
|
||||
0x20936, 0x2171b, 0x28968, 0x2b184, 0x30b8e, 0x31d28, 0x35782,
|
||||
0x381ea, 0x38321, 0x3b414, 0x3e14b, 0x43615, 0x49a51, 0x4a319,
|
||||
0x58271, 0x5dbb9, 0x5dbcf, 0x62db4, 0x653d2, 0x655f6, 0x66382,
|
||||
0x7057d, 0x765b0, 0x79c7c, 0x83167, 0x86e7b, 0x8a5f4];
|
||||
static V2:[u32;42] = [0x33b8, 0x3fd9, 0x8f2b, 0xba0d, 0x11e2d, 0x1d51d, 0x2786e, 0x29625,
|
||||
0x2a862, 0x2a972, 0x2e6d7, 0x319df, 0x37ce7, 0x3f771, 0x4373b,
|
||||
0x439b7, 0x48626, 0x49c7d, 0x4a6f1, 0x4a808, 0x4e518, 0x519e3,
|
||||
0x526bb, 0x54988, 0x564e9, 0x58a6c, 0x5a4dd, 0x63fa2, 0x68ad1,
|
||||
0x69e52, 0x6bf53, 0x70841, 0x76343, 0x763a4, 0x79681, 0x7d006,
|
||||
0x7d633, 0x7eebe, 0x7fe7c, 0x811fa, 0x863c1, 0x8b149];
|
||||
static V3:[u32;42] = [0x24ae, 0x5180, 0x9f3d, 0xd379, 0x102c9, 0x15787, 0x16df4, 0x19509,
|
||||
0x19a78, 0x235a0, 0x24210, 0x24410, 0x2567f, 0x282c3, 0x2d986,
|
||||
0x2efde, 0x319d7, 0x334d7, 0x336dd, 0x34296, 0x35809, 0x3ad40,
|
||||
0x46d81, 0x48c92, 0x4b374, 0x4c353, 0x4fe4c, 0x50e4f, 0x53202,
|
||||
0x5d167, 0x6527c, 0x6a8b5, 0x6c70d, 0x76d90, 0x794f4, 0x7c411,
|
||||
0x7c5d4, 0x7f59f, 0x7fead, 0x872d8, 0x875b4, 0x95c6b];
|
||||
// cuckoo28 at 50% edges of letter 'u'
|
||||
static V4: Proof = Proof([0x1abd16, 0x7bb47e, 0x860253, 0xfad0b2, 0x121aa4d, 0x150a10b,
|
||||
0x20605cb, 0x20ae7e3, 0x235a9be, 0x2640f4a, 0x2724c36, 0x2a6d38c,
|
||||
0x2c50b28, 0x30850f2, 0x309668a, 0x30c85bd, 0x345f42c, 0x3901676,
|
||||
0x432838f, 0x472158a, 0x4d04e9d, 0x4d6a987, 0x4f577bf, 0x4fbc49c,
|
||||
0x593978d, 0x5acd98f, 0x5e60917, 0x6310602, 0x6385e88, 0x64f149c,
|
||||
0x66d472e, 0x68e4df9, 0x6b4a89c, 0x6bb751d, 0x6e09792, 0x6e57e1d,
|
||||
0x6ecfcdd, 0x70abddc, 0x7291dfd, 0x788069e, 0x79a15b1, 0x7d1a1e9]);
|
||||
static V4:[u32;42] = [0x1abd16, 0x7bb47e, 0x860253, 0xfad0b2, 0x121aa4d, 0x150a10b,
|
||||
0x20605cb, 0x20ae7e3, 0x235a9be, 0x2640f4a, 0x2724c36, 0x2a6d38c,
|
||||
0x2c50b28, 0x30850f2, 0x309668a, 0x30c85bd, 0x345f42c, 0x3901676,
|
||||
0x432838f, 0x472158a, 0x4d04e9d, 0x4d6a987, 0x4f577bf, 0x4fbc49c,
|
||||
0x593978d, 0x5acd98f, 0x5e60917, 0x6310602, 0x6385e88, 0x64f149c,
|
||||
0x66d472e, 0x68e4df9, 0x6b4a89c, 0x6bb751d, 0x6e09792, 0x6e57e1d,
|
||||
0x6ecfcdd, 0x70abddc, 0x7291dfd, 0x788069e, 0x79a15b1, 0x7d1a1e9];
|
||||
|
||||
/// Find a 42-cycle on Cuckoo20 at 75% easiness and verifiy against a few
|
||||
/// known cycle proofs
|
||||
/// generated by other implementations.
|
||||
#[test]
|
||||
fn mine20_vectors() {
|
||||
let nonces1 = Miner::new(75, 20).mine(&[49]).unwrap();
|
||||
assert_eq!(V1, nonces1);
|
||||
let nonces1 = Miner::new(75, 20, 42).mine(&[49]).unwrap();
|
||||
assert_eq!(Proof::new(V1.to_vec()), nonces1);
|
||||
|
||||
let nonces2 = Miner::new(70, 20).mine(&[50]).unwrap();
|
||||
assert_eq!(V2, nonces2);
|
||||
let nonces2 = Miner::new(70, 20, 42).mine(&[50]).unwrap();
|
||||
assert_eq!(Proof::new(V2.to_vec()), nonces2);
|
||||
|
||||
let nonces3 = Miner::new(70, 20).mine(&[51]).unwrap();
|
||||
assert_eq!(V3, nonces3);
|
||||
let nonces3 = Miner::new(70, 20, 42).mine(&[51]).unwrap();
|
||||
assert_eq!(Proof::new(V3.to_vec()), nonces3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validate20_vectors() {
|
||||
assert!(Cuckoo::new(&[49], 20).verify(V1.clone(), 75));
|
||||
assert!(Cuckoo::new(&[50], 20).verify(V2.clone(), 70));
|
||||
assert!(Cuckoo::new(&[51], 20).verify(V3.clone(), 70));
|
||||
assert!(Cuckoo::new(&[49], 20).verify(Proof::new(V1.to_vec().clone()), 75));
|
||||
assert!(Cuckoo::new(&[50], 20).verify(Proof::new(V2.to_vec().clone()), 70));
|
||||
assert!(Cuckoo::new(&[51], 20).verify(Proof::new(V3.to_vec().clone()), 70));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validate28_vectors() {
|
||||
assert!(Cuckoo::new(&[117], 28).verify(V4.clone(), 50));
|
||||
assert!(Cuckoo::new(&[117], 28).verify(Proof::new(V4.to_vec().clone()), 50));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validate_fail() {
|
||||
// edge checks
|
||||
assert!(!Cuckoo::new(&[49], 20).verify(Proof([0; 42]), 75));
|
||||
assert!(!Cuckoo::new(&[49], 20).verify(Proof([0xffff; 42]), 75));
|
||||
assert!(!Cuckoo::new(&[49], 20).verify(Proof::new(vec![0; 42]), 75));
|
||||
assert!(!Cuckoo::new(&[49], 20).verify(Proof::new(vec![0xffff; 42]), 75));
|
||||
// wrong data for proof
|
||||
assert!(!Cuckoo::new(&[50], 20).verify(V1.clone(), 75));
|
||||
assert!(!Cuckoo::new(&[117], 20).verify(V4.clone(), 50));
|
||||
assert!(!Cuckoo::new(&[50], 20).verify(Proof::new(V1.to_vec().clone()), 75));
|
||||
assert!(!Cuckoo::new(&[117], 20).verify(Proof::new(V4.to_vec().clone()), 50));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -398,13 +400,13 @@ mod test {
|
||||
// cuckoo20
|
||||
for n in 1..5 {
|
||||
let h = [n; 32];
|
||||
let nonces = Miner::new(75, 20).mine(&h).unwrap();
|
||||
let nonces = Miner::new(75, 20, 42).mine(&h).unwrap();
|
||||
assert!(Cuckoo::new(&h, 20).verify(nonces, 75));
|
||||
}
|
||||
// cuckoo18
|
||||
for n in 1..5 {
|
||||
let h = [n; 32];
|
||||
let nonces = Miner::new(75, 18).mine(&h).unwrap();
|
||||
let nonces = Miner::new(75, 18, 42).mine(&h).unwrap();
|
||||
assert!(Cuckoo::new(&h, 18).verify(nonces, 75));
|
||||
}
|
||||
}
|
||||
|
||||
+12
-9
@@ -31,6 +31,8 @@ use consensus::EASINESS;
|
||||
use core::BlockHeader;
|
||||
use core::hash::Hashed;
|
||||
use core::Proof;
|
||||
use global;
|
||||
use global::{MiningParameterMode, MINING_PARAMETER_MODE};
|
||||
use core::target::Difficulty;
|
||||
use pow::cuckoo::{Cuckoo, Error};
|
||||
|
||||
@@ -41,7 +43,7 @@ use pow::cuckoo::{Cuckoo, 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, proof_size:usize) -> Self;
|
||||
|
||||
/// Actually perform a mining attempt on the given input and
|
||||
/// return a proof if found
|
||||
@@ -54,10 +56,10 @@ pub trait MiningWorker {
|
||||
pub fn verify_size(bh: &BlockHeader, cuckoo_sz: u32) -> bool {
|
||||
// make sure the pow hash shows a difficulty at least as large as the target
|
||||
// difficulty
|
||||
if bh.difficulty > bh.pow.to_difficulty() {
|
||||
if bh.difficulty > bh.pow.clone().to_difficulty() {
|
||||
return false;
|
||||
}
|
||||
Cuckoo::new(&bh.hash()[..], cuckoo_sz).verify(bh.pow, EASINESS as u64)
|
||||
Cuckoo::new(&bh.hash()[..], cuckoo_sz).verify(bh.pow.clone(), EASINESS as u64)
|
||||
}
|
||||
|
||||
/// Uses the much easier Cuckoo20 (mostly for
|
||||
@@ -82,7 +84,7 @@ pub fn pow_size<T: MiningWorker>(miner:&mut T, bh: &mut BlockHeader,
|
||||
// diff, we're all good
|
||||
|
||||
if let Ok(proof) = miner.mine(&pow_hash[..]) {
|
||||
if proof.to_difficulty() >= diff {
|
||||
if proof.clone().to_difficulty() >= diff {
|
||||
bh.pow = proof;
|
||||
return Ok(());
|
||||
}
|
||||
@@ -104,16 +106,17 @@ mod test {
|
||||
use super::*;
|
||||
use core::target::Difficulty;
|
||||
use genesis;
|
||||
use consensus::MINIMUM_DIFFICULTY;
|
||||
use consensus::MINIMUM_DIFFICULTY;
|
||||
|
||||
#[test]
|
||||
fn genesis_pow() {
|
||||
global::set_mining_mode(MiningParameterMode::AutomatedTesting);
|
||||
let mut b = genesis::genesis();
|
||||
b.header.nonce = 310;
|
||||
let mut internal_miner = cuckoo::Miner::new(EASINESS, 12);
|
||||
pow_size(&mut internal_miner, &mut b.header, Difficulty::from_num(MINIMUM_DIFFICULTY), 12).unwrap();
|
||||
let mut internal_miner = cuckoo::Miner::new(EASINESS, global::sizeshift() as u32, global::proofsize());
|
||||
pow_size(&mut internal_miner, &mut b.header, Difficulty::from_num(MINIMUM_DIFFICULTY), global::sizeshift() as u32).unwrap();
|
||||
assert!(b.header.nonce != 310);
|
||||
assert!(b.header.pow.to_difficulty() >= Difficulty::from_num(MINIMUM_DIFFICULTY));
|
||||
assert!(verify_size(&b.header, 12));
|
||||
assert!(b.header.pow.clone().to_difficulty() >= Difficulty::from_num(MINIMUM_DIFFICULTY));
|
||||
assert!(verify_size(&b.header, global::sizeshift() as u32));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user