2018-09-19 17:25:57 -07:00
|
|
|
//! budget program
|
2018-09-17 13:36:31 -07:00
|
|
|
use bincode::{self, deserialize, serialize_into, serialized_size};
|
|
|
|
use chrono::prelude::{DateTime, Utc};
|
2018-12-14 20:39:10 -08:00
|
|
|
use log::*;
|
|
|
|
use serde_derive::{Deserialize, Serialize};
|
2019-03-02 13:23:22 -08:00
|
|
|
use solana_budget_api::budget_expr::BudgetExpr;
|
|
|
|
use solana_budget_api::budget_instruction::Instruction;
|
|
|
|
use solana_budget_api::payment_plan::Witness;
|
2018-12-04 14:38:19 -08:00
|
|
|
use solana_sdk::account::KeyedAccount;
|
2018-09-17 13:36:31 -07:00
|
|
|
use std::io;
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
|
|
|
|
pub enum BudgetError {
|
2018-09-28 16:16:35 -07:00
|
|
|
InsufficientFunds,
|
|
|
|
ContractAlreadyExists,
|
|
|
|
ContractNotPending,
|
|
|
|
SourceIsPendingContract,
|
|
|
|
UninitializedContract,
|
2018-09-17 13:36:31 -07:00
|
|
|
NegativeTokens,
|
2018-09-28 16:16:35 -07:00
|
|
|
DestinationMissing,
|
2018-09-18 18:45:44 -07:00
|
|
|
FailedWitness,
|
2018-09-20 09:38:37 -07:00
|
|
|
UserdataTooSmall,
|
2018-09-21 13:21:01 -07:00
|
|
|
UserdataDeserializeFailure,
|
2018-10-18 20:10:33 -07:00
|
|
|
UnsignedKey,
|
2018-09-17 13:36:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
|
2018-11-23 11:12:56 -08:00
|
|
|
pub struct BudgetProgram {
|
2018-09-17 13:36:31 -07:00
|
|
|
pub initialized: bool,
|
2018-11-02 19:13:33 -07:00
|
|
|
pub pending_budget: Option<BudgetExpr>,
|
2018-09-17 13:36:31 -07:00
|
|
|
}
|
|
|
|
|
2018-11-23 12:45:34 -08:00
|
|
|
fn apply_debits(
|
2018-12-04 14:38:19 -08:00
|
|
|
keyed_accounts: &mut [KeyedAccount],
|
2018-11-23 12:45:34 -08:00
|
|
|
instruction: &Instruction,
|
|
|
|
) -> Result<(), BudgetError> {
|
2018-12-04 14:38:19 -08:00
|
|
|
if !keyed_accounts[0].account.userdata.is_empty() {
|
2018-11-23 12:45:34 -08:00
|
|
|
trace!("source is pending");
|
|
|
|
return Err(BudgetError::SourceIsPendingContract);
|
2018-09-17 13:36:31 -07:00
|
|
|
}
|
2018-11-23 12:45:34 -08:00
|
|
|
match instruction {
|
|
|
|
Instruction::NewBudget(expr) => {
|
|
|
|
let expr = expr.clone();
|
|
|
|
if let Some(payment) = expr.final_payment() {
|
2018-12-04 14:38:19 -08:00
|
|
|
keyed_accounts[1].account.tokens += payment.tokens;
|
2018-11-23 12:45:34 -08:00
|
|
|
Ok(())
|
|
|
|
} else {
|
2018-12-04 14:38:19 -08:00
|
|
|
let existing = BudgetProgram::deserialize(&keyed_accounts[1].account.userdata).ok();
|
2018-11-23 12:45:34 -08:00
|
|
|
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);
|
2018-12-04 14:38:19 -08:00
|
|
|
keyed_accounts[1].account.tokens += keyed_accounts[0].account.tokens;
|
|
|
|
keyed_accounts[0].account.tokens = 0;
|
2018-11-23 12:45:34 -08:00
|
|
|
program.initialized = true;
|
2018-12-04 14:38:19 -08:00
|
|
|
program.serialize(&mut keyed_accounts[1].account.userdata)
|
2018-11-23 12:45:34 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Instruction::ApplyTimestamp(dt) => {
|
2018-12-04 14:38:19 -08:00
|
|
|
if let Ok(mut program) = BudgetProgram::deserialize(&keyed_accounts[1].account.userdata)
|
|
|
|
{
|
2018-11-23 12:45:34 -08:00
|
|
|
if !program.is_pending() {
|
|
|
|
Err(BudgetError::ContractNotPending)
|
|
|
|
} else if !program.initialized {
|
|
|
|
trace!("contract is uninitialized");
|
|
|
|
Err(BudgetError::UninitializedContract)
|
|
|
|
} else {
|
|
|
|
trace!("apply timestamp");
|
2018-12-04 14:38:19 -08:00
|
|
|
program.apply_timestamp(keyed_accounts, *dt)?;
|
2018-11-23 12:45:34 -08:00
|
|
|
trace!("apply timestamp committed");
|
2018-12-04 14:38:19 -08:00
|
|
|
program.serialize(&mut keyed_accounts[1].account.userdata)
|
2018-11-23 12:45:34 -08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Err(BudgetError::UninitializedContract)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Instruction::ApplySignature => {
|
2018-12-04 14:38:19 -08:00
|
|
|
if let Ok(mut program) = BudgetProgram::deserialize(&keyed_accounts[1].account.userdata)
|
|
|
|
{
|
2018-11-23 12:45:34 -08:00
|
|
|
if !program.is_pending() {
|
|
|
|
Err(BudgetError::ContractNotPending)
|
|
|
|
} else if !program.initialized {
|
|
|
|
trace!("contract is uninitialized");
|
|
|
|
Err(BudgetError::UninitializedContract)
|
|
|
|
} else {
|
|
|
|
trace!("apply signature");
|
2018-12-04 14:38:19 -08:00
|
|
|
program.apply_signature(keyed_accounts)?;
|
2018-11-23 12:45:34 -08:00
|
|
|
trace!("apply signature committed");
|
2018-12-04 14:38:19 -08:00
|
|
|
program.serialize(&mut keyed_accounts[1].account.userdata)
|
2018-11-23 12:45:34 -08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Err(BudgetError::UninitializedContract)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Budget DSL contract interface
|
|
|
|
/// * 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 .
|
2018-11-23 14:14:13 -08:00
|
|
|
pub fn process_instruction(
|
2018-12-04 14:38:19 -08:00
|
|
|
keyed_accounts: &mut [KeyedAccount],
|
|
|
|
data: &[u8],
|
2018-11-23 12:45:34 -08:00
|
|
|
) -> Result<(), BudgetError> {
|
2018-12-04 14:38:19 -08:00
|
|
|
if let Ok(instruction) = deserialize(data) {
|
2018-11-23 14:14:13 -08:00
|
|
|
trace!("process_instruction: {:?}", instruction);
|
2018-12-04 14:38:19 -08:00
|
|
|
apply_debits(keyed_accounts, &instruction)
|
2018-11-23 12:45:34 -08:00
|
|
|
} else {
|
2018-12-04 14:38:19 -08:00
|
|
|
info!("Invalid transaction userdata: {:?}", data);
|
2018-11-23 12:45:34 -08:00
|
|
|
Err(BudgetError::UserdataDeserializeFailure)
|
2018-09-17 13:36:31 -07:00
|
|
|
}
|
2018-11-23 12:45:34 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl BudgetProgram {
|
|
|
|
fn is_pending(&self) -> bool {
|
|
|
|
self.pending_budget != None
|
|
|
|
}
|
2018-09-17 13:36:31 -07:00
|
|
|
/// Process a Witness Signature. Any payment plans waiting on this signature
|
|
|
|
/// will progress one step.
|
2018-12-04 14:38:19 -08:00
|
|
|
fn apply_signature(&mut self, keyed_accounts: &mut [KeyedAccount]) -> Result<(), BudgetError> {
|
2018-09-18 18:45:44 -07:00
|
|
|
let mut final_payment = None;
|
2018-11-02 19:13:33 -07:00
|
|
|
if let Some(ref mut expr) = self.pending_budget {
|
2018-12-04 14:38:19 -08:00
|
|
|
let key = match keyed_accounts[0].signer_key() {
|
2018-10-18 20:10:33 -07:00
|
|
|
None => return Err(BudgetError::UnsignedKey),
|
|
|
|
Some(key) => key,
|
|
|
|
};
|
2018-11-02 19:13:33 -07:00
|
|
|
expr.apply_witness(&Witness::Signature, key);
|
|
|
|
final_payment = expr.final_payment();
|
2018-09-18 18:45:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(payment) = final_payment {
|
2019-02-07 11:14:10 -08:00
|
|
|
if let Some(key) = keyed_accounts[0].signer_key() {
|
|
|
|
if &payment.to == key {
|
|
|
|
self.pending_budget = None;
|
|
|
|
keyed_accounts[1].account.tokens -= payment.tokens;
|
|
|
|
keyed_accounts[0].account.tokens += payment.tokens;
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
}
|
2018-12-04 14:38:19 -08:00
|
|
|
if &payment.to != keyed_accounts[2].unsigned_key() {
|
2018-09-18 18:45:44 -07:00
|
|
|
trace!("destination missing");
|
2018-09-28 16:16:35 -07:00
|
|
|
return Err(BudgetError::DestinationMissing);
|
2018-09-17 13:36:31 -07:00
|
|
|
}
|
2018-09-20 12:50:11 -07:00
|
|
|
self.pending_budget = None;
|
2018-12-04 14:38:19 -08:00
|
|
|
keyed_accounts[1].account.tokens -= payment.tokens;
|
|
|
|
keyed_accounts[2].account.tokens += payment.tokens;
|
2018-09-17 13:36:31 -07:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Process a Witness Timestamp. Any payment plans waiting on this timestamp
|
|
|
|
/// will progress one step.
|
|
|
|
fn apply_timestamp(
|
|
|
|
&mut self,
|
2018-12-04 14:38:19 -08:00
|
|
|
keyed_accounts: &mut [KeyedAccount],
|
2018-09-17 13:36:31 -07:00
|
|
|
dt: DateTime<Utc>,
|
|
|
|
) -> Result<(), BudgetError> {
|
|
|
|
// Check to see if any timelocked transactions can be completed.
|
2018-09-18 18:45:44 -07:00
|
|
|
let mut final_payment = None;
|
2018-09-17 13:36:31 -07:00
|
|
|
|
2018-11-02 19:13:33 -07:00
|
|
|
if let Some(ref mut expr) = self.pending_budget {
|
2018-12-04 14:38:19 -08:00
|
|
|
let key = match keyed_accounts[0].signer_key() {
|
2018-10-18 20:10:33 -07:00
|
|
|
None => return Err(BudgetError::UnsignedKey),
|
|
|
|
Some(key) => key,
|
|
|
|
};
|
2018-11-02 19:13:33 -07:00
|
|
|
expr.apply_witness(&Witness::Timestamp(dt), key);
|
|
|
|
final_payment = expr.final_payment();
|
2018-09-17 13:36:31 -07:00
|
|
|
}
|
2018-09-18 18:45:44 -07:00
|
|
|
|
|
|
|
if let Some(payment) = final_payment {
|
2018-12-04 14:38:19 -08:00
|
|
|
if &payment.to != keyed_accounts[2].unsigned_key() {
|
2018-09-18 18:45:44 -07:00
|
|
|
trace!("destination missing");
|
2018-09-28 16:16:35 -07:00
|
|
|
return Err(BudgetError::DestinationMissing);
|
2018-09-18 18:45:44 -07:00
|
|
|
}
|
2018-09-20 12:50:11 -07:00
|
|
|
self.pending_budget = None;
|
2018-12-04 14:38:19 -08:00
|
|
|
keyed_accounts[1].account.tokens -= payment.tokens;
|
|
|
|
keyed_accounts[2].account.tokens += payment.tokens;
|
2018-09-17 13:36:31 -07:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-09-20 09:38:37 -07:00
|
|
|
fn serialize(&self, output: &mut [u8]) -> Result<(), BudgetError> {
|
2018-09-17 13:36:31 -07:00
|
|
|
let len = serialized_size(self).unwrap() as u64;
|
2018-09-20 09:38:37 -07:00
|
|
|
if output.len() < len as usize {
|
|
|
|
warn!(
|
|
|
|
"{} bytes required to serialize, only have {} bytes",
|
|
|
|
len,
|
|
|
|
output.len()
|
|
|
|
);
|
|
|
|
return Err(BudgetError::UserdataTooSmall);
|
|
|
|
}
|
2018-09-17 13:36:31 -07:00
|
|
|
{
|
|
|
|
let writer = io::BufWriter::new(&mut output[..8]);
|
|
|
|
serialize_into(writer, &len).unwrap();
|
|
|
|
}
|
2018-09-20 09:38:37 -07:00
|
|
|
|
2018-09-17 13:36:31 -07:00
|
|
|
{
|
|
|
|
let writer = io::BufWriter::new(&mut output[8..8 + len as usize]);
|
|
|
|
serialize_into(writer, self).unwrap();
|
|
|
|
}
|
2018-09-20 09:38:37 -07:00
|
|
|
Ok(())
|
2018-09-17 13:36:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2018-09-24 16:00:55 -07:00
|
|
|
if len < 2 {
|
2018-09-17 13:36:31 -07:00
|
|
|
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])
|
|
|
|
}
|
|
|
|
}
|
2018-12-04 14:38:19 -08:00
|
|
|
|
2018-09-17 13:36:31 -07:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2018-11-23 12:45:34 -08:00
|
|
|
use super::*;
|
2018-09-17 13:36:31 -07:00
|
|
|
use bincode::serialize;
|
2019-03-02 13:23:22 -08:00
|
|
|
use solana_budget_api::budget_transaction::BudgetTransaction;
|
|
|
|
use solana_budget_api::id;
|
2018-10-25 11:13:08 -07:00
|
|
|
use solana_sdk::account::Account;
|
2018-11-16 08:04:46 -08:00
|
|
|
use solana_sdk::hash::Hash;
|
2018-12-03 10:26:28 -08:00
|
|
|
use solana_sdk::signature::{Keypair, KeypairUtil};
|
2018-12-04 14:38:19 -08:00
|
|
|
use solana_sdk::transaction::{Instruction, Transaction};
|
2018-09-26 09:33:52 -07:00
|
|
|
|
2018-12-04 14:38:19 -08:00
|
|
|
fn process_transaction(
|
|
|
|
tx: &Transaction,
|
|
|
|
program_accounts: &mut [Account],
|
|
|
|
) -> Result<(), BudgetError> {
|
|
|
|
assert_eq!(tx.instructions.len(), 1);
|
|
|
|
let Instruction {
|
|
|
|
ref accounts,
|
|
|
|
ref userdata,
|
|
|
|
..
|
|
|
|
} = tx.instructions[0];
|
|
|
|
|
|
|
|
let mut keyed_accounts: Vec<_> = accounts
|
|
|
|
.iter()
|
|
|
|
.map(|&index| {
|
|
|
|
let index = index as usize;
|
|
|
|
let key = &tx.account_keys[index];
|
|
|
|
(key, index < tx.signatures.len())
|
2018-12-07 19:01:28 -08:00
|
|
|
})
|
|
|
|
.zip(program_accounts.iter_mut())
|
2018-12-04 14:38:19 -08:00
|
|
|
.map(|((key, is_signer), account)| KeyedAccount::new(key, is_signer, account))
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
super::process_instruction(&mut keyed_accounts, &userdata)
|
2018-09-28 16:16:35 -07:00
|
|
|
}
|
2018-09-17 13:36:31 -07:00
|
|
|
#[test]
|
|
|
|
fn test_serializer() {
|
2018-11-23 12:45:34 -08:00
|
|
|
let mut a = Account::new(0, 512, id());
|
2018-11-23 11:12:56 -08:00
|
|
|
let b = BudgetProgram::default();
|
2018-09-20 09:38:37 -07:00
|
|
|
b.serialize(&mut a.userdata).unwrap();
|
2018-09-17 13:36:31 -07:00
|
|
|
let buf = serialize(&b).unwrap();
|
|
|
|
assert_eq!(a.userdata[8..8 + buf.len()], buf[0..]);
|
2018-11-23 11:12:56 -08:00
|
|
|
let c = BudgetProgram::deserialize(&a.userdata).unwrap();
|
2018-09-17 13:36:31 -07:00
|
|
|
assert_eq!(b, c);
|
|
|
|
}
|
|
|
|
|
2018-09-20 09:38:37 -07:00
|
|
|
#[test]
|
|
|
|
fn test_serializer_userdata_too_small() {
|
2018-11-23 12:45:34 -08:00
|
|
|
let mut a = Account::new(0, 1, id());
|
2018-11-23 11:12:56 -08:00
|
|
|
let b = BudgetProgram::default();
|
2018-09-20 09:38:37 -07:00
|
|
|
assert_eq!(
|
|
|
|
b.serialize(&mut a.userdata),
|
|
|
|
Err(BudgetError::UserdataTooSmall)
|
|
|
|
);
|
|
|
|
}
|
2018-09-20 13:54:42 -07:00
|
|
|
#[test]
|
|
|
|
fn test_invalid_instruction() {
|
2018-11-23 12:45:34 -08:00
|
|
|
let mut accounts = vec![Account::new(1, 0, id()), Account::new(0, 512, id())];
|
2018-09-20 13:54:42 -07:00
|
|
|
let from = Keypair::new();
|
|
|
|
let contract = Keypair::new();
|
2018-11-06 05:50:00 -08:00
|
|
|
let userdata = (1u8, 2u8, 3u8);
|
2018-09-26 08:54:04 -07:00
|
|
|
let tx = Transaction::new(
|
2018-09-20 13:54:42 -07:00
|
|
|
&from,
|
|
|
|
&[contract.pubkey()],
|
2018-11-23 12:45:34 -08:00
|
|
|
id(),
|
2018-11-06 05:50:00 -08:00
|
|
|
&userdata,
|
2018-09-20 13:54:42 -07:00
|
|
|
Hash::default(),
|
|
|
|
0,
|
|
|
|
);
|
2018-09-28 16:16:35 -07:00
|
|
|
assert!(process_transaction(&tx, &mut accounts).is_err());
|
2018-09-20 13:54:42 -07:00
|
|
|
}
|
2018-09-20 09:38:37 -07:00
|
|
|
|
2018-10-18 20:10:33 -07:00
|
|
|
#[test]
|
|
|
|
fn test_unsigned_witness_key() {
|
|
|
|
let mut accounts = vec![
|
2018-11-23 12:45:34 -08:00
|
|
|
Account::new(1, 0, id()),
|
|
|
|
Account::new(0, 512, id()),
|
|
|
|
Account::new(0, 0, id()),
|
2018-10-18 20:10:33 -07:00
|
|
|
];
|
|
|
|
|
2018-11-23 11:12:56 -08:00
|
|
|
// Initialize BudgetProgram
|
2018-10-18 20:10:33 -07:00
|
|
|
let from = Keypair::new();
|
|
|
|
let contract = Keypair::new().pubkey();
|
|
|
|
let to = Keypair::new().pubkey();
|
|
|
|
let witness = Keypair::new().pubkey();
|
2019-02-01 07:36:35 -08:00
|
|
|
let tx = BudgetTransaction::new_when_signed(
|
2018-10-18 20:10:33 -07:00
|
|
|
&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();
|
2019-02-01 07:36:35 -08:00
|
|
|
let mut tx = BudgetTransaction::new_signature(&rando, contract, to, Hash::default());
|
2018-10-18 20:10:33 -07:00
|
|
|
|
|
|
|
// 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![
|
2018-11-23 12:45:34 -08:00
|
|
|
Account::new(1, 0, id()),
|
|
|
|
Account::new(0, 512, id()),
|
|
|
|
Account::new(0, 0, id()),
|
2018-10-18 20:10:33 -07:00
|
|
|
];
|
|
|
|
|
2018-11-23 11:12:56 -08:00
|
|
|
// Initialize BudgetProgram
|
2018-10-18 20:10:33 -07:00
|
|
|
let from = Keypair::new();
|
|
|
|
let contract = Keypair::new().pubkey();
|
|
|
|
let to = Keypair::new().pubkey();
|
|
|
|
let dt = Utc::now();
|
2019-02-01 07:36:35 -08:00
|
|
|
let tx = BudgetTransaction::new_on_date(
|
2018-10-18 20:10:33 -07:00
|
|
|
&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();
|
2019-02-01 07:36:35 -08:00
|
|
|
let mut tx = BudgetTransaction::new_timestamp(&rando, contract, to, dt, Hash::default());
|
2018-10-18 20:10:33 -07:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-09-17 13:36:31 -07:00
|
|
|
#[test]
|
|
|
|
fn test_transfer_on_date() {
|
|
|
|
let mut accounts = vec![
|
2018-11-23 12:45:34 -08:00
|
|
|
Account::new(1, 0, id()),
|
|
|
|
Account::new(0, 512, id()),
|
|
|
|
Account::new(0, 0, id()),
|
2018-09-17 13:36:31 -07:00
|
|
|
];
|
|
|
|
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();
|
2019-02-01 07:36:35 -08:00
|
|
|
let tx = BudgetTransaction::new_on_date(
|
2018-09-17 13:36:31 -07:00
|
|
|
&from,
|
|
|
|
to.pubkey(),
|
|
|
|
contract.pubkey(),
|
|
|
|
dt,
|
2018-09-19 16:44:03 -07:00
|
|
|
from.pubkey(),
|
2018-09-22 16:51:21 -07:00
|
|
|
None,
|
2018-09-17 13:36:31 -07:00
|
|
|
1,
|
|
|
|
Hash::default(),
|
|
|
|
);
|
2018-09-28 16:16:35 -07:00
|
|
|
process_transaction(&tx, &mut accounts).unwrap();
|
2018-09-17 13:36:31 -07:00
|
|
|
assert_eq!(accounts[from_account].tokens, 0);
|
|
|
|
assert_eq!(accounts[contract_account].tokens, 1);
|
2018-11-23 11:12:56 -08:00
|
|
|
let program = BudgetProgram::deserialize(&accounts[contract_account].userdata).unwrap();
|
|
|
|
assert!(program.is_pending());
|
2018-09-17 13:36:31 -07:00
|
|
|
|
|
|
|
// Attack! Try to payout to a rando key
|
2019-02-01 07:36:35 -08:00
|
|
|
let tx = BudgetTransaction::new_timestamp(
|
2018-09-17 13:36:31 -07:00
|
|
|
&from,
|
|
|
|
contract.pubkey(),
|
|
|
|
rando.pubkey(),
|
|
|
|
dt,
|
|
|
|
Hash::default(),
|
|
|
|
);
|
2018-09-24 16:00:55 -07:00
|
|
|
assert_eq!(
|
2018-09-28 16:16:35 -07:00
|
|
|
process_transaction(&tx, &mut accounts),
|
|
|
|
Err(BudgetError::DestinationMissing)
|
2018-09-24 16:00:55 -07:00
|
|
|
);
|
2018-09-17 13:36:31 -07:00
|
|
|
assert_eq!(accounts[from_account].tokens, 0);
|
|
|
|
assert_eq!(accounts[contract_account].tokens, 1);
|
|
|
|
assert_eq!(accounts[to_account].tokens, 0);
|
|
|
|
|
2018-11-23 11:12:56 -08:00
|
|
|
let program = BudgetProgram::deserialize(&accounts[contract_account].userdata).unwrap();
|
|
|
|
assert!(program.is_pending());
|
2018-09-17 13:36:31 -07:00
|
|
|
|
|
|
|
// Now, acknowledge the time in the condition occurred and
|
|
|
|
// that pubkey's funds are now available.
|
2019-02-01 07:36:35 -08:00
|
|
|
let tx = BudgetTransaction::new_timestamp(
|
2018-09-17 13:36:31 -07:00
|
|
|
&from,
|
|
|
|
contract.pubkey(),
|
|
|
|
to.pubkey(),
|
|
|
|
dt,
|
|
|
|
Hash::default(),
|
|
|
|
);
|
2018-09-28 16:16:35 -07:00
|
|
|
process_transaction(&tx, &mut accounts).unwrap();
|
2018-09-17 13:36:31 -07:00
|
|
|
assert_eq!(accounts[from_account].tokens, 0);
|
|
|
|
assert_eq!(accounts[contract_account].tokens, 0);
|
|
|
|
assert_eq!(accounts[to_account].tokens, 1);
|
|
|
|
|
2018-11-23 11:12:56 -08:00
|
|
|
let program = BudgetProgram::deserialize(&accounts[contract_account].userdata).unwrap();
|
|
|
|
assert!(!program.is_pending());
|
2018-09-17 13:36:31 -07:00
|
|
|
|
|
|
|
// try to replay the timestamp contract
|
2018-09-24 16:00:55 -07:00
|
|
|
assert_eq!(
|
2018-09-28 16:16:35 -07:00
|
|
|
process_transaction(&tx, &mut accounts),
|
|
|
|
Err(BudgetError::ContractNotPending)
|
2018-09-24 16:00:55 -07:00
|
|
|
);
|
2018-09-17 13:36:31 -07:00
|
|
|
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![
|
2018-11-23 12:45:34 -08:00
|
|
|
Account::new(1, 0, id()),
|
|
|
|
Account::new(0, 512, id()),
|
|
|
|
Account::new(0, 0, id()),
|
2018-09-17 13:36:31 -07:00
|
|
|
];
|
|
|
|
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();
|
2019-02-01 07:36:35 -08:00
|
|
|
let tx = BudgetTransaction::new_on_date(
|
2018-09-17 13:36:31 -07:00
|
|
|
&from,
|
|
|
|
to.pubkey(),
|
|
|
|
contract.pubkey(),
|
|
|
|
dt,
|
2018-09-19 16:44:03 -07:00
|
|
|
from.pubkey(),
|
2018-09-22 16:51:21 -07:00
|
|
|
Some(from.pubkey()),
|
2018-09-17 13:36:31 -07:00
|
|
|
1,
|
|
|
|
Hash::default(),
|
|
|
|
);
|
2018-09-28 16:16:35 -07:00
|
|
|
process_transaction(&tx, &mut accounts).unwrap();
|
2018-09-17 13:36:31 -07:00
|
|
|
assert_eq!(accounts[from_account].tokens, 0);
|
|
|
|
assert_eq!(accounts[contract_account].tokens, 1);
|
2018-11-23 11:12:56 -08:00
|
|
|
let program = BudgetProgram::deserialize(&accounts[contract_account].userdata).unwrap();
|
|
|
|
assert!(program.is_pending());
|
2018-09-17 13:36:31 -07:00
|
|
|
|
|
|
|
// Attack! try to put the tokens into the wrong account with cancel
|
2018-09-18 18:45:44 -07:00
|
|
|
let tx =
|
2019-02-01 07:36:35 -08:00
|
|
|
BudgetTransaction::new_signature(&to, contract.pubkey(), to.pubkey(), Hash::default());
|
2018-09-17 13:36:31 -07:00
|
|
|
// unit test hack, the `from account` is passed instead of the `to` account to avoid
|
|
|
|
// creating more account vectors
|
2018-09-28 16:16:35 -07:00
|
|
|
process_transaction(&tx, &mut accounts).unwrap();
|
2018-09-17 13:36:31 -07:00
|
|
|
// 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);
|
2019-02-07 11:14:10 -08:00
|
|
|
// this is the `to.pubkey()` account
|
2018-09-17 13:36:31 -07:00
|
|
|
assert_eq!(accounts[pay_account].tokens, 0);
|
|
|
|
|
|
|
|
// Now, cancel the transaction. from gets her funds back
|
2019-02-01 07:36:35 -08:00
|
|
|
let tx = BudgetTransaction::new_signature(
|
2018-09-17 13:36:31 -07:00
|
|
|
&from,
|
|
|
|
contract.pubkey(),
|
|
|
|
from.pubkey(),
|
|
|
|
Hash::default(),
|
|
|
|
);
|
2018-09-28 16:16:35 -07:00
|
|
|
process_transaction(&tx, &mut accounts).unwrap();
|
2019-02-07 11:14:10 -08:00
|
|
|
assert_eq!(accounts[from_account].tokens, 1);
|
2018-09-17 13:36:31 -07:00
|
|
|
assert_eq!(accounts[contract_account].tokens, 0);
|
2019-02-07 11:14:10 -08:00
|
|
|
assert_eq!(accounts[pay_account].tokens, 0);
|
2018-09-17 13:36:31 -07:00
|
|
|
|
2019-02-07 11:14:10 -08:00
|
|
|
// try to replay the cancel contract
|
2019-02-01 07:36:35 -08:00
|
|
|
let tx = BudgetTransaction::new_signature(
|
2018-09-17 13:36:31 -07:00
|
|
|
&from,
|
|
|
|
contract.pubkey(),
|
|
|
|
from.pubkey(),
|
|
|
|
Hash::default(),
|
|
|
|
);
|
2018-09-24 16:00:55 -07:00
|
|
|
assert_eq!(
|
2018-09-28 16:16:35 -07:00
|
|
|
process_transaction(&tx, &mut accounts),
|
|
|
|
Err(BudgetError::ContractNotPending)
|
2018-09-24 16:00:55 -07:00
|
|
|
);
|
2019-02-07 11:14:10 -08:00
|
|
|
assert_eq!(accounts[from_account].tokens, 1);
|
2018-09-17 13:36:31 -07:00
|
|
|
assert_eq!(accounts[contract_account].tokens, 0);
|
2019-02-07 11:14:10 -08:00
|
|
|
assert_eq!(accounts[pay_account].tokens, 0);
|
2018-09-17 13:36:31 -07:00
|
|
|
}
|
2018-09-17 21:09:11 -07:00
|
|
|
|
2018-09-20 09:38:37 -07:00
|
|
|
#[test]
|
|
|
|
fn test_userdata_too_small() {
|
|
|
|
let mut accounts = vec![
|
2018-11-23 12:45:34 -08:00
|
|
|
Account::new(1, 0, id()),
|
|
|
|
Account::new(1, 0, id()), // <== userdata is 0, which is not enough
|
|
|
|
Account::new(1, 0, id()),
|
2018-09-20 09:38:37 -07:00
|
|
|
];
|
|
|
|
let from = Keypair::new();
|
|
|
|
let contract = Keypair::new();
|
|
|
|
let to = Keypair::new();
|
2019-02-01 07:36:35 -08:00
|
|
|
let tx = BudgetTransaction::new_on_date(
|
2018-09-20 09:38:37 -07:00
|
|
|
&from,
|
|
|
|
to.pubkey(),
|
|
|
|
contract.pubkey(),
|
|
|
|
Utc::now(),
|
2018-09-21 18:51:42 -07:00
|
|
|
from.pubkey(),
|
2018-09-22 16:51:21 -07:00
|
|
|
None,
|
2018-09-20 09:38:37 -07:00
|
|
|
1,
|
|
|
|
Hash::default(),
|
|
|
|
);
|
|
|
|
|
2018-09-28 16:16:35 -07:00
|
|
|
assert!(process_transaction(&tx, &mut accounts).is_err());
|
2018-11-23 11:12:56 -08:00
|
|
|
assert!(BudgetProgram::deserialize(&accounts[1].userdata).is_err());
|
2018-09-20 09:38:37 -07:00
|
|
|
|
2019-02-01 07:36:35 -08:00
|
|
|
let tx = BudgetTransaction::new_timestamp(
|
2018-09-20 09:38:37 -07:00
|
|
|
&from,
|
|
|
|
contract.pubkey(),
|
|
|
|
to.pubkey(),
|
|
|
|
Utc::now(),
|
|
|
|
Hash::default(),
|
|
|
|
);
|
2018-09-28 16:16:35 -07:00
|
|
|
assert!(process_transaction(&tx, &mut accounts).is_err());
|
2018-11-23 11:12:56 -08:00
|
|
|
assert!(BudgetProgram::deserialize(&accounts[1].userdata).is_err());
|
2018-09-20 09:38:37 -07:00
|
|
|
|
|
|
|
// Success if there was no panic...
|
|
|
|
}
|
2018-09-17 13:36:31 -07:00
|
|
|
}
|