2018-03-06 11:54:45 -07:00
|
|
|
//! The `event` crate provides the data structures for log events.
|
2018-03-02 08:43:54 -07:00
|
|
|
|
2018-03-07 22:25:45 -07:00
|
|
|
use signature::{PublicKey, Signature, SignatureUtil};
|
2018-03-06 12:48:26 -07:00
|
|
|
use transaction::Transaction;
|
2018-03-07 22:25:45 -07:00
|
|
|
use chrono::prelude::*;
|
|
|
|
use bincode::serialize;
|
2018-03-02 08:43:54 -07:00
|
|
|
|
|
|
|
/// When 'event' is Tick, the event represents a simple clock tick, and exists for the
|
|
|
|
/// sole purpose of improving the performance of event log verification. A tick can
|
|
|
|
/// be generated in 'num_hashes' hashes and verified in 'num_hashes' hashes. By logging
|
2018-03-04 07:34:38 -07:00
|
|
|
/// a hash alongside the tick, each tick and be verified in parallel using the 'id'
|
2018-03-02 08:43:54 -07:00
|
|
|
/// of the preceding tick to seed its hashing.
|
|
|
|
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
|
2018-03-06 20:22:30 -07:00
|
|
|
pub enum Event {
|
2018-03-02 08:43:54 -07:00
|
|
|
Tick,
|
2018-03-06 20:22:30 -07:00
|
|
|
Transaction(Transaction<i64>),
|
2018-03-07 22:25:45 -07:00
|
|
|
Signature {
|
|
|
|
from: PublicKey,
|
|
|
|
tx_sig: Signature,
|
|
|
|
sig: Signature,
|
|
|
|
},
|
|
|
|
Timestamp {
|
|
|
|
from: PublicKey,
|
|
|
|
dt: DateTime<Utc>,
|
|
|
|
sig: Signature,
|
|
|
|
},
|
2018-03-02 08:43:54 -07:00
|
|
|
}
|
|
|
|
|
2018-03-06 20:22:30 -07:00
|
|
|
impl Event {
|
2018-03-07 22:25:45 -07:00
|
|
|
// TODO: Rename this to transaction_signature().
|
2018-03-06 12:26:39 -07:00
|
|
|
pub fn get_signature(&self) -> Option<Signature> {
|
|
|
|
match *self {
|
|
|
|
Event::Tick => None,
|
|
|
|
Event::Transaction(ref tr) => Some(tr.sig),
|
2018-03-07 22:25:45 -07:00
|
|
|
Event::Signature { .. } => None,
|
|
|
|
Event::Timestamp { .. } => None,
|
2018-03-06 12:26:39 -07:00
|
|
|
}
|
2018-03-02 08:43:54 -07:00
|
|
|
}
|
|
|
|
|
2018-03-06 12:26:39 -07:00
|
|
|
pub fn verify(&self) -> bool {
|
|
|
|
match *self {
|
|
|
|
Event::Tick => true,
|
|
|
|
Event::Transaction(ref tr) => tr.verify(),
|
2018-03-07 22:25:45 -07:00
|
|
|
Event::Signature { from, tx_sig, sig } => sig.verify(&from, &tx_sig),
|
|
|
|
Event::Timestamp { from, dt, sig } => sig.verify(&from, &serialize(&dt).unwrap()),
|
2018-03-06 12:26:39 -07:00
|
|
|
}
|
2018-03-02 08:43:54 -07:00
|
|
|
}
|
2018-03-06 11:19:55 -07:00
|
|
|
}
|