From 3ab12076e8198a9cd0d7243a10711f1687bd7e47 Mon Sep 17 00:00:00 2001 From: Greg Fitzgerald Date: Sat, 1 Sep 2018 16:03:45 -1000 Subject: [PATCH] Convert voting functions to methods More idiomatic Rust. --- src/ledger.rs | 10 +++++++++- src/lib.rs | 1 - src/replicate_stage.rs | 6 ++---- src/transaction.rs | 8 ++++++++ src/voting.rs | 18 ------------------ src/write_stage.rs | 3 +-- 6 files changed, 20 insertions(+), 26 deletions(-) delete mode 100644 src/voting.rs diff --git a/src/ledger.rs b/src/ledger.rs index 263b1d4026..455d189823 100644 --- a/src/ledger.rs +++ b/src/ledger.rs @@ -9,13 +9,14 @@ use log::Level::Trace; use packet::{self, SharedBlob, BLOB_DATA_SIZE}; use rayon::prelude::*; use result::{Error, Result}; +use signature::Pubkey; use std::collections::VecDeque; use std::fs::{create_dir_all, remove_dir_all, File, OpenOptions}; use std::io::prelude::*; use std::io::{self, BufReader, BufWriter, Seek, SeekFrom}; use std::mem::size_of; use std::path::Path; -use transaction::Transaction; +use transaction::{Transaction, Vote}; use window::WINDOW_SIZE; // @@ -413,6 +414,7 @@ pub trait Block { /// Verifies the hashes and counts of a slice of transactions are all consistent. fn verify(&self, start_hash: &Hash) -> bool; fn to_blobs(&self, blob_recycler: &packet::BlobRecycler, q: &mut VecDeque); + fn votes(&self) -> Vec<(Pubkey, Vote, Hash)>; } impl Block for [Entry] { @@ -438,6 +440,12 @@ impl Block for [Entry] { q.push_back(blob); } } + + fn votes(&self) -> Vec<(Pubkey, Vote, Hash)> { + self.iter() + .flat_map(|entry| entry.transactions.iter().filter_map(Transaction::vote)) + .collect() + } } pub fn reconstruct_entries_from_blobs(blobs: VecDeque) -> Result> { diff --git a/src/lib.rs b/src/lib.rs index fe7bb03aaa..bd8b941b76 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -54,7 +54,6 @@ pub mod tpu; pub mod transaction; pub mod tvu; pub mod vote_stage; -pub mod voting; pub mod wallet; pub mod window; pub mod write_stage; diff --git a/src/replicate_stage.rs b/src/replicate_stage.rs index 9eaadfa89f..5c91304355 100644 --- a/src/replicate_stage.rs +++ b/src/replicate_stage.rs @@ -3,7 +3,7 @@ use bank::Bank; use counter::Counter; use crdt::Crdt; -use ledger::{reconstruct_entries_from_blobs, LedgerWriter}; +use ledger::{reconstruct_entries_from_blobs, Block, LedgerWriter}; use log::Level; use packet::BlobRecycler; use result::{Error, Result}; @@ -19,7 +19,6 @@ use std::thread::{self, Builder, JoinHandle}; use std::time::Duration; use streamer::{responder, BlobReceiver}; use vote_stage::VoteStage; -use voting::entries_to_votes; pub struct ReplicateStage { thread_hdls: Vec>, @@ -49,9 +48,8 @@ impl ReplicateStage { } { - let votes = entries_to_votes(&entries); let mut wcrdt = crdt.write().unwrap(); - wcrdt.insert_votes(&votes); + wcrdt.insert_votes(&entries.votes()); } inc_new_counter_info!( diff --git a/src/transaction.rs b/src/transaction.rs index 09be5027f7..73fed6c81e 100644 --- a/src/transaction.rs +++ b/src/transaction.rs @@ -225,6 +225,14 @@ impl Transaction { true } } + + pub fn vote(&self) -> Option<(Pubkey, Vote, Hash)> { + if let Instruction::NewVote(ref vote) = self.instruction { + Some((self.from, vote.clone(), self.last_id)) + } else { + None + } + } } pub fn test_tx() -> Transaction { diff --git a/src/voting.rs b/src/voting.rs deleted file mode 100644 index 559ec08ec1..0000000000 --- a/src/voting.rs +++ /dev/null @@ -1,18 +0,0 @@ -use entry::Entry; -use hash::Hash; -use signature::Pubkey; -use transaction::{Instruction, Transaction, Vote}; - -pub fn entries_to_votes(entries: &[Entry]) -> Vec<(Pubkey, Vote, Hash)> { - entries - .iter() - .flat_map(|entry| entry.transactions.iter().filter_map(transaction_to_vote)) - .collect() -} - -pub fn transaction_to_vote(tx: &Transaction) -> Option<(Pubkey, Vote, Hash)> { - match tx.instruction { - Instruction::NewVote(ref vote) => Some((tx.from, vote.clone(), tx.last_id)), - _ => None, - } -} diff --git a/src/write_stage.rs b/src/write_stage.rs index 7ab7ef317b..5abe88a650 100644 --- a/src/write_stage.rs +++ b/src/write_stage.rs @@ -21,7 +21,6 @@ use std::thread::{self, Builder, JoinHandle}; use std::time::Duration; use streamer::{responder, BlobReceiver, BlobSender}; use vote_stage::send_leader_vote; -use voting::entries_to_votes; pub struct WriteStage { thread_hdls: Vec>, @@ -40,7 +39,7 @@ impl WriteStage { ) -> Result<()> { let entries = entry_receiver.recv_timeout(Duration::new(1, 0))?; - let votes = entries_to_votes(&entries); + let votes = &entries.votes(); crdt.write().unwrap().insert_votes(&votes); ledger_writer.write_entries(entries.clone())?;