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};
|
2018-09-20 12:50:11 -07:00
|
|
|
use budget::Budget;
|
2018-09-26 09:39:19 -07:00
|
|
|
use budget_instruction::Instruction;
|
2018-09-17 13:36:31 -07:00
|
|
|
use chrono::prelude::{DateTime, Utc};
|
2018-09-20 13:12:53 -07:00
|
|
|
use payment_plan::Witness;
|
2018-09-27 07:49:26 -07:00
|
|
|
use solana_program_interface::account::Account;
|
|
|
|
use solana_program_interface::pubkey::Pubkey;
|
2018-09-17 13:36:31 -07:00
|
|
|
use std::io;
|
|
|
|
use transaction::Transaction;
|
|
|
|
|
|
|
|
#[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-09-17 13:36:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
|
2018-09-20 19:17:37 -07:00
|
|
|
pub struct BudgetState {
|
2018-09-17 13:36:31 -07:00
|
|
|
pub initialized: bool,
|
2018-09-20 12:50:11 -07:00
|
|
|
pub pending_budget: Option<Budget>,
|
2018-09-17 13:36:31 -07:00
|
|
|
}
|
|
|
|
|
2018-09-19 17:25:57 -07:00
|
|
|
pub const BUDGET_PROGRAM_ID: [u8; 32] = [
|
2018-09-17 13:36:31 -07:00
|
|
|
1, 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,
|
|
|
|
];
|
2018-09-20 19:17:37 -07:00
|
|
|
impl BudgetState {
|
2018-09-17 13:36:31 -07:00
|
|
|
fn is_pending(&self) -> bool {
|
2018-09-20 12:50:11 -07:00
|
|
|
self.pending_budget != None
|
2018-09-17 13:36:31 -07:00
|
|
|
}
|
|
|
|
pub fn id() -> Pubkey {
|
2018-09-19 17:25:57 -07:00
|
|
|
Pubkey::new(&BUDGET_PROGRAM_ID)
|
2018-09-17 13:36:31 -07:00
|
|
|
}
|
2018-09-19 17:25:57 -07:00
|
|
|
pub fn check_id(program_id: &Pubkey) -> bool {
|
|
|
|
program_id.as_ref() == BUDGET_PROGRAM_ID
|
2018-09-17 13:36:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Process a Witness Signature. Any payment plans waiting on this signature
|
|
|
|
/// will progress one step.
|
|
|
|
fn apply_signature(
|
|
|
|
&mut self,
|
2018-09-28 16:16:35 -07:00
|
|
|
tx: &Transaction,
|
|
|
|
program_index: usize,
|
|
|
|
account: &mut [&mut Account],
|
2018-09-17 13:36:31 -07:00
|
|
|
) -> Result<(), BudgetError> {
|
2018-09-18 18:45:44 -07:00
|
|
|
let mut final_payment = None;
|
2018-09-20 12:50:11 -07:00
|
|
|
if let Some(ref mut budget) = self.pending_budget {
|
2018-09-28 16:16:35 -07:00
|
|
|
let key = tx.key(program_index, 0).unwrap();
|
|
|
|
budget.apply_witness(&Witness::Signature, key);
|
2018-09-20 12:50:11 -07:00
|
|
|
final_payment = budget.final_payment();
|
2018-09-18 18:45:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(payment) = final_payment {
|
2018-09-28 16:16:35 -07:00
|
|
|
if Some(&payment.to) != tx.key(program_index, 2) {
|
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-09-18 18:45:44 -07:00
|
|
|
account[1].tokens -= payment.tokens;
|
|
|
|
account[2].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-09-28 16:16:35 -07:00
|
|
|
tx: &Transaction,
|
|
|
|
program_index: usize,
|
|
|
|
accounts: &mut [&mut Account],
|
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-09-20 12:50:11 -07:00
|
|
|
if let Some(ref mut budget) = self.pending_budget {
|
2018-09-28 16:16:35 -07:00
|
|
|
let key = tx.key(program_index, 0).unwrap();
|
|
|
|
budget.apply_witness(&Witness::Timestamp(dt), key);
|
2018-09-20 12:50:11 -07:00
|
|
|
final_payment = budget.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-09-28 16:16:35 -07:00
|
|
|
if Some(&payment.to) != tx.key(program_index, 2) {
|
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-09-18 18:45:44 -07:00
|
|
|
accounts[1].tokens -= payment.tokens;
|
|
|
|
accounts[2].tokens += payment.tokens;
|
2018-09-17 13:36:31 -07:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Deduct tokens from the source account if it has sufficient funds and the contract isn't
|
|
|
|
/// pending
|
|
|
|
fn apply_debits_to_budget_state(
|
2018-09-28 16:16:35 -07:00
|
|
|
accounts: &mut [&mut Account],
|
2018-09-17 13:36:31 -07:00
|
|
|
instruction: &Instruction,
|
|
|
|
) -> Result<(), BudgetError> {
|
|
|
|
{
|
|
|
|
// if the source account userdata is not empty, this is a pending contract
|
|
|
|
if !accounts[0].userdata.is_empty() {
|
|
|
|
trace!("source is pending");
|
2018-09-28 16:16:35 -07:00
|
|
|
return Err(BudgetError::SourceIsPendingContract);
|
2018-09-17 13:36:31 -07:00
|
|
|
}
|
|
|
|
if let Instruction::NewContract(contract) = &instruction {
|
|
|
|
if contract.tokens < 0 {
|
|
|
|
trace!("negative tokens");
|
|
|
|
return Err(BudgetError::NegativeTokens);
|
|
|
|
}
|
|
|
|
|
|
|
|
if accounts[0].tokens < contract.tokens {
|
2018-09-17 19:37:59 -07:00
|
|
|
trace!("insufficient funds");
|
2018-09-28 16:16:35 -07:00
|
|
|
return Err(BudgetError::InsufficientFunds);
|
2018-09-17 13:36:31 -07:00
|
|
|
} else {
|
|
|
|
accounts[0].tokens -= contract.tokens;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Apply only a transaction's credits.
|
|
|
|
/// Note: It is safe to apply credits from multiple transactions in parallel.
|
|
|
|
fn apply_credits_to_budget_state(
|
|
|
|
tx: &Transaction,
|
2018-09-28 16:16:35 -07:00
|
|
|
program_index: usize,
|
|
|
|
accounts: &mut [&mut Account],
|
2018-09-17 13:36:31 -07:00
|
|
|
instruction: &Instruction,
|
|
|
|
) -> Result<(), BudgetError> {
|
|
|
|
match instruction {
|
|
|
|
Instruction::NewContract(contract) => {
|
2018-09-20 12:50:11 -07:00
|
|
|
let budget = contract.budget.clone();
|
|
|
|
if let Some(payment) = budget.final_payment() {
|
2018-09-17 13:36:31 -07:00
|
|
|
accounts[1].tokens += payment.tokens;
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
let existing = Self::deserialize(&accounts[1].userdata).ok();
|
|
|
|
if Some(true) == existing.map(|x| x.initialized) {
|
|
|
|
trace!("contract already exists");
|
2018-09-28 16:16:35 -07:00
|
|
|
Err(BudgetError::ContractAlreadyExists)
|
2018-09-17 13:36:31 -07:00
|
|
|
} else {
|
2018-09-20 19:17:37 -07:00
|
|
|
let mut state = BudgetState::default();
|
2018-09-20 12:50:11 -07:00
|
|
|
state.pending_budget = Some(budget);
|
2018-09-17 13:36:31 -07:00
|
|
|
accounts[1].tokens += contract.tokens;
|
|
|
|
state.initialized = true;
|
2018-09-20 09:38:37 -07:00
|
|
|
state.serialize(&mut accounts[1].userdata)
|
2018-09-17 13:36:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Instruction::ApplyTimestamp(dt) => {
|
2018-09-20 09:38:37 -07:00
|
|
|
if let Ok(mut state) = Self::deserialize(&accounts[1].userdata) {
|
|
|
|
if !state.is_pending() {
|
2018-09-28 16:16:35 -07:00
|
|
|
Err(BudgetError::ContractNotPending)
|
2018-09-20 09:38:37 -07:00
|
|
|
} else if !state.initialized {
|
|
|
|
trace!("contract is uninitialized");
|
2018-09-28 16:16:35 -07:00
|
|
|
Err(BudgetError::UninitializedContract)
|
2018-09-20 09:38:37 -07:00
|
|
|
} else {
|
|
|
|
trace!("apply timestamp");
|
2018-09-28 16:16:35 -07:00
|
|
|
state.apply_timestamp(tx, program_index, accounts, *dt)?;
|
2018-09-20 09:38:37 -07:00
|
|
|
trace!("apply timestamp committed");
|
|
|
|
state.serialize(&mut accounts[1].userdata)
|
|
|
|
}
|
2018-09-17 13:36:31 -07:00
|
|
|
} else {
|
2018-09-28 16:16:35 -07:00
|
|
|
Err(BudgetError::UninitializedContract)
|
2018-09-17 13:36:31 -07:00
|
|
|
}
|
|
|
|
}
|
2018-09-18 18:45:44 -07:00
|
|
|
Instruction::ApplySignature => {
|
2018-09-20 09:38:37 -07:00
|
|
|
if let Ok(mut state) = Self::deserialize(&accounts[1].userdata) {
|
|
|
|
if !state.is_pending() {
|
2018-09-28 16:16:35 -07:00
|
|
|
Err(BudgetError::ContractNotPending)
|
2018-09-20 09:38:37 -07:00
|
|
|
} else if !state.initialized {
|
|
|
|
trace!("contract is uninitialized");
|
2018-09-28 16:16:35 -07:00
|
|
|
Err(BudgetError::UninitializedContract)
|
2018-09-20 09:38:37 -07:00
|
|
|
} else {
|
|
|
|
trace!("apply signature");
|
2018-09-28 16:16:35 -07:00
|
|
|
state.apply_signature(tx, program_index, accounts)?;
|
2018-09-20 09:38:37 -07:00
|
|
|
trace!("apply signature committed");
|
|
|
|
state.serialize(&mut accounts[1].userdata)
|
|
|
|
}
|
2018-09-17 13:36:31 -07:00
|
|
|
} else {
|
2018-09-28 16:16:35 -07:00
|
|
|
Err(BudgetError::UninitializedContract)
|
2018-09-17 13:36:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Instruction::NewVote(_vote) => {
|
|
|
|
// TODO: move vote instruction into a different contract
|
|
|
|
trace!("GOT VOTE! last_id={}", tx.last_id);
|
|
|
|
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])
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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 .
|
2018-09-21 13:21:01 -07:00
|
|
|
pub fn process_transaction(
|
|
|
|
tx: &Transaction,
|
2018-09-28 16:16:35 -07:00
|
|
|
program_index: usize,
|
|
|
|
accounts: &mut [&mut Account],
|
2018-09-21 13:21:01 -07:00
|
|
|
) -> Result<(), BudgetError> {
|
2018-09-28 16:16:35 -07:00
|
|
|
if let Ok(instruction) = deserialize(tx.userdata(program_index)) {
|
2018-09-20 13:54:42 -07:00
|
|
|
trace!("process_transaction: {:?}", instruction);
|
2018-09-28 16:16:35 -07:00
|
|
|
Self::apply_debits_to_budget_state(accounts, &instruction).and_then(|_| {
|
|
|
|
Self::apply_credits_to_budget_state(tx, program_index, accounts, &instruction)
|
|
|
|
})
|
2018-09-20 13:54:42 -07:00
|
|
|
} else {
|
2018-09-28 16:16:35 -07:00
|
|
|
info!(
|
|
|
|
"Invalid transaction userdata: {:?}",
|
|
|
|
tx.userdata(program_index)
|
|
|
|
);
|
2018-09-21 13:21:01 -07:00
|
|
|
Err(BudgetError::UserdataDeserializeFailure)
|
2018-09-20 13:54:42 -07:00
|
|
|
}
|
2018-09-17 13:36:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//TODO the contract needs to provide a "get_balance" introspection call of the userdata
|
|
|
|
pub fn get_balance(account: &Account) -> i64 {
|
2018-09-18 18:45:44 -07:00
|
|
|
if let Ok(state) = deserialize(&account.userdata) {
|
2018-09-20 19:17:37 -07:00
|
|
|
let state: BudgetState = state;
|
2018-09-18 18:45:44 -07:00
|
|
|
if state.is_pending() {
|
2018-09-17 13:36:31 -07:00
|
|
|
0
|
|
|
|
} else {
|
|
|
|
account.tokens
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
account.tokens
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use bincode::serialize;
|
2018-09-20 19:17:37 -07:00
|
|
|
use budget_program::{BudgetError, BudgetState};
|
2018-09-26 09:33:52 -07:00
|
|
|
use budget_transaction::BudgetTransaction;
|
2018-09-17 21:09:11 -07:00
|
|
|
use chrono::prelude::{DateTime, NaiveDate, Utc};
|
2018-09-17 13:36:31 -07:00
|
|
|
use hash::Hash;
|
2018-09-26 16:55:36 -07:00
|
|
|
use signature::{GenKeys, Keypair, KeypairUtil};
|
2018-09-27 07:49:26 -07:00
|
|
|
use solana_program_interface::account::Account;
|
|
|
|
use solana_program_interface::pubkey::Pubkey;
|
2018-09-26 09:33:52 -07:00
|
|
|
use transaction::Transaction;
|
|
|
|
|
2018-09-28 16:16:35 -07:00
|
|
|
fn process_transaction(tx: &Transaction, accounts: &mut [Account]) -> Result<(), BudgetError> {
|
|
|
|
let mut refs: Vec<&mut Account> = accounts.iter_mut().collect();
|
|
|
|
BudgetState::process_transaction(&tx, 0, &mut refs[..])
|
|
|
|
}
|
2018-09-17 13:36:31 -07:00
|
|
|
#[test]
|
|
|
|
fn test_serializer() {
|
2018-09-20 19:17:37 -07:00
|
|
|
let mut a = Account::new(0, 512, BudgetState::id());
|
|
|
|
let b = BudgetState::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-09-20 19:17:37 -07:00
|
|
|
let c = BudgetState::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-09-20 19:17:37 -07:00
|
|
|
let mut a = Account::new(0, 1, BudgetState::id());
|
|
|
|
let b = BudgetState::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() {
|
|
|
|
let mut accounts = vec![
|
2018-09-20 19:17:37 -07:00
|
|
|
Account::new(1, 0, BudgetState::id()),
|
|
|
|
Account::new(0, 512, BudgetState::id()),
|
2018-09-20 13:54:42 -07:00
|
|
|
];
|
|
|
|
let from = Keypair::new();
|
|
|
|
let contract = Keypair::new();
|
2018-09-28 16:16:35 -07:00
|
|
|
let userdata = vec![1, 2, 3];
|
2018-09-26 08:54:04 -07:00
|
|
|
let tx = Transaction::new(
|
2018-09-20 13:54:42 -07:00
|
|
|
&from,
|
|
|
|
&[contract.pubkey()],
|
2018-09-20 19:17:37 -07:00
|
|
|
BudgetState::id(),
|
2018-09-28 16:16:35 -07: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-09-17 13:36:31 -07:00
|
|
|
#[test]
|
|
|
|
fn test_transfer_on_date() {
|
|
|
|
let mut accounts = vec![
|
2018-09-20 19:17:37 -07:00
|
|
|
Account::new(1, 0, BudgetState::id()),
|
|
|
|
Account::new(0, 512, BudgetState::id()),
|
|
|
|
Account::new(0, 0, BudgetState::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();
|
|
|
|
let tx = Transaction::budget_new_on_date(
|
|
|
|
&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-09-20 19:17:37 -07:00
|
|
|
let state = BudgetState::deserialize(&accounts[contract_account].userdata).unwrap();
|
2018-09-17 13:36:31 -07:00
|
|
|
assert!(state.is_pending());
|
|
|
|
|
|
|
|
// Attack! Try to payout to a rando key
|
|
|
|
let tx = Transaction::budget_new_timestamp(
|
|
|
|
&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-09-20 19:17:37 -07:00
|
|
|
let state = BudgetState::deserialize(&accounts[contract_account].userdata).unwrap();
|
2018-09-17 13:36:31 -07:00
|
|
|
assert!(state.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(),
|
|
|
|
);
|
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-09-20 19:17:37 -07:00
|
|
|
let state = BudgetState::deserialize(&accounts[contract_account].userdata).unwrap();
|
2018-09-17 13:36:31 -07:00
|
|
|
assert!(!state.is_pending());
|
|
|
|
|
|
|
|
// 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-09-20 19:17:37 -07:00
|
|
|
Account::new(1, 0, BudgetState::id()),
|
|
|
|
Account::new(0, 512, BudgetState::id()),
|
|
|
|
Account::new(0, 0, BudgetState::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();
|
|
|
|
let tx = Transaction::budget_new_on_date(
|
|
|
|
&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-09-20 19:17:37 -07:00
|
|
|
let state = BudgetState::deserialize(&accounts[contract_account].userdata).unwrap();
|
2018-09-17 13:36:31 -07:00
|
|
|
assert!(state.is_pending());
|
|
|
|
|
|
|
|
// Attack! try to put the tokens into the wrong account with cancel
|
2018-09-18 18:45:44 -07:00
|
|
|
let tx =
|
|
|
|
Transaction::budget_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);
|
|
|
|
// 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(),
|
|
|
|
);
|
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[pay_account].tokens, 1);
|
|
|
|
|
|
|
|
// try to replay the signature contract
|
|
|
|
let tx = Transaction::budget_new_signature(
|
|
|
|
&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
|
|
|
);
|
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[pay_account].tokens, 1);
|
|
|
|
}
|
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-09-20 19:17:37 -07:00
|
|
|
Account::new(1, 0, BudgetState::id()),
|
|
|
|
Account::new(1, 0, BudgetState::id()), // <== userdata is 0, which is not enough
|
|
|
|
Account::new(1, 0, BudgetState::id()),
|
2018-09-20 09:38:37 -07:00
|
|
|
];
|
|
|
|
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(),
|
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-09-20 19:17:37 -07:00
|
|
|
assert!(BudgetState::deserialize(&accounts[1].userdata).is_err());
|
2018-09-20 09:38:37 -07:00
|
|
|
|
|
|
|
let tx = Transaction::budget_new_timestamp(
|
|
|
|
&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-09-20 19:17:37 -07:00
|
|
|
assert!(BudgetState::deserialize(&accounts[1].userdata).is_err());
|
2018-09-20 09:38:37 -07:00
|
|
|
|
|
|
|
// Success if there was no panic...
|
|
|
|
}
|
|
|
|
|
2018-09-17 21:09:11 -07:00
|
|
|
/// 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);
|
2018-09-19 14:39:25 -07:00
|
|
|
let date_iso8601 = "2016-07-08T09:10:11Z";
|
2018-09-17 21:09:11 -07:00
|
|
|
|
|
|
|
let tx = Transaction::budget_new(&keypair, to, 192, Hash::default());
|
|
|
|
assert_eq!(
|
2018-09-28 16:16:35 -07:00
|
|
|
tx.userdata(0).to_vec(),
|
2018-09-17 21:09:11 -07:00
|
|
|
vec![
|
2018-09-20 12:50:11 -07:00
|
|
|
0, 0, 0, 0, 192, 0, 0, 0, 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
|
2018-09-17 21:09:11 -07:00
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2018-09-19 16:44:03 -07:00
|
|
|
let tx = Transaction::budget_new_on_date(
|
|
|
|
&keypair,
|
|
|
|
to,
|
|
|
|
contract,
|
|
|
|
date,
|
|
|
|
keypair.pubkey(),
|
2018-09-22 16:51:21 -07:00
|
|
|
Some(keypair.pubkey()),
|
2018-09-19 16:44:03 -07:00
|
|
|
192,
|
|
|
|
Hash::default(),
|
|
|
|
);
|
2018-09-17 21:09:11 -07:00
|
|
|
assert_eq!(
|
2018-09-28 16:16:35 -07:00
|
|
|
tx.userdata(0).to_vec(),
|
2018-09-17 21:09:11 -07:00
|
|
|
vec![
|
2018-09-20 12:50:11 -07:00
|
|
|
0, 0, 0, 0, 192, 0, 0, 0, 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,
|
2018-09-17 21:09:11 -07:00
|
|
|
187, 167, 181, 188, 22, 59, 206, 105, 231, 150, 215, 30, 78, 212, 76, 16, 252, 180,
|
2018-09-20 12:50:11 -07:00
|
|
|
72, 134, 137, 247, 161, 68
|
2018-09-17 21:09:11 -07:00
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2018-09-19 09:34:20 -07:00
|
|
|
// ApplyTimestamp(date)
|
2018-09-17 21:09:11 -07:00
|
|
|
let tx = Transaction::budget_new_timestamp(
|
|
|
|
&keypair,
|
|
|
|
keypair.pubkey(),
|
|
|
|
to,
|
|
|
|
date,
|
|
|
|
Hash::default(),
|
|
|
|
);
|
2018-09-18 08:02:57 -07:00
|
|
|
let mut expected_userdata = vec![1, 0, 0, 0, 20, 0, 0, 0, 0, 0, 0, 0];
|
2018-09-19 14:39:25 -07:00
|
|
|
expected_userdata.extend(date_iso8601.as_bytes());
|
2018-09-28 16:16:35 -07:00
|
|
|
assert_eq!(tx.userdata(0).to_vec(), expected_userdata);
|
2018-09-17 21:09:11 -07:00
|
|
|
|
|
|
|
// ApplySignature
|
2018-09-18 18:45:44 -07:00
|
|
|
let tx = Transaction::budget_new_signature(&keypair, keypair.pubkey(), to, Hash::default());
|
2018-09-28 16:16:35 -07:00
|
|
|
assert_eq!(tx.userdata(0).to_vec(), vec![2, 0, 0, 0]);
|
2018-09-17 21:09:11 -07:00
|
|
|
}
|
2018-09-17 13:36:31 -07:00
|
|
|
}
|