Boot fees from TransactionBuilder

This commit is contained in:
Greg Fitzgerald 2019-03-15 10:30:50 -06:00
parent e091aa87ea
commit aca739b800
7 changed files with 41 additions and 109 deletions

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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(

View File

@ -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
}

View File

@ -35,28 +35,13 @@ fn compile_instructions(
/// A utility for constructing transactions
#[derive(Default)]
pub struct TransactionBuilder {
fee: u64,
instructions: Vec<Instruction>,
}
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<Instruction>) -> Transaction {
let fee = 0;
Self { fee, instructions }.compile()
pub fn new(instructions: Vec<Instruction>) -> 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()
);
}
}

View File

@ -442,7 +442,7 @@ fn process_configure_staking(
authorized_voter_option: Option<Pubkey>,
) -> 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(),