Move budget_program out of src/
This commit is contained in:
26
src/bank.rs
26
src/bank.rs
@@ -5,7 +5,6 @@
|
||||
|
||||
use bincode::deserialize;
|
||||
use bincode::serialize;
|
||||
use budget_program;
|
||||
use counter::Counter;
|
||||
use entry::Entry;
|
||||
use itertools::Itertools;
|
||||
@@ -15,7 +14,6 @@ use ledger::Block;
|
||||
use log::Level;
|
||||
use mint::Mint;
|
||||
use native_loader;
|
||||
use payment_plan::Payment;
|
||||
use poh_recorder::PohRecorder;
|
||||
use poh_service::NUM_TICKS_PER_SECOND;
|
||||
use rayon::prelude::*;
|
||||
@@ -23,8 +21,10 @@ use rpc::RpcSignatureStatus;
|
||||
use runtime::{self, RuntimeError};
|
||||
use solana_sdk::account::Account;
|
||||
use solana_sdk::bpf_loader;
|
||||
use solana_sdk::budget_program;
|
||||
use solana_sdk::hash::{hash, Hash};
|
||||
use solana_sdk::native_program::ProgramError;
|
||||
use solana_sdk::payment_plan::Payment;
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
use solana_sdk::signature::Keypair;
|
||||
use solana_sdk::signature::Signature;
|
||||
@@ -448,6 +448,16 @@ impl Bank {
|
||||
|
||||
accounts.store(&bpf_loader::id(), &bpf_loader_account);
|
||||
|
||||
// Budget program
|
||||
let budget_program_account = Account {
|
||||
tokens: 1,
|
||||
owner: budget_program::id(),
|
||||
userdata: b"solana_budget_program".to_vec(),
|
||||
executable: true,
|
||||
loader: native_loader::id(),
|
||||
};
|
||||
accounts.store(&budget_program::id(), &budget_program_account);
|
||||
|
||||
// Erc20 token program
|
||||
let erc20_account = Account {
|
||||
tokens: 1,
|
||||
@@ -765,10 +775,6 @@ impl Bank {
|
||||
}
|
||||
|
||||
fn load_executable_accounts(&self, mut program_id: Pubkey) -> Result<Vec<(Pubkey, Account)>> {
|
||||
if runtime::is_legacy_program(&program_id) {
|
||||
return Ok(vec![]);
|
||||
}
|
||||
|
||||
let mut accounts = Vec::new();
|
||||
let mut depth = 0;
|
||||
loop {
|
||||
@@ -1228,11 +1234,13 @@ impl Bank {
|
||||
}
|
||||
|
||||
pub fn read_balance(account: &Account) -> u64 {
|
||||
// TODO: Re-instate budget_program special case?
|
||||
/*
|
||||
if budget_program::check_id(&account.owner) {
|
||||
budget_program::get_balance(account)
|
||||
} else {
|
||||
account.tokens
|
||||
return budget_program::get_balance(account);
|
||||
}
|
||||
*/
|
||||
account.tokens
|
||||
}
|
||||
/// Each program would need to be able to introspect its own state
|
||||
/// this is hard-coded to the Budget language
|
||||
|
||||
@@ -1,232 +0,0 @@
|
||||
//! The `budget_expr` module provides a domain-specific language for payment plans. Users create BudgetExpr objects that
|
||||
//! 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.
|
||||
|
||||
use chrono::prelude::*;
|
||||
use payment_plan::{Payment, Witness};
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
use std::mem;
|
||||
|
||||
/// A data type representing a `Witness` that the payment plan is waiting on.
|
||||
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
|
||||
pub enum Condition {
|
||||
/// Wait for a `Timestamp` `Witness` at or after the given `DateTime`.
|
||||
Timestamp(DateTime<Utc>, Pubkey),
|
||||
|
||||
/// Wait for a `Signature` `Witness` from `Pubkey`.
|
||||
Signature(Pubkey),
|
||||
}
|
||||
|
||||
impl Condition {
|
||||
/// Return true if the given Witness satisfies this 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
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A data type representing a payment plan.
|
||||
#[repr(C)]
|
||||
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
|
||||
pub enum BudgetExpr {
|
||||
/// Make a payment.
|
||||
Pay(Payment),
|
||||
|
||||
/// Make a payment after some condition.
|
||||
After(Condition, Payment),
|
||||
|
||||
/// Either make a payment after one condition or a different payment after another
|
||||
/// condition, which ever condition is satisfied first.
|
||||
Or((Condition, Payment), (Condition, Payment)),
|
||||
|
||||
/// Make a payment after both of two conditions are satisfied
|
||||
And(Condition, Condition, Payment),
|
||||
}
|
||||
|
||||
impl BudgetExpr {
|
||||
/// Create the simplest budget - one that pays `tokens` to Pubkey.
|
||||
pub fn new_payment(tokens: u64, to: Pubkey) -> Self {
|
||||
BudgetExpr::Pay(Payment { tokens, to })
|
||||
}
|
||||
|
||||
/// Create a budget that pays `tokens` to `to` after being witnessed by `from`.
|
||||
pub fn new_authorized_payment(from: Pubkey, tokens: u64, to: Pubkey) -> Self {
|
||||
BudgetExpr::After(Condition::Signature(from), Payment { tokens, to })
|
||||
}
|
||||
|
||||
/// Create a budget that pays tokens` to `to` after being witnessed by 2x `from`s
|
||||
pub fn new_2_2_multisig_payment(from0: Pubkey, from1: Pubkey, tokens: u64, to: Pubkey) -> Self {
|
||||
BudgetExpr::And(
|
||||
Condition::Signature(from0),
|
||||
Condition::Signature(from1),
|
||||
Payment { tokens, to },
|
||||
)
|
||||
}
|
||||
|
||||
/// Create a budget that pays `tokens` to `to` after the given DateTime.
|
||||
pub fn new_future_payment(dt: DateTime<Utc>, from: Pubkey, tokens: u64, to: Pubkey) -> Self {
|
||||
BudgetExpr::After(Condition::Timestamp(dt, from), Payment { tokens, to })
|
||||
}
|
||||
|
||||
/// Create a budget that pays `tokens` to `to` after the given DateTime
|
||||
/// unless cancelled by `from`.
|
||||
pub fn new_cancelable_future_payment(
|
||||
dt: DateTime<Utc>,
|
||||
from: Pubkey,
|
||||
tokens: u64,
|
||||
to: Pubkey,
|
||||
) -> Self {
|
||||
BudgetExpr::Or(
|
||||
(Condition::Timestamp(dt, from), Payment { tokens, to }),
|
||||
(Condition::Signature(from), Payment { tokens, to: from }),
|
||||
)
|
||||
}
|
||||
|
||||
/// Return Payment if the budget requires no additional Witnesses.
|
||||
pub fn final_payment(&self) -> Option<Payment> {
|
||||
match self {
|
||||
BudgetExpr::Pay(payment) => Some(payment.clone()),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Return true if the budget spends exactly `spendable_tokens`.
|
||||
pub fn verify(&self, spendable_tokens: u64) -> bool {
|
||||
match self {
|
||||
BudgetExpr::Pay(payment)
|
||||
| BudgetExpr::After(_, payment)
|
||||
| BudgetExpr::And(_, _, payment) => payment.tokens == spendable_tokens,
|
||||
BudgetExpr::Or(a, b) => {
|
||||
a.1.tokens == spendable_tokens && b.1.tokens == spendable_tokens
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 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) {
|
||||
let new_expr = match self {
|
||||
BudgetExpr::After(cond, payment) if cond.is_satisfied(witness, from) => {
|
||||
Some(BudgetExpr::Pay(payment.clone()))
|
||||
}
|
||||
BudgetExpr::Or((cond, payment), _) if cond.is_satisfied(witness, from) => {
|
||||
Some(BudgetExpr::Pay(payment.clone()))
|
||||
}
|
||||
BudgetExpr::Or(_, (cond, payment)) if cond.is_satisfied(witness, from) => {
|
||||
Some(BudgetExpr::Pay(payment.clone()))
|
||||
}
|
||||
BudgetExpr::And(cond0, cond1, payment) => {
|
||||
if cond0.is_satisfied(witness, from) {
|
||||
Some(BudgetExpr::After(cond1.clone(), payment.clone()))
|
||||
} else if cond1.is_satisfied(witness, from) {
|
||||
Some(BudgetExpr::After(cond0.clone(), payment.clone()))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
if let Some(expr) = new_expr {
|
||||
mem::replace(self, expr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use solana_sdk::signature::{Keypair, KeypairUtil};
|
||||
|
||||
#[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).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 = Keypair::new().pubkey();
|
||||
let to = Keypair::new().pubkey();
|
||||
|
||||
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() {
|
||||
// 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);
|
||||
let from = Keypair::new().pubkey();
|
||||
let to = Keypair::new().pubkey();
|
||||
|
||||
let mut expr = BudgetExpr::new_future_payment(dt, from, 42, to);
|
||||
let orig_expr = expr.clone();
|
||||
expr.apply_witness(&Witness::Timestamp(dt), &to); // <-- Attack!
|
||||
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);
|
||||
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);
|
||||
expr.apply_witness(&Witness::Signature, &from);
|
||||
assert_eq!(expr, BudgetExpr::new_payment(42, from));
|
||||
}
|
||||
#[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);
|
||||
expr.apply_witness(&Witness::Signature, &from0);
|
||||
assert_eq!(expr, BudgetExpr::new_authorized_payment(from1, 42, to));
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
use budget_expr::BudgetExpr;
|
||||
use chrono::prelude::{DateTime, Utc};
|
||||
|
||||
/// A smart contract.
|
||||
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
|
||||
pub struct Contract {
|
||||
/// The number of tokens allocated to the `BudgetExpr` and any transaction fees.
|
||||
pub tokens: u64,
|
||||
pub budget_expr: BudgetExpr,
|
||||
}
|
||||
|
||||
/// An instruction to progress the smart contract.
|
||||
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
|
||||
pub enum Instruction {
|
||||
/// Declare and instantiate `BudgetExpr`.
|
||||
NewBudget(BudgetExpr),
|
||||
|
||||
/// Tell a payment plan acknowledge the given `DateTime` has past.
|
||||
ApplyTimestamp(DateTime<Utc>),
|
||||
|
||||
/// Tell the budget that the `NewBudget` with `Signature` has been
|
||||
/// signed by the containing transaction's `Pubkey`.
|
||||
ApplySignature,
|
||||
}
|
||||
@@ -1,650 +0,0 @@
|
||||
//! budget program
|
||||
use bincode::{self, deserialize, serialize_into, serialized_size};
|
||||
use budget_expr::BudgetExpr;
|
||||
use budget_instruction::Instruction;
|
||||
use chrono::prelude::{DateTime, Utc};
|
||||
use payment_plan::Witness;
|
||||
use solana_sdk::account::Account;
|
||||
use solana_sdk::native_program::ProgramError;
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
use solana_sdk::transaction::Transaction;
|
||||
use std::io;
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
|
||||
pub enum BudgetError {
|
||||
InsufficientFunds,
|
||||
ContractAlreadyExists,
|
||||
ContractNotPending,
|
||||
SourceIsPendingContract,
|
||||
UninitializedContract,
|
||||
NegativeTokens,
|
||||
DestinationMissing,
|
||||
FailedWitness,
|
||||
UserdataTooSmall,
|
||||
UserdataDeserializeFailure,
|
||||
UnsignedKey,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
|
||||
pub struct BudgetProgram {
|
||||
pub initialized: bool,
|
||||
pub pending_budget: Option<BudgetExpr>,
|
||||
}
|
||||
|
||||
const BUDGET_PROGRAM_ID: [u8; 32] = [
|
||||
129, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0,
|
||||
];
|
||||
|
||||
pub fn id() -> Pubkey {
|
||||
Pubkey::new(&BUDGET_PROGRAM_ID)
|
||||
}
|
||||
|
||||
pub fn check_id(program_id: &Pubkey) -> bool {
|
||||
program_id.as_ref() == BUDGET_PROGRAM_ID
|
||||
}
|
||||
|
||||
fn apply_debits(
|
||||
tx: &Transaction,
|
||||
instruction_index: usize,
|
||||
accounts: &mut [&mut Account],
|
||||
instruction: &Instruction,
|
||||
) -> Result<(), BudgetError> {
|
||||
if !accounts[0].userdata.is_empty() {
|
||||
trace!("source is pending");
|
||||
return Err(BudgetError::SourceIsPendingContract);
|
||||
}
|
||||
match instruction {
|
||||
Instruction::NewBudget(expr) => {
|
||||
let expr = expr.clone();
|
||||
if let Some(payment) = expr.final_payment() {
|
||||
accounts[1].tokens += payment.tokens;
|
||||
Ok(())
|
||||
} else {
|
||||
let existing = BudgetProgram::deserialize(&accounts[1].userdata).ok();
|
||||
if Some(true) == existing.map(|x| x.initialized) {
|
||||
trace!("contract already exists");
|
||||
Err(BudgetError::ContractAlreadyExists)
|
||||
} else {
|
||||
let mut program = BudgetProgram::default();
|
||||
program.pending_budget = Some(expr);
|
||||
accounts[1].tokens += accounts[0].tokens;
|
||||
accounts[0].tokens = 0;
|
||||
program.initialized = true;
|
||||
program.serialize(&mut accounts[1].userdata)
|
||||
}
|
||||
}
|
||||
}
|
||||
Instruction::ApplyTimestamp(dt) => {
|
||||
if let Ok(mut program) = BudgetProgram::deserialize(&accounts[1].userdata) {
|
||||
if !program.is_pending() {
|
||||
Err(BudgetError::ContractNotPending)
|
||||
} else if !program.initialized {
|
||||
trace!("contract is uninitialized");
|
||||
Err(BudgetError::UninitializedContract)
|
||||
} else {
|
||||
trace!("apply timestamp");
|
||||
program.apply_timestamp(tx, instruction_index, accounts, *dt)?;
|
||||
trace!("apply timestamp committed");
|
||||
program.serialize(&mut accounts[1].userdata)
|
||||
}
|
||||
} else {
|
||||
Err(BudgetError::UninitializedContract)
|
||||
}
|
||||
}
|
||||
Instruction::ApplySignature => {
|
||||
if let Ok(mut program) = BudgetProgram::deserialize(&accounts[1].userdata) {
|
||||
if !program.is_pending() {
|
||||
Err(BudgetError::ContractNotPending)
|
||||
} else if !program.initialized {
|
||||
trace!("contract is uninitialized");
|
||||
Err(BudgetError::UninitializedContract)
|
||||
} else {
|
||||
trace!("apply signature");
|
||||
program.apply_signature(tx, instruction_index, accounts)?;
|
||||
trace!("apply signature committed");
|
||||
program.serialize(&mut accounts[1].userdata)
|
||||
}
|
||||
} else {
|
||||
Err(BudgetError::UninitializedContract)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Budget DSL contract interface
|
||||
/// * tx - the transaction
|
||||
/// * accounts[0] - The source of the tokens
|
||||
/// * accounts[1] - The contract context. Once the contract has been completed, the tokens can
|
||||
/// be spent from this account .
|
||||
pub fn process_instruction(
|
||||
tx: &Transaction,
|
||||
instruction_index: usize,
|
||||
accounts: &mut [&mut Account],
|
||||
) -> Result<(), BudgetError> {
|
||||
if let Ok(instruction) = deserialize(tx.userdata(instruction_index)) {
|
||||
trace!("process_instruction: {:?}", instruction);
|
||||
apply_debits(tx, instruction_index, accounts, &instruction)
|
||||
} else {
|
||||
info!(
|
||||
"Invalid transaction userdata: {:?}",
|
||||
tx.userdata(instruction_index)
|
||||
);
|
||||
Err(BudgetError::UserdataDeserializeFailure)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn process(
|
||||
tx: &Transaction,
|
||||
instruction_index: usize,
|
||||
accounts: &mut [&mut Account],
|
||||
) -> std::result::Result<(), ProgramError> {
|
||||
process_instruction(&tx, instruction_index, accounts).map_err(|_| ProgramError::GenericError)
|
||||
}
|
||||
|
||||
//TODO the contract needs to provide a "get_balance" introspection call of the userdata
|
||||
pub fn get_balance(account: &Account) -> u64 {
|
||||
if let Ok(program) = deserialize(&account.userdata) {
|
||||
let program: BudgetProgram = program;
|
||||
if program.is_pending() {
|
||||
0
|
||||
} else {
|
||||
account.tokens
|
||||
}
|
||||
} else {
|
||||
account.tokens
|
||||
}
|
||||
}
|
||||
|
||||
impl BudgetProgram {
|
||||
fn is_pending(&self) -> bool {
|
||||
self.pending_budget != None
|
||||
}
|
||||
/// Process a Witness Signature. Any payment plans waiting on this signature
|
||||
/// will progress one step.
|
||||
fn apply_signature(
|
||||
&mut self,
|
||||
tx: &Transaction,
|
||||
instruction_index: usize,
|
||||
accounts: &mut [&mut Account],
|
||||
) -> Result<(), BudgetError> {
|
||||
let mut final_payment = None;
|
||||
if let Some(ref mut expr) = self.pending_budget {
|
||||
let key = match tx.signer_key(instruction_index, 0) {
|
||||
None => return Err(BudgetError::UnsignedKey),
|
||||
Some(key) => key,
|
||||
};
|
||||
expr.apply_witness(&Witness::Signature, key);
|
||||
final_payment = expr.final_payment();
|
||||
}
|
||||
|
||||
if let Some(payment) = final_payment {
|
||||
if Some(&payment.to) != tx.key(instruction_index, 2) {
|
||||
trace!("destination missing");
|
||||
return Err(BudgetError::DestinationMissing);
|
||||
}
|
||||
self.pending_budget = None;
|
||||
accounts[1].tokens -= payment.tokens;
|
||||
accounts[2].tokens += payment.tokens;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Process a Witness Timestamp. Any payment plans waiting on this timestamp
|
||||
/// will progress one step.
|
||||
fn apply_timestamp(
|
||||
&mut self,
|
||||
tx: &Transaction,
|
||||
instruction_index: usize,
|
||||
accounts: &mut [&mut Account],
|
||||
dt: DateTime<Utc>,
|
||||
) -> Result<(), BudgetError> {
|
||||
// Check to see if any timelocked transactions can be completed.
|
||||
let mut final_payment = None;
|
||||
|
||||
if let Some(ref mut expr) = self.pending_budget {
|
||||
let key = match tx.signer_key(instruction_index, 0) {
|
||||
None => return Err(BudgetError::UnsignedKey),
|
||||
Some(key) => key,
|
||||
};
|
||||
expr.apply_witness(&Witness::Timestamp(dt), key);
|
||||
final_payment = expr.final_payment();
|
||||
}
|
||||
|
||||
if let Some(payment) = final_payment {
|
||||
if Some(&payment.to) != tx.key(instruction_index, 2) {
|
||||
trace!("destination missing");
|
||||
return Err(BudgetError::DestinationMissing);
|
||||
}
|
||||
self.pending_budget = None;
|
||||
accounts[1].tokens -= payment.tokens;
|
||||
accounts[2].tokens += payment.tokens;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize(&self, output: &mut [u8]) -> Result<(), BudgetError> {
|
||||
let len = serialized_size(self).unwrap() as u64;
|
||||
if output.len() < len as usize {
|
||||
warn!(
|
||||
"{} bytes required to serialize, only have {} bytes",
|
||||
len,
|
||||
output.len()
|
||||
);
|
||||
return Err(BudgetError::UserdataTooSmall);
|
||||
}
|
||||
{
|
||||
let writer = io::BufWriter::new(&mut output[..8]);
|
||||
serialize_into(writer, &len).unwrap();
|
||||
}
|
||||
|
||||
{
|
||||
let writer = io::BufWriter::new(&mut output[8..8 + len as usize]);
|
||||
serialize_into(writer, self).unwrap();
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn deserialize(input: &[u8]) -> bincode::Result<Self> {
|
||||
if input.len() < 8 {
|
||||
return Err(Box::new(bincode::ErrorKind::SizeLimit));
|
||||
}
|
||||
let len: u64 = deserialize(&input[..8]).unwrap();
|
||||
if len < 2 {
|
||||
return Err(Box::new(bincode::ErrorKind::SizeLimit));
|
||||
}
|
||||
if input.len() < 8 + len as usize {
|
||||
return Err(Box::new(bincode::ErrorKind::SizeLimit));
|
||||
}
|
||||
deserialize(&input[8..8 + len as usize])
|
||||
}
|
||||
}
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
use bincode::serialize;
|
||||
use budget_transaction::BudgetTransaction;
|
||||
use chrono::prelude::{DateTime, NaiveDate, Utc};
|
||||
use signature::GenKeys;
|
||||
use solana_sdk::account::Account;
|
||||
use solana_sdk::hash::Hash;
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
use solana_sdk::signature::{Keypair, KeypairUtil};
|
||||
use solana_sdk::transaction::Transaction;
|
||||
|
||||
fn process_transaction(tx: &Transaction, accounts: &mut [Account]) -> Result<(), BudgetError> {
|
||||
let mut refs: Vec<&mut Account> = accounts.iter_mut().collect();
|
||||
super::process_instruction(&tx, 0, &mut refs[..])
|
||||
}
|
||||
#[test]
|
||||
fn test_serializer() {
|
||||
let mut a = Account::new(0, 512, id());
|
||||
let b = BudgetProgram::default();
|
||||
b.serialize(&mut a.userdata).unwrap();
|
||||
let buf = serialize(&b).unwrap();
|
||||
assert_eq!(a.userdata[8..8 + buf.len()], buf[0..]);
|
||||
let c = BudgetProgram::deserialize(&a.userdata).unwrap();
|
||||
assert_eq!(b, c);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_serializer_userdata_too_small() {
|
||||
let mut a = Account::new(0, 1, id());
|
||||
let b = BudgetProgram::default();
|
||||
assert_eq!(
|
||||
b.serialize(&mut a.userdata),
|
||||
Err(BudgetError::UserdataTooSmall)
|
||||
);
|
||||
}
|
||||
#[test]
|
||||
fn test_invalid_instruction() {
|
||||
let mut accounts = vec![Account::new(1, 0, id()), Account::new(0, 512, id())];
|
||||
let from = Keypair::new();
|
||||
let contract = Keypair::new();
|
||||
let userdata = (1u8, 2u8, 3u8);
|
||||
let tx = Transaction::new(
|
||||
&from,
|
||||
&[contract.pubkey()],
|
||||
id(),
|
||||
&userdata,
|
||||
Hash::default(),
|
||||
0,
|
||||
);
|
||||
assert!(process_transaction(&tx, &mut accounts).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_unsigned_witness_key() {
|
||||
let mut accounts = vec![
|
||||
Account::new(1, 0, id()),
|
||||
Account::new(0, 512, id()),
|
||||
Account::new(0, 0, id()),
|
||||
];
|
||||
|
||||
// Initialize BudgetProgram
|
||||
let from = Keypair::new();
|
||||
let contract = Keypair::new().pubkey();
|
||||
let to = Keypair::new().pubkey();
|
||||
let witness = Keypair::new().pubkey();
|
||||
let tx = Transaction::budget_new_when_signed(
|
||||
&from,
|
||||
to,
|
||||
contract,
|
||||
witness,
|
||||
None,
|
||||
1,
|
||||
Hash::default(),
|
||||
);
|
||||
process_transaction(&tx, &mut accounts).unwrap();
|
||||
|
||||
// Attack! Part 1: Sign a witness transaction with a random key.
|
||||
let rando = Keypair::new();
|
||||
let mut tx = Transaction::budget_new_signature(&rando, contract, to, Hash::default());
|
||||
|
||||
// Attack! Part 2: Point the instruction to the expected, but unsigned, key.
|
||||
tx.account_keys.push(from.pubkey());
|
||||
tx.instructions[0].accounts[0] = 3;
|
||||
|
||||
// Ensure the transaction fails because of the unsigned key.
|
||||
assert_eq!(
|
||||
process_transaction(&tx, &mut accounts),
|
||||
Err(BudgetError::UnsignedKey)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_unsigned_timestamp() {
|
||||
let mut accounts = vec![
|
||||
Account::new(1, 0, id()),
|
||||
Account::new(0, 512, id()),
|
||||
Account::new(0, 0, id()),
|
||||
];
|
||||
|
||||
// Initialize BudgetProgram
|
||||
let from = Keypair::new();
|
||||
let contract = Keypair::new().pubkey();
|
||||
let to = Keypair::new().pubkey();
|
||||
let dt = Utc::now();
|
||||
let tx = Transaction::budget_new_on_date(
|
||||
&from,
|
||||
to,
|
||||
contract,
|
||||
dt,
|
||||
from.pubkey(),
|
||||
None,
|
||||
1,
|
||||
Hash::default(),
|
||||
);
|
||||
process_transaction(&tx, &mut accounts).unwrap();
|
||||
|
||||
// Attack! Part 1: Sign a timestamp transaction with a random key.
|
||||
let rando = Keypair::new();
|
||||
let mut tx = Transaction::budget_new_timestamp(&rando, contract, to, dt, Hash::default());
|
||||
|
||||
// Attack! Part 2: Point the instruction to the expected, but unsigned, key.
|
||||
tx.account_keys.push(from.pubkey());
|
||||
tx.instructions[0].accounts[0] = 3;
|
||||
|
||||
// Ensure the transaction fails because of the unsigned key.
|
||||
assert_eq!(
|
||||
process_transaction(&tx, &mut accounts),
|
||||
Err(BudgetError::UnsignedKey)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_transfer_on_date() {
|
||||
let mut accounts = vec![
|
||||
Account::new(1, 0, id()),
|
||||
Account::new(0, 512, id()),
|
||||
Account::new(0, 0, id()),
|
||||
];
|
||||
let from_account = 0;
|
||||
let contract_account = 1;
|
||||
let to_account = 2;
|
||||
let from = Keypair::new();
|
||||
let contract = Keypair::new();
|
||||
let to = Keypair::new();
|
||||
let rando = Keypair::new();
|
||||
let dt = Utc::now();
|
||||
let tx = Transaction::budget_new_on_date(
|
||||
&from,
|
||||
to.pubkey(),
|
||||
contract.pubkey(),
|
||||
dt,
|
||||
from.pubkey(),
|
||||
None,
|
||||
1,
|
||||
Hash::default(),
|
||||
);
|
||||
process_transaction(&tx, &mut accounts).unwrap();
|
||||
assert_eq!(accounts[from_account].tokens, 0);
|
||||
assert_eq!(accounts[contract_account].tokens, 1);
|
||||
let program = BudgetProgram::deserialize(&accounts[contract_account].userdata).unwrap();
|
||||
assert!(program.is_pending());
|
||||
|
||||
// Attack! Try to payout to a rando key
|
||||
let tx = Transaction::budget_new_timestamp(
|
||||
&from,
|
||||
contract.pubkey(),
|
||||
rando.pubkey(),
|
||||
dt,
|
||||
Hash::default(),
|
||||
);
|
||||
assert_eq!(
|
||||
process_transaction(&tx, &mut accounts),
|
||||
Err(BudgetError::DestinationMissing)
|
||||
);
|
||||
assert_eq!(accounts[from_account].tokens, 0);
|
||||
assert_eq!(accounts[contract_account].tokens, 1);
|
||||
assert_eq!(accounts[to_account].tokens, 0);
|
||||
|
||||
let program = BudgetProgram::deserialize(&accounts[contract_account].userdata).unwrap();
|
||||
assert!(program.is_pending());
|
||||
|
||||
// Now, acknowledge the time in the condition occurred and
|
||||
// that pubkey's funds are now available.
|
||||
let tx = Transaction::budget_new_timestamp(
|
||||
&from,
|
||||
contract.pubkey(),
|
||||
to.pubkey(),
|
||||
dt,
|
||||
Hash::default(),
|
||||
);
|
||||
process_transaction(&tx, &mut accounts).unwrap();
|
||||
assert_eq!(accounts[from_account].tokens, 0);
|
||||
assert_eq!(accounts[contract_account].tokens, 0);
|
||||
assert_eq!(accounts[to_account].tokens, 1);
|
||||
|
||||
let program = BudgetProgram::deserialize(&accounts[contract_account].userdata).unwrap();
|
||||
assert!(!program.is_pending());
|
||||
|
||||
// try to replay the timestamp contract
|
||||
assert_eq!(
|
||||
process_transaction(&tx, &mut accounts),
|
||||
Err(BudgetError::ContractNotPending)
|
||||
);
|
||||
assert_eq!(accounts[from_account].tokens, 0);
|
||||
assert_eq!(accounts[contract_account].tokens, 0);
|
||||
assert_eq!(accounts[to_account].tokens, 1);
|
||||
}
|
||||
#[test]
|
||||
fn test_cancel_transfer() {
|
||||
let mut accounts = vec![
|
||||
Account::new(1, 0, id()),
|
||||
Account::new(0, 512, id()),
|
||||
Account::new(0, 0, id()),
|
||||
];
|
||||
let from_account = 0;
|
||||
let contract_account = 1;
|
||||
let pay_account = 2;
|
||||
let from = Keypair::new();
|
||||
let contract = Keypair::new();
|
||||
let to = Keypair::new();
|
||||
let dt = Utc::now();
|
||||
let tx = Transaction::budget_new_on_date(
|
||||
&from,
|
||||
to.pubkey(),
|
||||
contract.pubkey(),
|
||||
dt,
|
||||
from.pubkey(),
|
||||
Some(from.pubkey()),
|
||||
1,
|
||||
Hash::default(),
|
||||
);
|
||||
process_transaction(&tx, &mut accounts).unwrap();
|
||||
assert_eq!(accounts[from_account].tokens, 0);
|
||||
assert_eq!(accounts[contract_account].tokens, 1);
|
||||
let program = BudgetProgram::deserialize(&accounts[contract_account].userdata).unwrap();
|
||||
assert!(program.is_pending());
|
||||
|
||||
// Attack! try to put the tokens into the wrong account with cancel
|
||||
let tx =
|
||||
Transaction::budget_new_signature(&to, contract.pubkey(), to.pubkey(), Hash::default());
|
||||
// unit test hack, the `from account` is passed instead of the `to` account to avoid
|
||||
// creating more account vectors
|
||||
process_transaction(&tx, &mut accounts).unwrap();
|
||||
// nothing should be changed because apply witness didn't finalize a payment
|
||||
assert_eq!(accounts[from_account].tokens, 0);
|
||||
assert_eq!(accounts[contract_account].tokens, 1);
|
||||
// this would be the `to.pubkey()` account
|
||||
assert_eq!(accounts[pay_account].tokens, 0);
|
||||
|
||||
// Now, cancel the transaction. from gets her funds back
|
||||
let tx = Transaction::budget_new_signature(
|
||||
&from,
|
||||
contract.pubkey(),
|
||||
from.pubkey(),
|
||||
Hash::default(),
|
||||
);
|
||||
process_transaction(&tx, &mut accounts).unwrap();
|
||||
assert_eq!(accounts[from_account].tokens, 0);
|
||||
assert_eq!(accounts[contract_account].tokens, 0);
|
||||
assert_eq!(accounts[pay_account].tokens, 1);
|
||||
|
||||
// try to replay the signature contract
|
||||
let tx = Transaction::budget_new_signature(
|
||||
&from,
|
||||
contract.pubkey(),
|
||||
from.pubkey(),
|
||||
Hash::default(),
|
||||
);
|
||||
assert_eq!(
|
||||
process_transaction(&tx, &mut accounts),
|
||||
Err(BudgetError::ContractNotPending)
|
||||
);
|
||||
assert_eq!(accounts[from_account].tokens, 0);
|
||||
assert_eq!(accounts[contract_account].tokens, 0);
|
||||
assert_eq!(accounts[pay_account].tokens, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_userdata_too_small() {
|
||||
let mut accounts = vec![
|
||||
Account::new(1, 0, id()),
|
||||
Account::new(1, 0, id()), // <== userdata is 0, which is not enough
|
||||
Account::new(1, 0, id()),
|
||||
];
|
||||
let from = Keypair::new();
|
||||
let contract = Keypair::new();
|
||||
let to = Keypair::new();
|
||||
let tx = Transaction::budget_new_on_date(
|
||||
&from,
|
||||
to.pubkey(),
|
||||
contract.pubkey(),
|
||||
Utc::now(),
|
||||
from.pubkey(),
|
||||
None,
|
||||
1,
|
||||
Hash::default(),
|
||||
);
|
||||
|
||||
assert!(process_transaction(&tx, &mut accounts).is_err());
|
||||
assert!(BudgetProgram::deserialize(&accounts[1].userdata).is_err());
|
||||
|
||||
let tx = Transaction::budget_new_timestamp(
|
||||
&from,
|
||||
contract.pubkey(),
|
||||
to.pubkey(),
|
||||
Utc::now(),
|
||||
Hash::default(),
|
||||
);
|
||||
assert!(process_transaction(&tx, &mut accounts).is_err());
|
||||
assert!(BudgetProgram::deserialize(&accounts[1].userdata).is_err());
|
||||
|
||||
// Success if there was no panic...
|
||||
}
|
||||
|
||||
/// Detect binary changes in the serialized contract userdata, which could have a downstream
|
||||
/// affect on SDKs and DApps
|
||||
#[test]
|
||||
fn test_sdk_serialize() {
|
||||
let keypair = &GenKeys::new([0u8; 32]).gen_n_keypairs(1)[0];
|
||||
let to = Pubkey::new(&[
|
||||
1, 1, 1, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 8, 7, 6, 5, 4,
|
||||
1, 1, 1,
|
||||
]);
|
||||
let contract = Pubkey::new(&[
|
||||
2, 2, 2, 4, 5, 6, 7, 8, 9, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 9, 8, 7, 6, 5, 4,
|
||||
2, 2, 2,
|
||||
]);
|
||||
let date =
|
||||
DateTime::<Utc>::from_utc(NaiveDate::from_ymd(2016, 7, 8).and_hms(9, 10, 11), Utc);
|
||||
let date_iso8601 = "2016-07-08T09:10:11Z";
|
||||
|
||||
let tx = Transaction::budget_new(&keypair, to, 192, Hash::default());
|
||||
assert_eq!(
|
||||
tx.userdata(0).to_vec(),
|
||||
vec![2, 0, 0, 0, 192, 0, 0, 0, 0, 0, 0, 0]
|
||||
);
|
||||
assert_eq!(
|
||||
tx.userdata(1).to_vec(),
|
||||
vec![
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 192, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 4, 5, 6, 7, 8, 9, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 8, 7, 6, 5, 4, 1, 1, 1
|
||||
]
|
||||
);
|
||||
|
||||
let tx = Transaction::budget_new_on_date(
|
||||
&keypair,
|
||||
to,
|
||||
contract,
|
||||
date,
|
||||
keypair.pubkey(),
|
||||
Some(keypair.pubkey()),
|
||||
192,
|
||||
Hash::default(),
|
||||
);
|
||||
assert_eq!(
|
||||
tx.userdata(0).to_vec(),
|
||||
vec![
|
||||
0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 20, 0, 0, 0, 0, 0, 0, 0, 50, 48, 49, 54, 45,
|
||||
48, 55, 45, 48, 56, 84, 48, 57, 58, 49, 48, 58, 49, 49, 90, 32, 253, 186, 201, 177,
|
||||
11, 117, 135, 187, 167, 181, 188, 22, 59, 206, 105, 231, 150, 215, 30, 78, 212, 76,
|
||||
16, 252, 180, 72, 134, 137, 247, 161, 68, 192, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 4, 5,
|
||||
6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 8, 7, 6, 5, 4, 1, 1, 1, 1,
|
||||
0, 0, 0, 32, 253, 186, 201, 177, 11, 117, 135, 187, 167, 181, 188, 22, 59, 206,
|
||||
105, 231, 150, 215, 30, 78, 212, 76, 16, 252, 180, 72, 134, 137, 247, 161, 68, 192,
|
||||
0, 0, 0, 0, 0, 0, 0, 32, 253, 186, 201, 177, 11, 117, 135, 187, 167, 181, 188, 22,
|
||||
59, 206, 105, 231, 150, 215, 30, 78, 212, 76, 16, 252, 180, 72, 134, 137, 247, 161,
|
||||
68
|
||||
]
|
||||
);
|
||||
|
||||
// ApplyTimestamp(date)
|
||||
let tx = Transaction::budget_new_timestamp(
|
||||
&keypair,
|
||||
keypair.pubkey(),
|
||||
to,
|
||||
date,
|
||||
Hash::default(),
|
||||
);
|
||||
let mut expected_userdata = vec![1, 0, 0, 0, 20, 0, 0, 0, 0, 0, 0, 0];
|
||||
expected_userdata.extend(date_iso8601.as_bytes());
|
||||
assert_eq!(tx.userdata(0).to_vec(), expected_userdata);
|
||||
|
||||
// ApplySignature
|
||||
let tx = Transaction::budget_new_signature(&keypair, keypair.pubkey(), to, Hash::default());
|
||||
assert_eq!(tx.userdata(0).to_vec(), vec![2, 0, 0, 0]);
|
||||
}
|
||||
}
|
||||
@@ -1,346 +0,0 @@
|
||||
//! The `budget_transaction` module provides functionality for creating Budget transactions.
|
||||
|
||||
use bincode::deserialize;
|
||||
use budget_expr::{BudgetExpr, Condition};
|
||||
use budget_instruction::Instruction;
|
||||
use budget_program;
|
||||
use chrono::prelude::*;
|
||||
use payment_plan::Payment;
|
||||
use solana_sdk::hash::Hash;
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
use solana_sdk::signature::{Keypair, KeypairUtil};
|
||||
use solana_sdk::system_instruction::SystemInstruction;
|
||||
use solana_sdk::system_program;
|
||||
|
||||
use solana_sdk::transaction::{self, Transaction};
|
||||
|
||||
pub trait BudgetTransaction {
|
||||
fn budget_new_taxed(
|
||||
from_keypair: &Keypair,
|
||||
to: Pubkey,
|
||||
tokens: u64,
|
||||
fee: u64,
|
||||
last_id: Hash,
|
||||
) -> Self;
|
||||
|
||||
fn budget_new(from_keypair: &Keypair, to: Pubkey, tokens: u64, last_id: Hash) -> Self;
|
||||
|
||||
fn budget_new_timestamp(
|
||||
from_keypair: &Keypair,
|
||||
contract: Pubkey,
|
||||
to: Pubkey,
|
||||
dt: DateTime<Utc>,
|
||||
last_id: Hash,
|
||||
) -> Self;
|
||||
|
||||
fn budget_new_signature(
|
||||
from_keypair: &Keypair,
|
||||
contract: Pubkey,
|
||||
to: Pubkey,
|
||||
last_id: Hash,
|
||||
) -> Self;
|
||||
|
||||
fn budget_new_on_date(
|
||||
from_keypair: &Keypair,
|
||||
to: Pubkey,
|
||||
contract: Pubkey,
|
||||
dt: DateTime<Utc>,
|
||||
dt_pubkey: Pubkey,
|
||||
cancelable: Option<Pubkey>,
|
||||
tokens: u64,
|
||||
last_id: Hash,
|
||||
) -> Self;
|
||||
|
||||
fn budget_new_when_signed(
|
||||
from_keypair: &Keypair,
|
||||
to: Pubkey,
|
||||
contract: Pubkey,
|
||||
witness: Pubkey,
|
||||
cancelable: Option<Pubkey>,
|
||||
tokens: u64,
|
||||
last_id: Hash,
|
||||
) -> Self;
|
||||
|
||||
fn instruction(&self, program_index: usize) -> Option<Instruction>;
|
||||
fn system_instruction(&self, program_index: usize) -> Option<SystemInstruction>;
|
||||
|
||||
fn verify_plan(&self) -> bool;
|
||||
}
|
||||
|
||||
impl BudgetTransaction for Transaction {
|
||||
/// Create and sign a new Transaction. Used for unit-testing.
|
||||
fn budget_new_taxed(
|
||||
from_keypair: &Keypair,
|
||||
to: Pubkey,
|
||||
tokens: u64,
|
||||
fee: u64,
|
||||
last_id: Hash,
|
||||
) -> Self {
|
||||
let contract = Keypair::new().pubkey();
|
||||
let keys = vec![from_keypair.pubkey(), contract];
|
||||
|
||||
let system_instruction = SystemInstruction::Move { tokens };
|
||||
|
||||
let payment = Payment {
|
||||
tokens: tokens - fee,
|
||||
to,
|
||||
};
|
||||
let budget_instruction = Instruction::NewBudget(BudgetExpr::Pay(payment));
|
||||
|
||||
let program_ids = vec![system_program::id(), budget_program::id()];
|
||||
|
||||
let instructions = vec![
|
||||
transaction::Instruction::new(0, &system_instruction, vec![0, 1]),
|
||||
transaction::Instruction::new(1, &budget_instruction, vec![1]),
|
||||
];
|
||||
|
||||
Self::new_with_instructions(
|
||||
&[from_keypair],
|
||||
&keys,
|
||||
last_id,
|
||||
fee,
|
||||
program_ids,
|
||||
instructions,
|
||||
)
|
||||
}
|
||||
|
||||
/// Create and sign a new Transaction. Used for unit-testing.
|
||||
fn budget_new(from_keypair: &Keypair, to: Pubkey, tokens: u64, last_id: Hash) -> Self {
|
||||
Self::budget_new_taxed(from_keypair, to, tokens, 0, last_id)
|
||||
}
|
||||
|
||||
/// Create and sign a new Witness Timestamp. Used for unit-testing.
|
||||
fn budget_new_timestamp(
|
||||
from_keypair: &Keypair,
|
||||
contract: Pubkey,
|
||||
to: Pubkey,
|
||||
dt: DateTime<Utc>,
|
||||
last_id: Hash,
|
||||
) -> Self {
|
||||
let instruction = Instruction::ApplyTimestamp(dt);
|
||||
Self::new(
|
||||
from_keypair,
|
||||
&[contract, to],
|
||||
budget_program::id(),
|
||||
&instruction,
|
||||
last_id,
|
||||
0,
|
||||
)
|
||||
}
|
||||
|
||||
/// Create and sign a new Witness Signature. Used for unit-testing.
|
||||
fn budget_new_signature(
|
||||
from_keypair: &Keypair,
|
||||
contract: Pubkey,
|
||||
to: Pubkey,
|
||||
last_id: Hash,
|
||||
) -> Self {
|
||||
let instruction = Instruction::ApplySignature;
|
||||
Self::new(
|
||||
from_keypair,
|
||||
&[contract, to],
|
||||
budget_program::id(),
|
||||
&instruction,
|
||||
last_id,
|
||||
0,
|
||||
)
|
||||
}
|
||||
|
||||
/// Create and sign a postdated Transaction. Used for unit-testing.
|
||||
fn budget_new_on_date(
|
||||
from_keypair: &Keypair,
|
||||
to: Pubkey,
|
||||
contract: Pubkey,
|
||||
dt: DateTime<Utc>,
|
||||
dt_pubkey: Pubkey,
|
||||
cancelable: Option<Pubkey>,
|
||||
tokens: u64,
|
||||
last_id: Hash,
|
||||
) -> Self {
|
||||
let expr = if let Some(from) = cancelable {
|
||||
BudgetExpr::Or(
|
||||
(Condition::Timestamp(dt, dt_pubkey), Payment { tokens, to }),
|
||||
(Condition::Signature(from), Payment { tokens, to: from }),
|
||||
)
|
||||
} else {
|
||||
BudgetExpr::After(Condition::Timestamp(dt, dt_pubkey), Payment { tokens, to })
|
||||
};
|
||||
let instruction = Instruction::NewBudget(expr);
|
||||
Self::new(
|
||||
from_keypair,
|
||||
&[contract],
|
||||
budget_program::id(),
|
||||
&instruction,
|
||||
last_id,
|
||||
0,
|
||||
)
|
||||
}
|
||||
/// Create and sign a multisig Transaction.
|
||||
fn budget_new_when_signed(
|
||||
from_keypair: &Keypair,
|
||||
to: Pubkey,
|
||||
contract: Pubkey,
|
||||
witness: Pubkey,
|
||||
cancelable: Option<Pubkey>,
|
||||
tokens: u64,
|
||||
last_id: Hash,
|
||||
) -> Self {
|
||||
let expr = if let Some(from) = cancelable {
|
||||
BudgetExpr::Or(
|
||||
(Condition::Signature(witness), Payment { tokens, to }),
|
||||
(Condition::Signature(from), Payment { tokens, to: from }),
|
||||
)
|
||||
} else {
|
||||
BudgetExpr::After(Condition::Signature(witness), Payment { tokens, to })
|
||||
};
|
||||
let instruction = Instruction::NewBudget(expr);
|
||||
Self::new(
|
||||
from_keypair,
|
||||
&[contract],
|
||||
budget_program::id(),
|
||||
&instruction,
|
||||
last_id,
|
||||
0,
|
||||
)
|
||||
}
|
||||
|
||||
fn instruction(&self, instruction_index: usize) -> Option<Instruction> {
|
||||
deserialize(&self.userdata(instruction_index)).ok()
|
||||
}
|
||||
|
||||
fn system_instruction(&self, instruction_index: usize) -> Option<SystemInstruction> {
|
||||
deserialize(&self.userdata(instruction_index)).ok()
|
||||
}
|
||||
|
||||
/// Verify only the payment plan.
|
||||
fn verify_plan(&self) -> bool {
|
||||
if let Some(SystemInstruction::Move { tokens }) = self.system_instruction(0) {
|
||||
if let Some(Instruction::NewBudget(expr)) = self.instruction(1) {
|
||||
if !(self.fee <= tokens && expr.verify(tokens - self.fee)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use bincode::{deserialize, serialize};
|
||||
|
||||
#[test]
|
||||
fn test_claim() {
|
||||
let keypair = Keypair::new();
|
||||
let zero = Hash::default();
|
||||
let tx0 = Transaction::budget_new(&keypair, keypair.pubkey(), 42, zero);
|
||||
assert!(tx0.verify_plan());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_transfer() {
|
||||
let zero = Hash::default();
|
||||
let keypair0 = Keypair::new();
|
||||
let keypair1 = Keypair::new();
|
||||
let pubkey1 = keypair1.pubkey();
|
||||
let tx0 = Transaction::budget_new(&keypair0, pubkey1, 42, zero);
|
||||
assert!(tx0.verify_plan());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_transfer_with_fee() {
|
||||
let zero = Hash::default();
|
||||
let keypair0 = Keypair::new();
|
||||
let pubkey1 = Keypair::new().pubkey();
|
||||
assert!(Transaction::budget_new_taxed(&keypair0, pubkey1, 1, 1, zero).verify_plan());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_serialize_claim() {
|
||||
let expr = BudgetExpr::Pay(Payment {
|
||||
tokens: 0,
|
||||
to: Default::default(),
|
||||
});
|
||||
let instruction = Instruction::NewBudget(expr);
|
||||
let instructions = vec![transaction::Instruction::new(0, &instruction, vec![])];
|
||||
let claim0 = Transaction {
|
||||
account_keys: vec![],
|
||||
last_id: Default::default(),
|
||||
signatures: vec![],
|
||||
program_ids: vec![],
|
||||
instructions,
|
||||
fee: 0,
|
||||
};
|
||||
let buf = serialize(&claim0).unwrap();
|
||||
let claim1: Transaction = deserialize(&buf).unwrap();
|
||||
assert_eq!(claim1, claim0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_token_attack() {
|
||||
let zero = Hash::default();
|
||||
let keypair = Keypair::new();
|
||||
let pubkey = keypair.pubkey();
|
||||
let mut tx = Transaction::budget_new(&keypair, pubkey, 42, zero);
|
||||
let mut system_instruction = tx.system_instruction(0).unwrap();
|
||||
if let SystemInstruction::Move { ref mut tokens } = system_instruction {
|
||||
*tokens = 1_000_000; // <-- attack, part 1!
|
||||
let mut instruction = tx.instruction(1).unwrap();
|
||||
if let Instruction::NewBudget(ref mut expr) = instruction {
|
||||
if let BudgetExpr::Pay(ref mut payment) = expr {
|
||||
payment.tokens = *tokens; // <-- attack, part 2!
|
||||
}
|
||||
}
|
||||
tx.instructions[1].userdata = serialize(&instruction).unwrap();
|
||||
}
|
||||
tx.instructions[0].userdata = serialize(&system_instruction).unwrap();
|
||||
assert!(tx.verify_plan());
|
||||
assert!(!tx.verify_signature());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hijack_attack() {
|
||||
let keypair0 = Keypair::new();
|
||||
let keypair1 = Keypair::new();
|
||||
let thief_keypair = Keypair::new();
|
||||
let pubkey1 = keypair1.pubkey();
|
||||
let zero = Hash::default();
|
||||
let mut tx = Transaction::budget_new(&keypair0, pubkey1, 42, zero);
|
||||
let mut instruction = tx.instruction(1);
|
||||
if let Some(Instruction::NewBudget(ref mut expr)) = instruction {
|
||||
if let BudgetExpr::Pay(ref mut payment) = expr {
|
||||
payment.to = thief_keypair.pubkey(); // <-- attack!
|
||||
}
|
||||
}
|
||||
tx.instructions[1].userdata = serialize(&instruction).unwrap();
|
||||
assert!(tx.verify_plan());
|
||||
assert!(!tx.verify_signature());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_overspend_attack() {
|
||||
let keypair0 = Keypair::new();
|
||||
let keypair1 = Keypair::new();
|
||||
let zero = Hash::default();
|
||||
let mut tx = Transaction::budget_new(&keypair0, keypair1.pubkey(), 1, zero);
|
||||
let mut instruction = tx.instruction(1).unwrap();
|
||||
if let Instruction::NewBudget(ref mut expr) = instruction {
|
||||
if let BudgetExpr::Pay(ref mut payment) = expr {
|
||||
payment.tokens = 2; // <-- attack!
|
||||
}
|
||||
}
|
||||
tx.instructions[1].userdata = serialize(&instruction).unwrap();
|
||||
assert!(!tx.verify_plan());
|
||||
|
||||
// Also, ensure all branchs of the plan spend all tokens
|
||||
let mut instruction = tx.instruction(1).unwrap();
|
||||
if let Instruction::NewBudget(ref mut expr) = instruction {
|
||||
if let BudgetExpr::Pay(ref mut payment) = expr {
|
||||
payment.tokens = 0; // <-- whoops!
|
||||
}
|
||||
}
|
||||
tx.instructions[1].userdata = serialize(&instruction).unwrap();
|
||||
assert!(!tx.verify_plan());
|
||||
}
|
||||
}
|
||||
@@ -272,9 +272,9 @@ pub fn reconstruct_entries_from_blobs(blobs: Vec<SharedBlob>) -> Result<(Vec<Ent
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use budget_transaction::BudgetTransaction;
|
||||
use chrono::prelude::*;
|
||||
use entry::Entry;
|
||||
use solana_sdk::budget_transaction::BudgetTransaction;
|
||||
use solana_sdk::hash::hash;
|
||||
use solana_sdk::signature::{Keypair, KeypairUtil};
|
||||
use solana_sdk::transaction::Transaction;
|
||||
|
||||
@@ -3,24 +3,24 @@
|
||||
//! access read to a persistent file-based ledger.
|
||||
|
||||
use bincode::{self, deserialize_from, serialize_into, serialized_size};
|
||||
use budget_transaction::BudgetTransaction;
|
||||
use chrono::prelude::Utc;
|
||||
use entry::Entry;
|
||||
use log::Level::Trace;
|
||||
use mint::Mint;
|
||||
use packet::{SharedBlob, BLOB_DATA_SIZE};
|
||||
use rayon::prelude::*;
|
||||
use solana_sdk::budget_transaction::BudgetTransaction;
|
||||
use solana_sdk::hash::{hash, Hash};
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
use solana_sdk::signature::{Keypair, KeypairUtil};
|
||||
use solana_sdk::transaction::Transaction;
|
||||
use solana_sdk::vote_program::Vote;
|
||||
use std::fs::{copy, create_dir_all, remove_dir_all, File, OpenOptions};
|
||||
use std::io::prelude::*;
|
||||
use std::io::{self, BufReader, BufWriter, Seek, SeekFrom};
|
||||
use std::mem::size_of;
|
||||
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
|
||||
use std::path::Path;
|
||||
use solana_sdk::vote_program::Vote;
|
||||
use vote_transaction::VoteTransaction;
|
||||
|
||||
//
|
||||
@@ -701,15 +701,14 @@ pub fn make_large_test_entries(num_entries: usize) -> Vec<Entry> {
|
||||
mod tests {
|
||||
use super::*;
|
||||
use bincode::{deserialize, serialized_size};
|
||||
use budget_transaction::BudgetTransaction;
|
||||
use entry::{next_entry, reconstruct_entries_from_blobs, Entry};
|
||||
use packet::{to_blobs, BLOB_DATA_SIZE, PACKET_DATA_SIZE};
|
||||
use solana_sdk::hash::hash;
|
||||
use solana_sdk::signature::{Keypair, KeypairUtil};
|
||||
use solana_sdk::transaction::Transaction;
|
||||
use solana_sdk::vote_program::Vote;
|
||||
use std;
|
||||
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
|
||||
use solana_sdk::vote_program::Vote;
|
||||
|
||||
#[test]
|
||||
fn test_verify_slice() {
|
||||
|
||||
@@ -14,9 +14,6 @@ pub mod banking_stage;
|
||||
pub mod blob_fetch_stage;
|
||||
pub mod bloom;
|
||||
pub mod broadcast_stage;
|
||||
pub mod budget_expr;
|
||||
pub mod budget_instruction;
|
||||
pub mod budget_transaction;
|
||||
#[cfg(feature = "chacha")]
|
||||
pub mod chacha;
|
||||
#[cfg(all(feature = "chacha", feature = "cuda"))]
|
||||
@@ -31,7 +28,6 @@ pub mod crds_traits_impls;
|
||||
pub mod crds_value;
|
||||
#[macro_use]
|
||||
pub mod contact_info;
|
||||
pub mod budget_program;
|
||||
pub mod cluster_info;
|
||||
pub mod compute_leader_finality_service;
|
||||
pub mod db_ledger;
|
||||
@@ -51,7 +47,6 @@ pub mod native_loader;
|
||||
pub mod ncp;
|
||||
pub mod netutil;
|
||||
pub mod packet;
|
||||
pub mod payment_plan;
|
||||
pub mod poh;
|
||||
pub mod poh_recorder;
|
||||
pub mod poh_service;
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
//! The `plan` module provides a domain-specific language for payment plans. Users create BudgetExpr objects that
|
||||
//! are given to an interpreter. The interpreter listens for `Witness` transactions,
|
||||
//! which it uses to reduce the payment plan. When the plan is reduced to a
|
||||
//! `Payment`, the payment is executed.
|
||||
|
||||
use chrono::prelude::*;
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
|
||||
/// The types of events a payment plan can process.
|
||||
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
|
||||
pub enum Witness {
|
||||
/// The current time.
|
||||
Timestamp(DateTime<Utc>),
|
||||
|
||||
/// A signature from Pubkey.
|
||||
Signature,
|
||||
}
|
||||
|
||||
/// Some amount of tokens that should be sent to the `to` `Pubkey`.
|
||||
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
|
||||
pub struct Payment {
|
||||
/// Amount to be paid.
|
||||
pub tokens: u64,
|
||||
|
||||
/// The `Pubkey` that `tokens` should be paid to.
|
||||
pub to: Pubkey,
|
||||
}
|
||||
@@ -246,10 +246,10 @@ impl RpcSolPubSub for RpcSolPubSubImpl {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use budget_program;
|
||||
use budget_transaction::BudgetTransaction;
|
||||
use jsonrpc_core::futures::sync::mpsc;
|
||||
use mint::Mint;
|
||||
use solana_sdk::budget_program;
|
||||
use solana_sdk::budget_transaction::BudgetTransaction;
|
||||
use solana_sdk::signature::{Keypair, KeypairUtil};
|
||||
use solana_sdk::transaction::Transaction;
|
||||
use std::net::{IpAddr, Ipv4Addr};
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
use budget_program;
|
||||
use native_loader;
|
||||
use solana_sdk::account::{create_keyed_accounts, Account, KeyedAccount};
|
||||
use solana_sdk::native_program::ProgramError;
|
||||
@@ -13,10 +12,6 @@ pub enum RuntimeError {
|
||||
ProgramError(u8, ProgramError),
|
||||
}
|
||||
|
||||
pub fn is_legacy_program(program_id: &Pubkey) -> bool {
|
||||
budget_program::check_id(program_id)
|
||||
}
|
||||
|
||||
/// Process an instruction
|
||||
/// This method calls the instruction's program entrypoint method
|
||||
fn process_instruction(
|
||||
@@ -28,36 +23,25 @@ fn process_instruction(
|
||||
) -> Result<(), ProgramError> {
|
||||
let program_id = tx.program_id(instruction_index);
|
||||
|
||||
// Call the program method
|
||||
// It's up to the program to implement its own rules on moving funds
|
||||
if is_legacy_program(&program_id) {
|
||||
if budget_program::check_id(&program_id) {
|
||||
budget_program::process(&tx, instruction_index, program_accounts)?;
|
||||
} else {
|
||||
unreachable!();
|
||||
};
|
||||
Ok(())
|
||||
} else {
|
||||
let mut keyed_accounts = create_keyed_accounts(executable_accounts);
|
||||
let mut keyed_accounts2: Vec<_> = tx.instructions[instruction_index]
|
||||
.accounts
|
||||
.iter()
|
||||
.map(|&index| {
|
||||
let index = index as usize;
|
||||
let key = &tx.account_keys[index];
|
||||
(key, index < tx.signatures.len())
|
||||
}).zip(program_accounts.iter_mut())
|
||||
.map(|((key, is_signer), account)| KeyedAccount::new(key, is_signer, account))
|
||||
.collect();
|
||||
keyed_accounts.append(&mut keyed_accounts2);
|
||||
let mut keyed_accounts = create_keyed_accounts(executable_accounts);
|
||||
let mut keyed_accounts2: Vec<_> = tx.instructions[instruction_index]
|
||||
.accounts
|
||||
.iter()
|
||||
.map(|&index| {
|
||||
let index = index as usize;
|
||||
let key = &tx.account_keys[index];
|
||||
(key, index < tx.signatures.len())
|
||||
}).zip(program_accounts.iter_mut())
|
||||
.map(|((key, is_signer), account)| KeyedAccount::new(key, is_signer, account))
|
||||
.collect();
|
||||
keyed_accounts.append(&mut keyed_accounts2);
|
||||
|
||||
native_loader::process_instruction(
|
||||
&program_id,
|
||||
&mut keyed_accounts,
|
||||
&tx.instructions[instruction_index].userdata,
|
||||
tick_height,
|
||||
)
|
||||
}
|
||||
native_loader::process_instruction(
|
||||
&program_id,
|
||||
&mut keyed_accounts,
|
||||
&tx.instructions[instruction_index].userdata,
|
||||
tick_height,
|
||||
)
|
||||
}
|
||||
|
||||
fn verify_instruction(
|
||||
|
||||
@@ -323,9 +323,9 @@ pub fn make_packet_from_transaction(tx: Transaction) -> Packet {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use bincode::serialize;
|
||||
use budget_program;
|
||||
use packet::{Packet, SharedPackets};
|
||||
use sigverify;
|
||||
use solana_sdk::budget_program;
|
||||
use solana_sdk::hash::Hash;
|
||||
use solana_sdk::signature::{Keypair, KeypairUtil};
|
||||
use solana_sdk::system_instruction::SystemInstruction;
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
use bincode::serialize;
|
||||
use bs58;
|
||||
use budget_program;
|
||||
use budget_transaction::BudgetTransaction;
|
||||
use chrono::prelude::*;
|
||||
use clap::ArgMatches;
|
||||
use elf;
|
||||
@@ -14,6 +12,8 @@ use rpc_request::{get_rpc_request_str, RpcClient, RpcRequest};
|
||||
use serde_json;
|
||||
use solana_drone::drone::{request_airdrop_transaction, DRONE_PORT};
|
||||
use solana_sdk::bpf_loader;
|
||||
use solana_sdk::budget_program;
|
||||
use solana_sdk::budget_transaction::BudgetTransaction;
|
||||
use solana_sdk::hash::Hash;
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
use solana_sdk::signature::{Keypair, KeypairUtil, Signature};
|
||||
|
||||
Reference in New Issue
Block a user