Add program_ids() methods

Added CompiledInstruction::program_id() so that we don't need to pass
around instruction indexes just for Message::program_id().

Also added Message.program_ids() that returns a slice so that we
can move those pubkeys into Message::account_keys.
This commit is contained in:
Greg Fitzgerald
2019-04-02 16:02:57 -06:00
parent 025b4f90de
commit 4c0bc1fd88
6 changed files with 31 additions and 25 deletions

View File

@@ -129,4 +129,8 @@ impl CompiledInstruction {
accounts,
}
}
pub fn program_id<'a>(&self, program_ids: &'a [Pubkey]) -> &'a Pubkey {
&program_ids[self.program_ids_index as usize]
}
}

View File

@@ -83,7 +83,7 @@ pub struct Message {
/// All the program id keys used to execute this transaction's instructions
#[serde(with = "short_vec")]
pub program_ids: Vec<Pubkey>,
program_ids: Vec<Pubkey>,
/// Programs that will be executed in sequence and committed in one atomic transaction if all
/// succeed.
@@ -123,9 +123,8 @@ impl Message {
)
}
pub fn program_id(&self, instruction_index: usize) -> &Pubkey {
let program_ids_index = self.instructions[instruction_index].program_ids_index;
&self.program_ids[program_ids_index as usize]
pub fn program_ids(&self) -> &[Pubkey] {
&self.program_ids
}
}

View File

@@ -145,9 +145,6 @@ impl Transaction {
}
}
}
pub fn program_id(&self, instruction_index: usize) -> &Pubkey {
self.message().program_id(instruction_index)
}
/// Return a message containing all data that should be signed.
pub fn message(&self) -> &Message {
@@ -185,7 +182,7 @@ impl Transaction {
pub fn verify_refs(&self) -> bool {
let message = self.message();
for instruction in &message.instructions {
if (instruction.program_ids_index as usize) >= message.program_ids.len() {
if (instruction.program_ids_index as usize) >= message.program_ids().len() {
return false;
}
for account_index in &instruction.accounts {
@@ -207,6 +204,12 @@ mod tests {
use bincode::{deserialize, serialize, serialized_size};
use std::mem::size_of;
fn get_program_id(tx: &Transaction, instruction_index: usize) -> &Pubkey {
let message = tx.message();
let instruction = &message.instructions[instruction_index];
instruction.program_id(message.program_ids())
}
#[test]
fn test_refs() {
let key = Keypair::new();
@@ -245,8 +248,8 @@ mod tests {
assert_eq!(tx.key(0, 2), None);
assert_eq!(tx.signer_key(0, 2), None);
assert_eq!(*tx.program_id(0), prog1);
assert_eq!(*tx.program_id(1), prog2);
assert_eq!(*get_program_id(&tx, 0), prog1);
assert_eq!(*get_program_id(&tx, 1), prog2);
}
#[test]
fn test_refs_invalid_program_id() {
@@ -272,7 +275,7 @@ mod tests {
vec![Pubkey::default()],
instructions,
);
assert_eq!(*tx.program_id(0), Pubkey::default());
assert_eq!(*get_program_id(&tx, 0), Pubkey::default());
assert!(!tx.verify_refs());
}
@@ -350,7 +353,7 @@ mod tests {
+ (tx.message.account_keys.len() * size_of::<Pubkey>())
+ blockhash_size
+ len_size
+ (tx.message.program_ids.len() * size_of::<Pubkey>())
+ (tx.message.program_ids().len() * size_of::<Pubkey>())
+ len_size
+ expected_instruction_size;
assert_eq!(expected_transaction_size, 214);