slog-rs logging (#171)

* added global slog instance, changed all logging macro formats to include logger instance
* adding configuration to logging, allowing for multiple log outputs
* updates to test, changes to build docs
* rustfmt
* moving logging functions into util crate
This commit is contained in:
Yeastplume
2017-10-12 17:56:44 +01:00
committed by Ignotus Peverell
parent b85006ebe5
commit 8e382a7593
62 changed files with 973 additions and 1006 deletions
+34 -208
View File
@@ -314,8 +314,8 @@ impl Miner {
/// Utility to transform a 8 bytes of a byte array into a u64.
fn u8_to_u64(p: &[u8], i: usize) -> u64 {
(p[i] as u64) | (p[i + 1] as u64) << 8 | (p[i + 2] as u64) << 16 |
(p[i + 3] as u64) << 24 | (p[i + 4] as u64) << 32 | (p[i + 5] as u64) << 40 |
(p[i + 6] as u64) << 48 | (p[i + 7] as u64) << 56
(p[i + 3] as u64) << 24 | (p[i + 4] as u64) << 32 | (p[i + 5] as u64) << 40 |
(p[i + 6] as u64) << 48 | (p[i + 7] as u64) << 56
}
#[cfg(test)]
@@ -324,183 +324,32 @@ mod test {
use core::core::Proof;
static V1: [u32; 42] = [
0x1fe9,
0x2050,
0x4581,
0x6322,
0x65ab,
0xb3c1,
0xc1a4,
0xe257,
0x106ae,
0x17b11,
0x202d4,
0x2705d,
0x2deb2,
0x2f80e,
0x32298,
0x34782,
0x35c5a,
0x37458,
0x38f28,
0x406b2,
0x40e34,
0x40fc6,
0x42220,
0x42d13,
0x46c0f,
0x4fd47,
0x55ad2,
0x598f7,
0x5aa8f,
0x62aa3,
0x65725,
0x65dcb,
0x671c7,
0x6eb20,
0x752fe,
0x7594f,
0x79b9c,
0x7f775,
0x81635,
0x8401c,
0x844e5,
0x89fa8,
];
static V2: [u32; 42] = [
0x2a37,
0x7557,
0xa3c3,
0xfce6,
0x1248e,
0x15837,
0x1827f,
0x18a93,
0x1a7dd,
0x1b56b,
0x1ceb4,
0x1f962,
0x1fe2a,
0x29cb9,
0x2f30e,
0x2f771,
0x336bf,
0x34355,
0x391d7,
0x39495,
0x3be0c,
0x463be,
0x4d0c2,
0x4eead,
0x50214,
0x520de,
0x52a86,
0x53818,
0x53b3b,
0x54c0b,
0x572fa,
0x5d79c,
0x5e3c2,
0x6769e,
0x6a0fe,
0x6d835,
0x6fc7c,
0x70f03,
0x79d4a,
0x7b03e,
0x81e09,
0x9bd44,
];
static V3: [u32; 42] = [
0x8158,
0x9f18,
0xc4ba,
0x108c7,
0x11caa,
0x13b82,
0x1618f,
0x1c83b,
0x1ec89,
0x24354,
0x28864,
0x2a0fb,
0x2ce50,
0x2e8fa,
0x32b36,
0x343e6,
0x34dc9,
0x36881,
0x3ffca,
0x40f79,
0x42721,
0x43b8c,
0x44b9d,
0x47ed3,
0x4cd34,
0x5278a,
0x5ab64,
0x5b4d4,
0x5d842,
0x5fa33,
0x6464e,
0x676ee,
0x685d6,
0x69df0,
0x6a5fd,
0x6bda3,
0x72544,
0x77974,
0x7908c,
0x80e67,
0x81ef4,
0x8d882,
];
static V1: [u32; 42] = [0x1fe9, 0x2050, 0x4581, 0x6322, 0x65ab, 0xb3c1, 0xc1a4, 0xe257,
0x106ae, 0x17b11, 0x202d4, 0x2705d, 0x2deb2, 0x2f80e, 0x32298,
0x34782, 0x35c5a, 0x37458, 0x38f28, 0x406b2, 0x40e34, 0x40fc6,
0x42220, 0x42d13, 0x46c0f, 0x4fd47, 0x55ad2, 0x598f7, 0x5aa8f,
0x62aa3, 0x65725, 0x65dcb, 0x671c7, 0x6eb20, 0x752fe, 0x7594f,
0x79b9c, 0x7f775, 0x81635, 0x8401c, 0x844e5, 0x89fa8];
static V2: [u32; 42] = [0x2a37, 0x7557, 0xa3c3, 0xfce6, 0x1248e, 0x15837, 0x1827f, 0x18a93,
0x1a7dd, 0x1b56b, 0x1ceb4, 0x1f962, 0x1fe2a, 0x29cb9, 0x2f30e,
0x2f771, 0x336bf, 0x34355, 0x391d7, 0x39495, 0x3be0c, 0x463be,
0x4d0c2, 0x4eead, 0x50214, 0x520de, 0x52a86, 0x53818, 0x53b3b,
0x54c0b, 0x572fa, 0x5d79c, 0x5e3c2, 0x6769e, 0x6a0fe, 0x6d835,
0x6fc7c, 0x70f03, 0x79d4a, 0x7b03e, 0x81e09, 0x9bd44];
static V3: [u32; 42] = [0x8158, 0x9f18, 0xc4ba, 0x108c7, 0x11caa, 0x13b82, 0x1618f, 0x1c83b,
0x1ec89, 0x24354, 0x28864, 0x2a0fb, 0x2ce50, 0x2e8fa, 0x32b36,
0x343e6, 0x34dc9, 0x36881, 0x3ffca, 0x40f79, 0x42721, 0x43b8c,
0x44b9d, 0x47ed3, 0x4cd34, 0x5278a, 0x5ab64, 0x5b4d4, 0x5d842,
0x5fa33, 0x6464e, 0x676ee, 0x685d6, 0x69df0, 0x6a5fd, 0x6bda3,
0x72544, 0x77974, 0x7908c, 0x80e67, 0x81ef4, 0x8d882];
// cuckoo28 at 50% edges of letter 'u'
static V4: [u32; 42] = [
0x1CBBFD,
0x2C5452,
0x520338,
0x6740C5,
0x8C6997,
0xC77150,
0xFD4972,
0x1060FA7,
0x11BFEA0,
0x1343E8D,
0x14CE02A,
0x1533515,
0x1715E61,
0x1996D9B,
0x1CB296B,
0x1FCA180,
0x209A367,
0x20AD02E,
0x23CD2E4,
0x2A3B360,
0x2DD1C0C,
0x333A200,
0x33D77BC,
0x3620C78,
0x3DD7FB8,
0x3FBFA49,
0x41BDED2,
0x4A86FD9,
0x570DE24,
0x57CAB86,
0x594B886,
0x5C74C94,
0x5DE7572,
0x60ADD6F,
0x635918B,
0x6C9E120,
0x6EFA583,
0x7394ACA,
0x7556A23,
0x77F70AA,
0x7CF750A,
0x7F60790,
];
static V4: [u32; 42] = [0x1CBBFD, 0x2C5452, 0x520338, 0x6740C5, 0x8C6997, 0xC77150, 0xFD4972,
0x1060FA7, 0x11BFEA0, 0x1343E8D, 0x14CE02A, 0x1533515, 0x1715E61,
0x1996D9B, 0x1CB296B, 0x1FCA180, 0x209A367, 0x20AD02E, 0x23CD2E4,
0x2A3B360, 0x2DD1C0C, 0x333A200, 0x33D77BC, 0x3620C78, 0x3DD7FB8,
0x3FBFA49, 0x41BDED2, 0x4A86FD9, 0x570DE24, 0x57CAB86, 0x594B886,
0x5C74C94, 0x5DE7572, 0x60ADD6F, 0x635918B, 0x6C9E120, 0x6EFA583,
0x7394ACA, 0x7556A23, 0x77F70AA, 0x7CF750A, 0x7F60790];
/// Find a 42-cycle on Cuckoo20 at 75% easiness and verifiy against a few
/// known cycle proofs
@@ -519,51 +368,28 @@ mod test {
#[test]
fn validate20_vectors() {
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,
));
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() {
let mut test_header = [0; 32];
test_header[0] = 24;
assert!(Cuckoo::new(&test_header, 28).verify(
Proof::new(V4.to_vec().clone()),
50,
));
assert!(Cuckoo::new(&test_header, 28).verify(Proof::new(V4.to_vec().clone()), 50));
}
#[test]
fn validate_fail() {
// edge checks
assert!(!Cuckoo::new(&[49], 20).verify(Proof::new(vec![0; 42]), 75));
assert!(!Cuckoo::new(&[49], 20).verify(
Proof::new(vec![0xffff; 42]),
75,
));
assert!(!Cuckoo::new(&[49], 20).verify(Proof::new(vec![0xffff; 42]), 75));
// wrong data for proof
assert!(!Cuckoo::new(&[50], 20).verify(
Proof::new(V1.to_vec().clone()),
75,
));
assert!(!Cuckoo::new(&[50], 20).verify(Proof::new(V1.to_vec().clone()), 75));
let mut test_header = [0; 32];
test_header[0] = 24;
assert!(!Cuckoo::new(&test_header, 20).verify(
Proof::new(
V4.to_vec().clone(),
),
50,
));
assert!(!Cuckoo::new(&test_header, 20).verify(Proof::new(V4.to_vec().clone()), 50));
}
+13 -17
View File
@@ -34,13 +34,13 @@ extern crate time;
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate log;
extern crate env_logger;
extern crate slog;
extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate grin_core as core;
extern crate grin_util as util;
extern crate cuckoo_miner;
@@ -63,9 +63,7 @@ use cuckoo::{Cuckoo, Error};
pub trait MiningWorker {
/// This only sets parameters and does initialisation work now
fn new(ease: u32, sizeshift: u32, proof_size: usize) -> Self
where
Self: Sized;
fn new(ease: u32, sizeshift: u32, proof_size: usize) -> Self where Self: Sized;
/// Actually perform a mining attempt on the given input and
/// return a proof if found
@@ -85,11 +83,10 @@ pub fn verify_size(bh: &BlockHeader, cuckoo_sz: u32) -> bool {
/// Uses the much easier Cuckoo20 (mostly for
/// tests).
pub fn pow20<T: MiningWorker>(
miner: &mut T,
bh: &mut BlockHeader,
diff: Difficulty,
) -> Result<(), Error> {
pub fn pow20<T: MiningWorker>(miner: &mut T,
bh: &mut BlockHeader,
diff: Difficulty)
-> Result<(), Error> {
pow_size(miner, bh, diff, 20)
}
@@ -99,7 +96,7 @@ pub fn pow20<T: MiningWorker>(
///
pub fn mine_genesis_block(miner_config: Option<types::MinerConfig>) -> Option<core::core::Block> {
info!("Starting miner loop for Genesis Block");
info!(util::LOGGER, "Starting miner loop for Genesis Block");
let mut gen = genesis::genesis();
let diff = gen.header.difficulty.clone();
@@ -127,12 +124,11 @@ pub fn mine_genesis_block(miner_config: Option<types::MinerConfig>) -> Option<co
/// Mining Worker,
/// until the required difficulty target is reached. May take a while for a low
/// target...
pub fn pow_size<T: MiningWorker + ?Sized>(
miner: &mut T,
bh: &mut BlockHeader,
diff: Difficulty,
_: u32,
) -> Result<(), Error> {
pub fn pow_size<T: MiningWorker + ?Sized>(miner: &mut T,
bh: &mut BlockHeader,
diff: Difficulty,
_: u32)
-> Result<(), Error> {
let start_nonce = bh.nonce;
// if we're in production mode, try the pre-mined solution first
+12 -5
View File
@@ -26,6 +26,7 @@ use core::global;
use core::core::Proof;
use types::MinerConfig;
use util::LOGGER;
use std::sync::Mutex;
@@ -91,7 +92,7 @@ impl PluginMiner {
// Load from here instead
if let Some(ref c) = *loaded_config_ref {
debug!("Not re-loading plugin or directory.");
debug!(LOGGER, "Not re-loading plugin or directory.");
// this will load the associated plugin
let result = CuckooMiner::new(c.clone());
self.miner = Some(result.unwrap());
@@ -104,6 +105,7 @@ impl PluginMiner {
if let Err(_) = result {
error!(
LOGGER,
"Unable to load cuckoo-miner plugin directory, either from configuration or [exe_path]/plugins."
);
panic!("Unable to load plugin directory... Please check configuration values");
@@ -123,7 +125,12 @@ impl PluginMiner {
let mut config = CuckooMinerConfig::new();
info!("Mining plugin {} - {}", index, caps[0].full_path.clone());
info!(
LOGGER,
"Mining plugin {} - {}",
index,
caps[0].full_path.clone()
);
config.plugin_full_path = caps[0].full_path.clone();
if let Some(l) = miner_config.clone().cuckoo_miner_plugin_config {
if let Some(lp) = l[index].parameter_list.clone() {
@@ -140,8 +147,8 @@ impl PluginMiner {
// this will load the associated plugin
let result = CuckooMiner::new(cuckoo_configs.clone());
if let Err(e) = result {
error!("Error initializing mining plugin: {:?}", e);
// error!("Accepted values are: {:?}", caps[0].parameters);
error!(LOGGER, "Error initializing mining plugin: {:?}", e);
// error!(LOGGER, "Accepted values are: {:?}", caps[0].parameters);
panic!("Unable to init mining plugin.");
}
@@ -155,7 +162,7 @@ impl PluginMiner {
// this will load the associated plugin
let result = CuckooMiner::new(self.config.clone());
if let Err(e) = result {
error!("Error initializing mining plugin: {:?}", e);
error!(LOGGER, "Error initializing mining plugin: {:?}", e);
panic!("Unable to init mining plugin.");
}
result.unwrap()