remove entry_writer.rs (#1720)

This commit is contained in:
Rob Walker
2018-11-06 12:42:31 -08:00
committed by GitHub
parent bfad138bb3
commit 76694bfcf4
3 changed files with 0 additions and 139 deletions

View File

@ -1487,14 +1487,12 @@ mod tests {
use budget_program::BudgetState;
use entry::next_entry;
use entry::Entry;
use entry_writer::{self, EntryWriter};
use hash::hash;
use jsonrpc_macros::pubsub::{Subscriber, SubscriptionId};
use ledger;
use signature::Keypair;
use signature::{GenKeys, KeypairUtil};
use std;
use std::io::{BufReader, Cursor, Seek, SeekFrom};
use system_transaction::SystemTransaction;
use tokio::prelude::{Async, Stream};
use transaction::Instruction;
@ -1915,40 +1913,6 @@ mod tests {
assert_eq!(bank.last_id(), last_id);
}
// Write the given entries to a file and then return a file iterator to them.
fn to_file_iter(entries: impl Iterator<Item = Entry>) -> impl Iterator<Item = Entry> {
let mut file = Cursor::new(vec![]);
EntryWriter::write_entries(&mut file, entries).unwrap();
file.seek(SeekFrom::Start(0)).unwrap();
let reader = BufReader::new(file);
entry_writer::read_entries(reader).map(|x| x.unwrap())
}
#[test]
fn test_process_ledger_from_file() {
let (ledger, pubkey) = create_sample_ledger(1);
let ledger = to_file_iter(ledger);
let bank = Bank::default();
bank.process_ledger(ledger).unwrap();
assert_eq!(bank.get_balance(&pubkey), 1);
}
#[test]
fn test_process_ledger_from_files() {
let dummy_leader_id = Keypair::new().pubkey();
let dummy_leader_tokens = 1;
let mint = Mint::new_with_leader(2, dummy_leader_id, dummy_leader_tokens);
let genesis = to_file_iter(mint.create_entries().into_iter());
let block = to_file_iter(create_sample_block_with_ticks(&mint, 1, 1));
let bank = Bank::default();
bank.process_ledger(genesis.chain(block)).unwrap();
assert_eq!(bank.get_balance(&mint.pubkey()), 0);
}
#[test]
fn test_hash_internal_state() {
let dummy_leader_id = Keypair::new().pubkey();

View File

@ -1,102 +0,0 @@
//! The `entry_writer` module helps implement the TPU's write stage. It
//! writes entries to the given writer, which is typically a file or
//! stdout, and then sends the Entry to its output channel.
use bincode;
use entry::Entry;
use std::io::{self, BufRead, Error, ErrorKind, Write};
use std::mem::size_of;
pub struct EntryWriter {}
impl EntryWriter {
fn write_entry<W: Write>(writer: &mut W, entry: &Entry) -> io::Result<()> {
let entry_bytes =
bincode::serialize(&entry).map_err(|e| Error::new(ErrorKind::Other, e.to_string()))?;
let len = entry_bytes.len();
let len_bytes =
bincode::serialize(&len).map_err(|e| Error::new(ErrorKind::Other, e.to_string()))?;
writer.write_all(&len_bytes[..])?;
writer.write_all(&entry_bytes[..])?;
writer.flush()
}
pub fn write_entries<W: Write, I>(writer: &mut W, entries: I) -> io::Result<()>
where
I: IntoIterator<Item = Entry>,
{
for entry in entries {
Self::write_entry(writer, &entry)?;
}
Ok(())
}
}
struct EntryReader<R: BufRead> {
reader: R,
entry_bytes: Vec<u8>,
}
impl<R: BufRead> Iterator for EntryReader<R> {
type Item = io::Result<Entry>;
fn next(&mut self) -> Option<io::Result<Entry>> {
let mut entry_len_bytes = [0u8; size_of::<usize>()];
if self.reader.read_exact(&mut entry_len_bytes[..]).is_ok() {
let entry_len = bincode::deserialize(&entry_len_bytes).unwrap();
if entry_len > self.entry_bytes.len() {
self.entry_bytes.resize(entry_len, 0);
}
if let Err(e) = self.reader.read_exact(&mut self.entry_bytes[..entry_len]) {
Some(Err(e))
} else {
Some(
bincode::deserialize(&self.entry_bytes)
.map_err(|e| Error::new(ErrorKind::Other, e.to_string())),
)
}
} else {
None // EOF (probably)
}
}
}
/// Return an iterator for all the entries in the given file.
pub fn read_entries<R: BufRead>(reader: R) -> impl Iterator<Item = io::Result<Entry>> {
EntryReader {
reader,
entry_bytes: Vec::new(),
}
}
#[cfg(test)]
mod tests {
use super::*;
use mint::Mint;
use std::io::Cursor;
/// Same as read_entries() but parsing a buffer and returning a vector.
fn read_entries_from_buf(s: &[u8]) -> io::Result<Vec<Entry>> {
let mut result = vec![];
let reader = Cursor::new(s);
for x in read_entries(reader) {
trace!("entry... {:?}", x);
result.push(x?);
}
Ok(result)
}
#[test]
fn test_read_entries_from_buf() {
let mint = Mint::new(1);
let mut buf = vec![];
EntryWriter::write_entries(&mut buf, mint.create_entries()).unwrap();
let entries = read_entries_from_buf(&buf).unwrap();
assert_eq!(entries, mint.create_entries());
}
}

View File

@ -29,7 +29,6 @@ pub mod budget_program;
pub mod compute_leader_finality_service;
pub mod drone;
pub mod entry;
pub mod entry_writer;
#[cfg(feature = "erasure")]
pub mod erasure;
pub mod fetch_stage;