solana/src/bin/fullnode.rs

105 lines
3.2 KiB
Rust
Raw Normal View History

2018-06-13 18:42:30 -07:00
extern crate atty;
2018-03-26 21:07:11 -07:00
extern crate env_logger;
extern crate getopts;
2018-06-18 15:49:41 -07:00
extern crate log;
2018-03-03 23:13:40 -08:00
extern crate serde_json;
2018-03-27 15:24:05 -07:00
extern crate solana;
2018-02-28 17:04:35 -08:00
2018-06-13 15:07:36 -07:00
use atty::{is, Stream};
2018-06-13 18:42:30 -07:00
use getopts::Options;
2018-07-02 11:20:35 -07:00
use solana::crdt::{ReplicatedData, TestNode};
use solana::fullnode::start;
2018-04-11 19:24:14 -07:00
use std::env;
use std::fs::File;
2018-07-02 11:20:35 -07:00
use std::io::{stdin, stdout, Write};
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
2018-04-19 07:06:19 -07:00
use std::process::exit;
2018-05-11 12:06:05 -07:00
use std::sync::atomic::AtomicBool;
2018-07-02 11:20:35 -07:00
use std::sync::{Arc, Mutex};
//use std::time::Duration;
2018-02-28 17:04:35 -08:00
fn print_usage(program: &str, opts: Options) {
let mut brief = format!("Usage: cat <transaction.log> | {} [options]\n\n", program);
brief += " Run a Solana node to handle transactions and\n";
brief += " write a new transaction log to stdout.\n";
brief += " Takes existing transaction log from stdin.";
print!("{}", opts.usage(&brief));
}
2018-07-02 11:23:20 -07:00
fn main() -> () {
env_logger::init();
let mut opts = Options::new();
opts.optflag("h", "help", "print help");
opts.optopt("l", "", "run with the identity found in FILE", "FILE");
2018-06-23 16:08:53 -07:00
opts.optopt(
"t",
"",
2018-06-23 16:44:56 -07:00
"testnet; connect to the network at this gossip entry point",
2018-06-23 16:43:26 -07:00
"HOST:PORT",
2018-06-23 16:08:53 -07:00
);
opts.optopt(
"o",
"",
"output log to FILE, defaults to stdout (ignored by validators)",
"FILE",
);
let args: Vec<String> = env::args().collect();
let matches = match opts.parse(&args[1..]) {
Ok(m) => m,
2018-04-19 07:06:19 -07:00
Err(e) => {
eprintln!("{}", e);
exit(1);
}
};
if matches.opt_present("h") {
let program = args[0].clone();
print_usage(&program, opts);
return;
}
2018-06-13 15:07:36 -07:00
if is(Stream::Stdin) {
2018-04-21 06:12:57 -07:00
eprintln!("nothing found on stdin, expected a log file");
exit(1);
}
2018-05-31 15:06:04 -07:00
let bind_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 8000);
let mut repl_data = ReplicatedData::new_leader(&bind_addr);
if matches.opt_present("l") {
let path = matches.opt_str("l").unwrap();
2018-05-30 07:17:04 -07:00
if let Ok(file) = File::open(path.clone()) {
if let Ok(data) = serde_json::from_reader(file) {
repl_data = data;
} else {
eprintln!("failed to parse {}", path);
exit(1);
2018-05-30 07:17:04 -07:00
}
2018-06-15 15:29:43 -07:00
} else {
eprintln!("failed to read {}", path);
exit(1);
}
}
2018-07-02 11:20:35 -07:00
let node = TestNode::new_with_bind_addr(repl_data, bind_addr);
let exit = Arc::new(AtomicBool::new(false));
2018-07-02 11:20:35 -07:00
let mut reader = stdin().lock();
2018-06-23 16:08:53 -07:00
let threads = if matches.opt_present("t") {
2018-06-23 17:01:38 -07:00
let testnet_address_string = matches.opt_str("t").unwrap();
let testnet_addr = testnet_address_string.parse().unwrap();
2018-07-02 11:20:35 -07:00
start(node, false, &mut reader, Some(testnet_addr), None, exit)
} else {
repl_data.current_leader_id = repl_data.id.clone();
2018-07-02 11:20:35 -07:00
let outfile: Write + Send + 'static = if matches.opt_present("o") {
let path = matches.opt_str("o").unwrap();
2018-07-02 11:20:35 -07:00
let f = File::create(&path).expect(&format!("unable to open output file \"{}\"", path));
Mutex::new(f)
} else {
2018-07-02 11:20:35 -07:00
stdout().lock()
};
2018-07-02 11:20:35 -07:00
start(node, true, &mut reader, None, Some(&outfile), exit)
};
for t in threads {
t.join().expect("join");
}
}