2018-03-29 12:20:54 -06:00
|
|
|
//! The `transaction` module provides functionality for creating log transactions.
|
2018-03-06 12:18:17 -07:00
|
|
|
|
|
|
|
|
use bincode::serialize;
|
2018-03-07 21:55:49 -07:00
|
|
|
use chrono::prelude::*;
|
2018-03-26 22:03:26 -06:00
|
|
|
use hash::Hash;
|
2018-03-20 15:43:04 -06:00
|
|
|
use plan::{Condition, Payment, Plan};
|
2018-04-02 21:15:21 -06:00
|
|
|
use rayon::prelude::*;
|
2018-03-26 22:03:26 -06:00
|
|
|
use signature::{KeyPair, KeyPairUtil, PublicKey, Signature, SignatureUtil};
|
2018-03-11 00:11:08 -07:00
|
|
|
|
2018-03-10 16:55:39 -07:00
|
|
|
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
|
2018-03-17 14:42:50 -06:00
|
|
|
pub struct Transaction {
|
2018-03-10 16:55:39 -07:00
|
|
|
pub from: PublicKey,
|
2018-03-17 14:42:50 -06:00
|
|
|
pub plan: Plan,
|
2018-03-19 10:03:41 -06:00
|
|
|
pub tokens: i64,
|
2018-03-06 17:36:45 -07:00
|
|
|
pub last_id: Hash,
|
2018-03-06 12:18:17 -07:00
|
|
|
pub sig: Signature,
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-17 14:42:50 -06:00
|
|
|
impl Transaction {
|
2018-03-29 12:20:54 -06:00
|
|
|
/// Create and sign a new Transaction. Used for unit-testing.
|
2018-03-19 10:03:41 -06:00
|
|
|
pub fn new(from_keypair: &KeyPair, to: PublicKey, tokens: i64, last_id: Hash) -> Self {
|
2018-03-10 17:11:12 -07:00
|
|
|
let from = from_keypair.pubkey();
|
2018-03-20 15:43:04 -06:00
|
|
|
let plan = Plan::Pay(Payment { tokens, to });
|
2018-03-10 16:55:39 -07:00
|
|
|
let mut tr = Transaction {
|
2018-03-10 17:11:12 -07:00
|
|
|
from,
|
2018-03-10 16:55:39 -07:00
|
|
|
plan,
|
2018-03-19 10:03:41 -06:00
|
|
|
tokens,
|
2018-03-07 21:55:49 -07:00
|
|
|
last_id,
|
|
|
|
|
sig: Signature::default(),
|
|
|
|
|
};
|
|
|
|
|
tr.sign(from_keypair);
|
|
|
|
|
tr
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-29 12:20:54 -06:00
|
|
|
/// Create and sign a postdated Transaction. Used for unit-testing.
|
2018-03-07 21:55:49 -07:00
|
|
|
pub fn new_on_date(
|
|
|
|
|
from_keypair: &KeyPair,
|
|
|
|
|
to: PublicKey,
|
|
|
|
|
dt: DateTime<Utc>,
|
2018-03-19 10:03:41 -06:00
|
|
|
tokens: i64,
|
2018-03-07 21:55:49 -07:00
|
|
|
last_id: Hash,
|
|
|
|
|
) -> Self {
|
2018-03-07 22:25:45 -07:00
|
|
|
let from = from_keypair.pubkey();
|
2018-03-10 22:00:27 -07:00
|
|
|
let plan = Plan::Race(
|
2018-03-20 15:43:04 -06:00
|
|
|
(Condition::Timestamp(dt), Payment { tokens, to }),
|
|
|
|
|
(Condition::Signature(from), Payment { tokens, to: from }),
|
2018-03-10 22:00:27 -07:00
|
|
|
);
|
2018-03-10 16:55:39 -07:00
|
|
|
let mut tr = Transaction {
|
|
|
|
|
from,
|
|
|
|
|
plan,
|
2018-03-19 10:03:41 -06:00
|
|
|
tokens,
|
2018-03-06 12:18:17 -07:00
|
|
|
last_id,
|
2018-03-06 16:34:14 -07:00
|
|
|
sig: Signature::default(),
|
|
|
|
|
};
|
|
|
|
|
tr.sign(from_keypair);
|
|
|
|
|
tr
|
2018-03-06 12:18:17 -07:00
|
|
|
}
|
2018-03-06 12:26:39 -07:00
|
|
|
|
2018-03-06 16:34:14 -07:00
|
|
|
fn get_sign_data(&self) -> Vec<u8> {
|
2018-04-02 20:34:18 -06:00
|
|
|
serialize(&(&self.plan, &self.tokens, &self.last_id)).unwrap()
|
2018-03-06 12:26:39 -07:00
|
|
|
}
|
2018-03-06 12:18:17 -07:00
|
|
|
|
2018-03-29 12:20:54 -06:00
|
|
|
/// Sign this transaction.
|
2018-03-07 11:05:06 -07:00
|
|
|
pub fn sign(&mut self, keypair: &KeyPair) {
|
2018-03-06 16:34:14 -07:00
|
|
|
let sign_data = self.get_sign_data();
|
|
|
|
|
self.sig = Signature::clone_from_slice(keypair.sign(&sign_data).as_ref());
|
|
|
|
|
}
|
2018-03-06 12:18:17 -07:00
|
|
|
|
2018-03-29 12:20:54 -06:00
|
|
|
/// Verify this transaction's signature and its spending plan.
|
2018-03-06 16:34:14 -07:00
|
|
|
pub fn verify(&self) -> bool {
|
2018-03-19 10:03:41 -06:00
|
|
|
self.sig.verify(&self.from, &self.get_sign_data()) && self.plan.verify(self.tokens)
|
2018-03-06 16:34:14 -07:00
|
|
|
}
|
2018-03-06 12:18:17 -07:00
|
|
|
}
|
|
|
|
|
|
2018-03-28 22:02:47 -06:00
|
|
|
/// Verify a batch of signatures.
|
|
|
|
|
pub fn verify_signatures(transactions: &[Transaction]) -> bool {
|
|
|
|
|
transactions.par_iter().all(|tr| tr.verify())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Verify a batch of spending plans.
|
|
|
|
|
pub fn verify_plans(transactions: &[Transaction]) -> bool {
|
|
|
|
|
transactions.par_iter().all(|tr| tr.plan.verify(tr.tokens))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Verify a batch of transactions.
|
|
|
|
|
pub fn verify_transactions(transactions: &[Transaction]) -> bool {
|
|
|
|
|
verify_signatures(transactions) && verify_plans(transactions)
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-06 12:18:17 -07:00
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
|
|
|
|
use bincode::{deserialize, serialize};
|
2018-03-06 16:34:14 -07:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_claim() {
|
2018-03-07 15:32:22 -07:00
|
|
|
let keypair = KeyPair::new();
|
2018-03-06 17:36:45 -07:00
|
|
|
let zero = Hash::default();
|
2018-03-17 14:42:50 -06:00
|
|
|
let tr0 = Transaction::new(&keypair, keypair.pubkey(), 42, zero);
|
2018-03-06 16:34:14 -07:00
|
|
|
assert!(tr0.verify());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_transfer() {
|
2018-03-06 17:36:45 -07:00
|
|
|
let zero = Hash::default();
|
2018-03-07 15:32:22 -07:00
|
|
|
let keypair0 = KeyPair::new();
|
|
|
|
|
let keypair1 = KeyPair::new();
|
|
|
|
|
let pubkey1 = keypair1.pubkey();
|
2018-03-17 14:42:50 -06:00
|
|
|
let tr0 = Transaction::new(&keypair0, pubkey1, 42, zero);
|
2018-03-06 16:34:14 -07:00
|
|
|
assert!(tr0.verify());
|
|
|
|
|
}
|
2018-03-06 12:18:17 -07:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_serialize_claim() {
|
2018-03-20 15:43:04 -06:00
|
|
|
let plan = Plan::Pay(Payment {
|
2018-03-19 10:03:41 -06:00
|
|
|
tokens: 0,
|
2018-03-10 22:00:27 -07:00
|
|
|
to: Default::default(),
|
2018-03-20 15:43:04 -06:00
|
|
|
});
|
2018-03-10 16:55:39 -07:00
|
|
|
let claim0 = Transaction {
|
|
|
|
|
from: Default::default(),
|
|
|
|
|
plan,
|
2018-03-19 10:03:41 -06:00
|
|
|
tokens: 0,
|
2018-03-06 16:34:14 -07:00
|
|
|
last_id: Default::default(),
|
|
|
|
|
sig: Default::default(),
|
|
|
|
|
};
|
2018-03-06 12:18:17 -07:00
|
|
|
let buf = serialize(&claim0).unwrap();
|
2018-03-17 14:42:50 -06:00
|
|
|
let claim1: Transaction = deserialize(&buf).unwrap();
|
2018-03-06 12:18:17 -07:00
|
|
|
assert_eq!(claim1, claim0);
|
|
|
|
|
}
|
2018-03-06 16:34:14 -07:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_bad_event_signature() {
|
2018-03-06 17:36:45 -07:00
|
|
|
let zero = Hash::default();
|
2018-03-07 15:32:22 -07:00
|
|
|
let keypair = KeyPair::new();
|
|
|
|
|
let pubkey = keypair.pubkey();
|
2018-03-17 14:42:50 -06:00
|
|
|
let mut tr = Transaction::new(&keypair, pubkey, 42, zero);
|
2018-03-06 16:34:14 -07:00
|
|
|
tr.sign(&keypair);
|
2018-03-19 10:03:41 -06:00
|
|
|
tr.tokens = 1_000_000; // <-- attack!
|
2018-03-06 16:34:14 -07:00
|
|
|
assert!(!tr.verify());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_hijack_attack() {
|
2018-03-07 15:32:22 -07:00
|
|
|
let keypair0 = KeyPair::new();
|
|
|
|
|
let keypair1 = KeyPair::new();
|
|
|
|
|
let thief_keypair = KeyPair::new();
|
|
|
|
|
let pubkey1 = keypair1.pubkey();
|
2018-03-06 17:36:45 -07:00
|
|
|
let zero = Hash::default();
|
2018-03-17 14:42:50 -06:00
|
|
|
let mut tr = Transaction::new(&keypair0, pubkey1, 42, zero);
|
2018-03-06 16:34:14 -07:00
|
|
|
tr.sign(&keypair0);
|
2018-03-20 15:43:04 -06:00
|
|
|
if let Plan::Pay(ref mut payment) = tr.plan {
|
2018-03-10 22:00:27 -07:00
|
|
|
payment.to = thief_keypair.pubkey(); // <-- attack!
|
|
|
|
|
};
|
2018-03-06 16:34:14 -07:00
|
|
|
assert!(!tr.verify());
|
|
|
|
|
}
|
2018-03-28 22:02:47 -06:00
|
|
|
|
2018-04-02 21:45:17 -06:00
|
|
|
#[test]
|
|
|
|
|
fn test_overspend_attack() {
|
|
|
|
|
let keypair0 = KeyPair::new();
|
|
|
|
|
let keypair1 = KeyPair::new();
|
|
|
|
|
let zero = Hash::default();
|
|
|
|
|
let mut tr = Transaction::new(&keypair0, keypair1.pubkey(), 1, zero);
|
|
|
|
|
if let Plan::Pay(ref mut payment) = tr.plan {
|
|
|
|
|
payment.tokens = 2; // <-- attack!
|
|
|
|
|
}
|
|
|
|
|
assert!(!tr.verify());
|
|
|
|
|
|
|
|
|
|
// Also, ensure all branchs of the plan spend all tokens
|
|
|
|
|
if let Plan::Pay(ref mut payment) = tr.plan {
|
|
|
|
|
payment.tokens = 0; // <-- whoops!
|
|
|
|
|
}
|
|
|
|
|
assert!(!tr.verify());
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-28 22:02:47 -06:00
|
|
|
#[test]
|
|
|
|
|
fn test_verify_transactions() {
|
|
|
|
|
let alice_keypair = KeyPair::new();
|
|
|
|
|
let bob_pubkey = KeyPair::new().pubkey();
|
|
|
|
|
let carol_pubkey = KeyPair::new().pubkey();
|
|
|
|
|
let last_id = Hash::default();
|
|
|
|
|
let tr0 = Transaction::new(&alice_keypair, bob_pubkey, 1, last_id);
|
|
|
|
|
let tr1 = Transaction::new(&alice_keypair, carol_pubkey, 1, last_id);
|
|
|
|
|
let transactions = vec![tr0, tr1];
|
|
|
|
|
assert!(verify_transactions(&transactions));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(all(feature = "unstable", test))]
|
|
|
|
|
mod bench {
|
|
|
|
|
extern crate test;
|
|
|
|
|
use self::test::Bencher;
|
|
|
|
|
use transaction::*;
|
|
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
|
fn verify_signatures_bench(bencher: &mut Bencher) {
|
|
|
|
|
let alice_keypair = KeyPair::new();
|
|
|
|
|
let last_id = Hash::default();
|
|
|
|
|
let transactions: Vec<_> = (0..64)
|
|
|
|
|
.into_par_iter()
|
|
|
|
|
.map(|_| {
|
|
|
|
|
let rando_pubkey = KeyPair::new().pubkey();
|
|
|
|
|
Transaction::new(&alice_keypair, rando_pubkey, 1, last_id)
|
|
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
bencher.iter(|| {
|
|
|
|
|
assert!(verify_signatures(&transactions));
|
|
|
|
|
});
|
|
|
|
|
}
|
2018-03-06 12:18:17 -07:00
|
|
|
}
|