Sha256Hash -> Hash
Because in Loom, there's just the one. Hopefully no worries that it shares a name with std::Hash.
This commit is contained in:
@ -11,15 +11,15 @@ with by verifying each entry's hash can be generated from the hash in the previo
|
|||||||
extern crate silk;
|
extern crate silk;
|
||||||
|
|
||||||
use silk::historian::Historian;
|
use silk::historian::Historian;
|
||||||
use silk::log::{verify_slice, Entry, Sha256Hash};
|
use silk::log::{verify_slice, Entry, Hash};
|
||||||
use silk::event::{generate_keypair, get_pubkey, sign_claim_data, Event};
|
use silk::event::{generate_keypair, get_pubkey, sign_claim_data, Event};
|
||||||
use std::thread::sleep;
|
use std::thread::sleep;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use std::sync::mpsc::SendError;
|
use std::sync::mpsc::SendError;
|
||||||
|
|
||||||
fn create_log(hist: &Historian<Sha256Hash>) -> Result<(), SendError<Event<Sha256Hash>>> {
|
fn create_log(hist: &Historian<Hash>) -> Result<(), SendError<Event<Hash>>> {
|
||||||
sleep(Duration::from_millis(15));
|
sleep(Duration::from_millis(15));
|
||||||
let asset = Sha256Hash::default();
|
let asset = Hash::default();
|
||||||
let keypair = generate_keypair();
|
let keypair = generate_keypair();
|
||||||
let event0 = Event::new_claim(get_pubkey(&keypair), asset, sign_claim_data(&asset, &keypair));
|
let event0 = Event::new_claim(get_pubkey(&keypair), asset, sign_claim_data(&asset, &keypair));
|
||||||
hist.sender.send(event0)?;
|
hist.sender.send(event0)?;
|
||||||
@ -28,11 +28,11 @@ fn create_log(hist: &Historian<Sha256Hash>) -> Result<(), SendError<Event<Sha256
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let seed = Sha256Hash::default();
|
let seed = Hash::default();
|
||||||
let hist = Historian::new(&seed, Some(10));
|
let hist = Historian::new(&seed, Some(10));
|
||||||
create_log(&hist).expect("send error");
|
create_log(&hist).expect("send error");
|
||||||
drop(hist.sender);
|
drop(hist.sender);
|
||||||
let entries: Vec<Entry<Sha256Hash>> = hist.receiver.iter().collect();
|
let entries: Vec<Entry<Hash>> = hist.receiver.iter().collect();
|
||||||
for entry in &entries {
|
for entry in &entries {
|
||||||
println!("{:?}", entry);
|
println!("{:?}", entry);
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
//! event log to record transactions. Its users can deposit funds and
|
//! event log to record transactions. Its users can deposit funds and
|
||||||
//! transfer funds to other users.
|
//! transfer funds to other users.
|
||||||
|
|
||||||
use hash::Sha256Hash;
|
use hash::Hash;
|
||||||
use entry::Entry;
|
use entry::Entry;
|
||||||
use event::Event;
|
use event::Event;
|
||||||
use transaction::Transaction;
|
use transaction::Transaction;
|
||||||
@ -27,8 +27,8 @@ pub type Result<T> = result::Result<T, AccountingError>;
|
|||||||
pub struct Accountant {
|
pub struct Accountant {
|
||||||
pub historian: Historian<i64>,
|
pub historian: Historian<i64>,
|
||||||
pub balances: HashMap<PublicKey, i64>,
|
pub balances: HashMap<PublicKey, i64>,
|
||||||
pub first_id: Sha256Hash,
|
pub first_id: Hash,
|
||||||
pub last_id: Sha256Hash,
|
pub last_id: Hash,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Accountant {
|
impl Accountant {
|
||||||
@ -67,7 +67,7 @@ impl Accountant {
|
|||||||
Self::new_from_entries(gen.create_entries(), ms_per_tick)
|
Self::new_from_entries(gen.create_entries(), ms_per_tick)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn sync(self: &mut Self) -> Sha256Hash {
|
pub fn sync(self: &mut Self) -> Hash {
|
||||||
while let Ok(entry) = self.historian.receiver.try_recv() {
|
while let Ok(entry) = self.historian.receiver.try_recv() {
|
||||||
self.last_id = entry.id;
|
self.last_id = entry.id;
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@ use std::io;
|
|||||||
use accountant::Accountant;
|
use accountant::Accountant;
|
||||||
use transaction::Transaction;
|
use transaction::Transaction;
|
||||||
use signature::PublicKey;
|
use signature::PublicKey;
|
||||||
use hash::Sha256Hash;
|
use hash::Hash;
|
||||||
use entry::Entry;
|
use entry::Entry;
|
||||||
use std::net::UdpSocket;
|
use std::net::UdpSocket;
|
||||||
use bincode::{deserialize, serialize};
|
use bincode::{deserialize, serialize};
|
||||||
@ -15,7 +15,7 @@ pub struct AccountantSkel {
|
|||||||
pub enum Request {
|
pub enum Request {
|
||||||
Transaction(Transaction<i64>),
|
Transaction(Transaction<i64>),
|
||||||
GetBalance { key: PublicKey },
|
GetBalance { key: PublicKey },
|
||||||
GetEntries { last_id: Sha256Hash },
|
GetEntries { last_id: Hash },
|
||||||
GetId { is_last: bool },
|
GetId { is_last: bool },
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -23,7 +23,7 @@ pub enum Request {
|
|||||||
pub enum Response {
|
pub enum Response {
|
||||||
Balance { key: PublicKey, val: Option<i64> },
|
Balance { key: PublicKey, val: Option<i64> },
|
||||||
Entries { entries: Vec<Entry<i64>> },
|
Entries { entries: Vec<Entry<i64>> },
|
||||||
Id { id: Sha256Hash, is_last: bool },
|
Id { id: Hash, is_last: bool },
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AccountantSkel {
|
impl AccountantSkel {
|
||||||
|
@ -7,7 +7,7 @@ use std::io;
|
|||||||
use bincode::{deserialize, serialize};
|
use bincode::{deserialize, serialize};
|
||||||
use transaction::Transaction;
|
use transaction::Transaction;
|
||||||
use signature::{PublicKey, Signature};
|
use signature::{PublicKey, Signature};
|
||||||
use hash::Sha256Hash;
|
use hash::Hash;
|
||||||
use entry::Entry;
|
use entry::Entry;
|
||||||
use ring::signature::Ed25519KeyPair;
|
use ring::signature::Ed25519KeyPair;
|
||||||
use accountant_skel::{Request, Response};
|
use accountant_skel::{Request, Response};
|
||||||
@ -15,7 +15,7 @@ use accountant_skel::{Request, Response};
|
|||||||
pub struct AccountantStub {
|
pub struct AccountantStub {
|
||||||
pub addr: String,
|
pub addr: String,
|
||||||
pub socket: UdpSocket,
|
pub socket: UdpSocket,
|
||||||
pub last_id: Option<Sha256Hash>,
|
pub last_id: Option<Hash>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AccountantStub {
|
impl AccountantStub {
|
||||||
@ -38,7 +38,7 @@ impl AccountantStub {
|
|||||||
n: i64,
|
n: i64,
|
||||||
keypair: &Ed25519KeyPair,
|
keypair: &Ed25519KeyPair,
|
||||||
to: PublicKey,
|
to: PublicKey,
|
||||||
last_id: &Sha256Hash,
|
last_id: &Hash,
|
||||||
) -> io::Result<Signature> {
|
) -> io::Result<Signature> {
|
||||||
let tr = Transaction::new(keypair, to, n, *last_id);
|
let tr = Transaction::new(keypair, to, n, *last_id);
|
||||||
let sig = tr.sig;
|
let sig = tr.sig;
|
||||||
@ -59,7 +59,7 @@ impl AccountantStub {
|
|||||||
Ok(None)
|
Ok(None)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_id(&self, is_last: bool) -> io::Result<Sha256Hash> {
|
fn get_id(&self, is_last: bool) -> io::Result<Hash> {
|
||||||
let req = Request::GetId { is_last };
|
let req = Request::GetId { is_last };
|
||||||
let data = serialize(&req).expect("serialize GetId");
|
let data = serialize(&req).expect("serialize GetId");
|
||||||
self.socket.send_to(&data, &self.addr)?;
|
self.socket.send_to(&data, &self.addr)?;
|
||||||
@ -72,7 +72,7 @@ impl AccountantStub {
|
|||||||
Ok(Default::default())
|
Ok(Default::default())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_last_id(&self) -> io::Result<Sha256Hash> {
|
pub fn get_last_id(&self) -> io::Result<Hash> {
|
||||||
self.get_id(true)
|
self.get_id(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
extern crate silk;
|
extern crate silk;
|
||||||
|
|
||||||
use silk::historian::Historian;
|
use silk::historian::Historian;
|
||||||
use silk::hash::Sha256Hash;
|
use silk::hash::Hash;
|
||||||
use silk::entry::Entry;
|
use silk::entry::Entry;
|
||||||
use silk::log::verify_slice;
|
use silk::log::verify_slice;
|
||||||
use silk::signature::{generate_keypair, get_pubkey};
|
use silk::signature::{generate_keypair, get_pubkey};
|
||||||
@ -11,12 +11,9 @@ use std::thread::sleep;
|
|||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use std::sync::mpsc::SendError;
|
use std::sync::mpsc::SendError;
|
||||||
|
|
||||||
fn create_log(
|
fn create_log(hist: &Historian<Hash>, seed: &Hash) -> Result<(), SendError<Event<Hash>>> {
|
||||||
hist: &Historian<Sha256Hash>,
|
|
||||||
seed: &Sha256Hash,
|
|
||||||
) -> Result<(), SendError<Event<Sha256Hash>>> {
|
|
||||||
sleep(Duration::from_millis(15));
|
sleep(Duration::from_millis(15));
|
||||||
let asset = Sha256Hash::default();
|
let asset = Hash::default();
|
||||||
let keypair = generate_keypair();
|
let keypair = generate_keypair();
|
||||||
let tr = Transaction::new(&keypair, get_pubkey(&keypair), asset, *seed);
|
let tr = Transaction::new(&keypair, get_pubkey(&keypair), asset, *seed);
|
||||||
let event0 = Event::Transaction(tr);
|
let event0 = Event::Transaction(tr);
|
||||||
@ -26,11 +23,11 @@ fn create_log(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let seed = Sha256Hash::default();
|
let seed = Hash::default();
|
||||||
let hist = Historian::new(&seed, Some(10));
|
let hist = Historian::new(&seed, Some(10));
|
||||||
create_log(&hist, &seed).expect("send error");
|
create_log(&hist, &seed).expect("send error");
|
||||||
drop(hist.sender);
|
drop(hist.sender);
|
||||||
let entries: Vec<Entry<Sha256Hash>> = hist.receiver.iter().collect();
|
let entries: Vec<Entry<Hash>> = hist.receiver.iter().collect();
|
||||||
for entry in &entries {
|
for entry in &entries {
|
||||||
println!("{:?}", entry);
|
println!("{:?}", entry);
|
||||||
}
|
}
|
||||||
|
30
src/entry.rs
30
src/entry.rs
@ -1,18 +1,18 @@
|
|||||||
use hash::{extend_and_hash, hash, Sha256Hash};
|
use hash::{extend_and_hash, hash, Hash};
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use event::Event;
|
use event::Event;
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
|
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
|
||||||
pub struct Entry<T> {
|
pub struct Entry<T> {
|
||||||
pub num_hashes: u64,
|
pub num_hashes: u64,
|
||||||
pub id: Sha256Hash,
|
pub id: Hash,
|
||||||
pub event: Event<T>,
|
pub event: Event<T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Serialize> Entry<T> {
|
impl<T: Serialize> Entry<T> {
|
||||||
/// Creates a Entry from the number of hashes 'num_hashes' since the previous event
|
/// Creates a Entry from the number of hashes 'num_hashes' since the previous event
|
||||||
/// and that resulting 'id'.
|
/// and that resulting 'id'.
|
||||||
pub fn new_tick(num_hashes: u64, id: &Sha256Hash) -> Self {
|
pub fn new_tick(num_hashes: u64, id: &Hash) -> Self {
|
||||||
Entry {
|
Entry {
|
||||||
num_hashes,
|
num_hashes,
|
||||||
id: *id,
|
id: *id,
|
||||||
@ -22,7 +22,7 @@ impl<T: Serialize> Entry<T> {
|
|||||||
|
|
||||||
/// Verifies self.id is the result of hashing a 'start_hash' 'self.num_hashes' times.
|
/// 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.
|
/// If the event is not a Tick, then hash that as well.
|
||||||
pub fn verify(&self, start_hash: &Sha256Hash) -> bool {
|
pub fn verify(&self, start_hash: &Hash) -> bool {
|
||||||
if !self.event.verify() {
|
if !self.event.verify() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -33,11 +33,7 @@ impl<T: Serialize> Entry<T> {
|
|||||||
/// Creates the hash 'num_hashes' after start_hash. If the event contains
|
/// 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
|
/// signature, the final hash will be a hash of both the previous ID and
|
||||||
/// the signature.
|
/// the signature.
|
||||||
pub fn next_hash<T: Serialize>(
|
pub fn next_hash<T: Serialize>(start_hash: &Hash, num_hashes: u64, event: &Event<T>) -> Hash {
|
||||||
start_hash: &Sha256Hash,
|
|
||||||
num_hashes: u64,
|
|
||||||
event: &Event<T>,
|
|
||||||
) -> Sha256Hash {
|
|
||||||
let mut id = *start_hash;
|
let mut id = *start_hash;
|
||||||
let sig = event.get_signature();
|
let sig = event.get_signature();
|
||||||
let start_index = if sig.is_some() { 1 } else { 0 };
|
let start_index = if sig.is_some() { 1 } else { 0 };
|
||||||
@ -51,11 +47,7 @@ pub fn next_hash<T: Serialize>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Creates the next Entry 'num_hashes' after 'start_hash'.
|
/// Creates the next Entry 'num_hashes' after 'start_hash'.
|
||||||
pub fn create_entry<T: Serialize>(
|
pub fn create_entry<T: Serialize>(start_hash: &Hash, cur_hashes: u64, event: Event<T>) -> Entry<T> {
|
||||||
start_hash: &Sha256Hash,
|
|
||||||
cur_hashes: u64,
|
|
||||||
event: Event<T>,
|
|
||||||
) -> Entry<T> {
|
|
||||||
let sig = event.get_signature();
|
let sig = event.get_signature();
|
||||||
let num_hashes = cur_hashes + if sig.is_some() { 1 } else { 0 };
|
let num_hashes = cur_hashes + if sig.is_some() { 1 } else { 0 };
|
||||||
let id = next_hash(start_hash, 0, &event);
|
let id = next_hash(start_hash, 0, &event);
|
||||||
@ -68,7 +60,7 @@ pub fn create_entry<T: Serialize>(
|
|||||||
|
|
||||||
/// Creates the next Tick Entry 'num_hashes' after 'start_hash'.
|
/// Creates the next Tick Entry 'num_hashes' after 'start_hash'.
|
||||||
pub fn create_entry_mut<T: Serialize>(
|
pub fn create_entry_mut<T: Serialize>(
|
||||||
start_hash: &mut Sha256Hash,
|
start_hash: &mut Hash,
|
||||||
cur_hashes: &mut u64,
|
cur_hashes: &mut u64,
|
||||||
event: Event<T>,
|
event: Event<T>,
|
||||||
) -> Entry<T> {
|
) -> Entry<T> {
|
||||||
@ -79,7 +71,7 @@ pub fn create_entry_mut<T: Serialize>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Creates the next Tick Entry 'num_hashes' after 'start_hash'.
|
/// Creates the next Tick Entry 'num_hashes' after 'start_hash'.
|
||||||
pub fn next_tick<T: Serialize>(start_hash: &Sha256Hash, num_hashes: u64) -> Entry<T> {
|
pub fn next_tick<T: Serialize>(start_hash: &Hash, num_hashes: u64) -> Entry<T> {
|
||||||
let event = Event::Tick;
|
let event = Event::Tick;
|
||||||
Entry {
|
Entry {
|
||||||
num_hashes,
|
num_hashes,
|
||||||
@ -94,7 +86,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_event_verify() {
|
fn test_event_verify() {
|
||||||
let zero = Sha256Hash::default();
|
let zero = Hash::default();
|
||||||
let one = hash(&zero);
|
let one = hash(&zero);
|
||||||
assert!(Entry::<u8>::new_tick(0, &zero).verify(&zero)); // base case
|
assert!(Entry::<u8>::new_tick(0, &zero).verify(&zero)); // base case
|
||||||
assert!(!Entry::<u8>::new_tick(0, &zero).verify(&one)); // base case, bad
|
assert!(!Entry::<u8>::new_tick(0, &zero).verify(&one)); // base case, bad
|
||||||
@ -104,7 +96,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_next_tick() {
|
fn test_next_tick() {
|
||||||
let zero = Sha256Hash::default();
|
let zero = Hash::default();
|
||||||
assert_eq!(next_tick::<Sha256Hash>(&zero, 1).num_hashes, 1)
|
assert_eq!(next_tick::<Hash>(&zero, 1).num_hashes, 1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@ use transaction::Transaction;
|
|||||||
use signature::{generate_keypair, get_pubkey, PublicKey};
|
use signature::{generate_keypair, get_pubkey, PublicKey};
|
||||||
use entry::Entry;
|
use entry::Entry;
|
||||||
use log::create_entries;
|
use log::create_entries;
|
||||||
use hash::{hash, Sha256Hash};
|
use hash::{hash, Hash};
|
||||||
use ring::rand::SystemRandom;
|
use ring::rand::SystemRandom;
|
||||||
use ring::signature::Ed25519KeyPair;
|
use ring::signature::Ed25519KeyPair;
|
||||||
use untrusted::Input;
|
use untrusted::Input;
|
||||||
@ -44,7 +44,7 @@ impl Genesis {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_seed(&self) -> Sha256Hash {
|
pub fn get_seed(&self) -> Hash {
|
||||||
hash(&self.pkcs8)
|
hash(&self.pkcs8)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,17 +2,17 @@ use generic_array::GenericArray;
|
|||||||
use generic_array::typenum::U32;
|
use generic_array::typenum::U32;
|
||||||
use sha2::{Digest, Sha256};
|
use sha2::{Digest, Sha256};
|
||||||
|
|
||||||
pub type Sha256Hash = GenericArray<u8, U32>;
|
pub type Hash = GenericArray<u8, U32>;
|
||||||
|
|
||||||
/// Return a Sha256 hash for the given data.
|
/// Return a Sha256 hash for the given data.
|
||||||
pub fn hash(val: &[u8]) -> Sha256Hash {
|
pub fn hash(val: &[u8]) -> Hash {
|
||||||
let mut hasher = Sha256::default();
|
let mut hasher = Sha256::default();
|
||||||
hasher.input(val);
|
hasher.input(val);
|
||||||
hasher.result()
|
hasher.result()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return the hash of the given hash extended with the given value.
|
/// Return the hash of the given hash extended with the given value.
|
||||||
pub fn extend_and_hash(id: &Sha256Hash, val: &[u8]) -> Sha256Hash {
|
pub fn extend_and_hash(id: &Hash, val: &[u8]) -> Hash {
|
||||||
let mut hash_data = id.to_vec();
|
let mut hash_data = id.to_vec();
|
||||||
hash_data.extend_from_slice(val);
|
hash_data.extend_from_slice(val);
|
||||||
hash(&hash_data)
|
hash(&hash_data)
|
||||||
|
@ -5,7 +5,7 @@ use std::thread::{spawn, JoinHandle};
|
|||||||
use std::collections::HashSet;
|
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, Hash};
|
||||||
use entry::Entry;
|
use entry::Entry;
|
||||||
use logger::{ExitReason, Logger};
|
use logger::{ExitReason, Logger};
|
||||||
use signature::Signature;
|
use signature::Signature;
|
||||||
@ -21,7 +21,7 @@ pub struct Historian<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<T: 'static + Serialize + Clone + Debug + Send> Historian<T> {
|
impl<T: 'static + Serialize + Clone + Debug + Send> Historian<T> {
|
||||||
pub fn new(start_hash: &Sha256Hash, ms_per_tick: Option<u64>) -> Self {
|
pub fn new(start_hash: &Hash, ms_per_tick: Option<u64>) -> Self {
|
||||||
let (sender, event_receiver) = sync_channel(1000);
|
let (sender, event_receiver) = sync_channel(1000);
|
||||||
let (entry_sender, receiver) = sync_channel(1000);
|
let (entry_sender, receiver) = sync_channel(1000);
|
||||||
let thread_hdl =
|
let thread_hdl =
|
||||||
@ -38,7 +38,7 @@ impl<T: 'static + Serialize + Clone + Debug + Send> Historian<T> {
|
|||||||
/// A background thread that will continue tagging received Event messages and
|
/// A background thread that will continue tagging received Event messages and
|
||||||
/// sending back Entry messages until either the receiver or sender channel is closed.
|
/// sending back Entry messages until either the receiver or sender channel is closed.
|
||||||
fn create_logger(
|
fn create_logger(
|
||||||
start_hash: Sha256Hash,
|
start_hash: Hash,
|
||||||
ms_per_tick: Option<u64>,
|
ms_per_tick: Option<u64>,
|
||||||
receiver: Receiver<Event<T>>,
|
receiver: Receiver<Event<T>>,
|
||||||
sender: SyncSender<Entry<T>>,
|
sender: SyncSender<Entry<T>>,
|
||||||
@ -75,7 +75,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_historian() {
|
fn test_historian() {
|
||||||
let zero = Sha256Hash::default();
|
let zero = Hash::default();
|
||||||
let hist = Historian::new(&zero, None);
|
let hist = Historian::new(&zero, None);
|
||||||
|
|
||||||
hist.sender.send(Event::Tick).unwrap();
|
hist.sender.send(Event::Tick).unwrap();
|
||||||
@ -99,7 +99,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_historian_closed_sender() {
|
fn test_historian_closed_sender() {
|
||||||
let zero = Sha256Hash::default();
|
let zero = Hash::default();
|
||||||
let hist = Historian::<u8>::new(&zero, None);
|
let hist = Historian::<u8>::new(&zero, None);
|
||||||
drop(hist.receiver);
|
drop(hist.receiver);
|
||||||
hist.sender.send(Event::Tick).unwrap();
|
hist.sender.send(Event::Tick).unwrap();
|
||||||
@ -119,12 +119,12 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_ticking_historian() {
|
fn test_ticking_historian() {
|
||||||
let zero = Sha256Hash::default();
|
let zero = Hash::default();
|
||||||
let hist = Historian::new(&zero, Some(20));
|
let hist = Historian::new(&zero, Some(20));
|
||||||
sleep(Duration::from_millis(30));
|
sleep(Duration::from_millis(30));
|
||||||
hist.sender.send(Event::Tick).unwrap();
|
hist.sender.send(Event::Tick).unwrap();
|
||||||
drop(hist.sender);
|
drop(hist.sender);
|
||||||
let entries: Vec<Entry<Sha256Hash>> = hist.receiver.iter().collect();
|
let entries: Vec<Entry<Hash>> = hist.receiver.iter().collect();
|
||||||
|
|
||||||
// Ensure one entry is sent back for each tick sent in.
|
// Ensure one entry is sent back for each tick sent in.
|
||||||
assert_eq!(entries.len(), 1);
|
assert_eq!(entries.len(), 1);
|
||||||
|
23
src/log.rs
23
src/log.rs
@ -13,37 +13,34 @@
|
|||||||
/// 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::Sha256Hash;
|
use hash::Hash;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use entry::{create_entry_mut, next_tick, Entry};
|
use entry::{create_entry_mut, next_tick, Entry};
|
||||||
use event::Event;
|
use event::Event;
|
||||||
use rayon::prelude::*;
|
use rayon::prelude::*;
|
||||||
|
|
||||||
/// 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<Hash>], start_hash: &Hash) -> bool {
|
||||||
let genesis = [Entry::new_tick(Default::default(), start_hash)];
|
let genesis = [Entry::new_tick(Default::default(), start_hash)];
|
||||||
let event_pairs = genesis.par_iter().chain(events).zip(events);
|
let event_pairs = genesis.par_iter().chain(events).zip(events);
|
||||||
event_pairs.all(|(x0, x1)| x1.verify(&x0.id))
|
event_pairs.all(|(x0, x1)| x1.verify(&x0.id))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 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_i64(events: &[Entry<i64>], start_hash: &Sha256Hash) -> bool {
|
pub fn verify_slice_i64(events: &[Entry<i64>], start_hash: &Hash) -> bool {
|
||||||
let genesis = [Entry::new_tick(Default::default(), start_hash)];
|
let genesis = [Entry::new_tick(Default::default(), start_hash)];
|
||||||
let event_pairs = genesis.par_iter().chain(events).zip(events);
|
let event_pairs = genesis.par_iter().chain(events).zip(events);
|
||||||
event_pairs.all(|(x0, x1)| x1.verify(&x0.id))
|
event_pairs.all(|(x0, x1)| x1.verify(&x0.id))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Verifies the hashes and events serially. Exists only for reference.
|
/// Verifies the hashes and events serially. Exists only for reference.
|
||||||
pub fn verify_slice_seq<T: Serialize>(events: &[Entry<T>], start_hash: &Sha256Hash) -> bool {
|
pub fn verify_slice_seq<T: Serialize>(events: &[Entry<T>], start_hash: &Hash) -> bool {
|
||||||
let genesis = [Entry::new_tick(0, start_hash)];
|
let genesis = [Entry::new_tick(0, start_hash)];
|
||||||
let mut event_pairs = genesis.iter().chain(events).zip(events);
|
let mut event_pairs = genesis.iter().chain(events).zip(events);
|
||||||
event_pairs.all(|(x0, x1)| x1.verify(&x0.id))
|
event_pairs.all(|(x0, x1)| x1.verify(&x0.id))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_entries<T: Serialize>(
|
pub fn create_entries<T: Serialize>(start_hash: &Hash, events: Vec<Event<T>>) -> Vec<Entry<T>> {
|
||||||
start_hash: &Sha256Hash,
|
|
||||||
events: Vec<Event<T>>,
|
|
||||||
) -> Vec<Entry<T>> {
|
|
||||||
let mut id = *start_hash;
|
let mut id = *start_hash;
|
||||||
events
|
events
|
||||||
.into_iter()
|
.into_iter()
|
||||||
@ -52,7 +49,7 @@ pub fn create_entries<T: Serialize>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Create a vector of Ticks of length 'len' from 'start_hash' hash and 'num_hashes'.
|
/// Create a vector of Ticks of length 'len' from 'start_hash' hash and 'num_hashes'.
|
||||||
pub fn next_ticks(start_hash: &Sha256Hash, num_hashes: u64, len: usize) -> Vec<Entry<Sha256Hash>> {
|
pub fn next_ticks(start_hash: &Hash, num_hashes: u64, len: usize) -> Vec<Entry<Hash>> {
|
||||||
let mut id = *start_hash;
|
let mut id = *start_hash;
|
||||||
let mut ticks = vec![];
|
let mut ticks = vec![];
|
||||||
for _ in 0..len {
|
for _ in 0..len {
|
||||||
@ -70,8 +67,8 @@ mod tests {
|
|||||||
use transaction::Transaction;
|
use transaction::Transaction;
|
||||||
use hash::hash;
|
use hash::hash;
|
||||||
|
|
||||||
fn verify_slice_generic(verify_slice: fn(&[Entry<Sha256Hash>], &Sha256Hash) -> bool) {
|
fn verify_slice_generic(verify_slice: fn(&[Entry<Hash>], &Hash) -> bool) {
|
||||||
let zero = Sha256Hash::default();
|
let zero = Hash::default();
|
||||||
let one = hash(&zero);
|
let one = hash(&zero);
|
||||||
assert!(verify_slice(&vec![], &zero)); // base case
|
assert!(verify_slice(&vec![], &zero)); // base case
|
||||||
assert!(verify_slice(&vec![Entry::new_tick(0, &zero)], &zero)); // singleton case 1
|
assert!(verify_slice(&vec![Entry::new_tick(0, &zero)], &zero)); // singleton case 1
|
||||||
@ -90,12 +87,12 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_verify_slice_seq() {
|
fn test_verify_slice_seq() {
|
||||||
verify_slice_generic(verify_slice_seq::<Sha256Hash>);
|
verify_slice_generic(verify_slice_seq::<Hash>);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_reorder_attack() {
|
fn test_reorder_attack() {
|
||||||
let zero = Sha256Hash::default();
|
let zero = Hash::default();
|
||||||
let one = hash(&zero);
|
let one = hash(&zero);
|
||||||
|
|
||||||
// First, verify entries
|
// First, verify entries
|
||||||
|
@ -7,7 +7,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::Hash;
|
||||||
use entry::{create_entry_mut, Entry};
|
use entry::{create_entry_mut, Entry};
|
||||||
use event::Event;
|
use event::Event;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
@ -23,7 +23,7 @@ pub enum ExitReason {
|
|||||||
pub struct Logger<T> {
|
pub struct Logger<T> {
|
||||||
pub sender: SyncSender<Entry<T>>,
|
pub sender: SyncSender<Entry<T>>,
|
||||||
pub receiver: Receiver<Event<T>>,
|
pub receiver: Receiver<Event<T>>,
|
||||||
pub last_id: Sha256Hash,
|
pub last_id: Hash,
|
||||||
pub num_hashes: u64,
|
pub num_hashes: u64,
|
||||||
pub num_ticks: u64,
|
pub num_ticks: u64,
|
||||||
}
|
}
|
||||||
@ -32,7 +32,7 @@ impl<T: Serialize + Clone + Debug> Logger<T> {
|
|||||||
pub fn new(
|
pub fn new(
|
||||||
receiver: Receiver<Event<T>>,
|
receiver: Receiver<Event<T>>,
|
||||||
sender: SyncSender<Entry<T>>,
|
sender: SyncSender<Entry<T>>,
|
||||||
start_hash: Sha256Hash,
|
start_hash: Hash,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Logger {
|
Logger {
|
||||||
receiver,
|
receiver,
|
||||||
|
@ -4,24 +4,19 @@ use signature::{get_pubkey, verify_signature, PublicKey, Signature};
|
|||||||
use ring::signature::Ed25519KeyPair;
|
use ring::signature::Ed25519KeyPair;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use bincode::serialize;
|
use bincode::serialize;
|
||||||
use hash::Sha256Hash;
|
use hash::Hash;
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
|
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
|
||||||
pub struct Transaction<T> {
|
pub struct Transaction<T> {
|
||||||
pub from: PublicKey,
|
pub from: PublicKey,
|
||||||
pub to: PublicKey,
|
pub to: PublicKey,
|
||||||
pub asset: T,
|
pub asset: T,
|
||||||
pub last_id: Sha256Hash,
|
pub last_id: Hash,
|
||||||
pub sig: Signature,
|
pub sig: Signature,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Serialize> Transaction<T> {
|
impl<T: Serialize> Transaction<T> {
|
||||||
pub fn new(
|
pub fn new(from_keypair: &Ed25519KeyPair, to: PublicKey, asset: T, last_id: Hash) -> Self {
|
||||||
from_keypair: &Ed25519KeyPair,
|
|
||||||
to: PublicKey,
|
|
||||||
asset: T,
|
|
||||||
last_id: Sha256Hash,
|
|
||||||
) -> Self {
|
|
||||||
let mut tr = Transaction {
|
let mut tr = Transaction {
|
||||||
from: get_pubkey(&from_keypair),
|
from: get_pubkey(&from_keypair),
|
||||||
to,
|
to,
|
||||||
@ -58,14 +53,14 @@ mod tests {
|
|||||||
fn test_claim() {
|
fn test_claim() {
|
||||||
let keypair = generate_keypair();
|
let keypair = generate_keypair();
|
||||||
let asset = hash(b"hello, world");
|
let asset = hash(b"hello, world");
|
||||||
let zero = Sha256Hash::default();
|
let zero = Hash::default();
|
||||||
let tr0 = Transaction::new(&keypair, get_pubkey(&keypair), asset, zero);
|
let tr0 = Transaction::new(&keypair, get_pubkey(&keypair), asset, zero);
|
||||||
assert!(tr0.verify());
|
assert!(tr0.verify());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_transfer() {
|
fn test_transfer() {
|
||||||
let zero = Sha256Hash::default();
|
let zero = Hash::default();
|
||||||
let keypair0 = generate_keypair();
|
let keypair0 = generate_keypair();
|
||||||
let keypair1 = generate_keypair();
|
let keypair1 = generate_keypair();
|
||||||
let pubkey1 = get_pubkey(&keypair1);
|
let pubkey1 = get_pubkey(&keypair1);
|
||||||
@ -90,7 +85,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_bad_event_signature() {
|
fn test_bad_event_signature() {
|
||||||
let zero = Sha256Hash::default();
|
let zero = Hash::default();
|
||||||
let keypair = generate_keypair();
|
let keypair = generate_keypair();
|
||||||
let pubkey = get_pubkey(&keypair);
|
let pubkey = get_pubkey(&keypair);
|
||||||
let mut tr = Transaction::new(&keypair, pubkey, hash(b"hello, world"), zero);
|
let mut tr = Transaction::new(&keypair, pubkey, hash(b"hello, world"), zero);
|
||||||
@ -105,7 +100,7 @@ mod tests {
|
|||||||
let keypair1 = generate_keypair();
|
let keypair1 = generate_keypair();
|
||||||
let thief_keypair = generate_keypair();
|
let thief_keypair = generate_keypair();
|
||||||
let pubkey1 = get_pubkey(&keypair1);
|
let pubkey1 = get_pubkey(&keypair1);
|
||||||
let zero = Sha256Hash::default();
|
let zero = Hash::default();
|
||||||
let mut tr = Transaction::new(&keypair0, pubkey1, hash(b"hello, world"), zero);
|
let mut tr = Transaction::new(&keypair0, pubkey1, hash(b"hello, world"), zero);
|
||||||
tr.sign(&keypair0);
|
tr.sign(&keypair0);
|
||||||
tr.to = get_pubkey(&thief_keypair); // <-- attack!
|
tr.to = get_pubkey(&thief_keypair); // <-- attack!
|
||||||
|
Reference in New Issue
Block a user