From aca739b8007dabfc2392977755950ad76ea1f925 Mon Sep 17 00:00:00 2001 From: Greg Fitzgerald Date: Fri, 15 Mar 2019 10:30:50 -0600 Subject: [PATCH] Boot fees from TransactionBuilder --- programs/budget_api/src/budget_transaction.rs | 28 +++------- .../rewards_api/src/rewards_transaction.rs | 10 ++-- programs/vote_api/src/vote_transaction.rs | 56 +++++++------------ runtime/tests/system.rs | 4 +- sdk/src/transaction.rs | 3 +- sdk/src/transaction_builder.rs | 47 +--------------- wallet/src/wallet.rs | 2 +- 7 files changed, 41 insertions(+), 109 deletions(-) diff --git a/programs/budget_api/src/budget_transaction.rs b/programs/budget_api/src/budget_transaction.rs index 7449d4f9df..e103eb1583 100644 --- a/programs/budget_api/src/budget_transaction.rs +++ b/programs/budget_api/src/budget_transaction.rs @@ -28,17 +28,11 @@ impl BudgetTransaction { ) -> Transaction { let from = from_keypair.pubkey(); let space = serialized_size(&BudgetState::new(expr.clone())).unwrap(); - let mut tx = TransactionBuilder::new(fee) - .push(SystemInstruction::new_program_account( - &from, - contract, - lamports, - space, - &id(), - )) - .push(BudgetInstruction::new_initialize_account(contract, expr)) - .compile(); - + let create_ix = + SystemInstruction::new_program_account(&from, contract, lamports, space, &id()); + let init_ix = BudgetInstruction::new_initialize_account(contract, expr); + let mut tx = TransactionBuilder::new(vec![create_ix, init_ix]).compile(); + tx.fee = fee; tx.sign(&[from_keypair], recent_blockhash); tx } @@ -72,11 +66,8 @@ impl BudgetTransaction { recent_blockhash: Hash, ) -> Transaction { let from = from_keypair.pubkey(); - let mut tx = TransactionBuilder::default() - .push(BudgetInstruction::new_apply_timestamp( - &from, contract, to, dt, - )) - .compile(); + let ix = BudgetInstruction::new_apply_timestamp(&from, contract, to, dt); + let mut tx = TransactionBuilder::new(vec![ix]).compile(); tx.sign(&[from_keypair], recent_blockhash); tx } @@ -89,9 +80,8 @@ impl BudgetTransaction { recent_blockhash: Hash, ) -> Transaction { let from = from_keypair.pubkey(); - let mut tx = TransactionBuilder::default() - .push(BudgetInstruction::new_apply_signature(&from, contract, to)) - .compile(); + let ix = BudgetInstruction::new_apply_signature(&from, contract, to); + let mut tx = TransactionBuilder::new(vec![ix]).compile(); tx.sign(&[from_keypair], recent_blockhash); tx } diff --git a/programs/rewards_api/src/rewards_transaction.rs b/programs/rewards_api/src/rewards_transaction.rs index a49f21fa5a..0cc817b052 100644 --- a/programs/rewards_api/src/rewards_transaction.rs +++ b/programs/rewards_api/src/rewards_transaction.rs @@ -40,12 +40,10 @@ impl RewardsTransaction { fee: u64, ) -> Transaction { let vote_id = vote_keypair.pubkey(); - let mut tx = TransactionBuilder::new(fee) - .push(RewardsInstruction::new_redeem_vote_credits( - &vote_id, rewards_id, - )) - .push(VoteInstruction::new_clear_credits(&vote_id)) - .compile(); + let redeem_ix = RewardsInstruction::new_redeem_vote_credits(&vote_id, rewards_id); + let clear_ix = VoteInstruction::new_clear_credits(&vote_id); + let mut tx = TransactionBuilder::new(vec![redeem_ix, clear_ix]).compile(); + tx.fee = fee; tx.sign(&[vote_keypair], blockhash); tx } diff --git a/programs/vote_api/src/vote_transaction.rs b/programs/vote_api/src/vote_transaction.rs index 6ec8277eeb..fc6712beec 100644 --- a/programs/vote_api/src/vote_transaction.rs +++ b/programs/vote_api/src/vote_transaction.rs @@ -22,9 +22,9 @@ impl VoteTransaction { fee: u64, ) -> Transaction { let vote = Vote { slot }; - let mut tx = TransactionBuilder::new(fee) - .push(VoteInstruction::new_vote(staking_account, vote)) - .compile(); + let ix = VoteInstruction::new_vote(staking_account, vote); + let mut tx = TransactionBuilder::new(vec![ix]).compile(); + tx.fee = fee; tx.sign(&[authorized_voter_keypair], recent_blockhash); tx } @@ -39,16 +39,11 @@ impl VoteTransaction { ) -> Transaction { let from_id = from_keypair.pubkey(); let space = VoteState::max_size() as u64; - let mut tx = TransactionBuilder::new(fee) - .push(SystemInstruction::new_program_account( - &from_id, - staker_id, - lamports, - space, - &id(), - )) - .push(VoteInstruction::new_initialize_account(staker_id)) - .compile(); + let create_ix = + SystemInstruction::new_program_account(&from_id, staker_id, lamports, space, &id()); + let init_ix = VoteInstruction::new_initialize_account(staker_id); + let mut tx = TransactionBuilder::new(vec![create_ix, init_ix]).compile(); + tx.fee = fee; tx.sign(&[from_keypair], recent_blockhash); tx } @@ -65,17 +60,12 @@ impl VoteTransaction { let from_id = from_keypair.pubkey(); let voter_id = voter_keypair.pubkey(); let space = VoteState::max_size() as u64; - let mut tx = TransactionBuilder::new(fee) - .push(SystemInstruction::new_program_account( - &from_id, - &voter_id, - lamports, - space, - &id(), - )) - .push(VoteInstruction::new_initialize_account(&voter_id)) - .push(VoteInstruction::new_delegate_stake(&voter_id, &delegate_id)) - .compile(); + let create_ix = + SystemInstruction::new_program_account(&from_id, &voter_id, lamports, space, &id()); + let init_ix = VoteInstruction::new_initialize_account(&voter_id); + let delegate_ix = VoteInstruction::new_delegate_stake(&voter_id, &delegate_id); + let mut tx = TransactionBuilder::new(vec![create_ix, init_ix, delegate_ix]).compile(); + tx.fee = fee; tx.sign(&[from_keypair, voter_keypair], recent_blockhash); tx } @@ -87,12 +77,9 @@ impl VoteTransaction { authorized_voter_id: &Pubkey, fee: u64, ) -> Transaction { - let mut tx = TransactionBuilder::new(fee) - .push(VoteInstruction::new_authorize_voter( - &vote_keypair.pubkey(), - authorized_voter_id, - )) - .compile(); + let ix = VoteInstruction::new_authorize_voter(&vote_keypair.pubkey(), authorized_voter_id); + let mut tx = TransactionBuilder::new(vec![ix]).compile(); + tx.fee = fee; tx.sign(&[vote_keypair], recent_blockhash); tx } @@ -104,12 +91,9 @@ impl VoteTransaction { node_id: &Pubkey, fee: u64, ) -> Transaction { - let mut tx = TransactionBuilder::new(fee) - .push(VoteInstruction::new_delegate_stake( - &vote_keypair.pubkey(), - node_id, - )) - .compile(); + let ix = VoteInstruction::new_delegate_stake(&vote_keypair.pubkey(), node_id); + let mut tx = TransactionBuilder::new(vec![ix]).compile(); + tx.fee = fee; tx.sign(&[vote_keypair], recent_blockhash); tx } diff --git a/runtime/tests/system.rs b/runtime/tests/system.rs index 5d58cd684a..f839d9eacd 100644 --- a/runtime/tests/system.rs +++ b/runtime/tests/system.rs @@ -21,7 +21,7 @@ fn test_system_unsigned_transaction() { // Fund to account to bypass AccountNotFound error let ix = SystemInstruction::new_move(&from_pubkey, &to_pubkey, 50); - let mut tx = TransactionBuilder::new_with_instruction(ix); + let mut tx = TransactionBuilder::new(vec![ix]).compile(); alice_client.process_transaction(&mut tx).unwrap(); // Erroneously sign transaction with recipient account key @@ -31,7 +31,7 @@ fn test_system_unsigned_transaction() { &SystemInstruction::Move { lamports: 10 }, vec![(from_pubkey, false), (to_pubkey, true)], ); - let mut tx = TransactionBuilder::new_with_instruction(ix); + let mut tx = TransactionBuilder::new(vec![ix]).compile(); assert_eq!( mallory_client.process_transaction(&mut tx), Err(TransactionError::InstructionError( diff --git a/sdk/src/transaction.rs b/sdk/src/transaction.rs index 53d851c197..23d2135739 100644 --- a/sdk/src/transaction.rs +++ b/sdk/src/transaction.rs @@ -199,7 +199,8 @@ impl Transaction { account_keys.push((*pubkey, false)); } let instruction = Instruction::new(*program_id, data, account_keys); - let mut transaction = TransactionBuilder::new(fee).push(instruction).compile(); + let mut transaction = TransactionBuilder::new(vec![instruction]).compile(); + transaction.fee = fee; transaction.recent_blockhash = recent_blockhash; transaction } diff --git a/sdk/src/transaction_builder.rs b/sdk/src/transaction_builder.rs index 5d30642f79..81054dc93f 100644 --- a/sdk/src/transaction_builder.rs +++ b/sdk/src/transaction_builder.rs @@ -35,28 +35,13 @@ fn compile_instructions( /// A utility for constructing transactions #[derive(Default)] pub struct TransactionBuilder { - fee: u64, instructions: Vec, } impl TransactionBuilder { - /// Create a new TransactionBuilder. - pub fn new(fee: u64) -> Self { - Self { - fee, - instructions: vec![], - } - } - /// Create a new unsigned transaction from a single instruction - pub fn new_with_instruction(instruction: Instruction) -> Transaction { - Self::new_with_instructions(vec![instruction]) - } - - /// Create a new unsigned transaction from a single instruction - pub fn new_with_instructions(instructions: Vec) -> Transaction { - let fee = 0; - Self { fee, instructions }.compile() + pub fn new(instructions: Vec) -> Self { + Self { instructions } } /// Add an instruction. @@ -107,7 +92,7 @@ impl TransactionBuilder { signatures: Vec::with_capacity(signed_len), account_keys: signed_keys, recent_blockhash: Hash::default(), - fee: self.fee, + fee: 0, program_ids, instructions, } @@ -228,11 +213,6 @@ mod tests { assert_eq!(tx.signatures.capacity(), 1); } - #[test] - fn test_transaction_builder_fee() { - assert_eq!(TransactionBuilder::new(42).compile().fee, 42); - } - #[test] fn test_transaction_builder_kitchen_sink() { let program_id0 = Pubkey::default(); @@ -249,25 +229,4 @@ mod tests { assert_eq!(tx.instructions[1], CompiledInstruction::new(1, &0, vec![0])); assert_eq!(tx.instructions[2], CompiledInstruction::new(0, &0, vec![0])); } - - #[test] - fn test_transaction_builder_new_with_instruction() { - let ix = Instruction::new(Pubkey::default(), &0, vec![]); - assert_eq!( - TransactionBuilder::new_with_instruction(ix.clone()), - TransactionBuilder::default().push(ix.clone()).compile() - ); - } - - #[test] - fn test_transaction_builder_new_with_instructions() { - let ix = Instruction::new(Pubkey::default(), &0, vec![]); - assert_eq!( - TransactionBuilder::new_with_instructions(vec![ix.clone(), ix.clone()]), - TransactionBuilder::default() - .push(ix.clone()) - .push(ix.clone()) - .compile() - ); - } } diff --git a/wallet/src/wallet.rs b/wallet/src/wallet.rs index e4ef388254..dd3900b20b 100644 --- a/wallet/src/wallet.rs +++ b/wallet/src/wallet.rs @@ -442,7 +442,7 @@ fn process_configure_staking( authorized_voter_option: Option, ) -> ProcessResult { let recent_blockhash = get_recent_blockhash(&rpc_client)?; - let mut tx = TransactionBuilder::new(0); + let mut tx = TransactionBuilder::default(); if let Some(delegate_id) = delegate_option { tx.push(VoteInstruction::new_delegate_stake( &config.id.pubkey(),