Convert voting functions to methods

More idiomatic Rust.
This commit is contained in:
Greg Fitzgerald 2018-09-01 16:03:45 -10:00
parent 6a383c45fc
commit 3ab12076e8
6 changed files with 20 additions and 26 deletions

View File

@ -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<SharedBlob>);
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<SharedBlob>) -> Result<Vec<Entry>> {

View File

@ -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;

View File

@ -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<JoinHandle<()>>,
@ -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!(

View File

@ -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 {

View File

@ -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,
}
}

View File

@ -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<JoinHandle<()>>,
@ -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())?;