solana/src/payment_plan.rs

49 lines
1.6 KiB
Rust
Raw Normal View History

//! The `plan` module provides a domain-specific language for payment plans. Users create Budget objects that
2018-05-25 14:51:41 -07:00
//! are given to an interpreter. The interpreter listens for `Witness` transactions,
2018-03-20 14:52:46 -07:00
//! which it uses to reduce the payment plan. When the plan is reduced to a
//! `Payment`, the payment is executed.
2018-03-17 13:42:43 -07:00
use chrono::prelude::*;
use signature::PublicKey;
2018-03-17 13:42:43 -07:00
2018-05-22 20:42:04 -07:00
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
2018-03-20 14:31:28 -07:00
pub enum Witness {
2018-03-17 13:42:43 -07:00
Timestamp(DateTime<Utc>),
Signature(PublicKey),
}
2018-03-20 14:31:28 -07:00
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub enum Condition {
Timestamp(DateTime<Utc>),
Signature(PublicKey),
}
impl Condition {
2018-03-29 11:20:54 -07:00
/// Return true if the given Witness satisfies this Condition.
2018-03-20 15:53:41 -07:00
pub fn is_satisfied(&self, witness: &Witness) -> bool {
match (self, witness) {
(&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 13:42:43 -07:00
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct Payment {
2018-03-19 09:03:41 -07:00
pub tokens: i64,
2018-03-17 13:42:43 -07:00
pub to: PublicKey,
}
2018-05-29 12:00:44 -07:00
pub trait PaymentPlan {
/// Return Payment if the spending plan requires no additional Witnesses.
fn final_payment(&self) -> Option<Payment>;
/// Return true if the plan spends exactly `spendable_tokens`.
fn verify(&self, spendable_tokens: i64) -> bool;
/// Apply a witness to the spending plan to see if the plan can be reduced.
/// If so, modify the plan in-place.
fn apply_witness(&mut self, witness: &Witness);
}