2018-03-30 10:43:38 -07:00
|
|
|
//! The `plan` module provides a domain-specific language for payment plans. Users create Plan objects that
|
2018-03-20 14:52:46 -07:00
|
|
|
//! are given to an interpreter. The interpreter listens for `Witness` events,
|
|
|
|
//! which it uses to reduce the payment plan. When the plan is reduced to a
|
|
|
|
//! `Payment`, the payment is executed.
|
2018-03-17 13:42:43 -07:00
|
|
|
|
|
|
|
use chrono::prelude::*;
|
2018-03-26 21:03:26 -07:00
|
|
|
use signature::PublicKey;
|
2018-03-17 13:42:43 -07:00
|
|
|
use std::mem;
|
|
|
|
|
2018-03-20 14:31:28 -07:00
|
|
|
pub enum Witness {
|
2018-03-17 13:42:43 -07:00
|
|
|
Timestamp(DateTime<Utc>),
|
|
|
|
Signature(PublicKey),
|
|
|
|
}
|
|
|
|
|
2018-03-20 14:31:28 -07:00
|
|
|
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
|
|
|
|
pub enum Condition {
|
2018-03-17 13:42:58 -07:00
|
|
|
Timestamp(DateTime<Utc>),
|
|
|
|
Signature(PublicKey),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Condition {
|
2018-03-29 11:20:54 -07:00
|
|
|
/// Return true if the given Witness satisfies this Condition.
|
2018-03-20 15:53:41 -07:00
|
|
|
pub fn is_satisfied(&self, witness: &Witness) -> bool {
|
|
|
|
match (self, witness) {
|
2018-03-20 14:25:48 -07:00
|
|
|
(&Condition::Signature(ref pubkey), &Witness::Signature(ref from)) => pubkey == from,
|
|
|
|
(&Condition::Timestamp(ref dt), &Witness::Timestamp(ref last_time)) => dt <= last_time,
|
2018-03-17 13:42:58 -07:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-17 13:42:43 -07:00
|
|
|
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
|
2018-03-17 13:42:50 -07:00
|
|
|
pub struct Payment {
|
2018-03-19 09:03:41 -07:00
|
|
|
pub tokens: i64,
|
2018-03-17 13:42:43 -07:00
|
|
|
pub to: PublicKey,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
|
2018-03-17 13:42:50 -07:00
|
|
|
pub enum Plan {
|
2018-03-20 14:43:04 -07:00
|
|
|
Pay(Payment),
|
|
|
|
After(Condition, Payment),
|
|
|
|
Race((Condition, Payment), (Condition, Payment)),
|
2018-03-17 13:42:43 -07:00
|
|
|
}
|
|
|
|
|
2018-03-17 13:42:50 -07:00
|
|
|
impl Plan {
|
2018-03-29 11:20:54 -07:00
|
|
|
/// Create the simplest spending plan - one that pays `tokens` to PublicKey.
|
2018-03-19 09:03:41 -07:00
|
|
|
pub fn new_payment(tokens: i64, to: PublicKey) -> Self {
|
2018-03-20 14:43:04 -07:00
|
|
|
Plan::Pay(Payment { tokens, to })
|
2018-03-18 20:02:28 -07:00
|
|
|
}
|
|
|
|
|
2018-03-29 11:20:54 -07:00
|
|
|
/// Create a spending plan that pays `tokens` to `to` after being witnessed by `from`.
|
2018-03-19 09:03:41 -07:00
|
|
|
pub fn new_authorized_payment(from: PublicKey, tokens: i64, to: PublicKey) -> Self {
|
2018-03-20 14:43:04 -07:00
|
|
|
Plan::After(Condition::Signature(from), Payment { tokens, to })
|
2018-03-18 20:02:28 -07:00
|
|
|
}
|
|
|
|
|
2018-03-29 11:20:54 -07:00
|
|
|
/// Create a spending plan that pays `tokens` to `to` after the given DateTime.
|
2018-03-19 09:03:41 -07:00
|
|
|
pub fn new_future_payment(dt: DateTime<Utc>, tokens: i64, to: PublicKey) -> Self {
|
2018-03-20 14:43:04 -07:00
|
|
|
Plan::After(Condition::Timestamp(dt), Payment { tokens, to })
|
2018-03-18 20:02:28 -07:00
|
|
|
}
|
|
|
|
|
2018-03-29 11:20:54 -07:00
|
|
|
/// Create a spending plan that pays `tokens` to `to` after the given DateTime
|
|
|
|
/// unless cancelled by `from`.
|
2018-03-18 20:02:28 -07:00
|
|
|
pub fn new_cancelable_future_payment(
|
|
|
|
dt: DateTime<Utc>,
|
|
|
|
from: PublicKey,
|
2018-03-19 09:03:41 -07:00
|
|
|
tokens: i64,
|
2018-03-18 20:02:28 -07:00
|
|
|
to: PublicKey,
|
|
|
|
) -> Self {
|
|
|
|
Plan::Race(
|
2018-03-20 14:43:04 -07:00
|
|
|
(Condition::Timestamp(dt), Payment { tokens, to }),
|
|
|
|
(Condition::Signature(from), Payment { tokens, to: from }),
|
2018-03-18 20:02:28 -07:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-04-02 12:51:44 -07:00
|
|
|
/// Return Payment if the spending plan requires no additional Witnesses.
|
|
|
|
pub fn final_payment(&self) -> Option<Payment> {
|
2018-03-20 15:52:47 -07:00
|
|
|
match *self {
|
2018-04-02 12:51:44 -07:00
|
|
|
Plan::Pay(ref payment) => Some(payment.clone()),
|
|
|
|
_ => None,
|
2018-03-20 15:52:47 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-29 11:20:54 -07:00
|
|
|
/// Return true if the plan spends exactly `spendable_tokens`.
|
2018-03-19 09:03:41 -07:00
|
|
|
pub fn verify(&self, spendable_tokens: i64) -> bool {
|
2018-03-17 13:42:43 -07:00
|
|
|
match *self {
|
2018-03-22 13:38:06 -07:00
|
|
|
Plan::Pay(ref payment) | Plan::After(_, ref payment) => {
|
|
|
|
payment.tokens == spendable_tokens
|
|
|
|
}
|
2018-03-17 13:42:58 -07:00
|
|
|
Plan::Race(ref a, ref b) => {
|
2018-03-20 14:43:04 -07:00
|
|
|
a.1.tokens == spendable_tokens && b.1.tokens == spendable_tokens
|
2018-03-17 13:42:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-20 15:58:14 -07:00
|
|
|
/// Apply a witness to the spending plan to see if the plan can be reduced.
|
|
|
|
/// If so, modify the plan in-place.
|
2018-03-22 13:38:06 -07:00
|
|
|
pub fn apply_witness(&mut self, witness: &Witness) {
|
2018-03-20 16:32:02 -07:00
|
|
|
let new_payment = match *self {
|
2018-03-22 13:38:06 -07:00
|
|
|
Plan::After(ref cond, ref payment) if cond.is_satisfied(witness) => Some(payment),
|
|
|
|
Plan::Race((ref cond, ref payment), _) if cond.is_satisfied(witness) => Some(payment),
|
|
|
|
Plan::Race(_, (ref cond, ref payment)) if cond.is_satisfied(witness) => Some(payment),
|
2018-03-20 16:32:02 -07:00
|
|
|
_ => None,
|
2018-03-22 13:38:06 -07:00
|
|
|
}.cloned();
|
2018-03-17 13:42:43 -07:00
|
|
|
|
2018-03-20 14:43:04 -07:00
|
|
|
if let Some(payment) = new_payment {
|
|
|
|
mem::replace(self, Plan::Pay(payment));
|
2018-03-17 13:42:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-18 20:02:28 -07:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_signature_satisfied() {
|
|
|
|
let sig = PublicKey::default();
|
2018-03-20 14:25:48 -07:00
|
|
|
assert!(Condition::Signature(sig).is_satisfied(&Witness::Signature(sig)));
|
2018-03-18 20:02:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_timestamp_satisfied() {
|
|
|
|
let dt1 = Utc.ymd(2014, 11, 14).and_hms(8, 9, 10);
|
|
|
|
let dt2 = Utc.ymd(2014, 11, 14).and_hms(10, 9, 8);
|
2018-03-20 14:25:48 -07:00
|
|
|
assert!(Condition::Timestamp(dt1).is_satisfied(&Witness::Timestamp(dt1)));
|
|
|
|
assert!(Condition::Timestamp(dt1).is_satisfied(&Witness::Timestamp(dt2)));
|
|
|
|
assert!(!Condition::Timestamp(dt2).is_satisfied(&Witness::Timestamp(dt1)));
|
2018-03-18 20:02:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_verify_plan() {
|
|
|
|
let dt = Utc.ymd(2014, 11, 14).and_hms(8, 9, 10);
|
|
|
|
let from = PublicKey::default();
|
|
|
|
let to = PublicKey::default();
|
|
|
|
assert!(Plan::new_payment(42, to).verify(42));
|
|
|
|
assert!(Plan::new_authorized_payment(from, 42, to).verify(42));
|
|
|
|
assert!(Plan::new_future_payment(dt, 42, to).verify(42));
|
|
|
|
assert!(Plan::new_cancelable_future_payment(dt, from, 42, to).verify(42));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_authorized_payment() {
|
|
|
|
let from = PublicKey::default();
|
|
|
|
let to = PublicKey::default();
|
|
|
|
|
|
|
|
let mut plan = Plan::new_authorized_payment(from, 42, to);
|
2018-03-22 13:38:06 -07:00
|
|
|
plan.apply_witness(&Witness::Signature(from));
|
2018-03-18 20:02:28 -07:00
|
|
|
assert_eq!(plan, Plan::new_payment(42, to));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_future_payment() {
|
|
|
|
let dt = Utc.ymd(2014, 11, 14).and_hms(8, 9, 10);
|
|
|
|
let to = PublicKey::default();
|
|
|
|
|
|
|
|
let mut plan = Plan::new_future_payment(dt, 42, to);
|
2018-03-22 13:38:06 -07:00
|
|
|
plan.apply_witness(&Witness::Timestamp(dt));
|
2018-03-18 20:02:28 -07:00
|
|
|
assert_eq!(plan, Plan::new_payment(42, to));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_cancelable_future_payment() {
|
|
|
|
let dt = Utc.ymd(2014, 11, 14).and_hms(8, 9, 10);
|
|
|
|
let from = PublicKey::default();
|
|
|
|
let to = PublicKey::default();
|
|
|
|
|
|
|
|
let mut plan = Plan::new_cancelable_future_payment(dt, from, 42, to);
|
2018-03-22 13:38:06 -07:00
|
|
|
plan.apply_witness(&Witness::Timestamp(dt));
|
2018-03-18 20:02:28 -07:00
|
|
|
assert_eq!(plan, Plan::new_payment(42, to));
|
|
|
|
|
|
|
|
let mut plan = Plan::new_cancelable_future_payment(dt, from, 42, to);
|
2018-03-22 13:38:06 -07:00
|
|
|
plan.apply_witness(&Witness::Signature(from));
|
2018-03-18 20:02:28 -07:00
|
|
|
assert_eq!(plan, Plan::new_payment(42, from));
|
|
|
|
}
|
|
|
|
}
|