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 {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|