Punt on the Script abstraction
Low ROI
This commit is contained in:
@@ -70,16 +70,20 @@ impl BudgetExpr {
|
||||
witness: &Pubkey,
|
||||
lamports: u64,
|
||||
to: &Pubkey,
|
||||
from: &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)),
|
||||
Condition::Signature(from),
|
||||
Box::new(BudgetExpr::new_payment(lamports, &from)),
|
||||
),
|
||||
)
|
||||
}
|
||||
@@ -119,16 +123,20 @@ impl BudgetExpr {
|
||||
dt_pubkey: &Pubkey,
|
||||
lamports: u64,
|
||||
to: &Pubkey,
|
||||
from: &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)),
|
||||
Condition::Signature(from),
|
||||
Box::new(Self::new_payment(lamports, &from)),
|
||||
),
|
||||
)
|
||||
}
|
||||
@@ -213,7 +221,9 @@ mod tests {
|
||||
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));
|
||||
assert!(
|
||||
BudgetExpr::new_cancelable_future_payment(dt, &from, 42, &to, Some(from)).verify(42)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -257,11 +267,11 @@ mod tests {
|
||||
let from = Pubkey::default();
|
||||
let to = Pubkey::default();
|
||||
|
||||
let mut expr = BudgetExpr::new_cancelable_future_payment(dt, &from, 42, &to, &from);
|
||||
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, &from);
|
||||
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));
|
||||
}
|
||||
|
@@ -1,9 +1,13 @@
|
||||
use crate::budget_expr::BudgetExpr;
|
||||
use crate::budget_state::BudgetState;
|
||||
use crate::id;
|
||||
use bincode::serialized_size;
|
||||
use chrono::prelude::{DateTime, Utc};
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use solana_sdk::instruction::{AccountMeta, Instruction};
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
use solana_sdk::signature::{Keypair, KeypairUtil};
|
||||
use solana_sdk::system_instruction::SystemInstruction;
|
||||
|
||||
/// A smart contract.
|
||||
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
|
||||
@@ -63,4 +67,88 @@ impl BudgetInstruction {
|
||||
}
|
||||
Instruction::new(id(), &BudgetInstruction::ApplySignature, account_metas)
|
||||
}
|
||||
|
||||
pub fn new_account(
|
||||
from: &Pubkey,
|
||||
contract: &Pubkey,
|
||||
lamports: u64,
|
||||
expr: BudgetExpr,
|
||||
) -> Vec<Instruction> {
|
||||
if !expr.verify(lamports) {
|
||||
panic!("invalid budget expression");
|
||||
}
|
||||
let space = serialized_size(&BudgetState::new(expr.clone())).unwrap();
|
||||
vec![
|
||||
SystemInstruction::new_program_account(&from, contract, lamports, space, &id()),
|
||||
BudgetInstruction::new_initialize_account(contract, expr),
|
||||
]
|
||||
}
|
||||
|
||||
/// Create a new payment script.
|
||||
pub fn new_payment(from: &Pubkey, to: &Pubkey, lamports: u64) -> Vec<Instruction> {
|
||||
let contract = Keypair::new().pubkey();
|
||||
let expr = BudgetExpr::new_payment(lamports, to);
|
||||
Self::new_account(from, &contract, lamports, expr)
|
||||
}
|
||||
|
||||
/// Create a future payment script.
|
||||
pub fn new_on_date(
|
||||
from: &Pubkey,
|
||||
to: &Pubkey,
|
||||
contract: &Pubkey,
|
||||
dt: DateTime<Utc>,
|
||||
dt_pubkey: &Pubkey,
|
||||
cancelable: Option<Pubkey>,
|
||||
lamports: u64,
|
||||
) -> Vec<Instruction> {
|
||||
let expr =
|
||||
BudgetExpr::new_cancelable_future_payment(dt, dt_pubkey, lamports, to, cancelable);
|
||||
Self::new_account(from, contract, lamports, expr)
|
||||
}
|
||||
|
||||
/// Create a multisig payment script.
|
||||
pub fn new_when_signed(
|
||||
from: &Pubkey,
|
||||
to: &Pubkey,
|
||||
contract: &Pubkey,
|
||||
witness: &Pubkey,
|
||||
cancelable: Option<Pubkey>,
|
||||
lamports: u64,
|
||||
) -> Vec<Instruction> {
|
||||
let expr = BudgetExpr::new_cancelable_authorized_payment(witness, lamports, to, cancelable);
|
||||
Self::new_account(from, contract, lamports, expr)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::budget_expr::BudgetExpr;
|
||||
|
||||
#[test]
|
||||
fn test_budget_instruction_verify() {
|
||||
let alice_pubkey = Keypair::new().pubkey();
|
||||
let bob_pubkey = Keypair::new().pubkey();
|
||||
BudgetInstruction::new_payment(&alice_pubkey, &bob_pubkey, 1); // No panic! indicates success.
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn test_budget_instruction_overspend() {
|
||||
let alice_pubkey = Keypair::new().pubkey();
|
||||
let bob_pubkey = Keypair::new().pubkey();
|
||||
let budget_pubkey = Keypair::new().pubkey();
|
||||
let expr = BudgetExpr::new_payment(2, &bob_pubkey);
|
||||
BudgetInstruction::new_account(&alice_pubkey, &budget_pubkey, 1, expr);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn test_budget_instruction_underspend() {
|
||||
let alice_pubkey = Keypair::new().pubkey();
|
||||
let bob_pubkey = Keypair::new().pubkey();
|
||||
let budget_pubkey = Keypair::new().pubkey();
|
||||
let expr = BudgetExpr::new_payment(1, &bob_pubkey);
|
||||
BudgetInstruction::new_account(&alice_pubkey, &budget_pubkey, 2, expr);
|
||||
}
|
||||
}
|
||||
|
@@ -144,7 +144,6 @@ pub fn process_instruction(
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::budget_instruction::BudgetInstruction;
|
||||
use crate::budget_script::BudgetScript;
|
||||
use crate::id;
|
||||
use solana_runtime::bank::Bank;
|
||||
use solana_runtime::bank_client::BankClient;
|
||||
@@ -166,8 +165,8 @@ mod tests {
|
||||
let alice_client = BankClient::new(&bank, mint_keypair);
|
||||
let alice_pubkey = alice_client.pubkey();
|
||||
let bob_pubkey = Keypair::new().pubkey();
|
||||
let script = BudgetScript::pay(&alice_pubkey, &bob_pubkey, 100);
|
||||
alice_client.process_script(script).unwrap();
|
||||
let instructions = BudgetInstruction::new_payment(&alice_pubkey, &bob_pubkey, 100);
|
||||
alice_client.process_instructions(instructions).unwrap();
|
||||
assert_eq!(bank.get_balance(&bob_pubkey), 100);
|
||||
}
|
||||
|
||||
@@ -181,7 +180,7 @@ mod tests {
|
||||
let budget_pubkey = Keypair::new().pubkey();
|
||||
let bob_pubkey = Keypair::new().pubkey();
|
||||
let witness = Keypair::new().pubkey();
|
||||
let script = BudgetScript::pay_on_signature(
|
||||
let instructions = BudgetInstruction::new_when_signed(
|
||||
&alice_pubkey,
|
||||
&bob_pubkey,
|
||||
&budget_pubkey,
|
||||
@@ -189,7 +188,7 @@ mod tests {
|
||||
None,
|
||||
1,
|
||||
);
|
||||
alice_client.process_script(script).unwrap();
|
||||
alice_client.process_instructions(instructions).unwrap();
|
||||
|
||||
// Attack! Part 1: Sign a witness transaction with a random key.
|
||||
let mallory_client = BankClient::new(&bank, Keypair::new());
|
||||
@@ -223,7 +222,7 @@ mod tests {
|
||||
let budget_pubkey = Keypair::new().pubkey();
|
||||
let bob_pubkey = Keypair::new().pubkey();
|
||||
let dt = Utc::now();
|
||||
let script = BudgetScript::pay_on_date(
|
||||
let instructions = BudgetInstruction::new_on_date(
|
||||
&alice_pubkey,
|
||||
&bob_pubkey,
|
||||
&budget_pubkey,
|
||||
@@ -232,7 +231,7 @@ mod tests {
|
||||
None,
|
||||
1,
|
||||
);
|
||||
alice_client.process_script(script).unwrap();
|
||||
alice_client.process_instructions(instructions).unwrap();
|
||||
|
||||
// Attack! Part 1: Sign a timestamp transaction with a random key.
|
||||
let mallory_client = BankClient::new(&bank, Keypair::new());
|
||||
@@ -269,7 +268,7 @@ mod tests {
|
||||
let bob_pubkey = Keypair::new().pubkey();
|
||||
let mallory_pubkey = Keypair::new().pubkey();
|
||||
let dt = Utc::now();
|
||||
let script = BudgetScript::pay_on_date(
|
||||
let instructions = BudgetInstruction::new_on_date(
|
||||
&alice_pubkey,
|
||||
&bob_pubkey,
|
||||
&budget_pubkey,
|
||||
@@ -278,7 +277,7 @@ mod tests {
|
||||
None,
|
||||
1,
|
||||
);
|
||||
alice_client.process_script(script).unwrap();
|
||||
alice_client.process_instructions(instructions).unwrap();
|
||||
assert_eq!(bank.get_balance(&alice_pubkey), 1);
|
||||
assert_eq!(bank.get_balance(&budget_pubkey), 1);
|
||||
|
||||
@@ -328,7 +327,7 @@ mod tests {
|
||||
let bob_pubkey = Keypair::new().pubkey();
|
||||
let dt = Utc::now();
|
||||
|
||||
let script = BudgetScript::pay_on_date(
|
||||
let instructions = BudgetInstruction::new_on_date(
|
||||
&alice_pubkey,
|
||||
&bob_pubkey,
|
||||
&budget_pubkey,
|
||||
@@ -337,7 +336,7 @@ mod tests {
|
||||
Some(alice_pubkey),
|
||||
1,
|
||||
);
|
||||
alice_client.process_script(script).unwrap();
|
||||
alice_client.process_instructions(instructions).unwrap();
|
||||
assert_eq!(bank.get_balance(&alice_pubkey), 2);
|
||||
assert_eq!(bank.get_balance(&budget_pubkey), 1);
|
||||
|
||||
|
@@ -1,108 +0,0 @@
|
||||
use crate::budget_expr::BudgetExpr;
|
||||
use crate::budget_instruction::BudgetInstruction;
|
||||
use crate::budget_state::BudgetState;
|
||||
use crate::id;
|
||||
use bincode::serialized_size;
|
||||
use chrono::prelude::{DateTime, Utc};
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
use solana_sdk::script::Script;
|
||||
use solana_sdk::signature::{Keypair, KeypairUtil};
|
||||
use solana_sdk::system_instruction::SystemInstruction;
|
||||
|
||||
pub struct BudgetScript {}
|
||||
|
||||
impl BudgetScript {
|
||||
pub fn new_account(
|
||||
from: &Pubkey,
|
||||
contract: &Pubkey,
|
||||
lamports: u64,
|
||||
expr: BudgetExpr,
|
||||
) -> Script {
|
||||
if !expr.verify(lamports) {
|
||||
panic!("invalid budget expression");
|
||||
}
|
||||
let space = serialized_size(&BudgetState::new(expr.clone())).unwrap();
|
||||
let instructions = vec![
|
||||
SystemInstruction::new_program_account(&from, contract, lamports, space, &id()),
|
||||
BudgetInstruction::new_initialize_account(contract, expr),
|
||||
];
|
||||
Script::new(instructions)
|
||||
}
|
||||
|
||||
/// Create a new payment script.
|
||||
pub fn pay(from: &Pubkey, to: &Pubkey, lamports: u64) -> Script {
|
||||
let contract = Keypair::new().pubkey();
|
||||
let expr = BudgetExpr::new_payment(lamports, to);
|
||||
Self::new_account(from, &contract, lamports, expr)
|
||||
}
|
||||
|
||||
/// Create a future payment script.
|
||||
pub fn pay_on_date(
|
||||
from: &Pubkey,
|
||||
to: &Pubkey,
|
||||
contract: &Pubkey,
|
||||
dt: DateTime<Utc>,
|
||||
dt_pubkey: &Pubkey,
|
||||
cancelable: Option<Pubkey>,
|
||||
lamports: u64,
|
||||
) -> Script {
|
||||
let expr = if let Some(from) = &cancelable {
|
||||
BudgetExpr::new_cancelable_future_payment(dt, dt_pubkey, lamports, to, from)
|
||||
} else {
|
||||
BudgetExpr::new_future_payment(dt, dt_pubkey, lamports, to)
|
||||
};
|
||||
|
||||
Self::new_account(from, contract, lamports, expr)
|
||||
}
|
||||
|
||||
/// Create a multisig payment script.
|
||||
pub fn pay_on_signature(
|
||||
from: &Pubkey,
|
||||
to: &Pubkey,
|
||||
contract: &Pubkey,
|
||||
witness: &Pubkey,
|
||||
cancelable: Option<Pubkey>,
|
||||
lamports: u64,
|
||||
) -> Script {
|
||||
let expr = if let Some(from) = &cancelable {
|
||||
BudgetExpr::new_cancelable_authorized_payment(witness, lamports, to, from)
|
||||
} else {
|
||||
BudgetExpr::new_authorized_payment(witness, lamports, to)
|
||||
};
|
||||
|
||||
Self::new_account(from, contract, lamports, expr)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::budget_expr::BudgetExpr;
|
||||
|
||||
#[test]
|
||||
fn test_budget_script_verify() {
|
||||
let alice_pubkey = Keypair::new().pubkey();
|
||||
let bob_pubkey = Keypair::new().pubkey();
|
||||
BudgetScript::pay(&alice_pubkey, &bob_pubkey, 1); // No panic! indicates success.
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn test_budget_script_overspend() {
|
||||
let alice_pubkey = Keypair::new().pubkey();
|
||||
let bob_pubkey = Keypair::new().pubkey();
|
||||
let budget_pubkey = Keypair::new().pubkey();
|
||||
let expr = BudgetExpr::new_payment(2, &bob_pubkey);
|
||||
BudgetScript::new_account(&alice_pubkey, &budget_pubkey, 1, expr);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn test_budget_script_underspend() {
|
||||
let alice_pubkey = Keypair::new().pubkey();
|
||||
let bob_pubkey = Keypair::new().pubkey();
|
||||
let budget_pubkey = Keypair::new().pubkey();
|
||||
let expr = BudgetExpr::new_payment(1, &bob_pubkey);
|
||||
BudgetScript::new_account(&alice_pubkey, &budget_pubkey, 2, expr);
|
||||
}
|
||||
}
|
@@ -1,29 +1,15 @@
|
||||
//! The `budget_transaction` module provides functionality for creating Budget transactions.
|
||||
|
||||
use crate::budget_instruction::BudgetInstruction;
|
||||
use crate::budget_script::BudgetScript;
|
||||
use chrono::prelude::*;
|
||||
use solana_sdk::hash::Hash;
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
use solana_sdk::script::Script;
|
||||
use solana_sdk::signature::{Keypair, KeypairUtil};
|
||||
use solana_sdk::transaction::Transaction;
|
||||
|
||||
pub struct BudgetTransaction {}
|
||||
|
||||
impl BudgetTransaction {
|
||||
fn new_signed(
|
||||
from_keypair: &Keypair,
|
||||
script: Script,
|
||||
recent_blockhash: Hash,
|
||||
fee: u64,
|
||||
) -> Transaction {
|
||||
let mut tx = script.compile();
|
||||
tx.fee = fee;
|
||||
tx.sign(&[from_keypair], recent_blockhash);
|
||||
tx
|
||||
}
|
||||
|
||||
/// Create and sign a new Transaction. Used for unit-testing.
|
||||
pub fn new_payment(
|
||||
from_keypair: &Keypair,
|
||||
@@ -32,8 +18,8 @@ impl BudgetTransaction {
|
||||
recent_blockhash: Hash,
|
||||
fee: u64,
|
||||
) -> Transaction {
|
||||
let script = BudgetScript::pay(&from_keypair.pubkey(), to, lamports);
|
||||
Self::new_signed(from_keypair, script, recent_blockhash, fee)
|
||||
let ixs = BudgetInstruction::new_payment(&from_keypair.pubkey(), to, lamports);
|
||||
Transaction::new_signed_instructions(&[from_keypair], ixs, recent_blockhash, fee)
|
||||
}
|
||||
|
||||
/// Create and sign a new Witness Timestamp. Used for unit-testing.
|
||||
@@ -46,9 +32,7 @@ impl BudgetTransaction {
|
||||
) -> Transaction {
|
||||
let from = from_keypair.pubkey();
|
||||
let ix = BudgetInstruction::new_apply_timestamp(&from, contract, to, dt);
|
||||
let mut tx = Transaction::new(vec![ix]);
|
||||
tx.sign(&[from_keypair], recent_blockhash);
|
||||
tx
|
||||
Transaction::new_signed_instructions(&[from_keypair], vec![ix], recent_blockhash, 0)
|
||||
}
|
||||
|
||||
/// Create and sign a new Witness Signature. Used for unit-testing.
|
||||
@@ -60,9 +44,7 @@ impl BudgetTransaction {
|
||||
) -> Transaction {
|
||||
let from = from_keypair.pubkey();
|
||||
let ix = BudgetInstruction::new_apply_signature(&from, contract, to);
|
||||
let mut tx = Transaction::new(vec![ix]);
|
||||
tx.sign(&[from_keypair], recent_blockhash);
|
||||
tx
|
||||
Transaction::new_signed_instructions(&[from_keypair], vec![ix], recent_blockhash, 0)
|
||||
}
|
||||
|
||||
/// Create and sign a postdated Transaction. Used for unit-testing.
|
||||
@@ -76,7 +58,7 @@ impl BudgetTransaction {
|
||||
lamports: u64,
|
||||
recent_blockhash: Hash,
|
||||
) -> Transaction {
|
||||
let script = BudgetScript::pay_on_date(
|
||||
let ixs = BudgetInstruction::new_on_date(
|
||||
&from_keypair.pubkey(),
|
||||
to,
|
||||
contract,
|
||||
@@ -85,7 +67,7 @@ impl BudgetTransaction {
|
||||
cancelable,
|
||||
lamports,
|
||||
);
|
||||
Self::new_signed(from_keypair, script, recent_blockhash, 0)
|
||||
Transaction::new_signed_instructions(&[from_keypair], ixs, recent_blockhash, 0)
|
||||
}
|
||||
|
||||
/// Create and sign a multisig Transaction.
|
||||
@@ -98,7 +80,7 @@ impl BudgetTransaction {
|
||||
lamports: u64,
|
||||
recent_blockhash: Hash,
|
||||
) -> Transaction {
|
||||
let script = BudgetScript::pay_on_signature(
|
||||
let ixs = BudgetInstruction::new_when_signed(
|
||||
&from_keypair.pubkey(),
|
||||
to,
|
||||
contract,
|
||||
@@ -106,6 +88,6 @@ impl BudgetTransaction {
|
||||
cancelable,
|
||||
lamports,
|
||||
);
|
||||
Self::new_signed(from_keypair, script, recent_blockhash, 0)
|
||||
Transaction::new_signed_instructions(&[from_keypair], ixs, recent_blockhash, 0)
|
||||
}
|
||||
}
|
||||
|
@@ -1,7 +1,6 @@
|
||||
pub mod budget_expr;
|
||||
pub mod budget_instruction;
|
||||
pub mod budget_processor;
|
||||
pub mod budget_script;
|
||||
pub mod budget_state;
|
||||
pub mod budget_transaction;
|
||||
pub mod payment_plan;
|
||||
|
Reference in New Issue
Block a user