Rename TransactionCompiler to Script and use it to replace the type alias
This commit is contained in:
parent
99671472d1
commit
122c7bc2ef
|
@ -292,7 +292,7 @@ mod test {
|
||||||
dt,
|
dt,
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
alice_client.process_script(vec![instruction]).unwrap_err(),
|
alice_client.process_instruction(instruction).unwrap_err(),
|
||||||
TransactionError::InstructionError(
|
TransactionError::InstructionError(
|
||||||
0,
|
0,
|
||||||
InstructionError::ProgramError(ProgramError::CustomError(
|
InstructionError::ProgramError(ProgramError::CustomError(
|
||||||
|
@ -312,7 +312,7 @@ mod test {
|
||||||
// that pubkey's funds are now available.
|
// that pubkey's funds are now available.
|
||||||
let instruction =
|
let instruction =
|
||||||
BudgetInstruction::new_apply_timestamp(&alice_pubkey, &budget_pubkey, &bob_pubkey, dt);
|
BudgetInstruction::new_apply_timestamp(&alice_pubkey, &budget_pubkey, &bob_pubkey, dt);
|
||||||
alice_client.process_script(vec![instruction]).unwrap();
|
alice_client.process_instruction(instruction).unwrap();
|
||||||
assert_eq!(bank.get_balance(&alice_pubkey), 1);
|
assert_eq!(bank.get_balance(&alice_pubkey), 1);
|
||||||
assert_eq!(bank.get_balance(&budget_pubkey), 0);
|
assert_eq!(bank.get_balance(&budget_pubkey), 0);
|
||||||
assert_eq!(bank.get_balance(&bob_pubkey), 1);
|
assert_eq!(bank.get_balance(&bob_pubkey), 1);
|
||||||
|
@ -353,7 +353,7 @@ mod test {
|
||||||
|
|
||||||
let instruction =
|
let instruction =
|
||||||
BudgetInstruction::new_apply_signature(&mallory_pubkey, &budget_pubkey, &bob_pubkey);
|
BudgetInstruction::new_apply_signature(&mallory_pubkey, &budget_pubkey, &bob_pubkey);
|
||||||
mallory_client.process_script(vec![instruction]).unwrap();
|
mallory_client.process_instruction(instruction).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(&alice_pubkey), 1);
|
assert_eq!(bank.get_balance(&alice_pubkey), 1);
|
||||||
assert_eq!(bank.get_balance(&budget_pubkey), 1);
|
assert_eq!(bank.get_balance(&budget_pubkey), 1);
|
||||||
|
@ -362,7 +362,7 @@ mod test {
|
||||||
// Now, cancel the transaction. mint gets her funds back
|
// Now, cancel the transaction. mint gets her funds back
|
||||||
let instruction =
|
let instruction =
|
||||||
BudgetInstruction::new_apply_signature(&alice_pubkey, &budget_pubkey, &alice_pubkey);
|
BudgetInstruction::new_apply_signature(&alice_pubkey, &budget_pubkey, &alice_pubkey);
|
||||||
alice_client.process_script(vec![instruction]).unwrap();
|
alice_client.process_instruction(instruction).unwrap();
|
||||||
assert_eq!(bank.get_balance(&alice_pubkey), 2);
|
assert_eq!(bank.get_balance(&alice_pubkey), 2);
|
||||||
assert_eq!(bank.get_account(&budget_pubkey), None);
|
assert_eq!(bank.get_account(&budget_pubkey), None);
|
||||||
assert_eq!(bank.get_account(&bob_pubkey), None);
|
assert_eq!(bank.get_account(&bob_pubkey), None);
|
||||||
|
|
|
@ -5,9 +5,10 @@ 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::script::Script;
|
||||||
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::{Instruction, Script};
|
use solana_sdk::transaction::Instruction;
|
||||||
|
|
||||||
/// A smart contract.
|
/// A smart contract.
|
||||||
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
|
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
|
||||||
|
@ -48,10 +49,10 @@ impl BudgetInstruction {
|
||||||
expr: BudgetExpr,
|
expr: BudgetExpr,
|
||||||
) -> Script {
|
) -> Script {
|
||||||
let space = serialized_size(&BudgetState::new(expr.clone())).unwrap();
|
let space = serialized_size(&BudgetState::new(expr.clone())).unwrap();
|
||||||
vec![
|
Script::new(vec![
|
||||||
SystemInstruction::new_program_account(&from, contract, lamports, space, &id()),
|
SystemInstruction::new_program_account(&from, contract, lamports, space, &id()),
|
||||||
Self::new_initialize_account(contract, expr),
|
Self::new_initialize_account(contract, expr),
|
||||||
]
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a new payment script.
|
/// Create a new payment script.
|
||||||
|
|
|
@ -5,9 +5,10 @@ use bincode::deserialize;
|
||||||
use chrono::prelude::*;
|
use chrono::prelude::*;
|
||||||
use solana_sdk::hash::Hash;
|
use solana_sdk::hash::Hash;
|
||||||
use solana_sdk::pubkey::Pubkey;
|
use solana_sdk::pubkey::Pubkey;
|
||||||
|
use solana_sdk::script::Script;
|
||||||
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::{Script, Transaction};
|
use solana_sdk::transaction::Transaction;
|
||||||
|
|
||||||
pub struct BudgetTransaction {}
|
pub struct BudgetTransaction {}
|
||||||
|
|
||||||
|
@ -18,7 +19,7 @@ impl BudgetTransaction {
|
||||||
recent_blockhash: Hash,
|
recent_blockhash: Hash,
|
||||||
fee: u64,
|
fee: u64,
|
||||||
) -> Transaction {
|
) -> Transaction {
|
||||||
let mut tx = Transaction::new(script);
|
let mut tx = script.compile();
|
||||||
tx.fee = fee;
|
tx.fee = fee;
|
||||||
tx.sign(&[from_keypair], recent_blockhash);
|
tx.sign(&[from_keypair], recent_blockhash);
|
||||||
tx
|
tx
|
||||||
|
|
|
@ -55,9 +55,10 @@ mod tests {
|
||||||
use solana_runtime::bank::Bank;
|
use solana_runtime::bank::Bank;
|
||||||
use solana_runtime::bank_client::BankClient;
|
use solana_runtime::bank_client::BankClient;
|
||||||
use solana_sdk::genesis_block::GenesisBlock;
|
use solana_sdk::genesis_block::GenesisBlock;
|
||||||
|
use solana_sdk::script::Script;
|
||||||
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::{Instruction, Transaction};
|
use solana_sdk::transaction::Instruction;
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Default, Debug, PartialEq)]
|
#[derive(Serialize, Deserialize, Default, Debug, PartialEq)]
|
||||||
struct MyConfig {
|
struct MyConfig {
|
||||||
|
@ -96,7 +97,7 @@ mod tests {
|
||||||
let config_pubkey = config_client.pubkey();
|
let config_pubkey = config_client.pubkey();
|
||||||
|
|
||||||
let instruction = create_account_instruction(&from_pubkey, &config_pubkey);
|
let instruction = create_account_instruction(&from_pubkey, &config_pubkey);
|
||||||
from_client.process_script(vec![instruction]).unwrap();
|
from_client.process_instruction(instruction).unwrap();
|
||||||
|
|
||||||
config_client
|
config_client
|
||||||
}
|
}
|
||||||
|
@ -123,7 +124,7 @@ mod tests {
|
||||||
|
|
||||||
let my_config = MyConfig::new(42);
|
let my_config = MyConfig::new(42);
|
||||||
let instruction = ConfigInstruction::new_store(&config_pubkey, &my_config);
|
let instruction = ConfigInstruction::new_store(&config_pubkey, &my_config);
|
||||||
config_client.process_script(vec![instruction]).unwrap();
|
config_client.process_instruction(instruction).unwrap();
|
||||||
|
|
||||||
let config_account = bank.get_account(&config_pubkey).unwrap();
|
let config_account = bank.get_account(&config_pubkey).unwrap();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
@ -143,7 +144,8 @@ mod tests {
|
||||||
let instruction = ConfigInstruction::new_store(&config_pubkey, &my_config);
|
let instruction = ConfigInstruction::new_store(&config_pubkey, &my_config);
|
||||||
|
|
||||||
// Replace instruction data with a vector that's too large
|
// Replace instruction data with a vector that's too large
|
||||||
let mut transaction = Transaction::new(vec![instruction]);
|
let script = Script::new(vec![instruction]);
|
||||||
|
let mut transaction = script.compile();
|
||||||
transaction.instructions[0].data = vec![0; 123];
|
transaction.instructions[0].data = vec![0; 123];
|
||||||
config_client.process_transaction(transaction).unwrap_err();
|
config_client.process_transaction(transaction).unwrap_err();
|
||||||
}
|
}
|
||||||
|
@ -164,7 +166,8 @@ mod tests {
|
||||||
let store_instruction = ConfigInstruction::new_store(&config_pubkey, &my_config);
|
let store_instruction = ConfigInstruction::new_store(&config_pubkey, &my_config);
|
||||||
|
|
||||||
// Don't sign the transaction with `config_client`
|
// Don't sign the transaction with `config_client`
|
||||||
let mut transaction = Transaction::new(vec![move_instruction, store_instruction]);
|
let script = Script::new(vec![move_instruction, store_instruction]);
|
||||||
|
let mut transaction = script.compile();
|
||||||
transaction.sign_unchecked(&[&system_keypair], bank.last_blockhash());
|
transaction.sign_unchecked(&[&system_keypair], bank.last_blockhash());
|
||||||
let system_client = BankClient::new(&bank, system_keypair);
|
let system_client = BankClient::new(&bank, system_keypair);
|
||||||
system_client.process_transaction(transaction).unwrap_err();
|
system_client.process_transaction(transaction).unwrap_err();
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
use crate::bank::Bank;
|
use crate::bank::Bank;
|
||||||
use solana_sdk::pubkey::Pubkey;
|
use solana_sdk::pubkey::Pubkey;
|
||||||
|
use solana_sdk::script::Script;
|
||||||
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::{Script, Transaction, TransactionError};
|
use solana_sdk::transaction::{Instruction, Transaction, TransactionError};
|
||||||
|
|
||||||
pub struct BankClient<'a> {
|
pub struct BankClient<'a> {
|
||||||
bank: &'a Bank,
|
bank: &'a Bank,
|
||||||
|
@ -25,12 +26,25 @@ impl<'a> BankClient<'a> {
|
||||||
|
|
||||||
/// Create and process a transaction.
|
/// Create and process a transaction.
|
||||||
pub fn process_script(&self, script: Script) -> Result<(), TransactionError> {
|
pub fn process_script(&self, script: Script) -> Result<(), TransactionError> {
|
||||||
self.process_transaction(Transaction::new(script))
|
self.process_transaction(script.compile())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create and process a transaction from a list of instructions.
|
||||||
|
pub fn process_instructions(
|
||||||
|
&self,
|
||||||
|
instructions: Vec<Instruction>,
|
||||||
|
) -> Result<(), TransactionError> {
|
||||||
|
self.process_script(Script::new(instructions))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create and process a transaction from a single instruction.
|
||||||
|
pub fn process_instruction(&self, instruction: Instruction) -> Result<(), TransactionError> {
|
||||||
|
self.process_instructions(vec![instruction])
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Transfer lamports to pubkey
|
/// Transfer lamports to pubkey
|
||||||
pub fn transfer(&self, lamports: u64, pubkey: &Pubkey) -> Result<(), TransactionError> {
|
pub fn transfer(&self, lamports: u64, pubkey: &Pubkey) -> Result<(), TransactionError> {
|
||||||
let move_instruction = SystemInstruction::new_move(&self.pubkey(), pubkey, lamports);
|
let move_instruction = SystemInstruction::new_move(&self.pubkey(), pubkey, lamports);
|
||||||
self.process_script(vec![move_instruction])
|
self.process_instruction(move_instruction)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -109,6 +109,7 @@ mod tests {
|
||||||
use solana_sdk::account::Account;
|
use solana_sdk::account::Account;
|
||||||
use solana_sdk::genesis_block::GenesisBlock;
|
use solana_sdk::genesis_block::GenesisBlock;
|
||||||
use solana_sdk::native_program::ProgramError;
|
use solana_sdk::native_program::ProgramError;
|
||||||
|
use solana_sdk::script::Script;
|
||||||
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;
|
||||||
|
@ -291,11 +292,11 @@ mod tests {
|
||||||
|
|
||||||
// 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 malicious_script = vec![Instruction::new(
|
let malicious_script = Script::new(vec![Instruction::new(
|
||||||
system_program::id(),
|
system_program::id(),
|
||||||
&SystemInstruction::Move { lamports: 10 },
|
&SystemInstruction::Move { lamports: 10 },
|
||||||
vec![(alice_pubkey, false), (mallory_pubkey, true)],
|
vec![(alice_pubkey, false), (mallory_pubkey, true)],
|
||||||
)];
|
)]);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
mallory_client.process_script(malicious_script),
|
mallory_client.process_script(malicious_script),
|
||||||
Err(TransactionError::InstructionError(
|
Err(TransactionError::InstructionError(
|
||||||
|
|
|
@ -9,6 +9,7 @@ pub mod native_program;
|
||||||
pub mod packet;
|
pub mod packet;
|
||||||
pub mod pubkey;
|
pub mod pubkey;
|
||||||
pub mod rpc_port;
|
pub mod rpc_port;
|
||||||
|
pub mod script;
|
||||||
pub mod shortvec;
|
pub mod shortvec;
|
||||||
pub mod signature;
|
pub mod signature;
|
||||||
pub mod system_instruction;
|
pub mod system_instruction;
|
||||||
|
@ -16,7 +17,6 @@ pub mod system_program;
|
||||||
pub mod system_transaction;
|
pub mod system_transaction;
|
||||||
pub mod timing;
|
pub mod timing;
|
||||||
pub mod transaction;
|
pub mod transaction;
|
||||||
mod transaction_compiler;
|
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate serde_derive;
|
extern crate serde_derive;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
//! A library for composing transactions.
|
//! A library for building scripts and compiling them into transactions.
|
||||||
|
|
||||||
use crate::hash::Hash;
|
use crate::hash::Hash;
|
||||||
use crate::pubkey::Pubkey;
|
use crate::pubkey::Pubkey;
|
||||||
|
@ -33,11 +33,11 @@ fn compile_instructions(
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A utility for constructing transactions
|
/// A utility for constructing transactions
|
||||||
pub struct TransactionCompiler {
|
pub struct Script {
|
||||||
instructions: Vec<Instruction>,
|
instructions: Vec<Instruction>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TransactionCompiler {
|
impl Script {
|
||||||
/// Create a new unsigned transaction from a single instruction
|
/// Create a new unsigned transaction from a single instruction
|
||||||
pub fn new(instructions: Vec<Instruction>) -> Self {
|
pub fn new(instructions: Vec<Instruction>) -> Self {
|
||||||
Self { instructions }
|
Self { instructions }
|
||||||
|
@ -100,7 +100,7 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_transaction_builder_unique_program_ids() {
|
fn test_transaction_builder_unique_program_ids() {
|
||||||
let program_id0 = Pubkey::default();
|
let program_id0 = Pubkey::default();
|
||||||
let program_ids = TransactionCompiler::new(vec![
|
let program_ids = Script::new(vec![
|
||||||
Instruction::new(program_id0, &0, vec![]),
|
Instruction::new(program_id0, &0, vec![]),
|
||||||
Instruction::new(program_id0, &0, vec![]),
|
Instruction::new(program_id0, &0, vec![]),
|
||||||
])
|
])
|
||||||
|
@ -112,7 +112,7 @@ mod tests {
|
||||||
fn test_transaction_builder_unique_program_ids_not_adjacent() {
|
fn test_transaction_builder_unique_program_ids_not_adjacent() {
|
||||||
let program_id0 = Pubkey::default();
|
let program_id0 = Pubkey::default();
|
||||||
let program_id1 = Keypair::new().pubkey();
|
let program_id1 = Keypair::new().pubkey();
|
||||||
let program_ids = TransactionCompiler::new(vec![
|
let program_ids = Script::new(vec![
|
||||||
Instruction::new(program_id0, &0, vec![]),
|
Instruction::new(program_id0, &0, vec![]),
|
||||||
Instruction::new(program_id1, &0, vec![]),
|
Instruction::new(program_id1, &0, vec![]),
|
||||||
Instruction::new(program_id0, &0, vec![]),
|
Instruction::new(program_id0, &0, vec![]),
|
||||||
|
@ -125,7 +125,7 @@ mod tests {
|
||||||
fn test_transaction_builder_unique_program_ids_order_preserved() {
|
fn test_transaction_builder_unique_program_ids_order_preserved() {
|
||||||
let program_id0 = Keypair::new().pubkey();
|
let program_id0 = Keypair::new().pubkey();
|
||||||
let program_id1 = Pubkey::default(); // Key less than program_id0
|
let program_id1 = Pubkey::default(); // Key less than program_id0
|
||||||
let program_ids = TransactionCompiler::new(vec![
|
let program_ids = Script::new(vec![
|
||||||
Instruction::new(program_id0, &0, vec![]),
|
Instruction::new(program_id0, &0, vec![]),
|
||||||
Instruction::new(program_id1, &0, vec![]),
|
Instruction::new(program_id1, &0, vec![]),
|
||||||
Instruction::new(program_id0, &0, vec![]),
|
Instruction::new(program_id0, &0, vec![]),
|
||||||
|
@ -138,7 +138,7 @@ mod tests {
|
||||||
fn test_transaction_builder_unique_keys_both_signed() {
|
fn test_transaction_builder_unique_keys_both_signed() {
|
||||||
let program_id = Pubkey::default();
|
let program_id = Pubkey::default();
|
||||||
let id0 = Pubkey::default();
|
let id0 = Pubkey::default();
|
||||||
let keys = TransactionCompiler::new(vec![
|
let keys = Script::new(vec![
|
||||||
Instruction::new(program_id, &0, vec![(id0, true)]),
|
Instruction::new(program_id, &0, vec![(id0, true)]),
|
||||||
Instruction::new(program_id, &0, vec![(id0, true)]),
|
Instruction::new(program_id, &0, vec![(id0, true)]),
|
||||||
])
|
])
|
||||||
|
@ -150,7 +150,7 @@ mod tests {
|
||||||
fn test_transaction_builder_unique_keys_one_signed() {
|
fn test_transaction_builder_unique_keys_one_signed() {
|
||||||
let program_id = Pubkey::default();
|
let program_id = Pubkey::default();
|
||||||
let id0 = Pubkey::default();
|
let id0 = Pubkey::default();
|
||||||
let keys = TransactionCompiler::new(vec![
|
let keys = Script::new(vec![
|
||||||
Instruction::new(program_id, &0, vec![(id0, false)]),
|
Instruction::new(program_id, &0, vec![(id0, false)]),
|
||||||
Instruction::new(program_id, &0, vec![(id0, true)]),
|
Instruction::new(program_id, &0, vec![(id0, true)]),
|
||||||
])
|
])
|
||||||
|
@ -163,7 +163,7 @@ mod tests {
|
||||||
let program_id = Pubkey::default();
|
let program_id = Pubkey::default();
|
||||||
let id0 = Keypair::new().pubkey();
|
let id0 = Keypair::new().pubkey();
|
||||||
let id1 = Pubkey::default(); // Key less than id0
|
let id1 = Pubkey::default(); // Key less than id0
|
||||||
let keys = TransactionCompiler::new(vec![
|
let keys = Script::new(vec![
|
||||||
Instruction::new(program_id, &0, vec![(id0, false)]),
|
Instruction::new(program_id, &0, vec![(id0, false)]),
|
||||||
Instruction::new(program_id, &0, vec![(id1, false)]),
|
Instruction::new(program_id, &0, vec![(id1, false)]),
|
||||||
])
|
])
|
||||||
|
@ -176,7 +176,7 @@ mod tests {
|
||||||
let program_id = Pubkey::default();
|
let program_id = Pubkey::default();
|
||||||
let id0 = Pubkey::default();
|
let id0 = Pubkey::default();
|
||||||
let id1 = Keypair::new().pubkey();
|
let id1 = Keypair::new().pubkey();
|
||||||
let keys = TransactionCompiler::new(vec![
|
let keys = Script::new(vec![
|
||||||
Instruction::new(program_id, &0, vec![(id0, false)]),
|
Instruction::new(program_id, &0, vec![(id0, false)]),
|
||||||
Instruction::new(program_id, &0, vec![(id1, false)]),
|
Instruction::new(program_id, &0, vec![(id1, false)]),
|
||||||
Instruction::new(program_id, &0, vec![(id0, true)]),
|
Instruction::new(program_id, &0, vec![(id0, true)]),
|
||||||
|
@ -190,7 +190,7 @@ mod tests {
|
||||||
let program_id = Pubkey::default();
|
let program_id = Pubkey::default();
|
||||||
let id0 = Pubkey::default();
|
let id0 = Pubkey::default();
|
||||||
let id1 = Keypair::new().pubkey();
|
let id1 = Keypair::new().pubkey();
|
||||||
let keys = TransactionCompiler::new(vec![
|
let keys = Script::new(vec![
|
||||||
Instruction::new(program_id, &0, vec![(id0, false)]),
|
Instruction::new(program_id, &0, vec![(id0, false)]),
|
||||||
Instruction::new(program_id, &0, vec![(id1, true)]),
|
Instruction::new(program_id, &0, vec![(id1, true)]),
|
||||||
])
|
])
|
||||||
|
@ -203,14 +203,10 @@ mod tests {
|
||||||
fn test_transaction_builder_signed_keys_len() {
|
fn test_transaction_builder_signed_keys_len() {
|
||||||
let program_id = Pubkey::default();
|
let program_id = Pubkey::default();
|
||||||
let id0 = Pubkey::default();
|
let id0 = Pubkey::default();
|
||||||
let tx =
|
let tx = Script::new(vec![Instruction::new(program_id, &0, vec![(id0, false)])]).compile();
|
||||||
TransactionCompiler::new(vec![Instruction::new(program_id, &0, vec![(id0, false)])])
|
|
||||||
.compile();
|
|
||||||
assert_eq!(tx.signatures.capacity(), 0);
|
assert_eq!(tx.signatures.capacity(), 0);
|
||||||
|
|
||||||
let tx =
|
let tx = Script::new(vec![Instruction::new(program_id, &0, vec![(id0, true)])]).compile();
|
||||||
TransactionCompiler::new(vec![Instruction::new(program_id, &0, vec![(id0, true)])])
|
|
||||||
.compile();
|
|
||||||
assert_eq!(tx.signatures.capacity(), 1);
|
assert_eq!(tx.signatures.capacity(), 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -221,7 +217,7 @@ mod tests {
|
||||||
let id0 = Pubkey::default();
|
let id0 = Pubkey::default();
|
||||||
let keypair1 = Keypair::new();
|
let keypair1 = Keypair::new();
|
||||||
let id1 = keypair1.pubkey();
|
let id1 = keypair1.pubkey();
|
||||||
let tx = TransactionCompiler::new(vec![
|
let tx = Script::new(vec![
|
||||||
Instruction::new(program_id0, &0, vec![(id0, false)]),
|
Instruction::new(program_id0, &0, vec![(id0, false)]),
|
||||||
Instruction::new(program_id1, &0, vec![(id1, true)]),
|
Instruction::new(program_id1, &0, vec![(id1, true)]),
|
||||||
Instruction::new(program_id0, &0, vec![(id1, false)]),
|
Instruction::new(program_id0, &0, vec![(id1, false)]),
|
|
@ -4,13 +4,13 @@ use crate::hash::{Hash, Hasher};
|
||||||
use crate::native_program::ProgramError;
|
use crate::native_program::ProgramError;
|
||||||
use crate::packet::PACKET_DATA_SIZE;
|
use crate::packet::PACKET_DATA_SIZE;
|
||||||
use crate::pubkey::Pubkey;
|
use crate::pubkey::Pubkey;
|
||||||
|
use crate::script::Script;
|
||||||
use crate::shortvec::{
|
use crate::shortvec::{
|
||||||
deserialize_vec_bytes, deserialize_vec_with, encode_len, serialize_vec_bytes,
|
deserialize_vec_bytes, deserialize_vec_with, encode_len, serialize_vec_bytes,
|
||||||
serialize_vec_with,
|
serialize_vec_with,
|
||||||
};
|
};
|
||||||
use crate::signature::{KeypairUtil, Signature};
|
use crate::signature::{KeypairUtil, Signature};
|
||||||
use crate::system_instruction::SystemError;
|
use crate::system_instruction::SystemError;
|
||||||
use crate::transaction_compiler::TransactionCompiler;
|
|
||||||
use bincode::{serialize, Error};
|
use bincode::{serialize, Error};
|
||||||
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
|
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
|
||||||
use serde::{Deserialize, Serialize, Serializer};
|
use serde::{Deserialize, Serialize, Serializer};
|
||||||
|
@ -71,10 +71,7 @@ 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> {
|
||||||
|
@ -166,12 +163,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: CompiledScript,
|
pub instructions: Vec<CompiledInstruction>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Transaction {
|
impl Transaction {
|
||||||
pub fn new(script: Script) -> Self {
|
pub fn new(instructions: Vec<Instruction>) -> Self {
|
||||||
TransactionCompiler::new(script).compile()
|
Script::new(instructions).compile()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_with_blockhash_and_fee<T: Serialize>(
|
pub fn new_with_blockhash_and_fee<T: Serialize>(
|
||||||
|
@ -227,7 +224,7 @@ impl Transaction {
|
||||||
recent_blockhash: Hash,
|
recent_blockhash: Hash,
|
||||||
fee: u64,
|
fee: u64,
|
||||||
program_ids: Vec<Pubkey>,
|
program_ids: Vec<Pubkey>,
|
||||||
instructions: CompiledScript,
|
instructions: Vec<CompiledInstruction>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let mut account_keys: Vec<_> = from_keypairs
|
let mut account_keys: Vec<_> = from_keypairs
|
||||||
.iter()
|
.iter()
|
||||||
|
@ -469,7 +466,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: CompiledScript =
|
let instructions: Vec<CompiledInstruction> =
|
||||||
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