2018-03-06 16:31:17 -08:00
|
|
|
use event::Event;
|
2018-03-26 21:03:26 -07:00
|
|
|
use hash::{extend_and_hash, hash, Hash};
|
2018-03-28 21:02:47 -07:00
|
|
|
use rayon::prelude::*;
|
2018-03-06 16:31:17 -08:00
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
|
2018-03-06 19:22:30 -08:00
|
|
|
pub struct Entry {
|
2018-03-06 16:31:17 -08:00
|
|
|
pub num_hashes: u64,
|
2018-03-06 16:36:45 -08:00
|
|
|
pub id: Hash,
|
2018-03-09 15:16:29 -08:00
|
|
|
pub events: Vec<Event>,
|
2018-03-06 16:31:17 -08:00
|
|
|
}
|
|
|
|
|
2018-03-06 19:22:30 -08:00
|
|
|
impl Entry {
|
2018-03-22 13:40:28 -07:00
|
|
|
/// Creates a Entry from the number of hashes `num_hashes` since the previous event
|
|
|
|
/// and that resulting `id`.
|
2018-03-06 16:36:45 -08:00
|
|
|
pub fn new_tick(num_hashes: u64, id: &Hash) -> Self {
|
2018-03-06 16:31:17 -08:00
|
|
|
Entry {
|
|
|
|
num_hashes,
|
|
|
|
id: *id,
|
2018-03-09 15:16:29 -08:00
|
|
|
events: vec![],
|
2018-03-06 16:31:17 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-22 13:40:28 -07:00
|
|
|
/// Verifies self.id is the result of hashing a `start_hash` `self.num_hashes` times.
|
2018-03-06 16:31:17 -08:00
|
|
|
/// If the event is not a Tick, then hash that as well.
|
2018-03-06 16:36:45 -08:00
|
|
|
pub fn verify(&self, start_hash: &Hash) -> bool {
|
2018-03-28 21:02:47 -07:00
|
|
|
self.events.par_iter().all(|event| event.verify())
|
|
|
|
&& self.id == next_hash(start_hash, self.num_hashes, &self.events)
|
2018-03-06 16:31:17 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-22 13:40:28 -07:00
|
|
|
/// Creates the hash `num_hashes` after `start_hash`. If the event contains
|
2018-03-06 16:31:17 -08:00
|
|
|
/// signature, the final hash will be a hash of both the previous ID and
|
|
|
|
/// the signature.
|
2018-03-09 15:16:29 -08:00
|
|
|
pub fn next_hash(start_hash: &Hash, num_hashes: u64, events: &[Event]) -> Hash {
|
2018-03-06 16:31:17 -08:00
|
|
|
let mut id = *start_hash;
|
2018-03-09 15:16:29 -08:00
|
|
|
for _ in 1..num_hashes {
|
2018-03-06 16:31:17 -08:00
|
|
|
id = hash(&id);
|
|
|
|
}
|
2018-03-09 15:16:29 -08:00
|
|
|
|
|
|
|
// Hash all the event data
|
|
|
|
let mut hash_data = vec![];
|
|
|
|
for event in events {
|
|
|
|
let sig = event.get_signature();
|
|
|
|
if let Some(sig) = sig {
|
|
|
|
hash_data.extend_from_slice(&sig);
|
|
|
|
}
|
2018-03-06 16:31:17 -08:00
|
|
|
}
|
2018-03-09 15:16:29 -08:00
|
|
|
|
|
|
|
if !hash_data.is_empty() {
|
|
|
|
return extend_and_hash(&id, &hash_data);
|
|
|
|
}
|
|
|
|
|
2018-03-06 16:31:17 -08:00
|
|
|
id
|
|
|
|
}
|
|
|
|
|
2018-03-22 13:40:28 -07:00
|
|
|
/// Creates the next Entry `num_hashes` after `start_hash`.
|
2018-03-09 15:16:29 -08:00
|
|
|
pub fn create_entry(start_hash: &Hash, cur_hashes: u64, events: Vec<Event>) -> Entry {
|
|
|
|
let num_hashes = cur_hashes + if events.is_empty() { 0 } else { 1 };
|
|
|
|
let id = next_hash(start_hash, 0, &events);
|
2018-03-06 16:31:17 -08:00
|
|
|
Entry {
|
|
|
|
num_hashes,
|
|
|
|
id,
|
2018-03-09 15:16:29 -08:00
|
|
|
events,
|
2018-03-06 16:31:17 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-22 13:40:28 -07:00
|
|
|
/// Creates the next Tick Entry `num_hashes` after `start_hash`.
|
2018-03-09 15:16:29 -08:00
|
|
|
pub fn create_entry_mut(start_hash: &mut Hash, cur_hashes: &mut u64, events: Vec<Event>) -> Entry {
|
|
|
|
let entry = create_entry(start_hash, *cur_hashes, events);
|
2018-03-06 16:31:17 -08:00
|
|
|
*start_hash = entry.id;
|
|
|
|
*cur_hashes = 0;
|
|
|
|
entry
|
|
|
|
}
|
|
|
|
|
2018-03-22 13:40:28 -07:00
|
|
|
/// Creates the next Tick Entry `num_hashes` after `start_hash`.
|
2018-03-06 19:22:30 -08:00
|
|
|
pub fn next_tick(start_hash: &Hash, num_hashes: u64) -> Entry {
|
2018-03-06 16:31:17 -08:00
|
|
|
Entry {
|
|
|
|
num_hashes,
|
2018-03-09 15:16:29 -08:00
|
|
|
id: next_hash(start_hash, num_hashes, &[]),
|
|
|
|
events: vec![],
|
2018-03-06 16:31:17 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2018-03-26 21:03:26 -07:00
|
|
|
use entry::create_entry;
|
|
|
|
use event::Event;
|
2018-03-09 16:02:17 -08:00
|
|
|
use hash::hash;
|
|
|
|
use signature::{KeyPair, KeyPairUtil};
|
|
|
|
use transaction::Transaction;
|
2018-03-06 16:31:17 -08:00
|
|
|
|
|
|
|
#[test]
|
2018-03-09 16:02:17 -08:00
|
|
|
fn test_entry_verify() {
|
2018-03-06 16:36:45 -08:00
|
|
|
let zero = Hash::default();
|
2018-03-06 16:31:17 -08:00
|
|
|
let one = hash(&zero);
|
2018-03-06 19:22:30 -08:00
|
|
|
assert!(Entry::new_tick(0, &zero).verify(&zero)); // base case
|
|
|
|
assert!(!Entry::new_tick(0, &zero).verify(&one)); // base case, bad
|
|
|
|
assert!(next_tick(&zero, 1).verify(&zero)); // inductive step
|
|
|
|
assert!(!next_tick(&zero, 1).verify(&one)); // inductive step, bad
|
2018-03-06 16:31:17 -08:00
|
|
|
}
|
|
|
|
|
2018-03-09 16:02:17 -08:00
|
|
|
#[test]
|
|
|
|
fn test_event_reorder_attack() {
|
|
|
|
let zero = Hash::default();
|
|
|
|
|
|
|
|
// First, verify entries
|
|
|
|
let keypair = KeyPair::new();
|
|
|
|
let tr0 = Event::Transaction(Transaction::new(&keypair, keypair.pubkey(), 0, zero));
|
|
|
|
let tr1 = Event::Transaction(Transaction::new(&keypair, keypair.pubkey(), 1, zero));
|
|
|
|
let mut e0 = create_entry(&zero, 0, vec![tr0.clone(), tr1.clone()]);
|
|
|
|
assert!(e0.verify(&zero));
|
|
|
|
|
|
|
|
// Next, swap two events and ensure verification fails.
|
|
|
|
e0.events[0] = tr1; // <-- attack
|
|
|
|
e0.events[1] = tr0;
|
|
|
|
assert!(!e0.verify(&zero));
|
|
|
|
}
|
|
|
|
|
2018-03-06 16:31:17 -08:00
|
|
|
#[test]
|
|
|
|
fn test_next_tick() {
|
2018-03-06 16:36:45 -08:00
|
|
|
let zero = Hash::default();
|
2018-03-06 19:22:30 -08:00
|
|
|
assert_eq!(next_tick(&zero, 1).num_hashes, 1)
|
2018-03-06 16:31:17 -08:00
|
|
|
}
|
|
|
|
}
|