2018-03-29 11:20:54 -07:00
|
|
|
//! The `ledger` module provides the functions for parallel verification of the
|
|
|
|
//! Proof of History ledger.
|
2018-02-15 09:13:56 -08:00
|
|
|
|
2018-03-26 21:03:26 -07:00
|
|
|
use entry::{next_tick, Entry};
|
2018-03-06 16:36:45 -08:00
|
|
|
use hash::Hash;
|
2018-03-04 06:28:51 -08:00
|
|
|
use rayon::prelude::*;
|
2018-02-26 15:42:31 -08:00
|
|
|
|
2018-02-15 09:13:56 -08:00
|
|
|
/// Verifies the hashes and counts of a slice of events are all consistent.
|
2018-03-09 15:16:29 -08:00
|
|
|
pub fn verify_slice(entries: &[Entry], start_hash: &Hash) -> bool {
|
2018-02-19 15:17:13 -08:00
|
|
|
let genesis = [Entry::new_tick(Default::default(), start_hash)];
|
2018-03-28 21:02:47 -07:00
|
|
|
let entry_pairs = genesis.par_iter().chain(entries).zip(entries);
|
|
|
|
entry_pairs.all(|(x0, x1)| x1.verify(&x0.id))
|
2018-02-15 09:13:56 -08:00
|
|
|
}
|
|
|
|
|
2018-03-22 13:38:06 -07:00
|
|
|
/// Create a vector of Ticks of length `len` from `start_hash` hash and `num_hashes`.
|
2018-03-06 19:22:30 -08:00
|
|
|
pub fn next_ticks(start_hash: &Hash, num_hashes: u64, len: usize) -> Vec<Entry> {
|
2018-03-04 06:34:38 -08:00
|
|
|
let mut id = *start_hash;
|
2018-03-04 13:30:39 -08:00
|
|
|
let mut ticks = vec![];
|
|
|
|
for _ in 0..len {
|
|
|
|
let entry = next_tick(&id, num_hashes);
|
|
|
|
id = entry.id;
|
|
|
|
ticks.push(entry);
|
|
|
|
}
|
|
|
|
ticks
|
2018-02-15 09:13:56 -08:00
|
|
|
}
|
|
|
|
|
2018-02-15 16:47:05 -08:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2018-03-06 16:31:17 -08:00
|
|
|
use hash::hash;
|
2018-02-15 16:47:05 -08:00
|
|
|
|
2018-03-06 19:22:30 -08:00
|
|
|
#[test]
|
|
|
|
fn test_verify_slice() {
|
2018-03-06 16:36:45 -08:00
|
|
|
let zero = Hash::default();
|
2018-02-19 15:17:13 -08:00
|
|
|
let one = hash(&zero);
|
|
|
|
assert!(verify_slice(&vec![], &zero)); // base case
|
|
|
|
assert!(verify_slice(&vec![Entry::new_tick(0, &zero)], &zero)); // singleton case 1
|
|
|
|
assert!(!verify_slice(&vec![Entry::new_tick(0, &zero)], &one)); // singleton case 2, bad
|
2018-03-04 13:30:39 -08:00
|
|
|
assert!(verify_slice(&next_ticks(&zero, 0, 2), &zero)); // inductive step
|
2018-02-19 15:17:13 -08:00
|
|
|
|
2018-03-04 13:30:39 -08:00
|
|
|
let mut bad_ticks = next_ticks(&zero, 0, 2);
|
2018-03-04 06:34:38 -08:00
|
|
|
bad_ticks[1].id = one;
|
2018-02-19 15:17:13 -08:00
|
|
|
assert!(!verify_slice(&bad_ticks, &zero)); // inductive step, bad
|
2018-02-16 08:14:42 -08:00
|
|
|
}
|
2018-02-15 16:47:05 -08:00
|
|
|
}
|
|
|
|
|
2018-02-15 09:13:56 -08:00
|
|
|
#[cfg(all(feature = "unstable", test))]
|
|
|
|
mod bench {
|
|
|
|
extern crate test;
|
|
|
|
use self::test::Bencher;
|
2018-03-19 09:09:17 -07:00
|
|
|
use ledger::*;
|
2018-02-15 09:13:56 -08:00
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn event_bench(bencher: &mut Bencher) {
|
2018-02-19 15:17:13 -08:00
|
|
|
let start_hash = Default::default();
|
2018-03-04 13:30:39 -08:00
|
|
|
let events = next_ticks(&start_hash, 10_000, 8);
|
2018-02-16 09:38:12 -08:00
|
|
|
bencher.iter(|| {
|
2018-02-19 15:17:13 -08:00
|
|
|
assert!(verify_slice(&events, &start_hash));
|
2018-02-16 09:38:12 -08:00
|
|
|
});
|
|
|
|
}
|
2018-02-15 09:13:56 -08:00
|
|
|
}
|