use chrono::prelude::*;
use serde_derive::{Deserialize, Serialize};
use solana_sdk::hash::Hash;
use solana_sdk::pubkey::Pubkey;
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub enum Witness {
Timestamp(DateTime<Utc>),
Signature,
AccountData(Hash, Pubkey),
}
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct Payment {
pub lamports: u64,
pub to: Pubkey,
}
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct AccountConstraints {
pub key: Pubkey,
pub program_id: Pubkey,
pub data_hash: Hash,
}
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub enum Condition {
Timestamp(DateTime<Utc>, Pubkey),
Signature(Pubkey),
AccountData(AccountConstraints),
}
impl Condition {
pub fn is_satisfied(&self, witness: &Witness, from: &Pubkey) -> bool {
match (self, witness) {
(Condition::Signature(pubkey), Witness::Signature) => pubkey == from,
(Condition::Timestamp(dt, pubkey), Witness::Timestamp(last_time)) => {
pubkey == from && dt <= last_time
}
(
Condition::AccountData(constraints),
Witness::AccountData(actual_hash, program_id),
) => {
constraints.program_id == *program_id
&& constraints.key == *from
&& constraints.data_hash == *actual_hash
}
_ => false,
}
}
}
#[repr(C)]
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub enum BudgetExpr {
Pay(Payment),
After(Condition, Box<BudgetExpr>),
Or((Condition, Box<BudgetExpr>), (Condition, Box<BudgetExpr>)),
And(Condition, Condition, Box<BudgetExpr>),
}
impl BudgetExpr {
pub fn new_payment(lamports: u64, to: &Pubkey) -> Self {
BudgetExpr::Pay(Payment { lamports, to: *to })
}
pub fn new_authorized_payment(from: &Pubkey, lamports: u64, to: &Pubkey) -> Self {
BudgetExpr::After(
Condition::Signature(*from),
Box::new(Self::new_payment(lamports, to)),
)
}
pub fn new_payment_when_account_data(
account_pubkey: &Pubkey,
account_program_id: &Pubkey,
account_hash: Hash,
lamports: u64,
to: &Pubkey,
) -> Self {
BudgetExpr::After(
Condition::AccountData(AccountConstraints {
key: *account_pubkey,
program_id: *account_program_id,
data_hash: account_hash,
}),
Box::new(Self::new_payment(lamports, to)),
)
}
pub fn new_cancelable_authorized_payment(
witness: &Pubkey,
lamports: u64,
to: &Pubkey,
from: Option<Pubkey>,
) -> Self {
if from.is_none() {
return Self::new_authorized_payment(witness, lamports, to);
}
let from = from.unwrap();
BudgetExpr::Or(
(
Condition::Signature(*witness),
Box::new(BudgetExpr::new_payment(lamports, to)),
),
(
Condition::Signature(from),
Box::new(BudgetExpr::new_payment(lamports, &from)),
),
)
}
pub fn new_2_2_multisig_payment(
from0: &Pubkey,
from1: &Pubkey,
lamports: u64,
to: &Pubkey,
) -> Self {
BudgetExpr::And(
Condition::Signature(*from0),
Condition::Signature(*from1),
Box::new(Self::new_payment(lamports, to)),
)
}
pub fn new_future_payment(
dt: DateTime<Utc>,
dt_pubkey: &Pubkey,
lamports: u64,
to: &Pubkey,
) -> Self {
BudgetExpr::After(
Condition::Timestamp(dt, *dt_pubkey),
Box::new(Self::new_payment(lamports, to)),
)
}
pub fn new_cancelable_future_payment(
dt: DateTime<Utc>,
dt_pubkey: &Pubkey,
lamports: u64,
to: &Pubkey,
from: Option<Pubkey>,
) -> Self {
if from.is_none() {
return Self::new_future_payment(dt, dt_pubkey, lamports, to);
}
let from = from.unwrap();
BudgetExpr::Or(
(
Condition::Timestamp(dt, *dt_pubkey),
Box::new(Self::new_payment(lamports, to)),
),
(
Condition::Signature(from),
Box::new(Self::new_payment(lamports, &from)),
),
)
}
pub fn final_payment(&self) -> Option<Payment> {
match self {
BudgetExpr::Pay(payment) => Some(payment.clone()),
_ => None,
}
}
pub fn verify(&self, spendable_lamports: u64) -> bool {
match self {
BudgetExpr::Pay(payment) => payment.lamports == spendable_lamports,
BudgetExpr::After(_, sub_expr) | BudgetExpr::And(_, _, sub_expr) => {
sub_expr.verify(spendable_lamports)
}
BudgetExpr::Or(a, b) => {
a.1.verify(spendable_lamports) && b.1.verify(spendable_lamports)
}
}
}
pub fn apply_witness(&mut self, witness: &Witness, from: &Pubkey) {
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
}
}
_ => None,
};
if let Some(expr) = new_expr {
*self = *expr;
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_signature_satisfied() {
let from = Pubkey::default();
assert!(Condition::Signature(from).is_satisfied(&Witness::Signature, &from));
}
#[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));
}
#[test]
fn test_verify() {
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, Some(from)).verify(42)
);
}
#[test]
fn test_authorized_payment() {
let from = Pubkey::default();
let to = Pubkey::default();
let mut expr = BudgetExpr::new_authorized_payment(&from, 42, &to);
expr.apply_witness(&Witness::Signature, &from);
assert_eq!(expr, BudgetExpr::new_payment(42, &to));
}
#[test]
fn test_future_payment() {
let dt = Utc.ymd(2014, 11, 14).and_hms(8, 9, 10);
let from = solana_sdk::pubkey::new_rand();
let to = solana_sdk::pubkey::new_rand();
let mut expr = BudgetExpr::new_future_payment(dt, &from, 42, &to);
expr.apply_witness(&Witness::Timestamp(dt), &from);
assert_eq!(expr, BudgetExpr::new_payment(42, &to));
}
#[test]
fn test_unauthorized_future_payment() {
let dt = Utc.ymd(2014, 11, 14).and_hms(8, 9, 10);
let from = solana_sdk::pubkey::new_rand();
let to = solana_sdk::pubkey::new_rand();
let mut expr = BudgetExpr::new_future_payment(dt, &from, 42, &to);
let orig_expr = expr.clone();
expr.apply_witness(&Witness::Timestamp(dt), &to);
assert_eq!(expr, orig_expr);
}
#[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();
let mut expr = BudgetExpr::new_cancelable_future_payment(dt, &from, 42, &to, Some(from));
expr.apply_witness(&Witness::Timestamp(dt), &from);
assert_eq!(expr, BudgetExpr::new_payment(42, &to));
let mut expr = BudgetExpr::new_cancelable_future_payment(dt, &from, 42, &to, Some(from));
expr.apply_witness(&Witness::Signature, &from);
assert_eq!(expr, BudgetExpr::new_payment(42, &from));
}
#[test]
fn test_2_2_multisig_payment() {
let from0 = solana_sdk::pubkey::new_rand();
let from1 = solana_sdk::pubkey::new_rand();
let to = Pubkey::default();
let mut 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));
}
#[test]
fn test_multisig_after_sig() {
let from0 = solana_sdk::pubkey::new_rand();
let from1 = solana_sdk::pubkey::new_rand();
let from2 = solana_sdk::pubkey::new_rand();
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 = solana_sdk::pubkey::new_rand();
let from1 = solana_sdk::pubkey::new_rand();
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));
}
}