Add Rust BPF RefCell borrow helpers (#8047)

This commit is contained in:
Jack May
2020-01-30 20:40:27 -08:00
committed by GitHub
parent a0964bb2c2
commit 2226c1b75c
5 changed files with 61 additions and 8 deletions

View File

@ -28,7 +28,7 @@ impl From<MyError> for ProgramError {
entrypoint!(process_instruction);
fn process_instruction(
_program_id: &Pubkey,
_accounts: &[AccountInfo],
accounts: &[AccountInfo],
instruction_data: &[u8],
) -> Result<(), ProgramError> {
match instruction_data[0] {
@ -38,7 +38,7 @@ fn process_instruction(
}
2 => {
info!("return a builtin");
Err(ProgramError::AccountBorrowFailed)
Err(ProgramError::InvalidAccountData)
}
3 => {
info!("return custom error");
@ -52,6 +52,12 @@ fn process_instruction(
info!("return error that conflicts with builtin");
Err(MyError::ConflictingBuiltin.into())
}
6 => {
let data = accounts[0].try_borrow_mut_data()?;
let data2 = accounts[0].try_borrow_mut_data()?;
assert_eq!(*data, *data2);
Ok(())
}
_ => {
info!("Unrecognized command");
Err(ProgramError::InvalidInstructionData)

View File

@ -384,7 +384,7 @@ mod bpf {
let result = bank_client.send_instruction(&mint_keypair, instruction);
assert_eq!(
result.unwrap_err().unwrap(),
TransactionError::InstructionError(0, InstructionError::AccountBorrowFailed)
TransactionError::InstructionError(0, InstructionError::InvalidAccountData)
);
let instruction = Instruction::new(program_id, &3u8, account_metas.clone());
@ -413,6 +413,13 @@ mod bpf {
let instruction = Instruction::new(program_id, &6u8, account_metas.clone());
let result = bank_client.send_instruction(&mint_keypair, instruction);
assert_eq!(
result.unwrap_err().unwrap(),
TransactionError::InstructionError(0, InstructionError::AccountBorrowFailed)
);
let instruction = Instruction::new(program_id, &7u8, account_metas.clone());
let result = bank_client.send_instruction(&mint_keypair, instruction);
assert_eq!(
result.unwrap_err().unwrap(),
TransactionError::InstructionError(0, InstructionError::InvalidInstructionData)