Move entry into its own module

Hmm, Logger doesn't depend on log.
This commit is contained in:
Greg Fitzgerald 2018-03-06 17:31:17 -07:00
parent 5a44c36b1f
commit 1436bb1ff2
10 changed files with 128 additions and 115 deletions

View File

@ -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};

View File

@ -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};

View File

@ -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};

View File

@ -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;

110
src/entry.rs Normal file
View File

@ -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<T> {
pub num_hashes: u64,
pub id: Sha256Hash,
pub event: Event<T>,
}
impl<T: Serialize> Entry<T> {
/// 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<T: Serialize>(
start_hash: &Sha256Hash,
num_hashes: u64,
event: &Event<T>,
) -> 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<T: Serialize>(
start_hash: &Sha256Hash,
cur_hashes: u64,
event: Event<T>,
) -> Entry<T> {
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<T: Serialize>(
start_hash: &mut Sha256Hash,
cur_hashes: &mut u64,
event: Event<T>,
) -> Entry<T> {
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<T: Serialize>(start_hash: &Sha256Hash, num_hashes: u64) -> Entry<T> {
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::<u8>::new_tick(0, &zero).verify(&zero)); // base case
assert!(!Entry::<u8>::new_tick(0, &zero).verify(&one)); // base case, bad
assert!(next_tick::<u8>(&zero, 1).verify(&zero)); // inductive step
assert!(!next_tick::<u8>(&zero, 1).verify(&one)); // inductive step, bad
}
#[test]
fn test_next_tick() {
let zero = Sha256Hash::default();
assert_eq!(next_tick::<Sha256Hash>(&zero, 1).num_hashes, 1)
}
}

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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<T> {
pub num_hashes: u64,
pub id: Sha256Hash,
pub event: Event<T>,
}
impl<T: Serialize> Entry<T> {
/// 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<T: Serialize>(
start_hash: &Sha256Hash,
num_hashes: u64,
event: &Event<T>,
) -> 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<T: Serialize>(
start_hash: &Sha256Hash,
cur_hashes: u64,
event: Event<T>,
) -> Entry<T> {
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<T: Serialize>(
start_hash: &mut Sha256Hash,
cur_hashes: &mut u64,
event: Event<T>,
) -> Entry<T> {
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<T: Serialize>(start_hash: &Sha256Hash, num_hashes: u64) -> Entry<T> {
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<Sha256Hash>], 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::<u8>::new_tick(0, &zero).verify(&zero)); // base case
assert!(!Entry::<u8>::new_tick(0, &zero).verify(&one)); // base case, bad
assert!(next_tick::<u8>(&zero, 1).verify(&zero)); // inductive step
assert!(!next_tick::<u8>(&zero, 1).verify(&one)); // inductive step, bad
}
#[test]
fn test_next_tick() {
let zero = Sha256Hash::default();
assert_eq!(next_tick::<Sha256Hash>(&zero, 1).num_hashes, 1)
}
use hash::hash;
fn verify_slice_generic(verify_slice: fn(&[Entry<Sha256Hash>], &Sha256Hash) -> bool) {
let zero = Sha256Hash::default();

View File

@ -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;