|
|
@@ -60,10 +60,6 @@ pub type IndexOfAccount = u16;
|
|
|
pub struct InstructionAccount {
|
|
|
/// Points to the account and its key in the `TransactionContext`
|
|
|
pub index_in_transaction: IndexOfAccount,
|
|
|
- /// Points to the first occurrence in the parent `InstructionContext`
|
|
|
- ///
|
|
|
- /// This excludes the program accounts.
|
|
|
- pub index_in_caller: IndexOfAccount,
|
|
|
/// Points to the first occurrence in the current `InstructionContext`
|
|
|
///
|
|
|
/// This excludes the program accounts.
|
|
|
@@ -77,14 +73,12 @@ pub struct InstructionAccount {
|
|
|
impl InstructionAccount {
|
|
|
pub fn new(
|
|
|
index_in_transaction: IndexOfAccount,
|
|
|
- index_in_caller: IndexOfAccount,
|
|
|
index_in_callee: IndexOfAccount,
|
|
|
is_signer: bool,
|
|
|
is_writable: bool,
|
|
|
) -> InstructionAccount {
|
|
|
InstructionAccount {
|
|
|
index_in_transaction,
|
|
|
- index_in_caller,
|
|
|
index_in_callee,
|
|
|
is_signer: is_signer as u8,
|
|
|
is_writable: is_writable as u8,
|
|
|
@@ -736,6 +730,18 @@ impl InstructionContext {
|
|
|
.index_in_transaction as IndexOfAccount)
|
|
|
}
|
|
|
|
|
|
+ /// Get the index of account in instruction from the index in transaction
|
|
|
+ pub fn get_index_of_account_in_instruction(
|
|
|
+ &self,
|
|
|
+ index_in_transaction: IndexOfAccount,
|
|
|
+ ) -> Result<IndexOfAccount, InstructionError> {
|
|
|
+ self.instruction_accounts
|
|
|
+ .iter()
|
|
|
+ .position(|account| account.index_in_transaction == index_in_transaction)
|
|
|
+ .map(|idx| idx as IndexOfAccount)
|
|
|
+ .ok_or(InstructionError::MissingAccount)
|
|
|
+ }
|
|
|
+
|
|
|
/// Returns `Some(instruction_account_index)` if this is a duplicate
|
|
|
/// and `None` if it is the first account with this key
|
|
|
pub fn is_instruction_account_duplicate(
|
|
|
@@ -771,7 +777,7 @@ impl InstructionContext {
|
|
|
&'a self,
|
|
|
transaction_context: &'b TransactionContext,
|
|
|
index_in_transaction: IndexOfAccount,
|
|
|
- index_in_instruction: IndexOfAccount,
|
|
|
+ index_in_instruction: Option<IndexOfAccount>,
|
|
|
) -> Result<BorrowedAccount<'a>, InstructionError> {
|
|
|
let account = transaction_context
|
|
|
.accounts
|
|
|
@@ -783,7 +789,7 @@ impl InstructionContext {
|
|
|
transaction_context,
|
|
|
instruction_context: self,
|
|
|
index_in_transaction,
|
|
|
- index_in_instruction,
|
|
|
+ index_in_instruction_accounts: index_in_instruction,
|
|
|
account,
|
|
|
})
|
|
|
}
|
|
|
@@ -809,11 +815,7 @@ impl InstructionContext {
|
|
|
) -> Result<BorrowedAccount<'a>, InstructionError> {
|
|
|
let index_in_transaction =
|
|
|
self.get_index_of_program_account_in_transaction(program_account_index)?;
|
|
|
- self.try_borrow_account(
|
|
|
- transaction_context,
|
|
|
- index_in_transaction,
|
|
|
- program_account_index,
|
|
|
- )
|
|
|
+ self.try_borrow_account(transaction_context, index_in_transaction, None)
|
|
|
}
|
|
|
|
|
|
/// Gets an instruction account of this Instruction
|
|
|
@@ -827,8 +829,7 @@ impl InstructionContext {
|
|
|
self.try_borrow_account(
|
|
|
transaction_context,
|
|
|
index_in_transaction,
|
|
|
- self.get_number_of_program_accounts()
|
|
|
- .saturating_add(instruction_account_index),
|
|
|
+ Some(instruction_account_index),
|
|
|
)
|
|
|
}
|
|
|
|
|
|
@@ -884,7 +885,8 @@ pub struct BorrowedAccount<'a> {
|
|
|
transaction_context: &'a TransactionContext,
|
|
|
instruction_context: &'a InstructionContext,
|
|
|
index_in_transaction: IndexOfAccount,
|
|
|
- index_in_instruction: IndexOfAccount,
|
|
|
+ // Program accounts are not part of the instruction_accounts vector, and thus None
|
|
|
+ index_in_instruction_accounts: Option<IndexOfAccount>,
|
|
|
account: RefMut<'a, AccountSharedData>,
|
|
|
}
|
|
|
|
|
|
@@ -1195,28 +1197,24 @@ impl BorrowedAccount<'_> {
|
|
|
|
|
|
/// Returns whether this account is a signer (instruction wide)
|
|
|
pub fn is_signer(&self) -> bool {
|
|
|
- if self.index_in_instruction < self.instruction_context.get_number_of_program_accounts() {
|
|
|
- return false;
|
|
|
+ if let Some(index_in_instruction_accounts) = self.index_in_instruction_accounts {
|
|
|
+ self.instruction_context
|
|
|
+ .is_instruction_account_signer(index_in_instruction_accounts)
|
|
|
+ .unwrap_or_default()
|
|
|
+ } else {
|
|
|
+ false
|
|
|
}
|
|
|
- self.instruction_context
|
|
|
- .is_instruction_account_signer(
|
|
|
- self.index_in_instruction
|
|
|
- .saturating_sub(self.instruction_context.get_number_of_program_accounts()),
|
|
|
- )
|
|
|
- .unwrap_or_default()
|
|
|
}
|
|
|
|
|
|
/// Returns whether this account is writable (instruction wide)
|
|
|
pub fn is_writable(&self) -> bool {
|
|
|
- if self.index_in_instruction < self.instruction_context.get_number_of_program_accounts() {
|
|
|
- return false;
|
|
|
+ if let Some(index_in_instruction_accounts) = self.index_in_instruction_accounts {
|
|
|
+ self.instruction_context
|
|
|
+ .is_instruction_account_writable(index_in_instruction_accounts)
|
|
|
+ .unwrap_or_default()
|
|
|
+ } else {
|
|
|
+ false
|
|
|
}
|
|
|
- self.instruction_context
|
|
|
- .is_instruction_account_writable(
|
|
|
- self.index_in_instruction
|
|
|
- .saturating_sub(self.instruction_context.get_number_of_program_accounts()),
|
|
|
- )
|
|
|
- .unwrap_or_default()
|
|
|
}
|
|
|
|
|
|
/// Returns true if the owner of this account is the current `InstructionContext`s last program (instruction wide)
|