Skip to content

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 authored Jun 14, 2022
1 parent fc166da commit 7b786ff
Showing 5 changed files with 284 additions and 65 deletions.
16 changes: 14 additions & 2 deletions transaction-status/src/parse_associated_token.rs
Original file line number Diff line number Diff line change
@@ -125,7 +125,7 @@ mod test {
&convert_pubkey(mint),
);
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,
@@ -145,6 +145,12 @@ mod test {
})
}
);
compiled_instruction.accounts.pop();
assert!(parse_associated_token(
&compiled_instruction,
&AccountKeys::new(&convert_account_keys(&message), None)
)
.is_err());
}

#[test]
@@ -160,7 +166,7 @@ mod test {
&convert_pubkey(mint),
);
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,
@@ -179,5 +185,11 @@ mod test {
})
}
);
compiled_instruction.accounts.pop();
assert!(parse_associated_token(
&compiled_instruction,
&AccountKeys::new(&convert_account_keys(&message), None)
)
.is_err());
}
}
129 changes: 105 additions & 24 deletions transaction-status/src/parse_bpf_loader.rs
Original file line number Diff line number Diff line change
@@ -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(),
}),
})
}
}
}

@@ -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,
@@ -183,7 +193,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],
@@ -204,9 +214,15 @@ mod test {
&AccountKeys::new(&missing_account_keys, None)
)
.is_err());
message.instructions[0].accounts.pop();
assert!(parse_bpf_loader(
&message.instructions[0],
&AccountKeys::new(&account_keys, None)
)
.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],
@@ -225,6 +241,12 @@ mod test {
&AccountKeys::new(&missing_account_keys, None)
)
.is_err());
message.instructions[0].accounts.pop();
assert!(parse_bpf_loader(
&message.instructions[0],
&AccountKeys::new(&account_keys, None)
)
.is_err());

let bad_compiled_instruction = CompiledInstruction {
program_id_index: 3,
@@ -264,7 +286,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],
@@ -284,6 +306,14 @@ mod test {
&AccountKeys::new(&message.account_keys[0..2], None)
)
.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],
&AccountKeys::new(&keys, None)
)
.is_err());
}

#[test]
@@ -299,7 +329,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],
@@ -321,6 +351,13 @@ mod test {
&AccountKeys::new(&message.account_keys[0..1], None)
)
.is_err());
let keys = message.account_keys.clone();
message.instructions[0].accounts.pop();
assert!(parse_bpf_upgradeable_loader(
&message.instructions[0],
&AccountKeys::new(&keys, None)
)
.is_err());
}

#[test]
@@ -345,7 +382,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],
@@ -372,6 +409,13 @@ mod test {
&AccountKeys::new(&message.account_keys[0..7], None)
)
.is_err());
let keys = message.account_keys.clone();
message.instructions[1].accounts.pop();
assert!(parse_bpf_upgradeable_loader(
&message.instructions[1],
&AccountKeys::new(&keys, None)
)
.is_err());
}

#[test]
@@ -391,7 +435,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],
@@ -416,6 +460,13 @@ mod test {
&AccountKeys::new(&message.account_keys[0..6], None)
)
.is_err());
let keys = message.account_keys.clone();
message.instructions[0].accounts.pop();
assert!(parse_bpf_upgradeable_loader(
&message.instructions[0],
&AccountKeys::new(&keys, None)
)
.is_err());
}

#[test]
@@ -428,7 +479,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],
@@ -449,6 +500,14 @@ mod test {
&AccountKeys::new(&message.account_keys[0..1], None)
)
.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],
&AccountKeys::new(&keys, None)
)
.is_err());
}

#[test]
@@ -465,7 +524,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],
@@ -486,13 +545,21 @@ mod test {
&AccountKeys::new(&message.account_keys[0..1], None)
)
.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],
&AccountKeys::new(&keys, None)
)
.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],
@@ -513,6 +580,13 @@ mod test {
&AccountKeys::new(&message.account_keys[0..1], None)
)
.is_err());
let keys = message.account_keys.clone();
message.instructions[0].accounts.pop();
assert!(parse_bpf_upgradeable_loader(
&message.instructions[0],
&AccountKeys::new(&keys, None)
)
.is_err());
}

#[test]
@@ -522,7 +596,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],
@@ -543,5 +617,12 @@ mod test {
&AccountKeys::new(&message.account_keys[0..1], None)
)
.is_err());
let keys = message.account_keys.clone();
message.instructions[0].accounts.pop();
assert!(parse_bpf_upgradeable_loader(
&message.instructions[0],
&AccountKeys::new(&keys, None)
)
.is_err());
}
}
Loading

0 comments on commit 7b786ff

Please sign in to comment.