solana/programs/budget_api/src/budget_expr.rs

314 lines
11 KiB
Rust
Raw Normal View History

//! The `budget_expr` module provides a domain-specific language for pa&yment plans. Users create BudgetExpr objects that
2018-05-29 12:28:07 -07:00
//! 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.
2018-12-14 20:39:10 -08:00
use crate::payment_plan::{Payment, Witness};
2018-05-29 12:28:07 -07:00
use chrono::prelude::*;
2019-03-02 13:23:22 -08:00
use serde_derive::{Deserialize, Serialize};
use solana_sdk::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`.
Timestamp(DateTime<Utc>, Pubkey),
2018-06-06 09:03:40 -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.
pub fn is_satisfied(&self, witness: &Witness, from: &Pubkey) -> bool {
2018-05-29 12:28:07 -07:00
match (self, witness) {
(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)]
2018-11-02 19:13:33 -07:00
pub enum BudgetExpr {
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.
After(Condition, Box<BudgetExpr>),
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.
Or((Condition, Box<BudgetExpr>), (Condition, Box<BudgetExpr>)),
/// Make a payment after both of two conditions are satisfied
And(Condition, Condition, Box<BudgetExpr>),
2018-05-29 12:28:07 -07:00
}
2018-11-02 19:13:33 -07:00
impl BudgetExpr {
2019-03-05 17:27:25 -08:00
/// Create the simplest budget - one that pays `lamports` to Pubkey.
pub fn new_payment(lamports: u64, to: &Pubkey) -> Self {
BudgetExpr::Pay(Payment { lamports, to: *to })
2018-05-29 12:28:07 -07:00
}
2019-03-05 17:27:25 -08:00
/// Create a budget that pays `lamports` to `to` after being witnessed by `from`.
pub fn new_authorized_payment(from: &Pubkey, lamports: u64, to: &Pubkey) -> Self {
BudgetExpr::After(
Condition::Signature(*from),
2019-03-05 17:27:25 -08:00
Box::new(Self::new_payment(lamports, to)),
)
2018-05-29 12:28:07 -07:00
}
/// Create a budget that pays `lamports` to `to` after being witnessed by `witness` unless
/// canceled with a signature from `from`.
pub fn new_cancelable_authorized_payment(
witness: &Pubkey,
lamports: u64,
to: &Pubkey,
from: &Pubkey,
) -> Self {
BudgetExpr::Or(
(
Condition::Signature(*witness),
Box::new(BudgetExpr::new_payment(lamports, to)),
),
(
Condition::Signature(*from),
Box::new(BudgetExpr::new_payment(lamports, from)),
),
)
}
2018-05-29 12:28:07 -07:00
2019-03-05 17:27:25 -08:00
/// Create a budget that pays lamports` to `to` after being witnessed by 2x `from`s
pub fn new_2_2_multisig_payment(
from0: &Pubkey,
from1: &Pubkey,
2019-03-05 17:27:25 -08:00
lamports: u64,
to: &Pubkey,
2019-03-05 17:27:25 -08:00
) -> Self {
2018-11-02 19:13:33 -07:00
BudgetExpr::And(
Condition::Signature(*from0),
Condition::Signature(*from1),
2019-03-05 17:27:25 -08:00
Box::new(Self::new_payment(lamports, to)),
)
}
/// Create a budget that pays `lamports` to `to` after the given DateTime signed
/// by `dt_pubkey`.
pub fn new_future_payment(
dt: DateTime<Utc>,
dt_pubkey: &Pubkey,
lamports: u64,
to: &Pubkey,
) -> Self {
BudgetExpr::After(
Condition::Timestamp(dt, *dt_pubkey),
2019-03-05 17:27:25 -08:00
Box::new(Self::new_payment(lamports, to)),
)
2018-05-29 12:28:07 -07:00
}
2019-03-05 17:27:25 -08:00
/// Create a budget that pays `lamports` to `to` after the given DateTime
/// signed by `dt_pubkey` unless canceled by `from`.
2018-05-29 12:28:07 -07:00
pub fn new_cancelable_future_payment(
dt: DateTime<Utc>,
dt_pubkey: &Pubkey,
2019-03-05 17:27:25 -08:00
lamports: u64,
to: &Pubkey,
from: &Pubkey,
2018-05-29 12:28:07 -07:00
) -> Self {
2018-11-02 19:13:33 -07:00
BudgetExpr::Or(
(
Condition::Timestamp(dt, *dt_pubkey),
2019-03-05 17:27:25 -08:00
Box::new(Self::new_payment(lamports, to)),
),
(
Condition::Signature(*from),
Box::new(Self::new_payment(lamports, from)),
),
2018-05-29 12:28:07 -07:00
)
}
/// Return Payment if the budget requires no additional Witnesses.
pub fn final_payment(&self) -> Option<Payment> {
match self {
2018-11-02 19:13:33 -07:00
BudgetExpr::Pay(payment) => Some(payment.clone()),
2018-05-29 12:28:07 -07:00
_ => None,
}
}
2019-03-05 17:27:25 -08:00
/// Return true if the budget spends exactly `spendable_lamports`.
pub fn verify(&self, spendable_lamports: u64) -> bool {
match self {
2019-03-05 17:27:25 -08:00
BudgetExpr::Pay(payment) => payment.lamports == spendable_lamports,
BudgetExpr::After(_, sub_expr) | BudgetExpr::And(_, _, sub_expr) => {
2019-03-05 17:27:25 -08:00
sub_expr.verify(spendable_lamports)
}
BudgetExpr::Or(a, b) => {
a.1.verify(spendable_lamports) && b.1.verify(spendable_lamports)
}
2018-05-29 12:28:07 -07:00
}
}
/// Apply a witness to the budget to see if the budget can be reduced.
/// If so, modify the budget in-place.
pub fn apply_witness(&mut self, witness: &Witness, from: &Pubkey) {
2018-11-02 19:13:33 -07:00
let new_expr = match self {
BudgetExpr::After(cond, sub_expr) if cond.is_satisfied(witness, from) => {
Some(sub_expr.clone())
}
BudgetExpr::Or((cond, sub_expr), _) if cond.is_satisfied(witness, from) => {
Some(sub_expr.clone())
}
BudgetExpr::Or(_, (cond, sub_expr)) if cond.is_satisfied(witness, from) => {
Some(sub_expr.clone())
}
BudgetExpr::And(cond0, cond1, sub_expr) => {
if cond0.is_satisfied(witness, from) {
Some(Box::new(BudgetExpr::After(cond1.clone(), sub_expr.clone())))
} else if cond1.is_satisfied(witness, from) {
Some(Box::new(BudgetExpr::After(cond0.clone(), sub_expr.clone())))
} else {
None
}
}
2018-05-29 12:28:07 -07:00
_ => None,
};
2018-11-02 19:13:33 -07:00
if let Some(expr) = new_expr {
mem::replace(self, *expr);
2018-05-29 12:28:07 -07:00
}
}
}
#[cfg(test)]
mod tests {
use super::*;
2019-03-02 13:23:22 -08:00
use solana_sdk::signature::{Keypair, KeypairUtil};
2018-05-29 12:28:07 -07:00
#[test]
fn test_signature_satisfied() {
let from = Pubkey::default();
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);
let from = Pubkey::default();
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]
fn test_verify() {
2018-05-29 12:28:07 -07:00
let dt = Utc.ymd(2014, 11, 14).and_hms(8, 9, 10);
let from = Pubkey::default();
let to = Pubkey::default();
assert!(BudgetExpr::new_payment(42, &to).verify(42));
assert!(BudgetExpr::new_authorized_payment(&from, 42, &to).verify(42));
assert!(BudgetExpr::new_future_payment(dt, &from, 42, &to).verify(42));
assert!(BudgetExpr::new_cancelable_future_payment(dt, &from, 42, &to, &from).verify(42));
2018-05-29 12:28:07 -07:00
}
#[test]
fn test_authorized_payment() {
let from = Pubkey::default();
let to = Pubkey::default();
2018-05-29 12:28:07 -07:00
let mut expr = BudgetExpr::new_authorized_payment(&from, 42, &to);
2018-11-02 19:13:33 -07:00
expr.apply_witness(&Witness::Signature, &from);
assert_eq!(expr, BudgetExpr::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
let mut expr = BudgetExpr::new_future_payment(dt, &from, 42, &to);
2018-11-02 19:13:33 -07:00
expr.apply_witness(&Witness::Timestamp(dt), &from);
assert_eq!(expr, BudgetExpr::new_payment(42, &to));
2018-05-29 12:28:07 -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();
let mut expr = BudgetExpr::new_future_payment(dt, &from, 42, &to);
2018-11-02 19:13:33 -07:00
let orig_expr = expr.clone();
expr.apply_witness(&Witness::Timestamp(dt), &to); // <-- Attack!
assert_eq!(expr, orig_expr);
}
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);
let from = Pubkey::default();
let to = Pubkey::default();
2018-05-29 12:28:07 -07:00
let mut expr = BudgetExpr::new_cancelable_future_payment(dt, &from, 42, &to, &from);
2018-11-02 19:13:33 -07:00
expr.apply_witness(&Witness::Timestamp(dt), &from);
assert_eq!(expr, BudgetExpr::new_payment(42, &to));
2018-05-29 12:28:07 -07:00
let mut expr = BudgetExpr::new_cancelable_future_payment(dt, &from, 42, &to, &from);
2018-11-02 19:13:33 -07:00
expr.apply_witness(&Witness::Signature, &from);
assert_eq!(expr, BudgetExpr::new_payment(42, &from));
2018-05-29 12:28:07 -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 expr = BudgetExpr::new_2_2_multisig_payment(&from0, &from1, 42, &to);
2018-11-02 19:13:33 -07:00
expr.apply_witness(&Witness::Signature, &from0);
assert_eq!(expr, BudgetExpr::new_authorized_payment(&from1, 42, &to));
}
#[test]
fn test_multisig_after_sig() {
let from0 = Keypair::new().pubkey();
let from1 = Keypair::new().pubkey();
let from2 = Keypair::new().pubkey();
let to = Pubkey::default();
let expr = BudgetExpr::new_2_2_multisig_payment(&from0, &from1, 42, &to);
let mut expr = BudgetExpr::After(Condition::Signature(from2), Box::new(expr));
expr.apply_witness(&Witness::Signature, &from2);
expr.apply_witness(&Witness::Signature, &from0);
assert_eq!(expr, BudgetExpr::new_authorized_payment(&from1, 42, &to));
}
#[test]
fn test_multisig_after_ts() {
let from0 = Keypair::new().pubkey();
let from1 = Keypair::new().pubkey();
let dt = Utc.ymd(2014, 11, 11).and_hms(7, 7, 7);
let to = Pubkey::default();
let expr = BudgetExpr::new_2_2_multisig_payment(&from0, &from1, 42, &to);
let mut expr = BudgetExpr::After(Condition::Timestamp(dt, from0), Box::new(expr));
expr.apply_witness(&Witness::Timestamp(dt), &from0);
assert_eq!(
expr,
BudgetExpr::new_2_2_multisig_payment(&from0, &from1, 42, &to)
);
expr.apply_witness(&Witness::Signature, &from0);
assert_eq!(expr, BudgetExpr::new_authorized_payment(&from1, 42, &to));
}
2018-05-29 12:28:07 -07:00
}