2020-06-06 10:18:28 -07:00
|
|
|
use crate::{
|
2021-05-19 09:50:34 -07:00
|
|
|
accounts::Accounts, ancestors::Ancestors, instruction_recorder::InstructionRecorder,
|
2021-08-26 17:30:36 -07:00
|
|
|
log_collector::LogCollector, rent_collector::RentCollector,
|
2020-06-06 10:18:28 -07:00
|
|
|
};
|
|
|
|
use log::*;
|
2019-05-30 21:31:35 -07:00
|
|
|
use serde::{Deserialize, Serialize};
|
2021-06-04 07:04:31 -07:00
|
|
|
use solana_measure::measure::Measure;
|
2021-08-26 17:30:36 -07:00
|
|
|
use solana_program_runtime::{ExecuteDetailsTimings, Executors, InstructionProcessor, PreAccount};
|
2020-01-28 17:03:20 -08:00
|
|
|
use solana_sdk::{
|
2021-03-10 13:28:03 -08:00
|
|
|
account::{AccountSharedData, ReadableAccount, WritableAccount},
|
2021-07-22 10:18:51 -07:00
|
|
|
compute_budget::ComputeBudget,
|
2021-07-08 10:43:34 -07:00
|
|
|
feature_set::{
|
2021-09-28 01:13:03 -07:00
|
|
|
demote_program_write_locks, do_support_realloc, neon_evm_compute_budget,
|
2021-10-19 21:01:39 -07:00
|
|
|
prevent_calling_precompiles_as_programs, remove_native_loader, requestable_heap_size,
|
|
|
|
tx_wide_compute_cap, FeatureSet,
|
2021-07-08 10:43:34 -07:00
|
|
|
},
|
2021-07-29 01:50:20 -07:00
|
|
|
hash::Hash,
|
2021-08-26 17:30:36 -07:00
|
|
|
ic_logger_msg,
|
2021-10-21 11:57:42 -07:00
|
|
|
instruction::{AccountMeta, CompiledInstruction, Instruction, InstructionError},
|
2021-08-26 17:30:36 -07:00
|
|
|
keyed_account::{create_keyed_accounts_unified, KeyedAccount},
|
2020-01-28 17:03:20 -08:00
|
|
|
message::Message,
|
2021-09-28 23:25:08 -07:00
|
|
|
precompiles::is_precompile,
|
2020-10-28 20:21:50 -07:00
|
|
|
process_instruction::{
|
2021-07-22 10:18:51 -07:00
|
|
|
ComputeMeter, Executor, InvokeContext, InvokeContextStackFrame, Logger,
|
2020-10-28 13:16:13 -07:00
|
|
|
ProcessInstructionWithContext,
|
2020-10-28 20:21:50 -07:00
|
|
|
},
|
2020-01-28 17:03:20 -08:00
|
|
|
pubkey::Pubkey,
|
2020-04-28 14:33:56 -07:00
|
|
|
rent::Rent,
|
2021-04-06 00:08:03 -07:00
|
|
|
sysvar::instructions,
|
2020-01-28 17:03:20 -08:00
|
|
|
transaction::TransactionError,
|
|
|
|
};
|
2021-08-26 17:30:36 -07:00
|
|
|
use std::{cell::RefCell, rc::Rc, sync::Arc};
|
2019-02-18 22:26:22 -08:00
|
|
|
|
2020-08-21 15:31:19 -07:00
|
|
|
pub struct ThisComputeMeter {
|
|
|
|
remaining: u64,
|
|
|
|
}
|
|
|
|
impl ComputeMeter for ThisComputeMeter {
|
|
|
|
fn consume(&mut self, amount: u64) -> Result<(), InstructionError> {
|
2020-09-25 11:08:10 -07:00
|
|
|
let exceeded = self.remaining < amount;
|
2020-08-21 15:31:19 -07:00
|
|
|
self.remaining = self.remaining.saturating_sub(amount);
|
2020-09-25 11:08:10 -07:00
|
|
|
if exceeded {
|
2020-08-21 15:31:19 -07:00
|
|
|
return Err(InstructionError::ComputationalBudgetExceeded);
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
fn get_remaining(&self) -> u64 {
|
|
|
|
self.remaining
|
|
|
|
}
|
|
|
|
}
|
2021-10-21 11:57:42 -07:00
|
|
|
impl ThisComputeMeter {
|
|
|
|
pub fn new_ref(remaining: u64) -> Rc<RefCell<Self>> {
|
|
|
|
Rc::new(RefCell::new(Self { remaining }))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ThisLogger {
|
|
|
|
log_collector: Option<Rc<LogCollector>>,
|
|
|
|
}
|
|
|
|
impl Logger for ThisLogger {
|
|
|
|
fn log_enabled(&self) -> bool {
|
|
|
|
log_enabled!(log::Level::Info) || self.log_collector.is_some()
|
|
|
|
}
|
|
|
|
fn log(&self, message: &str) {
|
|
|
|
debug!("{}", message);
|
|
|
|
if let Some(log_collector) = &self.log_collector {
|
|
|
|
log_collector.log(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl ThisLogger {
|
|
|
|
pub fn new_ref(log_collector: Option<Rc<LogCollector>>) -> Rc<RefCell<Self>> {
|
|
|
|
Rc::new(RefCell::new(Self { log_collector }))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-29 09:57:14 -07:00
|
|
|
pub struct ThisInvokeContext<'a> {
|
2021-09-29 10:05:25 -07:00
|
|
|
instruction_index: usize,
|
2021-04-19 09:48:48 -07:00
|
|
|
invoke_stack: Vec<InvokeContextStackFrame<'a>>,
|
2020-06-13 13:20:08 -07:00
|
|
|
rent: Rent,
|
|
|
|
pre_accounts: Vec<PreAccount>,
|
2021-07-05 04:49:37 -07:00
|
|
|
accounts: &'a [(Pubkey, Rc<RefCell<AccountSharedData>>)],
|
2020-10-29 09:57:14 -07:00
|
|
|
programs: &'a [(Pubkey, ProcessInstructionWithContext)],
|
2020-06-13 13:20:08 -07:00
|
|
|
logger: Rc<RefCell<dyn Logger>>,
|
2021-07-22 10:18:51 -07:00
|
|
|
compute_budget: ComputeBudget,
|
2020-08-21 15:31:19 -07:00
|
|
|
compute_meter: Rc<RefCell<dyn ComputeMeter>>,
|
2020-09-14 17:42:37 -07:00
|
|
|
executors: Rc<RefCell<Executors>>,
|
2021-09-29 10:05:25 -07:00
|
|
|
instruction_recorders: Option<&'a [InstructionRecorder]>,
|
2020-09-29 14:36:30 -07:00
|
|
|
feature_set: Arc<FeatureSet>,
|
2021-03-03 15:07:45 -08:00
|
|
|
pub timings: ExecuteDetailsTimings,
|
2021-04-12 16:04:57 -07:00
|
|
|
account_db: Arc<Accounts>,
|
2021-10-21 11:57:42 -07:00
|
|
|
ancestors: Option<&'a Ancestors>,
|
2021-05-20 14:00:50 -07:00
|
|
|
#[allow(clippy::type_complexity)]
|
|
|
|
sysvars: RefCell<Vec<(Pubkey, Option<Rc<Vec<u8>>>)>>,
|
2021-10-21 11:57:42 -07:00
|
|
|
blockhash: Hash,
|
2021-10-22 14:32:40 -07:00
|
|
|
lamports_per_signature: u64,
|
2021-09-29 10:11:06 -07:00
|
|
|
return_data: (Pubkey, Vec<u8>),
|
2020-04-28 14:33:56 -07:00
|
|
|
}
|
2020-10-29 09:57:14 -07:00
|
|
|
impl<'a> ThisInvokeContext<'a> {
|
2020-12-22 09:26:55 -08:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2020-06-03 12:48:19 -07:00
|
|
|
pub fn new(
|
|
|
|
rent: Rent,
|
2021-07-05 04:49:37 -07:00
|
|
|
accounts: &'a [(Pubkey, Rc<RefCell<AccountSharedData>>)],
|
2020-10-29 09:57:14 -07:00
|
|
|
programs: &'a [(Pubkey, ProcessInstructionWithContext)],
|
2020-06-13 13:20:08 -07:00
|
|
|
log_collector: Option<Rc<LogCollector>>,
|
2021-07-22 10:18:51 -07:00
|
|
|
compute_budget: ComputeBudget,
|
2021-07-16 00:31:22 -07:00
|
|
|
compute_meter: Rc<RefCell<dyn ComputeMeter>>,
|
2020-09-14 17:42:37 -07:00
|
|
|
executors: Rc<RefCell<Executors>>,
|
2021-09-29 10:05:25 -07:00
|
|
|
instruction_recorders: Option<&'a [InstructionRecorder]>,
|
2020-09-29 14:36:30 -07:00
|
|
|
feature_set: Arc<FeatureSet>,
|
2021-04-12 16:04:57 -07:00
|
|
|
account_db: Arc<Accounts>,
|
2021-10-21 11:57:42 -07:00
|
|
|
ancestors: Option<&'a Ancestors>,
|
|
|
|
blockhash: Hash,
|
2021-10-22 14:32:40 -07:00
|
|
|
lamports_per_signature: u64,
|
2021-09-29 10:05:25 -07:00
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
instruction_index: 0,
|
2021-07-22 10:18:51 -07:00
|
|
|
invoke_stack: Vec::with_capacity(compute_budget.max_invoke_depth),
|
2020-04-28 14:33:56 -07:00
|
|
|
rent,
|
2021-09-29 10:05:25 -07:00
|
|
|
pre_accounts: Vec::new(),
|
2021-07-05 04:49:37 -07:00
|
|
|
accounts,
|
2020-06-03 12:48:19 -07:00
|
|
|
programs,
|
2021-10-21 11:57:42 -07:00
|
|
|
logger: ThisLogger::new_ref(log_collector),
|
2021-07-22 10:18:51 -07:00
|
|
|
compute_budget,
|
2021-07-16 00:31:22 -07:00
|
|
|
compute_meter,
|
2020-09-14 17:42:37 -07:00
|
|
|
executors,
|
2021-09-29 10:05:25 -07:00
|
|
|
instruction_recorders,
|
2020-09-29 14:36:30 -07:00
|
|
|
feature_set,
|
2021-03-03 15:07:45 -08:00
|
|
|
timings: ExecuteDetailsTimings::default(),
|
2021-04-12 16:04:57 -07:00
|
|
|
account_db,
|
|
|
|
ancestors,
|
2021-10-21 11:57:42 -07:00
|
|
|
sysvars: RefCell::new(Vec::new()),
|
2021-07-29 01:50:20 -07:00
|
|
|
blockhash,
|
2021-10-22 14:32:40 -07:00
|
|
|
lamports_per_signature,
|
2021-09-29 10:11:06 -07:00
|
|
|
return_data: (Pubkey::default(), Vec::new()),
|
2021-09-29 10:05:25 -07:00
|
|
|
}
|
2020-04-28 14:33:56 -07:00
|
|
|
}
|
2021-10-21 11:57:42 -07:00
|
|
|
|
|
|
|
pub fn new_mock_with_features(
|
|
|
|
accounts: &'a [(Pubkey, Rc<RefCell<AccountSharedData>>)],
|
|
|
|
programs: &'a [(Pubkey, ProcessInstructionWithContext)],
|
|
|
|
feature_set: Arc<FeatureSet>,
|
|
|
|
) -> Self {
|
|
|
|
Self::new(
|
|
|
|
Rent::default(),
|
|
|
|
accounts,
|
|
|
|
programs,
|
|
|
|
None,
|
|
|
|
ComputeBudget::default(),
|
|
|
|
ThisComputeMeter::new_ref(std::i64::MAX as u64),
|
|
|
|
Rc::new(RefCell::new(Executors::default())),
|
|
|
|
None,
|
|
|
|
feature_set,
|
|
|
|
Arc::new(Accounts::default_for_tests()),
|
|
|
|
None,
|
|
|
|
Hash::default(),
|
2021-10-22 14:32:40 -07:00
|
|
|
0,
|
2021-10-21 11:57:42 -07:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_mock(
|
|
|
|
accounts: &'a [(Pubkey, Rc<RefCell<AccountSharedData>>)],
|
|
|
|
programs: &'a [(Pubkey, ProcessInstructionWithContext)],
|
|
|
|
) -> Self {
|
|
|
|
Self::new_mock_with_features(accounts, programs, Arc::new(FeatureSet::all_enabled()))
|
|
|
|
}
|
2020-04-28 14:33:56 -07:00
|
|
|
}
|
2020-10-29 09:57:14 -07:00
|
|
|
impl<'a> InvokeContext for ThisInvokeContext<'a> {
|
2021-04-19 09:48:48 -07:00
|
|
|
fn push(
|
|
|
|
&mut self,
|
2021-09-09 23:36:21 -07:00
|
|
|
message: &Message,
|
|
|
|
instruction: &CompiledInstruction,
|
|
|
|
program_indices: &[usize],
|
2021-09-29 10:05:25 -07:00
|
|
|
account_indices: Option<&[usize]>,
|
2021-04-19 09:48:48 -07:00
|
|
|
) -> Result<(), InstructionError> {
|
2021-07-22 10:18:51 -07:00
|
|
|
if self.invoke_stack.len() > self.compute_budget.max_invoke_depth {
|
2020-04-28 14:33:56 -07:00
|
|
|
return Err(InstructionError::CallDepth);
|
|
|
|
}
|
2021-06-02 02:15:19 -07:00
|
|
|
|
2021-10-12 23:58:20 -07:00
|
|
|
if let Some(index_of_program_id) = program_indices.last() {
|
|
|
|
let program_id = &self.accounts[*index_of_program_id].0;
|
|
|
|
let contains = self
|
|
|
|
.invoke_stack
|
|
|
|
.iter()
|
|
|
|
.any(|frame| frame.program_id() == Some(program_id));
|
|
|
|
let is_last = if let Some(last_frame) = self.invoke_stack.last() {
|
|
|
|
last_frame.program_id() == Some(program_id)
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
|
|
|
if contains && !is_last {
|
|
|
|
// Reentrancy not allowed unless caller is calling itself
|
|
|
|
return Err(InstructionError::ReentrancyNotAllowed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-29 10:05:25 -07:00
|
|
|
if self.invoke_stack.is_empty() {
|
2021-10-21 11:57:42 -07:00
|
|
|
if !self.feature_set.is_active(&tx_wide_compute_cap::id()) {
|
|
|
|
self.compute_meter = ThisComputeMeter::new_ref(self.compute_budget.max_units);
|
|
|
|
}
|
|
|
|
|
2021-09-29 10:05:25 -07:00
|
|
|
self.pre_accounts = Vec::with_capacity(instruction.accounts.len());
|
|
|
|
let mut work = |_unique_index: usize, account_index: usize| {
|
2021-10-12 10:26:55 -07:00
|
|
|
if account_index < self.accounts.len() {
|
2021-09-29 10:05:25 -07:00
|
|
|
let account = self.accounts[account_index].1.borrow();
|
|
|
|
self.pre_accounts
|
|
|
|
.push(PreAccount::new(&self.accounts[account_index].0, &account));
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
Err(InstructionError::MissingAccount)
|
|
|
|
};
|
|
|
|
instruction.visit_each_account(&mut work)?;
|
|
|
|
}
|
|
|
|
|
2021-09-09 23:36:21 -07:00
|
|
|
// Create the KeyedAccounts that will be passed to the program
|
|
|
|
let demote_program_write_locks = self
|
|
|
|
.feature_set
|
|
|
|
.is_active(&demote_program_write_locks::id());
|
|
|
|
let keyed_accounts = program_indices
|
|
|
|
.iter()
|
|
|
|
.map(|account_index| {
|
|
|
|
(
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
&self.accounts[*account_index].0,
|
|
|
|
&self.accounts[*account_index].1 as &RefCell<AccountSharedData>,
|
|
|
|
)
|
|
|
|
})
|
2021-09-21 05:41:02 -07:00
|
|
|
.chain(instruction.accounts.iter().map(|index_in_instruction| {
|
|
|
|
let index_in_instruction = *index_in_instruction as usize;
|
2021-09-29 10:05:25 -07:00
|
|
|
let account_index = if let Some(account_indices) = account_indices {
|
|
|
|
account_indices[index_in_instruction]
|
|
|
|
} else {
|
|
|
|
index_in_instruction
|
|
|
|
};
|
2021-09-09 23:36:21 -07:00
|
|
|
(
|
2021-09-21 05:41:02 -07:00
|
|
|
message.is_signer(index_in_instruction),
|
|
|
|
message.is_writable(index_in_instruction, demote_program_write_locks),
|
|
|
|
&self.accounts[account_index].0,
|
|
|
|
&self.accounts[account_index].1 as &RefCell<AccountSharedData>,
|
2021-09-09 23:36:21 -07:00
|
|
|
)
|
|
|
|
}))
|
|
|
|
.collect::<Vec<_>>();
|
2021-10-12 23:58:20 -07:00
|
|
|
|
2021-04-19 09:48:48 -07:00
|
|
|
self.invoke_stack.push(InvokeContextStackFrame::new(
|
2021-10-12 23:58:20 -07:00
|
|
|
program_indices.len(),
|
2021-04-19 09:48:48 -07:00
|
|
|
create_keyed_accounts_unified(keyed_accounts.as_slice()),
|
|
|
|
));
|
2020-04-28 14:33:56 -07:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
fn pop(&mut self) {
|
2021-04-19 09:48:48 -07:00
|
|
|
self.invoke_stack.pop();
|
2020-04-28 14:33:56 -07:00
|
|
|
}
|
2020-11-12 12:44:37 -08:00
|
|
|
fn invoke_depth(&self) -> usize {
|
2021-04-19 09:48:48 -07:00
|
|
|
self.invoke_stack.len()
|
2020-11-12 12:44:37 -08:00
|
|
|
}
|
2021-09-29 10:05:25 -07:00
|
|
|
fn verify(
|
|
|
|
&mut self,
|
|
|
|
message: &Message,
|
|
|
|
instruction: &CompiledInstruction,
|
|
|
|
program_indices: &[usize],
|
|
|
|
) -> Result<(), InstructionError> {
|
|
|
|
let program_id = instruction.program_id(&message.account_keys);
|
|
|
|
let demote_program_write_locks = self.is_feature_active(&demote_program_write_locks::id());
|
|
|
|
let do_support_realloc = self.is_feature_active(&do_support_realloc::id());
|
|
|
|
|
|
|
|
// Verify all executable accounts have zero outstanding refs
|
|
|
|
for account_index in program_indices.iter() {
|
|
|
|
self.accounts[*account_index]
|
|
|
|
.1
|
|
|
|
.try_borrow_mut()
|
|
|
|
.map_err(|_| InstructionError::AccountBorrowOutstanding)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the per-account instruction results
|
|
|
|
let (mut pre_sum, mut post_sum) = (0_u128, 0_u128);
|
|
|
|
let mut work = |unique_index: usize, account_index: usize| {
|
|
|
|
{
|
|
|
|
// Verify account has no outstanding references
|
|
|
|
let _ = self.accounts[account_index]
|
|
|
|
.1
|
|
|
|
.try_borrow_mut()
|
|
|
|
.map_err(|_| InstructionError::AccountBorrowOutstanding)?;
|
|
|
|
}
|
|
|
|
let account = self.accounts[account_index].1.borrow();
|
|
|
|
self.pre_accounts[unique_index]
|
|
|
|
.verify(
|
|
|
|
program_id,
|
|
|
|
message.is_writable(account_index, demote_program_write_locks),
|
|
|
|
&self.rent,
|
|
|
|
&account,
|
|
|
|
&mut self.timings,
|
|
|
|
true,
|
|
|
|
do_support_realloc,
|
|
|
|
)
|
|
|
|
.map_err(|err| {
|
|
|
|
ic_logger_msg!(
|
|
|
|
self.logger,
|
|
|
|
"failed to verify account {}: {}",
|
|
|
|
self.pre_accounts[unique_index].key(),
|
|
|
|
err
|
|
|
|
);
|
|
|
|
err
|
|
|
|
})?;
|
|
|
|
pre_sum += u128::from(self.pre_accounts[unique_index].lamports());
|
|
|
|
post_sum += u128::from(account.lamports());
|
|
|
|
Ok(())
|
|
|
|
};
|
|
|
|
instruction.visit_each_account(&mut work)?;
|
|
|
|
|
|
|
|
// Verify that the total sum of all the lamports did not change
|
|
|
|
if pre_sum != post_sum {
|
|
|
|
return Err(InstructionError::UnbalancedInstruction);
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-04-28 14:33:56 -07:00
|
|
|
fn verify_and_update(
|
|
|
|
&mut self,
|
|
|
|
instruction: &CompiledInstruction,
|
2021-09-21 05:41:02 -07:00
|
|
|
account_indices: &[usize],
|
2021-07-07 07:14:00 -07:00
|
|
|
write_privileges: &[bool],
|
2020-04-28 14:33:56 -07:00
|
|
|
) -> Result<(), InstructionError> {
|
2021-09-28 01:13:03 -07:00
|
|
|
let do_support_realloc = self.feature_set.is_active(&do_support_realloc::id());
|
2021-10-12 23:58:20 -07:00
|
|
|
let program_id = self
|
2021-04-19 09:48:48 -07:00
|
|
|
.invoke_stack
|
|
|
|
.last()
|
2021-10-12 23:58:20 -07:00
|
|
|
.and_then(|frame| frame.program_id())
|
2021-04-19 09:48:48 -07:00
|
|
|
.ok_or(InstructionError::CallDepth)?;
|
2021-09-21 05:41:02 -07:00
|
|
|
let rent = &self.rent;
|
2021-09-29 10:05:25 -07:00
|
|
|
let logger = &self.logger;
|
2021-09-21 05:41:02 -07:00
|
|
|
let accounts = &self.accounts;
|
|
|
|
let pre_accounts = &mut self.pre_accounts;
|
|
|
|
let timings = &mut self.timings;
|
|
|
|
|
|
|
|
// Verify the per-account instruction results
|
|
|
|
let (mut pre_sum, mut post_sum) = (0_u128, 0_u128);
|
|
|
|
let mut work = |_unique_index: usize, index_in_instruction: usize| {
|
|
|
|
if index_in_instruction < write_privileges.len()
|
|
|
|
&& index_in_instruction < account_indices.len()
|
|
|
|
{
|
|
|
|
let account_index = account_indices[index_in_instruction];
|
|
|
|
let (key, account) = &accounts[account_index];
|
|
|
|
let is_writable = write_privileges[index_in_instruction];
|
|
|
|
// Find the matching PreAccount
|
|
|
|
for pre_account in pre_accounts.iter_mut() {
|
|
|
|
if key == pre_account.key() {
|
|
|
|
{
|
|
|
|
// Verify account has no outstanding references
|
|
|
|
let _ = account
|
|
|
|
.try_borrow_mut()
|
|
|
|
.map_err(|_| InstructionError::AccountBorrowOutstanding)?;
|
|
|
|
}
|
|
|
|
let account = account.borrow();
|
|
|
|
pre_account
|
2021-09-28 01:13:03 -07:00
|
|
|
.verify(
|
|
|
|
program_id,
|
|
|
|
is_writable,
|
|
|
|
rent,
|
|
|
|
&account,
|
|
|
|
timings,
|
|
|
|
false,
|
|
|
|
do_support_realloc,
|
|
|
|
)
|
2021-09-21 05:41:02 -07:00
|
|
|
.map_err(|err| {
|
|
|
|
ic_logger_msg!(logger, "failed to verify account {}: {}", key, err);
|
|
|
|
err
|
|
|
|
})?;
|
|
|
|
pre_sum += u128::from(pre_account.lamports());
|
|
|
|
post_sum += u128::from(account.lamports());
|
|
|
|
if is_writable && !pre_account.executable() {
|
|
|
|
pre_account.update(&account);
|
|
|
|
}
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(InstructionError::MissingAccount)
|
|
|
|
};
|
|
|
|
instruction.visit_each_account(&mut work)?;
|
|
|
|
|
|
|
|
// Verify that the total sum of all the lamports did not change
|
|
|
|
if pre_sum != post_sum {
|
|
|
|
return Err(InstructionError::UnbalancedInstruction);
|
|
|
|
}
|
|
|
|
Ok(())
|
2020-04-28 14:33:56 -07:00
|
|
|
}
|
2020-05-08 12:24:36 -07:00
|
|
|
fn get_caller(&self) -> Result<&Pubkey, InstructionError> {
|
2021-04-19 09:48:48 -07:00
|
|
|
self.invoke_stack
|
2020-05-08 12:24:36 -07:00
|
|
|
.last()
|
2021-10-12 23:58:20 -07:00
|
|
|
.and_then(|frame| frame.program_id())
|
2021-04-19 09:48:48 -07:00
|
|
|
.ok_or(InstructionError::CallDepth)
|
|
|
|
}
|
|
|
|
fn remove_first_keyed_account(&mut self) -> Result<(), InstructionError> {
|
2021-10-08 02:41:07 -07:00
|
|
|
if !self.is_feature_active(&remove_native_loader::id()) {
|
|
|
|
let stack_frame = &mut self
|
|
|
|
.invoke_stack
|
|
|
|
.last_mut()
|
|
|
|
.ok_or(InstructionError::CallDepth)?;
|
|
|
|
stack_frame.keyed_accounts_range.start =
|
|
|
|
stack_frame.keyed_accounts_range.start.saturating_add(1);
|
|
|
|
}
|
2021-04-19 09:48:48 -07:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
fn get_keyed_accounts(&self) -> Result<&[KeyedAccount], InstructionError> {
|
|
|
|
self.invoke_stack
|
|
|
|
.last()
|
|
|
|
.map(|frame| &frame.keyed_accounts[frame.keyed_accounts_range.clone()])
|
|
|
|
.ok_or(InstructionError::CallDepth)
|
2020-05-08 12:24:36 -07:00
|
|
|
}
|
2020-10-28 20:21:50 -07:00
|
|
|
fn get_programs(&self) -> &[(Pubkey, ProcessInstructionWithContext)] {
|
2020-10-29 09:57:14 -07:00
|
|
|
self.programs
|
2020-06-03 12:48:19 -07:00
|
|
|
}
|
2020-06-13 13:20:08 -07:00
|
|
|
fn get_logger(&self) -> Rc<RefCell<dyn Logger>> {
|
|
|
|
self.logger.clone()
|
|
|
|
}
|
2020-08-21 15:31:19 -07:00
|
|
|
fn get_compute_meter(&self) -> Rc<RefCell<dyn ComputeMeter>> {
|
|
|
|
self.compute_meter.clone()
|
|
|
|
}
|
2020-10-29 23:43:10 -07:00
|
|
|
fn add_executor(&self, pubkey: &Pubkey, executor: Arc<dyn Executor>) {
|
2020-09-14 17:42:37 -07:00
|
|
|
self.executors.borrow_mut().insert(*pubkey, executor);
|
|
|
|
}
|
2020-10-29 23:43:10 -07:00
|
|
|
fn get_executor(&self, pubkey: &Pubkey) -> Option<Arc<dyn Executor>> {
|
2021-06-18 06:34:46 -07:00
|
|
|
self.executors.borrow().get(pubkey)
|
2020-09-14 17:42:37 -07:00
|
|
|
}
|
2021-09-29 10:05:25 -07:00
|
|
|
fn set_instruction_index(&mut self, instruction_index: usize) {
|
|
|
|
self.instruction_index = instruction_index;
|
|
|
|
}
|
2020-09-24 07:36:22 -07:00
|
|
|
fn record_instruction(&self, instruction: &Instruction) {
|
2021-09-29 10:05:25 -07:00
|
|
|
if let Some(instruction_recorders) = &self.instruction_recorders {
|
|
|
|
instruction_recorders[self.instruction_index].record_instruction(instruction.clone());
|
2020-09-24 07:36:22 -07:00
|
|
|
}
|
|
|
|
}
|
2020-09-29 14:36:30 -07:00
|
|
|
fn is_feature_active(&self, feature_id: &Pubkey) -> bool {
|
|
|
|
self.feature_set.is_active(feature_id)
|
|
|
|
}
|
2021-09-09 23:36:21 -07:00
|
|
|
fn get_account(&self, pubkey: &Pubkey) -> Option<(usize, Rc<RefCell<AccountSharedData>>)> {
|
|
|
|
for (index, (key, account)) in self.accounts.iter().enumerate().rev() {
|
2021-05-28 09:50:25 -07:00
|
|
|
if key == pubkey {
|
2021-09-09 23:36:21 -07:00
|
|
|
return Some((index, account.clone()));
|
2021-05-28 09:50:25 -07:00
|
|
|
}
|
2021-09-09 23:36:21 -07:00
|
|
|
}
|
|
|
|
None
|
2020-12-17 15:39:49 -08:00
|
|
|
}
|
2021-03-03 15:07:45 -08:00
|
|
|
fn update_timing(
|
|
|
|
&mut self,
|
|
|
|
serialize_us: u64,
|
|
|
|
create_vm_us: u64,
|
|
|
|
execute_us: u64,
|
|
|
|
deserialize_us: u64,
|
|
|
|
) {
|
|
|
|
self.timings.serialize_us += serialize_us;
|
|
|
|
self.timings.create_vm_us += create_vm_us;
|
|
|
|
self.timings.execute_us += execute_us;
|
|
|
|
self.timings.deserialize_us += deserialize_us;
|
|
|
|
}
|
2021-10-21 11:57:42 -07:00
|
|
|
#[allow(clippy::type_complexity)]
|
|
|
|
fn get_sysvars(&self) -> &RefCell<Vec<(Pubkey, Option<Rc<Vec<u8>>>)>> {
|
|
|
|
&self.sysvars
|
|
|
|
}
|
2021-05-20 14:00:50 -07:00
|
|
|
fn get_sysvar_data(&self, id: &Pubkey) -> Option<Rc<Vec<u8>>> {
|
|
|
|
if let Ok(mut sysvars) = self.sysvars.try_borrow_mut() {
|
|
|
|
// Try share from cache
|
|
|
|
let mut result = sysvars
|
2021-04-12 16:04:57 -07:00
|
|
|
.iter()
|
|
|
|
.find_map(|(key, sysvar)| if id == key { sysvar.clone() } else { None });
|
2021-05-20 14:00:50 -07:00
|
|
|
if result.is_none() {
|
2021-10-21 11:57:42 -07:00
|
|
|
if let Some(ancestors) = self.ancestors {
|
|
|
|
// Load it
|
|
|
|
result = self
|
|
|
|
.account_db
|
|
|
|
.load_with_fixed_root(ancestors, id)
|
|
|
|
.map(|(account, _)| Rc::new(account.data().to_vec()));
|
|
|
|
// Cache it
|
|
|
|
sysvars.push((*id, result.clone()));
|
|
|
|
}
|
2021-05-20 14:00:50 -07:00
|
|
|
}
|
|
|
|
result
|
|
|
|
} else {
|
|
|
|
None
|
2021-04-12 16:04:57 -07:00
|
|
|
}
|
|
|
|
}
|
2021-07-22 10:18:51 -07:00
|
|
|
fn get_compute_budget(&self) -> &ComputeBudget {
|
|
|
|
&self.compute_budget
|
|
|
|
}
|
2021-10-21 11:57:42 -07:00
|
|
|
fn set_blockhash(&mut self, hash: Hash) {
|
|
|
|
self.blockhash = hash;
|
|
|
|
}
|
2021-07-29 01:50:20 -07:00
|
|
|
fn get_blockhash(&self) -> &Hash {
|
2021-10-21 11:57:42 -07:00
|
|
|
&self.blockhash
|
|
|
|
}
|
2021-10-22 14:32:40 -07:00
|
|
|
fn set_lamports_per_signature(&mut self, lamports_per_signature: u64) {
|
|
|
|
self.lamports_per_signature = lamports_per_signature;
|
2021-07-29 01:50:20 -07:00
|
|
|
}
|
2021-10-22 14:32:40 -07:00
|
|
|
fn get_lamports_per_signature(&self) -> u64 {
|
|
|
|
self.lamports_per_signature
|
2021-07-29 01:50:20 -07:00
|
|
|
}
|
2021-09-29 10:11:06 -07:00
|
|
|
fn set_return_data(&mut self, data: Vec<u8>) -> Result<(), InstructionError> {
|
|
|
|
self.return_data = (*self.get_caller()?, data);
|
|
|
|
Ok(())
|
2021-09-01 02:14:01 -07:00
|
|
|
}
|
2021-09-29 10:11:06 -07:00
|
|
|
fn get_return_data(&self) -> (Pubkey, &[u8]) {
|
|
|
|
(self.return_data.0, &self.return_data.1)
|
2021-09-01 02:14:01 -07:00
|
|
|
}
|
2020-06-13 13:20:08 -07:00
|
|
|
}
|
2021-10-21 11:57:42 -07:00
|
|
|
|
|
|
|
pub struct MockInvokeContextPreparation {
|
|
|
|
pub accounts: Vec<(Pubkey, Rc<RefCell<AccountSharedData>>)>,
|
|
|
|
pub message: Message,
|
|
|
|
pub account_indices: Vec<usize>,
|
2020-06-13 13:20:08 -07:00
|
|
|
}
|
2021-10-21 11:57:42 -07:00
|
|
|
|
|
|
|
pub fn prepare_mock_invoke_context(
|
|
|
|
program_indices: &[usize],
|
|
|
|
instruction_data: &[u8],
|
|
|
|
keyed_accounts: &[(bool, bool, Pubkey, Rc<RefCell<AccountSharedData>>)],
|
|
|
|
) -> MockInvokeContextPreparation {
|
|
|
|
#[allow(clippy::type_complexity)]
|
|
|
|
let (accounts, mut metas): (
|
|
|
|
Vec<(Pubkey, Rc<RefCell<AccountSharedData>>)>,
|
|
|
|
Vec<AccountMeta>,
|
|
|
|
) = keyed_accounts
|
|
|
|
.iter()
|
|
|
|
.map(|(is_signer, is_writable, pubkey, account)| {
|
|
|
|
(
|
|
|
|
(*pubkey, account.clone()),
|
|
|
|
AccountMeta {
|
|
|
|
pubkey: *pubkey,
|
|
|
|
is_signer: *is_signer,
|
|
|
|
is_writable: *is_writable,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.unzip();
|
|
|
|
let program_id = if let Some(program_index) = program_indices.last() {
|
|
|
|
accounts[*program_index].0
|
|
|
|
} else {
|
|
|
|
Pubkey::default()
|
|
|
|
};
|
|
|
|
for program_index in program_indices.iter().rev() {
|
|
|
|
metas.remove(*program_index);
|
2020-06-06 10:18:28 -07:00
|
|
|
}
|
2021-10-21 11:57:42 -07:00
|
|
|
let message = Message::new(
|
|
|
|
&[Instruction::new_with_bytes(
|
|
|
|
program_id,
|
|
|
|
instruction_data,
|
|
|
|
metas,
|
|
|
|
)],
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
let account_indices: Vec<usize> = message
|
|
|
|
.account_keys
|
|
|
|
.iter()
|
|
|
|
.map(|search_key| {
|
|
|
|
accounts
|
|
|
|
.iter()
|
|
|
|
.position(|(key, _account)| key == search_key)
|
|
|
|
.unwrap_or(accounts.len())
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
MockInvokeContextPreparation {
|
|
|
|
accounts,
|
|
|
|
message,
|
|
|
|
account_indices,
|
2020-06-06 10:18:28 -07:00
|
|
|
}
|
2020-04-28 14:33:56 -07:00
|
|
|
}
|
|
|
|
|
2021-10-21 11:57:42 -07:00
|
|
|
pub fn mock_process_instruction(
|
|
|
|
loader_id: &Pubkey,
|
|
|
|
mut program_indices: Vec<usize>,
|
|
|
|
instruction_data: &[u8],
|
|
|
|
keyed_accounts: &[(bool, bool, Pubkey, Rc<RefCell<AccountSharedData>>)],
|
|
|
|
process_instruction: ProcessInstructionWithContext,
|
|
|
|
) -> Result<(), InstructionError> {
|
|
|
|
let mut preparation =
|
|
|
|
prepare_mock_invoke_context(&program_indices, instruction_data, keyed_accounts);
|
|
|
|
let processor_account = AccountSharedData::new_ref(0, 0, &solana_sdk::native_loader::id());
|
|
|
|
program_indices.insert(0, preparation.accounts.len());
|
|
|
|
preparation.accounts.push((*loader_id, processor_account));
|
|
|
|
let mut invoke_context = ThisInvokeContext::new_mock(&preparation.accounts, &[]);
|
|
|
|
invoke_context.push(
|
|
|
|
&preparation.message,
|
|
|
|
&preparation.message.instructions[0],
|
|
|
|
&program_indices,
|
|
|
|
Some(&preparation.account_indices),
|
|
|
|
)?;
|
|
|
|
process_instruction(1, instruction_data, &mut invoke_context)
|
|
|
|
}
|
|
|
|
|
2021-08-26 17:30:36 -07:00
|
|
|
#[derive(Debug, Default, Clone, Deserialize, Serialize)]
|
2021-09-29 10:05:25 -07:00
|
|
|
pub struct MessageProcessor {}
|
2020-07-06 04:22:23 -07:00
|
|
|
|
|
|
|
#[cfg(RUSTC_WITH_SPECIALIZATION)]
|
2020-10-19 21:07:46 -07:00
|
|
|
impl ::solana_frozen_abi::abi_example::AbiExample for MessageProcessor {
|
2020-07-06 04:22:23 -07:00
|
|
|
fn example() -> Self {
|
|
|
|
// MessageProcessor's fields are #[serde(skip)]-ed and not Serialize
|
|
|
|
// so, just rely on Default anyway.
|
|
|
|
MessageProcessor::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-02 08:35:38 -07:00
|
|
|
impl MessageProcessor {
|
2021-09-29 10:05:25 -07:00
|
|
|
/// Process a message.
|
|
|
|
/// This method calls each instruction in the message over the set of loaded accounts.
|
|
|
|
/// For each instruction it calls the program entrypoint method and verifies that the result of
|
2019-03-15 19:48:11 -07:00
|
|
|
/// the call does not violate the bank's accounting rules.
|
2021-09-29 10:05:25 -07:00
|
|
|
/// The accounts are committed back to the bank only if every instruction succeeds.
|
2020-09-20 10:58:12 -07:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2021-09-29 10:05:25 -07:00
|
|
|
#[allow(clippy::type_complexity)]
|
|
|
|
pub fn process_message(
|
|
|
|
instruction_processor: &InstructionProcessor,
|
2019-03-29 09:05:06 -07:00
|
|
|
message: &Message,
|
2021-09-29 10:05:25 -07:00
|
|
|
program_indices: &[Vec<usize>],
|
2021-07-05 04:49:37 -07:00
|
|
|
accounts: &[(Pubkey, Rc<RefCell<AccountSharedData>>)],
|
2020-03-31 10:07:38 -07:00
|
|
|
rent_collector: &RentCollector,
|
2020-06-13 13:20:08 -07:00
|
|
|
log_collector: Option<Rc<LogCollector>>,
|
2020-09-14 17:42:37 -07:00
|
|
|
executors: Rc<RefCell<Executors>>,
|
2021-09-29 10:05:25 -07:00
|
|
|
instruction_recorders: Option<&[InstructionRecorder]>,
|
2020-09-29 14:36:30 -07:00
|
|
|
feature_set: Arc<FeatureSet>,
|
2021-07-22 10:18:51 -07:00
|
|
|
compute_budget: ComputeBudget,
|
2021-07-16 00:31:22 -07:00
|
|
|
compute_meter: Rc<RefCell<dyn ComputeMeter>>,
|
2021-03-03 15:07:45 -08:00
|
|
|
timings: &mut ExecuteDetailsTimings,
|
2021-04-12 16:04:57 -07:00
|
|
|
account_db: Arc<Accounts>,
|
|
|
|
ancestors: &Ancestors,
|
2021-09-29 10:05:25 -07:00
|
|
|
blockhash: Hash,
|
2021-10-22 14:32:40 -07:00
|
|
|
lamports_per_signature: u64,
|
2021-09-29 10:05:25 -07:00
|
|
|
) -> Result<(), TransactionError> {
|
2020-04-28 14:33:56 -07:00
|
|
|
let mut invoke_context = ThisInvokeContext::new(
|
|
|
|
rent_collector.rent,
|
2021-04-19 09:48:48 -07:00
|
|
|
accounts,
|
2021-09-29 10:05:25 -07:00
|
|
|
instruction_processor.programs(),
|
2020-06-06 10:18:28 -07:00
|
|
|
log_collector,
|
2021-07-22 10:18:51 -07:00
|
|
|
compute_budget,
|
2021-07-16 00:31:22 -07:00
|
|
|
compute_meter,
|
2020-09-14 17:42:37 -07:00
|
|
|
executors,
|
2021-09-29 10:05:25 -07:00
|
|
|
instruction_recorders,
|
2020-09-29 14:36:30 -07:00
|
|
|
feature_set,
|
2021-04-12 16:04:57 -07:00
|
|
|
account_db,
|
2021-10-21 11:57:42 -07:00
|
|
|
Some(ancestors),
|
|
|
|
blockhash,
|
2021-10-22 14:32:40 -07:00
|
|
|
lamports_per_signature,
|
2021-09-29 10:05:25 -07:00
|
|
|
);
|
|
|
|
let compute_meter = invoke_context.get_compute_meter();
|
2021-10-11 16:47:17 -07:00
|
|
|
|
|
|
|
debug_assert_eq!(program_indices.len(), message.instructions.len());
|
2021-09-29 10:05:25 -07:00
|
|
|
for (instruction_index, (instruction, program_indices)) in message
|
|
|
|
.instructions
|
|
|
|
.iter()
|
|
|
|
.zip(program_indices.iter())
|
|
|
|
.enumerate()
|
|
|
|
{
|
|
|
|
let program_id = instruction.program_id(&message.account_keys);
|
|
|
|
if invoke_context.is_feature_active(&prevent_calling_precompiles_as_programs::id())
|
|
|
|
&& is_precompile(program_id, |id| invoke_context.is_feature_active(id))
|
|
|
|
{
|
|
|
|
// Precompiled programs don't have an instruction processor
|
|
|
|
continue;
|
|
|
|
}
|
2021-09-26 23:28:45 -07:00
|
|
|
|
2021-09-28 16:59:01 -07:00
|
|
|
let mut time = Measure::start("execute_instruction");
|
|
|
|
let pre_remaining_units = compute_meter.borrow().get_remaining();
|
2021-09-29 10:05:25 -07:00
|
|
|
|
|
|
|
// Fixup the special instructions key if present
|
|
|
|
// before the account pre-values are taken care of
|
|
|
|
for (pubkey, accont) in accounts.iter().take(message.account_keys.len()) {
|
|
|
|
if instructions::check_id(pubkey) {
|
|
|
|
let mut mut_account_ref = accont.borrow_mut();
|
|
|
|
instructions::store_current_index(
|
|
|
|
mut_account_ref.data_as_mut_slice(),
|
|
|
|
instruction_index as u16,
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut compute_budget = compute_budget;
|
2021-10-19 21:01:39 -07:00
|
|
|
if !invoke_context.is_feature_active(&tx_wide_compute_cap::id())
|
|
|
|
&& invoke_context.is_feature_active(&neon_evm_compute_budget::id())
|
2021-09-29 10:05:25 -07:00
|
|
|
&& *program_id == crate::neon_evm_program::id()
|
|
|
|
{
|
|
|
|
// Bump the compute budget for neon_evm
|
|
|
|
compute_budget.max_units = compute_budget.max_units.max(500_000);
|
2021-10-19 21:01:39 -07:00
|
|
|
}
|
|
|
|
if !invoke_context.is_feature_active(&requestable_heap_size::id())
|
|
|
|
&& invoke_context.is_feature_active(&neon_evm_compute_budget::id())
|
|
|
|
&& *program_id == crate::neon_evm_program::id()
|
|
|
|
{
|
|
|
|
// Bump the compute budget for neon_evm
|
2021-09-29 10:05:25 -07:00
|
|
|
compute_budget.heap_size = Some(256 * 1024);
|
|
|
|
}
|
|
|
|
|
|
|
|
invoke_context.set_instruction_index(instruction_index);
|
|
|
|
let result = invoke_context
|
2021-10-12 23:58:20 -07:00
|
|
|
.push(message, instruction, program_indices, None)
|
2021-09-29 10:05:25 -07:00
|
|
|
.and_then(|_| {
|
2021-10-10 13:29:18 -07:00
|
|
|
instruction_processor
|
|
|
|
.process_instruction(&instruction.data, &mut invoke_context)?;
|
2021-09-29 10:05:25 -07:00
|
|
|
invoke_context.verify(message, instruction, program_indices)?;
|
|
|
|
timings.accumulate(&invoke_context.timings);
|
|
|
|
Ok(())
|
|
|
|
})
|
2021-06-04 07:04:31 -07:00
|
|
|
.map_err(|err| TransactionError::InstructionError(instruction_index as u8, err));
|
2021-09-29 10:05:25 -07:00
|
|
|
invoke_context.pop();
|
|
|
|
|
2021-06-04 07:04:31 -07:00
|
|
|
time.stop();
|
2021-07-16 12:40:12 -07:00
|
|
|
let post_remaining_units = compute_meter.borrow().get_remaining();
|
2021-07-19 11:05:10 -07:00
|
|
|
timings.accumulate_program(
|
|
|
|
instruction.program_id(&message.account_keys),
|
|
|
|
time.as_us(),
|
|
|
|
pre_remaining_units - post_remaining_units,
|
|
|
|
);
|
2021-06-04 07:04:31 -07:00
|
|
|
|
2021-09-29 10:05:25 -07:00
|
|
|
result?;
|
2019-03-11 15:35:25 -07:00
|
|
|
}
|
2019-03-15 19:48:11 -07:00
|
|
|
Ok(())
|
|
|
|
}
|
2019-03-11 15:35:25 -07:00
|
|
|
}
|
|
|
|
|
2019-02-18 22:26:22 -08:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2020-01-28 17:03:20 -08:00
|
|
|
use solana_sdk::{
|
|
|
|
instruction::{AccountMeta, Instruction, InstructionError},
|
2021-10-21 11:57:42 -07:00
|
|
|
keyed_account::keyed_account_at_index,
|
2020-01-28 17:03:20 -08:00
|
|
|
message::Message,
|
2021-08-26 17:30:36 -07:00
|
|
|
native_loader::{self, create_loadable_account_for_test},
|
2021-09-28 23:25:08 -07:00
|
|
|
secp256k1_instruction::new_secp256k1_instruction,
|
|
|
|
secp256k1_program,
|
2020-01-28 17:03:20 -08:00
|
|
|
};
|
2019-02-18 22:26:22 -08:00
|
|
|
|
2021-09-29 10:05:25 -07:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
enum MockInstruction {
|
|
|
|
NoopSuccess,
|
|
|
|
NoopFail,
|
|
|
|
ModifyOwned,
|
|
|
|
ModifyNotOwned,
|
|
|
|
ModifyReadonly,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn mock_process_instruction(
|
2021-10-08 02:41:07 -07:00
|
|
|
first_instruction_account: usize,
|
2021-09-29 10:05:25 -07:00
|
|
|
data: &[u8],
|
|
|
|
invoke_context: &mut dyn InvokeContext,
|
|
|
|
) -> Result<(), InstructionError> {
|
2021-10-10 13:29:18 -07:00
|
|
|
let program_id = invoke_context.get_caller()?;
|
2021-09-29 10:05:25 -07:00
|
|
|
let keyed_accounts = invoke_context.get_keyed_accounts()?;
|
2021-10-08 02:41:07 -07:00
|
|
|
assert_eq!(
|
|
|
|
*program_id,
|
2021-10-21 11:57:42 -07:00
|
|
|
keyed_account_at_index(keyed_accounts, first_instruction_account)?.owner()?
|
2021-10-08 02:41:07 -07:00
|
|
|
);
|
2021-09-29 10:05:25 -07:00
|
|
|
assert_ne!(
|
2021-10-21 11:57:42 -07:00
|
|
|
keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?.owner()?,
|
|
|
|
*keyed_account_at_index(keyed_accounts, first_instruction_account)?.unsigned_key()
|
2021-09-29 10:05:25 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
if let Ok(instruction) = bincode::deserialize(data) {
|
|
|
|
match instruction {
|
|
|
|
MockInstruction::NoopSuccess => (),
|
|
|
|
MockInstruction::NoopFail => return Err(InstructionError::GenericError),
|
|
|
|
MockInstruction::ModifyOwned => {
|
2021-10-21 11:57:42 -07:00
|
|
|
keyed_account_at_index(keyed_accounts, first_instruction_account)?
|
2021-10-08 02:41:07 -07:00
|
|
|
.try_account_ref_mut()?
|
|
|
|
.data_as_mut_slice()[0] = 1
|
2021-09-29 10:05:25 -07:00
|
|
|
}
|
|
|
|
MockInstruction::ModifyNotOwned => {
|
2021-10-21 11:57:42 -07:00
|
|
|
keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?
|
2021-10-08 02:41:07 -07:00
|
|
|
.try_account_ref_mut()?
|
|
|
|
.data_as_mut_slice()[0] = 1
|
2021-09-29 10:05:25 -07:00
|
|
|
}
|
|
|
|
MockInstruction::ModifyReadonly => {
|
2021-10-21 11:57:42 -07:00
|
|
|
keyed_account_at_index(keyed_accounts, first_instruction_account + 2)?
|
2021-10-08 02:41:07 -07:00
|
|
|
.try_account_ref_mut()?
|
|
|
|
.data_as_mut_slice()[0] = 1
|
2021-09-29 10:05:25 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Err(InstructionError::InvalidInstructionData);
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-04-28 14:33:56 -07:00
|
|
|
#[test]
|
|
|
|
fn test_invoke_context() {
|
|
|
|
const MAX_DEPTH: usize = 10;
|
2021-04-19 09:48:48 -07:00
|
|
|
let mut invoke_stack = vec![];
|
2020-04-28 14:33:56 -07:00
|
|
|
let mut accounts = vec![];
|
2021-04-19 09:48:48 -07:00
|
|
|
let mut metas = vec![];
|
2020-04-28 14:33:56 -07:00
|
|
|
for i in 0..MAX_DEPTH {
|
2021-04-19 09:48:48 -07:00
|
|
|
invoke_stack.push(solana_sdk::pubkey::new_rand());
|
2021-07-05 04:49:37 -07:00
|
|
|
accounts.push((
|
|
|
|
solana_sdk::pubkey::new_rand(),
|
|
|
|
Rc::new(RefCell::new(AccountSharedData::new(
|
|
|
|
i as u64,
|
|
|
|
1,
|
|
|
|
&invoke_stack[i],
|
|
|
|
))),
|
|
|
|
));
|
|
|
|
metas.push(AccountMeta::new(accounts[i].0, false));
|
2020-04-28 14:33:56 -07:00
|
|
|
}
|
2021-04-19 09:48:48 -07:00
|
|
|
for program_id in invoke_stack.iter() {
|
2021-07-05 04:49:37 -07:00
|
|
|
accounts.push((
|
|
|
|
*program_id,
|
|
|
|
Rc::new(RefCell::new(AccountSharedData::new(
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
&solana_sdk::pubkey::Pubkey::default(),
|
|
|
|
))),
|
|
|
|
));
|
2021-04-19 09:48:48 -07:00
|
|
|
metas.push(AccountMeta::new(*program_id, false));
|
2020-11-29 02:06:43 -08:00
|
|
|
}
|
2021-09-21 05:41:02 -07:00
|
|
|
let account_indices = (0..accounts.len()).collect::<Vec<usize>>();
|
2020-11-29 02:06:43 -08:00
|
|
|
|
2021-04-19 09:48:48 -07:00
|
|
|
let message = Message::new(
|
|
|
|
&[Instruction::new_with_bytes(invoke_stack[0], &[0], metas)],
|
|
|
|
None,
|
|
|
|
);
|
2021-10-21 11:57:42 -07:00
|
|
|
let mut invoke_context = ThisInvokeContext::new_mock(&accounts, &[]);
|
2020-04-28 14:33:56 -07:00
|
|
|
|
|
|
|
// Check call depth increases and has a limit
|
2021-09-29 10:05:25 -07:00
|
|
|
let mut depth_reached = 0;
|
2021-10-12 23:58:20 -07:00
|
|
|
for _ in 0..invoke_stack.len() {
|
2021-09-09 23:36:21 -07:00
|
|
|
if Err(InstructionError::CallDepth)
|
2021-10-12 10:26:55 -07:00
|
|
|
== invoke_context.push(
|
|
|
|
&message,
|
|
|
|
&message.instructions[0],
|
|
|
|
&[MAX_DEPTH + depth_reached],
|
|
|
|
None,
|
|
|
|
)
|
2021-09-09 23:36:21 -07:00
|
|
|
{
|
2020-04-28 14:33:56 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
depth_reached += 1;
|
|
|
|
}
|
|
|
|
assert_ne!(depth_reached, 0);
|
|
|
|
assert!(depth_reached < MAX_DEPTH);
|
|
|
|
|
|
|
|
// Mock each invocation
|
|
|
|
for owned_index in (1..depth_reached).rev() {
|
|
|
|
let not_owned_index = owned_index - 1;
|
|
|
|
let metas = vec![
|
2021-07-05 04:49:37 -07:00
|
|
|
AccountMeta::new(accounts[not_owned_index].0, false),
|
|
|
|
AccountMeta::new(accounts[owned_index].0, false),
|
2020-04-28 14:33:56 -07:00
|
|
|
];
|
2020-06-24 13:52:38 -07:00
|
|
|
let message = Message::new(
|
2021-03-03 21:46:48 -08:00
|
|
|
&[Instruction::new_with_bytes(
|
2021-04-19 09:48:48 -07:00
|
|
|
invoke_stack[owned_index],
|
2021-03-03 21:46:48 -08:00
|
|
|
&[0],
|
|
|
|
metas,
|
|
|
|
)],
|
2020-05-15 12:23:09 -07:00
|
|
|
None,
|
|
|
|
);
|
2021-09-21 05:41:02 -07:00
|
|
|
let write_privileges: Vec<bool> = (0..message.account_keys.len())
|
|
|
|
.map(|i| message.is_writable(i, /*demote_program_write_locks=*/ true))
|
|
|
|
.collect();
|
2020-04-28 14:33:56 -07:00
|
|
|
|
|
|
|
// modify account owned by the program
|
2021-07-05 04:49:37 -07:00
|
|
|
accounts[owned_index].1.borrow_mut().data_as_mut_slice()[0] =
|
2021-03-10 13:28:03 -08:00
|
|
|
(MAX_DEPTH + owned_index) as u8;
|
2020-04-28 14:33:56 -07:00
|
|
|
invoke_context
|
2021-09-21 05:41:02 -07:00
|
|
|
.verify_and_update(
|
|
|
|
&message.instructions[0],
|
|
|
|
&account_indices[not_owned_index..owned_index + 1],
|
|
|
|
&write_privileges,
|
|
|
|
)
|
2020-04-28 14:33:56 -07:00
|
|
|
.unwrap();
|
|
|
|
assert_eq!(
|
2021-08-26 17:30:36 -07:00
|
|
|
invoke_context.pre_accounts[owned_index].data()[0],
|
2020-04-28 14:33:56 -07:00
|
|
|
(MAX_DEPTH + owned_index) as u8
|
|
|
|
);
|
|
|
|
|
|
|
|
// modify account not owned by the program
|
2021-07-05 04:49:37 -07:00
|
|
|
let data = accounts[not_owned_index].1.borrow_mut().data()[0];
|
|
|
|
accounts[not_owned_index].1.borrow_mut().data_as_mut_slice()[0] =
|
2021-03-10 13:28:03 -08:00
|
|
|
(MAX_DEPTH + not_owned_index) as u8;
|
2020-04-28 14:33:56 -07:00
|
|
|
assert_eq!(
|
|
|
|
invoke_context.verify_and_update(
|
|
|
|
&message.instructions[0],
|
2021-09-21 05:41:02 -07:00
|
|
|
&account_indices[not_owned_index..owned_index + 1],
|
2021-07-07 07:14:00 -07:00
|
|
|
&write_privileges,
|
2020-04-28 14:33:56 -07:00
|
|
|
),
|
|
|
|
Err(InstructionError::ExternalAccountDataModified)
|
|
|
|
);
|
2021-08-26 17:30:36 -07:00
|
|
|
assert_eq!(invoke_context.pre_accounts[not_owned_index].data()[0], data);
|
2021-07-05 04:49:37 -07:00
|
|
|
accounts[not_owned_index].1.borrow_mut().data_as_mut_slice()[0] = data;
|
2020-04-28 14:33:56 -07:00
|
|
|
|
|
|
|
invoke_context.pop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-18 22:26:22 -08:00
|
|
|
#[test]
|
2021-09-29 10:05:25 -07:00
|
|
|
fn test_invoke_context_verify() {
|
2020-10-19 12:23:14 -07:00
|
|
|
let accounts = vec![(
|
|
|
|
solana_sdk::pubkey::new_rand(),
|
2021-03-10 23:04:00 -08:00
|
|
|
Rc::new(RefCell::new(AccountSharedData::default())),
|
2020-10-19 12:23:14 -07:00
|
|
|
)];
|
2021-09-29 10:05:25 -07:00
|
|
|
let message = Message::new(
|
|
|
|
&[Instruction::new_with_bincode(
|
|
|
|
accounts[0].0,
|
|
|
|
&MockInstruction::NoopSuccess,
|
|
|
|
vec![AccountMeta::new_readonly(accounts[0].0, false)],
|
|
|
|
)],
|
|
|
|
None,
|
|
|
|
);
|
2021-10-21 11:57:42 -07:00
|
|
|
let mut invoke_context = ThisInvokeContext::new_mock(&accounts, &[]);
|
2021-09-29 10:05:25 -07:00
|
|
|
invoke_context
|
2021-10-12 23:58:20 -07:00
|
|
|
.push(&message, &message.instructions[0], &[0], None)
|
2021-09-29 10:05:25 -07:00
|
|
|
.unwrap();
|
|
|
|
assert!(invoke_context
|
|
|
|
.verify(&message, &message.instructions[0], &[0])
|
|
|
|
.is_ok());
|
2020-01-22 09:11:56 -08:00
|
|
|
|
2020-04-28 14:33:56 -07:00
|
|
|
let mut _borrowed = accounts[0].1.borrow();
|
2020-01-22 17:54:06 -08:00
|
|
|
assert_eq!(
|
2021-09-29 10:05:25 -07:00
|
|
|
invoke_context.verify(&message, &message.instructions[0], &[0]),
|
2020-01-22 17:54:06 -08:00
|
|
|
Err(InstructionError::AccountBorrowOutstanding)
|
|
|
|
);
|
2019-02-18 22:26:22 -08:00
|
|
|
}
|
|
|
|
|
2019-06-10 19:50:02 -07:00
|
|
|
#[test]
|
2019-11-05 08:38:35 -08:00
|
|
|
fn test_process_message_readonly_handling() {
|
2019-06-10 19:50:02 -07:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
enum MockSystemInstruction {
|
2019-11-05 08:38:35 -08:00
|
|
|
Correct,
|
|
|
|
AttemptCredit { lamports: u64 },
|
|
|
|
AttemptDataChange { data: u8 },
|
2019-06-10 19:50:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn mock_system_process_instruction(
|
2021-10-08 02:41:07 -07:00
|
|
|
first_instruction_account: usize,
|
2019-06-10 19:50:02 -07:00
|
|
|
data: &[u8],
|
2021-04-19 09:48:48 -07:00
|
|
|
invoke_context: &mut dyn InvokeContext,
|
2019-06-10 19:50:02 -07:00
|
|
|
) -> Result<(), InstructionError> {
|
2021-04-19 09:48:48 -07:00
|
|
|
let keyed_accounts = invoke_context.get_keyed_accounts()?;
|
2019-06-10 19:50:02 -07:00
|
|
|
if let Ok(instruction) = bincode::deserialize(data) {
|
|
|
|
match instruction {
|
2019-11-05 08:38:35 -08:00
|
|
|
MockSystemInstruction::Correct => Ok(()),
|
|
|
|
MockSystemInstruction::AttemptCredit { lamports } => {
|
2021-10-21 11:57:42 -07:00
|
|
|
keyed_account_at_index(keyed_accounts, first_instruction_account)?
|
2021-05-03 08:45:15 -07:00
|
|
|
.account
|
|
|
|
.borrow_mut()
|
|
|
|
.checked_sub_lamports(lamports)?;
|
2021-10-21 11:57:42 -07:00
|
|
|
keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?
|
2021-04-27 07:56:18 -07:00
|
|
|
.account
|
|
|
|
.borrow_mut()
|
|
|
|
.checked_add_lamports(lamports)?;
|
2019-06-10 19:50:02 -07:00
|
|
|
Ok(())
|
|
|
|
}
|
2019-11-05 08:38:35 -08:00
|
|
|
// Change data in a read-only account
|
|
|
|
MockSystemInstruction::AttemptDataChange { data } => {
|
2021-10-21 11:57:42 -07:00
|
|
|
keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?
|
2021-10-08 02:41:07 -07:00
|
|
|
.account
|
|
|
|
.borrow_mut()
|
|
|
|
.set_data(vec![data]);
|
2019-06-10 19:50:02 -07:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Err(InstructionError::InvalidInstructionData)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mock_system_program_id = Pubkey::new(&[2u8; 32]);
|
2020-03-31 10:07:38 -07:00
|
|
|
let rent_collector = RentCollector::default();
|
2021-09-29 10:05:25 -07:00
|
|
|
let mut instruction_processor = InstructionProcessor::default();
|
|
|
|
instruction_processor.add_program(&mock_system_program_id, mock_system_process_instruction);
|
2019-06-10 19:50:02 -07:00
|
|
|
|
2021-09-09 23:36:21 -07:00
|
|
|
let program_account = Rc::new(RefCell::new(create_loadable_account_for_test(
|
|
|
|
"mock_system_program",
|
|
|
|
)));
|
2021-07-05 04:49:37 -07:00
|
|
|
let accounts = vec![
|
|
|
|
(
|
|
|
|
solana_sdk::pubkey::new_rand(),
|
|
|
|
AccountSharedData::new_ref(100, 1, &mock_system_program_id),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
solana_sdk::pubkey::new_rand(),
|
|
|
|
AccountSharedData::new_ref(0, 1, &mock_system_program_id),
|
|
|
|
),
|
2021-09-09 23:36:21 -07:00
|
|
|
(mock_system_program_id, program_account),
|
2021-07-05 04:49:37 -07:00
|
|
|
];
|
2021-09-09 23:36:21 -07:00
|
|
|
let program_indices = vec![vec![2]];
|
2019-06-10 19:50:02 -07:00
|
|
|
|
2020-09-14 17:42:37 -07:00
|
|
|
let executors = Rc::new(RefCell::new(Executors::default()));
|
2021-04-12 16:04:57 -07:00
|
|
|
let ancestors = Ancestors::default();
|
2020-09-14 17:42:37 -07:00
|
|
|
|
2019-06-10 19:50:02 -07:00
|
|
|
let account_metas = vec![
|
2021-07-05 04:49:37 -07:00
|
|
|
AccountMeta::new(accounts[0].0, true),
|
|
|
|
AccountMeta::new_readonly(accounts[1].0, false),
|
2019-06-10 19:50:02 -07:00
|
|
|
];
|
2020-06-24 13:52:38 -07:00
|
|
|
let message = Message::new(
|
2021-03-03 21:46:48 -08:00
|
|
|
&[Instruction::new_with_bincode(
|
2020-06-24 13:52:38 -07:00
|
|
|
mock_system_program_id,
|
|
|
|
&MockSystemInstruction::Correct,
|
|
|
|
account_metas.clone(),
|
|
|
|
)],
|
2021-07-05 04:49:37 -07:00
|
|
|
Some(&accounts[0].0),
|
2020-06-24 13:52:38 -07:00
|
|
|
);
|
2019-06-10 19:50:02 -07:00
|
|
|
|
2021-09-29 10:05:25 -07:00
|
|
|
let result = MessageProcessor::process_message(
|
|
|
|
&instruction_processor,
|
2020-09-14 17:42:37 -07:00
|
|
|
&message,
|
2021-09-09 23:36:21 -07:00
|
|
|
&program_indices,
|
2020-09-14 17:42:37 -07:00
|
|
|
&accounts,
|
|
|
|
&rent_collector,
|
|
|
|
None,
|
|
|
|
executors.clone(),
|
2020-09-24 07:36:22 -07:00
|
|
|
None,
|
2020-09-29 20:18:28 -07:00
|
|
|
Arc::new(FeatureSet::all_enabled()),
|
2021-07-22 10:18:51 -07:00
|
|
|
ComputeBudget::new(),
|
2021-10-21 11:57:42 -07:00
|
|
|
ThisComputeMeter::new_ref(std::i64::MAX as u64),
|
2021-03-03 15:07:45 -08:00
|
|
|
&mut ExecuteDetailsTimings::default(),
|
2021-08-06 13:52:27 -07:00
|
|
|
Arc::new(Accounts::default_for_tests()),
|
2021-04-12 16:04:57 -07:00
|
|
|
&ancestors,
|
2021-07-29 01:50:20 -07:00
|
|
|
Hash::default(),
|
2021-10-22 14:32:40 -07:00
|
|
|
0,
|
2020-09-14 17:42:37 -07:00
|
|
|
);
|
2019-06-10 19:50:02 -07:00
|
|
|
assert_eq!(result, Ok(()));
|
2021-07-05 04:49:37 -07:00
|
|
|
assert_eq!(accounts[0].1.borrow().lamports(), 100);
|
|
|
|
assert_eq!(accounts[1].1.borrow().lamports(), 0);
|
2019-06-10 19:50:02 -07:00
|
|
|
|
2020-06-24 13:52:38 -07:00
|
|
|
let message = Message::new(
|
2021-03-03 21:46:48 -08:00
|
|
|
&[Instruction::new_with_bincode(
|
2020-06-24 13:52:38 -07:00
|
|
|
mock_system_program_id,
|
|
|
|
&MockSystemInstruction::AttemptCredit { lamports: 50 },
|
|
|
|
account_metas.clone(),
|
|
|
|
)],
|
2021-07-05 04:49:37 -07:00
|
|
|
Some(&accounts[0].0),
|
2020-06-24 13:52:38 -07:00
|
|
|
);
|
2019-06-10 19:50:02 -07:00
|
|
|
|
2021-09-29 10:05:25 -07:00
|
|
|
let result = MessageProcessor::process_message(
|
|
|
|
&instruction_processor,
|
2020-09-14 17:42:37 -07:00
|
|
|
&message,
|
2021-09-09 23:36:21 -07:00
|
|
|
&program_indices,
|
2020-09-14 17:42:37 -07:00
|
|
|
&accounts,
|
|
|
|
&rent_collector,
|
|
|
|
None,
|
|
|
|
executors.clone(),
|
2020-09-24 07:36:22 -07:00
|
|
|
None,
|
2020-09-29 20:18:28 -07:00
|
|
|
Arc::new(FeatureSet::all_enabled()),
|
2021-07-22 10:18:51 -07:00
|
|
|
ComputeBudget::new(),
|
2021-10-21 11:57:42 -07:00
|
|
|
ThisComputeMeter::new_ref(std::i64::MAX as u64),
|
2021-03-03 15:07:45 -08:00
|
|
|
&mut ExecuteDetailsTimings::default(),
|
2021-08-06 13:52:27 -07:00
|
|
|
Arc::new(Accounts::default_for_tests()),
|
2021-04-12 16:04:57 -07:00
|
|
|
&ancestors,
|
2021-07-29 01:50:20 -07:00
|
|
|
Hash::default(),
|
2021-10-22 14:32:40 -07:00
|
|
|
0,
|
2020-09-14 17:42:37 -07:00
|
|
|
);
|
2019-06-10 19:50:02 -07:00
|
|
|
assert_eq!(
|
|
|
|
result,
|
|
|
|
Err(TransactionError::InstructionError(
|
|
|
|
0,
|
2019-11-05 08:38:35 -08:00
|
|
|
InstructionError::ReadonlyLamportChange
|
2019-06-10 19:50:02 -07:00
|
|
|
))
|
|
|
|
);
|
|
|
|
|
2020-06-24 13:52:38 -07:00
|
|
|
let message = Message::new(
|
2021-03-03 21:46:48 -08:00
|
|
|
&[Instruction::new_with_bincode(
|
2020-06-24 13:52:38 -07:00
|
|
|
mock_system_program_id,
|
|
|
|
&MockSystemInstruction::AttemptDataChange { data: 50 },
|
|
|
|
account_metas,
|
|
|
|
)],
|
2021-07-05 04:49:37 -07:00
|
|
|
Some(&accounts[0].0),
|
2020-06-24 13:52:38 -07:00
|
|
|
);
|
2019-06-10 19:50:02 -07:00
|
|
|
|
2021-09-29 10:05:25 -07:00
|
|
|
let result = MessageProcessor::process_message(
|
|
|
|
&instruction_processor,
|
2020-09-14 17:42:37 -07:00
|
|
|
&message,
|
2021-09-09 23:36:21 -07:00
|
|
|
&program_indices,
|
2020-09-14 17:42:37 -07:00
|
|
|
&accounts,
|
|
|
|
&rent_collector,
|
|
|
|
None,
|
|
|
|
executors,
|
2020-09-24 07:36:22 -07:00
|
|
|
None,
|
2020-09-29 20:18:28 -07:00
|
|
|
Arc::new(FeatureSet::all_enabled()),
|
2021-07-22 10:18:51 -07:00
|
|
|
ComputeBudget::new(),
|
2021-10-21 11:57:42 -07:00
|
|
|
ThisComputeMeter::new_ref(std::i64::MAX as u64),
|
2021-03-03 15:07:45 -08:00
|
|
|
&mut ExecuteDetailsTimings::default(),
|
2021-08-06 13:52:27 -07:00
|
|
|
Arc::new(Accounts::default_for_tests()),
|
2021-04-12 16:04:57 -07:00
|
|
|
&ancestors,
|
2021-07-29 01:50:20 -07:00
|
|
|
Hash::default(),
|
2021-10-22 14:32:40 -07:00
|
|
|
0,
|
2020-09-14 17:42:37 -07:00
|
|
|
);
|
2019-06-10 19:50:02 -07:00
|
|
|
assert_eq!(
|
|
|
|
result,
|
|
|
|
Err(TransactionError::InstructionError(
|
|
|
|
0,
|
2019-11-05 08:38:35 -08:00
|
|
|
InstructionError::ReadonlyDataModified
|
2019-06-10 19:50:02 -07:00
|
|
|
))
|
|
|
|
);
|
2019-02-22 12:08:54 -08:00
|
|
|
}
|
2020-01-22 09:11:56 -08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_process_message_duplicate_accounts() {
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
enum MockSystemInstruction {
|
|
|
|
BorrowFail,
|
|
|
|
MultiBorrowMut,
|
|
|
|
DoWork { lamports: u64, data: u8 },
|
|
|
|
}
|
|
|
|
|
|
|
|
fn mock_system_process_instruction(
|
2021-10-08 02:41:07 -07:00
|
|
|
first_instruction_account: usize,
|
2020-01-22 09:11:56 -08:00
|
|
|
data: &[u8],
|
2021-04-19 09:48:48 -07:00
|
|
|
invoke_context: &mut dyn InvokeContext,
|
2020-01-22 09:11:56 -08:00
|
|
|
) -> Result<(), InstructionError> {
|
2021-04-19 09:48:48 -07:00
|
|
|
let keyed_accounts = invoke_context.get_keyed_accounts()?;
|
2020-01-22 09:11:56 -08:00
|
|
|
if let Ok(instruction) = bincode::deserialize(data) {
|
|
|
|
match instruction {
|
|
|
|
MockSystemInstruction::BorrowFail => {
|
2021-10-08 02:41:07 -07:00
|
|
|
let from_account =
|
2021-10-21 11:57:42 -07:00
|
|
|
keyed_account_at_index(keyed_accounts, first_instruction_account)?
|
|
|
|
.try_account_ref_mut()?;
|
2021-10-08 02:41:07 -07:00
|
|
|
let dup_account =
|
2021-10-21 11:57:42 -07:00
|
|
|
keyed_account_at_index(keyed_accounts, first_instruction_account + 2)?
|
|
|
|
.try_account_ref_mut()?;
|
2021-04-22 13:04:55 -07:00
|
|
|
if from_account.lamports() != dup_account.lamports() {
|
2020-01-22 09:11:56 -08:00
|
|
|
return Err(InstructionError::InvalidArgument);
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
MockSystemInstruction::MultiBorrowMut => {
|
|
|
|
let from_lamports = {
|
2021-10-08 02:41:07 -07:00
|
|
|
let from_account =
|
2021-10-21 11:57:42 -07:00
|
|
|
keyed_account_at_index(keyed_accounts, first_instruction_account)?
|
|
|
|
.try_account_ref_mut()?;
|
2021-05-03 08:45:54 -07:00
|
|
|
from_account.lamports()
|
2020-01-22 09:11:56 -08:00
|
|
|
};
|
|
|
|
let dup_lamports = {
|
2021-10-21 11:57:42 -07:00
|
|
|
let dup_account = keyed_account_at_index(
|
|
|
|
keyed_accounts,
|
|
|
|
first_instruction_account + 2,
|
|
|
|
)?
|
|
|
|
.try_account_ref_mut()?;
|
2021-05-03 08:45:54 -07:00
|
|
|
dup_account.lamports()
|
2020-01-22 09:11:56 -08:00
|
|
|
};
|
|
|
|
if from_lamports != dup_lamports {
|
|
|
|
return Err(InstructionError::InvalidArgument);
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
MockSystemInstruction::DoWork { lamports, data } => {
|
|
|
|
{
|
2021-10-21 11:57:42 -07:00
|
|
|
let mut to_account = keyed_account_at_index(
|
|
|
|
keyed_accounts,
|
|
|
|
first_instruction_account + 1,
|
|
|
|
)?
|
|
|
|
.try_account_ref_mut()?;
|
|
|
|
let mut dup_account = keyed_account_at_index(
|
|
|
|
keyed_accounts,
|
|
|
|
first_instruction_account + 2,
|
|
|
|
)?
|
|
|
|
.try_account_ref_mut()?;
|
2021-05-03 08:45:15 -07:00
|
|
|
dup_account.checked_sub_lamports(lamports)?;
|
2021-04-27 07:56:18 -07:00
|
|
|
to_account.checked_add_lamports(lamports)?;
|
2021-03-11 14:40:45 -08:00
|
|
|
dup_account.set_data(vec![data]);
|
2020-01-22 09:11:56 -08:00
|
|
|
}
|
2021-10-21 11:57:42 -07:00
|
|
|
keyed_account_at_index(keyed_accounts, first_instruction_account)?
|
2021-05-03 08:45:15 -07:00
|
|
|
.try_account_ref_mut()?
|
|
|
|
.checked_sub_lamports(lamports)?;
|
2021-10-21 11:57:42 -07:00
|
|
|
keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?
|
2021-04-27 07:56:18 -07:00
|
|
|
.try_account_ref_mut()?
|
|
|
|
.checked_add_lamports(lamports)?;
|
2020-01-22 09:11:56 -08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Err(InstructionError::InvalidInstructionData)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mock_program_id = Pubkey::new(&[2u8; 32]);
|
2020-03-31 10:07:38 -07:00
|
|
|
let rent_collector = RentCollector::default();
|
2021-09-29 10:05:25 -07:00
|
|
|
let mut instruction_processor = InstructionProcessor::default();
|
|
|
|
instruction_processor.add_program(&mock_program_id, mock_system_process_instruction);
|
2020-01-22 09:11:56 -08:00
|
|
|
|
2021-09-09 23:36:21 -07:00
|
|
|
let program_account = Rc::new(RefCell::new(create_loadable_account_for_test(
|
|
|
|
"mock_system_program",
|
|
|
|
)));
|
2021-07-05 04:49:37 -07:00
|
|
|
let accounts = vec![
|
|
|
|
(
|
|
|
|
solana_sdk::pubkey::new_rand(),
|
|
|
|
AccountSharedData::new_ref(100, 1, &mock_program_id),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
solana_sdk::pubkey::new_rand(),
|
|
|
|
AccountSharedData::new_ref(0, 1, &mock_program_id),
|
|
|
|
),
|
2021-09-09 23:36:21 -07:00
|
|
|
(mock_program_id, program_account),
|
2021-07-05 04:49:37 -07:00
|
|
|
];
|
2021-09-09 23:36:21 -07:00
|
|
|
let program_indices = vec![vec![2]];
|
2020-01-22 09:11:56 -08:00
|
|
|
|
2020-09-14 17:42:37 -07:00
|
|
|
let executors = Rc::new(RefCell::new(Executors::default()));
|
2021-04-12 16:04:57 -07:00
|
|
|
let ancestors = Ancestors::default();
|
2020-09-14 17:42:37 -07:00
|
|
|
|
2020-01-22 09:11:56 -08:00
|
|
|
let account_metas = vec![
|
2021-07-05 04:49:37 -07:00
|
|
|
AccountMeta::new(accounts[0].0, true),
|
|
|
|
AccountMeta::new(accounts[1].0, false),
|
|
|
|
AccountMeta::new(accounts[0].0, false),
|
2020-01-22 09:11:56 -08:00
|
|
|
];
|
|
|
|
|
|
|
|
// Try to borrow mut the same account
|
2020-06-24 13:52:38 -07:00
|
|
|
let message = Message::new(
|
2021-03-03 21:46:48 -08:00
|
|
|
&[Instruction::new_with_bincode(
|
2020-06-24 13:52:38 -07:00
|
|
|
mock_program_id,
|
|
|
|
&MockSystemInstruction::BorrowFail,
|
|
|
|
account_metas.clone(),
|
|
|
|
)],
|
2021-07-05 04:49:37 -07:00
|
|
|
Some(&accounts[0].0),
|
2020-06-24 13:52:38 -07:00
|
|
|
);
|
2021-09-29 10:05:25 -07:00
|
|
|
let result = MessageProcessor::process_message(
|
|
|
|
&instruction_processor,
|
2020-09-14 17:42:37 -07:00
|
|
|
&message,
|
2021-09-09 23:36:21 -07:00
|
|
|
&program_indices,
|
2020-09-14 17:42:37 -07:00
|
|
|
&accounts,
|
|
|
|
&rent_collector,
|
|
|
|
None,
|
|
|
|
executors.clone(),
|
2020-09-24 07:36:22 -07:00
|
|
|
None,
|
2020-09-29 20:18:28 -07:00
|
|
|
Arc::new(FeatureSet::all_enabled()),
|
2021-07-22 10:18:51 -07:00
|
|
|
ComputeBudget::new(),
|
2021-10-21 11:57:42 -07:00
|
|
|
ThisComputeMeter::new_ref(std::i64::MAX as u64),
|
2021-03-03 15:07:45 -08:00
|
|
|
&mut ExecuteDetailsTimings::default(),
|
2021-08-06 13:52:27 -07:00
|
|
|
Arc::new(Accounts::default_for_tests()),
|
2021-04-12 16:04:57 -07:00
|
|
|
&ancestors,
|
2021-07-29 01:50:20 -07:00
|
|
|
Hash::default(),
|
2021-10-22 14:32:40 -07:00
|
|
|
0,
|
2020-09-14 17:42:37 -07:00
|
|
|
);
|
2020-01-22 09:11:56 -08:00
|
|
|
assert_eq!(
|
|
|
|
result,
|
|
|
|
Err(TransactionError::InstructionError(
|
|
|
|
0,
|
|
|
|
InstructionError::AccountBorrowFailed
|
|
|
|
))
|
|
|
|
);
|
|
|
|
|
|
|
|
// Try to borrow mut the same account in a safe way
|
2020-06-24 13:52:38 -07:00
|
|
|
let message = Message::new(
|
2021-03-03 21:46:48 -08:00
|
|
|
&[Instruction::new_with_bincode(
|
2020-06-24 13:52:38 -07:00
|
|
|
mock_program_id,
|
|
|
|
&MockSystemInstruction::MultiBorrowMut,
|
|
|
|
account_metas.clone(),
|
|
|
|
)],
|
2021-07-05 04:49:37 -07:00
|
|
|
Some(&accounts[0].0),
|
2020-06-24 13:52:38 -07:00
|
|
|
);
|
2021-09-29 10:05:25 -07:00
|
|
|
let result = MessageProcessor::process_message(
|
|
|
|
&instruction_processor,
|
2020-09-14 17:42:37 -07:00
|
|
|
&message,
|
2021-09-09 23:36:21 -07:00
|
|
|
&program_indices,
|
2020-09-14 17:42:37 -07:00
|
|
|
&accounts,
|
|
|
|
&rent_collector,
|
|
|
|
None,
|
|
|
|
executors.clone(),
|
2020-09-24 07:36:22 -07:00
|
|
|
None,
|
2020-09-29 20:18:28 -07:00
|
|
|
Arc::new(FeatureSet::all_enabled()),
|
2021-07-22 10:18:51 -07:00
|
|
|
ComputeBudget::new(),
|
2021-10-21 11:57:42 -07:00
|
|
|
ThisComputeMeter::new_ref(std::i64::MAX as u64),
|
2021-03-03 15:07:45 -08:00
|
|
|
&mut ExecuteDetailsTimings::default(),
|
2021-08-06 13:52:27 -07:00
|
|
|
Arc::new(Accounts::default_for_tests()),
|
2021-04-12 16:04:57 -07:00
|
|
|
&ancestors,
|
2021-07-29 01:50:20 -07:00
|
|
|
Hash::default(),
|
2021-10-22 14:32:40 -07:00
|
|
|
0,
|
2020-09-14 17:42:37 -07:00
|
|
|
);
|
2020-01-22 09:11:56 -08:00
|
|
|
assert_eq!(result, Ok(()));
|
|
|
|
|
|
|
|
// Do work on the same account but at different location in keyed_accounts[]
|
2020-06-24 13:52:38 -07:00
|
|
|
let message = Message::new(
|
2021-03-03 21:46:48 -08:00
|
|
|
&[Instruction::new_with_bincode(
|
2020-06-24 13:52:38 -07:00
|
|
|
mock_program_id,
|
|
|
|
&MockSystemInstruction::DoWork {
|
|
|
|
lamports: 10,
|
|
|
|
data: 42,
|
|
|
|
},
|
|
|
|
account_metas,
|
|
|
|
)],
|
2021-07-05 04:49:37 -07:00
|
|
|
Some(&accounts[0].0),
|
2020-06-24 13:52:38 -07:00
|
|
|
);
|
2021-04-12 16:04:57 -07:00
|
|
|
let ancestors = Ancestors::default();
|
2021-09-29 10:05:25 -07:00
|
|
|
let result = MessageProcessor::process_message(
|
|
|
|
&instruction_processor,
|
2020-09-14 17:42:37 -07:00
|
|
|
&message,
|
2021-09-09 23:36:21 -07:00
|
|
|
&program_indices,
|
2020-09-14 17:42:37 -07:00
|
|
|
&accounts,
|
|
|
|
&rent_collector,
|
|
|
|
None,
|
|
|
|
executors,
|
2020-09-24 07:36:22 -07:00
|
|
|
None,
|
2020-09-29 20:18:28 -07:00
|
|
|
Arc::new(FeatureSet::all_enabled()),
|
2021-07-22 10:18:51 -07:00
|
|
|
ComputeBudget::new(),
|
2021-10-21 11:57:42 -07:00
|
|
|
ThisComputeMeter::new_ref(std::i64::MAX as u64),
|
2021-03-03 15:07:45 -08:00
|
|
|
&mut ExecuteDetailsTimings::default(),
|
2021-08-06 13:52:27 -07:00
|
|
|
Arc::new(Accounts::default_for_tests()),
|
2021-04-12 16:04:57 -07:00
|
|
|
&ancestors,
|
2021-07-29 01:50:20 -07:00
|
|
|
Hash::default(),
|
2021-10-22 14:32:40 -07:00
|
|
|
0,
|
2020-09-14 17:42:37 -07:00
|
|
|
);
|
2020-01-22 09:11:56 -08:00
|
|
|
assert_eq!(result, Ok(()));
|
2021-07-05 04:49:37 -07:00
|
|
|
assert_eq!(accounts[0].1.borrow().lamports(), 80);
|
|
|
|
assert_eq!(accounts[1].1.borrow().lamports(), 20);
|
|
|
|
assert_eq!(accounts[0].1.borrow().data(), &vec![42]);
|
2020-01-22 09:11:56 -08:00
|
|
|
}
|
2020-04-28 14:33:56 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_process_cross_program() {
|
2020-10-19 12:12:08 -07:00
|
|
|
let caller_program_id = solana_sdk::pubkey::new_rand();
|
|
|
|
let callee_program_id = solana_sdk::pubkey::new_rand();
|
2020-05-20 09:24:57 -07:00
|
|
|
|
2021-03-09 13:06:07 -08:00
|
|
|
let owned_account = AccountSharedData::new(42, 1, &callee_program_id);
|
|
|
|
let not_owned_account = AccountSharedData::new(84, 1, &solana_sdk::pubkey::new_rand());
|
2021-10-12 10:26:55 -07:00
|
|
|
let readonly_account = AccountSharedData::new(168, 1, &solana_sdk::pubkey::new_rand());
|
|
|
|
let loader_account = AccountSharedData::new(0, 0, &native_loader::id());
|
2021-09-13 22:57:37 -07:00
|
|
|
let mut program_account = AccountSharedData::new(1, 0, &native_loader::id());
|
|
|
|
program_account.set_executable(true);
|
2020-04-28 14:33:56 -07:00
|
|
|
|
2021-07-05 04:49:37 -07:00
|
|
|
let accounts = vec![
|
|
|
|
(
|
|
|
|
solana_sdk::pubkey::new_rand(),
|
|
|
|
Rc::new(RefCell::new(owned_account)),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
solana_sdk::pubkey::new_rand(),
|
|
|
|
Rc::new(RefCell::new(not_owned_account)),
|
|
|
|
),
|
2021-09-09 23:36:21 -07:00
|
|
|
(
|
2021-09-13 22:57:37 -07:00
|
|
|
solana_sdk::pubkey::new_rand(),
|
|
|
|
Rc::new(RefCell::new(readonly_account)),
|
2021-09-09 23:36:21 -07:00
|
|
|
),
|
2021-10-12 10:26:55 -07:00
|
|
|
(caller_program_id, Rc::new(RefCell::new(loader_account))),
|
2021-09-13 22:57:37 -07:00
|
|
|
(callee_program_id, Rc::new(RefCell::new(program_account))),
|
2020-04-28 14:33:56 -07:00
|
|
|
];
|
2021-09-21 05:41:02 -07:00
|
|
|
let account_indices = [0, 1, 2];
|
2021-10-12 10:26:55 -07:00
|
|
|
let program_indices = [3, 4];
|
2021-04-19 09:48:48 -07:00
|
|
|
|
2020-10-30 01:10:13 -07:00
|
|
|
let programs: Vec<(_, ProcessInstructionWithContext)> =
|
|
|
|
vec![(callee_program_id, mock_process_instruction)];
|
2021-04-19 09:48:48 -07:00
|
|
|
let metas = vec![
|
2021-07-05 04:49:37 -07:00
|
|
|
AccountMeta::new(accounts[0].0, false),
|
|
|
|
AccountMeta::new(accounts[1].0, false),
|
2021-09-13 22:57:37 -07:00
|
|
|
AccountMeta::new_readonly(accounts[2].0, false),
|
2021-04-19 09:48:48 -07:00
|
|
|
];
|
|
|
|
|
2021-10-12 10:26:55 -07:00
|
|
|
let caller_instruction =
|
|
|
|
CompiledInstruction::new(program_indices[0] as u8, &(), vec![0, 1, 2, 3, 4]);
|
2021-09-13 22:57:37 -07:00
|
|
|
let callee_instruction = Instruction::new_with_bincode(
|
2021-04-19 09:48:48 -07:00
|
|
|
callee_program_id,
|
|
|
|
&MockInstruction::NoopSuccess,
|
|
|
|
metas.clone(),
|
|
|
|
);
|
2021-09-13 22:57:37 -07:00
|
|
|
let message = Message::new(&[callee_instruction], None);
|
|
|
|
|
2021-10-21 11:57:42 -07:00
|
|
|
let mut invoke_context = ThisInvokeContext::new_mock(&accounts, programs.as_slice());
|
2021-09-29 10:05:25 -07:00
|
|
|
invoke_context
|
2021-10-12 23:58:20 -07:00
|
|
|
.push(&message, &caller_instruction, &program_indices[..1], None)
|
2021-09-29 10:05:25 -07:00
|
|
|
.unwrap();
|
2020-04-28 14:33:56 -07:00
|
|
|
|
|
|
|
// not owned account modified by the caller (before the invoke)
|
2021-10-12 10:26:55 -07:00
|
|
|
let demote_program_write_locks =
|
|
|
|
invoke_context.is_feature_active(&demote_program_write_locks::id());
|
2021-03-10 23:04:00 -08:00
|
|
|
let caller_write_privileges = message
|
2021-01-22 15:28:01 -08:00
|
|
|
.account_keys
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
2021-09-03 20:05:30 -07:00
|
|
|
.map(|(i, _)| message.is_writable(i, demote_program_write_locks))
|
2021-01-22 15:28:01 -08:00
|
|
|
.collect::<Vec<bool>>();
|
2021-07-05 04:49:37 -07:00
|
|
|
accounts[0].1.borrow_mut().data_as_mut_slice()[0] = 1;
|
2020-04-28 14:33:56 -07:00
|
|
|
assert_eq!(
|
2021-08-26 17:30:36 -07:00
|
|
|
InstructionProcessor::process_cross_program_instruction(
|
2020-04-28 14:33:56 -07:00
|
|
|
&message,
|
2021-10-12 10:26:55 -07:00
|
|
|
&program_indices[1..],
|
2021-09-21 05:41:02 -07:00
|
|
|
&account_indices,
|
2021-03-10 23:04:00 -08:00
|
|
|
&caller_write_privileges,
|
2020-04-28 14:33:56 -07:00
|
|
|
&mut invoke_context,
|
|
|
|
),
|
|
|
|
Err(InstructionError::ExternalAccountDataModified)
|
|
|
|
);
|
2021-07-05 04:49:37 -07:00
|
|
|
accounts[0].1.borrow_mut().data_as_mut_slice()[0] = 0;
|
2020-04-28 14:33:56 -07:00
|
|
|
|
2021-09-13 22:57:37 -07:00
|
|
|
// readonly account modified by the invoker
|
|
|
|
accounts[2].1.borrow_mut().data_as_mut_slice()[0] = 1;
|
|
|
|
assert_eq!(
|
|
|
|
InstructionProcessor::process_cross_program_instruction(
|
|
|
|
&message,
|
2021-10-12 10:26:55 -07:00
|
|
|
&program_indices[1..],
|
2021-09-21 05:41:02 -07:00
|
|
|
&account_indices,
|
2021-09-13 22:57:37 -07:00
|
|
|
&caller_write_privileges,
|
|
|
|
&mut invoke_context,
|
|
|
|
),
|
|
|
|
Err(InstructionError::ReadonlyDataModified)
|
|
|
|
);
|
|
|
|
accounts[2].1.borrow_mut().data_as_mut_slice()[0] = 0;
|
|
|
|
|
2021-10-12 10:26:55 -07:00
|
|
|
invoke_context.pop();
|
|
|
|
|
2020-04-28 14:33:56 -07:00
|
|
|
let cases = vec![
|
|
|
|
(MockInstruction::NoopSuccess, Ok(())),
|
|
|
|
(
|
|
|
|
MockInstruction::NoopFail,
|
|
|
|
Err(InstructionError::GenericError),
|
|
|
|
),
|
|
|
|
(MockInstruction::ModifyOwned, Ok(())),
|
|
|
|
(
|
|
|
|
MockInstruction::ModifyNotOwned,
|
|
|
|
Err(InstructionError::ExternalAccountDataModified),
|
|
|
|
),
|
|
|
|
];
|
|
|
|
for case in cases {
|
2021-09-13 22:57:37 -07:00
|
|
|
let callee_instruction =
|
2021-03-03 21:46:48 -08:00
|
|
|
Instruction::new_with_bincode(callee_program_id, &case.0, metas.clone());
|
2021-09-13 22:57:37 -07:00
|
|
|
let message = Message::new(&[callee_instruction], None);
|
2021-09-29 10:05:25 -07:00
|
|
|
invoke_context
|
2021-10-12 23:58:20 -07:00
|
|
|
.push(&message, &caller_instruction, &program_indices[..1], None)
|
2021-09-29 10:05:25 -07:00
|
|
|
.unwrap();
|
2021-03-10 23:04:00 -08:00
|
|
|
let caller_write_privileges = message
|
2021-01-22 15:28:01 -08:00
|
|
|
.account_keys
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
2021-09-03 20:05:30 -07:00
|
|
|
.map(|(i, _)| message.is_writable(i, demote_program_write_locks))
|
2021-01-22 15:28:01 -08:00
|
|
|
.collect::<Vec<bool>>();
|
2020-04-28 14:33:56 -07:00
|
|
|
assert_eq!(
|
2021-08-26 17:30:36 -07:00
|
|
|
InstructionProcessor::process_cross_program_instruction(
|
2020-04-28 14:33:56 -07:00
|
|
|
&message,
|
2021-10-12 10:26:55 -07:00
|
|
|
&program_indices[1..],
|
2021-09-21 05:41:02 -07:00
|
|
|
&account_indices,
|
2021-03-10 23:04:00 -08:00
|
|
|
&caller_write_privileges,
|
2020-04-28 14:33:56 -07:00
|
|
|
&mut invoke_context,
|
|
|
|
),
|
|
|
|
case.1
|
|
|
|
);
|
2021-10-12 10:26:55 -07:00
|
|
|
invoke_context.pop();
|
2020-04-28 14:33:56 -07:00
|
|
|
}
|
|
|
|
}
|
2021-09-13 22:57:37 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_native_invoke() {
|
|
|
|
let caller_program_id = solana_sdk::pubkey::new_rand();
|
|
|
|
let callee_program_id = solana_sdk::pubkey::new_rand();
|
|
|
|
|
|
|
|
let owned_account = AccountSharedData::new(42, 1, &callee_program_id);
|
|
|
|
let not_owned_account = AccountSharedData::new(84, 1, &solana_sdk::pubkey::new_rand());
|
2021-10-12 10:26:55 -07:00
|
|
|
let readonly_account = AccountSharedData::new(168, 1, &solana_sdk::pubkey::new_rand());
|
|
|
|
let loader_account = AccountSharedData::new(0, 0, &native_loader::id());
|
2021-09-13 22:57:37 -07:00
|
|
|
let mut program_account = AccountSharedData::new(1, 0, &native_loader::id());
|
|
|
|
program_account.set_executable(true);
|
|
|
|
|
|
|
|
let accounts = vec![
|
|
|
|
(
|
|
|
|
solana_sdk::pubkey::new_rand(),
|
|
|
|
Rc::new(RefCell::new(owned_account)),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
solana_sdk::pubkey::new_rand(),
|
|
|
|
Rc::new(RefCell::new(not_owned_account)),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
solana_sdk::pubkey::new_rand(),
|
|
|
|
Rc::new(RefCell::new(readonly_account)),
|
|
|
|
),
|
2021-10-12 10:26:55 -07:00
|
|
|
(caller_program_id, Rc::new(RefCell::new(loader_account))),
|
2021-09-13 22:57:37 -07:00
|
|
|
(callee_program_id, Rc::new(RefCell::new(program_account))),
|
|
|
|
];
|
2021-10-12 10:26:55 -07:00
|
|
|
let program_indices = [3];
|
2021-09-13 22:57:37 -07:00
|
|
|
let programs: Vec<(_, ProcessInstructionWithContext)> =
|
|
|
|
vec![(callee_program_id, mock_process_instruction)];
|
|
|
|
let metas = vec![
|
|
|
|
AccountMeta::new(accounts[0].0, false),
|
|
|
|
AccountMeta::new(accounts[1].0, false),
|
|
|
|
AccountMeta::new_readonly(accounts[2].0, false),
|
|
|
|
];
|
|
|
|
|
2021-10-12 10:26:55 -07:00
|
|
|
let caller_instruction =
|
|
|
|
CompiledInstruction::new(program_indices[0] as u8, &(), vec![0, 1, 2, 3, 4]);
|
2021-09-13 22:57:37 -07:00
|
|
|
let callee_instruction = Instruction::new_with_bincode(
|
|
|
|
callee_program_id,
|
|
|
|
&MockInstruction::NoopSuccess,
|
|
|
|
metas.clone(),
|
|
|
|
);
|
|
|
|
let message = Message::new(&[callee_instruction.clone()], None);
|
|
|
|
|
2021-10-21 11:57:42 -07:00
|
|
|
let mut invoke_context = ThisInvokeContext::new_mock(&accounts, programs.as_slice());
|
2021-09-29 10:05:25 -07:00
|
|
|
invoke_context
|
2021-10-12 23:58:20 -07:00
|
|
|
.push(&message, &caller_instruction, &program_indices, None)
|
2021-09-29 10:05:25 -07:00
|
|
|
.unwrap();
|
2021-09-13 22:57:37 -07:00
|
|
|
|
|
|
|
// not owned account modified by the invoker
|
|
|
|
accounts[0].1.borrow_mut().data_as_mut_slice()[0] = 1;
|
|
|
|
assert_eq!(
|
|
|
|
InstructionProcessor::native_invoke(
|
|
|
|
&mut invoke_context,
|
|
|
|
callee_instruction.clone(),
|
|
|
|
&[0, 1, 2, 3],
|
|
|
|
&[]
|
|
|
|
),
|
|
|
|
Err(InstructionError::ExternalAccountDataModified)
|
|
|
|
);
|
|
|
|
accounts[0].1.borrow_mut().data_as_mut_slice()[0] = 0;
|
|
|
|
|
|
|
|
// readonly account modified by the invoker
|
|
|
|
accounts[2].1.borrow_mut().data_as_mut_slice()[0] = 1;
|
|
|
|
assert_eq!(
|
|
|
|
InstructionProcessor::native_invoke(
|
|
|
|
&mut invoke_context,
|
|
|
|
callee_instruction,
|
|
|
|
&[0, 1, 2, 3],
|
|
|
|
&[]
|
|
|
|
),
|
|
|
|
Err(InstructionError::ReadonlyDataModified)
|
|
|
|
);
|
|
|
|
accounts[2].1.borrow_mut().data_as_mut_slice()[0] = 0;
|
|
|
|
|
2021-10-12 10:26:55 -07:00
|
|
|
invoke_context.pop();
|
|
|
|
|
2021-09-13 22:57:37 -07:00
|
|
|
// Other test cases
|
|
|
|
let cases = vec![
|
|
|
|
(MockInstruction::NoopSuccess, Ok(())),
|
|
|
|
(
|
|
|
|
MockInstruction::NoopFail,
|
|
|
|
Err(InstructionError::GenericError),
|
|
|
|
),
|
|
|
|
(MockInstruction::ModifyOwned, Ok(())),
|
|
|
|
(
|
|
|
|
MockInstruction::ModifyNotOwned,
|
|
|
|
Err(InstructionError::ExternalAccountDataModified),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
MockInstruction::ModifyReadonly,
|
|
|
|
Err(InstructionError::ReadonlyDataModified),
|
|
|
|
),
|
|
|
|
];
|
|
|
|
for case in cases {
|
|
|
|
let callee_instruction =
|
|
|
|
Instruction::new_with_bincode(callee_program_id, &case.0, metas.clone());
|
|
|
|
let message = Message::new(&[callee_instruction.clone()], None);
|
2021-09-29 10:05:25 -07:00
|
|
|
invoke_context
|
2021-10-12 23:58:20 -07:00
|
|
|
.push(&message, &caller_instruction, &program_indices, None)
|
2021-09-29 10:05:25 -07:00
|
|
|
.unwrap();
|
2021-09-13 22:57:37 -07:00
|
|
|
assert_eq!(
|
|
|
|
InstructionProcessor::native_invoke(
|
|
|
|
&mut invoke_context,
|
|
|
|
callee_instruction,
|
|
|
|
&[0, 1, 2, 3],
|
|
|
|
&[]
|
|
|
|
),
|
|
|
|
case.1
|
|
|
|
);
|
2021-10-12 10:26:55 -07:00
|
|
|
invoke_context.pop();
|
2021-09-13 22:57:37 -07:00
|
|
|
}
|
|
|
|
}
|
2021-09-28 23:25:08 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_precompile() {
|
|
|
|
let mock_program_id = Pubkey::new_unique();
|
|
|
|
fn mock_process_instruction(
|
2021-10-08 02:41:07 -07:00
|
|
|
_first_instruction_account: usize,
|
2021-09-28 23:25:08 -07:00
|
|
|
_data: &[u8],
|
|
|
|
_invoke_context: &mut dyn InvokeContext,
|
|
|
|
) -> Result<(), InstructionError> {
|
|
|
|
Err(InstructionError::Custom(0xbabb1e))
|
|
|
|
}
|
2021-09-29 10:05:25 -07:00
|
|
|
let mut instruction_processor = InstructionProcessor::default();
|
|
|
|
instruction_processor.add_program(&mock_program_id, mock_process_instruction);
|
2021-09-28 23:25:08 -07:00
|
|
|
|
|
|
|
let secp256k1_account = AccountSharedData::new_ref(1, 0, &native_loader::id());
|
|
|
|
secp256k1_account.borrow_mut().set_executable(true);
|
|
|
|
let mock_program_account = AccountSharedData::new_ref(1, 0, &native_loader::id());
|
|
|
|
mock_program_account.borrow_mut().set_executable(true);
|
|
|
|
let accounts = vec![
|
|
|
|
(secp256k1_program::id(), secp256k1_account),
|
|
|
|
(mock_program_id, mock_program_account),
|
|
|
|
];
|
|
|
|
|
|
|
|
let message = Message::new(
|
|
|
|
&[
|
|
|
|
new_secp256k1_instruction(
|
|
|
|
&libsecp256k1::SecretKey::random(&mut rand::thread_rng()),
|
|
|
|
b"hello",
|
|
|
|
),
|
|
|
|
Instruction::new_with_bytes(mock_program_id, &[], vec![]),
|
|
|
|
],
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
|
2021-09-29 10:05:25 -07:00
|
|
|
let result = MessageProcessor::process_message(
|
|
|
|
&instruction_processor,
|
2021-09-28 23:25:08 -07:00
|
|
|
&message,
|
|
|
|
&[vec![0], vec![1]],
|
|
|
|
&accounts,
|
|
|
|
&RentCollector::default(),
|
|
|
|
None,
|
|
|
|
Rc::new(RefCell::new(Executors::default())),
|
|
|
|
None,
|
|
|
|
Arc::new(FeatureSet::all_enabled()),
|
|
|
|
ComputeBudget::new(),
|
2021-10-21 11:57:42 -07:00
|
|
|
ThisComputeMeter::new_ref(std::i64::MAX as u64),
|
2021-09-28 23:25:08 -07:00
|
|
|
&mut ExecuteDetailsTimings::default(),
|
|
|
|
Arc::new(Accounts::default_for_tests()),
|
|
|
|
&Ancestors::default(),
|
|
|
|
Hash::default(),
|
2021-10-22 14:32:40 -07:00
|
|
|
0,
|
2021-09-28 23:25:08 -07:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
result,
|
|
|
|
Err(TransactionError::InstructionError(
|
|
|
|
1,
|
|
|
|
InstructionError::Custom(0xbabb1e)
|
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|
2019-02-18 22:26:22 -08:00
|
|
|
}
|