-
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
Conversation
…nation code * Restored Unit Tests that detects whether all threads finish and that it returns `()` from call to `diamond_drops::run(config)` for all CLI modes * Added setter and getter of a TEST environment variable so we can detect when the user is running `cargo test` and only run certain snippets of the implementation that terminates threads so the tests pass, but does not terminate the threads when TEST is disabled. This saves us having to explicitly set environment variables at the command line * All tests passing, including 3x additional tests that verify that this functionality works. If you rem ove `cli::config_env::set_test_env();` from the Unit Tests that are included in main.rs then the tests w ill not pass
…nts" * Rearranged order of imported files, and struct attributes for consistency * Add `#[derive(Debug)]` to enums to allow use with `println!` with `{:?}` * Add meaningful message to `panic!` calls * Fix indentation of `notary.run();` * Change variable names used in collation/header.rs tests to be more readable and consistent with implementation * Add `id` attribute to `Notary` and `Proposer` structs for identification when debugging the client, such as knowing what notary id we are storing a collation into * Add `println!` for debugging received messages and current thread in SMC listener * Add `generate_notary()` function to remove code duplication in notary tests * Update SMC registration function to be notary or proposer specific, and to return primative boolean value with meaningful success or error logs * Change `get_selected_notaries()` function to accept `shard_id` argument of `ethereum_types::U256` instead of `usize` for consistency withe the rest of the code * Add `register_proposer_address()` since there will be both a notary registry and a proposer registry in the SMC Contract according to the Phase 1 Sharding spec * Change notary test function names to be more explicit
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.
These look really good, the only thing is that I think there are a lot of println! statements which I think we should be conservative with. I think we should add some logging configuration and log those messages to a file instead of printing them to the screen (since there may be a lot of them).
@@ -0,0 +1,40 @@ | |||
use std::env; |
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
#[macro_use]
extern crate log;
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.rs
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.
OK.
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 comment
The 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 comment
The 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 <command> -> logfile.log
.
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.
Do we need to have val.to_string()
? It doesn't look like this serves any purpose.
src/cli/config_log.rs
Outdated
@@ -0,0 +1,39 @@ | |||
use log; |
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 also need to add this here as well:
#[macro_use]
extern crate log;
Changes in commits #f94d253, #e71e58c, #2733ead, #1c44cea
|
src/cli/config_log.rs
Outdated
.level(LevelFilter::Debug) | ||
.level_for("overly-verbose-target", LevelFilter::Info), | ||
2 => base_config.level(LevelFilter::Debug), | ||
_3_or_more => base_config.level(LevelFilter::Trace), |
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.
How does _3_or_more
work? I mean, how does it know to match this expression if the value is 3 or more?
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 got it from this Fern example and assumed it worked the same way as the _
catch all since it compiles. I'll change it to _
which I'm more comfortable with too!
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.
OK, yeah it's not exactly clear from the example why it is that way, so it's probably better to use _
.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for renaming the variables.
… instead of u256_to_bytes32
@jamesray1 I think you forgot to run |
process::exit(1); | ||
}); | ||
|
||
diamond_drops::run(config); | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { |
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 seems a bit odd to put these tests in main.rs and not in /tests, but I guess it's not a bit deal. After all, integration tests are meant to test how parts of your library work together.
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.
Yeh it compiles, we can refactor it later
I'm looking into it. |
Changes in commits #db0cf58:
Changes in commits #6ff0d02, #fff7116
|
.format(move |out, message, record| { | ||
out.finish(format_args!( | ||
"{color_line}[{date}][{target}][{level}{color_line}] {message}\x1B[0m", | ||
color_line = format_args!("\x1B[{}m", colors_line.get_color(&record.level()).to_fg_str()), |
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.
What's \x1B[{}m
and to_fg_str()
?
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.
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.
match msg { | ||
client_thread::Command::Terminate => { break } | ||
} | ||
}, | ||
None => { } | ||
None => { | ||
trace!("No more pending messages from other threads to thread {:?} or channel hung up", thread::current()); |
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.
Can we keep lines to within 100 characters?
https://internals.rust-lang.org/t/99-vs-100-column-limit/47/6
Perhaps even to 79 characters.
Also pinging @ChosunOne.
src/notary.rs
Outdated
self.collation_vectors.entry(self.shard_id).or_insert(vec![]); | ||
let vector = self.collation_vectors.get_mut(&self.shard_id).unwrap(); | ||
vector.push(collation); | ||
} | ||
|
||
fn store_proposal(&mut self, proposal: collation::Collation) { | ||
debug!("Storing in notary id {} a new proposal collation mapped to shard id {}", self.id, self.shard_id); |
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.
Hmm, maybe we can change this to "a new proposal mapped...". Although this function takes the same argument as store_collation()
.
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.
Yeah obviously we will need to add more detail to Collation:body, but that isn't defined in a common spec yet, so we can just mark this as TODO.
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.
Reduce line lengths to 100 characters.
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.
Please add documentation comments.
https://rust-lang-nursery.github.io/api-guidelines/documentation.html
https://doc.rust-lang.org/book/first-edition/documentation.html
It will be easier to do this as we go, rather than having to backtrack later on.
Also pinging @ChosunOne to do the same.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to have val.to_string()
? It doesn't look like this serves any purpose.
}, | ||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to have "".to_string()
? It doesn't look like this serves any purpose.
I think the changes made here are fine to merge into my notary branch, we can address any remaining issues when it merges into develop. |
feat: Refactor notary. Detect when running cargo test to conditionally run thread termination code. Setup logger
Changes in Commit #97f182a:
()
from call todiamond_drops::run(config)
for all CLI modescargo test
and only run certain snippets of the implementation that terminates threads so the tests pass, but does not terminate the threads when TEST is disabled. This saves us having to explicitly set environment variables at the command linecli::config_env::set_test_env();
from the Unit Tests that are included in main.rs then the tests will not passChanges in Commit #c7cba5d:
RUST_ENV: TEST
orRUST_ENV: DEVELOP
for consistencyChanges in Commit #f8cad45:
#[derive(Debug)]
to enums to allow use withprintln!
with{:?}
panic!
callsnotary.run();
id
attribute toNotary
andProposer
structs for identification when debugging the client, such as knowing what notary id we are storing a collation intoprintln!
for debugging received messages and current thread in SMC listenergenerate_notary()
function to remove code duplication in notary testsget_selected_notaries()
function to acceptshard_id
argument ofethereum_types::U256
instead ofusize
for consistency withe the rest of the coderegister_proposer_address()
since there will be both a notary registry and a proposer registry in the SMC Contract according to the Phase 1 Sharding spec