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::*;
|
2018-09-20 13:12:53 -07:00
|
|
|
use payment_plan::{Payment, Witness};
|
2018-09-26 16:55:36 -07:00
|
|
|
use pubkey::Pubkey;
|
2018-05-29 12:28:07 -07:00
|
|
|
use std::mem;
|
|
|
|
|
2018-06-06 09:03:40 -07:00
|
|
|
/// A data type representing a `Witness` that the payment plan is waiting on.
|
2018-05-29 12:28:07 -07:00
|
|
|
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
|
|
|
|
pub enum Condition {
|
2018-06-06 09:03:40 -07:00
|
|
|
/// Wait for a `Timestamp` `Witness` at or after the given `DateTime`.
|
2018-08-09 08:13:57 -07:00
|
|
|
Timestamp(DateTime<Utc>, Pubkey),
|
2018-06-06 09:03:40 -07:00
|
|
|
|
2018-08-09 08:13:57 -07:00
|
|
|
/// Wait for a `Signature` `Witness` from `Pubkey`.
|
|
|
|
Signature(Pubkey),
|
2018-05-29 12:28:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Condition {
|
|
|
|
/// Return true if the given Witness satisfies this Condition.
|
2018-08-09 08:13:57 -07:00
|
|
|
pub fn is_satisfied(&self, witness: &Witness, from: &Pubkey) -> bool {
|
2018-05-29 12:28:07 -07:00
|
|
|
match (self, witness) {
|
2018-07-08 20:04:55 -07:00
|
|
|
(Condition::Signature(pubkey), Witness::Signature) => pubkey == from,
|
|
|
|
(Condition::Timestamp(dt, pubkey), Witness::Timestamp(last_time)) => {
|
|
|
|
pubkey == from && dt <= last_time
|
|
|
|
}
|
2018-05-29 12:28:07 -07:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-10 19:58:18 -07:00
|
|
|
/// A data type representing a payment plan.
|
2018-05-29 12:28:07 -07:00
|
|
|
#[repr(C)]
|
|
|
|
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
|
|
|
|
pub enum Budget {
|
2018-06-06 09:03:40 -07:00
|
|
|
/// Make a payment.
|
2018-05-29 12:28:07 -07:00
|
|
|
Pay(Payment),
|
2018-06-06 09:03:40 -07:00
|
|
|
|
|
|
|
/// Make a payment after some condition.
|
2018-05-29 12:28:07 -07:00
|
|
|
After(Condition, Payment),
|
2018-06-06 09:03:40 -07:00
|
|
|
|
|
|
|
/// Either make a payment after one condition or a different payment after another
|
|
|
|
/// condition, which ever condition is satisfied first.
|
2018-06-07 15:47:19 -07:00
|
|
|
Or((Condition, Payment), (Condition, Payment)),
|
2018-09-25 11:38:13 -07:00
|
|
|
|
|
|
|
/// Make a payment after both of two conditions are satisfied
|
|
|
|
And(Condition, Condition, Payment),
|
2018-05-29 12:28:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Budget {
|
2018-08-09 08:13:57 -07:00
|
|
|
/// Create the simplest budget - one that pays `tokens` to Pubkey.
|
|
|
|
pub fn new_payment(tokens: i64, to: Pubkey) -> Self {
|
2018-05-29 12:28:07 -07:00
|
|
|
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-08-09 08:13:57 -07:00
|
|
|
pub fn new_authorized_payment(from: Pubkey, tokens: i64, to: Pubkey) -> Self {
|
2018-05-29 12:28:07 -07:00
|
|
|
Budget::After(Condition::Signature(from), Payment { tokens, to })
|
|
|
|
}
|
|
|
|
|
2018-09-25 11:38:13 -07:00
|
|
|
/// Create a budget that pays tokens` to `to` after being witnessed by 2x `from`s
|
|
|
|
pub fn new_2_2_multisig_payment(from0: Pubkey, from1: Pubkey, tokens: i64, to: Pubkey) -> Self {
|
|
|
|
Budget::And(
|
|
|
|
Condition::Signature(from0),
|
|
|
|
Condition::Signature(from1),
|
|
|
|
Payment { tokens, to },
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-05-29 16:11:24 -07:00
|
|
|
/// Create a budget that pays `tokens` to `to` after the given DateTime.
|
2018-08-09 09:52:53 -07:00
|
|
|
pub fn new_future_payment(dt: DateTime<Utc>, from: Pubkey, tokens: i64, to: Pubkey) -> Self {
|
2018-07-08 20:04:55 -07:00
|
|
|
Budget::After(Condition::Timestamp(dt, from), Payment { tokens, to })
|
2018-05-29 12:28:07 -07:00
|
|
|
}
|
|
|
|
|
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>,
|
2018-08-09 08:13:57 -07:00
|
|
|
from: Pubkey,
|
2018-05-29 12:28:07 -07:00
|
|
|
tokens: i64,
|
2018-08-09 08:13:57 -07:00
|
|
|
to: Pubkey,
|
2018-05-29 12:28:07 -07:00
|
|
|
) -> Self {
|
2018-06-07 15:47:19 -07:00
|
|
|
Budget::Or(
|
2018-07-08 20:04:55 -07:00
|
|
|
(Condition::Timestamp(dt, from), Payment { tokens, to }),
|
2018-05-29 12:28:07 -07:00
|
|
|
(Condition::Signature(from), Payment { tokens, to: from }),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-05-29 16:11:24 -07:00
|
|
|
/// Return Payment if the budget requires no additional Witnesses.
|
2018-09-20 13:12:53 -07:00
|
|
|
pub 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-09-20 13:12:53 -07:00
|
|
|
pub fn verify(&self, spendable_tokens: i64) -> bool {
|
2018-06-04 16:17:23 -07:00
|
|
|
match self {
|
2018-09-25 11:38:13 -07:00
|
|
|
Budget::Pay(payment) | Budget::After(_, payment) | Budget::And(_, _, payment) => {
|
|
|
|
payment.tokens == spendable_tokens
|
|
|
|
}
|
2018-06-07 15:47:19 -07:00
|
|
|
Budget::Or(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-09-20 13:12:53 -07:00
|
|
|
pub fn apply_witness(&mut self, witness: &Witness, from: &Pubkey) {
|
2018-09-25 11:38:13 -07:00
|
|
|
let new_budget = match self {
|
|
|
|
Budget::After(cond, payment) if cond.is_satisfied(witness, from) => {
|
|
|
|
Some(Budget::Pay(payment.clone()))
|
|
|
|
}
|
|
|
|
Budget::Or((cond, payment), _) if cond.is_satisfied(witness, from) => {
|
|
|
|
Some(Budget::Pay(payment.clone()))
|
|
|
|
}
|
|
|
|
Budget::Or(_, (cond, payment)) if cond.is_satisfied(witness, from) => {
|
|
|
|
Some(Budget::Pay(payment.clone()))
|
|
|
|
}
|
|
|
|
Budget::And(cond0, cond1, payment) => {
|
|
|
|
if cond0.is_satisfied(witness, from) {
|
|
|
|
Some(Budget::After(cond1.clone(), payment.clone()))
|
|
|
|
} else if cond1.is_satisfied(witness, from) {
|
|
|
|
Some(Budget::After(cond0.clone(), payment.clone()))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2018-05-29 12:28:07 -07:00
|
|
|
_ => None,
|
2018-09-25 11:38:13 -07:00
|
|
|
};
|
|
|
|
if let Some(budget) = new_budget {
|
|
|
|
mem::replace(self, budget);
|
2018-05-29 12:28:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2018-08-09 07:56:04 -07:00
|
|
|
use signature::{Keypair, KeypairUtil};
|
2018-05-29 12:28:07 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_signature_satisfied() {
|
2018-08-09 08:13:57 -07:00
|
|
|
let from = Pubkey::default();
|
2018-07-08 20:04:55 -07:00
|
|
|
assert!(Condition::Signature(from).is_satisfied(&Witness::Signature, &from));
|
2018-05-29 12:28:07 -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-08-09 08:13:57 -07:00
|
|
|
let from = Pubkey::default();
|
2018-07-08 20:04:55 -07:00
|
|
|
assert!(Condition::Timestamp(dt1, from).is_satisfied(&Witness::Timestamp(dt1), &from));
|
|
|
|
assert!(Condition::Timestamp(dt1, from).is_satisfied(&Witness::Timestamp(dt2), &from));
|
|
|
|
assert!(!Condition::Timestamp(dt2, from).is_satisfied(&Witness::Timestamp(dt1), &from));
|
2018-05-29 12:28:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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);
|
2018-08-09 08:13:57 -07:00
|
|
|
let from = Pubkey::default();
|
|
|
|
let to = Pubkey::default();
|
2018-05-29 12:28:07 -07:00
|
|
|
assert!(Budget::new_payment(42, to).verify(42));
|
|
|
|
assert!(Budget::new_authorized_payment(from, 42, to).verify(42));
|
2018-07-08 20:04:55 -07:00
|
|
|
assert!(Budget::new_future_payment(dt, from, 42, to).verify(42));
|
2018-05-29 12:28:07 -07:00
|
|
|
assert!(Budget::new_cancelable_future_payment(dt, from, 42, to).verify(42));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_authorized_payment() {
|
2018-08-09 08:13:57 -07:00
|
|
|
let from = Pubkey::default();
|
|
|
|
let to = Pubkey::default();
|
2018-05-29 12:28:07 -07:00
|
|
|
|
2018-05-29 16:11:24 -07:00
|
|
|
let mut budget = Budget::new_authorized_payment(from, 42, to);
|
2018-07-08 20:04:55 -07:00
|
|
|
budget.apply_witness(&Witness::Signature, &from);
|
2018-05-29 16:11:24 -07:00
|
|
|
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);
|
2018-08-09 07:56:04 -07:00
|
|
|
let from = Keypair::new().pubkey();
|
|
|
|
let to = Keypair::new().pubkey();
|
2018-05-29 12:28:07 -07:00
|
|
|
|
2018-07-08 20:04:55 -07:00
|
|
|
let mut budget = Budget::new_future_payment(dt, from, 42, to);
|
|
|
|
budget.apply_witness(&Witness::Timestamp(dt), &from);
|
2018-05-29 16:11:24 -07:00
|
|
|
assert_eq!(budget, Budget::new_payment(42, to));
|
2018-05-29 12:28:07 -07:00
|
|
|
}
|
|
|
|
|
2018-07-08 20:04:55 -07:00
|
|
|
#[test]
|
|
|
|
fn test_unauthorized_future_payment() {
|
|
|
|
// Ensure timestamp will only be acknowledged if it came from the
|
|
|
|
// whitelisted public key.
|
|
|
|
let dt = Utc.ymd(2014, 11, 14).and_hms(8, 9, 10);
|
2018-08-09 07:56:04 -07:00
|
|
|
let from = Keypair::new().pubkey();
|
|
|
|
let to = Keypair::new().pubkey();
|
2018-07-08 20:04:55 -07:00
|
|
|
|
|
|
|
let mut budget = Budget::new_future_payment(dt, from, 42, to);
|
|
|
|
let orig_budget = budget.clone();
|
|
|
|
budget.apply_witness(&Witness::Timestamp(dt), &to); // <-- Attack!
|
|
|
|
assert_eq!(budget, orig_budget);
|
|
|
|
}
|
|
|
|
|
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);
|
2018-08-09 08:13:57 -07:00
|
|
|
let from = Pubkey::default();
|
|
|
|
let to = Pubkey::default();
|
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);
|
2018-07-08 20:04:55 -07:00
|
|
|
budget.apply_witness(&Witness::Timestamp(dt), &from);
|
2018-05-29 16:11:24 -07:00
|
|
|
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);
|
2018-07-08 20:04:55 -07:00
|
|
|
budget.apply_witness(&Witness::Signature, &from);
|
2018-05-29 16:11:24 -07:00
|
|
|
assert_eq!(budget, Budget::new_payment(42, from));
|
2018-05-29 12:28:07 -07:00
|
|
|
}
|
2018-09-25 11:38:13 -07:00
|
|
|
#[test]
|
|
|
|
fn test_2_2_multisig_payment() {
|
|
|
|
let from0 = Keypair::new().pubkey();
|
|
|
|
let from1 = Keypair::new().pubkey();
|
|
|
|
let to = Pubkey::default();
|
|
|
|
|
|
|
|
let mut budget = Budget::new_2_2_multisig_payment(from0, from1, 42, to);
|
|
|
|
budget.apply_witness(&Witness::Signature, &from0);
|
|
|
|
assert_eq!(budget, Budget::new_authorized_payment(from1, 42, to));
|
|
|
|
}
|
2018-05-29 12:28:07 -07:00
|
|
|
}
|