Refactor: CPI Instruction Recording (#22111)

* Unifies all InstructionRecorders of a transaction into one.

* Stops explicitly compiling CPI instructions for recording,
uses the indices gathered from instruction_accounts instead.
This commit is contained in:
Alexander Meißner
2021-12-25 13:35:43 +01:00
committed by GitHub
parent 60ddd93d09
commit cc947cad03
6 changed files with 64 additions and 51 deletions

View File

@@ -1,30 +1,32 @@
use {
solana_sdk::{
instruction::{CompiledInstruction, Instruction},
message::SanitizedMessage,
},
solana_sdk::instruction::CompiledInstruction,
std::{cell::RefCell, rc::Rc},
};
/// Records and compiles cross-program invoked instructions
#[derive(Clone, Default)]
#[derive(Clone)]
pub struct InstructionRecorder {
inner: Rc<RefCell<Vec<Instruction>>>,
records: Vec<Vec<CompiledInstruction>>,
}
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 new_ref(instructions_in_message: usize) -> Rc<RefCell<Self>> {
Rc::new(RefCell::new(Self {
records: Vec::with_capacity(instructions_in_message),
}))
}
pub fn record_instruction(&self, instruction: Instruction) {
self.inner.borrow_mut().push(instruction);
pub fn deconstruct(self) -> Vec<Vec<CompiledInstruction>> {
self.records
}
pub fn begin_next_recording(&mut self) {
self.records.push(Vec::new());
}
pub fn record_compiled_instruction(&mut self, instruction: CompiledInstruction) {
if let Some(records) = self.records.last_mut() {
records.push(instruction);
}
}
}