2018-05-29 12:28:07 -07:00
|
|
|
//! The `budget` module provides a domain-specific language for payment plans. Users create Budget objects that
|
|
|
|
//! are given to an interpreter. The interpreter listens for `Witness` transactions,
|
|
|
|
//! which it uses to reduce the payment plan. When the budget is reduced to a
|
|
|
|
//! `Payment`, the payment is executed.
|
|
|
|
|
|
|
|
use chrono::prelude::*;
|
|
|
|
use payment_plan::{Payment, PaymentPlan, Witness};
|
|
|
|
use signature::PublicKey;
|
|
|
|
use std::mem;
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
|
|
|
|
pub enum Condition {
|
|
|
|
Timestamp(DateTime<Utc>),
|
|
|
|
Signature(PublicKey),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Condition {
|
|
|
|
/// Return true if the given Witness satisfies this Condition.
|
|
|
|
pub fn is_satisfied(&self, witness: &Witness) -> bool {
|
|
|
|
match (self, witness) {
|
2018-06-04 16:17:23 -07:00
|
|
|
(Condition::Signature(pubkey), Witness::Signature(from)) => pubkey == from,
|
|
|
|
(Condition::Timestamp(dt), Witness::Timestamp(last_time)) => dt <= last_time,
|
2018-05-29 12:28:07 -07:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
|
|
|
|
pub enum Budget {
|
|
|
|
Pay(Payment),
|
|
|
|
After(Condition, Payment),
|
|
|
|
Race((Condition, Payment), (Condition, Payment)),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Budget {
|
2018-05-29 16:11:24 -07:00
|
|
|
/// Create the simplest budget - one that pays `tokens` to PublicKey.
|
2018-05-29 12:28:07 -07:00
|
|
|
pub fn new_payment(tokens: i64, to: PublicKey) -> Self {
|
|
|
|
Budget::Pay(Payment { tokens, to })
|
|
|
|
}
|
|
|
|
|
2018-05-29 16:11:24 -07:00
|
|
|
/// Create a budget that pays `tokens` to `to` after being witnessed by `from`.
|
2018-05-29 12:28:07 -07:00
|
|
|
pub fn new_authorized_payment(from: PublicKey, tokens: i64, to: PublicKey) -> Self {
|
|
|
|
Budget::After(Condition::Signature(from), Payment { tokens, to })
|
|
|
|
}
|
|
|
|
|
2018-05-29 16:11:24 -07:00
|
|
|
/// Create a budget that pays `tokens` to `to` after the given DateTime.
|
2018-05-29 12:28:07 -07:00
|
|
|
pub fn new_future_payment(dt: DateTime<Utc>, tokens: i64, to: PublicKey) -> Self {
|
|
|
|
Budget::After(Condition::Timestamp(dt), Payment { tokens, to })
|
|
|
|
}
|
|
|
|
|
2018-05-29 16:11:24 -07:00
|
|
|
/// Create a budget that pays `tokens` to `to` after the given DateTime
|
2018-05-29 12:28:07 -07:00
|
|
|
/// unless cancelled by `from`.
|
|
|
|
pub fn new_cancelable_future_payment(
|
|
|
|
dt: DateTime<Utc>,
|
|
|
|
from: PublicKey,
|
|
|
|
tokens: i64,
|
|
|
|
to: PublicKey,
|
|
|
|
) -> Self {
|
|
|
|
Budget::Race(
|
|
|
|
(Condition::Timestamp(dt), Payment { tokens, to }),
|
|
|
|
(Condition::Signature(from), Payment { tokens, to: from }),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PaymentPlan for Budget {
|
2018-05-29 16:11:24 -07:00
|
|
|
/// Return Payment if the budget requires no additional Witnesses.
|
2018-05-29 12:28:07 -07:00
|
|
|
fn final_payment(&self) -> Option<Payment> {
|
2018-06-04 16:17:23 -07:00
|
|
|
match self {
|
|
|
|
Budget::Pay(payment) => Some(payment.clone()),
|
2018-05-29 12:28:07 -07:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-29 16:11:24 -07:00
|
|
|
/// Return true if the budget spends exactly `spendable_tokens`.
|
2018-05-29 12:28:07 -07:00
|
|
|
fn verify(&self, spendable_tokens: i64) -> bool {
|
2018-06-04 16:17:23 -07:00
|
|
|
match self {
|
|
|
|
Budget::Pay(payment) | Budget::After(_, payment) => payment.tokens == spendable_tokens,
|
|
|
|
Budget::Race(a, b) => a.1.tokens == spendable_tokens && b.1.tokens == spendable_tokens,
|
2018-05-29 12:28:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-29 16:11:24 -07:00
|
|
|
/// Apply a witness to the budget to see if the budget can be reduced.
|
|
|
|
/// If so, modify the budget in-place.
|
2018-05-29 12:28:07 -07:00
|
|
|
fn apply_witness(&mut self, witness: &Witness) {
|
2018-06-04 16:17:23 -07:00
|
|
|
let new_payment = match self {
|
|
|
|
Budget::After(cond, payment) if cond.is_satisfied(witness) => Some(payment),
|
|
|
|
Budget::Race((cond, payment), _) if cond.is_satisfied(witness) => Some(payment),
|
|
|
|
Budget::Race(_, (cond, payment)) if cond.is_satisfied(witness) => Some(payment),
|
2018-05-29 12:28:07 -07:00
|
|
|
_ => None,
|
|
|
|
}.cloned();
|
|
|
|
|
|
|
|
if let Some(payment) = new_payment {
|
|
|
|
mem::replace(self, Budget::Pay(payment));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_signature_satisfied() {
|
|
|
|
let sig = PublicKey::default();
|
|
|
|
assert!(Condition::Signature(sig).is_satisfied(&Witness::Signature(sig)));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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);
|
|
|
|
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)));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2018-05-29 16:11:24 -07:00
|
|
|
fn test_verify() {
|
2018-05-29 12:28:07 -07:00
|
|
|
let dt = Utc.ymd(2014, 11, 14).and_hms(8, 9, 10);
|
|
|
|
let from = PublicKey::default();
|
|
|
|
let to = PublicKey::default();
|
|
|
|
assert!(Budget::new_payment(42, to).verify(42));
|
|
|
|
assert!(Budget::new_authorized_payment(from, 42, to).verify(42));
|
|
|
|
assert!(Budget::new_future_payment(dt, 42, to).verify(42));
|
|
|
|
assert!(Budget::new_cancelable_future_payment(dt, from, 42, to).verify(42));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_authorized_payment() {
|
|
|
|
let from = PublicKey::default();
|
|
|
|
let to = PublicKey::default();
|
|
|
|
|
2018-05-29 16:11:24 -07:00
|
|
|
let mut budget = Budget::new_authorized_payment(from, 42, to);
|
|
|
|
budget.apply_witness(&Witness::Signature(from));
|
|
|
|
assert_eq!(budget, Budget::new_payment(42, to));
|
2018-05-29 12:28:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_future_payment() {
|
|
|
|
let dt = Utc.ymd(2014, 11, 14).and_hms(8, 9, 10);
|
|
|
|
let to = PublicKey::default();
|
|
|
|
|
2018-05-29 16:11:24 -07:00
|
|
|
let mut budget = Budget::new_future_payment(dt, 42, to);
|
|
|
|
budget.apply_witness(&Witness::Timestamp(dt));
|
|
|
|
assert_eq!(budget, Budget::new_payment(42, to));
|
2018-05-29 12:28:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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();
|
|
|
|
|
2018-05-29 16:11:24 -07:00
|
|
|
let mut budget = Budget::new_cancelable_future_payment(dt, from, 42, to);
|
|
|
|
budget.apply_witness(&Witness::Timestamp(dt));
|
|
|
|
assert_eq!(budget, Budget::new_payment(42, to));
|
2018-05-29 12:28:07 -07:00
|
|
|
|
2018-05-29 16:11:24 -07:00
|
|
|
let mut budget = Budget::new_cancelable_future_payment(dt, from, 42, to);
|
|
|
|
budget.apply_witness(&Witness::Signature(from));
|
|
|
|
assert_eq!(budget, Budget::new_payment(42, from));
|
2018-05-29 12:28:07 -07:00
|
|
|
}
|
|
|
|
}
|