Replaces MockInvokeContext by ThisInvokeContext in tests (#20881)

* Replaces MockInvokeContext by ThisInvokeContext in BpfLoader, SystemInstructionProcessor, CLIs, ConfigProcessor, StakeProcessor and VoteProcessor.

* Finally, removes MockInvokeContext, MockComputeMeter and MockLogger.

* Adjusts assert_instruction_count test.

* Moves ThisInvokeContext to the program-runtime crate.
This commit is contained in:
Alexander Meißner
2021-11-04 21:47:32 +01:00
committed by GitHub
parent 0597594943
commit 7200c5106e
33 changed files with 2640 additions and 2783 deletions

View File

@@ -47,8 +47,6 @@ use crate::{
cost_tracker::CostTracker,
epoch_stakes::{EpochStakes, NodeVoteAccounts},
inline_spl_token_v2_0,
instruction_recorder::InstructionRecorder,
log_collector::LogCollector,
message_processor::MessageProcessor,
rent_collector::RentCollector,
stake_weighted_timestamp::{
@@ -71,7 +69,11 @@ use rayon::{
};
use solana_measure::measure::Measure;
use solana_metrics::{inc_new_counter_debug, inc_new_counter_info};
use solana_program_runtime::{ExecuteDetailsTimings, Executors, InstructionProcessor};
use solana_program_runtime::{
instruction_processor::{ExecuteDetailsTimings, Executors, InstructionProcessor},
instruction_recorder::InstructionRecorder,
log_collector::LogCollector,
};
#[allow(deprecated)]
use solana_sdk::recent_blockhashes_account;
use solana_sdk::{
@@ -3870,7 +3872,7 @@ impl Bank {
legacy_message,
&loaded_transaction.program_indices,
&account_refcells,
&self.rent_collector,
self.rent_collector.rent,
log_collector.clone(),
executors.clone(),
instruction_recorders.as_deref(),

View File

@@ -1,29 +0,0 @@
use std::{cell::RefCell, rc::Rc};
use solana_sdk::{
instruction::{CompiledInstruction, Instruction},
message::SanitizedMessage,
};
/// Records and compiles cross-program invoked instructions
#[derive(Clone, Default)]
pub struct InstructionRecorder {
inner: Rc<RefCell<Vec<Instruction>>>,
}
impl InstructionRecorder {
pub fn compile_instructions(
&self,
message: &SanitizedMessage,
) -> Option<Vec<CompiledInstruction>> {
self.inner
.borrow()
.iter()
.map(|ix| message.try_compile_instruction(ix))
.collect()
}
pub fn record_instruction(&self, instruction: Instruction) {
self.inner.borrow_mut().push(instruction);
}
}

View File

@@ -32,9 +32,7 @@ pub mod genesis_utils;
pub mod hardened_unpack;
pub mod in_mem_accounts_index;
pub mod inline_spl_token_v2_0;
pub mod instruction_recorder;
pub mod loader_utils;
pub mod log_collector;
pub mod message_processor;
pub mod neon_evm_program;
pub mod non_circulating_supply;

View File

@@ -1,58 +0,0 @@
use std::cell::RefCell;
const LOG_MESSAGES_BYTES_LIMIT: usize = 10 * 1000;
#[derive(Default)]
struct LogCollectorInner {
messages: Vec<String>,
bytes_written: usize,
limit_warning: bool,
}
#[derive(Default)]
pub struct LogCollector {
inner: RefCell<LogCollectorInner>,
}
impl LogCollector {
pub fn log(&self, message: &str) {
let mut inner = self.inner.borrow_mut();
if inner.bytes_written + message.len() >= LOG_MESSAGES_BYTES_LIMIT {
if !inner.limit_warning {
inner.limit_warning = true;
inner.messages.push(String::from("Log truncated"));
}
} else {
inner.bytes_written += message.len();
inner.messages.push(message.to_string());
}
}
}
impl From<LogCollector> for Vec<String> {
fn from(log_collector: LogCollector) -> Self {
log_collector.inner.into_inner().messages
}
}
#[cfg(test)]
pub(crate) mod tests {
use super::*;
#[test]
fn test_log_messages_bytes_limit() {
let lc = LogCollector::default();
for _i in 0..LOG_MESSAGES_BYTES_LIMIT * 2 {
lc.log("x");
}
let logs: Vec<_> = lc.into();
assert_eq!(logs.len(), LOG_MESSAGES_BYTES_LIMIT);
for log in logs.iter().take(LOG_MESSAGES_BYTES_LIMIT - 1) {
assert_eq!(*log, "x".to_string());
}
assert_eq!(logs.last(), Some(&"Log truncated".to_string()));
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -254,15 +254,14 @@ impl<'a> NonceKeyedAccount for KeyedAccount<'a> {
#[cfg(test)]
mod test {
use super::*;
use solana_program_runtime::invoke_context::ThisInvokeContext;
use solana_sdk::{
account::ReadableAccount,
account_utils::State as AccountUtilsState,
hash::{hash, Hash},
keyed_account::KeyedAccount,
nonce::{self, State},
nonce_account::create_account,
nonce_account::verify_nonce_account,
process_instruction::MockInvokeContext,
nonce_account::{create_account, verify_nonce_account},
system_instruction::SystemError,
};
@@ -283,11 +282,11 @@ mod test {
)
}
fn create_invoke_context_with_blockhash<'a>(seed: usize) -> MockInvokeContext<'a> {
let mut invoke_context = MockInvokeContext::new(&Pubkey::default(), vec![]);
fn create_invoke_context_with_blockhash<'a>(seed: usize) -> ThisInvokeContext<'a> {
let mut invoke_context = ThisInvokeContext::new_mock(&[], &[]);
let (blockhash, lamports_per_signature) = create_test_blockhash(seed);
invoke_context.blockhash = blockhash;
invoke_context.lamports_per_signature = lamports_per_signature;
invoke_context.set_blockhash(blockhash);
invoke_context.set_lamports_per_signature(lamports_per_signature);
invoke_context
}
@@ -974,12 +973,11 @@ mod test {
let min_lamports = rent.minimum_balance(State::size());
with_test_keyed_account(min_lamports + 42, true, |nonce_account| {
let mut signers = HashSet::new();
let invoke_context = MockInvokeContext::new(&Pubkey::default(), vec![]);
signers.insert(*nonce_account.signer_key().unwrap());
let result = nonce_account.authorize_nonce_account(
&Pubkey::default(),
&signers,
&invoke_context,
&ThisInvokeContext::new_mock(&[], &[]),
);
assert_eq!(result, Err(InstructionError::InvalidAccountData));
})

View File

@@ -24,7 +24,7 @@ use {
rayon::prelude::*,
serde::{de::DeserializeOwned, Deserialize, Serialize},
solana_measure::measure::Measure,
solana_program_runtime::InstructionProcessor,
solana_program_runtime::instruction_processor::InstructionProcessor,
solana_sdk::{
clock::{Epoch, Slot, UnixTimestamp},
epoch_schedule::EpochSchedule,

File diff suppressed because it is too large Load Diff