Boot Event timestamp/singature constructors
This commit is contained in:
parent
73d3c17507
commit
e12e154877
18
src/bank.rs
18
src/bank.rs
|
@ -259,15 +259,13 @@ impl Bank {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn partition_events(events: Vec<Event>) -> (Vec<Transaction>, Vec<Event>) {
|
fn partition_events(events: Vec<Event>) -> (Vec<Transaction>, Vec<Event>) {
|
||||||
let mut trs = vec![];
|
(
|
||||||
let mut rest = vec![];
|
events
|
||||||
for event in events {
|
.into_iter()
|
||||||
match event {
|
.map(|Event::Transaction(tr)| tr)
|
||||||
Event::Transaction(tr) => trs.push(tr),
|
.collect(),
|
||||||
_ => rest.push(event),
|
vec![],
|
||||||
}
|
)
|
||||||
}
|
|
||||||
(trs, rest)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn process_verified_events(&self, events: Vec<Event>) -> Vec<Result<Event>> {
|
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> {
|
pub fn process_verified_event(&self, event: Event) -> Result<Event> {
|
||||||
match event {
|
match event {
|
||||||
Event::Transaction(ref tr) => self.process_verified_transaction(tr),
|
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)
|
Ok(event)
|
||||||
}
|
}
|
||||||
|
|
|
@ -97,12 +97,9 @@ fn main() {
|
||||||
// fields are the same. That entry should be treated as a deposit, not a
|
// fields are the same. That entry should be treated as a deposit, not a
|
||||||
// transfer to oneself.
|
// transfer to oneself.
|
||||||
let entry1: Entry = entries.next().unwrap();
|
let entry1: Entry = entries.next().unwrap();
|
||||||
let deposit = if let Event::Transaction(ref tr) = entry1.events[0] {
|
let Event::Transaction(ref tr) = entry1.events[0];
|
||||||
if let Instruction::NewContract(contract) = &tr.instruction {
|
let deposit = if let Instruction::NewContract(contract) = &tr.instruction {
|
||||||
contract.plan.final_payment()
|
contract.plan.final_payment()
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
17
src/entry.rs
17
src/entry.rs
|
@ -68,14 +68,6 @@ fn add_event_data(hash_data: &mut Vec<u8>, event: &Event) {
|
||||||
hash_data.push(0u8);
|
hash_data.push(0u8);
|
||||||
hash_data.extend_from_slice(&tr.sig);
|
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 event::Event;
|
||||||
use hash::hash;
|
use hash::hash;
|
||||||
use signature::{KeyPair, KeyPairUtil};
|
use signature::{KeyPair, KeyPairUtil};
|
||||||
|
use transaction::Transaction;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_entry_verify() {
|
fn test_entry_verify() {
|
||||||
|
@ -154,8 +147,12 @@ mod tests {
|
||||||
|
|
||||||
// First, verify entries
|
// First, verify entries
|
||||||
let keypair = KeyPair::new();
|
let keypair = KeyPair::new();
|
||||||
let tr0 = Event::new_timestamp(&keypair, Utc::now(), zero);
|
let tr0 = Event::Transaction(Transaction::new_timestamp(&keypair, Utc::now(), zero));
|
||||||
let tr1 = Event::new_signature(&keypair, Default::default(), zero);
|
let tr1 = Event::Transaction(Transaction::new_signature(
|
||||||
|
&keypair,
|
||||||
|
Default::default(),
|
||||||
|
zero,
|
||||||
|
));
|
||||||
let mut e0 = Entry::new(&zero, 0, vec![tr0.clone(), tr1.clone()]);
|
let mut e0 = Entry::new(&zero, 0, vec![tr0.clone(), tr1.clone()]);
|
||||||
assert!(e0.verify(&zero));
|
assert!(e0.verify(&zero));
|
||||||
|
|
||||||
|
|
44
src/event.rs
44
src/event.rs
|
@ -1,25 +1,13 @@
|
||||||
//! The `event` module handles events, which may be a `Transaction`, or a `Witness` used to process a pending
|
//! The `event` module handles events, which may be a `Transaction`, or a `Witness` used to process a pending
|
||||||
//! Transaction.
|
//! Transaction.
|
||||||
|
|
||||||
use bincode::serialize;
|
|
||||||
use chrono::prelude::*;
|
|
||||||
use hash::Hash;
|
use hash::Hash;
|
||||||
use signature::{KeyPair, PublicKey, Signature, SignatureUtil};
|
use signature::{KeyPair, PublicKey};
|
||||||
use transaction::Transaction;
|
use transaction::Transaction;
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
|
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
|
||||||
pub enum Event {
|
pub enum Event {
|
||||||
Transaction(Transaction),
|
Transaction(Transaction),
|
||||||
Signature {
|
|
||||||
from: PublicKey,
|
|
||||||
tx_sig: Signature,
|
|
||||||
sig: Signature,
|
|
||||||
},
|
|
||||||
Timestamp {
|
|
||||||
from: PublicKey,
|
|
||||||
dt: DateTime<Utc>,
|
|
||||||
sig: Signature,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Event {
|
impl Event {
|
||||||
|
@ -33,41 +21,11 @@ impl Event {
|
||||||
Event::Transaction(tr)
|
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
|
/// Verify the Event's signature's are valid and if a transaction, that its
|
||||||
/// spending plan is valid.
|
/// spending plan is valid.
|
||||||
pub fn verify(&self) -> bool {
|
pub fn verify(&self) -> bool {
|
||||||
match *self {
|
match *self {
|
||||||
Event::Transaction(ref tr) => tr.verify_sig(),
|
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());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -76,11 +76,10 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_create_events() {
|
fn test_create_events() {
|
||||||
let mut events = Mint::new(100).create_events().into_iter();
|
let mut events = Mint::new(100).create_events().into_iter();
|
||||||
if let Event::Transaction(tr) = events.next().unwrap() {
|
let Event::Transaction(tr) = events.next().unwrap();
|
||||||
if let Instruction::NewContract(contract) = tr.instruction {
|
if let Instruction::NewContract(contract) = tr.instruction {
|
||||||
if let Plan::Pay(payment) = contract.plan {
|
if let Plan::Pay(payment) = contract.plan {
|
||||||
assert_eq!(tr.from, payment.to);
|
assert_eq!(tr.from, payment.to);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
assert_eq!(events.next(), None);
|
assert_eq!(events.next(), None);
|
||||||
|
|
Loading…
Reference in New Issue