diff --git a/src/accountant.rs b/src/accountant.rs index 61838382c..0bc97f5a6 100644 --- a/src/accountant.rs +++ b/src/accountant.rs @@ -3,7 +3,7 @@ //! transfer funds to other users. use hash::Sha256Hash; -use log::Entry; +use entry::Entry; use event::Event; use transaction::Transaction; use signature::{PublicKey, Signature}; diff --git a/src/accountant_skel.rs b/src/accountant_skel.rs index 9296b6a17..9cd25dad0 100644 --- a/src/accountant_skel.rs +++ b/src/accountant_skel.rs @@ -3,7 +3,7 @@ use accountant::Accountant; use transaction::Transaction; use signature::PublicKey; use hash::Sha256Hash; -use log::Entry; +use entry::Entry; use std::net::UdpSocket; use bincode::{deserialize, serialize}; diff --git a/src/accountant_stub.rs b/src/accountant_stub.rs index 1bded971e..61529642a 100644 --- a/src/accountant_stub.rs +++ b/src/accountant_stub.rs @@ -8,7 +8,7 @@ use bincode::{deserialize, serialize}; use transaction::Transaction; use signature::{PublicKey, Signature}; use hash::Sha256Hash; -use log::Entry; +use entry::Entry; use ring::signature::Ed25519KeyPair; use accountant_skel::{Request, Response}; diff --git a/src/bin/demo.rs b/src/bin/demo.rs index 76c27dc30..75614d56a 100644 --- a/src/bin/demo.rs +++ b/src/bin/demo.rs @@ -2,7 +2,8 @@ extern crate silk; use silk::historian::Historian; use silk::hash::Sha256Hash; -use silk::log::{verify_slice, Entry}; +use silk::entry::Entry; +use silk::log::verify_slice; use silk::signature::{generate_keypair, get_pubkey}; use silk::transaction::Transaction; use silk::event::Event; diff --git a/src/entry.rs b/src/entry.rs new file mode 100644 index 000000000..f970bc0ca --- /dev/null +++ b/src/entry.rs @@ -0,0 +1,110 @@ +use hash::{extend_and_hash, hash, Sha256Hash}; +use serde::Serialize; +use event::Event; + +#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)] +pub struct Entry { + pub num_hashes: u64, + pub id: Sha256Hash, + pub event: Event, +} + +impl Entry { + /// Creates a Entry from the number of hashes 'num_hashes' since the previous event + /// and that resulting 'id'. + pub fn new_tick(num_hashes: u64, id: &Sha256Hash) -> Self { + 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. + pub fn verify(&self, start_hash: &Sha256Hash) -> bool { + 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. +pub fn next_hash( + start_hash: &Sha256Hash, + num_hashes: u64, + event: &Event, +) -> Sha256Hash { + 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'. +pub fn create_entry( + start_hash: &Sha256Hash, + cur_hashes: u64, + event: Event, +) -> Entry { + 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'. +pub fn create_entry_mut( + start_hash: &mut Sha256Hash, + cur_hashes: &mut u64, + event: Event, +) -> Entry { + 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'. +pub fn next_tick(start_hash: &Sha256Hash, num_hashes: u64) -> Entry { + 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() { + let zero = Sha256Hash::default(); + let one = hash(&zero); + 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 + } + + #[test] + fn test_next_tick() { + let zero = Sha256Hash::default(); + assert_eq!(next_tick::(&zero, 1).num_hashes, 1) + } +} diff --git a/src/genesis.rs b/src/genesis.rs index 6ef4151ad..ef9df9772 100644 --- a/src/genesis.rs +++ b/src/genesis.rs @@ -3,7 +3,8 @@ use event::Event; use transaction::Transaction; use signature::{generate_keypair, get_pubkey, PublicKey}; -use log::{create_entries, Entry}; +use entry::Entry; +use log::create_entries; use hash::{hash, Sha256Hash}; use ring::rand::SystemRandom; use ring::signature::Ed25519KeyPair; diff --git a/src/historian.rs b/src/historian.rs index f0683c8f7..7bf21d9c3 100644 --- a/src/historian.rs +++ b/src/historian.rs @@ -6,7 +6,7 @@ use std::collections::HashSet; use std::sync::mpsc::{sync_channel, Receiver, SyncSender}; use std::time::Instant; use hash::{hash, Sha256Hash}; -use log::Entry; +use entry::Entry; use logger::{ExitReason, Logger}; use signature::Signature; use event::Event; diff --git a/src/lib.rs b/src/lib.rs index 4551c6833..e452859c9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,11 +1,12 @@ #![cfg_attr(feature = "unstable", feature(test))] -pub mod hash; -pub mod log; -pub mod logger; -pub mod event; -pub mod transaction; pub mod signature; +pub mod hash; +pub mod transaction; +pub mod event; +pub mod entry; +pub mod log; pub mod genesis; +pub mod logger; pub mod historian; pub mod accountant; pub mod accountant_skel; diff --git a/src/log.rs b/src/log.rs index 6a13f3abe..6d319c1e1 100644 --- a/src/log.rs +++ b/src/log.rs @@ -13,97 +13,12 @@ /// 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. -use hash::{extend_and_hash, hash, Sha256Hash}; +use hash::Sha256Hash; use serde::Serialize; +use entry::{create_entry_mut, next_tick, Entry}; use event::Event; use rayon::prelude::*; -#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)] -pub struct Entry { - pub num_hashes: u64, - pub id: Sha256Hash, - pub event: Event, -} - -impl Entry { - /// Creates a Entry from the number of hashes 'num_hashes' since the previous event - /// and that resulting 'id'. - pub fn new_tick(num_hashes: u64, id: &Sha256Hash) -> Self { - 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. - pub fn verify(&self, start_hash: &Sha256Hash) -> bool { - 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. -pub fn next_hash( - start_hash: &Sha256Hash, - num_hashes: u64, - event: &Event, -) -> Sha256Hash { - 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'. -pub fn create_entry( - start_hash: &Sha256Hash, - cur_hashes: u64, - event: Event, -) -> Entry { - 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'. -pub fn create_entry_mut( - start_hash: &mut Sha256Hash, - cur_hashes: &mut u64, - event: Event, -) -> Entry { - 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'. -pub fn next_tick(start_hash: &Sha256Hash, num_hashes: u64) -> Entry { - let event = Event::Tick; - Entry { - num_hashes, - id: next_hash(start_hash, num_hashes, &event), - event, - } -} - /// Verifies the hashes and counts of a slice of events are all consistent. pub fn verify_slice(events: &[Entry], start_hash: &Sha256Hash) -> bool { let genesis = [Entry::new_tick(Default::default(), start_hash)]; @@ -153,22 +68,7 @@ mod tests { use super::*; use signature::{generate_keypair, get_pubkey}; use transaction::Transaction; - - #[test] - fn test_event_verify() { - let zero = Sha256Hash::default(); - let one = hash(&zero); - 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 - } - - #[test] - fn test_next_tick() { - let zero = Sha256Hash::default(); - assert_eq!(next_tick::(&zero, 1).num_hashes, 1) - } + use hash::hash; fn verify_slice_generic(verify_slice: fn(&[Entry], &Sha256Hash) -> bool) { let zero = Sha256Hash::default(); diff --git a/src/logger.rs b/src/logger.rs index 8f47bc97c..50fc1feea 100644 --- a/src/logger.rs +++ b/src/logger.rs @@ -8,7 +8,7 @@ use std::sync::mpsc::{Receiver, SyncSender, TryRecvError}; use std::time::{Duration, Instant}; use hash::Sha256Hash; -use log::{create_entry_mut, Entry}; +use entry::{create_entry_mut, Entry}; use event::Event; use serde::Serialize; use std::fmt::Debug;