Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

Commit

Permalink
RPC instruction parser tests are missing some cases (#25951)
Browse files Browse the repository at this point in the history
* Fix num-accounts typo and extend test

* Add extra test cases to parse_system

* Add extra test cases to parse_vote

* Add extra test cases to parse_stake

* Fixup parse_bpf_loader

* Add extra test cases to parse_bpf_upgradeable_loader

* Add extra test cases to parse_associated_token
  • Loading branch information
Tyera Eulberg committed Jun 18, 2022
1 parent efc0d0b commit 3388bdd
Show file tree
Hide file tree
Showing 5 changed files with 245 additions and 64 deletions.
16 changes: 15 additions & 1 deletion transaction-status/src/parse_associated_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ fn check_num_associated_token_accounts(
mod test {
use {
super::*,
solana_account_decoder::parse_token::pubkey_from_spl_token,
spl_associated_token_account::{
create_associated_token_account,
solana_program::{
Expand All @@ -74,6 +75,14 @@ mod test {
}
}

fn convert_account_keys(message: &Message) -> Vec<Pubkey> {
message
.account_keys
.iter()
.map(pubkey_from_spl_token)
.collect()
}

#[test]
fn test_parse_associated_token() {
let mut keys: Vec<Pubkey> = vec![];
Expand All @@ -87,7 +96,7 @@ mod test {
&convert_pubkey(keys[2]),
);
let message = Message::new(&[create_ix], None);
let compiled_instruction = convert_compiled_instruction(&message.instructions[0]);
let mut compiled_instruction = convert_compiled_instruction(&message.instructions[0]);
assert_eq!(
parse_associated_token(&compiled_instruction, &keys).unwrap(),
ParsedInstructionEnum {
Expand All @@ -103,5 +112,10 @@ mod test {
})
}
);
compiled_instruction.accounts.pop();
assert!(
parse_associated_token(&compiled_instruction, &convert_account_keys(&message),)
.is_err()
);
}
}
89 changes: 65 additions & 24 deletions transaction-status/src/parse_bpf_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,26 @@ pub fn parse_bpf_loader(
));
}
match bpf_loader_instruction {
LoaderInstruction::Write { offset, bytes } => Ok(ParsedInstructionEnum {
instruction_type: "write".to_string(),
info: json!({
"offset": offset,
"bytes": base64::encode(bytes),
"account": account_keys[instruction.accounts[0] as usize].to_string(),
}),
}),
LoaderInstruction::Finalize => Ok(ParsedInstructionEnum {
instruction_type: "finalize".to_string(),
info: json!({
"account": account_keys[instruction.accounts[0] as usize].to_string(),
}),
}),
LoaderInstruction::Write { offset, bytes } => {
check_num_bpf_loader_accounts(&instruction.accounts, 1)?;
Ok(ParsedInstructionEnum {
instruction_type: "write".to_string(),
info: json!({
"offset": offset,
"bytes": base64::encode(bytes),
"account": account_keys[instruction.accounts[0] as usize].to_string(),
}),
})
}
LoaderInstruction::Finalize => {
check_num_bpf_loader_accounts(&instruction.accounts, 2)?;
Ok(ParsedInstructionEnum {
instruction_type: "finalize".to_string(),
info: json!({
"account": account_keys[instruction.accounts[0] as usize].to_string(),
}),
})
}
}
}

Expand Down Expand Up @@ -147,6 +153,10 @@ pub fn parse_bpf_upgradeable_loader(
}
}

fn check_num_bpf_loader_accounts(accounts: &[u8], num: usize) -> Result<(), ParseInstructionError> {
check_num_accounts(accounts, num, ParsableProgram::BpfLoader)
}

fn check_num_bpf_upgradeable_loader_accounts(
accounts: &[u8],
num: usize,
Expand Down Expand Up @@ -178,7 +188,7 @@ mod test {
offset,
bytes.clone(),
);
let message = Message::new(&[instruction], Some(&fee_payer));
let mut message = Message::new(&[instruction], Some(&fee_payer));
assert_eq!(
parse_bpf_loader(&message.instructions[0], &account_keys).unwrap(),
ParsedInstructionEnum {
Expand All @@ -191,9 +201,11 @@ mod test {
}
);
assert!(parse_bpf_loader(&message.instructions[0], &missing_account_keys).is_err());
message.instructions[0].accounts.pop();
assert!(parse_bpf_loader(&message.instructions[0], &account_keys,).is_err());

let instruction = solana_sdk::loader_instruction::finalize(&account_pubkey, &program_id);
let message = Message::new(&[instruction], Some(&fee_payer));
let mut message = Message::new(&[instruction], Some(&fee_payer));
assert_eq!(
parse_bpf_loader(&message.instructions[0], &account_keys).unwrap(),
ParsedInstructionEnum {
Expand All @@ -204,6 +216,8 @@ mod test {
}
);
assert!(parse_bpf_loader(&message.instructions[0], &missing_account_keys).is_err());
message.instructions[0].accounts.pop();
assert!(parse_bpf_loader(&message.instructions[0], &account_keys).is_err());

let bad_compiled_instruction = CompiledInstruction {
program_id_index: 3,
Expand Down Expand Up @@ -235,7 +249,7 @@ mod test {
max_data_len,
)
.unwrap();
let message = Message::new(&instructions, None);
let mut message = Message::new(&instructions, None);
assert_eq!(
parse_bpf_upgradeable_loader(&message.instructions[1], &message.account_keys).unwrap(),
ParsedInstructionEnum {
Expand All @@ -251,6 +265,10 @@ mod test {
&message.account_keys[0..2]
)
.is_err());
let keys = message.account_keys.clone();
message.instructions[1].accounts.pop();
message.instructions[1].accounts.pop();
assert!(parse_bpf_upgradeable_loader(&message.instructions[1], &keys,).is_err());
}

#[test]
Expand All @@ -266,7 +284,7 @@ mod test {
offset,
bytes.clone(),
);
let message = Message::new(&[instruction], None);
let mut message = Message::new(&[instruction], None);
assert_eq!(
parse_bpf_upgradeable_loader(&message.instructions[0], &message.account_keys).unwrap(),
ParsedInstructionEnum {
Expand All @@ -284,6 +302,9 @@ mod test {
&message.account_keys[0..1]
)
.is_err());
let keys = message.account_keys.clone();
message.instructions[0].accounts.pop();
assert!(parse_bpf_upgradeable_loader(&message.instructions[0], &keys,).is_err());
}

#[test]
Expand All @@ -308,7 +329,7 @@ mod test {
max_data_len,
)
.unwrap();
let message = Message::new(&instructions, None);
let mut message = Message::new(&instructions, None);
assert_eq!(
parse_bpf_upgradeable_loader(&message.instructions[1], &message.account_keys).unwrap(),
ParsedInstructionEnum {
Expand All @@ -331,6 +352,9 @@ mod test {
&message.account_keys[0..7]
)
.is_err());
let keys = message.account_keys.clone();
message.instructions[1].accounts.pop();
assert!(parse_bpf_upgradeable_loader(&message.instructions[1], &keys,).is_err());
}

#[test]
Expand All @@ -350,7 +374,7 @@ mod test {
&authority_address,
&spill_address,
);
let message = Message::new(&[instruction], None);
let mut message = Message::new(&[instruction], None);
assert_eq!(
parse_bpf_upgradeable_loader(&message.instructions[0], &message.account_keys).unwrap(),
ParsedInstructionEnum {
Expand All @@ -371,6 +395,9 @@ mod test {
&message.account_keys[0..6]
)
.is_err());
let keys = message.account_keys.clone();
message.instructions[0].accounts.pop();
assert!(parse_bpf_upgradeable_loader(&message.instructions[0], &keys,).is_err());
}

#[test]
Expand All @@ -383,7 +410,7 @@ mod test {
&current_authority_address,
&new_authority_address,
);
let message = Message::new(&[instruction], None);
let mut message = Message::new(&[instruction], None);
assert_eq!(
parse_bpf_upgradeable_loader(&message.instructions[0], &message.account_keys).unwrap(),
ParsedInstructionEnum {
Expand All @@ -400,6 +427,10 @@ mod test {
&message.account_keys[0..1]
)
.is_err());
let keys = message.account_keys.clone();
message.instructions[0].accounts.pop();
message.instructions[0].accounts.pop();
assert!(parse_bpf_upgradeable_loader(&message.instructions[0], &keys,).is_err());
}

#[test]
Expand All @@ -416,7 +447,7 @@ mod test {
&current_authority_address,
Some(&new_authority_address),
);
let message = Message::new(&[instruction], None);
let mut message = Message::new(&[instruction], None);
assert_eq!(
parse_bpf_upgradeable_loader(&message.instructions[0], &message.account_keys).unwrap(),
ParsedInstructionEnum {
Expand All @@ -433,13 +464,17 @@ mod test {
&message.account_keys[0..1]
)
.is_err());
let keys = message.account_keys.clone();
message.instructions[0].accounts.pop();
message.instructions[0].accounts.pop();
assert!(parse_bpf_upgradeable_loader(&message.instructions[0], &keys,).is_err());

let instruction = bpf_loader_upgradeable::set_upgrade_authority(
&program_address,
&current_authority_address,
None,
);
let message = Message::new(&[instruction], None);
let mut message = Message::new(&[instruction], None);
assert_eq!(
parse_bpf_upgradeable_loader(&message.instructions[0], &message.account_keys).unwrap(),
ParsedInstructionEnum {
Expand All @@ -456,6 +491,9 @@ mod test {
&message.account_keys[0..1]
)
.is_err());
let keys = message.account_keys.clone();
message.instructions[0].accounts.pop();
assert!(parse_bpf_upgradeable_loader(&message.instructions[0], &keys,).is_err());
}

#[test]
Expand All @@ -465,7 +503,7 @@ mod test {
let authority_address = Pubkey::new_unique();
let instruction =
bpf_loader_upgradeable::close(&close_address, &recipient_address, &authority_address);
let message = Message::new(&[instruction], None);
let mut message = Message::new(&[instruction], None);
assert_eq!(
parse_bpf_upgradeable_loader(&message.instructions[0], &message.account_keys).unwrap(),
ParsedInstructionEnum {
Expand All @@ -482,5 +520,8 @@ mod test {
&message.account_keys[0..1]
)
.is_err());
let keys = message.account_keys.clone();
message.instructions[0].accounts.pop();
assert!(parse_bpf_upgradeable_loader(&message.instructions[0], &keys,).is_err());
}
}
Loading

0 comments on commit 3388bdd

Please sign in to comment.