2020-09-29 01:36:46 -07:00
|
|
|
use solana_sdk::{
|
2021-03-09 15:06:07 -06:00
|
|
|
account::AccountSharedData,
|
2020-09-29 01:36:46 -07:00
|
|
|
instruction::{CompiledInstruction, Instruction, InstructionError},
|
2021-04-19 18:48:48 +02:00
|
|
|
keyed_account::{create_keyed_accounts_unified, KeyedAccount},
|
2020-09-29 01:36:46 -07:00
|
|
|
pubkey::Pubkey,
|
2021-05-20 14:00:50 -07:00
|
|
|
sysvar::Sysvar,
|
2020-09-29 01:36:46 -07:00
|
|
|
};
|
2021-07-09 20:58:18 +09:00
|
|
|
use std::{cell::RefCell, collections::HashSet, fmt::Debug, rc::Rc, sync::Arc};
|
2020-09-29 01:36:46 -07:00
|
|
|
|
2021-04-12 16:04:57 -07:00
|
|
|
/// Prototype of a native loader entry point
|
2020-09-29 01:36:46 -07:00
|
|
|
///
|
|
|
|
/// program_id: Program ID of the currently executing program
|
|
|
|
/// keyed_accounts: Accounts passed as part of the instruction
|
|
|
|
/// instruction_data: Instruction data
|
|
|
|
/// invoke_context: Invocation context
|
|
|
|
pub type LoaderEntrypoint = unsafe extern "C" fn(
|
|
|
|
program_id: &Pubkey,
|
|
|
|
instruction_data: &[u8],
|
|
|
|
invoke_context: &dyn InvokeContext,
|
|
|
|
) -> Result<(), InstructionError>;
|
|
|
|
|
|
|
|
pub type ProcessInstructionWithContext =
|
2021-04-19 18:48:48 +02:00
|
|
|
fn(&Pubkey, &[u8], &mut dyn InvokeContext) -> Result<(), InstructionError>;
|
|
|
|
|
|
|
|
pub struct InvokeContextStackFrame<'a> {
|
|
|
|
pub key: Pubkey,
|
|
|
|
pub keyed_accounts: Vec<KeyedAccount<'a>>,
|
|
|
|
pub keyed_accounts_range: std::ops::Range<usize>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> InvokeContextStackFrame<'a> {
|
|
|
|
pub fn new(key: Pubkey, keyed_accounts: Vec<KeyedAccount<'a>>) -> Self {
|
|
|
|
let keyed_accounts_range = std::ops::Range {
|
|
|
|
start: 0,
|
|
|
|
end: keyed_accounts.len(),
|
|
|
|
};
|
|
|
|
Self {
|
|
|
|
key,
|
|
|
|
keyed_accounts,
|
|
|
|
keyed_accounts_range,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-29 01:36:46 -07:00
|
|
|
|
|
|
|
/// Invocation context passed to loaders
|
|
|
|
pub trait InvokeContext {
|
2021-04-19 18:48:48 +02:00
|
|
|
/// Push a stack frame onto the invocation stack
|
|
|
|
///
|
|
|
|
/// Used in MessageProcessor::process_cross_program_instruction
|
|
|
|
fn push(
|
|
|
|
&mut self,
|
|
|
|
key: &Pubkey,
|
|
|
|
keyed_accounts: &[(bool, bool, &Pubkey, &RefCell<AccountSharedData>)],
|
|
|
|
) -> Result<(), InstructionError>;
|
|
|
|
/// Pop a stack frame from the invocation stack
|
|
|
|
///
|
|
|
|
/// Used in MessageProcessor::process_cross_program_instruction
|
2020-09-29 01:36:46 -07:00
|
|
|
fn pop(&mut self);
|
2020-11-12 12:44:37 -08:00
|
|
|
/// Current depth of the invocation stake
|
|
|
|
fn invoke_depth(&self) -> usize;
|
2020-09-29 01:36:46 -07:00
|
|
|
/// Verify and update PreAccount state based on program execution
|
|
|
|
fn verify_and_update(
|
|
|
|
&mut self,
|
|
|
|
instruction: &CompiledInstruction,
|
2021-07-05 13:49:37 +02:00
|
|
|
accounts: &[(Pubkey, Rc<RefCell<AccountSharedData>>)],
|
2021-07-07 09:14:00 -05:00
|
|
|
write_privileges: &[bool],
|
2020-09-29 01:36:46 -07:00
|
|
|
) -> Result<(), InstructionError>;
|
|
|
|
/// Get the program ID of the currently executing program
|
|
|
|
fn get_caller(&self) -> Result<&Pubkey, InstructionError>;
|
2021-04-19 18:48:48 +02:00
|
|
|
/// Removes the first keyed account
|
|
|
|
fn remove_first_keyed_account(&mut self) -> Result<(), InstructionError>;
|
|
|
|
/// Get the list of keyed accounts
|
|
|
|
fn get_keyed_accounts(&self) -> Result<&[KeyedAccount], InstructionError>;
|
2020-09-29 01:36:46 -07:00
|
|
|
/// Get a list of built-in programs
|
2020-10-28 20:21:50 -07:00
|
|
|
fn get_programs(&self) -> &[(Pubkey, ProcessInstructionWithContext)];
|
2020-09-29 01:36:46 -07:00
|
|
|
/// Get this invocation's logger
|
|
|
|
fn get_logger(&self) -> Rc<RefCell<dyn Logger>>;
|
|
|
|
/// Get this invocation's compute budget
|
2020-10-28 13:16:13 -07:00
|
|
|
fn get_bpf_compute_budget(&self) -> &BpfComputeBudget;
|
2020-09-29 01:36:46 -07:00
|
|
|
/// Get this invocation's compute meter
|
|
|
|
fn get_compute_meter(&self) -> Rc<RefCell<dyn ComputeMeter>>;
|
|
|
|
/// Loaders may need to do work in order to execute a program. Cache
|
|
|
|
/// the work that can be re-used across executions
|
2020-10-29 23:43:10 -07:00
|
|
|
fn add_executor(&self, pubkey: &Pubkey, executor: Arc<dyn Executor>);
|
2020-09-29 01:36:46 -07:00
|
|
|
/// Get the completed loader work that can be re-used across executions
|
2020-10-29 23:43:10 -07:00
|
|
|
fn get_executor(&self, pubkey: &Pubkey) -> Option<Arc<dyn Executor>>;
|
2020-09-29 01:36:46 -07:00
|
|
|
/// Record invoked instruction
|
|
|
|
fn record_instruction(&self, instruction: &Instruction);
|
2020-09-29 14:36:30 -07:00
|
|
|
/// Get the bank's active feature set
|
|
|
|
fn is_feature_active(&self, feature_id: &Pubkey) -> bool;
|
2021-07-05 13:49:37 +02:00
|
|
|
/// Get an account by its key
|
2021-03-10 23:04:00 -08:00
|
|
|
fn get_account(&self, pubkey: &Pubkey) -> Option<Rc<RefCell<AccountSharedData>>>;
|
2021-03-03 17:07:45 -06:00
|
|
|
/// Update timing
|
|
|
|
fn update_timing(
|
|
|
|
&mut self,
|
|
|
|
serialize_us: u64,
|
|
|
|
create_vm_us: u64,
|
|
|
|
execute_us: u64,
|
|
|
|
deserialize_us: u64,
|
|
|
|
);
|
2021-04-12 16:04:57 -07:00
|
|
|
/// Get sysvar data
|
2021-05-20 14:00:50 -07:00
|
|
|
fn get_sysvar_data(&self, id: &Pubkey) -> Option<Rc<Vec<u8>>>;
|
2020-09-29 01:36:46 -07:00
|
|
|
}
|
|
|
|
|
2021-01-21 09:57:59 -08:00
|
|
|
/// Convenience macro to log a message with an `Rc<RefCell<dyn Logger>>`
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! ic_logger_msg {
|
|
|
|
($logger:expr, $message:expr) => {
|
|
|
|
if let Ok(logger) = $logger.try_borrow_mut() {
|
|
|
|
if logger.log_enabled() {
|
|
|
|
logger.log($message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
($logger:expr, $fmt:expr, $($arg:tt)*) => {
|
|
|
|
if let Ok(logger) = $logger.try_borrow_mut() {
|
|
|
|
if logger.log_enabled() {
|
|
|
|
logger.log(&format!($fmt, $($arg)*));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Convenience macro to log a message with an `InvokeContext`
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! ic_msg {
|
|
|
|
($invoke_context:expr, $message:expr) => {
|
|
|
|
$crate::ic_logger_msg!($invoke_context.get_logger(), $message)
|
|
|
|
};
|
|
|
|
($invoke_context:expr, $fmt:expr, $($arg:tt)*) => {
|
|
|
|
$crate::ic_logger_msg!($invoke_context.get_logger(), $fmt, $($arg)*)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-05-20 14:00:50 -07:00
|
|
|
pub fn get_sysvar<T: Sysvar>(
|
|
|
|
invoke_context: &dyn InvokeContext,
|
|
|
|
id: &Pubkey,
|
|
|
|
) -> Result<T, InstructionError> {
|
|
|
|
let sysvar_data = invoke_context.get_sysvar_data(id).ok_or_else(|| {
|
|
|
|
ic_msg!(invoke_context, "Unable to get sysvar {}", id);
|
|
|
|
InstructionError::UnsupportedSysvar
|
|
|
|
})?;
|
|
|
|
|
|
|
|
bincode::deserialize(&sysvar_data).map_err(|err| {
|
|
|
|
ic_msg!(invoke_context, "Unable to get sysvar {}: {:?}", id, err);
|
|
|
|
InstructionError::UnsupportedSysvar
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-28 13:16:13 -07:00
|
|
|
#[derive(Clone, Copy, Debug, AbiExample)]
|
|
|
|
pub struct BpfComputeBudget {
|
2020-09-29 01:36:46 -07:00
|
|
|
/// Number of compute units that an instruction is allowed. Compute units
|
|
|
|
/// are consumed by program execution, resources they use, etc...
|
|
|
|
pub max_units: u64,
|
|
|
|
/// Number of compute units consumed by a log call
|
|
|
|
pub log_units: u64,
|
|
|
|
/// Number of compute units consumed by a log_u64 call
|
|
|
|
pub log_64_units: u64,
|
|
|
|
/// Number of compute units consumed by a create_program_address call
|
|
|
|
pub create_program_address_units: u64,
|
2020-10-15 09:11:54 -07:00
|
|
|
/// Number of compute units consumed by an invoke call (not including the cost incurred by
|
2020-09-29 01:36:46 -07:00
|
|
|
/// the called program)
|
|
|
|
pub invoke_units: u64,
|
2021-06-02 02:15:19 -07:00
|
|
|
/// Maximum cross-program invocation depth allowed
|
2020-09-29 01:36:46 -07:00
|
|
|
pub max_invoke_depth: usize,
|
2020-10-15 09:11:54 -07:00
|
|
|
/// Base number of compute units consumed to call SHA256
|
2020-09-29 23:29:20 -07:00
|
|
|
pub sha256_base_cost: u64,
|
2020-10-15 09:11:54 -07:00
|
|
|
/// Incremental number of units consumed by SHA256 (based on bytes)
|
2020-09-29 23:29:20 -07:00
|
|
|
pub sha256_byte_cost: u64,
|
2020-10-09 13:07:09 -07:00
|
|
|
/// Maximum BPF to BPF call depth
|
|
|
|
pub max_call_depth: usize,
|
|
|
|
/// Size of a stack frame in bytes, must match the size specified in the LLVM BPF backend
|
|
|
|
pub stack_frame_size: usize,
|
2020-10-15 09:11:54 -07:00
|
|
|
/// Number of compute units consumed by logging a `Pubkey`
|
|
|
|
pub log_pubkey_units: u64,
|
2020-12-28 17:14:17 -08:00
|
|
|
/// Maximum cross-program invocation instruction size
|
|
|
|
pub max_cpi_instruction_size: usize,
|
2021-03-15 22:41:44 -07:00
|
|
|
/// Number of account data bytes per conpute unit charged during a cross-program invocation
|
|
|
|
pub cpi_bytes_per_unit: u64,
|
2021-04-12 16:04:57 -07:00
|
|
|
/// Base number of compute units consumed to get a sysvar
|
|
|
|
pub sysvar_base_cost: u64,
|
2021-07-07 23:15:14 +03:00
|
|
|
/// Number of compute units consumed to call secp256k1_recover
|
|
|
|
pub secp256k1_recover_cost: u64,
|
2021-07-08 10:43:34 -07:00
|
|
|
/// Optional program heap region size, if `None` then loader default
|
|
|
|
pub heap_size: Option<usize>,
|
2020-09-29 01:36:46 -07:00
|
|
|
}
|
2020-10-28 13:16:13 -07:00
|
|
|
impl Default for BpfComputeBudget {
|
2020-09-29 01:36:46 -07:00
|
|
|
fn default() -> Self {
|
2021-02-18 09:56:11 -08:00
|
|
|
Self::new()
|
2020-10-09 10:33:12 -07:00
|
|
|
}
|
|
|
|
}
|
2020-10-28 13:16:13 -07:00
|
|
|
impl BpfComputeBudget {
|
2021-02-18 09:56:11 -08:00
|
|
|
pub fn new() -> Self {
|
2020-10-28 13:16:13 -07:00
|
|
|
BpfComputeBudget {
|
2021-02-18 09:56:11 -08:00
|
|
|
max_units: 200_000,
|
|
|
|
log_units: 100,
|
|
|
|
log_64_units: 100,
|
|
|
|
create_program_address_units: 1500,
|
|
|
|
invoke_units: 1000,
|
|
|
|
max_invoke_depth: 4,
|
2020-09-29 23:29:20 -07:00
|
|
|
sha256_base_cost: 85,
|
|
|
|
sha256_byte_cost: 1,
|
2021-02-18 09:56:11 -08:00
|
|
|
max_call_depth: 64,
|
2020-10-09 13:07:09 -07:00
|
|
|
stack_frame_size: 4_096,
|
2021-02-18 09:56:11 -08:00
|
|
|
log_pubkey_units: 100,
|
|
|
|
max_cpi_instruction_size: 1280, // IPv6 Min MTU size
|
2021-03-15 22:41:44 -07:00
|
|
|
cpi_bytes_per_unit: 250, // ~50MB at 200,000 units
|
2021-04-12 16:04:57 -07:00
|
|
|
sysvar_base_cost: 100,
|
2021-07-07 23:15:14 +03:00
|
|
|
secp256k1_recover_cost: 25_000,
|
2021-07-08 10:43:34 -07:00
|
|
|
heap_size: None,
|
2020-12-28 17:14:17 -08:00
|
|
|
}
|
2020-09-29 01:36:46 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Compute meter
|
|
|
|
pub trait ComputeMeter {
|
|
|
|
/// Consume compute units
|
|
|
|
fn consume(&mut self, amount: u64) -> Result<(), InstructionError>;
|
|
|
|
/// Get the number of remaining compute units
|
|
|
|
fn get_remaining(&self) -> u64;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Log messages
|
|
|
|
pub trait Logger {
|
|
|
|
fn log_enabled(&self) -> bool;
|
2020-11-12 12:44:37 -08:00
|
|
|
|
|
|
|
/// Log a message.
|
|
|
|
///
|
|
|
|
/// Unless explicitly stated, log messages are not considered stable and may change in the
|
|
|
|
/// future as necessary
|
2020-10-29 23:43:10 -07:00
|
|
|
fn log(&self, message: &str);
|
2020-09-29 01:36:46 -07:00
|
|
|
}
|
|
|
|
|
2020-11-12 12:44:37 -08:00
|
|
|
///
|
|
|
|
/// Stable program log messages
|
|
|
|
///
|
|
|
|
/// The format of these log messages should not be modified to avoid breaking downstream consumers
|
|
|
|
/// of program logging
|
|
|
|
///
|
|
|
|
pub mod stable_log {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
/// Log a program invoke.
|
|
|
|
///
|
|
|
|
/// The general form is:
|
|
|
|
/// "Program <address> invoke [<depth>]"
|
|
|
|
pub fn program_invoke(
|
|
|
|
logger: &Rc<RefCell<dyn Logger>>,
|
|
|
|
program_id: &Pubkey,
|
|
|
|
invoke_depth: usize,
|
|
|
|
) {
|
2021-01-21 09:57:59 -08:00
|
|
|
ic_logger_msg!(logger, "Program {} invoke [{}]", program_id, invoke_depth);
|
2020-11-12 12:44:37 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Log a message from the program itself.
|
|
|
|
///
|
|
|
|
/// The general form is:
|
|
|
|
/// "Program log: <program-generated output>"
|
|
|
|
/// That is, any program-generated output is guaranteed to be prefixed by "Program log: "
|
|
|
|
pub fn program_log(logger: &Rc<RefCell<dyn Logger>>, message: &str) {
|
2021-01-21 09:57:59 -08:00
|
|
|
ic_logger_msg!(logger, "Program log: {}", message);
|
2020-11-12 12:44:37 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Log successful program execution.
|
|
|
|
///
|
|
|
|
/// The general form is:
|
|
|
|
/// "Program <address> success"
|
|
|
|
pub fn program_success(logger: &Rc<RefCell<dyn Logger>>, program_id: &Pubkey) {
|
2021-01-21 09:57:59 -08:00
|
|
|
ic_logger_msg!(logger, "Program {} success", program_id);
|
2020-11-12 12:44:37 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Log program execution failure
|
|
|
|
///
|
|
|
|
/// The general form is:
|
|
|
|
/// "Program <address> failed: <program error details>"
|
|
|
|
pub fn program_failure(
|
|
|
|
logger: &Rc<RefCell<dyn Logger>>,
|
|
|
|
program_id: &Pubkey,
|
|
|
|
err: &InstructionError,
|
|
|
|
) {
|
2021-01-21 09:57:59 -08:00
|
|
|
ic_logger_msg!(logger, "Program {} failed: {}", program_id, err);
|
2020-11-12 12:44:37 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-29 01:36:46 -07:00
|
|
|
/// Program executor
|
2020-10-21 01:05:45 +09:00
|
|
|
pub trait Executor: Debug + Send + Sync {
|
2020-09-29 01:36:46 -07:00
|
|
|
/// Execute the program
|
|
|
|
fn execute(
|
|
|
|
&self,
|
2021-01-04 13:45:05 -08:00
|
|
|
loader_id: &Pubkey,
|
2020-09-29 01:36:46 -07:00
|
|
|
program_id: &Pubkey,
|
|
|
|
instruction_data: &[u8],
|
|
|
|
invoke_context: &mut dyn InvokeContext,
|
2020-12-07 09:49:55 +01:00
|
|
|
use_jit: bool,
|
2020-09-29 01:36:46 -07:00
|
|
|
) -> Result<(), InstructionError>;
|
|
|
|
}
|
2020-10-28 20:21:50 -07:00
|
|
|
|
|
|
|
#[derive(Debug, Default, Clone)]
|
|
|
|
pub struct MockComputeMeter {
|
|
|
|
pub remaining: u64,
|
|
|
|
}
|
|
|
|
impl ComputeMeter for MockComputeMeter {
|
|
|
|
fn consume(&mut self, amount: u64) -> Result<(), InstructionError> {
|
|
|
|
let exceeded = self.remaining < amount;
|
|
|
|
self.remaining = self.remaining.saturating_sub(amount);
|
|
|
|
if exceeded {
|
|
|
|
return Err(InstructionError::ComputationalBudgetExceeded);
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
fn get_remaining(&self) -> u64 {
|
|
|
|
self.remaining
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Default, Clone)]
|
|
|
|
pub struct MockLogger {
|
|
|
|
pub log: Rc<RefCell<Vec<String>>>,
|
|
|
|
}
|
|
|
|
impl Logger for MockLogger {
|
|
|
|
fn log_enabled(&self) -> bool {
|
|
|
|
true
|
|
|
|
}
|
2020-10-29 23:43:10 -07:00
|
|
|
fn log(&self, message: &str) {
|
2020-10-28 20:21:50 -07:00
|
|
|
self.log.borrow_mut().push(message.to_string());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-19 18:48:48 +02:00
|
|
|
pub struct MockInvokeContext<'a> {
|
|
|
|
pub invoke_stack: Vec<InvokeContextStackFrame<'a>>,
|
2020-10-28 20:21:50 -07:00
|
|
|
pub logger: MockLogger,
|
2020-10-28 13:16:13 -07:00
|
|
|
pub bpf_compute_budget: BpfComputeBudget,
|
2020-10-28 20:21:50 -07:00
|
|
|
pub compute_meter: MockComputeMeter,
|
2020-10-31 00:36:00 -07:00
|
|
|
pub programs: Vec<(Pubkey, ProcessInstructionWithContext)>,
|
2021-04-12 16:04:57 -07:00
|
|
|
pub accounts: Vec<(Pubkey, Rc<RefCell<AccountSharedData>>)>,
|
|
|
|
pub sysvars: Vec<(Pubkey, Option<Rc<Vec<u8>>>)>,
|
2021-07-09 20:58:18 +09:00
|
|
|
pub disabled_features: HashSet<Pubkey>,
|
2020-10-28 20:21:50 -07:00
|
|
|
}
|
2021-04-19 18:48:48 +02:00
|
|
|
impl<'a> MockInvokeContext<'a> {
|
|
|
|
pub fn new(keyed_accounts: Vec<KeyedAccount<'a>>) -> Self {
|
|
|
|
let bpf_compute_budget = BpfComputeBudget::default();
|
|
|
|
let mut invoke_context = MockInvokeContext {
|
|
|
|
invoke_stack: Vec::with_capacity(bpf_compute_budget.max_invoke_depth),
|
2020-10-28 20:21:50 -07:00
|
|
|
logger: MockLogger::default(),
|
2021-04-19 18:48:48 +02:00
|
|
|
bpf_compute_budget,
|
2020-10-28 20:21:50 -07:00
|
|
|
compute_meter: MockComputeMeter {
|
|
|
|
remaining: std::i64::MAX as u64,
|
|
|
|
},
|
2020-10-31 00:36:00 -07:00
|
|
|
programs: vec![],
|
2021-04-12 16:04:57 -07:00
|
|
|
accounts: vec![],
|
|
|
|
sysvars: vec![],
|
2021-07-09 20:58:18 +09:00
|
|
|
disabled_features: HashSet::default(),
|
2021-04-19 18:48:48 +02:00
|
|
|
};
|
|
|
|
invoke_context
|
|
|
|
.invoke_stack
|
|
|
|
.push(InvokeContextStackFrame::new(
|
|
|
|
Pubkey::default(),
|
|
|
|
keyed_accounts,
|
|
|
|
));
|
|
|
|
invoke_context
|
2020-10-28 20:21:50 -07:00
|
|
|
}
|
|
|
|
}
|
2021-05-20 14:00:50 -07:00
|
|
|
|
|
|
|
pub fn mock_set_sysvar<T: Sysvar>(
|
|
|
|
mock_invoke_context: &mut MockInvokeContext,
|
|
|
|
id: Pubkey,
|
|
|
|
sysvar: T,
|
|
|
|
) -> Result<(), InstructionError> {
|
|
|
|
let mut data = Vec::with_capacity(T::size_of());
|
|
|
|
|
|
|
|
bincode::serialize_into(&mut data, &sysvar).map_err(|err| {
|
|
|
|
ic_msg!(mock_invoke_context, "Unable to serialize sysvar: {:?}", err);
|
|
|
|
InstructionError::GenericError
|
|
|
|
})?;
|
|
|
|
mock_invoke_context.sysvars.push((id, Some(Rc::new(data))));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-04-19 18:48:48 +02:00
|
|
|
impl<'a> InvokeContext for MockInvokeContext<'a> {
|
|
|
|
fn push(
|
|
|
|
&mut self,
|
|
|
|
key: &Pubkey,
|
|
|
|
keyed_accounts: &[(bool, bool, &Pubkey, &RefCell<AccountSharedData>)],
|
|
|
|
) -> Result<(), InstructionError> {
|
|
|
|
fn transmute_lifetime<'a, 'b>(value: Vec<KeyedAccount<'a>>) -> Vec<KeyedAccount<'b>> {
|
|
|
|
unsafe { std::mem::transmute(value) }
|
|
|
|
}
|
|
|
|
self.invoke_stack.push(InvokeContextStackFrame::new(
|
|
|
|
*key,
|
|
|
|
transmute_lifetime(create_keyed_accounts_unified(keyed_accounts)),
|
|
|
|
));
|
2020-10-28 20:21:50 -07:00
|
|
|
Ok(())
|
|
|
|
}
|
2020-11-12 12:44:37 -08:00
|
|
|
fn pop(&mut self) {
|
2021-04-19 18:48:48 +02:00
|
|
|
self.invoke_stack.pop();
|
2020-11-12 12:44:37 -08:00
|
|
|
}
|
|
|
|
fn invoke_depth(&self) -> usize {
|
2021-04-19 18:48:48 +02:00
|
|
|
self.invoke_stack.len()
|
2020-11-12 12:44:37 -08:00
|
|
|
}
|
2020-10-28 20:21:50 -07:00
|
|
|
fn verify_and_update(
|
|
|
|
&mut self,
|
|
|
|
_instruction: &CompiledInstruction,
|
2021-07-05 13:49:37 +02:00
|
|
|
_accounts: &[(Pubkey, Rc<RefCell<AccountSharedData>>)],
|
2021-07-07 09:14:00 -05:00
|
|
|
_write_pivileges: &[bool],
|
2020-10-28 20:21:50 -07:00
|
|
|
) -> Result<(), InstructionError> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
fn get_caller(&self) -> Result<&Pubkey, InstructionError> {
|
2021-04-19 18:48:48 +02:00
|
|
|
self.invoke_stack
|
|
|
|
.last()
|
|
|
|
.map(|frame| &frame.key)
|
|
|
|
.ok_or(InstructionError::CallDepth)
|
|
|
|
}
|
|
|
|
fn remove_first_keyed_account(&mut self) -> Result<(), InstructionError> {
|
|
|
|
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);
|
|
|
|
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-10-28 20:21:50 -07:00
|
|
|
}
|
|
|
|
fn get_programs(&self) -> &[(Pubkey, ProcessInstructionWithContext)] {
|
2020-10-31 00:36:00 -07:00
|
|
|
&self.programs
|
2020-10-28 20:21:50 -07:00
|
|
|
}
|
|
|
|
fn get_logger(&self) -> Rc<RefCell<dyn Logger>> {
|
|
|
|
Rc::new(RefCell::new(self.logger.clone()))
|
|
|
|
}
|
2020-10-28 13:16:13 -07:00
|
|
|
fn get_bpf_compute_budget(&self) -> &BpfComputeBudget {
|
|
|
|
&self.bpf_compute_budget
|
2020-10-28 20:21:50 -07:00
|
|
|
}
|
|
|
|
fn get_compute_meter(&self) -> Rc<RefCell<dyn ComputeMeter>> {
|
|
|
|
Rc::new(RefCell::new(self.compute_meter.clone()))
|
|
|
|
}
|
2020-10-29 23:43:10 -07:00
|
|
|
fn add_executor(&self, _pubkey: &Pubkey, _executor: Arc<dyn Executor>) {}
|
|
|
|
fn get_executor(&self, _pubkey: &Pubkey) -> Option<Arc<dyn Executor>> {
|
2020-10-28 20:21:50 -07:00
|
|
|
None
|
|
|
|
}
|
|
|
|
fn record_instruction(&self, _instruction: &Instruction) {}
|
2021-07-09 20:58:18 +09:00
|
|
|
fn is_feature_active(&self, feature_id: &Pubkey) -> bool {
|
|
|
|
!self.disabled_features.contains(feature_id)
|
2020-10-28 20:21:50 -07:00
|
|
|
}
|
2021-04-12 16:04:57 -07:00
|
|
|
fn get_account(&self, pubkey: &Pubkey) -> Option<Rc<RefCell<AccountSharedData>>> {
|
|
|
|
for (key, account) in self.accounts.iter() {
|
|
|
|
if key == pubkey {
|
|
|
|
return Some(account.clone());
|
|
|
|
}
|
|
|
|
}
|
2020-12-17 15:39:49 -08:00
|
|
|
None
|
|
|
|
}
|
2021-03-03 17:07:45 -06:00
|
|
|
fn update_timing(
|
|
|
|
&mut self,
|
|
|
|
_serialize_us: u64,
|
|
|
|
_create_vm_us: u64,
|
|
|
|
_execute_us: u64,
|
|
|
|
_deserialize_us: u64,
|
|
|
|
) {
|
|
|
|
}
|
2021-05-20 14:00:50 -07:00
|
|
|
fn get_sysvar_data(&self, id: &Pubkey) -> Option<Rc<Vec<u8>>> {
|
2021-04-12 16:04:57 -07:00
|
|
|
self.sysvars
|
|
|
|
.iter()
|
|
|
|
.find_map(|(key, sysvar)| if id == key { sysvar.clone() } else { None })
|
|
|
|
}
|
2020-10-28 20:21:50 -07:00
|
|
|
}
|