Consolidate ledger serialization code

The new read_entries() works, but is overly-contrained. Not
using that function yet, but adding it here in the hopes some
Rust guru will tell us how to get that lifetime constraint out
of there.

Fixes #517
This commit is contained in:
Greg Fitzgerald 2018-07-02 00:09:41 -06:00 committed by Greg Fitzgerald
parent e7b7dfebf5
commit ec7e50b37d
3 changed files with 18 additions and 9 deletions

View File

@ -519,10 +519,9 @@ mod tests {
use super::*;
use bincode::serialize;
use entry::next_entry;
use entry_writer::EntryWriter;
use entry_writer::{self, EntryWriter};
use hash::hash;
use ledger::next_entries;
use serde_json;
use signature::KeyPairUtil;
use std::io::{BufRead, BufReader, Seek, SeekFrom};
@ -800,12 +799,12 @@ mod tests {
let mut file = tempfile().unwrap();
EntryWriter::write_entries(&mut file, &entries).unwrap();
file.seek(SeekFrom::Start(0)).unwrap();
let reader = BufReader::new(file);
reader
.lines()
.map(|line| serde_json::from_str(&line.unwrap()).unwrap())
.map(|line| entry_writer::read_entry(line.unwrap()).unwrap())
}
#[test]

View File

@ -9,7 +9,7 @@ use atty::{is, Stream};
use getopts::Options;
use solana::bank::Bank;
use solana::crdt::ReplicatedData;
use solana::entry::Entry;
use solana::entry_writer;
use solana::server::Server;
use std::env;
use std::fs::File;
@ -68,11 +68,10 @@ fn main() {
eprintln!("Initializing...");
let stdin = stdin();
let entries = stdin.lock().lines().map(|line| {
let entry: Entry = serde_json::from_str(&line.unwrap()).unwrap_or_else(|e| {
entry_writer::read_entry(line.unwrap()).unwrap_or_else(|e| {
eprintln!("failed to parse json: {}", e);
exit(1);
});
entry
})
});
eprintln!("done parsing...");

View File

@ -5,7 +5,7 @@
use bank::Bank;
use entry::Entry;
use serde_json;
use std::io::{self, Write};
use std::io::{self, BufRead, Error, ErrorKind, Write};
pub struct EntryWriter<'a, W> {
bank: &'a Bank,
@ -46,6 +46,17 @@ 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()))
}
// 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?))
}
#[cfg(test)]
mod tests {
use super::*;