Skip to content

Commit

Permalink
Merge branch 'master' into cl/issue-3661
Browse files Browse the repository at this point in the history
  • Loading branch information
clearloop authored May 28, 2024
2 parents 9ef1d04 + a1977c2 commit 0f3e90b
Show file tree
Hide file tree
Showing 21 changed files with 431 additions and 513 deletions.
310 changes: 155 additions & 155 deletions Cargo.lock

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace.package]
version = "1.4.1"
version = "1.4.2"
authors = ["Gear Technologies"]
edition = "2021"
license = "GPL-3.0"
Expand Down Expand Up @@ -97,7 +97,7 @@ members = [
]

[workspace.dependencies]
anyhow = "1.0.83"
anyhow = "1.0.86"
arbitrary = "1.3.2"
async-recursion = "1.1.1"
async-trait = "0.1.80"
Expand Down Expand Up @@ -153,8 +153,8 @@ sha-1 = "0.10.1"
subxt = "0.32.1"
subxt-metadata = "0.32.1"
subxt-codegen = "0.32.1"
syn = "2.0.63"
thiserror = "1.0.60"
syn = "2.0.64"
thiserror = "1.0.61"
tokio = { version = "1.37.0" }
uluru = "3.1.0"
url = "2.5.0"
Expand Down Expand Up @@ -460,7 +460,7 @@ schnorrkel = "0.9.1" # gcli
scopeguard = { version = "1.2.0", default-features = false } # pallets/gear
tabled = "0.10.0" # utils/regression-analysis
thousands = "0.2.0" # utils/regression-analysis
toml = "0.8.12" # utils/wasm-builder
toml = "0.8.13" # utils/wasm-builder
tracing = "0.1.40" # utils/node-loader
tracing-appender = "0.2" # utils/node-loader
tracing-subscriber = "0.3.18" # utils/node-loader
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ Gear Protocol provides a developer-friendly programming platform for decentraliz
- **Unique** :crown: : The main idea underpinning the Gear Protocol is the Actor model for message communications - secure, effective, clear.
- **Unique** :crown: : Parallelizable architecture ensures even greater speed.
- **Unique** :crown: : Continued messaging automation through delayed messages enables truly on-chain dApps.
- **Unique** :crown: : Gasless transactions through vouchers to leverage the dApps customer base and overall Web3 adoption.
- **Unique** :crown: : Built-in Actors to provide programs with enhanced access to pallets and to offload high-load computations off-chain.
- **Unique** :crown: : Create a dApp in minutes using Gear Protocol's libraries.
- Programs run in a Wasm VM, enabling near-native code execution speed.
Expand Down
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 0f3e90b

Please sign in to comment.