Refactor: Cleanup program-runtime dyn Traits (#21395)

* Unifies dyn Trait ComputeMeter, ThisComputeMeter and TransactionComputeMeter.

* Unifies dyn Trait Logger and ThisLogger.

* Moves Logger to log_collector.rs

* Unifies Logger and LogCollector.

* Removes inner RefCell from LogCollector.

* Adds the log::debug!() message to ic_logger_msg!() again.
This commit is contained in:
Alexander Meißner
2021-11-23 13:23:40 +01:00
committed by GitHub
parent cd5a39ee43
commit 22a2537aac
9 changed files with 302 additions and 340 deletions

View File

@ -102,7 +102,7 @@ use solana_sdk::{
hash::{extend_and_hash, hashv, Hash},
incinerator,
inflation::Inflation,
instruction::{CompiledInstruction, InstructionError},
instruction::CompiledInstruction,
lamports::LamportsError,
message::SanitizedMessage,
native_loader,
@ -426,28 +426,6 @@ impl CachedExecutors {
}
}
pub struct TransactionComputeMeter {
remaining: u64,
}
impl TransactionComputeMeter {
pub fn new(cap: u64) -> Self {
Self { remaining: cap }
}
}
impl ComputeMeter for TransactionComputeMeter {
fn consume(&mut self, amount: u64) -> std::result::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)]
pub struct BankRc {
/// where all the Accounts are stored
@ -3703,24 +3681,6 @@ impl Bank {
Ok(())
}
fn collect_log_messages(
log_collector: Option<Rc<LogCollector>>,
) -> Option<TransactionLogMessages> {
log_collector.and_then(|log_collector| Rc::try_unwrap(log_collector).map(Into::into).ok())
}
fn compile_recorded_instructions(
instruction_recorders: Option<Vec<InstructionRecorder>>,
message: &SanitizedMessage,
) -> Option<InnerInstructionsList> {
instruction_recorders.and_then(|instruction_recorders| {
instruction_recorders
.into_iter()
.map(|r| r.compile_instructions(message))
.collect()
})
}
/// Get any cached executors needed by the transaction
fn get_executors(
&self,
@ -3882,14 +3842,12 @@ impl Bank {
};
let log_collector = if enable_log_recording {
Some(Rc::new(LogCollector::default()))
Some(LogCollector::new_ref())
} else {
None
};
let compute_meter = Rc::new(RefCell::new(TransactionComputeMeter::new(
compute_budget.max_units,
)));
let compute_meter = ComputeMeter::new_ref(compute_budget.max_units);
let (blockhash, lamports_per_signature) = {
let blockhash_queue = self.blockhash_queue.read().unwrap();
@ -3925,11 +3883,21 @@ impl Bank {
process_result = Err(TransactionError::UnsupportedVersion);
}
transaction_log_messages.push(Self::collect_log_messages(log_collector));
inner_instructions.push(Self::compile_recorded_instructions(
instruction_recorders,
tx.message(),
));
let log_messages: Option<TransactionLogMessages> =
log_collector.and_then(|log_collector| {
Rc::try_unwrap(log_collector)
.map(|log_collector| log_collector.into_inner().into())
.ok()
});
transaction_log_messages.push(log_messages);
let inner_instruction_list: Option<InnerInstructionsList> =
instruction_recorders.and_then(|instruction_recorders| {
instruction_recorders
.into_iter()
.map(|r| r.compile_instructions(tx.message()))
.collect()
});
inner_instructions.push(inner_instruction_list);
if let Err(e) = Self::refcells_to_accounts(
&mut loaded_transaction.accounts,

View File

@ -17,7 +17,7 @@ fn process_instruction_with_program_logging(
instruction_data: &[u8],
invoke_context: &mut dyn InvokeContext,
) -> Result<(), InstructionError> {
let logger = invoke_context.get_logger();
let logger = invoke_context.get_log_collector();
let program_id = invoke_context.get_caller()?;
stable_log::program_invoke(&logger, program_id, invoke_context.invoke_depth());

View File

@ -45,12 +45,12 @@ impl MessageProcessor {
program_indices: &[Vec<usize>],
accounts: &[(Pubkey, Rc<RefCell<AccountSharedData>>)],
rent: Rent,
log_collector: Option<Rc<LogCollector>>,
log_collector: Option<Rc<RefCell<LogCollector>>>,
executors: Rc<RefCell<Executors>>,
instruction_recorders: Option<&[InstructionRecorder]>,
feature_set: Arc<FeatureSet>,
compute_budget: ComputeBudget,
compute_meter: Rc<RefCell<dyn ComputeMeter>>,
compute_meter: Rc<RefCell<ComputeMeter>>,
timings: &mut ExecuteDetailsTimings,
sysvars: &[(Pubkey, Vec<u8>)],
blockhash: Hash,
@ -134,7 +134,7 @@ impl MessageProcessor {
mod tests {
use super::*;
use crate::rent_collector::RentCollector;
use solana_program_runtime::invoke_context::ThisComputeMeter;
use solana_program_runtime::invoke_context::ComputeMeter;
use solana_sdk::{
account::ReadableAccount,
instruction::{AccountMeta, Instruction, InstructionError},
@ -244,7 +244,7 @@ mod tests {
None,
Arc::new(FeatureSet::all_enabled()),
ComputeBudget::new(),
ThisComputeMeter::new_ref(std::i64::MAX as u64),
ComputeMeter::new_ref(std::i64::MAX as u64),
&mut ExecuteDetailsTimings::default(),
&[],
Hash::default(),
@ -274,7 +274,7 @@ mod tests {
None,
Arc::new(FeatureSet::all_enabled()),
ComputeBudget::new(),
ThisComputeMeter::new_ref(std::i64::MAX as u64),
ComputeMeter::new_ref(std::i64::MAX as u64),
&mut ExecuteDetailsTimings::default(),
&[],
Hash::default(),
@ -308,7 +308,7 @@ mod tests {
None,
Arc::new(FeatureSet::all_enabled()),
ComputeBudget::new(),
ThisComputeMeter::new_ref(std::i64::MAX as u64),
ComputeMeter::new_ref(std::i64::MAX as u64),
&mut ExecuteDetailsTimings::default(),
&[],
Hash::default(),
@ -451,7 +451,7 @@ mod tests {
None,
Arc::new(FeatureSet::all_enabled()),
ComputeBudget::new(),
ThisComputeMeter::new_ref(std::i64::MAX as u64),
ComputeMeter::new_ref(std::i64::MAX as u64),
&mut ExecuteDetailsTimings::default(),
&[],
Hash::default(),
@ -485,7 +485,7 @@ mod tests {
None,
Arc::new(FeatureSet::all_enabled()),
ComputeBudget::new(),
ThisComputeMeter::new_ref(std::i64::MAX as u64),
ComputeMeter::new_ref(std::i64::MAX as u64),
&mut ExecuteDetailsTimings::default(),
&[],
Hash::default(),
@ -516,7 +516,7 @@ mod tests {
None,
Arc::new(FeatureSet::all_enabled()),
ComputeBudget::new(),
ThisComputeMeter::new_ref(std::i64::MAX as u64),
ComputeMeter::new_ref(std::i64::MAX as u64),
&mut ExecuteDetailsTimings::default(),
&[],
Hash::default(),
@ -572,7 +572,7 @@ mod tests {
None,
Arc::new(FeatureSet::all_enabled()),
ComputeBudget::new(),
ThisComputeMeter::new_ref(std::i64::MAX as u64),
ComputeMeter::new_ref(std::i64::MAX as u64),
&mut ExecuteDetailsTimings::default(),
&[],
Hash::default(),