Generalize Transaction::new to accept anything that implements KeypairUtil

This commit is contained in:
Greg Fitzgerald 2019-01-29 16:57:48 -07:00
parent 34c8b2cc2f
commit c741a960b9
8 changed files with 26 additions and 25 deletions

View File

@ -97,11 +97,11 @@ pub struct Transaction {
} }
impl Transaction { impl Transaction {
pub fn new<T: Serialize>( pub fn new<S: Serialize, T: KeypairUtil>(
from_keypair: &Keypair, from_keypair: &T,
transaction_keys: &[Pubkey], transaction_keys: &[Pubkey],
program_id: Pubkey, program_id: Pubkey,
userdata: &T, userdata: &S,
last_id: Hash, last_id: Hash,
fee: u64, fee: u64,
) -> Self { ) -> Self {
@ -130,7 +130,14 @@ impl Transaction {
let instructions = vec![Instruction::new(0, userdata, accounts)]; let instructions = vec![Instruction::new(0, userdata, accounts)];
let mut keys = vec![*from_pubkey]; let mut keys = vec![*from_pubkey];
keys.extend_from_slice(transaction_keys); keys.extend_from_slice(transaction_keys);
Self::new_with_instructions(&[], &keys[..], last_id, fee, program_ids, instructions) Self::new_with_instructions::<Keypair>(
&[],
&keys[..],
last_id,
fee,
program_ids,
instructions,
)
} }
/// Create a signed transaction /// Create a signed transaction
/// * `from_keypair` - The key used to sign the transaction. This key is stored as keys[0] /// * `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. /// * `fee` - The transaction fee.
/// * `program_ids` - The keys that identify programs used in the `instruction` vector. /// * `program_ids` - The keys that identify programs used in the `instruction` vector.
/// * `instructions` - The programs and their arguments that the transaction will execute atomically /// * `instructions` - The programs and their arguments that the transaction will execute atomically
pub fn new_with_instructions( pub fn new_with_instructions<T: KeypairUtil>(
from_keypairs: &[&Keypair], from_keypairs: &[&T],
keys: &[Pubkey], keys: &[Pubkey],
last_id: Hash, last_id: Hash,
fee: u64, fee: u64,
@ -211,7 +218,7 @@ impl Transaction {
} }
/// Sign this transaction. /// Sign this transaction.
pub fn sign(&mut self, keypairs: &[&Keypair], last_id: Hash) { pub fn sign<T: KeypairUtil>(&mut self, keypairs: &[&T], last_id: Hash) {
self.last_id = last_id; self.last_id = last_id;
let message = self.message(); let message = self.message();
self.signatures = keypairs self.signatures = keypairs

View File

@ -10,7 +10,7 @@ use crate::vote_program::{self, Vote, VoteInstruction};
use bincode::deserialize; use bincode::deserialize;
pub trait VoteTransaction { 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( fn vote_account_new(
validator_id: &Keypair, validator_id: &Keypair,
vote_account_id: Pubkey, vote_account_id: Pubkey,
@ -23,7 +23,8 @@ pub trait VoteTransaction {
} }
impl VoteTransaction for Transaction { 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); let instruction = VoteInstruction::NewVote(vote);
Transaction::new_unsigned( Transaction::new_unsigned(
vote_account, vote_account,

View File

@ -457,7 +457,7 @@ mod tests {
let mut error_counters = ErrorCounters::default(); let mut error_counters = ErrorCounters::default();
let instructions = vec![Instruction::new(1, &(), vec![0])]; let instructions = vec![Instruction::new(1, &(), vec![0])];
let tx = Transaction::new_with_instructions( let tx = Transaction::new_with_instructions::<Keypair>(
&[], &[],
&[], &[],
Hash::default(), Hash::default(),

View File

@ -607,7 +607,7 @@ mod tests {
let one = hash(&zero.as_ref()); let one = hash(&zero.as_ref());
let keypair = Keypair::new(); let keypair = Keypair::new();
let vote_account = 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 sig = vote_account.sign_message(&tx.message());
let tx0 = Transaction { let tx0 = Transaction {
signatures: vec![sig], signatures: vec![sig],
@ -664,7 +664,7 @@ mod tests {
let next_id = hash(&id.as_ref()); let next_id = hash(&id.as_ref());
let keypair = Keypair::new(); let keypair = Keypair::new();
let vote_account = 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 sig = vote_account.sign_message(&tx.message());
let tx_small = Transaction { let tx_small = Transaction {
signatures: vec![sig], signatures: vec![sig],

View File

@ -13,7 +13,7 @@ use solana_sdk::pubkey::Pubkey;
use solana_sdk::signature::{Keypair, KeypairUtil}; use solana_sdk::signature::{Keypair, KeypairUtil};
use solana_sdk::system_transaction::SystemTransaction; use solana_sdk::system_transaction::SystemTransaction;
use solana_sdk::transaction::Transaction; 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 solana_sdk::vote_transaction::VoteTransaction;
use std::io::Cursor; use std::io::Cursor;
use std::sync::Arc; use std::sync::Arc;
@ -499,8 +499,7 @@ pub fn make_active_set_entries(
last_entry_id = new_vote_account_entry.id; last_entry_id = new_vote_account_entry.id;
// 3) Create vote entry // 3) Create vote entry
let vote = Vote { tick_height: 1 }; let tx = Transaction::vote_new(&vote_account_id, 1, *last_tick_id, 0);
let tx = Transaction::vote_new(&vote_account_id, vote, *last_tick_id, 0);
let sig = active_keypair.sign_message(&tx.message()); let sig = active_keypair.sign_message(&tx.message());
let vote_tx = Transaction { let vote_tx = Transaction {
signatures: vec![sig], signatures: vec![sig],

View File

@ -242,7 +242,7 @@ impl StorageStage {
let last_id = client.get_last_id(); let last_id = client.get_last_id();
tx.sign(&[&keypair], last_id); tx.sign(&[keypair.as_ref()], last_id);
if exit.load(Ordering::Relaxed) { if exit.load(Ordering::Relaxed) {
Err(io::Error::new(io::ErrorKind::Other, "exit signaled"))?; Err(io::Error::new(io::ErrorKind::Other, "exit signaled"))?;
@ -454,7 +454,6 @@ mod tests {
use solana_sdk::pubkey::Pubkey; use solana_sdk::pubkey::Pubkey;
use solana_sdk::signature::{Keypair, KeypairUtil}; use solana_sdk::signature::{Keypair, KeypairUtil};
use solana_sdk::transaction::Transaction; use solana_sdk::transaction::Transaction;
use solana_sdk::vote_program::Vote;
use solana_sdk::vote_transaction::VoteTransaction; use solana_sdk::vote_transaction::VoteTransaction;
use std::cmp::{max, min}; use std::cmp::{max, min};
use std::fs::remove_dir_all; use std::fs::remove_dir_all;
@ -604,11 +603,8 @@ mod tests {
reference_keys.copy_from_slice(keys); reference_keys.copy_from_slice(keys);
} }
let mut vote_txs: Vec<Transaction> = Vec::new(); let mut vote_txs: Vec<Transaction> = Vec::new();
let vote = Vote {
tick_height: 123456,
};
let keypair = Keypair::new(); 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 sig = keypair.sign_message(&tx.message());
let vote_tx = Transaction { let vote_tx = Transaction {
signatures: vec![sig], signatures: vec![sig],

View File

@ -112,7 +112,7 @@ impl ThinClient {
tries: usize, tries: usize,
) -> io::Result<Signature> { ) -> io::Result<Signature> {
for x in 0..tries { 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 buf = vec![0; tx.serialized_size().unwrap() as usize];
let mut wr = std::io::Cursor::new(&mut buf[..]); let mut wr = std::io::Cursor::new(&mut buf[..]);
serialize_into(&mut wr, &tx).expect("serialize Transaction in pub fn transfer_signed"); serialize_into(&mut wr, &tx).expect("serialize Transaction in pub fn transfer_signed");

View File

@ -14,7 +14,6 @@ use solana_sdk::hash::Hash;
use solana_sdk::pubkey::Pubkey; use solana_sdk::pubkey::Pubkey;
use solana_sdk::signature::{Keypair, KeypairUtil, Signature}; use solana_sdk::signature::{Keypair, KeypairUtil, Signature};
use solana_sdk::transaction::Transaction; use solana_sdk::transaction::Transaction;
use solana_sdk::vote_program::Vote;
use solana_sdk::vote_transaction::VoteTransaction; use solana_sdk::vote_transaction::VoteTransaction;
use solana_vote_signer::rpc::LocalVoteSigner; use solana_vote_signer::rpc::LocalVoteSigner;
use solana_vote_signer::rpc::VoteSigner; 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 { 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, tick_height, *last_id, 0);
let mut tx = Transaction::vote_new(&self.vote_account, vote, *last_id, 0);
assert!(tx.signatures.is_empty()); assert!(tx.signatures.is_empty());
let sig = self.sign_message(&tx.message()); let sig = self.sign_message(&tx.message());
tx.signatures.push(sig); tx.signatures.push(sig);