From c790a1feba46926e54fea64b5c4afdee786fb1f9 Mon Sep 17 00:00:00 2001 From: Andreas Fackler Date: Wed, 9 May 2018 13:38:41 +0200 Subject: [PATCH] Improve example logging, add BinaryPath. --- examples/network/node.rs | 10 +++++++--- examples/run-consensus-nodes.sh | 3 +++ src/proto/mod.rs | 26 +++++++++++++++++++++----- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/examples/network/node.rs b/examples/network/node.rs index 78d42c8..440e5d0 100644 --- a/examples/network/node.rs +++ b/examples/network/node.rs @@ -39,7 +39,7 @@ use std::collections::HashSet; use std::fmt::Debug; use std::marker::{Send, Sync}; use std::net::SocketAddr; -use std::{io, iter, process}; +use std::{io, iter, process, thread, time}; use hbbft::broadcast; use network::commst; @@ -131,8 +131,9 @@ impl + PartialEq + Send + Sync + From> + ).run() { Ok(t) => { - debug!( - "Broadcast succeeded: {}", + println!( + "Broadcast succeeded! Node {} output: {}", + our_id, String::from_utf8(T::into(t)).unwrap() ); } @@ -167,6 +168,9 @@ impl + PartialEq + Send + Sync + From> + // messaging task. broadcast_handle.join(); + // Wait another second so that pending messages get sent out. + thread::sleep(time::Duration::from_secs(1)); + // Stop the messaging task. stop_tx .send(()) diff --git a/examples/run-consensus-nodes.sh b/examples/run-consensus-nodes.sh index 56597aa..49ef124 100644 --- a/examples/run-consensus-nodes.sh +++ b/examples/run-consensus-nodes.sh @@ -1,5 +1,7 @@ #! /bin/bash +export RUST_LOG=hbbft=debug + cargo build --example consensus-node cargo run --example consensus-node -- --bind-address=127.0.0.1:5000 --remote-address=127.0.0.1:5001 --remote-address=127.0.0.1:5002 --remote-address=127.0.0.1:5003 --value=Foo & sleep 1 @@ -8,3 +10,4 @@ sleep 1 cargo run --example consensus-node -- --bind-address=127.0.0.1:5002 --remote-address=127.0.0.1:5000 --remote-address=127.0.0.1:5001 --remote-address=127.0.0.1:5003 & sleep 1 cargo run --example consensus-node -- --bind-address=127.0.0.1:5003 --remote-address=127.0.0.1:5000 --remote-address=127.0.0.1:5001 --remote-address=127.0.0.1:5002 & +wait diff --git a/src/proto/mod.rs b/src/proto/mod.rs index c7a3c80..ba5e2a0 100644 --- a/src/proto/mod.rs +++ b/src/proto/mod.rs @@ -299,18 +299,34 @@ impl LemmaProto { } } +/// The path of a lemma in a Merkle tree +struct BinaryPath(Vec); + /// The path of the lemma, as a binary string -fn path_of_lemma(mut lemma: &Lemma) -> String { - let mut result = String::new(); +fn path_of_lemma(mut lemma: &Lemma) -> BinaryPath { + let mut result = Vec::new(); loop { match lemma.sibling_hash { None => (), - Some(Positioned::Left(_)) => result.push('1'), - Some(Positioned::Right(_)) => result.push('0'), + Some(Positioned::Left(_)) => result.push(true), + Some(Positioned::Right(_)) => result.push(false), } lemma = match lemma.sub_lemma.as_ref() { Some(lemma) => lemma, - None => return result, + None => return BinaryPath(result), } } } + +impl fmt::Display for BinaryPath { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + for b in &self.0 { + if *b { + write!(f, "1")?; + } else { + write!(f, "0")?; + } + } + Ok(()) + } +}