Generalize Transaction::new to accept anything that implements KeypairUtil
This commit is contained in:
parent
34c8b2cc2f
commit
c741a960b9
|
@ -97,11 +97,11 @@ pub struct Transaction {
|
|||
}
|
||||
|
||||
impl Transaction {
|
||||
pub fn new<T: Serialize>(
|
||||
from_keypair: &Keypair,
|
||||
pub fn new<S: Serialize, T: KeypairUtil>(
|
||||
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::<Keypair>(
|
||||
&[],
|
||||
&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<T: KeypairUtil>(
|
||||
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<T: KeypairUtil>(&mut self, keypairs: &[&T], last_id: Hash) {
|
||||
self.last_id = last_id;
|
||||
let message = self.message();
|
||||
self.signatures = keypairs
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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::<Keypair>(
|
||||
&[],
|
||||
&[],
|
||||
Hash::default(),
|
||||
|
|
|
@ -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],
|
||||
|
|
|
@ -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],
|
||||
|
|
|
@ -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<Transaction> = 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],
|
||||
|
|
|
@ -112,7 +112,7 @@ impl ThinClient {
|
|||
tries: usize,
|
||||
) -> io::Result<Signature> {
|
||||
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");
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue