use crate::{
common::Program,
precharge::{ContextCharged, ForModuleInstantiation},
};
use gear_core::{
code::InstrumentedCodeAndMetadata,
gas::{GasAllowanceCounter, GasCounter},
ids::ActorId,
message::IncomingDispatch,
pages::WasmPagesAmount,
program::MemoryInfix,
reservation::GasReserver,
};
pub struct ProcessExecutionContext {
pub(crate) gas_counter: GasCounter,
pub(crate) gas_allowance_counter: GasAllowanceCounter,
pub(crate) gas_reserver: GasReserver,
pub(crate) dispatch: IncomingDispatch,
pub(crate) balance: u128,
pub(crate) program: Program,
pub(crate) memory_size: WasmPagesAmount,
}
impl ProcessExecutionContext {
pub fn new(
context: ContextCharged<ForModuleInstantiation>,
instrumented_code_and_metadata: InstrumentedCodeAndMetadata,
balance: u128,
) -> Self {
let (
destination_id,
dispatch,
gas_counter,
gas_allowance_counter,
actor_data,
allocations_data,
) = context.into_final_parts();
let program = Program {
id: destination_id,
memory_infix: actor_data.memory_infix,
instrumented_code: instrumented_code_and_metadata.instrumented_code,
code_metadata: instrumented_code_and_metadata.metadata,
allocations: actor_data.allocations,
};
let gas_reserver = GasReserver::new(
&dispatch,
actor_data.gas_reservation_map,
allocations_data.max_reservations,
);
Self {
gas_counter,
gas_allowance_counter,
gas_reserver,
dispatch,
balance,
program,
memory_size: allocations_data.memory_size,
}
}
pub fn program_id(&self) -> ActorId {
self.program.id
}
pub fn memory_infix(&self) -> MemoryInfix {
self.program.memory_infix
}
}
#[derive(Debug, Default)]
pub struct SystemReservationContext {
pub current_reservation: Option<u64>,
pub previous_reservation: Option<u64>,
}
impl SystemReservationContext {
pub fn from_dispatch(dispatch: &IncomingDispatch) -> Self {
Self {
current_reservation: None,
previous_reservation: dispatch
.context()
.as_ref()
.and_then(|ctx| ctx.system_reservation()),
}
}
pub fn has_any(&self) -> bool {
self.current_reservation.is_some() || self.previous_reservation.is_some()
}
}