Skip to content

Commit

Permalink
refactor(core): remove core program (#3982)
Browse files Browse the repository at this point in the history
  • Loading branch information
grishasobol authored May 27, 2024
1 parent 1af64d6 commit a1977c2
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 310 deletions.
18 changes: 16 additions & 2 deletions core-processor/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ use alloc::{
vec::Vec,
};
use gear_core::{
code::InstrumentedCode,
gas::{GasAllowanceCounter, GasAmount, GasCounter},
ids::{CodeId, MessageId, ProgramId, ReservationId},
memory::{MemoryError, MemorySetupError, PageBuf},
message::{
ContextStore, Dispatch, DispatchKind, IncomingDispatch, MessageWaitedType, StoredDispatch,
},
pages::{numerated::tree::IntervalsTree, GearPage, WasmPage, WasmPagesAmount},
program::{MemoryInfix, Program},
program::MemoryInfix,
reservation::{GasReservationMap, GasReserver},
};
pub use gear_core_backend::error::TrapExplanation;
Expand Down Expand Up @@ -525,9 +526,22 @@ pub struct ExecutableActorData {
pub gas_reservation_map: GasReservationMap,
}

/// Program.
#[derive(Clone, Debug)]
pub(crate) struct Program {
/// Program id.
pub id: ProgramId,
/// Memory infix.
pub memory_infix: MemoryInfix,
/// Instrumented code.
pub code: InstrumentedCode,
/// Allocations.
pub allocations: IntervalsTree<WasmPage>,
}

/// Execution context.
#[derive(Debug)]
pub struct WasmExecutionContext {
pub(crate) struct WasmExecutionContext {
/// A counter for gas.
pub gas_counter: GasCounter,
/// A counter for gas allowance.
Expand Down
33 changes: 19 additions & 14 deletions core-processor/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@

//! Module contains context-structures for processing.

use crate::common::ExecutableActorData;
use crate::common::{ExecutableActorData, Program};
use gear_core::{
code::InstrumentedCode,
gas::{GasAllowanceCounter, GasCounter},
ids::ProgramId,
message::IncomingDispatch,
pages::WasmPagesAmount,
program::Program,
program::MemoryInfix,
reservation::GasReserver,
};

Expand Down Expand Up @@ -111,6 +111,18 @@ pub struct ProcessExecutionContext {
pub(crate) memory_size: WasmPagesAmount,
}

impl ProcessExecutionContext {
/// Returns program id.
pub fn program_id(&self) -> ProgramId {
self.program.id
}

/// Returns memory infix.
pub fn memory_infix(&self) -> MemoryInfix {
self.program.memory_infix
}
}

impl From<(ContextChargedForMemory, InstrumentedCode, u128)> for ProcessExecutionContext {
fn from(args: (ContextChargedForMemory, InstrumentedCode, u128)) -> Self {
let (context, code, balance) = args;
Expand All @@ -128,12 +140,12 @@ impl From<(ContextChargedForMemory, InstrumentedCode, u128)> for ProcessExecutio
memory_size,
} = context;

let program = Program::from_parts(
destination_id,
actor_data.memory_infix,
let program = Program {
id: destination_id,
memory_infix: actor_data.memory_infix,
code,
actor_data.allocations,
);
allocations: actor_data.allocations,
};

// Must be created once per taken from the queue dispatch by program.
let gas_reserver =
Expand All @@ -151,13 +163,6 @@ impl From<(ContextChargedForMemory, InstrumentedCode, u128)> for ProcessExecutio
}
}

impl ProcessExecutionContext {
/// Returns ref to program.
pub fn program(&self) -> &Program {
&self.program
}
}

/// System reservation context.
#[derive(Debug, Default)]
pub struct SystemReservationContext {
Expand Down
66 changes: 38 additions & 28 deletions core-processor/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use crate::{
common::{
ActorExecutionError, ActorExecutionErrorReplyReason, DispatchResult, DispatchResultKind,
ExecutionError, SystemExecutionError, WasmExecutionContext,
ExecutionError, Program, SystemExecutionError, WasmExecutionContext,
},
configs::{BlockInfo, ExecutionSettings},
ext::{ProcessorContext, ProcessorExternalities},
Expand All @@ -36,7 +36,7 @@ use gear_core::{
WasmEntryPoint,
},
pages::{numerated::tree::IntervalsTree, WasmPage},
program::{MemoryInfix, Program},
program::MemoryInfix,
reservation::GasReserver,
};
use gear_core_backend::{
Expand Down Expand Up @@ -71,24 +71,23 @@ where
memory_size,
} = context;

let program_id = program.id();
let kind = dispatch.kind();

log::debug!("Executing program {}", program_id);
log::debug!("Executing program {}", program.id);
log::debug!("Executing dispatch {:?}", dispatch);

// Creating allocations context.
let allocations_context = AllocationsContext::try_new(
memory_size,
program.allocations().clone(),
program.static_pages(),
program.stack_end(),
program.allocations,
program.code.static_pages(),
program.code.stack_end(),
settings.max_pages,
)
.map_err(SystemExecutionError::from)?;

// Creating message context.
let Some(message_context) = MessageContext::new(dispatch.clone(), program_id, msg_ctx_settings)
let Some(message_context) = MessageContext::new(dispatch.clone(), program.id, msg_ctx_settings)
else {
return Err(ActorExecutionError {
gas_amount: gas_counter.to_amount(),
Expand Down Expand Up @@ -123,7 +122,7 @@ where
message_context,
block_info: settings.block_info,
performance_multiplier: settings.performance_multiplier,
program_id,
program_id: program.id,
program_candidates_data: Default::default(),
forbidden_funcs: settings.forbidden_funcs,
reserve_for: settings.reserve_for,
Expand All @@ -141,18 +140,18 @@ where
let execute = || {
let env = Environment::new(
ext,
program.code_bytes(),
program.code.code(),
kind,
program.code().exports().clone(),
program.code.exports().clone(),
memory_size,
)?;
env.execute(|ctx, memory, globals_config| {
Ext::lazy_pages_init_for_program(
ctx,
memory,
program_id,
program.memory_infix(),
program.stack_end(),
program.id,
program.memory_infix,
program.code.stack_end(),
globals_config,
settings.lazy_pages_costs,
)
Expand Down Expand Up @@ -209,7 +208,10 @@ where
DispatchResultKind::Success
}
ActorTerminationReason::Trap(explanation) => {
log::debug!("💥 Trap during execution of {program_id}\n📔 Explanation: {explanation}");
log::debug!(
"💥 Trap during execution of {}\n📔 Explanation: {explanation}",
program.id
);
DispatchResultKind::Trap(explanation)
}
ActorTerminationReason::Wait(duration, waited_type) => {
Expand All @@ -229,7 +231,7 @@ where
Ok(DispatchResult {
kind,
dispatch,
program_id,
program_id: program.id,
context_store: info.context_store,
generated_dispatches: info.generated_dispatches,
awakening: info.awakening,
Expand Down Expand Up @@ -264,10 +266,18 @@ where
EP: WasmEntryPoint,
{
let (program_id, memory_infix) = program_info.unwrap_or_default();
let program = Program::new(program_id, memory_infix, instrumented_code);
let static_pages = program.static_pages();
let allocations = allocations.unwrap_or_else(|| program.allocations().clone());
let memory_size = allocations.end().map(|p| p.inc()).unwrap_or(static_pages);
let program = Program {
id: program_id,
memory_infix,
code: instrumented_code,
allocations: allocations.unwrap_or_default(),
};
let static_pages = program.code.static_pages();
let memory_size = program
.allocations
.end()
.map(|p| p.inc())
.unwrap_or(static_pages);

let message_context = MessageContext::new(
IncomingDispatch::new(
Expand All @@ -284,7 +294,7 @@ where
),
None,
),
program.id(),
program.id,
Default::default(),
)
.ok_or("Incorrect message store context: out of outgoing bytes limit")?;
Expand All @@ -296,16 +306,16 @@ where
value_counter: ValueCounter::new(Default::default()),
allocations_context: AllocationsContext::try_new(
memory_size,
allocations,
program.allocations,
static_pages,
program.stack_end(),
program.code.stack_end(),
512.into(),
)
.map_err(|e| format!("Failed to create alloc ctx: {e:?}"))?,
message_context,
block_info,
performance_multiplier: gsys::Percent::new(100),
program_id: program.id(),
program_id: program.id,
program_candidates_data: Default::default(),
forbidden_funcs: Default::default(),
reserve_for: Default::default(),
Expand All @@ -324,18 +334,18 @@ where
let execute = || {
let env = Environment::new(
ext,
program.code_bytes(),
program.code.code(),
function,
program.code().exports().clone(),
program.code.exports().clone(),
memory_size,
)?;
env.execute(|ctx, memory, globals_config| {
Ext::lazy_pages_init_for_program(
ctx,
memory,
program_id,
program.memory_infix(),
program.stack_end(),
program.memory_infix,
program.code.stack_end(),
globals_config,
Default::default(),
)
Expand Down
2 changes: 1 addition & 1 deletion core-processor/src/processing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ where

let dispatch = execution_context.dispatch;
let balance = execution_context.balance;
let program_id = execution_context.program.id();
let program_id = execution_context.program.id;
let execution_context = WasmExecutionContext {
gas_counter: execution_context.gas_counter,
gas_allowance_counter: execution_context.gas_allowance_counter,
Expand Down
Loading

0 comments on commit a1977c2

Please sign in to comment.