diff --git a/src/budget_program.rs b/src/budget_program.rs index 718c5abc43..a21eba5d45 100644 --- a/src/budget_program.rs +++ b/src/budget_program.rs @@ -292,12 +292,12 @@ mod test { ]; let from = Keypair::new(); let contract = Keypair::new(); - let userdata = vec![1, 2, 3]; + let userdata = (1u8, 2u8, 3u8); let tx = Transaction::new( &from, &[contract.pubkey()], BudgetState::id(), - userdata, + &userdata, Hash::default(), 0, ); diff --git a/src/budget_transaction.rs b/src/budget_transaction.rs index 2845119ec4..0d6e781814 100644 --- a/src/budget_transaction.rs +++ b/src/budget_transaction.rs @@ -1,6 +1,6 @@ //! The `budget_transaction` module provides functionality for creating Budget transactions. -use bincode::{deserialize, serialize}; +use bincode::deserialize; use budget_expr::{BudgetExpr, Condition}; use budget_instruction::Instruction; use budget_program::BudgetState; @@ -78,28 +78,18 @@ impl BudgetTransaction for Transaction { let keys = vec![from_keypair.pubkey(), contract]; let system_instruction = SystemProgram::Move { tokens }; - let move_userdata = serialize(&system_instruction).unwrap(); let payment = Payment { tokens: tokens - fee, to, }; let budget_instruction = Instruction::NewBudget(BudgetExpr::Pay(payment)); - let pay_userdata = serialize(&budget_instruction).unwrap(); let program_ids = vec![SystemProgram::id(), BudgetState::id()]; let instructions = vec![ - transaction::Instruction { - program_ids_index: 0, - userdata: move_userdata, - accounts: vec![0, 1], - }, - transaction::Instruction { - program_ids_index: 1, - userdata: pay_userdata, - accounts: vec![1], - }, + transaction::Instruction::new(0, &system_instruction, vec![0, 1]), + transaction::Instruction::new(1, &budget_instruction, vec![1]), ]; Self::new_with_instructions(from_keypair, &keys, last_id, fee, program_ids, instructions) @@ -119,12 +109,11 @@ impl BudgetTransaction for Transaction { last_id: Hash, ) -> Self { let instruction = Instruction::ApplyTimestamp(dt); - let userdata = serialize(&instruction).unwrap(); Self::new( from_keypair, &[contract, to], BudgetState::id(), - userdata, + &instruction, last_id, 0, ) @@ -138,12 +127,11 @@ impl BudgetTransaction for Transaction { last_id: Hash, ) -> Self { let instruction = Instruction::ApplySignature; - let userdata = serialize(&instruction).unwrap(); Self::new( from_keypair, &[contract, to], BudgetState::id(), - userdata, + &instruction, last_id, 0, ) @@ -169,12 +157,11 @@ impl BudgetTransaction for Transaction { BudgetExpr::After(Condition::Timestamp(dt, dt_pubkey), Payment { tokens, to }) }; let instruction = Instruction::NewBudget(expr); - let userdata = serialize(&instruction).expect("serialize instruction"); Self::new( from_keypair, &[contract], BudgetState::id(), - userdata, + &instruction, last_id, 0, ) @@ -198,12 +185,11 @@ impl BudgetTransaction for Transaction { BudgetExpr::After(Condition::Signature(witness), Payment { tokens, to }) }; let instruction = Instruction::NewBudget(expr); - let userdata = serialize(&instruction).expect("serialize instruction"); Self::new( from_keypair, &[contract], BudgetState::id(), - userdata, + &instruction, last_id, 0, ) @@ -270,12 +256,7 @@ mod tests { to: Default::default(), }); let instruction = Instruction::NewBudget(expr); - let userdata = serialize(&instruction).unwrap(); - let instructions = vec![transaction::Instruction { - program_ids_index: 0, - userdata, - accounts: vec![], - }]; + let instructions = vec![transaction::Instruction::new(0, &instruction, vec![])]; let claim0 = Transaction { account_keys: vec![], last_id: Default::default(), diff --git a/src/loader_transaction.rs b/src/loader_transaction.rs index ac15ef5e9c..e760900f9e 100644 --- a/src/loader_transaction.rs +++ b/src/loader_transaction.rs @@ -1,6 +1,5 @@ //! The `dynamic_transaction` module provides functionality for loading and calling a program -use bincode::serialize; use hash::Hash; use signature::{Keypair, KeypairUtil}; use solana_sdk::loader_instruction::LoaderInstruction; @@ -36,8 +35,7 @@ impl LoaderTransaction for Transaction { bytes.len() ); let instruction = LoaderInstruction::Write { offset, bytes }; - let userdata = serialize(&instruction).unwrap(); - Transaction::new(from_keypair, &[], loader, userdata, last_id, fee) + Transaction::new(from_keypair, &[], loader, &instruction, last_id, fee) } fn finalize(from_keypair: &Keypair, loader: Pubkey, last_id: Hash, fee: u64) -> Self { @@ -46,7 +44,6 @@ impl LoaderTransaction for Transaction { from_keypair.pubkey(), ); let instruction = LoaderInstruction::Finalize; - let userdata = serialize(&instruction).unwrap(); - Transaction::new(from_keypair, &[], loader, userdata, last_id, fee) + Transaction::new(from_keypair, &[], loader, &instruction, last_id, fee) } } diff --git a/src/storage_program.rs b/src/storage_program.rs index 7c2050d635..dd18914276 100644 --- a/src/storage_program.rs +++ b/src/storage_program.rs @@ -66,7 +66,7 @@ mod test { &keypair, &[], StorageProgram::id(), - vec![], + &(), Default::default(), 0, ); diff --git a/src/storage_transaction.rs b/src/storage_transaction.rs index 19463154aa..b99a7f388b 100644 --- a/src/storage_transaction.rs +++ b/src/storage_transaction.rs @@ -1,4 +1,3 @@ -use bincode::serialize; use hash::Hash; use signature::{Keypair, KeypairUtil}; use storage_program::StorageProgram; @@ -11,12 +10,11 @@ pub trait StorageTransaction { impl StorageTransaction for Transaction { fn storage_new_mining_proof(from_keypair: &Keypair, sha_state: Hash, last_id: Hash) -> Self { let program = StorageProgram::SubmitMiningProof { sha_state }; - let userdata = serialize(&program).unwrap(); Transaction::new( from_keypair, &[from_keypair.pubkey()], StorageProgram::id(), - userdata, + &program, last_id, 0, ) diff --git a/src/system_transaction.rs b/src/system_transaction.rs index 9d7338f1aa..fdb3ec3a6d 100644 --- a/src/system_transaction.rs +++ b/src/system_transaction.rs @@ -1,6 +1,5 @@ //! The `system_transaction` module provides functionality for creating system transactions. -use bincode::serialize; use hash::Hash; use signature::{Keypair, KeypairUtil}; use solana_sdk::pubkey::Pubkey; @@ -56,12 +55,11 @@ impl SystemTransaction for Transaction { space, program_id, }; - let userdata = serialize(&create).unwrap(); Transaction::new( from_keypair, &[to], SystemProgram::id(), - userdata, + &create, last_id, fee, ) @@ -69,12 +67,11 @@ impl SystemTransaction for Transaction { /// Create and sign new SystemProgram::Assign transaction fn system_assign(from_keypair: &Keypair, last_id: Hash, program_id: Pubkey, fee: u64) -> Self { let assign = SystemProgram::Assign { program_id }; - let userdata = serialize(&assign).unwrap(); Transaction::new( from_keypair, &[], SystemProgram::id(), - userdata, + &assign, last_id, fee, ) @@ -92,12 +89,11 @@ impl SystemTransaction for Transaction { fee: u64, ) -> Self { let move_tokens = SystemProgram::Move { tokens }; - let userdata = serialize(&move_tokens).unwrap(); Transaction::new( from_keypair, &[to], SystemProgram::id(), - userdata, + &move_tokens, last_id, fee, ) @@ -109,11 +105,7 @@ impl SystemTransaction for Transaction { .enumerate() .map(|(i, (_, amount))| { let spend = SystemProgram::Move { tokens: *amount }; - Instruction { - program_ids_index: 0, - userdata: serialize(&spend).unwrap(), - accounts: vec![0, i as u8 + 1], - } + Instruction::new(0, &spend, vec![0, i as u8 + 1]) }).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 fn system_spawn(from_keypair: &Keypair, last_id: Hash, fee: u64) -> Self { let spawn = SystemProgram::Spawn; - let userdata = serialize(&spawn).unwrap(); - Transaction::new( - from_keypair, - &[], - SystemProgram::id(), - userdata, - last_id, - fee, - ) + Transaction::new(from_keypair, &[], SystemProgram::id(), &spawn, last_id, fee) } } diff --git a/src/transaction.rs b/src/transaction.rs index aedd547fc6..beda8a40b1 100644 --- a/src/transaction.rs +++ b/src/transaction.rs @@ -2,6 +2,7 @@ use bincode::serialize; use hash::{Hash, Hasher}; +use serde::Serialize; use signature::{Keypair, KeypairUtil, Signature}; use solana_sdk::pubkey::Pubkey; use std::mem::size_of; @@ -23,6 +24,17 @@ pub struct Instruction { pub userdata: Vec, } +impl Instruction { + pub fn new(program_ids_index: u8, userdata: &T, accounts: Vec) -> Self { + let userdata = serialize(userdata).unwrap(); + Instruction { + program_ids_index, + userdata, + accounts, + } + } +} + /// An atomic transaction #[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)] pub struct Transaction { @@ -50,20 +62,17 @@ pub struct Transaction { } impl Transaction { - pub fn new( + pub fn new( from_keypair: &Keypair, transaction_keys: &[Pubkey], program_id: Pubkey, - userdata: Vec, + userdata: &T, last_id: Hash, fee: u64, ) -> Self { let program_ids = vec![program_id]; - let instructions = vec![Instruction { - program_ids_index: 0, - userdata, - accounts: (0..=transaction_keys.len() as u8).collect(), - }]; + let accounts = (0..=transaction_keys.len() as u8).collect(); + let instructions = vec![Instruction::new(0, userdata, accounts)]; Self::new_with_instructions( from_keypair, transaction_keys, @@ -202,16 +211,8 @@ mod tests { let prog1 = Keypair::new().pubkey(); let prog2 = Keypair::new().pubkey(); let instructions = vec![ - Instruction { - program_ids_index: 0, - userdata: vec![], - accounts: vec![0, 1], - }, - Instruction { - program_ids_index: 1, - userdata: vec![], - accounts: vec![0, 2], - }, + Instruction::new(0, &(), vec![0, 1]), + Instruction::new(1, &(), vec![0, 2]), ]; let tx = Transaction::new_with_instructions( &key, @@ -247,11 +248,7 @@ mod tests { #[test] fn test_refs_invalid_program_id() { let key = Keypair::new(); - let instructions = vec![Instruction { - program_ids_index: 1, - userdata: vec![], - accounts: vec![], - }]; + let instructions = vec![Instruction::new(1, &(), vec![])]; let tx = Transaction::new_with_instructions( &key, &[], @@ -265,11 +262,7 @@ mod tests { #[test] fn test_refs_invalid_account() { let key = Keypair::new(); - let instructions = vec![Instruction { - program_ids_index: 0, - userdata: vec![], - accounts: vec![1], - }]; + let instructions = vec![Instruction::new(0, &(), vec![1])]; let tx = Transaction::new_with_instructions( &key, &[], @@ -301,7 +294,7 @@ mod tests { keypair, &[keypair.pubkey(), to], program_id, - vec![1, 2, 3], + &(1u8, 2u8, 3u8), Hash::default(), 99, ); diff --git a/src/vote_transaction.rs b/src/vote_transaction.rs index 07c8013427..1bfcf33a5f 100644 --- a/src/vote_transaction.rs +++ b/src/vote_transaction.rs @@ -2,7 +2,7 @@ #[cfg(test)] use bank::Bank; -use bincode::{deserialize, serialize}; +use bincode::deserialize; use hash::Hash; #[cfg(test)] use result::Result; @@ -34,8 +34,14 @@ pub trait VoteTransaction { impl VoteTransaction for Transaction { fn vote_new(vote_account: &Keypair, vote: Vote, last_id: Hash, fee: u64) -> Self { let instruction = VoteInstruction::NewVote(vote); - let userdata = serialize(&instruction).expect("serialize instruction"); - Transaction::new(vote_account, &[], VoteProgram::id(), userdata, last_id, fee) + Transaction::new( + vote_account, + &[], + VoteProgram::id(), + &instruction, + last_id, + fee, + ) } fn vote_account_new( @@ -62,12 +68,11 @@ impl VoteTransaction for Transaction { fee: u64, ) -> Self { let register_tx = VoteInstruction::RegisterAccount; - let userdata = serialize(®ister_tx).unwrap(); Transaction::new( validator_id, &[vote_account_id], VoteProgram::id(), - userdata, + ®ister_tx, last_id, fee, ) diff --git a/tests/programs.rs b/tests/programs.rs index 6c0e28b44b..246b596e33 100644 --- a/tests/programs.rs +++ b/tests/programs.rs @@ -4,7 +4,6 @@ extern crate serde_derive; extern crate solana; extern crate solana_sdk; -use bincode::serialize; use solana::bank::Bank; #[cfg(feature = "bpf_c")] use solana::bpf_loader; @@ -189,7 +188,7 @@ fn test_program_native_noop() { &loader.mint.keypair(), &[], program.program.pubkey(), - vec![1u8], + &1u8, loader.mint.last_id(), 0, ); @@ -248,12 +247,11 @@ fn test_program_lua_move_funds() { loader.bank.process_transactions(&vec![tx.clone()]), ); - let data = serialize(&10).unwrap(); let tx = Transaction::new( &from, &[to], program.program.pubkey(), - data, + &10, loader.mint.last_id(), 0, );