Move entry into its own module
Hmm, Logger doesn't depend on log.
This commit is contained in:
parent
5a44c36b1f
commit
1436bb1ff2
|
@ -3,7 +3,7 @@
|
||||||
//! transfer funds to other users.
|
//! transfer funds to other users.
|
||||||
|
|
||||||
use hash::Sha256Hash;
|
use hash::Sha256Hash;
|
||||||
use log::Entry;
|
use entry::Entry;
|
||||||
use event::Event;
|
use event::Event;
|
||||||
use transaction::Transaction;
|
use transaction::Transaction;
|
||||||
use signature::{PublicKey, Signature};
|
use signature::{PublicKey, Signature};
|
||||||
|
|
|
@ -3,7 +3,7 @@ use accountant::Accountant;
|
||||||
use transaction::Transaction;
|
use transaction::Transaction;
|
||||||
use signature::PublicKey;
|
use signature::PublicKey;
|
||||||
use hash::Sha256Hash;
|
use hash::Sha256Hash;
|
||||||
use log::Entry;
|
use entry::Entry;
|
||||||
use std::net::UdpSocket;
|
use std::net::UdpSocket;
|
||||||
use bincode::{deserialize, serialize};
|
use bincode::{deserialize, serialize};
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,7 @@ use bincode::{deserialize, serialize};
|
||||||
use transaction::Transaction;
|
use transaction::Transaction;
|
||||||
use signature::{PublicKey, Signature};
|
use signature::{PublicKey, Signature};
|
||||||
use hash::Sha256Hash;
|
use hash::Sha256Hash;
|
||||||
use log::Entry;
|
use entry::Entry;
|
||||||
use ring::signature::Ed25519KeyPair;
|
use ring::signature::Ed25519KeyPair;
|
||||||
use accountant_skel::{Request, Response};
|
use accountant_skel::{Request, Response};
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,8 @@ extern crate silk;
|
||||||
|
|
||||||
use silk::historian::Historian;
|
use silk::historian::Historian;
|
||||||
use silk::hash::Sha256Hash;
|
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::signature::{generate_keypair, get_pubkey};
|
||||||
use silk::transaction::Transaction;
|
use silk::transaction::Transaction;
|
||||||
use silk::event::Event;
|
use silk::event::Event;
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,7 +3,8 @@
|
||||||
use event::Event;
|
use event::Event;
|
||||||
use transaction::Transaction;
|
use transaction::Transaction;
|
||||||
use signature::{generate_keypair, get_pubkey, PublicKey};
|
use signature::{generate_keypair, get_pubkey, PublicKey};
|
||||||
use log::{create_entries, Entry};
|
use entry::Entry;
|
||||||
|
use log::create_entries;
|
||||||
use hash::{hash, Sha256Hash};
|
use hash::{hash, Sha256Hash};
|
||||||
use ring::rand::SystemRandom;
|
use ring::rand::SystemRandom;
|
||||||
use ring::signature::Ed25519KeyPair;
|
use ring::signature::Ed25519KeyPair;
|
||||||
|
|
|
@ -6,7 +6,7 @@ use std::collections::HashSet;
|
||||||
use std::sync::mpsc::{sync_channel, Receiver, SyncSender};
|
use std::sync::mpsc::{sync_channel, Receiver, SyncSender};
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
use hash::{hash, Sha256Hash};
|
use hash::{hash, Sha256Hash};
|
||||||
use log::Entry;
|
use entry::Entry;
|
||||||
use logger::{ExitReason, Logger};
|
use logger::{ExitReason, Logger};
|
||||||
use signature::Signature;
|
use signature::Signature;
|
||||||
use event::Event;
|
use event::Event;
|
||||||
|
|
11
src/lib.rs
11
src/lib.rs
|
@ -1,11 +1,12 @@
|
||||||
#![cfg_attr(feature = "unstable", feature(test))]
|
#![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 signature;
|
||||||
|
pub mod hash;
|
||||||
|
pub mod transaction;
|
||||||
|
pub mod event;
|
||||||
|
pub mod entry;
|
||||||
|
pub mod log;
|
||||||
pub mod genesis;
|
pub mod genesis;
|
||||||
|
pub mod logger;
|
||||||
pub mod historian;
|
pub mod historian;
|
||||||
pub mod accountant;
|
pub mod accountant;
|
||||||
pub mod accountant_skel;
|
pub mod accountant_skel;
|
||||||
|
|
106
src/log.rs
106
src/log.rs
|
@ -13,97 +13,12 @@
|
||||||
/// fastest processor. Duration should therefore be estimated by assuming that the hash
|
/// 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.
|
/// 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 serde::Serialize;
|
||||||
|
use entry::{create_entry_mut, next_tick, Entry};
|
||||||
use event::Event;
|
use event::Event;
|
||||||
use rayon::prelude::*;
|
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.
|
/// Verifies the hashes and counts of a slice of events are all consistent.
|
||||||
pub fn verify_slice(events: &[Entry<Sha256Hash>], start_hash: &Sha256Hash) -> bool {
|
pub fn verify_slice(events: &[Entry<Sha256Hash>], start_hash: &Sha256Hash) -> bool {
|
||||||
let genesis = [Entry::new_tick(Default::default(), start_hash)];
|
let genesis = [Entry::new_tick(Default::default(), start_hash)];
|
||||||
|
@ -153,22 +68,7 @@ mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use signature::{generate_keypair, get_pubkey};
|
use signature::{generate_keypair, get_pubkey};
|
||||||
use transaction::Transaction;
|
use transaction::Transaction;
|
||||||
|
use hash::hash;
|
||||||
#[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)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn verify_slice_generic(verify_slice: fn(&[Entry<Sha256Hash>], &Sha256Hash) -> bool) {
|
fn verify_slice_generic(verify_slice: fn(&[Entry<Sha256Hash>], &Sha256Hash) -> bool) {
|
||||||
let zero = Sha256Hash::default();
|
let zero = Sha256Hash::default();
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
use std::sync::mpsc::{Receiver, SyncSender, TryRecvError};
|
use std::sync::mpsc::{Receiver, SyncSender, TryRecvError};
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
use hash::Sha256Hash;
|
use hash::Sha256Hash;
|
||||||
use log::{create_entry_mut, Entry};
|
use entry::{create_entry_mut, Entry};
|
||||||
use event::Event;
|
use event::Event;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
|
|
Loading…
Reference in New Issue