Replaces KeyedAccount by BorrowedAccount in system_instruction_processor. (#23217)

* Adds InstructionContext::check_number_of_instruction_accounts() and InstructionContext::get_instruction_account_key().
Bases check_sysvar_account() on instuction account indices.

* Adds instruction_account_indices enums to system_instruction_processor.

* Reorders parameters and adds InstructionContext.

* Replaces KeyedAccount by BorrowedAccount in system_instruction_processor (part 1).

* Replaces KeyedAccount by BorrowedAccount in system_instruction_processor (part 2).

* Replaces KeyedAccount by BorrowedAccount in system_instruction_processor (part 3).

* Replaces KeyedAccount by BorrowedAccount in system_instruction_processor (part 4).

* Replaces KeyedAccount by BorrowedAccount in system_instruction_processor (part 5).

* Code cleanup
This commit is contained in:
Alexander Meißner
2022-02-19 02:06:33 +01:00
committed by GitHub
parent 970f543ef6
commit ee7e411d68
4 changed files with 320 additions and 176 deletions

View File

@ -304,6 +304,18 @@ impl InstructionContext {
self.instruction_accounts.len()
}
/// Assert that enough account were supplied to this Instruction
pub fn check_number_of_instruction_accounts(
&self,
expected_at_least: usize,
) -> Result<(), InstructionError> {
if self.get_number_of_instruction_accounts() < expected_at_least {
Err(InstructionError::NotEnoughAccountKeys)
} else {
Ok(())
}
}
/// Number of accounts in this Instruction
pub fn get_number_of_accounts(&self) -> usize {
self.program_accounts
@ -360,6 +372,30 @@ impl InstructionContext {
}
}
/// Gets the key of the last program account of this Instruction
pub fn get_program_key<'a, 'b: 'a>(
&'a self,
transaction_context: &'b TransactionContext,
) -> Result<&'b Pubkey, InstructionError> {
let index_in_transaction =
self.get_index_in_transaction(self.program_accounts.len().saturating_sub(1))?;
transaction_context.get_key_of_account_at_index(index_in_transaction)
}
/// Gets the key of an instruction account (skipping program accounts)
pub fn get_instruction_account_key<'a, 'b: 'a>(
&'a self,
transaction_context: &'b TransactionContext,
instruction_account_index: usize,
) -> Result<&'b Pubkey, InstructionError> {
let index_in_transaction = self.get_index_in_transaction(
self.program_accounts
.len()
.saturating_add(instruction_account_index),
)?;
transaction_context.get_key_of_account_at_index(index_in_transaction)
}
/// Tries to borrow an account from this Instruction
pub fn try_borrow_account<'a, 'b: 'a>(
&'a self,
@ -382,16 +418,6 @@ impl InstructionContext {
})
}
/// Gets the key of the last program account of this Instruction
pub fn get_program_key<'a, 'b: 'a>(
&'a self,
transaction_context: &'b TransactionContext,
) -> Result<&'b Pubkey, InstructionError> {
let index_in_transaction =
self.get_index_in_transaction(self.program_accounts.len().saturating_sub(1))?;
transaction_context.get_key_of_account_at_index(index_in_transaction)
}
/// Gets the last program account of this Instruction
pub fn try_borrow_program_account<'a, 'b: 'a>(
&'a self,
@ -407,13 +433,13 @@ impl InstructionContext {
pub fn try_borrow_instruction_account<'a, 'b: 'a>(
&'a self,
transaction_context: &'b TransactionContext,
index_in_instruction: usize,
instruction_account_index: usize,
) -> Result<BorrowedAccount<'a>, InstructionError> {
self.try_borrow_account(
transaction_context,
self.program_accounts
.len()
.saturating_add(index_in_instruction),
.saturating_add(instruction_account_index),
)
}