From 968022a1b008a56a2776ff8c5f8e4f7e1c5d9fc0 Mon Sep 17 00:00:00 2001 From: Greg Fitzgerald Date: Fri, 15 Mar 2019 09:47:25 -0600 Subject: [PATCH] Instruction name swap * Instruction -> GenericInstruction * Instruction -> CompiledInstruction * Instruction -> Instruction --- core/src/sigverify.rs | 4 +- programs/budget_api/src/budget_instruction.rs | 18 +++---- .../rewards_api/src/rewards_instruction.rs | 6 +-- programs/storage/src/lib.rs | 4 +- programs/vote/tests/vote.rs | 6 +-- programs/vote_api/src/vote_instruction.rs | 25 ++++------ runtime/src/accounts.rs | 24 ++++----- runtime/src/bank.rs | 6 +-- runtime/tests/system.rs | 6 +-- sdk/src/system_instruction.rs | 10 ++-- sdk/src/system_transaction.rs | 4 +- sdk/src/transaction.rs | 50 ++++++++++++------- sdk/src/transaction_builder.rs | 28 +++++------ 13 files changed, 97 insertions(+), 94 deletions(-) diff --git a/core/src/sigverify.rs b/core/src/sigverify.rs index 225befe90..047074cf3 100644 --- a/core/src/sigverify.rs +++ b/core/src/sigverify.rs @@ -340,7 +340,7 @@ mod tests { use solana_sdk::signature::{Keypair, KeypairUtil}; use solana_sdk::system_instruction::SystemInstruction; use solana_sdk::system_program; - use solana_sdk::transaction::{Instruction, Transaction}; + use solana_sdk::transaction::{CompiledInstruction, Transaction}; const SIG_OFFSET: usize = std::mem::size_of::() + 1; @@ -501,7 +501,7 @@ mod tests { let program_ids = vec![system_program::id(), solana_budget_api::id()]; - let instructions = vec![Instruction::new(0, &system_instruction, vec![0, 1])]; + let instructions = vec![CompiledInstruction::new(0, &system_instruction, vec![0, 1])]; let tx = Transaction::new_with_instructions( &keypairs, diff --git a/programs/budget_api/src/budget_instruction.rs b/programs/budget_api/src/budget_instruction.rs index 31aa92170..71be56c6d 100644 --- a/programs/budget_api/src/budget_instruction.rs +++ b/programs/budget_api/src/budget_instruction.rs @@ -3,7 +3,7 @@ use crate::id; use chrono::prelude::{DateTime, Utc}; use serde_derive::{Deserialize, Serialize}; use solana_sdk::pubkey::Pubkey; -use solana_sdk::transaction_builder::BuilderInstruction; +use solana_sdk::transaction::Instruction; /// A smart contract. #[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)] @@ -28,13 +28,13 @@ pub enum BudgetInstruction { } impl BudgetInstruction { - pub fn new_initialize_account(contract: &Pubkey, expr: BudgetExpr) -> BuilderInstruction { + pub fn new_initialize_account(contract: &Pubkey, expr: BudgetExpr) -> Instruction { let mut keys = vec![]; if let BudgetExpr::Pay(payment) = &expr { keys.push((payment.to, false)); } keys.push((*contract, false)); - BuilderInstruction::new(id(), &BudgetInstruction::InitializeAccount(expr), keys) + Instruction::new(id(), &BudgetInstruction::InitializeAccount(expr), keys) } pub fn new_apply_timestamp( @@ -42,23 +42,19 @@ impl BudgetInstruction { contract: &Pubkey, to: &Pubkey, dt: DateTime, - ) -> BuilderInstruction { + ) -> Instruction { let mut keys = vec![(*from, true), (*contract, false)]; if from != to { keys.push((*to, false)); } - BuilderInstruction::new(id(), &BudgetInstruction::ApplyTimestamp(dt), keys) + Instruction::new(id(), &BudgetInstruction::ApplyTimestamp(dt), keys) } - pub fn new_apply_signature( - from: &Pubkey, - contract: &Pubkey, - to: &Pubkey, - ) -> BuilderInstruction { + pub fn new_apply_signature(from: &Pubkey, contract: &Pubkey, to: &Pubkey) -> Instruction { let mut keys = vec![(*from, true), (*contract, false)]; if from != to { keys.push((*to, false)); } - BuilderInstruction::new(id(), &BudgetInstruction::ApplySignature, keys) + Instruction::new(id(), &BudgetInstruction::ApplySignature, keys) } } diff --git a/programs/rewards_api/src/rewards_instruction.rs b/programs/rewards_api/src/rewards_instruction.rs index ab9c4f1de..f278b9611 100644 --- a/programs/rewards_api/src/rewards_instruction.rs +++ b/programs/rewards_api/src/rewards_instruction.rs @@ -1,7 +1,7 @@ use crate::id; use serde_derive::{Deserialize, Serialize}; use solana_sdk::pubkey::Pubkey; -use solana_sdk::transaction_builder::BuilderInstruction; +use solana_sdk::transaction::Instruction; #[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)] pub enum RewardsInstruction { @@ -9,8 +9,8 @@ pub enum RewardsInstruction { } impl RewardsInstruction { - pub fn new_redeem_vote_credits(vote_id: &Pubkey, rewards_id: &Pubkey) -> BuilderInstruction { - BuilderInstruction::new( + pub fn new_redeem_vote_credits(vote_id: &Pubkey, rewards_id: &Pubkey) -> Instruction { + Instruction::new( id(), &RewardsInstruction::RedeemVoteCredits, vec![(*vote_id, true), (*rewards_id, false)], diff --git a/programs/storage/src/lib.rs b/programs/storage/src/lib.rs index 882b2e4dd..c6fdf3dea 100644 --- a/programs/storage/src/lib.rs +++ b/programs/storage/src/lib.rs @@ -180,7 +180,7 @@ mod test { use solana_sdk::account::{create_keyed_accounts, Account}; use solana_sdk::hash::Hash; use solana_sdk::signature::{Keypair, KeypairUtil, Signature}; - use solana_sdk::transaction::{Instruction, Transaction}; + use solana_sdk::transaction::{CompiledInstruction, Transaction}; use solana_storage_api::{ProofStatus, StorageTransaction}; fn test_transaction( @@ -188,7 +188,7 @@ mod test { program_accounts: &mut [Account], ) -> Result<(), ProgramError> { assert_eq!(tx.instructions.len(), 1); - let Instruction { + let CompiledInstruction { ref accounts, ref data, .. diff --git a/programs/vote/tests/vote.rs b/programs/vote/tests/vote.rs index c6a94e289..ac343ccc8 100644 --- a/programs/vote/tests/vote.rs +++ b/programs/vote/tests/vote.rs @@ -5,8 +5,8 @@ use solana_sdk::native_program::ProgramError; use solana_sdk::pubkey::Pubkey; use solana_sdk::signature::{Keypair, KeypairUtil}; use solana_sdk::system_instruction::SystemInstruction; -use solana_sdk::transaction::{InstructionError, TransactionError}; -use solana_sdk::transaction_builder::{BuilderInstruction, TransactionBuilder}; +use solana_sdk::transaction::{Instruction, InstructionError, TransactionError}; +use solana_sdk::transaction_builder::TransactionBuilder; use solana_vote_api::vote_instruction::{Vote, VoteInstruction}; use solana_vote_api::vote_state::VoteState; use solana_vote_api::vote_transaction::VoteTransaction; @@ -111,7 +111,7 @@ fn test_vote_via_bank_with_no_signature() { let mallory_id = mallory_keypair.pubkey(); let blockhash = bank.last_blockhash(); - let vote_ix = BuilderInstruction::new( + let vote_ix = Instruction::new( solana_vote_api::id(), &VoteInstruction::Vote(Vote::new(0)), vec![(vote_id, false)], // <--- attack!! No signature. diff --git a/programs/vote_api/src/vote_instruction.rs b/programs/vote_api/src/vote_instruction.rs index ea69c829d..efccda259 100644 --- a/programs/vote_api/src/vote_instruction.rs +++ b/programs/vote_api/src/vote_instruction.rs @@ -1,7 +1,7 @@ use crate::id; use serde_derive::{Deserialize, Serialize}; use solana_sdk::pubkey::Pubkey; -use solana_sdk::transaction_builder::BuilderInstruction; +use solana_sdk::transaction::Instruction; #[derive(Serialize, Default, Deserialize, Debug, PartialEq, Eq, Clone)] pub struct Vote { @@ -32,34 +32,31 @@ pub enum VoteInstruction { } impl VoteInstruction { - pub fn new_clear_credits(vote_id: &Pubkey) -> BuilderInstruction { - BuilderInstruction::new(id(), &VoteInstruction::ClearCredits, vec![(*vote_id, true)]) + pub fn new_clear_credits(vote_id: &Pubkey) -> Instruction { + Instruction::new(id(), &VoteInstruction::ClearCredits, vec![(*vote_id, true)]) } - pub fn new_delegate_stake(vote_id: &Pubkey, delegate_id: &Pubkey) -> BuilderInstruction { - BuilderInstruction::new( + pub fn new_delegate_stake(vote_id: &Pubkey, delegate_id: &Pubkey) -> Instruction { + Instruction::new( id(), &VoteInstruction::DelegateStake(*delegate_id), vec![(*vote_id, true)], ) } - pub fn new_authorize_voter( - vote_id: &Pubkey, - authorized_voter_id: &Pubkey, - ) -> BuilderInstruction { - BuilderInstruction::new( + pub fn new_authorize_voter(vote_id: &Pubkey, authorized_voter_id: &Pubkey) -> Instruction { + Instruction::new( id(), &VoteInstruction::AuthorizeVoter(*authorized_voter_id), vec![(*vote_id, true)], ) } - pub fn new_initialize_account(vote_id: &Pubkey) -> BuilderInstruction { - BuilderInstruction::new( + pub fn new_initialize_account(vote_id: &Pubkey) -> Instruction { + Instruction::new( id(), &VoteInstruction::InitializeAccount, vec![(*vote_id, false)], ) } - pub fn new_vote(vote_id: &Pubkey, vote: Vote) -> BuilderInstruction { - BuilderInstruction::new(id(), &VoteInstruction::Vote(vote), vec![(*vote_id, true)]) + pub fn new_vote(vote_id: &Pubkey, vote: Vote) -> Instruction { + Instruction::new(id(), &VoteInstruction::Vote(vote), vec![(*vote_id, true)]) } } diff --git a/runtime/src/accounts.rs b/runtime/src/accounts.rs index 4f7f4a728..dd43d52a9 100644 --- a/runtime/src/accounts.rs +++ b/runtime/src/accounts.rs @@ -1017,7 +1017,7 @@ mod tests { use solana_sdk::hash::Hash; use solana_sdk::signature::Keypair; use solana_sdk::signature::KeypairUtil; - use solana_sdk::transaction::Instruction; + use solana_sdk::transaction::CompiledInstruction; use solana_sdk::transaction::Transaction; fn cleanup_paths(paths: &str) { @@ -1046,7 +1046,7 @@ mod tests { let accounts: Vec<(Pubkey, Account)> = Vec::new(); let mut error_counters = ErrorCounters::default(); - let instructions = vec![Instruction::new(1, &(), vec![0])]; + let instructions = vec![CompiledInstruction::new(1, &(), vec![0])]; let tx = Transaction::new_with_instructions::( &[], &[], @@ -1070,7 +1070,7 @@ mod tests { let keypair = Keypair::new(); - let instructions = vec![Instruction::new(1, &(), vec![0])]; + let instructions = vec![CompiledInstruction::new(1, &(), vec![0])]; let tx = Transaction::new_with_instructions( &[&keypair], &[], @@ -1102,7 +1102,7 @@ mod tests { let account = Account::new(2, 1, &Pubkey::default()); accounts.push((key1, account)); - let instructions = vec![Instruction::new(1, &(), vec![0])]; + let instructions = vec![CompiledInstruction::new(1, &(), vec![0])]; let tx = Transaction::new_with_instructions( &[&keypair], &[], @@ -1130,7 +1130,7 @@ mod tests { let account = Account::new(1, 1, &Pubkey::default()); accounts.push((key0, account)); - let instructions = vec![Instruction::new(1, &(), vec![0])]; + let instructions = vec![CompiledInstruction::new(1, &(), vec![0])]; let tx = Transaction::new_with_instructions( &[&keypair], &[], @@ -1165,7 +1165,7 @@ mod tests { let account = Account::new(2, 1, &Pubkey::default()); accounts.push((key1, account)); - let instructions = vec![Instruction::new(0, &(), vec![0, 1])]; + let instructions = vec![CompiledInstruction::new(0, &(), vec![0, 1])]; let tx = Transaction::new_with_instructions( &[&keypair], &[key1], @@ -1237,7 +1237,7 @@ mod tests { account.owner = key5; accounts.push((key6, account)); - let instructions = vec![Instruction::new(0, &(), vec![0])]; + let instructions = vec![CompiledInstruction::new(0, &(), vec![0])]; let tx = Transaction::new_with_instructions( &[&keypair], &[], @@ -1271,7 +1271,7 @@ mod tests { account.owner = Pubkey::default(); accounts.push((key1, account)); - let instructions = vec![Instruction::new(0, &(), vec![0])]; + let instructions = vec![CompiledInstruction::new(0, &(), vec![0])]; let tx = Transaction::new_with_instructions( &[&keypair], &[], @@ -1304,7 +1304,7 @@ mod tests { account.owner = native_loader::id(); accounts.push((key1, account)); - let instructions = vec![Instruction::new(0, &(), vec![0])]; + let instructions = vec![CompiledInstruction::new(0, &(), vec![0])]; let tx = Transaction::new_with_instructions( &[&keypair], &[], @@ -1351,8 +1351,8 @@ mod tests { accounts.push((key3, account)); let instructions = vec![ - Instruction::new(0, &(), vec![0]), - Instruction::new(1, &(), vec![0]), + CompiledInstruction::new(0, &(), vec![0]), + CompiledInstruction::new(1, &(), vec![0]), ]; let tx = Transaction::new_with_instructions( &[&keypair], @@ -1396,7 +1396,7 @@ mod tests { let account = Account::new(10, 1, &Pubkey::default()); accounts.push((pubkey, account)); - let instructions = vec![Instruction::new(0, &(), vec![0, 1])]; + let instructions = vec![CompiledInstruction::new(0, &(), vec![0, 1])]; // Simulate pay-to-self transaction, which loads the same account twice let tx = Transaction::new_with_instructions( &[&keypair], diff --git a/runtime/src/bank.rs b/runtime/src/bank.rs index 3d5077fbc..5d414ba1e 100644 --- a/runtime/src/bank.rs +++ b/runtime/src/bank.rs @@ -847,7 +847,7 @@ mod tests { use solana_sdk::system_instruction::SystemInstruction; use solana_sdk::system_program; use solana_sdk::system_transaction::SystemTransaction; - use solana_sdk::transaction::{Instruction, InstructionError}; + use solana_sdk::transaction::{CompiledInstruction, InstructionError}; #[test] fn test_bank_new() { @@ -927,12 +927,12 @@ mod tests { let bank = Bank::new(&genesis_block); let spend = SystemInstruction::Move { lamports: 1 }; let instructions = vec![ - Instruction { + CompiledInstruction { program_ids_index: 0, data: serialize(&spend).unwrap(), accounts: vec![0, 1], }, - Instruction { + CompiledInstruction { program_ids_index: 0, data: serialize(&spend).unwrap(), accounts: vec![0, 2], diff --git a/runtime/tests/system.rs b/runtime/tests/system.rs index 00aa9514d..5d58cd684 100644 --- a/runtime/tests/system.rs +++ b/runtime/tests/system.rs @@ -5,8 +5,8 @@ use solana_sdk::native_program::ProgramError; use solana_sdk::signature::{Keypair, KeypairUtil}; use solana_sdk::system_instruction::SystemInstruction; use solana_sdk::system_program; -use solana_sdk::transaction::{InstructionError, TransactionError}; -use solana_sdk::transaction_builder::{BuilderInstruction, TransactionBuilder}; +use solana_sdk::transaction::{Instruction, InstructionError, TransactionError}; +use solana_sdk::transaction_builder::TransactionBuilder; #[test] fn test_system_unsigned_transaction() { @@ -26,7 +26,7 @@ fn test_system_unsigned_transaction() { // Erroneously sign transaction with recipient account key // No signature case is tested by bank `test_zero_signatures()` - let ix = BuilderInstruction::new( + let ix = Instruction::new( system_program::id(), &SystemInstruction::Move { lamports: 10 }, vec![(from_pubkey, false), (to_pubkey, true)], diff --git a/sdk/src/system_instruction.rs b/sdk/src/system_instruction.rs index a11b3d1cc..34ba5ed2f 100644 --- a/sdk/src/system_instruction.rs +++ b/sdk/src/system_instruction.rs @@ -1,6 +1,6 @@ use crate::pubkey::Pubkey; use crate::system_program; -use crate::transaction_builder::BuilderInstruction; +use crate::transaction::Instruction; #[derive(Serialize, Debug, Clone, PartialEq)] pub enum SystemError { @@ -38,8 +38,8 @@ impl SystemInstruction { lamports: u64, space: u64, program_id: &Pubkey, - ) -> BuilderInstruction { - BuilderInstruction::new( + ) -> Instruction { + Instruction::new( system_program::id(), &SystemInstruction::CreateAccount { lamports, @@ -50,8 +50,8 @@ impl SystemInstruction { ) } - pub fn new_move(from_id: &Pubkey, to_id: &Pubkey, lamports: u64) -> BuilderInstruction { - BuilderInstruction::new( + pub fn new_move(from_id: &Pubkey, to_id: &Pubkey, lamports: u64) -> Instruction { + Instruction::new( system_program::id(), &SystemInstruction::Move { lamports }, vec![(*from_id, true), (*to_id, false)], diff --git a/sdk/src/system_transaction.rs b/sdk/src/system_transaction.rs index 3d71298a2..d2337c426 100644 --- a/sdk/src/system_transaction.rs +++ b/sdk/src/system_transaction.rs @@ -5,7 +5,7 @@ use crate::pubkey::Pubkey; use crate::signature::Keypair; use crate::system_instruction::SystemInstruction; use crate::system_program; -use crate::transaction::{Instruction, Transaction}; +use crate::transaction::{CompiledInstruction, Transaction}; pub struct SystemTransaction {} @@ -103,7 +103,7 @@ impl SystemTransaction { .enumerate() .map(|(i, (_, amount))| { let spend = SystemInstruction::Move { lamports: *amount }; - Instruction::new(0, &spend, vec![0, i as u8 + 1]) + CompiledInstruction::new(0, &spend, vec![0, i as u8 + 1]) }) .collect(); let to_keys: Vec<_> = moves.iter().map(|(to_key, _)| *to_key).collect(); diff --git a/sdk/src/transaction.rs b/sdk/src/transaction.rs index b1af0adf1..602769c2f 100644 --- a/sdk/src/transaction.rs +++ b/sdk/src/transaction.rs @@ -10,7 +10,7 @@ use crate::shortvec::{ }; use crate::signature::{KeypairUtil, Signature}; use crate::system_instruction::SystemError; -use crate::transaction_builder::{BuilderInstruction, TransactionBuilder}; +use crate::transaction_builder::TransactionBuilder; use bincode::{serialize, Error}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use serde::{Deserialize, Serialize, Serializer}; @@ -50,7 +50,7 @@ impl InstructionError { /// An instruction to execute a program #[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)] -pub struct Instruction { +pub struct GenericInstruction { /// Index into the transaction program ids array indicating the program account that executes this instruction pub program_ids_index: P, /// Ordered indices into the transaction keys array indicating which accounts to pass to the program @@ -59,7 +59,7 @@ pub struct Instruction { pub data: Vec, } -impl Instruction { +impl GenericInstruction { pub fn new(program_ids_index: P, data: &T, accounts: Vec) -> Self { let data = serialize(data).unwrap(); Self { @@ -70,7 +70,10 @@ impl Instruction { } } -impl Instruction { +pub type Instruction = GenericInstruction; +pub type CompiledInstruction = GenericInstruction; + +impl CompiledInstruction { pub fn serialize_with(mut writer: &mut Cursor<&mut [u8]>, ix: &Self) -> Result<(), Error> { writer.write_all(&[ix.program_ids_index])?; serialize_vec_bytes(&mut writer, &ix.accounts[..])?; @@ -84,7 +87,7 @@ impl Instruction { let program_ids_index = buf[0]; let accounts = deserialize_vec_bytes(&mut reader)?; let data = deserialize_vec_bytes(&mut reader)?; - Ok(Instruction { + Ok(CompiledInstruction { program_ids_index, accounts, data, @@ -160,7 +163,7 @@ pub struct Transaction { pub program_ids: Vec, /// Programs that will be executed in sequence and committed in one atomic transaction if all /// succeed. - pub instructions: Vec>, + pub instructions: Vec, } impl Transaction { @@ -195,7 +198,7 @@ impl Transaction { for pubkey in transaction_keys { account_keys.push((*pubkey, false)); } - let instruction = BuilderInstruction::new(*program_id, data, account_keys); + let instruction = Instruction::new(*program_id, data, account_keys); let mut transaction = TransactionBuilder::new(fee).push(instruction).compile(); transaction.recent_blockhash = recent_blockhash; transaction @@ -215,7 +218,7 @@ impl Transaction { recent_blockhash: Hash, fee: u64, program_ids: Vec, - instructions: Vec>, + instructions: Vec, ) -> Self { let mut account_keys: Vec<_> = from_keypairs .iter() @@ -275,8 +278,12 @@ impl Transaction { .expect("serialize fee"); serialize_vec_with(&mut wr, &self.program_ids, Transaction::serialize_pubkey) .expect("serialize program_ids"); - serialize_vec_with(&mut wr, &self.instructions, Instruction::serialize_with) - .expect("serialize instructions"); + serialize_vec_with( + &mut wr, + &self.instructions, + CompiledInstruction::serialize_with, + ) + .expect("serialize instructions"); let len = wr.position() as usize; wr.into_inner()[..len].to_vec() } @@ -416,8 +423,12 @@ impl Serialize for Transaction { .map_err(Error::custom)?; serialize_vec_with(&mut wr, &self.program_ids, Transaction::serialize_pubkey) .map_err(Error::custom)?; - serialize_vec_with(&mut wr, &self.instructions, Instruction::serialize_with) - .map_err(Error::custom)?; + serialize_vec_with( + &mut wr, + &self.instructions, + CompiledInstruction::serialize_with, + ) + .map_err(Error::custom)?; let size = wr.position() as usize; serializer.serialize_bytes(&wr.into_inner()[..size]) } @@ -449,8 +460,9 @@ impl<'a> serde::de::Visitor<'a> for TransactionVisitor { let program_ids: Vec = deserialize_vec_with(&mut rd, Transaction::deserialize_pubkey) .map_err(Error::custom)?; - let instructions: Vec> = - deserialize_vec_with(&mut rd, Instruction::deserialize_from).map_err(Error::custom)?; + let instructions: Vec = + deserialize_vec_with(&mut rd, CompiledInstruction::deserialize_from) + .map_err(Error::custom)?; Ok(Transaction { signatures, account_keys, @@ -485,8 +497,8 @@ mod tests { let prog1 = Keypair::new().pubkey(); let prog2 = Keypair::new().pubkey(); let instructions = vec![ - Instruction::new(0, &(), vec![0, 1]), - Instruction::new(1, &(), vec![0, 2]), + CompiledInstruction::new(0, &(), vec![0, 1]), + CompiledInstruction::new(1, &(), vec![0, 2]), ]; let tx = Transaction::new_with_instructions( &[&key], @@ -522,7 +534,7 @@ mod tests { #[test] fn test_refs_invalid_program_id() { let key = Keypair::new(); - let instructions = vec![Instruction::new(1, &(), vec![])]; + let instructions = vec![CompiledInstruction::new(1, &(), vec![])]; let tx = Transaction::new_with_instructions( &[&key], &[], @@ -536,7 +548,7 @@ mod tests { #[test] fn test_refs_invalid_account() { let key = Keypair::new(); - let instructions = vec![Instruction::new(0, &(), vec![1])]; + let instructions = vec![CompiledInstruction::new(0, &(), vec![1])]; let tx = Transaction::new_with_instructions( &[&key], &[], @@ -686,6 +698,6 @@ mod tests { .push(Instruction::new(program_id, &0, vec![(id0, true)])) .compile(); tx.sign(&[&keypair0], Hash::default()); - assert_eq!(tx.instructions[0], Instruction::new(0, &0, vec![0])); + assert_eq!(tx.instructions[0], CompiledInstruction::new(0, &0, vec![0])); } } diff --git a/sdk/src/transaction_builder.rs b/sdk/src/transaction_builder.rs index 9f1c72d03..5d30642f7 100644 --- a/sdk/src/transaction_builder.rs +++ b/sdk/src/transaction_builder.rs @@ -2,22 +2,20 @@ use crate::hash::Hash; use crate::pubkey::Pubkey; -use crate::transaction::{Instruction, Transaction}; +use crate::transaction::{CompiledInstruction, Instruction, Transaction}; use itertools::Itertools; -pub type BuilderInstruction = Instruction; - fn position(keys: &[Pubkey], key: &Pubkey) -> u8 { keys.iter().position(|k| k == key).unwrap() as u8 } fn compile_instruction( - ix: &BuilderInstruction, + ix: &Instruction, keys: &[Pubkey], program_ids: &[Pubkey], -) -> Instruction { +) -> CompiledInstruction { let accounts: Vec<_> = ix.accounts.iter().map(|(k, _)| position(keys, k)).collect(); - Instruction { + CompiledInstruction { program_ids_index: position(program_ids, &ix.program_ids_index), data: ix.data.clone(), accounts, @@ -25,10 +23,10 @@ fn compile_instruction( } fn compile_instructions( - ixs: &[BuilderInstruction], + ixs: &[Instruction], keys: &[Pubkey], program_ids: &[Pubkey], -) -> Vec> { +) -> Vec { ixs.iter() .map(|ix| compile_instruction(ix, keys, program_ids)) .collect() @@ -38,7 +36,7 @@ fn compile_instructions( #[derive(Default)] pub struct TransactionBuilder { fee: u64, - instructions: Vec, + instructions: Vec, } impl TransactionBuilder { @@ -51,18 +49,18 @@ impl TransactionBuilder { } /// Create a new unsigned transaction from a single instruction - pub fn new_with_instruction(instruction: BuilderInstruction) -> Transaction { + 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) -> Transaction { + pub fn new_with_instructions(instructions: Vec) -> Transaction { let fee = 0; Self { fee, instructions }.compile() } /// Add an instruction. - pub fn push(&mut self, instruction: BuilderInstruction) -> &mut Self { + pub fn push(&mut self, instruction: Instruction) -> &mut Self { self.instructions.push(instruction); self } @@ -247,9 +245,9 @@ mod tests { .push(Instruction::new(program_id1, &0, vec![(id1, true)])) .push(Instruction::new(program_id0, &0, vec![(id1, false)])) .compile(); - assert_eq!(tx.instructions[0], Instruction::new(0, &0, vec![1])); - assert_eq!(tx.instructions[1], Instruction::new(1, &0, vec![0])); - assert_eq!(tx.instructions[2], Instruction::new(0, &0, vec![0])); + assert_eq!(tx.instructions[0], CompiledInstruction::new(0, &0, vec![1])); + assert_eq!(tx.instructions[1], CompiledInstruction::new(1, &0, vec![0])); + assert_eq!(tx.instructions[2], CompiledInstruction::new(0, &0, vec![0])); } #[test]