diff --git a/src/bin/demo.rs b/src/bin/demo.rs index b158262556..d29049f512 100644 --- a/src/bin/demo.rs +++ b/src/bin/demo.rs @@ -19,7 +19,7 @@ fn main() { let hist = Historian::new(&seed, Some(10)); create_log(&hist).expect("send error"); drop(hist.sender); - let entries: Vec = hist.receiver.iter().collect(); + let entries: Vec> = hist.receiver.iter().collect(); for entry in &entries { println!("{:?}", entry); } diff --git a/src/historian.rs b/src/historian.rs index 52dccda910..358d7f8821 100644 --- a/src/historian.rs +++ b/src/historian.rs @@ -12,8 +12,8 @@ use log::{hash, hash_event, Entry, Event, Sha256Hash}; pub struct Historian { pub sender: Sender>, - pub receiver: Receiver, - pub thread_hdl: JoinHandle<(Entry, ExitReason)>, + pub receiver: Receiver>, + pub thread_hdl: JoinHandle<(Entry, ExitReason)>, } #[derive(Debug, PartialEq, Eq)] @@ -22,11 +22,11 @@ pub enum ExitReason { SendDisconnected, } fn log_event( - sender: &Sender, + sender: &Sender>, num_hashes: &mut u64, end_hash: &mut Sha256Hash, event: Event, -) -> Result<(), (Entry, ExitReason)> { +) -> Result<(), (Entry, ExitReason)> { *end_hash = hash_event(end_hash, &event); let entry = Entry { end_hash: *end_hash, @@ -42,13 +42,13 @@ fn log_event( fn log_events( receiver: &Receiver>, - sender: &Sender, + sender: &Sender>, num_hashes: &mut u64, end_hash: &mut Sha256Hash, epoch: SystemTime, num_ticks: &mut u64, ms_per_tick: Option, -) -> Result<(), (Entry, ExitReason)> { +) -> Result<(), (Entry, ExitReason)> { use std::sync::mpsc::TryRecvError; loop { if let Some(ms) = ms_per_tick { @@ -83,8 +83,8 @@ pub fn create_logger( start_hash: Sha256Hash, ms_per_tick: Option, receiver: Receiver>, - sender: Sender, -) -> JoinHandle<(Entry, ExitReason)> { + sender: Sender>, +) -> JoinHandle<(Entry, ExitReason)> { use std::thread; thread::spawn(move || { let mut end_hash = start_hash; @@ -179,7 +179,7 @@ mod tests { ExitReason::RecvDisconnected ); - let entries: Vec = hist.receiver.iter().collect(); + let entries: Vec> = hist.receiver.iter().collect(); assert!(entries.len() > 1); assert!(verify_slice(&entries, &zero)); } diff --git a/src/log.rs b/src/log.rs index 0739f51ea3..d20457125f 100644 --- a/src/log.rs +++ b/src/log.rs @@ -21,10 +21,10 @@ pub type PublicKey = GenericArray; pub type Signature = GenericArray; #[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)] -pub struct Entry { +pub struct Entry { pub num_hashes: u64, pub end_hash: Sha256Hash, - pub event: Event, + pub event: Event, } /// When 'event' is Tick, the event represents a simple clock tick, and exists for the @@ -51,7 +51,7 @@ pub enum Event { }, } -impl Entry { +impl Entry { /// Creates a Entry from the number of hashes 'num_hashes' since the previous event /// and that resulting 'end_hash'. pub fn new_tick(num_hashes: u64, end_hash: &Sha256Hash) -> Self { @@ -157,7 +157,11 @@ pub fn next_hash( } /// Creates the next Tick Entry 'num_hashes' after 'start_hash'. -pub fn next_entry(start_hash: &Sha256Hash, num_hashes: u64, event: Event) -> Entry { +pub fn next_entry( + start_hash: &Sha256Hash, + num_hashes: u64, + event: Event, +) -> Entry { Entry { num_hashes, end_hash: next_hash(start_hash, num_hashes, &event), @@ -169,20 +173,20 @@ pub fn next_entry_mut( start_hash: &mut Sha256Hash, num_hashes: u64, event: Event, -) -> Entry { +) -> Entry { let entry = next_entry(start_hash, num_hashes, event); *start_hash = entry.end_hash; entry } /// Creates the next Tick Entry 'num_hashes' after 'start_hash'. -pub fn next_tick(start_hash: &Sha256Hash, num_hashes: u64) -> Entry { +pub fn next_tick(start_hash: &Sha256Hash, num_hashes: u64) -> Entry { next_entry(start_hash, num_hashes, Event::Tick) } /// Verifies self.end_hash is the result of hashing a 'start_hash' 'self.num_hashes' times. /// If the event is not a Tick, then hash that as well. -pub fn verify_entry(entry: &Entry, start_hash: &Sha256Hash) -> bool { +pub fn verify_entry(entry: &Entry, start_hash: &Sha256Hash) -> bool { if let Event::Claim { key, data, sig } = entry.event { if !verify_signature(&key, &data, &sig) { return false; @@ -205,7 +209,7 @@ pub fn verify_entry(entry: &Entry, start_hash: &Sha256Hash) -> bool { } /// Verifies the hashes and counts of a slice of events are all consistent. -pub fn verify_slice(events: &[Entry], start_hash: &Sha256Hash) -> bool { +pub fn verify_slice(events: &[Entry], start_hash: &Sha256Hash) -> bool { use rayon::prelude::*; let genesis = [Entry::new_tick(Default::default(), start_hash)]; let event_pairs = genesis.par_iter().chain(events).zip(events); @@ -213,7 +217,7 @@ pub fn verify_slice(events: &[Entry], start_hash: &Sha256Hash) -> bool { } /// Verifies the hashes and events serially. Exists only for reference. -pub fn verify_slice_seq(events: &[Entry], start_hash: &Sha256Hash) -> bool { +pub fn verify_slice_seq(events: &[Entry], start_hash: &Sha256Hash) -> bool { let genesis = [Entry::new_tick(0, start_hash)]; let mut event_pairs = genesis.iter().chain(events).zip(events); event_pairs.all(|(x0, x1)| verify_entry(&x1, &x0.end_hash)) @@ -233,7 +237,7 @@ pub fn create_entries( start_hash: &Sha256Hash, num_hashes: u64, events: &[Event], -) -> Vec { +) -> Vec> { let mut end_hash = *start_hash; events .iter() @@ -242,7 +246,11 @@ pub fn create_entries( } /// Create a vector of Ticks of length 'len' from 'start_hash' hash and 'num_hashes'. -pub fn create_ticks(start_hash: &Sha256Hash, num_hashes: u64, len: usize) -> Vec { +pub fn create_ticks( + start_hash: &Sha256Hash, + num_hashes: u64, + len: usize, +) -> Vec> { use std::iter; let mut end_hash = *start_hash; iter::repeat(Event::Tick) @@ -271,7 +279,7 @@ mod tests { assert_eq!(next_tick(&zero, 1).num_hashes, 1) } - fn verify_slice_generic(verify_slice: fn(&[Entry], &Sha256Hash) -> bool) { + fn verify_slice_generic(verify_slice: fn(&[Entry], &Sha256Hash) -> bool) { let zero = Sha256Hash::default(); let one = hash(&zero); assert!(verify_slice(&vec![], &zero)); // base case