Files
solana/src/plan.rs

193 lines
5.7 KiB
Rust
Raw Normal View History

2018-03-17 14:42:43 -06: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),
}
pub enum Witness {
Timestamp(DateTime<Utc>),
Signature(PublicKey),
}
impl Condition {
pub fn is_satisfied(&self, event: &Witness) -> bool {
match (self, event) {
(&Condition::Signature(ref pubkey), &Witness::Signature(ref from)) => pubkey == from,
(&Condition::Timestamp(ref dt), &Witness::Timestamp(ref last_time)) => dt <= last_time,
_ => false,
}
}
}
2018-03-17 14:42:43 -06:00
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub enum Action {
Pay(Payment),
2018-03-17 14:42:43 -06:00
}
impl Action {
pub fn spendable(&self) -> i64 {
2018-03-17 14:42:43 -06:00
match *self {
2018-03-19 10:03:41 -06:00
Action::Pay(ref payment) => payment.tokens,
2018-03-17 14:42:43 -06:00
}
}
}
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct Payment {
2018-03-19 10:03:41 -06:00
pub tokens: i64,
2018-03-17 14:42:43 -06:00
pub to: PublicKey,
}
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub enum Plan {
Action(Action),
After(Condition, Action),
Race((Condition, Action), (Condition, Action)),
2018-03-17 14:42:43 -06:00
}
impl Plan {
2018-03-19 10:03:41 -06:00
pub fn new_payment(tokens: i64, to: PublicKey) -> Self {
Plan::Action(Action::Pay(Payment { tokens, to }))
2018-03-18 21:02:28 -06:00
}
2018-03-19 10:03:41 -06:00
pub fn new_authorized_payment(from: PublicKey, tokens: i64, to: PublicKey) -> Self {
2018-03-18 21:02:28 -06:00
Plan::After(
Condition::Signature(from),
2018-03-19 10:03:41 -06:00
Action::Pay(Payment { tokens, to }),
2018-03-18 21:02:28 -06:00
)
}
2018-03-19 10:03:41 -06:00
pub fn new_future_payment(dt: DateTime<Utc>, tokens: i64, to: PublicKey) -> Self {
Plan::After(
Condition::Timestamp(dt),
Action::Pay(Payment { tokens, to }),
)
2018-03-18 21:02:28 -06:00
}
pub fn new_cancelable_future_payment(
dt: DateTime<Utc>,
from: PublicKey,
2018-03-19 10:03:41 -06:00
tokens: i64,
2018-03-18 21:02:28 -06:00
to: PublicKey,
) -> Self {
Plan::Race(
2018-03-19 10:03:41 -06:00
(
Condition::Timestamp(dt),
Action::Pay(Payment { tokens, to }),
),
2018-03-18 21:02:28 -06:00
(
Condition::Signature(from),
2018-03-19 10:03:41 -06:00
Action::Pay(Payment { tokens, to: from }),
2018-03-18 21:02:28 -06:00
),
)
}
2018-03-19 10:03:41 -06:00
pub fn verify(&self, spendable_tokens: i64) -> bool {
2018-03-17 14:42:43 -06:00
match *self {
2018-03-19 10:03:41 -06:00
Plan::Action(ref action) => action.spendable() == spendable_tokens,
Plan::After(_, ref action) => action.spendable() == spendable_tokens,
Plan::Race(ref a, ref b) => {
2018-03-19 10:03:41 -06:00
a.1.spendable() == spendable_tokens && b.1.spendable() == spendable_tokens
2018-03-17 14:42:43 -06:00
}
}
}
pub fn process_witness(&mut self, event: Witness) -> bool {
let mut new_action = None;
2018-03-17 14:42:43 -06:00
match *self {
Plan::Action(_) => return true,
Plan::After(ref cond, ref action) => {
if cond.is_satisfied(&event) {
new_action = Some(action.clone());
2018-03-17 14:42:43 -06: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 14:42:43 -06:00
}
}
}
if let Some(action) = new_action {
mem::replace(self, Plan::Action(action));
2018-03-17 14:42:43 -06:00
true
} else {
false
}
}
}
2018-03-18 21:02:28 -06:00
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_signature_satisfied() {
let sig = PublicKey::default();
assert!(Condition::Signature(sig).is_satisfied(&Witness::Signature(sig)));
2018-03-18 21:02:28 -06: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);
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 21:02:28 -06: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);
assert!(plan.process_witness(Witness::Signature(from)));
2018-03-18 21:02:28 -06: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);
assert!(plan.process_witness(Witness::Timestamp(dt)));
2018-03-18 21:02:28 -06: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);
assert!(plan.process_witness(Witness::Timestamp(dt)));
2018-03-18 21:02:28 -06:00
assert_eq!(plan, Plan::new_payment(42, to));
let mut plan = Plan::new_cancelable_future_payment(dt, from, 42, to);
assert!(plan.process_witness(Witness::Signature(from)));
2018-03-18 21:02:28 -06:00
assert_eq!(plan, Plan::new_payment(42, from));
}
}