2018-03-17 13:42:43 -07:00
|
|
|
//! The `plan` crate provides functionality for creating spending plans.
|
|
|
|
|
|
|
|
use signature::PublicKey;
|
|
|
|
use chrono::prelude::*;
|
|
|
|
use std::mem;
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
|
|
|
|
pub enum Condition {
|
|
|
|
Timestamp(DateTime<Utc>),
|
|
|
|
Signature(PublicKey),
|
|
|
|
}
|
|
|
|
|
2018-03-17 13:42:58 -07:00
|
|
|
pub enum PlanEvent {
|
|
|
|
Timestamp(DateTime<Utc>),
|
|
|
|
Signature(PublicKey),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Condition {
|
|
|
|
pub fn is_satisfied(&self, event: &PlanEvent) -> bool {
|
|
|
|
match (self, event) {
|
|
|
|
(&Condition::Signature(ref pubkey), &PlanEvent::Signature(ref from)) => pubkey == from,
|
|
|
|
(&Condition::Timestamp(ref dt), &PlanEvent::Timestamp(ref last_time)) => {
|
|
|
|
dt <= last_time
|
|
|
|
}
|
|
|
|
_ => 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 enum Action {
|
|
|
|
Pay(Payment),
|
2018-03-17 13:42:43 -07:00
|
|
|
}
|
|
|
|
|
2018-03-17 13:42:50 -07:00
|
|
|
impl Action {
|
|
|
|
pub fn spendable(&self) -> i64 {
|
2018-03-17 13:42:43 -07:00
|
|
|
match *self {
|
2018-03-17 13:42:58 -07:00
|
|
|
Action::Pay(ref payment) => payment.asset,
|
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 {
|
|
|
|
pub asset: 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 {
|
|
|
|
Action(Action),
|
2018-03-17 13:42:58 -07:00
|
|
|
After(Condition, Action),
|
|
|
|
Race((Condition, Action), (Condition, Action)),
|
2018-03-17 13:42:43 -07:00
|
|
|
}
|
|
|
|
|
2018-03-17 13:42:50 -07:00
|
|
|
impl Plan {
|
2018-03-18 20:02:28 -07:00
|
|
|
pub fn new_payment(asset: i64, to: PublicKey) -> Self {
|
|
|
|
Plan::Action(Action::Pay(Payment { asset, to }))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_authorized_payment(from: PublicKey, asset: i64, to: PublicKey) -> Self {
|
|
|
|
Plan::After(
|
|
|
|
Condition::Signature(from),
|
|
|
|
Action::Pay(Payment { asset, to }),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_future_payment(dt: DateTime<Utc>, asset: i64, to: PublicKey) -> Self {
|
|
|
|
Plan::After(Condition::Timestamp(dt), Action::Pay(Payment { asset, to }))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_cancelable_future_payment(
|
|
|
|
dt: DateTime<Utc>,
|
|
|
|
from: PublicKey,
|
|
|
|
asset: i64,
|
|
|
|
to: PublicKey,
|
|
|
|
) -> Self {
|
|
|
|
Plan::Race(
|
|
|
|
(Condition::Timestamp(dt), Action::Pay(Payment { asset, to })),
|
|
|
|
(
|
|
|
|
Condition::Signature(from),
|
|
|
|
Action::Pay(Payment { asset, to: from }),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-03-17 13:42:50 -07:00
|
|
|
pub fn verify(&self, spendable_assets: i64) -> bool {
|
2018-03-17 13:42:43 -07:00
|
|
|
match *self {
|
2018-03-17 13:42:50 -07:00
|
|
|
Plan::Action(ref action) => action.spendable() == spendable_assets,
|
2018-03-17 13:42:58 -07:00
|
|
|
Plan::After(_, ref action) => action.spendable() == spendable_assets,
|
|
|
|
Plan::Race(ref a, ref b) => {
|
|
|
|
a.1.spendable() == spendable_assets && b.1.spendable() == spendable_assets
|
2018-03-17 13:42:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-17 13:42:58 -07:00
|
|
|
pub fn process_event(&mut self, event: PlanEvent) -> bool {
|
|
|
|
let mut new_action = None;
|
2018-03-17 13:42:43 -07:00
|
|
|
match *self {
|
|
|
|
Plan::Action(_) => return true,
|
2018-03-17 13:42:58 -07:00
|
|
|
Plan::After(ref cond, ref action) => {
|
|
|
|
if cond.is_satisfied(&event) {
|
|
|
|
new_action = Some(action.clone());
|
2018-03-17 13:42:43 -07:00
|
|
|
}
|
|
|
|
}
|
2018-03-17 13:42:58 -07:00
|
|
|
Plan::Race(ref a, ref b) => {
|
|
|
|
if a.0.is_satisfied(&event) {
|
|
|
|
new_action = Some(a.1.clone());
|
|
|
|
} else if b.0.is_satisfied(&event) {
|
|
|
|
new_action = Some(b.1.clone());
|
2018-03-17 13:42:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-17 13:42:58 -07:00
|
|
|
if let Some(action) = new_action {
|
|
|
|
mem::replace(self, Plan::Action(action));
|
2018-03-17 13:42:43 -07:00
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-18 20:02:28 -07:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_signature_satisfied() {
|
|
|
|
let sig = PublicKey::default();
|
|
|
|
assert!(Condition::Signature(sig).is_satisfied(&PlanEvent::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(&PlanEvent::Timestamp(dt1)));
|
|
|
|
assert!(Condition::Timestamp(dt1).is_satisfied(&PlanEvent::Timestamp(dt2)));
|
|
|
|
assert!(!Condition::Timestamp(dt2).is_satisfied(&PlanEvent::Timestamp(dt1)));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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);
|
|
|
|
assert!(plan.process_event(PlanEvent::Signature(from)));
|
|
|
|
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);
|
|
|
|
assert!(plan.process_event(PlanEvent::Timestamp(dt)));
|
|
|
|
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);
|
|
|
|
assert!(plan.process_event(PlanEvent::Timestamp(dt)));
|
|
|
|
assert_eq!(plan, Plan::new_payment(42, to));
|
|
|
|
|
|
|
|
let mut plan = Plan::new_cancelable_future_payment(dt, from, 42, to);
|
|
|
|
assert!(plan.process_event(PlanEvent::Signature(from)));
|
|
|
|
assert_eq!(plan, Plan::new_payment(42, from));
|
|
|
|
}
|
|
|
|
}
|