Banish stdin/stdout for ledger

step one: accept a "ledger file" argument instead of "outfile"
This commit is contained in:
Rob Walker
2018-07-12 14:29:36 -07:00
parent 4cca3ff454
commit a3ff40476e
3 changed files with 44 additions and 92 deletions

View File

@@ -9,7 +9,7 @@ extern crate solana;
use atty::{is, Stream};
use clap::{App, Arg};
use solana::crdt::{NodeInfo, TestNode};
use solana::fullnode::{Config, FullNode, InFile, OutFile};
use solana::fullnode::{Config, FullNode, Ledger};
use solana::service::Service;
use solana::signature::{KeyPair, KeyPairUtil};
use std::fs::File;
@@ -37,12 +37,12 @@ fn main() -> () {
.help("testnet; connect to the network at this gossip entry point"),
)
.arg(
Arg::with_name("output")
.short("o")
.long("output")
Arg::with_name("ledger")
.short("L")
.long("ledger")
.value_name("FILE")
.takes_value(true)
.help("output log to FILE, defaults to stdout (ignored by validators)"),
.help("use FILE as persistent ledger (defaults to stdin/stdout)"),
)
.get_matches();
if is(Stream::Stdin) {
@@ -69,27 +69,22 @@ fn main() -> () {
exit(1);
}
}
let ledger = if let Some(l) = matches.value_of("ledger") {
Ledger::Path(l.to_string())
} else {
Ledger::StdInOut
};
let mut node = TestNode::new_with_bind_addr(repl_data, bind_addr);
let fullnode = if let Some(t) = matches.value_of("testnet") {
let testnet_address_string = t.to_string();
let testnet_addr = testnet_address_string.parse().unwrap();
FullNode::new(
node,
false,
InFile::StdIn,
Some(keypair),
Some(testnet_addr),
None,
)
FullNode::new(node, false, ledger, Some(keypair), Some(testnet_addr))
} else {
node.data.leader_id = node.data.id;
let outfile = if let Some(o) = matches.value_of("output") {
OutFile::Path(o.to_string())
} else {
OutFile::StdOut
};
FullNode::new(node, true, InFile::StdIn, None, None, Some(outfile))
FullNode::new(node, true, ledger, None, None)
};
fullnode.join().expect("join");
}

View File

@@ -12,7 +12,7 @@ use service::Service;
use signature::{KeyPair, KeyPairUtil};
use std::collections::VecDeque;
use std::fs::{File, OpenOptions};
use std::io::{sink, stdin, stdout, BufReader};
use std::io::{stdin, stdout, BufReader};
use std::io::{Read, Write};
use std::net::SocketAddr;
use std::sync::atomic::{AtomicBool, Ordering};
@@ -30,13 +30,8 @@ pub struct FullNode {
thread_hdls: Vec<JoinHandle<()>>,
}
pub enum InFile {
StdIn,
Path(String),
}
pub enum OutFile {
StdOut,
pub enum Ledger {
StdInOut,
Path(String),
}
@@ -66,16 +61,24 @@ impl FullNode {
pub fn new(
mut node: TestNode,
leader: bool,
infile: InFile,
ledger: Ledger,
keypair_for_validator: Option<KeyPair>,
network_entry_for_validator: Option<SocketAddr>,
outfile_for_leader: Option<OutFile>,
) -> FullNode {
info!("creating bank...");
let bank = Bank::default();
let infile: Box<Read> = match infile {
InFile::Path(path) => Box::new(File::open(path).unwrap()),
InFile::StdIn => Box::new(stdin()),
let (infile, outfile): (Box<Read>, Box<Write + Send>) = match ledger {
Ledger::Path(path) => (
Box::new(File::open(path.clone()).expect("opening ledger file")),
Box::new(
OpenOptions::new()
.create(true)
.append(true)
.open(path)
.expect("opening ledger file"),
),
),
Ledger::StdInOut => (Box::new(stdin()), Box::new(stdout())),
};
let reader = BufReader::new(infile);
let entries = entry_writer::read_entries(reader).map(|e| e.expect("failed to parse entry"));
@@ -117,17 +120,6 @@ impl FullNode {
server
} else {
node.data.leader_id = node.data.id;
let outfile_for_leader: Box<Write + Send> = match outfile_for_leader {
Some(OutFile::Path(file)) => Box::new(
OpenOptions::new()
.create(true)
.append(true)
.open(file)
.expect("opening ledger file"),
),
Some(OutFile::StdOut) => Box::new(stdout()),
None => Box::new(sink()),
};
let server = FullNode::new_leader(
bank,
@@ -137,7 +129,7 @@ impl FullNode {
None,
node,
exit.clone(),
outfile_for_leader,
outfile,
);
info!(
"leader ready... local request address: {} (advertising {})",