Rustify core/src/core (#1122)

Small refactoring of one folder, if it makes sense I could extend the scope.
* Remove some cloning (real and just verbosity in the code)
* Naming conventions like to/into*
* Some Clippy's suggestions

I found that we don't use field init shorthand syntax, so I didn't touch this part, was it discussed before?
This commit is contained in:
hashmap
2018-06-01 21:41:26 +02:00
committed by Yeastplume
parent 7812a02233
commit 2fa32d15ce
27 changed files with 215 additions and 263 deletions
+9 -9
View File
@@ -17,8 +17,8 @@
//! simple miner is included, mostly for testing purposes. John Tromp's Tomato
//! miner will be much faster in almost every environment.
use std::collections::HashSet;
use std::cmp;
use std::collections::HashSet;
use blake2;
@@ -93,9 +93,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 = vec![0; proof.proof_size];
let mut vs = vec![0; proof.proof_size];
for n in 0..proof.proof_size {
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;
}
@@ -103,10 +103,10 @@ impl Cuckoo {
vs[n] = self.new_node(nonces[n], 1);
}
let mut i = 0;
let mut count = proof.proof_size;
let mut count = proof.proof_size();
loop {
let mut j = i;
for k in 0..proof.proof_size {
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 {
@@ -119,7 +119,7 @@ impl Cuckoo {
return false;
}
i = j;
for k in 0..proof.proof_size {
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 {
@@ -361,8 +361,8 @@ mod test {
assert!(Cuckoo::new(&[51], 20).verify(Proof::new(V3.to_vec().clone()), 70));
}
/// Just going to disable this for now, as it's painful to try and get a valid
/// cuckoo28 vector (TBD: 30 is more relevant now anyhow)
/// Just going to disable this for now, as it's painful to try and get a
/// valid cuckoo28 vector (TBD: 30 is more relevant now anyhow)
#[test]
#[ignore]
fn validate28_vectors() {
+6 -6
View File
@@ -35,14 +35,14 @@ extern crate time;
extern crate grin_util as util;
mod siphash;
pub mod cuckoo;
mod siphash;
use consensus;
use core::{Block, BlockHeader};
use core::target::Difficulty;
use global;
use core::{Block, BlockHeader};
use genesis;
use global;
use pow::cuckoo::{Cuckoo, Error};
/// Validates the proof of work of a given header, and that the proof of work
@@ -97,7 +97,7 @@ pub fn pow_size(
if let Ok(proof) =
cuckoo::Miner::new(&pow_hash[..], consensus::EASINESS, proof_size, sz).mine()
{
if proof.clone().to_difficulty() >= diff {
if proof.to_difficulty() >= diff {
bh.pow = proof.clone();
return Ok(());
}
@@ -117,9 +117,9 @@ pub fn pow_size(
#[cfg(test)]
mod test {
use super::*;
use global;
use core::target::Difficulty;
use genesis;
use global;
/// We'll be generating genesis blocks differently
#[ignore]
@@ -134,7 +134,7 @@ mod test {
global::sizeshift(),
).unwrap();
assert!(b.header.nonce != 310);
assert!(b.header.pow.clone().to_difficulty() >= Difficulty::one());
assert!(b.header.pow.to_difficulty() >= Difficulty::one());
assert!(verify_size(&b.header, global::sizeshift()));
}
}
+21 -21
View File
@@ -24,30 +24,30 @@ pub fn siphash24(v: [u64; 4], nonce: u64) -> u64 {
// macro for left rotation
macro_rules! rotl {
($num:ident, $shift:expr) => {
$num = ($num << $shift) | ($num >> (64 - $shift));
}
}
($num:ident, $shift:expr) => {
$num = ($num << $shift) | ($num >> (64 - $shift));
};
}
// macro for a single siphash round
macro_rules! round {
() => {
v0 = v0.wrapping_add(v1);
v2 = v2.wrapping_add(v3);
rotl!(v1, 13);
rotl!(v3, 16);
v1 ^= v0;
v3 ^= v2;
rotl!(v0, 32);
v2 = v2.wrapping_add(v1);
v0 = v0.wrapping_add(v3);
rotl!(v1, 17);
rotl!(v3, 21);
v1 ^= v2;
v3 ^= v0;
rotl!(v2, 32);
}
}
() => {
v0 = v0.wrapping_add(v1);
v2 = v2.wrapping_add(v3);
rotl!(v1, 13);
rotl!(v3, 16);
v1 ^= v0;
v3 ^= v2;
rotl!(v0, 32);
v2 = v2.wrapping_add(v1);
v0 = v0.wrapping_add(v3);
rotl!(v1, 17);
rotl!(v3, 21);
v1 ^= v2;
v3 ^= v0;
rotl!(v2, 32);
};
}
// 2 rounds
round!();