2018-02-18 09:59:15 -07:00
|
|
|
//! The `log` crate provides the foundational data structures for Proof-of-History,
|
|
|
|
//! an ordered log of events in time.
|
2018-02-15 10:13:56 -07:00
|
|
|
|
2018-02-18 09:59:15 -07:00
|
|
|
/// Each log entry contains three pieces of data. The 'num_hashes' field is the number
|
2018-03-04 07:34:38 -07:00
|
|
|
/// of hashes performed since the previous entry. The 'id' field is the result
|
|
|
|
/// of hashing 'id' from the previous entry 'num_hashes' times. The 'event'
|
|
|
|
/// field points to an Event that took place shortly after 'id' was generated.
|
2018-02-15 10:13:56 -07:00
|
|
|
///
|
2018-02-15 10:48:30 -07:00
|
|
|
/// If you divide 'num_hashes' by the amount of time it takes to generate a new hash, you
|
2018-02-15 10:13:56 -07:00
|
|
|
/// get a duration estimate since the last event. Since processing power increases
|
2018-02-15 10:48:30 -07:00
|
|
|
/// over time, one should expect the duration 'num_hashes' represents to decrease proportionally.
|
2018-02-15 10:13:56 -07:00
|
|
|
/// Though processing power varies across nodes, the network gives priority to the
|
|
|
|
/// fastest processor. Duration should therefore be estimated by assuming that the hash
|
|
|
|
/// was generated by the fastest processor at the time the entry was logged.
|
2018-02-19 16:17:13 -07:00
|
|
|
|
2018-03-06 17:36:45 -07:00
|
|
|
use hash::Hash;
|
2018-03-09 16:16:29 -07:00
|
|
|
use entry::{create_entry, next_tick, Entry};
|
2018-03-06 12:26:39 -07:00
|
|
|
use event::Event;
|
2018-03-04 07:28:51 -07:00
|
|
|
use rayon::prelude::*;
|
2018-02-26 16:42:31 -07:00
|
|
|
|
2018-02-15 10:13:56 -07:00
|
|
|
/// Verifies the hashes and counts of a slice of events are all consistent.
|
2018-03-09 16:16:29 -07:00
|
|
|
pub fn verify_slice(entries: &[Entry], start_hash: &Hash) -> bool {
|
2018-02-19 16:17:13 -07:00
|
|
|
let genesis = [Entry::new_tick(Default::default(), start_hash)];
|
2018-03-09 16:16:29 -07:00
|
|
|
let event_pairs = genesis.par_iter().chain(entries).zip(entries);
|
2018-03-06 12:35:12 -07:00
|
|
|
event_pairs.all(|(x0, x1)| x1.verify(&x0.id))
|
2018-02-15 10:13:56 -07:00
|
|
|
}
|
|
|
|
|
2018-03-06 20:22:30 -07:00
|
|
|
pub fn create_entries(start_hash: &Hash, events: Vec<Event>) -> Vec<Entry> {
|
2018-03-09 16:16:29 -07:00
|
|
|
vec![create_entry(start_hash, 0, events)]
|
2018-02-26 11:01:19 -07:00
|
|
|
}
|
|
|
|
|
2018-02-15 11:50:48 -07:00
|
|
|
/// Create a vector of Ticks of length 'len' from 'start_hash' hash and 'num_hashes'.
|
2018-03-06 20:22:30 -07:00
|
|
|
pub fn next_ticks(start_hash: &Hash, num_hashes: u64, len: usize) -> Vec<Entry> {
|
2018-03-04 07:34:38 -07:00
|
|
|
let mut id = *start_hash;
|
2018-03-04 14:30:39 -07: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 10:13:56 -07:00
|
|
|
}
|
|
|
|
|
2018-02-15 17:47:05 -07:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2018-03-06 17:31:17 -07:00
|
|
|
use hash::hash;
|
2018-02-15 17:47:05 -07:00
|
|
|
|
2018-03-06 20:22:30 -07:00
|
|
|
#[test]
|
|
|
|
fn test_verify_slice() {
|
2018-03-06 17:36:45 -07:00
|
|
|
let zero = Hash::default();
|
2018-02-19 16:17:13 -07: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 14:30:39 -07:00
|
|
|
assert!(verify_slice(&next_ticks(&zero, 0, 2), &zero)); // inductive step
|
2018-02-19 16:17:13 -07:00
|
|
|
|
2018-03-04 14:30:39 -07:00
|
|
|
let mut bad_ticks = next_ticks(&zero, 0, 2);
|
2018-03-04 07:34:38 -07:00
|
|
|
bad_ticks[1].id = one;
|
2018-02-19 16:17:13 -07:00
|
|
|
assert!(!verify_slice(&bad_ticks, &zero)); // inductive step, bad
|
2018-02-16 09:14:42 -07:00
|
|
|
}
|
|
|
|
|
2018-03-09 16:16:29 -07:00
|
|
|
// TODO: This is no longer relevant. Instead, test for reordered ticks.
|
|
|
|
//#[test]
|
|
|
|
//fn test_reorder_attack() {
|
|
|
|
// let zero = Hash::default();
|
2018-02-20 14:46:36 -07:00
|
|
|
|
2018-03-09 16:16:29 -07:00
|
|
|
// // First, verify entries
|
|
|
|
// let keypair = KeyPair::new();
|
|
|
|
// let tr0 = Transaction::new(&keypair, keypair.pubkey(), 0, zero);
|
|
|
|
// let tr1 = Transaction::new(&keypair, keypair.pubkey(), 1, zero);
|
|
|
|
// let events = vec![Event::Transaction(tr0), Event::Transaction(tr1)];
|
|
|
|
// let mut entries = create_entries(&zero, events);
|
|
|
|
// assert!(verify_slice(&entries, &zero));
|
2018-02-20 14:46:36 -07:00
|
|
|
|
2018-03-09 16:16:29 -07:00
|
|
|
// // Next, swap two events and ensure verification fails.
|
|
|
|
// let event0 = entries[0].event.clone();
|
|
|
|
// let event1 = entries[1].event.clone();
|
|
|
|
// entries[0].event = event1;
|
|
|
|
// entries[1].event = event0;
|
|
|
|
// assert!(!verify_slice(&entries, &zero));
|
|
|
|
//}
|
2018-02-15 17:47:05 -07:00
|
|
|
}
|
|
|
|
|
2018-02-15 10:13:56 -07:00
|
|
|
#[cfg(all(feature = "unstable", test))]
|
|
|
|
mod bench {
|
|
|
|
extern crate test;
|
|
|
|
use self::test::Bencher;
|
2018-02-18 09:59:15 -07:00
|
|
|
use log::*;
|
2018-02-15 10:13:56 -07:00
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn event_bench(bencher: &mut Bencher) {
|
2018-02-19 16:17:13 -07:00
|
|
|
let start_hash = Default::default();
|
2018-03-04 14:30:39 -07:00
|
|
|
let events = next_ticks(&start_hash, 10_000, 8);
|
2018-02-16 09:38:12 -08:00
|
|
|
bencher.iter(|| {
|
2018-02-19 16:17:13 -07:00
|
|
|
assert!(verify_slice(&events, &start_hash));
|
2018-02-16 09:38:12 -08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn event_bench_seq(bencher: &mut Bencher) {
|
2018-02-19 16:17:13 -07:00
|
|
|
let start_hash = Default::default();
|
2018-03-04 14:30:39 -07:00
|
|
|
let events = next_ticks(&start_hash, 10_000, 8);
|
2018-02-15 10:13:56 -07:00
|
|
|
bencher.iter(|| {
|
2018-02-26 17:03:50 -07:00
|
|
|
assert!(verify_slice_seq(&events, &start_hash));
|
2018-02-15 10:13:56 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|