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

@ -0,0 +1,29 @@
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);
}
}

File diff suppressed because it is too large Load Diff

View File

@ -1,8 +1,8 @@
#![cfg_attr(RUSTC_WITH_SPECIALIZATION, feature(min_specialization))]
#![allow(clippy::integer_arithmetic)]
#![allow(clippy::integer_arithmetic)] // TODO: Remove
mod instruction_processor;
mod native_loader;
pub use instruction_processor::*;
pub use native_loader::*;
pub mod instruction_processor;
pub mod instruction_recorder;
pub mod invoke_context;
pub mod log_collector;
pub mod native_loader;

View File

@ -0,0 +1,58 @@
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()));
}
}