Boot Event timestamp/singature constructors

This commit is contained in:
Greg Fitzgerald 2018-05-23 23:53:10 -06:00
parent 73d3c17507
commit e12e154877
5 changed files with 22 additions and 75 deletions

View File

@ -259,15 +259,13 @@ impl Bank {
}
fn partition_events(events: Vec<Event>) -> (Vec<Transaction>, Vec<Event>) {
let mut trs = vec![];
let mut rest = vec![];
for event in events {
match event {
Event::Transaction(tr) => trs.push(tr),
_ => rest.push(event),
}
}
(trs, rest)
(
events
.into_iter()
.map(|Event::Transaction(tr)| tr)
.collect(),
vec![],
)
}
pub fn process_verified_events(&self, events: Vec<Event>) -> Vec<Result<Event>> {
@ -367,8 +365,6 @@ impl Bank {
pub fn process_verified_event(&self, event: Event) -> Result<Event> {
match event {
Event::Transaction(ref tr) => self.process_verified_transaction(tr),
Event::Signature { from, tx_sig, .. } => self.process_verified_sig(from, tx_sig),
Event::Timestamp { from, dt, .. } => self.process_verified_timestamp(from, dt),
}?;
Ok(event)
}

View File

@ -97,12 +97,9 @@ fn main() {
// fields are the same. That entry should be treated as a deposit, not a
// transfer to oneself.
let entry1: Entry = entries.next().unwrap();
let deposit = if let Event::Transaction(ref tr) = entry1.events[0] {
if let Instruction::NewContract(contract) = &tr.instruction {
contract.plan.final_payment()
} else {
None
}
let Event::Transaction(ref tr) = entry1.events[0];
let deposit = if let Instruction::NewContract(contract) = &tr.instruction {
contract.plan.final_payment()
} else {
None
};

View File

@ -68,14 +68,6 @@ fn add_event_data(hash_data: &mut Vec<u8>, event: &Event) {
hash_data.push(0u8);
hash_data.extend_from_slice(&tr.sig);
}
Event::Signature { ref sig, .. } => {
hash_data.push(1u8);
hash_data.extend_from_slice(sig);
}
Event::Timestamp { ref sig, .. } => {
hash_data.push(2u8);
hash_data.extend_from_slice(sig);
}
}
}
@ -120,6 +112,7 @@ mod tests {
use event::Event;
use hash::hash;
use signature::{KeyPair, KeyPairUtil};
use transaction::Transaction;
#[test]
fn test_entry_verify() {
@ -154,8 +147,12 @@ mod tests {
// First, verify entries
let keypair = KeyPair::new();
let tr0 = Event::new_timestamp(&keypair, Utc::now(), zero);
let tr1 = Event::new_signature(&keypair, Default::default(), zero);
let tr0 = Event::Transaction(Transaction::new_timestamp(&keypair, Utc::now(), zero));
let tr1 = Event::Transaction(Transaction::new_signature(
&keypair,
Default::default(),
zero,
));
let mut e0 = Entry::new(&zero, 0, vec![tr0.clone(), tr1.clone()]);
assert!(e0.verify(&zero));

View File

@ -1,25 +1,13 @@
//! The `event` module handles events, which may be a `Transaction`, or a `Witness` used to process a pending
//! Transaction.
use bincode::serialize;
use chrono::prelude::*;
use hash::Hash;
use signature::{KeyPair, PublicKey, Signature, SignatureUtil};
use signature::{KeyPair, PublicKey};
use transaction::Transaction;
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub enum Event {
Transaction(Transaction),
Signature {
from: PublicKey,
tx_sig: Signature,
sig: Signature,
},
Timestamp {
from: PublicKey,
dt: DateTime<Utc>,
sig: Signature,
},
}
impl Event {
@ -33,41 +21,11 @@ impl Event {
Event::Transaction(tr)
}
/// Create and sign a new Witness Timestamp. Used for unit-testing.
pub fn new_timestamp(from: &KeyPair, dt: DateTime<Utc>, last_id: Hash) -> Self {
let tr = Transaction::new_timestamp(from, dt, last_id);
Event::Transaction(tr)
}
/// Create and sign a new Witness Signature. Used for unit-testing.
pub fn new_signature(from: &KeyPair, tx_sig: Signature, last_id: Hash) -> Self {
let tr = Transaction::new_signature(from, tx_sig, last_id);
Event::Transaction(tr)
}
/// Verify the Event's signature's are valid and if a transaction, that its
/// spending plan is valid.
pub fn verify(&self) -> bool {
match *self {
Event::Transaction(ref tr) => tr.verify_sig(),
Event::Signature { from, tx_sig, sig } => sig.verify(&from, &tx_sig),
Event::Timestamp { from, dt, sig } => sig.verify(
&from,
&serialize(&dt).expect("serialize 'dt' in pub fn verify"),
),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use signature::{KeyPair, KeyPairUtil};
#[test]
fn test_event_verify() {
let zero = Hash::default();
assert!(Event::new_timestamp(&KeyPair::new(), Utc::now(), zero).verify());
assert!(Event::new_signature(&KeyPair::new(), Signature::default(), zero).verify());
}
}

View File

@ -76,11 +76,10 @@ mod tests {
#[test]
fn test_create_events() {
let mut events = Mint::new(100).create_events().into_iter();
if let Event::Transaction(tr) = events.next().unwrap() {
if let Instruction::NewContract(contract) = tr.instruction {
if let Plan::Pay(payment) = contract.plan {
assert_eq!(tr.from, payment.to);
}
let Event::Transaction(tr) = events.next().unwrap();
if let Instruction::NewContract(contract) = tr.instruction {
if let Plan::Pay(payment) = contract.plan {
assert_eq!(tr.from, payment.to);
}
}
assert_eq!(events.next(), None);