-
Notifications
You must be signed in to change notification settings - Fork 14
feat: Refactor notary. Detect when running cargo test to conditionally run thread termination code. Setup logger #32
Changes from 5 commits
97f182a
c7cba5d
f8cad45
f94d253
e71e58c
2733ead
1c44cea
361dfc3
db0cf58
6ff0d02
fff7116
b913550
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use std::env; | ||
|
||
pub fn get_env() -> String { | ||
let key = "RUST_ENV"; | ||
match env::var(key) { | ||
Ok(val) => { | ||
debug!("Found environment variable key {}: {:?}", key, val); | ||
val.to_string() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We may want to read the debug! log to a target. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nevermind, it's OK to read to the terminal, a developer can use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to have |
||
}, | ||
Err(e) => { | ||
error!("Error interpreting environment variable key {}: {}", key, e); | ||
"".to_string() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to have |
||
}, | ||
} | ||
} | ||
|
||
pub fn set_test_env() { | ||
// Set the environment variable key `TEST` to value of "1" | ||
// when `cargo test` has been run, otherwise set it to "0" | ||
let key = "RUST_ENV"; | ||
let value_test = "TEST"; | ||
let value_development = "DEVELOPMENT"; | ||
if let Some(arg0) = env::args().nth(0) { | ||
if arg0 == "target/debug/diamond_drops" { | ||
env::set_var(key, value_development); | ||
assert_eq!(env::var(key), Ok(value_development.to_string())); | ||
} else { | ||
env::set_var(key, value_test); | ||
assert_eq!(env::var(key), Ok(value_test.to_string())); | ||
} | ||
} | ||
} | ||
|
||
pub fn is_running_with_cargo_test() -> bool { | ||
if get_env() == "TEST" { | ||
true | ||
} else { | ||
false | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use log; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you also need to add this here as well:
|
||
use log::{Record, Level, Metadata, SetLoggerError, LevelFilter}; | ||
use chrono::Local; | ||
|
||
static LOGGER: DiamondDropsLogger = DiamondDropsLogger; | ||
|
||
struct DiamondDropsLogger; | ||
|
||
impl log::Log for DiamondDropsLogger { | ||
fn enabled(&self, metadata: &Metadata) -> bool { | ||
metadata.level() <= Level::Trace | ||
} | ||
|
||
fn log(&self, record: &Record) { | ||
if self.enabled(record.metadata()) { | ||
println!("{} [{}] - {}", Local::now().format("%Y-%m-%dT%H:%M:%S"), record.level(), record.args()); | ||
} | ||
} | ||
|
||
fn flush(&self) {} | ||
} | ||
|
||
pub fn init() -> () { | ||
/*! Initialisation of [Log Crate](https://crates.io/crates/log) with choice of logging level macros */ | ||
/*! from highest priority to lowest: `error!`, `warn!`, `info!`, `debug!` and `trace!`. */ | ||
/*! [Compile time filters](https://docs.rs/log/0.4.1/log/#compile-time-filters) are configured in Cargo.toml */ | ||
|
||
let logger = log::set_logger(&LOGGER); | ||
match logger { | ||
Ok(res) => { | ||
log::set_max_level(LevelFilter::Trace); | ||
eprintln!("Success initializing Rust Logger to max level: {}", log::max_level()); | ||
() | ||
} | ||
Err(e) => { | ||
eprintln!("Error initializing Rust Logger: {}", e); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
pub mod config; | ||
pub mod args; | ||
pub mod config_env; | ||
pub mod config_log; | ||
pub mod args; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ pub struct Header { | |
} | ||
|
||
impl Header { | ||
pub fn new(shard_id: ethereum_types::U256, | ||
pub fn new(shard_id: ethereum_types::U256, | ||
//parent_hash: ethereum_types::H256, | ||
chunk_root: ethereum_types::H256, | ||
period: ethereum_types::U256, | ||
|
@@ -88,49 +88,49 @@ mod tests { | |
fn it_produces_correct_hash() { | ||
// Build the args for collation header creation | ||
// Shard Id | ||
let sid = ethereum_types::U256::from_dec_str("1").unwrap(); | ||
let sid_bytes = u256_to_bytes32(sid); | ||
let shard_id = ethereum_types::U256::from_dec_str("1").unwrap(); | ||
let shard_id_bytes = u256_to_bytes32(shard_id); | ||
|
||
/* | ||
// Parent Hash | ||
let ph_bytes: [u8; 32] = [0x50, 0xa1, 0xb3, 0xd5, 0x14, 0xd4, 0x99, 0x63, | ||
let parent_hash_bytes: [u8; 32] = [0x50, 0xa1, 0xb3, 0xd5, 0x14, 0xd4, 0x99, 0x63, | ||
0x54, 0x14, 0x7a, 0xd2, 0x89, 0x61, 0x75, 0xb0, | ||
0x7d, 0x43, 0x7f, 0x9e, 0x58, 0xfa, 0x3c, 0x44, | ||
0x86, 0xc0, 0x42, 0xf4, 0xc3, 0xd5, 0x05, 0x9b]; | ||
let ph = ethereum_types::H256::from_slice(&ph_bytes[..]); | ||
let parent_hash = ethereum_types::H256::from_slice(&parent_hash_bytes[..]); | ||
*/ | ||
|
||
// Chunk Root | ||
let cr_bytes: [u8; 32] = [0x50, 0xce, 0xc0, 0x49, 0x54, 0x77, 0xfb, 0x7e, | ||
0x65, 0x25, 0xc2, 0xa0, 0x39, 0xa3, 0xa9, 0x95, | ||
0x34, 0x90, 0x35, 0xb2, 0xa8, 0x23, 0xa4, 0x99, | ||
0x0b, 0x27, 0xf6, 0xd7, 0xd5, 0x5e, 0xec, 0x6b]; | ||
let cr = ethereum_types::H256::from_slice(&cr_bytes[..]); | ||
let chunk_root_bytes: [u8; 32] = [0x50, 0xce, 0xc0, 0x49, 0x54, 0x77, 0xfb, 0x7e, | ||
0x65, 0x25, 0xc2, 0xa0, 0x39, 0xa3, 0xa9, 0x95, | ||
0x34, 0x90, 0x35, 0xb2, 0xa8, 0x23, 0xa4, 0x99, | ||
0x0b, 0x27, 0xf6, 0xd7, 0xd5, 0x5e, 0xec, 0x6b]; | ||
let chunk_root = ethereum_types::H256::from_slice(&chunk_root_bytes[..]); | ||
|
||
// Period | ||
let period = ethereum_types::U256::from_dec_str("1").unwrap(); | ||
let period_bytes = u256_to_bytes32(period); | ||
|
||
// Proposer Address | ||
let proposer_bytes: [u8; 20] = [0x39, 0xa4, 0x2d, 0x47, 0x4a, | ||
let proposer_address_bytes: [u8; 20] = [0x39, 0xa4, 0x2d, 0x47, 0x4a, | ||
0x52, 0x96, 0xab, 0x98, 0x52, | ||
0x3b, 0x1a, 0x3d, 0xef, 0x8f, | ||
0x18, 0x67, 0xad, 0x32, 0xb0]; | ||
let proposer = ethereum_types::H160::from_slice(&proposer_bytes[..]); | ||
let proposer_address = ethereum_types::H160::from_slice(&proposer_address_bytes[..]); | ||
|
||
// Create the header | ||
let header = Header::new(sid, /*ph,*/ cr, period, proposer); | ||
let header = Header::new(shard_id, /*parent_hash,*/ chunk_root, period, proposer_address); | ||
|
||
// Calculate its generated hash | ||
let header_hash = header.hash(); | ||
|
||
// Calculate the expected hash | ||
let mut sha3 = tiny_keccak::Keccak::new_sha3_256(); | ||
sha3.update(&sid_bytes[..]); | ||
//sha3.update(&ph_bytes[..]); | ||
sha3.update(&cr_bytes[..]); | ||
sha3.update(&shard_id_bytes[..]); | ||
//sha3.update(&parent_hash_bytes[..]); | ||
sha3.update(&chunk_root_bytes[..]); | ||
sha3.update(&period_bytes[..]); | ||
sha3.update(&proposer_bytes[..]); | ||
sha3.update(&proposer_address_bytes[..]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for renaming the variables. |
||
|
||
let mut expected_bytes: [u8; 32] = [0; 32]; | ||
sha3.finalize(&mut expected_bytes); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you need to add
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's ok, we only have to include
#[macro_use] extern crate log;
in main.rs and lib.rsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK.