From c741a960b99dada0130d91e6c720a893bbc1f8f9 Mon Sep 17 00:00:00 2001 From: Greg Fitzgerald Date: Tue, 29 Jan 2019 16:57:48 -0700 Subject: [PATCH] Generalize Transaction::new to accept anything that implements KeypairUtil --- sdk/src/transaction.rs | 21 ++++++++++++++------- sdk/src/vote_transaction.rs | 5 +++-- src/accounts.rs | 2 +- src/entry.rs | 4 ++-- src/leader_scheduler.rs | 5 ++--- src/storage_stage.rs | 8 ++------ src/thin_client.rs | 2 +- src/vote_signer_proxy.rs | 4 +--- 8 files changed, 26 insertions(+), 25 deletions(-) diff --git a/sdk/src/transaction.rs b/sdk/src/transaction.rs index 6e13c40739..74f65c328a 100644 --- a/sdk/src/transaction.rs +++ b/sdk/src/transaction.rs @@ -97,11 +97,11 @@ pub struct Transaction { } impl Transaction { - pub fn new( - from_keypair: &Keypair, + pub fn new( + from_keypair: &T, transaction_keys: &[Pubkey], program_id: Pubkey, - userdata: &T, + userdata: &S, last_id: Hash, fee: u64, ) -> Self { @@ -130,7 +130,14 @@ impl Transaction { let instructions = vec![Instruction::new(0, userdata, accounts)]; let mut keys = vec![*from_pubkey]; keys.extend_from_slice(transaction_keys); - Self::new_with_instructions(&[], &keys[..], last_id, fee, program_ids, instructions) + Self::new_with_instructions::( + &[], + &keys[..], + last_id, + fee, + program_ids, + instructions, + ) } /// Create a signed transaction /// * `from_keypair` - The key used to sign the transaction. This key is stored as keys[0] @@ -140,8 +147,8 @@ impl Transaction { /// * `fee` - The transaction fee. /// * `program_ids` - The keys that identify programs used in the `instruction` vector. /// * `instructions` - The programs and their arguments that the transaction will execute atomically - pub fn new_with_instructions( - from_keypairs: &[&Keypair], + pub fn new_with_instructions( + from_keypairs: &[&T], keys: &[Pubkey], last_id: Hash, fee: u64, @@ -211,7 +218,7 @@ impl Transaction { } /// Sign this transaction. - pub fn sign(&mut self, keypairs: &[&Keypair], last_id: Hash) { + pub fn sign(&mut self, keypairs: &[&T], last_id: Hash) { self.last_id = last_id; let message = self.message(); self.signatures = keypairs diff --git a/sdk/src/vote_transaction.rs b/sdk/src/vote_transaction.rs index 57db2fe6ac..906bd5c154 100644 --- a/sdk/src/vote_transaction.rs +++ b/sdk/src/vote_transaction.rs @@ -10,7 +10,7 @@ use crate::vote_program::{self, Vote, VoteInstruction}; use bincode::deserialize; pub trait VoteTransaction { - fn vote_new(vote_account: &Pubkey, vote: Vote, last_id: Hash, fee: u64) -> Self; + fn vote_new(vote_account: &Pubkey, tick_height: u64, last_id: Hash, fee: u64) -> Self; fn vote_account_new( validator_id: &Keypair, vote_account_id: Pubkey, @@ -23,7 +23,8 @@ pub trait VoteTransaction { } impl VoteTransaction for Transaction { - fn vote_new(vote_account: &Pubkey, vote: Vote, last_id: Hash, fee: u64) -> Self { + fn vote_new(vote_account: &Pubkey, tick_height: u64, last_id: Hash, fee: u64) -> Self { + let vote = Vote { tick_height }; let instruction = VoteInstruction::NewVote(vote); Transaction::new_unsigned( vote_account, diff --git a/src/accounts.rs b/src/accounts.rs index 3a962bb376..70baaf2768 100644 --- a/src/accounts.rs +++ b/src/accounts.rs @@ -457,7 +457,7 @@ mod tests { let mut error_counters = ErrorCounters::default(); let instructions = vec![Instruction::new(1, &(), vec![0])]; - let tx = Transaction::new_with_instructions( + let tx = Transaction::new_with_instructions::( &[], &[], Hash::default(), diff --git a/src/entry.rs b/src/entry.rs index 318dfccbd6..c438a9eb63 100644 --- a/src/entry.rs +++ b/src/entry.rs @@ -607,7 +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(), Vote { tick_height: 1 }, one, 1); + 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], @@ -664,7 +664,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(), Vote { tick_height: 1 }, next_id, 2); + 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], diff --git a/src/leader_scheduler.rs b/src/leader_scheduler.rs index 2b525df10c..d95ea9872e 100644 --- a/src/leader_scheduler.rs +++ b/src/leader_scheduler.rs @@ -13,7 +13,7 @@ use solana_sdk::pubkey::Pubkey; use solana_sdk::signature::{Keypair, KeypairUtil}; use solana_sdk::system_transaction::SystemTransaction; use solana_sdk::transaction::Transaction; -use solana_sdk::vote_program::{self, Vote, VoteProgram}; +use solana_sdk::vote_program::{self, VoteProgram}; use solana_sdk::vote_transaction::VoteTransaction; use std::io::Cursor; use std::sync::Arc; @@ -499,8 +499,7 @@ pub fn make_active_set_entries( last_entry_id = new_vote_account_entry.id; // 3) Create vote entry - let vote = Vote { tick_height: 1 }; - let tx = Transaction::vote_new(&vote_account_id, vote, *last_tick_id, 0); + 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], diff --git a/src/storage_stage.rs b/src/storage_stage.rs index f38acc3522..65f5d20398 100644 --- a/src/storage_stage.rs +++ b/src/storage_stage.rs @@ -242,7 +242,7 @@ impl StorageStage { let last_id = client.get_last_id(); - tx.sign(&[&keypair], last_id); + tx.sign(&[keypair.as_ref()], last_id); if exit.load(Ordering::Relaxed) { Err(io::Error::new(io::ErrorKind::Other, "exit signaled"))?; @@ -454,7 +454,6 @@ mod tests { use solana_sdk::pubkey::Pubkey; use solana_sdk::signature::{Keypair, KeypairUtil}; use solana_sdk::transaction::Transaction; - use solana_sdk::vote_program::Vote; use solana_sdk::vote_transaction::VoteTransaction; use std::cmp::{max, min}; use std::fs::remove_dir_all; @@ -604,11 +603,8 @@ mod tests { reference_keys.copy_from_slice(keys); } let mut vote_txs: Vec = Vec::new(); - let vote = Vote { - tick_height: 123456, - }; let keypair = Keypair::new(); - let tx = Transaction::vote_new(&keypair.pubkey(), vote, Hash::default(), 1); + 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], diff --git a/src/thin_client.rs b/src/thin_client.rs index d0427fa0d2..a8c86c9172 100644 --- a/src/thin_client.rs +++ b/src/thin_client.rs @@ -112,7 +112,7 @@ impl ThinClient { tries: usize, ) -> io::Result { for x in 0..tries { - tx.sign(&[&keypair], self.get_last_id()); + tx.sign(&[keypair], self.get_last_id()); let mut buf = vec![0; tx.serialized_size().unwrap() as usize]; let mut wr = std::io::Cursor::new(&mut buf[..]); serialize_into(&mut wr, &tx).expect("serialize Transaction in pub fn transfer_signed"); diff --git a/src/vote_signer_proxy.rs b/src/vote_signer_proxy.rs index ea97dd22e3..5103445120 100644 --- a/src/vote_signer_proxy.rs +++ b/src/vote_signer_proxy.rs @@ -14,7 +14,6 @@ use solana_sdk::hash::Hash; use solana_sdk::pubkey::Pubkey; use solana_sdk::signature::{Keypair, KeypairUtil, Signature}; use solana_sdk::transaction::Transaction; -use solana_sdk::vote_program::Vote; use solana_sdk::vote_transaction::VoteTransaction; use solana_vote_signer::rpc::LocalVoteSigner; use solana_vote_signer::rpc::VoteSigner; @@ -176,8 +175,7 @@ impl VoteSignerProxy { } pub fn new_signed_vote_transaction(&self, last_id: &Hash, tick_height: u64) -> Transaction { - let vote = Vote { tick_height }; - let mut tx = Transaction::vote_new(&self.vote_account, vote, *last_id, 0); + 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);