-
Notifications
You must be signed in to change notification settings - Fork 96
/
consensus-node.rs
64 lines (54 loc) · 1.58 KB
/
consensus-node.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//! Example of a consensus node that uses the `hbbft::node::Node` struct for
//! running the distributed consensus state machine.
mod network;
use std::collections::HashSet;
use std::net::SocketAddr;
use std::process;
use std::vec::Vec;
use docopt::Docopt;
use crate::network::node::Node;
const VERSION: &str = "0.1.0";
const USAGE: &str = "
Consensus node example
Usage:
consensus-node --bind-address=<host:port> [--value=VALUE] [--remote-address=<host:port>]...
consensus-node --help | -h
consensus-node --version
";
#[derive(Debug)]
struct Args {
bind_address: SocketAddr,
remote_addresses: HashSet<SocketAddr>,
value: Option<Vec<u8>>,
}
fn parse_args() -> Args {
let args = Docopt::new(USAGE)
.unwrap_or_else(|e| e.exit())
.version(Some(VERSION.to_owned()))
.parse()
.unwrap_or_else(|e| e.exit());
if args.get_count("-h") > 0 || args.get_count("--help") > 0 {
print!("{}", USAGE);
process::exit(0);
}
Args {
value: if args.get_count("--value") > 0 {
Some(args.get_str("--value").as_bytes().to_vec())
} else {
None
},
bind_address: args.get_str("--bind-address").parse().unwrap(),
remote_addresses: args
.get_vec("--remote-address")
.iter()
.map(|s| s.parse().unwrap())
.collect(),
}
}
pub fn main() {
env_logger::init();
let args: Args = parse_args();
println!("{:?}", args);
let node = Node::new(args.bind_address, args.remote_addresses, args.value);
node.run().expect("Node failed");
}