From 85e7046cafe1bc2a246aff1d96400c6e6cc7afc3 Mon Sep 17 00:00:00 2001 From: Greg Fitzgerald Date: Tue, 29 Jan 2019 17:16:59 -0700 Subject: [PATCH] Use signer for signing transactions, not constructing them --- bench-tps/src/bench.rs | 2 +- sdk/src/vote_transaction.rs | 18 ++++++++++++++---- src/compute_leader_confirmation_service.rs | 6 ++++-- src/entry.rs | 22 ++-------------------- src/leader_scheduler.rs | 15 +++------------ src/storage_stage.rs | 11 +---------- src/vote_signer_proxy.rs | 10 +--------- 7 files changed, 26 insertions(+), 58 deletions(-) diff --git a/bench-tps/src/bench.rs b/bench-tps/src/bench.rs index 0842fa02c..e9c60a888 100644 --- a/bench-tps/src/bench.rs +++ b/bench-tps/src/bench.rs @@ -370,7 +370,7 @@ pub fn fund_keys(client: &mut ThinClient, source: &Keypair, dests: &[Keypair], t // re-sign retained to_fund_txes with updated last_id to_fund_txs.par_iter_mut().for_each(|(k, tx)| { - tx.sign(&[k], last_id); + tx.sign(&[*k], last_id); }); to_fund_txs.iter().for_each(|(_, tx)| { diff --git a/sdk/src/vote_transaction.rs b/sdk/src/vote_transaction.rs index 906bd5c15..f3925417a 100644 --- a/sdk/src/vote_transaction.rs +++ b/sdk/src/vote_transaction.rs @@ -2,7 +2,7 @@ use crate::hash::Hash; use crate::pubkey::Pubkey; -use crate::signature::Keypair; +use crate::signature::{Keypair, KeypairUtil}; use crate::system_instruction::SystemInstruction; use crate::system_program; use crate::transaction::{Instruction, Transaction}; @@ -10,7 +10,12 @@ use crate::vote_program::{self, Vote, VoteInstruction}; use bincode::deserialize; pub trait VoteTransaction { - fn vote_new(vote_account: &Pubkey, tick_height: u64, last_id: Hash, fee: u64) -> Self; + fn vote_new( + vote_account: &T, + tick_height: u64, + last_id: Hash, + fee: u64, + ) -> Self; fn vote_account_new( validator_id: &Keypair, vote_account_id: Pubkey, @@ -23,10 +28,15 @@ pub trait VoteTransaction { } impl VoteTransaction for Transaction { - fn vote_new(vote_account: &Pubkey, tick_height: u64, last_id: Hash, fee: u64) -> Self { + fn vote_new( + vote_account: &T, + tick_height: u64, + last_id: Hash, + fee: u64, + ) -> Self { let vote = Vote { tick_height }; let instruction = VoteInstruction::NewVote(vote); - Transaction::new_unsigned( + Transaction::new( vote_account, &[], vote_program::id(), diff --git a/src/compute_leader_confirmation_service.rs b/src/compute_leader_confirmation_service.rs index 35827f15f..872e74054 100644 --- a/src/compute_leader_confirmation_service.rs +++ b/src/compute_leader_confirmation_service.rs @@ -165,6 +165,8 @@ pub mod tests { use bincode::serialize; use solana_sdk::hash::hash; use solana_sdk::signature::{Keypair, KeypairUtil}; + use solana_sdk::transaction::Transaction; + use solana_sdk::vote_transaction::VoteTransaction; use std::sync::Arc; use std::thread::sleep; use std::time::Duration; @@ -204,7 +206,7 @@ pub mod tests { .expect("Expected successful creation of account"); if i < 6 { - let vote_tx = vote_signer.new_signed_vote_transaction(&last_id, (i + 1) as u64); + let vote_tx = Transaction::vote_new(&vote_signer, (i + 1) as u64, last_id, 0); bank.process_transaction(&vote_tx).unwrap(); } (vote_signer, validator_keypair) @@ -222,7 +224,7 @@ pub mod tests { // Get another validator to vote, so we now have 2/3 consensus let vote_signer = &vote_accounts[7].0; - let vote_tx = vote_signer.new_signed_vote_transaction(&ids[6], 7); + let vote_tx = Transaction::vote_new(vote_signer, 7, ids[6], 0); bank.process_transaction(&vote_tx).unwrap(); ComputeLeaderConfirmationService::compute_confirmation( diff --git a/src/entry.rs b/src/entry.rs index c438a9eb6..7ae5682f0 100644 --- a/src/entry.rs +++ b/src/entry.rs @@ -607,16 +607,7 @@ mod tests { let one = hash(&zero.as_ref()); let keypair = Keypair::new(); let vote_account = Keypair::new(); - let tx = Transaction::vote_new(&vote_account.pubkey(), 1, one, 1); - let sig = vote_account.sign_message(&tx.message()); - let tx0 = Transaction { - signatures: vec![sig], - account_keys: tx.account_keys, - last_id: tx.last_id, - fee: tx.fee, - program_ids: tx.program_ids, - instructions: tx.instructions, - }; + let tx0 = Transaction::vote_new(&vote_account, 1, one, 1); let tx1 = Transaction::budget_new_timestamp( &keypair, keypair.pubkey(), @@ -664,16 +655,7 @@ mod tests { let next_id = hash(&id.as_ref()); let keypair = Keypair::new(); let vote_account = Keypair::new(); - let tx = Transaction::vote_new(&vote_account.pubkey(), 1, next_id, 2); - let sig = vote_account.sign_message(&tx.message()); - let tx_small = Transaction { - signatures: vec![sig], - account_keys: tx.account_keys, - last_id: tx.last_id, - fee: tx.fee, - program_ids: tx.program_ids, - instructions: tx.instructions, - }; + let tx_small = Transaction::vote_new(&vote_account, 1, next_id, 2); let tx_large = Transaction::budget_new(&keypair, keypair.pubkey(), 1, next_id); let tx_small_size = tx_small.serialized_size().unwrap() as usize; diff --git a/src/leader_scheduler.rs b/src/leader_scheduler.rs index d95ea9872..ef174b026 100644 --- a/src/leader_scheduler.rs +++ b/src/leader_scheduler.rs @@ -499,16 +499,7 @@ pub fn make_active_set_entries( last_entry_id = new_vote_account_entry.id; // 3) Create vote entry - let tx = Transaction::vote_new(&vote_account_id, 1, *last_tick_id, 0); - let sig = active_keypair.sign_message(&tx.message()); - let vote_tx = Transaction { - signatures: vec![sig], - account_keys: tx.account_keys, - last_id: tx.last_id, - fee: tx.fee, - program_ids: tx.program_ids, - instructions: tx.instructions, - }; + let vote_tx = Transaction::vote_new(&vote_signer, 1, *last_tick_id, 0); let vote_entry = Entry::new(&last_entry_id, 0, 1, vec![vote_tx]); last_entry_id = vote_entry.id; @@ -521,6 +512,7 @@ pub fn make_active_set_entries( #[cfg(test)] mod tests { + use super::*; use crate::bank::Bank; use crate::genesis_block::GenesisBlock; use crate::leader_scheduler::{ @@ -544,8 +536,7 @@ mod tests { } fn push_vote(vote_signer: &VoteSignerProxy, bank: &Bank, height: u64, last_id: Hash) { - let new_vote_tx = vote_signer.new_signed_vote_transaction(&last_id, height); - + let new_vote_tx = Transaction::vote_new(vote_signer, height, last_id, 0); bank.process_transaction(&new_vote_tx).unwrap(); } diff --git a/src/storage_stage.rs b/src/storage_stage.rs index 65f5d2039..5d7e2533f 100644 --- a/src/storage_stage.rs +++ b/src/storage_stage.rs @@ -604,16 +604,7 @@ mod tests { } let mut vote_txs: Vec = Vec::new(); let keypair = Keypair::new(); - let tx = Transaction::vote_new(&keypair.pubkey(), 123456, Hash::default(), 1); - let sig = keypair.sign_message(&tx.message()); - let vote_tx = Transaction { - signatures: vec![sig], - account_keys: tx.account_keys, - last_id: tx.last_id, - fee: tx.fee, - program_ids: tx.program_ids, - instructions: tx.instructions, - }; + let vote_tx = Transaction::vote_new(&keypair, 123456, Hash::default(), 1); vote_txs.push(vote_tx); let vote_entries = vec![Entry::new(&Hash::default(), 0, 1, vote_txs)]; storage_entry_sender.send(vote_entries).unwrap(); diff --git a/src/vote_signer_proxy.rs b/src/vote_signer_proxy.rs index 510344512..e3a626e08 100644 --- a/src/vote_signer_proxy.rs +++ b/src/vote_signer_proxy.rs @@ -148,7 +148,7 @@ impl VoteSignerProxy { ); } - let tx = self.new_signed_vote_transaction(&bank.last_id(), bank.tick_height()); + let tx = Transaction::vote_new(self, bank.tick_height(), bank.last_id(), 0); match VoteSignerProxy::get_leader_tpu(&bank, cluster_info) { Ok(tpu) => { @@ -174,14 +174,6 @@ impl VoteSignerProxy { Ok(()) } - pub fn new_signed_vote_transaction(&self, last_id: &Hash, tick_height: u64) -> Transaction { - let mut tx = Transaction::vote_new(&self.vote_account, tick_height, *last_id, 0); - assert!(tx.signatures.is_empty()); - let sig = self.sign_message(&tx.message()); - tx.signatures.push(sig); - tx - } - fn new_signed_vote_blob(&self, tx: &Transaction, leader_tpu: SocketAddr) -> Result { let shared_blob = SharedBlob::default(); {