2018-03-06 16:36:45 -08:00
|
|
|
use hash::{extend_and_hash, hash, Hash};
|
2018-03-06 16:31:17 -08:00
|
|
|
use event::Event;
|
|
|
|
|
|
|
|
#[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-06 19:22:30 -08:00
|
|
|
pub event: Event,
|
2018-03-06 16:31:17 -08:00
|
|
|
}
|
|
|
|
|
2018-03-06 19:22:30 -08:00
|
|
|
impl Entry {
|
2018-03-06 16:31:17 -08: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,
|
|
|
|
event: Event::Tick,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Verifies self.id is the result of hashing a 'start_hash' 'self.num_hashes' times.
|
|
|
|
/// 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-06 16:31:17 -08:00
|
|
|
if !self.event.verify() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
self.id == next_hash(start_hash, self.num_hashes, &self.event)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates the hash 'num_hashes' after start_hash. If the event contains
|
|
|
|
/// signature, the final hash will be a hash of both the previous ID and
|
|
|
|
/// the signature.
|
2018-03-06 19:22:30 -08:00
|
|
|
pub fn next_hash(start_hash: &Hash, num_hashes: u64, event: &Event) -> Hash {
|
2018-03-06 16:31:17 -08:00
|
|
|
let mut id = *start_hash;
|
|
|
|
let sig = event.get_signature();
|
|
|
|
let start_index = if sig.is_some() { 1 } else { 0 };
|
|
|
|
for _ in start_index..num_hashes {
|
|
|
|
id = hash(&id);
|
|
|
|
}
|
|
|
|
if let Some(sig) = sig {
|
|
|
|
id = extend_and_hash(&id, &sig);
|
|
|
|
}
|
|
|
|
id
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates the next Entry 'num_hashes' after 'start_hash'.
|
2018-03-06 19:22:30 -08:00
|
|
|
pub fn create_entry(start_hash: &Hash, cur_hashes: u64, event: Event) -> Entry {
|
2018-03-06 16:31:17 -08:00
|
|
|
let sig = event.get_signature();
|
|
|
|
let num_hashes = cur_hashes + if sig.is_some() { 1 } else { 0 };
|
|
|
|
let id = next_hash(start_hash, 0, &event);
|
|
|
|
Entry {
|
|
|
|
num_hashes,
|
|
|
|
id,
|
|
|
|
event,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates the next Tick Entry 'num_hashes' after 'start_hash'.
|
2018-03-06 19:22:30 -08:00
|
|
|
pub fn create_entry_mut(start_hash: &mut Hash, cur_hashes: &mut u64, event: Event) -> Entry {
|
2018-03-06 16:31:17 -08:00
|
|
|
let entry = create_entry(start_hash, *cur_hashes, event);
|
|
|
|
*start_hash = entry.id;
|
|
|
|
*cur_hashes = 0;
|
|
|
|
entry
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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
|
|
|
let event = Event::Tick;
|
|
|
|
Entry {
|
|
|
|
num_hashes,
|
|
|
|
id: next_hash(start_hash, num_hashes, &event),
|
|
|
|
event,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_event_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
|
|
|
}
|
|
|
|
|
|
|
|
#[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
|
|
|
}
|
|
|
|
}
|