Allow events to hold any kind of data
This commit is contained in:
@ -6,7 +6,7 @@ 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) -> Result<(), SendError<Event>> {
|
fn create_log(hist: &Historian) -> Result<(), SendError<Event<Sha256Hash>>> {
|
||||||
sleep(Duration::from_millis(15));
|
sleep(Duration::from_millis(15));
|
||||||
let data = Sha256Hash::default();
|
let data = Sha256Hash::default();
|
||||||
hist.sender.send(Event::Discovery { data })?;
|
hist.sender.send(Event::Discovery { data })?;
|
||||||
|
@ -11,7 +11,7 @@ use std::time::{Duration, SystemTime};
|
|||||||
use log::{hash, hash_event, Entry, Event, Sha256Hash};
|
use log::{hash, hash_event, Entry, Event, Sha256Hash};
|
||||||
|
|
||||||
pub struct Historian {
|
pub struct Historian {
|
||||||
pub sender: Sender<Event>,
|
pub sender: Sender<Event<Sha256Hash>>,
|
||||||
pub receiver: Receiver<Entry>,
|
pub receiver: Receiver<Entry>,
|
||||||
pub thread_hdl: JoinHandle<(Entry, ExitReason)>,
|
pub thread_hdl: JoinHandle<(Entry, ExitReason)>,
|
||||||
}
|
}
|
||||||
@ -25,7 +25,7 @@ fn log_event(
|
|||||||
sender: &Sender<Entry>,
|
sender: &Sender<Entry>,
|
||||||
num_hashes: &mut u64,
|
num_hashes: &mut u64,
|
||||||
end_hash: &mut Sha256Hash,
|
end_hash: &mut Sha256Hash,
|
||||||
event: Event,
|
event: Event<Sha256Hash>,
|
||||||
) -> Result<(), (Entry, ExitReason)> {
|
) -> Result<(), (Entry, ExitReason)> {
|
||||||
*end_hash = hash_event(end_hash, &event);
|
*end_hash = hash_event(end_hash, &event);
|
||||||
let entry = Entry {
|
let entry = Entry {
|
||||||
@ -41,7 +41,7 @@ fn log_event(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn log_events(
|
fn log_events(
|
||||||
receiver: &Receiver<Event>,
|
receiver: &Receiver<Event<Sha256Hash>>,
|
||||||
sender: &Sender<Entry>,
|
sender: &Sender<Entry>,
|
||||||
num_hashes: &mut u64,
|
num_hashes: &mut u64,
|
||||||
end_hash: &mut Sha256Hash,
|
end_hash: &mut Sha256Hash,
|
||||||
@ -82,7 +82,7 @@ fn log_events(
|
|||||||
pub fn create_logger(
|
pub fn create_logger(
|
||||||
start_hash: Sha256Hash,
|
start_hash: Sha256Hash,
|
||||||
ms_per_tick: Option<u64>,
|
ms_per_tick: Option<u64>,
|
||||||
receiver: Receiver<Event>,
|
receiver: Receiver<Event<Sha256Hash>>,
|
||||||
sender: Sender<Entry>,
|
sender: Sender<Entry>,
|
||||||
) -> JoinHandle<(Entry, ExitReason)> {
|
) -> JoinHandle<(Entry, ExitReason)> {
|
||||||
use std::thread;
|
use std::thread;
|
||||||
|
40
src/log.rs
40
src/log.rs
@ -24,7 +24,7 @@ pub type Signature = GenericArray<u8, U64>;
|
|||||||
pub struct Entry {
|
pub struct Entry {
|
||||||
pub num_hashes: u64,
|
pub num_hashes: u64,
|
||||||
pub end_hash: Sha256Hash,
|
pub end_hash: Sha256Hash,
|
||||||
pub event: Event,
|
pub event: Event<Sha256Hash>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// When 'event' is Tick, the event represents a simple clock tick, and exists for the
|
/// When 'event' is Tick, the event represents a simple clock tick, and exists for the
|
||||||
@ -33,20 +33,20 @@ pub struct Entry {
|
|||||||
/// a hash alongside the tick, each tick and be verified in parallel using the 'end_hash'
|
/// a hash alongside the tick, each tick and be verified in parallel using the 'end_hash'
|
||||||
/// of the preceding tick to seed its hashing.
|
/// of the preceding tick to seed its hashing.
|
||||||
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
|
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
|
||||||
pub enum Event {
|
pub enum Event<T> {
|
||||||
Tick,
|
Tick,
|
||||||
Discovery {
|
Discovery {
|
||||||
data: Sha256Hash,
|
data: T,
|
||||||
},
|
},
|
||||||
Claim {
|
Claim {
|
||||||
key: PublicKey,
|
key: PublicKey,
|
||||||
data: Sha256Hash,
|
data: T,
|
||||||
sig: Signature,
|
sig: Signature,
|
||||||
},
|
},
|
||||||
Transaction {
|
Transaction {
|
||||||
from: PublicKey,
|
from: PublicKey,
|
||||||
to: PublicKey,
|
to: PublicKey,
|
||||||
data: Sha256Hash,
|
data: T,
|
||||||
sig: Signature,
|
sig: Signature,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -73,7 +73,7 @@ pub fn generate_keypair() -> Ed25519KeyPair {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Return a Claim Event for the given hash and key-pair.
|
/// Return a Claim Event for the given hash and key-pair.
|
||||||
pub fn sign_hash(data: &Sha256Hash, keypair: &Ed25519KeyPair) -> Event {
|
pub fn sign_hash(data: &Sha256Hash, keypair: &Ed25519KeyPair) -> Event<Sha256Hash> {
|
||||||
let sig = keypair.sign(data);
|
let sig = keypair.sign(data);
|
||||||
let peer_public_key_bytes = keypair.public_key_bytes();
|
let peer_public_key_bytes = keypair.public_key_bytes();
|
||||||
let sig_bytes = sig.as_ref();
|
let sig_bytes = sig.as_ref();
|
||||||
@ -85,7 +85,11 @@ pub fn sign_hash(data: &Sha256Hash, keypair: &Ed25519KeyPair) -> Event {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Return a Transaction Event that indicates a transfer in ownership of the given hash.
|
/// Return a Transaction Event that indicates a transfer in ownership of the given hash.
|
||||||
pub fn transfer_hash(data: &Sha256Hash, keypair: &Ed25519KeyPair, to: PublicKey) -> Event {
|
pub fn transfer_hash(
|
||||||
|
data: &Sha256Hash,
|
||||||
|
keypair: &Ed25519KeyPair,
|
||||||
|
to: PublicKey,
|
||||||
|
) -> Event<Sha256Hash> {
|
||||||
let from_public_key_bytes = keypair.public_key_bytes();
|
let from_public_key_bytes = keypair.public_key_bytes();
|
||||||
let mut sign_data = data.to_vec();
|
let mut sign_data = data.to_vec();
|
||||||
sign_data.extend_from_slice(&to);
|
sign_data.extend_from_slice(&to);
|
||||||
@ -115,7 +119,7 @@ pub fn extend_and_hash(end_hash: &Sha256Hash, ty: u8, val: &[u8]) -> Sha256Hash
|
|||||||
hash(&hash_data)
|
hash(&hash_data)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn hash_event(end_hash: &Sha256Hash, event: &Event) -> Sha256Hash {
|
pub fn hash_event(end_hash: &Sha256Hash, event: &Event<Sha256Hash>) -> Sha256Hash {
|
||||||
match *event {
|
match *event {
|
||||||
Event::Tick => *end_hash,
|
Event::Tick => *end_hash,
|
||||||
Event::Discovery { data } => extend_and_hash(end_hash, 1, &data),
|
Event::Discovery { data } => extend_and_hash(end_hash, 1, &data),
|
||||||
@ -140,7 +144,11 @@ pub fn hash_event(end_hash: &Sha256Hash, event: &Event) -> Sha256Hash {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn next_hash(start_hash: &Sha256Hash, num_hashes: u64, event: &Event) -> Sha256Hash {
|
pub fn next_hash(
|
||||||
|
start_hash: &Sha256Hash,
|
||||||
|
num_hashes: u64,
|
||||||
|
event: &Event<Sha256Hash>,
|
||||||
|
) -> Sha256Hash {
|
||||||
let mut end_hash = *start_hash;
|
let mut end_hash = *start_hash;
|
||||||
for _ in 0..num_hashes {
|
for _ in 0..num_hashes {
|
||||||
end_hash = hash(&end_hash);
|
end_hash = hash(&end_hash);
|
||||||
@ -149,7 +157,7 @@ pub fn next_hash(start_hash: &Sha256Hash, num_hashes: u64, event: &Event) -> Sha
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Creates the next Tick Entry 'num_hashes' after 'start_hash'.
|
/// Creates the next Tick Entry 'num_hashes' after 'start_hash'.
|
||||||
pub fn next_entry(start_hash: &Sha256Hash, num_hashes: u64, event: Event) -> Entry {
|
pub fn next_entry(start_hash: &Sha256Hash, num_hashes: u64, event: Event<Sha256Hash>) -> Entry {
|
||||||
Entry {
|
Entry {
|
||||||
num_hashes,
|
num_hashes,
|
||||||
end_hash: next_hash(start_hash, num_hashes, &event),
|
end_hash: next_hash(start_hash, num_hashes, &event),
|
||||||
@ -157,7 +165,11 @@ pub fn next_entry(start_hash: &Sha256Hash, num_hashes: u64, event: Event) -> Ent
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn next_entry_mut(start_hash: &mut Sha256Hash, num_hashes: u64, event: Event) -> Entry {
|
pub fn next_entry_mut(
|
||||||
|
start_hash: &mut Sha256Hash,
|
||||||
|
num_hashes: u64,
|
||||||
|
event: Event<Sha256Hash>,
|
||||||
|
) -> Entry {
|
||||||
let entry = next_entry(start_hash, num_hashes, event);
|
let entry = next_entry(start_hash, num_hashes, event);
|
||||||
*start_hash = entry.end_hash;
|
*start_hash = entry.end_hash;
|
||||||
entry
|
entry
|
||||||
@ -217,7 +229,11 @@ pub fn verify_signature(peer_public_key_bytes: &[u8], msg_bytes: &[u8], sig_byte
|
|||||||
signature::verify(&signature::ED25519, peer_public_key, msg, sig).is_ok()
|
signature::verify(&signature::ED25519, peer_public_key, msg, sig).is_ok()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_entries(start_hash: &Sha256Hash, num_hashes: u64, events: &[Event]) -> Vec<Entry> {
|
pub fn create_entries(
|
||||||
|
start_hash: &Sha256Hash,
|
||||||
|
num_hashes: u64,
|
||||||
|
events: &[Event<Sha256Hash>],
|
||||||
|
) -> Vec<Entry> {
|
||||||
let mut end_hash = *start_hash;
|
let mut end_hash = *start_hash;
|
||||||
events
|
events
|
||||||
.iter()
|
.iter()
|
||||||
|
Reference in New Issue
Block a user