Pass any serializable to Transaction constructor

This commit is contained in:
Greg Fitzgerald
2018-11-06 06:50:00 -07:00
parent d8d23c9971
commit bfad138bb3
9 changed files with 52 additions and 96 deletions

View File

@ -292,12 +292,12 @@ mod test {
]; ];
let from = Keypair::new(); let from = Keypair::new();
let contract = Keypair::new(); let contract = Keypair::new();
let userdata = vec![1, 2, 3]; let userdata = (1u8, 2u8, 3u8);
let tx = Transaction::new( let tx = Transaction::new(
&from, &from,
&[contract.pubkey()], &[contract.pubkey()],
BudgetState::id(), BudgetState::id(),
userdata, &userdata,
Hash::default(), Hash::default(),
0, 0,
); );

View File

@ -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 bincode::{deserialize, serialize}; use bincode::deserialize;
use budget_expr::{BudgetExpr, Condition}; use budget_expr::{BudgetExpr, Condition};
use budget_instruction::Instruction; use budget_instruction::Instruction;
use budget_program::BudgetState; use budget_program::BudgetState;
@ -78,28 +78,18 @@ impl BudgetTransaction for Transaction {
let keys = vec![from_keypair.pubkey(), contract]; let keys = vec![from_keypair.pubkey(), contract];
let system_instruction = SystemProgram::Move { tokens }; let system_instruction = SystemProgram::Move { tokens };
let move_userdata = serialize(&system_instruction).unwrap();
let payment = Payment { let payment = Payment {
tokens: tokens - fee, tokens: tokens - fee,
to, to,
}; };
let budget_instruction = Instruction::NewBudget(BudgetExpr::Pay(payment)); let budget_instruction = Instruction::NewBudget(BudgetExpr::Pay(payment));
let pay_userdata = serialize(&budget_instruction).unwrap();
let program_ids = vec![SystemProgram::id(), BudgetState::id()]; let program_ids = vec![SystemProgram::id(), BudgetState::id()];
let instructions = vec![ let instructions = vec![
transaction::Instruction { transaction::Instruction::new(0, &system_instruction, vec![0, 1]),
program_ids_index: 0, transaction::Instruction::new(1, &budget_instruction, vec![1]),
userdata: move_userdata,
accounts: vec![0, 1],
},
transaction::Instruction {
program_ids_index: 1,
userdata: pay_userdata,
accounts: vec![1],
},
]; ];
Self::new_with_instructions(from_keypair, &keys, last_id, fee, program_ids, instructions) Self::new_with_instructions(from_keypair, &keys, last_id, fee, program_ids, instructions)
@ -119,12 +109,11 @@ impl BudgetTransaction for Transaction {
last_id: Hash, last_id: Hash,
) -> Self { ) -> Self {
let instruction = Instruction::ApplyTimestamp(dt); let instruction = Instruction::ApplyTimestamp(dt);
let userdata = serialize(&instruction).unwrap();
Self::new( Self::new(
from_keypair, from_keypair,
&[contract, to], &[contract, to],
BudgetState::id(), BudgetState::id(),
userdata, &instruction,
last_id, last_id,
0, 0,
) )
@ -138,12 +127,11 @@ impl BudgetTransaction for Transaction {
last_id: Hash, last_id: Hash,
) -> Self { ) -> Self {
let instruction = Instruction::ApplySignature; let instruction = Instruction::ApplySignature;
let userdata = serialize(&instruction).unwrap();
Self::new( Self::new(
from_keypair, from_keypair,
&[contract, to], &[contract, to],
BudgetState::id(), BudgetState::id(),
userdata, &instruction,
last_id, last_id,
0, 0,
) )
@ -169,12 +157,11 @@ impl BudgetTransaction for Transaction {
BudgetExpr::After(Condition::Timestamp(dt, dt_pubkey), Payment { tokens, to }) BudgetExpr::After(Condition::Timestamp(dt, dt_pubkey), Payment { tokens, to })
}; };
let instruction = Instruction::NewBudget(expr); let instruction = Instruction::NewBudget(expr);
let userdata = serialize(&instruction).expect("serialize instruction");
Self::new( Self::new(
from_keypair, from_keypair,
&[contract], &[contract],
BudgetState::id(), BudgetState::id(),
userdata, &instruction,
last_id, last_id,
0, 0,
) )
@ -198,12 +185,11 @@ impl BudgetTransaction for Transaction {
BudgetExpr::After(Condition::Signature(witness), Payment { tokens, to }) BudgetExpr::After(Condition::Signature(witness), Payment { tokens, to })
}; };
let instruction = Instruction::NewBudget(expr); let instruction = Instruction::NewBudget(expr);
let userdata = serialize(&instruction).expect("serialize instruction");
Self::new( Self::new(
from_keypair, from_keypair,
&[contract], &[contract],
BudgetState::id(), BudgetState::id(),
userdata, &instruction,
last_id, last_id,
0, 0,
) )
@ -270,12 +256,7 @@ mod tests {
to: Default::default(), to: Default::default(),
}); });
let instruction = Instruction::NewBudget(expr); let instruction = Instruction::NewBudget(expr);
let userdata = serialize(&instruction).unwrap(); let instructions = vec![transaction::Instruction::new(0, &instruction, vec![])];
let instructions = vec![transaction::Instruction {
program_ids_index: 0,
userdata,
accounts: vec![],
}];
let claim0 = Transaction { let claim0 = Transaction {
account_keys: vec![], account_keys: vec![],
last_id: Default::default(), last_id: Default::default(),

View File

@ -1,6 +1,5 @@
//! The `dynamic_transaction` module provides functionality for loading and calling a program //! The `dynamic_transaction` module provides functionality for loading and calling a program
use bincode::serialize;
use hash::Hash; use hash::Hash;
use signature::{Keypair, KeypairUtil}; use signature::{Keypair, KeypairUtil};
use solana_sdk::loader_instruction::LoaderInstruction; use solana_sdk::loader_instruction::LoaderInstruction;
@ -36,8 +35,7 @@ impl LoaderTransaction for Transaction {
bytes.len() bytes.len()
); );
let instruction = LoaderInstruction::Write { offset, bytes }; let instruction = LoaderInstruction::Write { offset, bytes };
let userdata = serialize(&instruction).unwrap(); Transaction::new(from_keypair, &[], loader, &instruction, last_id, fee)
Transaction::new(from_keypair, &[], loader, userdata, last_id, fee)
} }
fn finalize(from_keypair: &Keypair, loader: Pubkey, last_id: Hash, fee: u64) -> Self { fn finalize(from_keypair: &Keypair, loader: Pubkey, last_id: Hash, fee: u64) -> Self {
@ -46,7 +44,6 @@ impl LoaderTransaction for Transaction {
from_keypair.pubkey(), from_keypair.pubkey(),
); );
let instruction = LoaderInstruction::Finalize; let instruction = LoaderInstruction::Finalize;
let userdata = serialize(&instruction).unwrap(); Transaction::new(from_keypair, &[], loader, &instruction, last_id, fee)
Transaction::new(from_keypair, &[], loader, userdata, last_id, fee)
} }
} }

View File

@ -66,7 +66,7 @@ mod test {
&keypair, &keypair,
&[], &[],
StorageProgram::id(), StorageProgram::id(),
vec![], &(),
Default::default(), Default::default(),
0, 0,
); );

View File

@ -1,4 +1,3 @@
use bincode::serialize;
use hash::Hash; use hash::Hash;
use signature::{Keypair, KeypairUtil}; use signature::{Keypair, KeypairUtil};
use storage_program::StorageProgram; use storage_program::StorageProgram;
@ -11,12 +10,11 @@ pub trait StorageTransaction {
impl StorageTransaction for Transaction { impl StorageTransaction for Transaction {
fn storage_new_mining_proof(from_keypair: &Keypair, sha_state: Hash, last_id: Hash) -> Self { fn storage_new_mining_proof(from_keypair: &Keypair, sha_state: Hash, last_id: Hash) -> Self {
let program = StorageProgram::SubmitMiningProof { sha_state }; let program = StorageProgram::SubmitMiningProof { sha_state };
let userdata = serialize(&program).unwrap();
Transaction::new( Transaction::new(
from_keypair, from_keypair,
&[from_keypair.pubkey()], &[from_keypair.pubkey()],
StorageProgram::id(), StorageProgram::id(),
userdata, &program,
last_id, last_id,
0, 0,
) )

View File

@ -1,6 +1,5 @@
//! The `system_transaction` module provides functionality for creating system transactions. //! The `system_transaction` module provides functionality for creating system transactions.
use bincode::serialize;
use hash::Hash; use hash::Hash;
use signature::{Keypair, KeypairUtil}; use signature::{Keypair, KeypairUtil};
use solana_sdk::pubkey::Pubkey; use solana_sdk::pubkey::Pubkey;
@ -56,12 +55,11 @@ impl SystemTransaction for Transaction {
space, space,
program_id, program_id,
}; };
let userdata = serialize(&create).unwrap();
Transaction::new( Transaction::new(
from_keypair, from_keypair,
&[to], &[to],
SystemProgram::id(), SystemProgram::id(),
userdata, &create,
last_id, last_id,
fee, fee,
) )
@ -69,12 +67,11 @@ impl SystemTransaction for Transaction {
/// Create and sign new SystemProgram::Assign transaction /// Create and sign new SystemProgram::Assign transaction
fn system_assign(from_keypair: &Keypair, last_id: Hash, program_id: Pubkey, fee: u64) -> Self { fn system_assign(from_keypair: &Keypair, last_id: Hash, program_id: Pubkey, fee: u64) -> Self {
let assign = SystemProgram::Assign { program_id }; let assign = SystemProgram::Assign { program_id };
let userdata = serialize(&assign).unwrap();
Transaction::new( Transaction::new(
from_keypair, from_keypair,
&[], &[],
SystemProgram::id(), SystemProgram::id(),
userdata, &assign,
last_id, last_id,
fee, fee,
) )
@ -92,12 +89,11 @@ impl SystemTransaction for Transaction {
fee: u64, fee: u64,
) -> Self { ) -> Self {
let move_tokens = SystemProgram::Move { tokens }; let move_tokens = SystemProgram::Move { tokens };
let userdata = serialize(&move_tokens).unwrap();
Transaction::new( Transaction::new(
from_keypair, from_keypair,
&[to], &[to],
SystemProgram::id(), SystemProgram::id(),
userdata, &move_tokens,
last_id, last_id,
fee, fee,
) )
@ -109,11 +105,7 @@ impl SystemTransaction for Transaction {
.enumerate() .enumerate()
.map(|(i, (_, amount))| { .map(|(i, (_, amount))| {
let spend = SystemProgram::Move { tokens: *amount }; let spend = SystemProgram::Move { tokens: *amount };
Instruction { Instruction::new(0, &spend, vec![0, i as u8 + 1])
program_ids_index: 0,
userdata: serialize(&spend).unwrap(),
accounts: vec![0, i as u8 + 1],
}
}).collect(); }).collect();
let to_keys: Vec<_> = moves.iter().map(|(to_key, _)| *to_key).collect(); let to_keys: Vec<_> = moves.iter().map(|(to_key, _)| *to_key).collect();
@ -129,15 +121,7 @@ impl SystemTransaction for Transaction {
/// Create and sign new SystemProgram::Spawn transaction /// Create and sign new SystemProgram::Spawn transaction
fn system_spawn(from_keypair: &Keypair, last_id: Hash, fee: u64) -> Self { fn system_spawn(from_keypair: &Keypair, last_id: Hash, fee: u64) -> Self {
let spawn = SystemProgram::Spawn; let spawn = SystemProgram::Spawn;
let userdata = serialize(&spawn).unwrap(); Transaction::new(from_keypair, &[], SystemProgram::id(), &spawn, last_id, fee)
Transaction::new(
from_keypair,
&[],
SystemProgram::id(),
userdata,
last_id,
fee,
)
} }
} }

View File

@ -2,6 +2,7 @@
use bincode::serialize; use bincode::serialize;
use hash::{Hash, Hasher}; use hash::{Hash, Hasher};
use serde::Serialize;
use signature::{Keypair, KeypairUtil, Signature}; use signature::{Keypair, KeypairUtil, Signature};
use solana_sdk::pubkey::Pubkey; use solana_sdk::pubkey::Pubkey;
use std::mem::size_of; use std::mem::size_of;
@ -23,6 +24,17 @@ pub struct Instruction {
pub userdata: Vec<u8>, pub userdata: Vec<u8>,
} }
impl Instruction {
pub fn new<T: Serialize>(program_ids_index: u8, userdata: &T, accounts: Vec<u8>) -> Self {
let userdata = serialize(userdata).unwrap();
Instruction {
program_ids_index,
userdata,
accounts,
}
}
}
/// An atomic transaction /// An atomic transaction
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)] #[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct Transaction { pub struct Transaction {
@ -50,20 +62,17 @@ pub struct Transaction {
} }
impl Transaction { impl Transaction {
pub fn new( pub fn new<T: Serialize>(
from_keypair: &Keypair, from_keypair: &Keypair,
transaction_keys: &[Pubkey], transaction_keys: &[Pubkey],
program_id: Pubkey, program_id: Pubkey,
userdata: Vec<u8>, userdata: &T,
last_id: Hash, last_id: Hash,
fee: u64, fee: u64,
) -> Self { ) -> Self {
let program_ids = vec![program_id]; let program_ids = vec![program_id];
let instructions = vec![Instruction { let accounts = (0..=transaction_keys.len() as u8).collect();
program_ids_index: 0, let instructions = vec![Instruction::new(0, userdata, accounts)];
userdata,
accounts: (0..=transaction_keys.len() as u8).collect(),
}];
Self::new_with_instructions( Self::new_with_instructions(
from_keypair, from_keypair,
transaction_keys, transaction_keys,
@ -202,16 +211,8 @@ mod tests {
let prog1 = Keypair::new().pubkey(); let prog1 = Keypair::new().pubkey();
let prog2 = Keypair::new().pubkey(); let prog2 = Keypair::new().pubkey();
let instructions = vec![ let instructions = vec![
Instruction { Instruction::new(0, &(), vec![0, 1]),
program_ids_index: 0, Instruction::new(1, &(), vec![0, 2]),
userdata: vec![],
accounts: vec![0, 1],
},
Instruction {
program_ids_index: 1,
userdata: vec![],
accounts: vec![0, 2],
},
]; ];
let tx = Transaction::new_with_instructions( let tx = Transaction::new_with_instructions(
&key, &key,
@ -247,11 +248,7 @@ mod tests {
#[test] #[test]
fn test_refs_invalid_program_id() { fn test_refs_invalid_program_id() {
let key = Keypair::new(); let key = Keypair::new();
let instructions = vec![Instruction { let instructions = vec![Instruction::new(1, &(), vec![])];
program_ids_index: 1,
userdata: vec![],
accounts: vec![],
}];
let tx = Transaction::new_with_instructions( let tx = Transaction::new_with_instructions(
&key, &key,
&[], &[],
@ -265,11 +262,7 @@ mod tests {
#[test] #[test]
fn test_refs_invalid_account() { fn test_refs_invalid_account() {
let key = Keypair::new(); let key = Keypair::new();
let instructions = vec![Instruction { let instructions = vec![Instruction::new(0, &(), vec![1])];
program_ids_index: 0,
userdata: vec![],
accounts: vec![1],
}];
let tx = Transaction::new_with_instructions( let tx = Transaction::new_with_instructions(
&key, &key,
&[], &[],
@ -301,7 +294,7 @@ mod tests {
keypair, keypair,
&[keypair.pubkey(), to], &[keypair.pubkey(), to],
program_id, program_id,
vec![1, 2, 3], &(1u8, 2u8, 3u8),
Hash::default(), Hash::default(),
99, 99,
); );

View File

@ -2,7 +2,7 @@
#[cfg(test)] #[cfg(test)]
use bank::Bank; use bank::Bank;
use bincode::{deserialize, serialize}; use bincode::deserialize;
use hash::Hash; use hash::Hash;
#[cfg(test)] #[cfg(test)]
use result::Result; use result::Result;
@ -34,8 +34,14 @@ pub trait VoteTransaction {
impl VoteTransaction for Transaction { impl VoteTransaction for Transaction {
fn vote_new(vote_account: &Keypair, vote: Vote, last_id: Hash, fee: u64) -> Self { fn vote_new(vote_account: &Keypair, vote: Vote, last_id: Hash, fee: u64) -> Self {
let instruction = VoteInstruction::NewVote(vote); let instruction = VoteInstruction::NewVote(vote);
let userdata = serialize(&instruction).expect("serialize instruction"); Transaction::new(
Transaction::new(vote_account, &[], VoteProgram::id(), userdata, last_id, fee) vote_account,
&[],
VoteProgram::id(),
&instruction,
last_id,
fee,
)
} }
fn vote_account_new( fn vote_account_new(
@ -62,12 +68,11 @@ impl VoteTransaction for Transaction {
fee: u64, fee: u64,
) -> Self { ) -> Self {
let register_tx = VoteInstruction::RegisterAccount; let register_tx = VoteInstruction::RegisterAccount;
let userdata = serialize(&register_tx).unwrap();
Transaction::new( Transaction::new(
validator_id, validator_id,
&[vote_account_id], &[vote_account_id],
VoteProgram::id(), VoteProgram::id(),
userdata, &register_tx,
last_id, last_id,
fee, fee,
) )

View File

@ -4,7 +4,6 @@ extern crate serde_derive;
extern crate solana; extern crate solana;
extern crate solana_sdk; extern crate solana_sdk;
use bincode::serialize;
use solana::bank::Bank; use solana::bank::Bank;
#[cfg(feature = "bpf_c")] #[cfg(feature = "bpf_c")]
use solana::bpf_loader; use solana::bpf_loader;
@ -189,7 +188,7 @@ fn test_program_native_noop() {
&loader.mint.keypair(), &loader.mint.keypair(),
&[], &[],
program.program.pubkey(), program.program.pubkey(),
vec![1u8], &1u8,
loader.mint.last_id(), loader.mint.last_id(),
0, 0,
); );
@ -248,12 +247,11 @@ fn test_program_lua_move_funds() {
loader.bank.process_transactions(&vec![tx.clone()]), loader.bank.process_transactions(&vec![tx.clone()]),
); );
let data = serialize(&10).unwrap();
let tx = Transaction::new( let tx = Transaction::new(
&from, &from,
&[to], &[to],
program.program.pubkey(), program.program.pubkey(),
data, &10,
loader.mint.last_id(), loader.mint.last_id(),
0, 0,
); );