2021-12-27 09:49:32 -08:00
|
|
|
//! Successors of instruction_context_context::StackFrame, KeyedAccount and AccountInfo
|
|
|
|
|
2022-02-18 21:32:29 -08:00
|
|
|
use {
|
|
|
|
crate::{
|
|
|
|
account::{AccountSharedData, ReadableAccount, WritableAccount},
|
|
|
|
instruction::InstructionError,
|
|
|
|
lamports::LamportsError,
|
|
|
|
pubkey::Pubkey,
|
|
|
|
},
|
|
|
|
std::{
|
|
|
|
cell::{RefCell, RefMut},
|
|
|
|
collections::HashSet,
|
|
|
|
pin::Pin,
|
|
|
|
},
|
2022-01-03 14:30:56 -08:00
|
|
|
};
|
2021-12-27 09:49:32 -08:00
|
|
|
|
|
|
|
pub type TransactionAccount = (Pubkey, AccountSharedData);
|
|
|
|
|
2022-05-24 15:04:46 -07:00
|
|
|
/// Contains account meta data which varies between instruction.
|
|
|
|
///
|
|
|
|
/// It also contains indices to other structures for faster lookup.
|
2021-12-27 09:49:32 -08:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct InstructionAccount {
|
2022-05-24 15:04:46 -07:00
|
|
|
/// Points to the account and its key in the `TransactionContext`
|
2021-12-30 06:46:36 -08:00
|
|
|
pub index_in_transaction: usize,
|
2022-05-24 15:04:46 -07:00
|
|
|
/// Points to the first occurrence in the parent `InstructionContext`
|
|
|
|
///
|
2022-05-25 04:43:20 -07:00
|
|
|
/// This excludes the program accounts.
|
2021-12-30 06:46:36 -08:00
|
|
|
pub index_in_caller: usize,
|
2022-05-24 15:04:46 -07:00
|
|
|
/// Points to the first occurrence in the current `InstructionContext`
|
|
|
|
///
|
|
|
|
/// This excludes the program accounts.
|
|
|
|
pub index_in_callee: usize,
|
|
|
|
/// Is this account supposed to sign
|
2021-12-27 09:49:32 -08:00
|
|
|
pub is_signer: bool,
|
2022-05-24 15:04:46 -07:00
|
|
|
/// Is this account allowed to become writable
|
2021-12-27 09:49:32 -08:00
|
|
|
pub is_writable: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Loaded transaction shared between runtime and programs.
|
|
|
|
///
|
|
|
|
/// This context is valid for the entire duration of a transaction being processed.
|
2021-12-30 06:46:36 -08:00
|
|
|
#[derive(Debug)]
|
2021-12-27 09:49:32 -08:00
|
|
|
pub struct TransactionContext {
|
2022-01-03 14:30:56 -08:00
|
|
|
account_keys: Pin<Box<[Pubkey]>>,
|
|
|
|
accounts: Pin<Box<[RefCell<AccountSharedData>]>>,
|
2021-12-27 09:49:32 -08:00
|
|
|
instruction_context_capacity: usize,
|
2022-02-09 11:04:49 -08:00
|
|
|
instruction_stack: Vec<usize>,
|
2022-01-19 13:40:09 -08:00
|
|
|
number_of_instructions_at_transaction_level: usize,
|
2022-02-02 16:45:57 -08:00
|
|
|
instruction_trace: InstructionTrace,
|
2022-03-22 15:17:05 -07:00
|
|
|
return_data: TransactionReturnData,
|
2021-12-27 09:49:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TransactionContext {
|
|
|
|
/// Constructs a new TransactionContext
|
|
|
|
pub fn new(
|
|
|
|
transaction_accounts: Vec<TransactionAccount>,
|
|
|
|
instruction_context_capacity: usize,
|
2022-01-19 13:40:09 -08:00
|
|
|
number_of_instructions_at_transaction_level: usize,
|
2021-12-27 09:49:32 -08:00
|
|
|
) -> Self {
|
2022-01-03 14:30:56 -08:00
|
|
|
let (account_keys, accounts): (Vec<Pubkey>, Vec<RefCell<AccountSharedData>>) =
|
|
|
|
transaction_accounts
|
|
|
|
.into_iter()
|
|
|
|
.map(|(key, account)| (key, RefCell::new(account)))
|
|
|
|
.unzip();
|
2021-12-27 09:49:32 -08:00
|
|
|
Self {
|
2022-01-03 14:30:56 -08:00
|
|
|
account_keys: Pin::new(account_keys.into_boxed_slice()),
|
|
|
|
accounts: Pin::new(accounts.into_boxed_slice()),
|
2021-12-27 09:49:32 -08:00
|
|
|
instruction_context_capacity,
|
2022-02-09 11:04:49 -08:00
|
|
|
instruction_stack: Vec::with_capacity(instruction_context_capacity),
|
2022-01-19 13:40:09 -08:00
|
|
|
number_of_instructions_at_transaction_level,
|
|
|
|
instruction_trace: Vec::with_capacity(number_of_instructions_at_transaction_level),
|
2022-03-22 15:17:05 -07:00
|
|
|
return_data: TransactionReturnData::default(),
|
2021-12-27 09:49:32 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Used in mock_process_instruction
|
|
|
|
pub fn deconstruct_without_keys(self) -> Result<Vec<AccountSharedData>, InstructionError> {
|
2022-02-09 11:04:49 -08:00
|
|
|
if !self.instruction_stack.is_empty() {
|
2021-12-27 09:49:32 -08:00
|
|
|
return Err(InstructionError::CallDepth);
|
|
|
|
}
|
2022-01-03 14:30:56 -08:00
|
|
|
Ok(Vec::from(Pin::into_inner(self.accounts))
|
2021-12-27 09:49:32 -08:00
|
|
|
.into_iter()
|
|
|
|
.map(|account| account.into_inner())
|
|
|
|
.collect())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the total number of accounts loaded in this Transaction
|
|
|
|
pub fn get_number_of_accounts(&self) -> usize {
|
|
|
|
self.accounts.len()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Searches for an account by its key
|
2022-02-03 02:34:51 -08:00
|
|
|
pub fn get_key_of_account_at_index(
|
|
|
|
&self,
|
|
|
|
index_in_transaction: usize,
|
|
|
|
) -> Result<&Pubkey, InstructionError> {
|
|
|
|
self.account_keys
|
|
|
|
.get(index_in_transaction)
|
|
|
|
.ok_or(InstructionError::NotEnoughAccountKeys)
|
2021-12-27 09:49:32 -08:00
|
|
|
}
|
|
|
|
|
2022-03-21 05:53:32 -07:00
|
|
|
/// Returns the keys for the accounts loaded in this Transaction
|
|
|
|
pub fn get_keys_of_accounts(&self) -> &[Pubkey] {
|
|
|
|
&self.account_keys
|
|
|
|
}
|
|
|
|
|
2021-12-27 09:49:32 -08:00
|
|
|
/// Searches for an account by its key
|
2022-02-03 02:34:51 -08:00
|
|
|
pub fn get_account_at_index(
|
|
|
|
&self,
|
|
|
|
index_in_transaction: usize,
|
|
|
|
) -> Result<&RefCell<AccountSharedData>, InstructionError> {
|
|
|
|
self.accounts
|
|
|
|
.get(index_in_transaction)
|
|
|
|
.ok_or(InstructionError::NotEnoughAccountKeys)
|
2021-12-27 09:49:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Searches for an account by its key
|
|
|
|
pub fn find_index_of_account(&self, pubkey: &Pubkey) -> Option<usize> {
|
|
|
|
self.account_keys.iter().position(|key| key == pubkey)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Searches for a program account by its key
|
|
|
|
pub fn find_index_of_program_account(&self, pubkey: &Pubkey) -> Option<usize> {
|
|
|
|
self.account_keys.iter().rposition(|key| key == pubkey)
|
|
|
|
}
|
|
|
|
|
2022-04-12 17:52:47 -07:00
|
|
|
/// Gets an InstructionContext by its nesting level in the stack
|
|
|
|
pub fn get_instruction_context_at(
|
2021-12-27 09:49:32 -08:00
|
|
|
&self,
|
2021-12-30 06:46:36 -08:00
|
|
|
level: usize,
|
2022-04-12 17:52:47 -07:00
|
|
|
) -> Result<&InstructionContext, InstructionError> {
|
2022-02-09 11:04:49 -08:00
|
|
|
let top_level_index = *self
|
|
|
|
.instruction_stack
|
2022-06-21 13:26:51 -07:00
|
|
|
.first()
|
2022-02-09 11:04:49 -08:00
|
|
|
.ok_or(InstructionError::CallDepth)?;
|
|
|
|
let cpi_index = if level == 0 {
|
|
|
|
0
|
|
|
|
} else {
|
|
|
|
*self
|
|
|
|
.instruction_stack
|
|
|
|
.get(level)
|
|
|
|
.ok_or(InstructionError::CallDepth)?
|
|
|
|
};
|
|
|
|
let instruction_context = self
|
|
|
|
.instruction_trace
|
|
|
|
.get(top_level_index)
|
|
|
|
.and_then(|instruction_trace| instruction_trace.get(cpi_index))
|
|
|
|
.ok_or(InstructionError::CallDepth)?;
|
|
|
|
debug_assert_eq!(instruction_context.nesting_level, level);
|
|
|
|
Ok(instruction_context)
|
2021-12-27 09:49:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the max height of the InstructionContext stack
|
|
|
|
pub fn get_instruction_context_capacity(&self) -> usize {
|
|
|
|
self.instruction_context_capacity
|
|
|
|
}
|
|
|
|
|
2022-02-02 16:45:57 -08:00
|
|
|
/// Gets instruction stack height, top-level instructions are height
|
|
|
|
/// `solana_sdk::instruction::TRANSACTION_LEVEL_STACK_HEIGHT`
|
2021-12-30 06:46:36 -08:00
|
|
|
pub fn get_instruction_context_stack_height(&self) -> usize {
|
2022-02-09 11:04:49 -08:00
|
|
|
self.instruction_stack.len()
|
2021-12-27 09:49:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the current InstructionContext
|
|
|
|
pub fn get_current_instruction_context(&self) -> Result<&InstructionContext, InstructionError> {
|
2021-12-30 06:46:36 -08:00
|
|
|
let level = self
|
2022-02-09 11:04:49 -08:00
|
|
|
.get_instruction_context_stack_height()
|
2021-12-30 06:46:36 -08:00
|
|
|
.checked_sub(1)
|
|
|
|
.ok_or(InstructionError::CallDepth)?;
|
|
|
|
self.get_instruction_context_at(level)
|
2021-12-27 09:49:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Pushes a new InstructionContext
|
|
|
|
pub fn push(
|
|
|
|
&mut self,
|
2021-12-30 06:46:36 -08:00
|
|
|
program_accounts: &[usize],
|
2021-12-27 09:49:32 -08:00
|
|
|
instruction_accounts: &[InstructionAccount],
|
2021-12-30 06:46:36 -08:00
|
|
|
instruction_data: &[u8],
|
2022-02-09 11:04:49 -08:00
|
|
|
record_instruction_in_transaction_context_push: bool,
|
2021-12-27 09:49:32 -08:00
|
|
|
) -> Result<(), InstructionError> {
|
2022-02-09 11:04:49 -08:00
|
|
|
let index_in_trace = if self.instruction_stack.is_empty() {
|
2022-01-19 13:40:09 -08:00
|
|
|
debug_assert!(
|
|
|
|
self.instruction_trace.len() < self.number_of_instructions_at_transaction_level
|
|
|
|
);
|
2022-02-09 11:04:49 -08:00
|
|
|
let instruction_context = InstructionContext {
|
|
|
|
nesting_level: self.instruction_stack.len(),
|
|
|
|
program_accounts: program_accounts.to_vec(),
|
|
|
|
instruction_accounts: instruction_accounts.to_vec(),
|
|
|
|
instruction_data: instruction_data.to_vec(),
|
|
|
|
};
|
|
|
|
self.instruction_trace.push(vec![instruction_context]);
|
|
|
|
self.instruction_trace.len().saturating_sub(1)
|
|
|
|
} else if let Some(instruction_trace) = self.instruction_trace.last_mut() {
|
|
|
|
if record_instruction_in_transaction_context_push {
|
|
|
|
let instruction_context = InstructionContext {
|
|
|
|
nesting_level: self.instruction_stack.len(),
|
|
|
|
program_accounts: program_accounts.to_vec(),
|
|
|
|
instruction_accounts: instruction_accounts.to_vec(),
|
|
|
|
instruction_data: instruction_data.to_vec(),
|
|
|
|
};
|
|
|
|
instruction_trace.push(instruction_context);
|
|
|
|
}
|
|
|
|
instruction_trace.len().saturating_sub(1)
|
|
|
|
} else {
|
|
|
|
return Err(InstructionError::CallDepth);
|
|
|
|
};
|
|
|
|
if self.instruction_stack.len() >= self.instruction_context_capacity {
|
|
|
|
return Err(InstructionError::CallDepth);
|
2022-01-19 13:40:09 -08:00
|
|
|
}
|
2022-02-09 11:04:49 -08:00
|
|
|
self.instruction_stack.push(index_in_trace);
|
2021-12-27 09:49:32 -08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Pops the current InstructionContext
|
|
|
|
pub fn pop(&mut self) -> Result<(), InstructionError> {
|
2022-02-09 11:04:49 -08:00
|
|
|
if self.instruction_stack.is_empty() {
|
2021-12-27 09:49:32 -08:00
|
|
|
return Err(InstructionError::CallDepth);
|
|
|
|
}
|
2022-02-09 11:04:49 -08:00
|
|
|
self.instruction_stack.pop();
|
2021-12-27 09:49:32 -08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the return data of the current InstructionContext or any above
|
|
|
|
pub fn get_return_data(&self) -> (&Pubkey, &[u8]) {
|
2022-03-22 15:17:05 -07:00
|
|
|
(&self.return_data.program_id, &self.return_data.data)
|
2021-12-27 09:49:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the return data of the current InstructionContext
|
2022-01-10 09:26:51 -08:00
|
|
|
pub fn set_return_data(
|
|
|
|
&mut self,
|
|
|
|
program_id: Pubkey,
|
|
|
|
data: Vec<u8>,
|
|
|
|
) -> Result<(), InstructionError> {
|
2022-03-22 15:17:05 -07:00
|
|
|
self.return_data = TransactionReturnData { program_id, data };
|
2021-12-27 09:49:32 -08:00
|
|
|
Ok(())
|
|
|
|
}
|
2022-01-19 13:40:09 -08:00
|
|
|
|
|
|
|
/// Used by the runtime when a new CPI instruction begins
|
2022-02-09 11:04:49 -08:00
|
|
|
///
|
|
|
|
/// Deprecated, automatically done in push()
|
|
|
|
/// once record_instruction_in_transaction_context_push is activated.
|
|
|
|
pub fn record_instruction(&mut self, instruction: InstructionContext) {
|
2022-01-19 13:40:09 -08:00
|
|
|
if let Some(records) = self.instruction_trace.last_mut() {
|
2022-02-09 11:04:49 -08:00
|
|
|
records.push(instruction);
|
2022-01-19 13:40:09 -08:00
|
|
|
}
|
|
|
|
}
|
2022-02-02 16:45:57 -08:00
|
|
|
|
|
|
|
/// Returns instruction trace
|
|
|
|
pub fn get_instruction_trace(&self) -> &InstructionTrace {
|
|
|
|
&self.instruction_trace
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:17:05 -07:00
|
|
|
/// Return data at the end of a transaction
|
2022-05-22 18:00:42 -07:00
|
|
|
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Eq, Serialize)]
|
2022-03-22 15:17:05 -07:00
|
|
|
pub struct TransactionReturnData {
|
|
|
|
pub program_id: Pubkey,
|
|
|
|
pub data: Vec<u8>,
|
|
|
|
}
|
|
|
|
|
2022-02-02 16:45:57 -08:00
|
|
|
/// List of (stack height, instruction) for each top-level instruction
|
2022-02-09 11:04:49 -08:00
|
|
|
pub type InstructionTrace = Vec<Vec<InstructionContext>>;
|
2022-02-02 16:45:57 -08:00
|
|
|
|
2021-12-27 09:49:32 -08:00
|
|
|
/// Loaded instruction shared between runtime and programs.
|
|
|
|
///
|
|
|
|
/// This context is valid for the entire duration of a (possibly cross program) instruction being processed.
|
2022-02-02 16:45:57 -08:00
|
|
|
#[derive(Debug, Clone)]
|
2021-12-27 09:49:32 -08:00
|
|
|
pub struct InstructionContext {
|
2022-02-09 11:04:49 -08:00
|
|
|
nesting_level: usize,
|
2021-12-30 06:46:36 -08:00
|
|
|
program_accounts: Vec<usize>,
|
|
|
|
instruction_accounts: Vec<InstructionAccount>,
|
2021-12-27 09:49:32 -08:00
|
|
|
instruction_data: Vec<u8>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl InstructionContext {
|
2022-02-02 16:45:57 -08:00
|
|
|
/// New
|
|
|
|
pub fn new(
|
2022-02-09 11:04:49 -08:00
|
|
|
nesting_level: usize,
|
2022-02-02 16:45:57 -08:00
|
|
|
program_accounts: &[usize],
|
|
|
|
instruction_accounts: &[InstructionAccount],
|
|
|
|
instruction_data: &[u8],
|
|
|
|
) -> Self {
|
|
|
|
InstructionContext {
|
2022-02-09 11:04:49 -08:00
|
|
|
nesting_level,
|
2022-02-02 16:45:57 -08:00
|
|
|
program_accounts: program_accounts.to_vec(),
|
|
|
|
instruction_accounts: instruction_accounts.to_vec(),
|
|
|
|
instruction_data: instruction_data.to_vec(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-09 11:04:49 -08:00
|
|
|
/// How many Instructions were on the stack after this one was pushed
|
|
|
|
///
|
|
|
|
/// That is the number of nested parent Instructions plus one (itself).
|
|
|
|
pub fn get_stack_height(&self) -> usize {
|
|
|
|
self.nesting_level.saturating_add(1)
|
|
|
|
}
|
|
|
|
|
2021-12-27 09:49:32 -08:00
|
|
|
/// Number of program accounts
|
|
|
|
pub fn get_number_of_program_accounts(&self) -> usize {
|
2021-12-30 06:46:36 -08:00
|
|
|
self.program_accounts.len()
|
2021-12-27 09:49:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Number of accounts in this Instruction (without program accounts)
|
|
|
|
pub fn get_number_of_instruction_accounts(&self) -> usize {
|
2021-12-30 06:46:36 -08:00
|
|
|
self.instruction_accounts.len()
|
2021-12-27 09:49:32 -08:00
|
|
|
}
|
|
|
|
|
2022-02-18 17:06:33 -08:00
|
|
|
/// 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(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-27 09:49:32 -08:00
|
|
|
/// Data parameter for the programs `process_instruction` handler
|
|
|
|
pub fn get_instruction_data(&self) -> &[u8] {
|
|
|
|
&self.instruction_data
|
|
|
|
}
|
|
|
|
|
2021-12-30 06:46:36 -08:00
|
|
|
/// Searches for a program account by its key
|
|
|
|
pub fn find_index_of_program_account(
|
|
|
|
&self,
|
|
|
|
transaction_context: &TransactionContext,
|
|
|
|
pubkey: &Pubkey,
|
|
|
|
) -> Option<usize> {
|
|
|
|
self.program_accounts
|
|
|
|
.iter()
|
|
|
|
.position(|index_in_transaction| {
|
|
|
|
&transaction_context.account_keys[*index_in_transaction] == pubkey
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-06-16 09:46:17 -07:00
|
|
|
/// Searches for an instruction account by its key
|
|
|
|
pub fn find_index_of_instruction_account(
|
2021-12-30 06:46:36 -08:00
|
|
|
&self,
|
|
|
|
transaction_context: &TransactionContext,
|
|
|
|
pubkey: &Pubkey,
|
|
|
|
) -> Option<usize> {
|
|
|
|
self.instruction_accounts
|
|
|
|
.iter()
|
|
|
|
.position(|instruction_account| {
|
|
|
|
&transaction_context.account_keys[instruction_account.index_in_transaction]
|
|
|
|
== pubkey
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-06-16 09:46:17 -07:00
|
|
|
/// Translates the given instruction wide program_account_index into a transaction wide index
|
|
|
|
pub fn get_index_of_program_account_in_transaction(
|
2022-01-03 14:30:56 -08:00
|
|
|
&self,
|
2022-06-16 09:46:17 -07:00
|
|
|
program_account_index: usize,
|
|
|
|
) -> Result<usize, InstructionError> {
|
|
|
|
Ok(*self
|
|
|
|
.program_accounts
|
|
|
|
.get(program_account_index)
|
|
|
|
.ok_or(InstructionError::NotEnoughAccountKeys)?)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Translates the given instruction wide instruction_account_index into a transaction wide index
|
|
|
|
pub fn get_index_of_instruction_account_in_transaction(
|
|
|
|
&self,
|
|
|
|
instruction_account_index: usize,
|
2022-01-03 14:30:56 -08:00
|
|
|
) -> Result<usize, InstructionError> {
|
2022-06-16 09:46:17 -07:00
|
|
|
Ok(self
|
|
|
|
.instruction_accounts
|
|
|
|
.get(instruction_account_index)
|
|
|
|
.ok_or(InstructionError::NotEnoughAccountKeys)?
|
2022-01-03 14:30:56 -08:00
|
|
|
.index_in_transaction)
|
|
|
|
}
|
|
|
|
|
2022-06-16 09:46:17 -07:00
|
|
|
/// Returns `Some(instruction_account_index)` if this is a duplicate
|
2022-05-24 15:04:46 -07:00
|
|
|
/// and `None` if it is the first account with this key
|
2022-06-16 09:46:17 -07:00
|
|
|
pub fn is_instruction_account_duplicate(
|
2022-05-24 15:04:46 -07:00
|
|
|
&self,
|
2022-06-16 09:46:17 -07:00
|
|
|
instruction_account_index: usize,
|
2022-05-24 15:04:46 -07:00
|
|
|
) -> Result<Option<usize>, InstructionError> {
|
2022-06-16 09:46:17 -07:00
|
|
|
let index_in_callee = self
|
|
|
|
.instruction_accounts
|
|
|
|
.get(instruction_account_index)
|
|
|
|
.ok_or(InstructionError::NotEnoughAccountKeys)?
|
|
|
|
.index_in_callee;
|
|
|
|
Ok(if index_in_callee == instruction_account_index {
|
|
|
|
None
|
2022-05-24 15:04:46 -07:00
|
|
|
} else {
|
2022-06-16 09:46:17 -07:00
|
|
|
Some(index_in_callee)
|
|
|
|
})
|
2022-05-24 15:04:46 -07:00
|
|
|
}
|
|
|
|
|
2022-02-18 17:06:33 -08:00
|
|
|
/// Gets the key of the last program account of this Instruction
|
2022-06-16 09:46:17 -07:00
|
|
|
pub fn get_last_program_key<'a, 'b: 'a>(
|
2022-02-18 17:06:33 -08:00
|
|
|
&'a self,
|
|
|
|
transaction_context: &'b TransactionContext,
|
|
|
|
) -> Result<&'b Pubkey, InstructionError> {
|
2022-06-16 09:46:17 -07:00
|
|
|
let result = self
|
|
|
|
.get_index_of_program_account_in_transaction(
|
|
|
|
self.program_accounts.len().saturating_sub(1),
|
|
|
|
)
|
|
|
|
.and_then(|index_in_transaction| {
|
|
|
|
transaction_context.get_key_of_account_at_index(index_in_transaction)
|
|
|
|
});
|
|
|
|
debug_assert!(result.is_ok());
|
|
|
|
result
|
2022-02-18 17:06:33 -08:00
|
|
|
}
|
|
|
|
|
2022-06-16 09:46:17 -07:00
|
|
|
fn try_borrow_account<'a, 'b: 'a>(
|
2021-12-27 09:49:32 -08:00
|
|
|
&'a self,
|
|
|
|
transaction_context: &'b TransactionContext,
|
2022-06-16 09:46:17 -07:00
|
|
|
index_in_transaction: usize,
|
2021-12-27 09:49:32 -08:00
|
|
|
index_in_instruction: usize,
|
|
|
|
) -> Result<BorrowedAccount<'a>, InstructionError> {
|
2022-06-16 09:46:17 -07:00
|
|
|
let account = transaction_context
|
|
|
|
.accounts
|
|
|
|
.get(index_in_transaction)
|
|
|
|
.ok_or(InstructionError::MissingAccount)?
|
2021-12-27 09:49:32 -08:00
|
|
|
.try_borrow_mut()
|
|
|
|
.map_err(|_| InstructionError::AccountBorrowFailed)?;
|
|
|
|
Ok(BorrowedAccount {
|
|
|
|
transaction_context,
|
|
|
|
instruction_context: self,
|
|
|
|
index_in_transaction,
|
|
|
|
index_in_instruction,
|
|
|
|
account,
|
|
|
|
})
|
|
|
|
}
|
2021-12-30 06:46:36 -08:00
|
|
|
|
2022-01-05 00:39:37 -08:00
|
|
|
/// Gets the last program account of this Instruction
|
2022-06-16 09:46:17 -07:00
|
|
|
pub fn try_borrow_last_program_account<'a, 'b: 'a>(
|
|
|
|
&'a self,
|
|
|
|
transaction_context: &'b TransactionContext,
|
|
|
|
) -> Result<BorrowedAccount<'a>, InstructionError> {
|
|
|
|
let result = self.try_borrow_program_account(
|
|
|
|
transaction_context,
|
|
|
|
self.program_accounts.len().saturating_sub(1),
|
|
|
|
);
|
|
|
|
debug_assert!(result.is_ok());
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Tries to borrow a program account from this Instruction
|
2021-12-30 06:46:36 -08:00
|
|
|
pub fn try_borrow_program_account<'a, 'b: 'a>(
|
|
|
|
&'a self,
|
|
|
|
transaction_context: &'b TransactionContext,
|
2022-06-16 09:46:17 -07:00
|
|
|
program_account_index: usize,
|
2021-12-30 06:46:36 -08:00
|
|
|
) -> Result<BorrowedAccount<'a>, InstructionError> {
|
2022-06-16 09:46:17 -07:00
|
|
|
let index_in_transaction =
|
|
|
|
self.get_index_of_program_account_in_transaction(program_account_index)?;
|
2021-12-30 06:46:36 -08:00
|
|
|
self.try_borrow_account(
|
|
|
|
transaction_context,
|
2022-06-16 09:46:17 -07:00
|
|
|
index_in_transaction,
|
|
|
|
program_account_index,
|
2021-12-30 06:46:36 -08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-01-05 00:39:37 -08:00
|
|
|
/// Gets an instruction account of this Instruction
|
2021-12-30 06:46:36 -08:00
|
|
|
pub fn try_borrow_instruction_account<'a, 'b: 'a>(
|
|
|
|
&'a self,
|
|
|
|
transaction_context: &'b TransactionContext,
|
2022-02-18 17:06:33 -08:00
|
|
|
instruction_account_index: usize,
|
2021-12-30 06:46:36 -08:00
|
|
|
) -> Result<BorrowedAccount<'a>, InstructionError> {
|
2022-06-16 09:46:17 -07:00
|
|
|
let index_in_transaction =
|
|
|
|
self.get_index_of_instruction_account_in_transaction(instruction_account_index)?;
|
2021-12-30 06:46:36 -08:00
|
|
|
self.try_borrow_account(
|
|
|
|
transaction_context,
|
2022-06-16 09:46:17 -07:00
|
|
|
index_in_transaction,
|
2021-12-30 06:46:36 -08:00
|
|
|
self.program_accounts
|
|
|
|
.len()
|
2022-02-18 17:06:33 -08:00
|
|
|
.saturating_add(instruction_account_index),
|
2021-12-30 06:46:36 -08:00
|
|
|
)
|
|
|
|
}
|
2022-01-05 00:39:37 -08:00
|
|
|
|
2022-06-16 09:46:17 -07:00
|
|
|
/// Returns whether an instruction account is a signer
|
|
|
|
pub fn is_instruction_account_signer(
|
|
|
|
&self,
|
|
|
|
instruction_account_index: usize,
|
|
|
|
) -> Result<bool, InstructionError> {
|
|
|
|
Ok(self
|
|
|
|
.instruction_accounts
|
|
|
|
.get(instruction_account_index)
|
|
|
|
.ok_or(InstructionError::MissingAccount)?
|
|
|
|
.is_signer)
|
2022-02-02 16:45:57 -08:00
|
|
|
}
|
|
|
|
|
2022-06-16 09:46:17 -07:00
|
|
|
/// Returns whether an instruction account is writable
|
|
|
|
pub fn is_instruction_account_writable(
|
|
|
|
&self,
|
|
|
|
instruction_account_index: usize,
|
|
|
|
) -> Result<bool, InstructionError> {
|
|
|
|
Ok(self
|
|
|
|
.instruction_accounts
|
|
|
|
.get(instruction_account_index)
|
|
|
|
.ok_or(InstructionError::MissingAccount)?
|
|
|
|
.is_writable)
|
2022-02-03 08:19:42 -08:00
|
|
|
}
|
|
|
|
|
2022-06-16 09:46:17 -07:00
|
|
|
/// Calculates the set of all keys of signer instruction accounts in this Instruction
|
2022-02-03 08:19:42 -08:00
|
|
|
pub fn get_signers(&self, transaction_context: &TransactionContext) -> HashSet<Pubkey> {
|
|
|
|
let mut result = HashSet::new();
|
|
|
|
for instruction_account in self.instruction_accounts.iter() {
|
|
|
|
if instruction_account.is_signer {
|
|
|
|
result.insert(
|
|
|
|
transaction_context.account_keys[instruction_account.index_in_transaction],
|
|
|
|
);
|
|
|
|
}
|
2022-02-02 16:45:57 -08:00
|
|
|
}
|
2022-02-03 08:19:42 -08:00
|
|
|
result
|
2022-02-02 16:45:57 -08:00
|
|
|
}
|
2021-12-27 09:49:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Shared account borrowed from the TransactionContext and an InstructionContext.
|
2021-12-30 06:46:36 -08:00
|
|
|
#[derive(Debug)]
|
2021-12-27 09:49:32 -08:00
|
|
|
pub struct BorrowedAccount<'a> {
|
|
|
|
transaction_context: &'a TransactionContext,
|
|
|
|
instruction_context: &'a InstructionContext,
|
|
|
|
index_in_transaction: usize,
|
|
|
|
index_in_instruction: usize,
|
|
|
|
account: RefMut<'a, AccountSharedData>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> BorrowedAccount<'a> {
|
2021-12-30 06:46:36 -08:00
|
|
|
/// Returns the index of this account (transaction wide)
|
|
|
|
pub fn get_index_in_transaction(&self) -> usize {
|
|
|
|
self.index_in_transaction
|
|
|
|
}
|
|
|
|
|
2021-12-27 09:49:32 -08:00
|
|
|
/// Returns the public key of this account (transaction wide)
|
|
|
|
pub fn get_key(&self) -> &Pubkey {
|
|
|
|
&self.transaction_context.account_keys[self.index_in_transaction]
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the owner of this account (transaction wide)
|
|
|
|
pub fn get_owner(&self) -> &Pubkey {
|
|
|
|
self.account.owner()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Assignes the owner of this account (transaction wide)
|
2022-05-21 08:47:09 -07:00
|
|
|
pub fn set_owner(&mut self, pubkey: &[u8]) -> Result<(), InstructionError> {
|
2022-01-03 14:30:56 -08:00
|
|
|
self.account.copy_into_owner_from_slice(pubkey);
|
2022-05-21 08:47:09 -07:00
|
|
|
Ok(())
|
2021-12-27 09:49:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the number of lamports of this account (transaction wide)
|
|
|
|
pub fn get_lamports(&self) -> u64 {
|
|
|
|
self.account.lamports()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Overwrites the number of lamports of this account (transaction wide)
|
2022-05-21 08:47:09 -07:00
|
|
|
pub fn set_lamports(&mut self, lamports: u64) -> Result<(), InstructionError> {
|
2021-12-27 09:49:32 -08:00
|
|
|
self.account.set_lamports(lamports);
|
2022-05-21 08:47:09 -07:00
|
|
|
Ok(())
|
2021-12-27 09:49:32 -08:00
|
|
|
}
|
|
|
|
|
2021-12-30 06:46:36 -08:00
|
|
|
/// Adds lamports to this account (transaction wide)
|
|
|
|
pub fn checked_add_lamports(&mut self, lamports: u64) -> Result<(), InstructionError> {
|
|
|
|
self.set_lamports(
|
|
|
|
self.get_lamports()
|
|
|
|
.checked_add(lamports)
|
|
|
|
.ok_or(LamportsError::ArithmeticOverflow)?,
|
2022-05-21 08:47:09 -07:00
|
|
|
)
|
2021-12-30 06:46:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Subtracts lamports from this account (transaction wide)
|
|
|
|
pub fn checked_sub_lamports(&mut self, lamports: u64) -> Result<(), InstructionError> {
|
|
|
|
self.set_lamports(
|
|
|
|
self.get_lamports()
|
|
|
|
.checked_sub(lamports)
|
|
|
|
.ok_or(LamportsError::ArithmeticUnderflow)?,
|
2022-05-21 08:47:09 -07:00
|
|
|
)
|
2022-01-05 00:39:37 -08:00
|
|
|
}
|
|
|
|
|
2021-12-27 09:49:32 -08:00
|
|
|
/// Returns a read-only slice of the account data (transaction wide)
|
|
|
|
pub fn get_data(&self) -> &[u8] {
|
|
|
|
self.account.data()
|
|
|
|
}
|
|
|
|
|
2022-02-03 08:19:42 -08:00
|
|
|
/// Returns a writable slice of the account data (transaction wide)
|
2022-05-21 08:47:09 -07:00
|
|
|
pub fn get_data_mut(&mut self) -> Result<&mut [u8], InstructionError> {
|
|
|
|
Ok(self.account.data_as_mut_slice())
|
2022-02-03 08:19:42 -08:00
|
|
|
}
|
|
|
|
|
2022-01-03 14:30:56 -08:00
|
|
|
/// Overwrites the account data and size (transaction wide)
|
2022-05-21 08:47:09 -07:00
|
|
|
pub fn set_data(&mut self, data: &[u8]) -> Result<(), InstructionError> {
|
2022-01-03 14:30:56 -08:00
|
|
|
if data.len() == self.account.data().len() {
|
|
|
|
self.account.data_as_mut_slice().copy_from_slice(data);
|
|
|
|
} else {
|
|
|
|
self.account.set_data_from_slice(data);
|
2021-12-30 06:46:36 -08:00
|
|
|
}
|
2022-05-21 08:47:09 -07:00
|
|
|
Ok(())
|
2021-12-30 06:46:36 -08:00
|
|
|
}
|
|
|
|
|
2022-02-03 08:19:42 -08:00
|
|
|
/// Resizes the account data (transaction wide)
|
|
|
|
///
|
|
|
|
/// Fills it with zeros at the end if is extended or truncates at the end otherwise.
|
2022-05-21 08:47:09 -07:00
|
|
|
pub fn set_data_length(&mut self, new_len: usize) -> Result<(), InstructionError> {
|
2022-02-03 08:19:42 -08:00
|
|
|
self.account.data_mut().resize(new_len, 0);
|
2022-05-21 08:47:09 -07:00
|
|
|
Ok(())
|
2022-02-03 08:19:42 -08:00
|
|
|
}
|
2022-01-03 14:30:56 -08:00
|
|
|
|
2021-12-30 06:46:36 -08:00
|
|
|
/// Deserializes the account data into a state
|
|
|
|
pub fn get_state<T: serde::de::DeserializeOwned>(&self) -> Result<T, InstructionError> {
|
|
|
|
self.account
|
|
|
|
.deserialize_data()
|
|
|
|
.map_err(|_| InstructionError::InvalidAccountData)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Serializes a state into the account data
|
|
|
|
pub fn set_state<T: serde::Serialize>(&mut self, state: &T) -> Result<(), InstructionError> {
|
|
|
|
let data = self.account.data_as_mut_slice();
|
|
|
|
let serialized_size =
|
|
|
|
bincode::serialized_size(state).map_err(|_| InstructionError::GenericError)?;
|
|
|
|
if serialized_size > data.len() as u64 {
|
|
|
|
return Err(InstructionError::AccountDataTooSmall);
|
|
|
|
}
|
2022-01-03 14:30:56 -08:00
|
|
|
bincode::serialize_into(&mut *data, state).map_err(|_| InstructionError::GenericError)?;
|
2022-02-03 08:19:42 -08:00
|
|
|
Ok(())
|
2021-12-30 06:46:36 -08:00
|
|
|
}
|
|
|
|
|
2021-12-27 09:49:32 -08:00
|
|
|
/// Returns whether this account is executable (transaction wide)
|
|
|
|
pub fn is_executable(&self) -> bool {
|
|
|
|
self.account.executable()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Configures whether this account is executable (transaction wide)
|
2022-05-21 08:47:09 -07:00
|
|
|
pub fn set_executable(&mut self, is_executable: bool) -> Result<(), InstructionError> {
|
2021-12-27 09:49:32 -08:00
|
|
|
self.account.set_executable(is_executable);
|
2022-05-21 08:47:09 -07:00
|
|
|
Ok(())
|
2022-01-03 14:30:56 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the rent epoch of this account (transaction wide)
|
|
|
|
pub fn get_rent_epoch(&self) -> u64 {
|
|
|
|
self.account.rent_epoch()
|
2021-12-27 09:49:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns whether this account is a signer (instruction wide)
|
|
|
|
pub fn is_signer(&self) -> bool {
|
2022-06-16 09:46:17 -07:00
|
|
|
if self.index_in_instruction < self.instruction_context.program_accounts.len() {
|
|
|
|
return false;
|
|
|
|
}
|
2022-02-03 08:19:42 -08:00
|
|
|
self.instruction_context
|
2022-06-16 09:46:17 -07:00
|
|
|
.is_instruction_account_signer(
|
|
|
|
self.index_in_instruction
|
|
|
|
.saturating_sub(self.instruction_context.program_accounts.len()),
|
|
|
|
)
|
2022-02-03 08:19:42 -08:00
|
|
|
.unwrap_or_default()
|
2021-12-27 09:49:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns whether this account is writable (instruction wide)
|
|
|
|
pub fn is_writable(&self) -> bool {
|
2022-06-16 09:46:17 -07:00
|
|
|
if self.index_in_instruction < self.instruction_context.program_accounts.len() {
|
|
|
|
return false;
|
|
|
|
}
|
2022-02-03 08:19:42 -08:00
|
|
|
self.instruction_context
|
2022-06-16 09:46:17 -07:00
|
|
|
.is_instruction_account_writable(
|
|
|
|
self.index_in_instruction
|
|
|
|
.saturating_sub(self.instruction_context.program_accounts.len()),
|
|
|
|
)
|
2022-02-03 08:19:42 -08:00
|
|
|
.unwrap_or_default()
|
2021-12-27 09:49:32 -08:00
|
|
|
}
|
|
|
|
}
|
2022-03-22 15:17:05 -07:00
|
|
|
|
|
|
|
/// Everything that needs to be recorded from a TransactionContext after execution
|
|
|
|
pub struct ExecutionRecord {
|
|
|
|
pub accounts: Vec<TransactionAccount>,
|
|
|
|
pub instruction_trace: InstructionTrace,
|
|
|
|
pub return_data: TransactionReturnData,
|
|
|
|
}
|
|
|
|
/// Used by the bank in the runtime to write back the processed accounts and recorded instructions
|
|
|
|
impl From<TransactionContext> for ExecutionRecord {
|
|
|
|
fn from(context: TransactionContext) -> Self {
|
|
|
|
Self {
|
|
|
|
accounts: Vec::from(Pin::into_inner(context.account_keys))
|
|
|
|
.into_iter()
|
|
|
|
.zip(
|
|
|
|
Vec::from(Pin::into_inner(context.accounts))
|
|
|
|
.into_iter()
|
|
|
|
.map(|account| account.into_inner()),
|
|
|
|
)
|
|
|
|
.collect(),
|
|
|
|
instruction_trace: context.instruction_trace,
|
|
|
|
return_data: context.return_data,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|