Introducing Scripts
A sequence of instructions. A client compiles the script and then uses the compiled script to construction a transaction. Then it adds a adds a blockhash, signs the transaction, and sends it off for processing.
This commit is contained in:
parent
55cdbedb52
commit
ae4d14a2ad
|
@ -143,9 +143,10 @@ pub fn process_instruction(
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::*;
|
use super::*;
|
||||||
use solana_budget_api::budget_transaction::BudgetTransaction;
|
use solana_budget_api::budget_instruction::BudgetInstruction;
|
||||||
use solana_budget_api::id;
|
use solana_budget_api::id;
|
||||||
use solana_runtime::bank::Bank;
|
use solana_runtime::bank::Bank;
|
||||||
|
use solana_runtime::bank_client::BankClient;
|
||||||
use solana_sdk::genesis_block::GenesisBlock;
|
use solana_sdk::genesis_block::GenesisBlock;
|
||||||
use solana_sdk::signature::{Keypair, KeypairUtil};
|
use solana_sdk::signature::{Keypair, KeypairUtil};
|
||||||
use solana_sdk::transaction::{InstructionError, Transaction, TransactionError};
|
use solana_sdk::transaction::{InstructionError, Transaction, TransactionError};
|
||||||
|
@ -157,41 +158,41 @@ mod test {
|
||||||
(bank, mint_keypair)
|
(bank, mint_keypair)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_invalid_instruction() {
|
|
||||||
let (bank, from) = create_bank(10_000);
|
|
||||||
let blockhash = bank.last_blockhash();
|
|
||||||
let contract = Keypair::new();
|
|
||||||
let data = (1u8, 2u8, 3u8);
|
|
||||||
let tx = Transaction::new_signed(&from, &[contract.pubkey()], &id(), &data, blockhash, 0);
|
|
||||||
assert!(bank.process_transaction(&tx).is_err());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_unsigned_witness_key() {
|
fn test_unsigned_witness_key() {
|
||||||
let (bank, from) = create_bank(10_000);
|
let (bank, mint_keypair) = create_bank(10_000);
|
||||||
let blockhash = bank.last_blockhash();
|
let alice_client = BankClient::new(&bank, mint_keypair);
|
||||||
|
let alice_pubkey = alice_client.pubkey();
|
||||||
|
|
||||||
// Initialize BudgetState
|
// Initialize BudgetState
|
||||||
let contract = Keypair::new().pubkey();
|
let budget_pubkey = Keypair::new().pubkey();
|
||||||
let to = Keypair::new().pubkey();
|
let bob_pubkey = Keypair::new().pubkey();
|
||||||
let witness = Keypair::new().pubkey();
|
let witness = Keypair::new().pubkey();
|
||||||
let tx =
|
let script = BudgetInstruction::new_when_signed_script(
|
||||||
BudgetTransaction::new_when_signed(&from, &to, &contract, &witness, None, 1, blockhash);
|
&alice_pubkey,
|
||||||
bank.process_transaction(&tx).unwrap();
|
&bob_pubkey,
|
||||||
|
&budget_pubkey,
|
||||||
|
&witness,
|
||||||
|
None,
|
||||||
|
1,
|
||||||
|
);
|
||||||
|
alice_client.process_script(script).unwrap();
|
||||||
|
|
||||||
// Attack! Part 1: Sign a witness transaction with a random key.
|
// Attack! Part 1: Sign a witness transaction with a random key.
|
||||||
let rando = Keypair::new();
|
let mallory_client = BankClient::new(&bank, Keypair::new());
|
||||||
bank.transfer(1, &from, &rando.pubkey(), blockhash).unwrap();
|
let mallory_pubkey = mallory_client.pubkey();
|
||||||
let mut tx = BudgetTransaction::new_signature(&rando, &contract, &to, blockhash);
|
alice_client.transfer(1, &mallory_pubkey).unwrap();
|
||||||
|
let instruction =
|
||||||
|
BudgetInstruction::new_apply_signature(&mallory_pubkey, &budget_pubkey, &bob_pubkey);
|
||||||
|
let mut transaction = Transaction::new(vec![instruction]);
|
||||||
|
|
||||||
// Attack! Part 2: Point the instruction to the expected, but unsigned, key.
|
// Attack! Part 2: Point the instruction to the expected, but unsigned, key.
|
||||||
tx.account_keys.push(from.pubkey());
|
transaction.account_keys.push(alice_pubkey);
|
||||||
tx.instructions[0].accounts[0] = 3;
|
transaction.instructions[0].accounts[0] = 3;
|
||||||
|
|
||||||
// Ensure the transaction fails because of the unsigned key.
|
// Ensure the transaction fails because of the unsigned key.
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
bank.process_transaction(&tx),
|
mallory_client.process_transaction(transaction),
|
||||||
Err(TransactionError::InstructionError(
|
Err(TransactionError::InstructionError(
|
||||||
0,
|
0,
|
||||||
InstructionError::ProgramError(ProgramError::MissingRequiredSignature)
|
InstructionError::ProgramError(ProgramError::MissingRequiredSignature)
|
||||||
|
@ -201,37 +202,44 @@ mod test {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_unsigned_timestamp() {
|
fn test_unsigned_timestamp() {
|
||||||
let (bank, from) = create_bank(10_000);
|
let (bank, mint_keypair) = create_bank(10_000);
|
||||||
let blockhash = bank.last_blockhash();
|
let alice_client = BankClient::new(&bank, mint_keypair);
|
||||||
|
let alice_pubkey = alice_client.pubkey();
|
||||||
|
|
||||||
// Initialize BudgetState
|
// Initialize BudgetState
|
||||||
let contract = Keypair::new().pubkey();
|
let budget_pubkey = Keypair::new().pubkey();
|
||||||
let to = Keypair::new().pubkey();
|
let bob_pubkey = Keypair::new().pubkey();
|
||||||
let dt = Utc::now();
|
let dt = Utc::now();
|
||||||
let tx = BudgetTransaction::new_on_date(
|
let script = BudgetInstruction::new_on_date_script(
|
||||||
&from,
|
&alice_pubkey,
|
||||||
&to,
|
&bob_pubkey,
|
||||||
&contract,
|
&budget_pubkey,
|
||||||
dt,
|
dt,
|
||||||
&from.pubkey(),
|
&alice_pubkey,
|
||||||
None,
|
None,
|
||||||
1,
|
1,
|
||||||
blockhash,
|
|
||||||
);
|
);
|
||||||
bank.process_transaction(&tx).unwrap();
|
alice_client.process_script(script).unwrap();
|
||||||
|
|
||||||
// Attack! Part 1: Sign a timestamp transaction with a random key.
|
// Attack! Part 1: Sign a timestamp transaction with a random key.
|
||||||
let rando = Keypair::new();
|
let mallory_client = BankClient::new(&bank, Keypair::new());
|
||||||
bank.transfer(1, &from, &rando.pubkey(), blockhash).unwrap();
|
let mallory_pubkey = mallory_client.pubkey();
|
||||||
let mut tx = BudgetTransaction::new_timestamp(&rando, &contract, &to, dt, blockhash);
|
alice_client.transfer(1, &mallory_pubkey).unwrap();
|
||||||
|
let instruction = BudgetInstruction::new_apply_timestamp(
|
||||||
|
&mallory_pubkey,
|
||||||
|
&budget_pubkey,
|
||||||
|
&bob_pubkey,
|
||||||
|
dt,
|
||||||
|
);
|
||||||
|
let mut transaction = Transaction::new(vec![instruction]);
|
||||||
|
|
||||||
// Attack! Part 2: Point the instruction to the expected, but unsigned, key.
|
// Attack! Part 2: Point the instruction to the expected, but unsigned, key.
|
||||||
tx.account_keys.push(from.pubkey());
|
transaction.account_keys.push(alice_pubkey);
|
||||||
tx.instructions[0].accounts[0] = 3;
|
transaction.instructions[0].accounts[0] = 3;
|
||||||
|
|
||||||
// Ensure the transaction fails because of the unsigned key.
|
// Ensure the transaction fails because of the unsigned key.
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
bank.process_transaction(&tx),
|
mallory_client.process_transaction(transaction),
|
||||||
Err(TransactionError::InstructionError(
|
Err(TransactionError::InstructionError(
|
||||||
0,
|
0,
|
||||||
InstructionError::ProgramError(ProgramError::MissingRequiredSignature)
|
InstructionError::ProgramError(ProgramError::MissingRequiredSignature)
|
||||||
|
@ -241,40 +249,39 @@ mod test {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_transfer_on_date() {
|
fn test_transfer_on_date() {
|
||||||
let (bank, from) = create_bank(2);
|
let (bank, mint_keypair) = create_bank(2);
|
||||||
let blockhash = bank.last_blockhash();
|
let alice_client = BankClient::new(&bank, mint_keypair);
|
||||||
let contract = Keypair::new();
|
let alice_pubkey = alice_client.pubkey();
|
||||||
let to = Keypair::new();
|
let budget_pubkey = Keypair::new().pubkey();
|
||||||
let rando = Keypair::new();
|
let bob_pubkey = Keypair::new().pubkey();
|
||||||
|
let mallory_pubkey = Keypair::new().pubkey();
|
||||||
let dt = Utc::now();
|
let dt = Utc::now();
|
||||||
let tx = BudgetTransaction::new_on_date(
|
let script = BudgetInstruction::new_on_date_script(
|
||||||
&from,
|
&alice_pubkey,
|
||||||
&to.pubkey(),
|
&bob_pubkey,
|
||||||
&contract.pubkey(),
|
&budget_pubkey,
|
||||||
dt,
|
dt,
|
||||||
&from.pubkey(),
|
&alice_pubkey,
|
||||||
None,
|
None,
|
||||||
1,
|
1,
|
||||||
blockhash,
|
|
||||||
);
|
);
|
||||||
bank.process_transaction(&tx).unwrap();
|
alice_client.process_script(script).unwrap();
|
||||||
assert_eq!(bank.get_balance(&from.pubkey()), 1);
|
assert_eq!(bank.get_balance(&alice_pubkey), 1);
|
||||||
assert_eq!(bank.get_balance(&contract.pubkey()), 1);
|
assert_eq!(bank.get_balance(&budget_pubkey), 1);
|
||||||
|
|
||||||
let contract_account = bank.get_account(&contract.pubkey()).unwrap();
|
let contract_account = bank.get_account(&budget_pubkey).unwrap();
|
||||||
let budget_state = BudgetState::deserialize(&contract_account.data).unwrap();
|
let budget_state = BudgetState::deserialize(&contract_account.data).unwrap();
|
||||||
assert!(budget_state.is_pending());
|
assert!(budget_state.is_pending());
|
||||||
|
|
||||||
// Attack! Try to payout to a rando key
|
// Attack! Try to payout to mallory_pubkey
|
||||||
let tx = BudgetTransaction::new_timestamp(
|
let instruction = BudgetInstruction::new_apply_timestamp(
|
||||||
&from,
|
&alice_pubkey,
|
||||||
&contract.pubkey(),
|
&budget_pubkey,
|
||||||
&rando.pubkey(),
|
&mallory_pubkey,
|
||||||
dt,
|
dt,
|
||||||
blockhash,
|
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
bank.process_transaction(&tx).unwrap_err(),
|
alice_client.process_script(vec![instruction]).unwrap_err(),
|
||||||
TransactionError::InstructionError(
|
TransactionError::InstructionError(
|
||||||
0,
|
0,
|
||||||
InstructionError::ProgramError(ProgramError::CustomError(
|
InstructionError::ProgramError(ProgramError::CustomError(
|
||||||
|
@ -282,77 +289,71 @@ mod test {
|
||||||
))
|
))
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
assert_eq!(bank.get_balance(&from.pubkey()), 1);
|
assert_eq!(bank.get_balance(&alice_pubkey), 1);
|
||||||
assert_eq!(bank.get_balance(&contract.pubkey()), 1);
|
assert_eq!(bank.get_balance(&budget_pubkey), 1);
|
||||||
assert_eq!(bank.get_balance(&to.pubkey()), 0);
|
assert_eq!(bank.get_balance(&bob_pubkey), 0);
|
||||||
|
|
||||||
let contract_account = bank.get_account(&contract.pubkey()).unwrap();
|
let contract_account = bank.get_account(&budget_pubkey).unwrap();
|
||||||
let budget_state = BudgetState::deserialize(&contract_account.data).unwrap();
|
let budget_state = BudgetState::deserialize(&contract_account.data).unwrap();
|
||||||
assert!(budget_state.is_pending());
|
assert!(budget_state.is_pending());
|
||||||
|
|
||||||
// Now, acknowledge the time in the condition occurred and
|
// Now, acknowledge the time in the condition occurred and
|
||||||
// that pubkey's funds are now available.
|
// that pubkey's funds are now available.
|
||||||
let tx = BudgetTransaction::new_timestamp(
|
let instruction =
|
||||||
&from,
|
BudgetInstruction::new_apply_timestamp(&alice_pubkey, &budget_pubkey, &bob_pubkey, dt);
|
||||||
&contract.pubkey(),
|
alice_client.process_script(vec![instruction]).unwrap();
|
||||||
&to.pubkey(),
|
assert_eq!(bank.get_balance(&alice_pubkey), 1);
|
||||||
dt,
|
assert_eq!(bank.get_balance(&budget_pubkey), 0);
|
||||||
blockhash,
|
assert_eq!(bank.get_balance(&bob_pubkey), 1);
|
||||||
);
|
assert_eq!(bank.get_account(&budget_pubkey), None);
|
||||||
bank.process_transaction(&tx).unwrap();
|
|
||||||
assert_eq!(bank.get_balance(&from.pubkey()), 1);
|
|
||||||
assert_eq!(bank.get_balance(&contract.pubkey()), 0);
|
|
||||||
assert_eq!(bank.get_balance(&to.pubkey()), 1);
|
|
||||||
assert_eq!(bank.get_account(&contract.pubkey()), None);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_cancel_transfer() {
|
fn test_cancel_transfer() {
|
||||||
let (bank, from) = create_bank(3);
|
let (bank, mint_keypair) = create_bank(3);
|
||||||
let blockhash = bank.last_blockhash();
|
let alice_client = BankClient::new(&bank, mint_keypair);
|
||||||
let contract = Keypair::new();
|
let alice_pubkey = alice_client.pubkey();
|
||||||
let to = Keypair::new();
|
let budget_pubkey = Keypair::new().pubkey();
|
||||||
|
let bob_pubkey = Keypair::new().pubkey();
|
||||||
let dt = Utc::now();
|
let dt = Utc::now();
|
||||||
|
|
||||||
let tx = BudgetTransaction::new_on_date(
|
let script = BudgetInstruction::new_on_date_script(
|
||||||
&from,
|
&alice_pubkey,
|
||||||
&to.pubkey(),
|
&bob_pubkey,
|
||||||
&contract.pubkey(),
|
&budget_pubkey,
|
||||||
dt,
|
dt,
|
||||||
&from.pubkey(),
|
&alice_pubkey,
|
||||||
Some(from.pubkey()),
|
Some(alice_pubkey),
|
||||||
1,
|
1,
|
||||||
blockhash,
|
|
||||||
);
|
);
|
||||||
bank.process_transaction(&tx).unwrap();
|
alice_client.process_script(script).unwrap();
|
||||||
assert_eq!(bank.get_balance(&from.pubkey()), 2);
|
assert_eq!(bank.get_balance(&alice_pubkey), 2);
|
||||||
assert_eq!(bank.get_balance(&contract.pubkey()), 1);
|
assert_eq!(bank.get_balance(&budget_pubkey), 1);
|
||||||
|
|
||||||
let contract_account = bank.get_account(&contract.pubkey()).unwrap();
|
let contract_account = bank.get_account(&budget_pubkey).unwrap();
|
||||||
let budget_state = BudgetState::deserialize(&contract_account.data).unwrap();
|
let budget_state = BudgetState::deserialize(&contract_account.data).unwrap();
|
||||||
assert!(budget_state.is_pending());
|
assert!(budget_state.is_pending());
|
||||||
|
|
||||||
// Attack! try to put the lamports into the wrong account with cancel
|
// Attack! try to put the lamports into the wrong account with cancel
|
||||||
let rando = Keypair::new();
|
let mallory_client = BankClient::new(&bank, Keypair::new());
|
||||||
bank.transfer(1, &from, &rando.pubkey(), blockhash).unwrap();
|
let mallory_pubkey = mallory_client.pubkey();
|
||||||
assert_eq!(bank.get_balance(&from.pubkey()), 1);
|
alice_client.transfer(1, &mallory_pubkey).unwrap();
|
||||||
|
assert_eq!(bank.get_balance(&alice_pubkey), 1);
|
||||||
|
|
||||||
let tx =
|
let instruction =
|
||||||
BudgetTransaction::new_signature(&rando, &contract.pubkey(), &to.pubkey(), blockhash);
|
BudgetInstruction::new_apply_signature(&mallory_pubkey, &budget_pubkey, &bob_pubkey);
|
||||||
// unit test hack, the `from account` is passed instead of the `to` account to avoid
|
mallory_client.process_script(vec![instruction]).unwrap();
|
||||||
// creating more account vectors
|
|
||||||
bank.process_transaction(&tx).unwrap();
|
|
||||||
// nothing should be changed because apply witness didn't finalize a payment
|
// nothing should be changed because apply witness didn't finalize a payment
|
||||||
assert_eq!(bank.get_balance(&from.pubkey()), 1);
|
assert_eq!(bank.get_balance(&alice_pubkey), 1);
|
||||||
assert_eq!(bank.get_balance(&contract.pubkey()), 1);
|
assert_eq!(bank.get_balance(&budget_pubkey), 1);
|
||||||
assert_eq!(bank.get_account(&to.pubkey()), None);
|
assert_eq!(bank.get_account(&bob_pubkey), None);
|
||||||
|
|
||||||
// Now, cancel the transaction. from gets her funds back
|
// Now, cancel the transaction. mint gets her funds back
|
||||||
let tx =
|
let instruction =
|
||||||
BudgetTransaction::new_signature(&from, &contract.pubkey(), &from.pubkey(), blockhash);
|
BudgetInstruction::new_apply_signature(&alice_pubkey, &budget_pubkey, &alice_pubkey);
|
||||||
bank.process_transaction(&tx).unwrap();
|
alice_client.process_script(vec![instruction]).unwrap();
|
||||||
assert_eq!(bank.get_balance(&from.pubkey()), 2);
|
assert_eq!(bank.get_balance(&alice_pubkey), 2);
|
||||||
assert_eq!(bank.get_account(&contract.pubkey()), None);
|
assert_eq!(bank.get_account(&budget_pubkey), None);
|
||||||
assert_eq!(bank.get_account(&to.pubkey()), None);
|
assert_eq!(bank.get_account(&bob_pubkey), None);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,12 @@
|
||||||
use crate::budget_expr::BudgetExpr;
|
use crate::budget_expr::{BudgetExpr, Condition};
|
||||||
|
use crate::budget_state::BudgetState;
|
||||||
use crate::id;
|
use crate::id;
|
||||||
|
use bincode::serialized_size;
|
||||||
use chrono::prelude::{DateTime, Utc};
|
use chrono::prelude::{DateTime, Utc};
|
||||||
use serde_derive::{Deserialize, Serialize};
|
use serde_derive::{Deserialize, Serialize};
|
||||||
use solana_sdk::pubkey::Pubkey;
|
use solana_sdk::pubkey::Pubkey;
|
||||||
use solana_sdk::transaction::Instruction;
|
use solana_sdk::system_instruction::SystemInstruction;
|
||||||
|
use solana_sdk::transaction::{Instruction, Script};
|
||||||
|
|
||||||
/// A smart contract.
|
/// A smart contract.
|
||||||
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
|
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
|
||||||
|
@ -37,6 +40,80 @@ impl BudgetInstruction {
|
||||||
Instruction::new(id(), &BudgetInstruction::InitializeAccount(expr), keys)
|
Instruction::new(id(), &BudgetInstruction::InitializeAccount(expr), keys)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn new_initialize_account_script(
|
||||||
|
from: &Pubkey,
|
||||||
|
contract: &Pubkey,
|
||||||
|
lamports: u64,
|
||||||
|
expr: BudgetExpr,
|
||||||
|
) -> Script {
|
||||||
|
let space = serialized_size(&BudgetState::new(expr.clone())).unwrap();
|
||||||
|
vec![
|
||||||
|
SystemInstruction::new_program_account(&from, contract, lamports, space, &id()),
|
||||||
|
Self::new_initialize_account(contract, expr),
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create a postdated payment script.
|
||||||
|
pub fn new_on_date_script(
|
||||||
|
from: &Pubkey,
|
||||||
|
to: &Pubkey,
|
||||||
|
contract: &Pubkey,
|
||||||
|
dt: DateTime<Utc>,
|
||||||
|
dt_pubkey: &Pubkey,
|
||||||
|
cancelable: Option<Pubkey>,
|
||||||
|
lamports: u64,
|
||||||
|
) -> Script {
|
||||||
|
let expr = if let Some(from) = cancelable {
|
||||||
|
BudgetExpr::Or(
|
||||||
|
(
|
||||||
|
Condition::Timestamp(dt, *dt_pubkey),
|
||||||
|
Box::new(BudgetExpr::new_payment(lamports, to)),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
Condition::Signature(from),
|
||||||
|
Box::new(BudgetExpr::new_payment(lamports, &from)),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
BudgetExpr::After(
|
||||||
|
Condition::Timestamp(dt, *dt_pubkey),
|
||||||
|
Box::new(BudgetExpr::new_payment(lamports, to)),
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
Self::new_initialize_account_script(from, contract, lamports, expr)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create a multisig payment script.
|
||||||
|
pub fn new_when_signed_script(
|
||||||
|
from: &Pubkey,
|
||||||
|
to: &Pubkey,
|
||||||
|
contract: &Pubkey,
|
||||||
|
witness: &Pubkey,
|
||||||
|
cancelable: Option<Pubkey>,
|
||||||
|
lamports: u64,
|
||||||
|
) -> Script {
|
||||||
|
let expr = if let Some(from) = cancelable {
|
||||||
|
BudgetExpr::Or(
|
||||||
|
(
|
||||||
|
Condition::Signature(*witness),
|
||||||
|
Box::new(BudgetExpr::new_payment(lamports, to)),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
Condition::Signature(from),
|
||||||
|
Box::new(BudgetExpr::new_payment(lamports, &from)),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
BudgetExpr::After(
|
||||||
|
Condition::Signature(*witness),
|
||||||
|
Box::new(BudgetExpr::new_payment(lamports, to)),
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
Self::new_initialize_account_script(from, contract, lamports, expr)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn new_apply_timestamp(
|
pub fn new_apply_timestamp(
|
||||||
from: &Pubkey,
|
from: &Pubkey,
|
||||||
contract: &Pubkey,
|
contract: &Pubkey,
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
//! The `budget_transaction` module provides functionality for creating Budget transactions.
|
//! The `budget_transaction` module provides functionality for creating Budget transactions.
|
||||||
|
|
||||||
use crate::budget_expr::{BudgetExpr, Condition};
|
use crate::budget_expr::BudgetExpr;
|
||||||
use crate::budget_instruction::BudgetInstruction;
|
use crate::budget_instruction::BudgetInstruction;
|
||||||
use crate::budget_state::BudgetState;
|
use crate::budget_state::BudgetState;
|
||||||
use crate::id;
|
use crate::id;
|
||||||
|
@ -10,7 +10,7 @@ use solana_sdk::hash::Hash;
|
||||||
use solana_sdk::pubkey::Pubkey;
|
use solana_sdk::pubkey::Pubkey;
|
||||||
use solana_sdk::signature::{Keypair, KeypairUtil};
|
use solana_sdk::signature::{Keypair, KeypairUtil};
|
||||||
use solana_sdk::system_instruction::SystemInstruction;
|
use solana_sdk::system_instruction::SystemInstruction;
|
||||||
use solana_sdk::transaction::Transaction;
|
use solana_sdk::transaction::{Script, Transaction};
|
||||||
|
|
||||||
pub struct BudgetTransaction {}
|
pub struct BudgetTransaction {}
|
||||||
|
|
||||||
|
@ -36,6 +36,18 @@ impl BudgetTransaction {
|
||||||
tx
|
tx
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn new_signed(
|
||||||
|
from_keypair: &Keypair,
|
||||||
|
script: Script,
|
||||||
|
recent_blockhash: Hash,
|
||||||
|
fee: u64,
|
||||||
|
) -> Transaction {
|
||||||
|
let mut tx = Transaction::new(script);
|
||||||
|
tx.fee = fee;
|
||||||
|
tx.sign(&[from_keypair], recent_blockhash);
|
||||||
|
tx
|
||||||
|
}
|
||||||
|
|
||||||
/// Create and sign a new Transaction. Used for unit-testing.
|
/// Create and sign a new Transaction. Used for unit-testing.
|
||||||
pub fn new_payment(
|
pub fn new_payment(
|
||||||
from_keypair: &Keypair,
|
from_keypair: &Keypair,
|
||||||
|
@ -96,24 +108,16 @@ impl BudgetTransaction {
|
||||||
lamports: u64,
|
lamports: u64,
|
||||||
recent_blockhash: Hash,
|
recent_blockhash: Hash,
|
||||||
) -> Transaction {
|
) -> Transaction {
|
||||||
let expr = if let Some(from) = cancelable {
|
let script = BudgetInstruction::new_on_date_script(
|
||||||
BudgetExpr::Or(
|
&from_keypair.pubkey(),
|
||||||
(
|
to,
|
||||||
Condition::Timestamp(dt, *dt_pubkey),
|
contract,
|
||||||
Box::new(BudgetExpr::new_payment(lamports, to)),
|
dt,
|
||||||
),
|
dt_pubkey,
|
||||||
(
|
cancelable,
|
||||||
Condition::Signature(from),
|
lamports,
|
||||||
Box::new(BudgetExpr::new_payment(lamports, &from)),
|
);
|
||||||
),
|
Self::new_signed(from_keypair, script, recent_blockhash, 0)
|
||||||
)
|
|
||||||
} else {
|
|
||||||
BudgetExpr::After(
|
|
||||||
Condition::Timestamp(dt, *dt_pubkey),
|
|
||||||
Box::new(BudgetExpr::new_payment(lamports, to)),
|
|
||||||
)
|
|
||||||
};
|
|
||||||
Self::new(from_keypair, contract, expr, lamports, recent_blockhash, 0)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create and sign a multisig Transaction.
|
/// Create and sign a multisig Transaction.
|
||||||
|
@ -126,24 +130,15 @@ impl BudgetTransaction {
|
||||||
lamports: u64,
|
lamports: u64,
|
||||||
recent_blockhash: Hash,
|
recent_blockhash: Hash,
|
||||||
) -> Transaction {
|
) -> Transaction {
|
||||||
let expr = if let Some(from) = cancelable {
|
let script = BudgetInstruction::new_when_signed_script(
|
||||||
BudgetExpr::Or(
|
&from_keypair.pubkey(),
|
||||||
(
|
to,
|
||||||
Condition::Signature(*witness),
|
contract,
|
||||||
Box::new(BudgetExpr::new_payment(lamports, to)),
|
witness,
|
||||||
),
|
cancelable,
|
||||||
(
|
lamports,
|
||||||
Condition::Signature(from),
|
);
|
||||||
Box::new(BudgetExpr::new_payment(lamports, &from)),
|
Self::new_signed(from_keypair, script, recent_blockhash, 0)
|
||||||
),
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
BudgetExpr::After(
|
|
||||||
Condition::Signature(*witness),
|
|
||||||
Box::new(BudgetExpr::new_payment(lamports, to)),
|
|
||||||
)
|
|
||||||
};
|
|
||||||
Self::new(from_keypair, contract, expr, lamports, recent_blockhash, 0)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn system_instruction(tx: &Transaction, index: usize) -> Option<SystemInstruction> {
|
pub fn system_instruction(tx: &Transaction, index: usize) -> Option<SystemInstruction> {
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
use crate::bank::Bank;
|
use crate::bank::Bank;
|
||||||
use solana_sdk::signature::Keypair;
|
use solana_sdk::pubkey::Pubkey;
|
||||||
use solana_sdk::transaction::{Transaction, TransactionError};
|
use solana_sdk::signature::{Keypair, KeypairUtil};
|
||||||
|
use solana_sdk::system_instruction::SystemInstruction;
|
||||||
|
use solana_sdk::transaction::{Script, Transaction, TransactionError};
|
||||||
|
|
||||||
pub struct BankClient<'a> {
|
pub struct BankClient<'a> {
|
||||||
bank: &'a Bank,
|
bank: &'a Bank,
|
||||||
|
@ -12,8 +14,23 @@ impl<'a> BankClient<'a> {
|
||||||
Self { bank, keypair }
|
Self { bank, keypair }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn process_transaction(&self, tx: &mut Transaction) -> Result<(), TransactionError> {
|
pub fn pubkey(&self) -> Pubkey {
|
||||||
|
self.keypair.pubkey()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn process_transaction(&self, mut tx: Transaction) -> Result<(), TransactionError> {
|
||||||
tx.sign(&[&self.keypair], self.bank.last_blockhash());
|
tx.sign(&[&self.keypair], self.bank.last_blockhash());
|
||||||
self.bank.process_transaction(tx)
|
self.bank.process_transaction(&mut tx)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create and process a transaction.
|
||||||
|
pub fn process_script(&self, script: Script) -> Result<(), TransactionError> {
|
||||||
|
self.process_transaction(Transaction::new(script))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Transfer lamports to pubkey
|
||||||
|
pub fn transfer(&self, lamports: u64, pubkey: &Pubkey) -> Result<(), TransactionError> {
|
||||||
|
let move_instruction = SystemInstruction::new_move(&self.pubkey(), pubkey, lamports);
|
||||||
|
self.process_script(vec![move_instruction])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,39 +5,36 @@ use solana_sdk::native_program::ProgramError;
|
||||||
use solana_sdk::signature::{Keypair, KeypairUtil};
|
use solana_sdk::signature::{Keypair, KeypairUtil};
|
||||||
use solana_sdk::system_instruction::SystemInstruction;
|
use solana_sdk::system_instruction::SystemInstruction;
|
||||||
use solana_sdk::system_program;
|
use solana_sdk::system_program;
|
||||||
use solana_sdk::transaction::{Instruction, InstructionError, Transaction, TransactionError};
|
use solana_sdk::transaction::{Instruction, InstructionError, TransactionError};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_system_unsigned_transaction() {
|
fn test_system_unsigned_transaction() {
|
||||||
let (genesis_block, from_keypair) = GenesisBlock::new(100);
|
let (genesis_block, mint_keypair) = GenesisBlock::new(100);
|
||||||
let bank = Bank::new(&genesis_block);
|
let bank = Bank::new(&genesis_block);
|
||||||
let from_pubkey = from_keypair.pubkey();
|
|
||||||
let alice_client = BankClient::new(&bank, from_keypair);
|
|
||||||
|
|
||||||
let to_keypair = Keypair::new();
|
let alice_client = BankClient::new(&bank, mint_keypair);
|
||||||
let to_pubkey = to_keypair.pubkey();
|
let alice_pubkey = alice_client.pubkey();
|
||||||
let mallory_client = BankClient::new(&bank, to_keypair);
|
|
||||||
|
let mallory_client = BankClient::new(&bank, Keypair::new());
|
||||||
|
let mallory_pubkey = mallory_client.pubkey();
|
||||||
|
|
||||||
// Fund to account to bypass AccountNotFound error
|
// Fund to account to bypass AccountNotFound error
|
||||||
let ix = SystemInstruction::new_move(&from_pubkey, &to_pubkey, 50);
|
alice_client.transfer(50, &mallory_pubkey).unwrap();
|
||||||
let mut tx = Transaction::new(vec![ix]);
|
|
||||||
alice_client.process_transaction(&mut tx).unwrap();
|
|
||||||
|
|
||||||
// Erroneously sign transaction with recipient account key
|
// Erroneously sign transaction with recipient account key
|
||||||
// No signature case is tested by bank `test_zero_signatures()`
|
// No signature case is tested by bank `test_zero_signatures()`
|
||||||
let ix = Instruction::new(
|
let malicious_script = vec![Instruction::new(
|
||||||
system_program::id(),
|
system_program::id(),
|
||||||
&SystemInstruction::Move { lamports: 10 },
|
&SystemInstruction::Move { lamports: 10 },
|
||||||
vec![(from_pubkey, false), (to_pubkey, true)],
|
vec![(alice_pubkey, false), (mallory_pubkey, true)],
|
||||||
);
|
)];
|
||||||
let mut tx = Transaction::new(vec![ix]);
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
mallory_client.process_transaction(&mut tx),
|
mallory_client.process_script(malicious_script),
|
||||||
Err(TransactionError::InstructionError(
|
Err(TransactionError::InstructionError(
|
||||||
0,
|
0,
|
||||||
InstructionError::ProgramError(ProgramError::MissingRequiredSignature)
|
InstructionError::ProgramError(ProgramError::MissingRequiredSignature)
|
||||||
))
|
))
|
||||||
);
|
);
|
||||||
assert_eq!(bank.get_balance(&from_pubkey), 50);
|
assert_eq!(bank.get_balance(&alice_pubkey), 50);
|
||||||
assert_eq!(bank.get_balance(&to_pubkey), 50);
|
assert_eq!(bank.get_balance(&mallory_pubkey), 50);
|
||||||
}
|
}
|
||||||
|
|
|
@ -71,7 +71,10 @@ impl<P, Q> GenericInstruction<P, Q> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type Instruction = GenericInstruction<Pubkey, (Pubkey, bool)>;
|
pub type Instruction = GenericInstruction<Pubkey, (Pubkey, bool)>;
|
||||||
|
pub type Script = Vec<Instruction>;
|
||||||
|
|
||||||
pub type CompiledInstruction = GenericInstruction<u8, u8>;
|
pub type CompiledInstruction = GenericInstruction<u8, u8>;
|
||||||
|
pub type CompiledScript = Vec<CompiledInstruction>;
|
||||||
|
|
||||||
impl CompiledInstruction {
|
impl CompiledInstruction {
|
||||||
pub fn serialize_with(mut writer: &mut Cursor<&mut [u8]>, ix: &Self) -> Result<(), Error> {
|
pub fn serialize_with(mut writer: &mut Cursor<&mut [u8]>, ix: &Self) -> Result<(), Error> {
|
||||||
|
@ -163,12 +166,12 @@ pub struct Transaction {
|
||||||
pub program_ids: Vec<Pubkey>,
|
pub program_ids: Vec<Pubkey>,
|
||||||
/// Programs that will be executed in sequence and committed in one atomic transaction if all
|
/// Programs that will be executed in sequence and committed in one atomic transaction if all
|
||||||
/// succeed.
|
/// succeed.
|
||||||
pub instructions: Vec<CompiledInstruction>,
|
pub instructions: CompiledScript,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Transaction {
|
impl Transaction {
|
||||||
pub fn new(instructions: Vec<Instruction>) -> Self {
|
pub fn new(script: Script) -> Self {
|
||||||
TransactionCompiler::new(instructions).compile()
|
TransactionCompiler::new(script).compile()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_with_blockhash_and_fee<T: Serialize>(
|
pub fn new_with_blockhash_and_fee<T: Serialize>(
|
||||||
|
@ -224,7 +227,7 @@ impl Transaction {
|
||||||
recent_blockhash: Hash,
|
recent_blockhash: Hash,
|
||||||
fee: u64,
|
fee: u64,
|
||||||
program_ids: Vec<Pubkey>,
|
program_ids: Vec<Pubkey>,
|
||||||
instructions: Vec<CompiledInstruction>,
|
instructions: CompiledScript,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let mut account_keys: Vec<_> = from_keypairs
|
let mut account_keys: Vec<_> = from_keypairs
|
||||||
.iter()
|
.iter()
|
||||||
|
@ -466,7 +469,7 @@ impl<'a> serde::de::Visitor<'a> for TransactionVisitor {
|
||||||
let program_ids: Vec<Pubkey> =
|
let program_ids: Vec<Pubkey> =
|
||||||
deserialize_vec_with(&mut rd, Transaction::deserialize_pubkey)
|
deserialize_vec_with(&mut rd, Transaction::deserialize_pubkey)
|
||||||
.map_err(Error::custom)?;
|
.map_err(Error::custom)?;
|
||||||
let instructions: Vec<CompiledInstruction> =
|
let instructions: CompiledScript =
|
||||||
deserialize_vec_with(&mut rd, CompiledInstruction::deserialize_from)
|
deserialize_vec_with(&mut rd, CompiledInstruction::deserialize_from)
|
||||||
.map_err(Error::custom)?;
|
.map_err(Error::custom)?;
|
||||||
Ok(Transaction {
|
Ok(Transaction {
|
||||||
|
|
Loading…
Reference in New Issue