Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup fullnode read/write logs #548

Merged
merged 2 commits into from
Jul 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ mod tests {
use hash::hash;
use ledger::next_entries;
use signature::KeyPairUtil;
use std::io::{BufRead, BufReader, Cursor, Seek, SeekFrom};
use std::io::{BufReader, Cursor, Seek, SeekFrom};

#[test]
fn test_two_payments_to_one_party() {
Expand Down Expand Up @@ -807,9 +807,7 @@ mod tests {
file.seek(SeekFrom::Start(0)).unwrap();

let reader = BufReader::new(file);
reader
.lines()
.map(|line| entry_writer::read_entry(line.unwrap()).unwrap())
entry_writer::read_entries(reader).map(|x| x.unwrap())
}

#[test]
Expand Down
11 changes: 4 additions & 7 deletions src/entry_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,12 @@ impl<'a, W: Write> EntryWriter<'a, W> {
}
}

pub fn read_entry(s: String) -> io::Result<Entry> {
serde_json::from_str(&s).map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
pub fn read_entry(s: &str) -> io::Result<Entry> {
serde_json::from_str(s).map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
}

// TODO: How to implement this without attaching the input's lifetime to the output?
pub fn read_entries<'a, R: BufRead>(
reader: &'a mut R,
) -> impl Iterator<Item = io::Result<Entry>> + 'a {
reader.lines().map(|s| read_entry(s?))
pub fn read_entries<R: BufRead>(reader: R) -> impl Iterator<Item = io::Result<Entry>> {
reader.lines().map(|s| read_entry(&s?))
}

#[cfg(test)]
Expand Down
71 changes: 22 additions & 49 deletions src/fullnode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use ncp::Ncp;
use packet::BlobRecycler;
use rpu::Rpu;
use std::fs::File;
use std::io::Write;
use std::io::{sink, stdin, stdout, BufReader};
use std::io::{Read, Write};
use std::net::SocketAddr;
use std::sync::atomic::AtomicBool;
use std::sync::{Arc, RwLock};
Expand Down Expand Up @@ -44,23 +44,14 @@ impl FullNode {
) -> FullNode {
info!("creating bank...");
let bank = Bank::default();
let entry_height = match infile {
InFile::Path(path) => {
let f = File::open(path).unwrap();
let mut r = BufReader::new(f);
let entries =
entry_writer::read_entries(&mut r).map(|e| e.expect("failed to parse entry"));
info!("processing ledger...");
bank.process_ledger(entries).expect("process_ledger")
}
InFile::StdIn => {
let mut r = BufReader::new(stdin());
let entries =
entry_writer::read_entries(&mut r).map(|e| e.expect("failed to parse entry"));
info!("processing ledger...");
bank.process_ledger(entries).expect("process_ledger")
}
let infile: Box<Read> = match infile {
InFile::Path(path) => Box::new(File::open(path).unwrap()),
InFile::StdIn => Box::new(stdin()),
};
let reader = BufReader::new(infile);
let entries = entry_writer::read_entries(reader).map(|e| e.expect("failed to parse entry"));
info!("processing ledger...");
let entry_height = bank.process_ledger(entries).expect("process_ledger");

// entry_height is the network-wide agreed height of the ledger.
// initialize it from the input ledger
Expand Down Expand Up @@ -93,41 +84,23 @@ impl FullNode {
server
} else {
node.data.current_leader_id = node.data.id.clone();
let server = match outfile_for_leader {
let outfile_for_leader: Box<Write + Send> = match outfile_for_leader {
Some(OutFile::Path(file)) => {
FullNode::new_leader(
bank,
entry_height,
//Some(Duration::from_millis(1000)),
None,
node,
exit.clone(),
File::create(file).expect("opening ledger file"),
)
}
Some(OutFile::StdOut) => {
FullNode::new_leader(
bank,
entry_height,
//Some(Duration::from_millis(1000)),
None,
node,
exit.clone(),
stdout(),
)
}
None => {
FullNode::new_leader(
bank,
entry_height,
//Some(Duration::from_millis(1000)),
None,
node,
exit.clone(),
sink(),
)
Box::new(File::create(file).expect("opening ledger file"))
}
Some(OutFile::StdOut) => Box::new(stdout()),
None => Box::new(sink()),
};

let server = FullNode::new_leader(
bank,
entry_height,
//Some(Duration::from_millis(1000)),
None,
node,
exit.clone(),
outfile_for_leader,
);
info!(
"leader ready... local request address: {} (advertising {})",
local_requests_addr, requests_addr
Expand Down