Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ethexe): impl InBlockTransition (temp changes within the block) #4272

Merged
merged 8 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions _typos.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ extend-exclude = [
overlayed = "overlayed" # typo in sp-state-machine, won't fix.
ws = "ws"
dur = "dur"
clonable = "clonable"
4 changes: 2 additions & 2 deletions common/src/auxiliary/task_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
//! Auxiliary implementation of the task pool.

use super::{AuxiliaryDoubleStorageWrap, BlockNumber, DoubleBTreeMap};
use crate::scheduler::{ScheduledTask, TaskPoolImpl};
use gear_core::ids::ProgramId;
use crate::scheduler::TaskPoolImpl;
use gear_core::{ids::ProgramId, tasks::ScheduledTask};
use std::cell::RefCell;

/// Task pool implementation that can be used in a native, non-wasm runtimes.
Expand Down
100 changes: 98 additions & 2 deletions common/src/scheduler/scope.rs → common/src/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,104 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use crate::storage::{CountedByKey, DoubleMapStorage, EmptyCallback, KeyIterableByKeyMap};
use core::marker::PhantomData;
//! Module for scheduler implementation.
//!
//! Scheduler provides API for all available regular or time-dependent actions.

use crate::storage::{
CountedByKey, DoubleMapStorage, EmptyCallback, KeyIterableByKeyMap, ValueStorage,
};
use core::{fmt::Debug, marker::PhantomData};

/// Represents scheduler's logic of centralized delayed tasks management logic.
pub trait Scheduler {
/// Block number type of the messenger.
type BlockNumber;
/// Task type.
type Task;
/// Cost type.
type Cost;
/// Inner error type generated by gear's storage types.
type Error: TaskPoolError;
/// Output error of each storage algorithm.
///
/// Implements `From<Self::Error>` to be able to return
/// any required error type.
type OutputError: From<Self::Error> + Debug;

/// Storing costs per block.
type CostsPerBlock: SchedulingCostsPerBlock<BlockNumber = Self::BlockNumber, Cost = Self::Cost>;

/// The first block of incomplete tasks, which have already passed,
/// but still contain tasks to deal with.
///
/// Used for checking if scheduler is able to process
/// current block aimed tasks, or there are some
/// incomplete job from previous blocks.
type FirstIncompleteTasksBlock: ValueStorage<Value = Self::BlockNumber>;

/// Gear task pool.
///
/// Task pool contains tasks with block number when they should be done.
type TaskPool: TaskPool<
BlockNumber = Self::BlockNumber,
Task = Self::Task,
Error = Self::Error,
OutputError = Self::OutputError,
> + CountedByKey<Key = Self::BlockNumber, Length = usize>
+ KeyIterableByKeyMap<Key1 = Self::BlockNumber, Key2 = Self::Task>;

/// Resets all related to messenger storages.
///
/// It's a temporary production solution to avoid DB migrations
/// and would be available for test purposes only in the future.
fn reset() {
Self::FirstIncompleteTasksBlock::kill();
Self::TaskPool::clear();
}
}

/// Storing costs getter trait.
pub trait SchedulingCostsPerBlock {
/// Block number type.
type BlockNumber;
/// Cost type.
type Cost;

/// Extra reserve for being able to pay for blocks with incomplete tasks.
fn reserve_for() -> Self::BlockNumber;

/// Cost for storing code per block.
fn code() -> Self::Cost;
/// Cost for storing message in mailbox per block.
fn mailbox() -> Self::Cost;
/// Cost for storing program per block.
fn program() -> Self::Cost;
/// Cost for storing message in waitlist per block.
fn waitlist() -> Self::Cost;
/// Cost for reservation holding.
fn reservation() -> Self::Cost;
/// Cost for storing message in dispatch stash.
/// Everything sent delayed goes into dispatch stash.
fn dispatch_stash() -> Self::Cost;

/// Derives the cost per block based on the lock identifier
fn by_storage_type(storage: StorageType) -> Self::Cost;
}

/// The type whose variants correspond to various storages used in Gear,
/// including waitlist, mailbox, delayed messages stash etc.
/// Used as a parameter in functions performing some common actions on storages
/// like, for instance, holding cost calculation, to signal a concrete storage kind.
#[derive(Debug, Clone, Copy)]
pub enum StorageType {
Code,
Mailbox,
Program,
Waitlist,
Reservation,
DispatchStash,
}

/// Represents tasks managing logic.
pub trait TaskPool {
Expand Down
120 changes: 0 additions & 120 deletions common/src/scheduler/mod.rs

This file was deleted.

4 changes: 2 additions & 2 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

extern crate alloc;

pub mod buffer;
pub mod code;
pub mod costs;
pub mod env;
Expand All @@ -40,9 +41,8 @@ pub mod pages;
pub mod percent;
pub mod program;
pub mod reservation;

pub mod buffer;
pub mod str;
pub mod tasks;

// This allows all casts from u32 into usize be safe.
const _: () = assert!(size_of::<u32>() <= size_of::<usize>());
23 changes: 12 additions & 11 deletions common/src/scheduler/task.rs → core/src/tasks.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// This file is part of Gear.

// Copyright (C) 2022-2024 Gear Technologies Inc.
// Copyright (C) 2021-2024 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
Expand All @@ -16,21 +16,19 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use crate::Gas;
use gear_core::ids::{CodeId, MessageId, ProgramId, ReservationId};
use sp_runtime::{
codec::{self, Decode, Encode, MaxEncodedLen},
scale_info::{self, TypeInfo},
};
//! The module provides primitives for all available regular or time-dependent tasks.

use crate::ids::{CodeId, MessageId, ProgramId, ReservationId};
use gsys::Gas;
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;

/// Scheduled task sense and required data for processing action.
///
/// CAUTION: NEVER ALLOW `ScheduledTask<AccountId>` BE A BIG DATA.
/// To avoid redundant migrations only append new variant(s) to the enum
/// with an explicit corresponding scale codec index.
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Encode, Decode, TypeInfo, MaxEncodedLen)]
#[codec(crate = codec)]
#[scale_info(crate = scale_info)]
pub enum ScheduledTask<AccountId> {
// Rent charging section.
// -----
Expand Down Expand Up @@ -71,7 +69,9 @@ pub enum ScheduledTask<AccountId> {
/// The message itself stored in DispatchStash.
#[codec(index = 7)]
SendUserMessage {
/// What message to send.
message_id: MessageId,
/// Should it be inserted into users mailbox.
to_mailbox: bool,
},

Expand All @@ -86,6 +86,7 @@ pub enum ScheduledTask<AccountId> {
}

impl<AccountId> ScheduledTask<AccountId> {
/// Processing function of current task with given handler.
pub fn process_with(self, handler: &mut impl TaskHandler<AccountId>) -> Gas {
use ScheduledTask::*;

Expand Down Expand Up @@ -134,10 +135,10 @@ pub trait TaskHandler<AccountId> {
/// Wake message action.
fn wake_message(&mut self, program_id: ProgramId, message_id: MessageId) -> Gas;

// Send delayed message to program action.
/// Send delayed message to program action.
fn send_dispatch(&mut self, stashed_message_id: MessageId) -> Gas;

// Send delayed message to user action.
/// Send delayed message to user action.
fn send_user_message(&mut self, stashed_message_id: MessageId, to_mailbox: bool) -> Gas;

/// Remove gas reservation action.
Expand Down
1 change: 0 additions & 1 deletion ethexe/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ repository.workspace = true
[dependencies]
gear-core.workspace = true
gprimitives = { workspace = true, features = ["serde"] }
common.workspace = true

parity-scale-codec.workspace = true
derive_more.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion ethexe/common/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use gear_core::{
use gprimitives::H256;
use parity_scale_codec::{Decode, Encode};

pub type ScheduledTask = common::scheduler::ScheduledTask<ActorId>;
pub type ScheduledTask = gear_core::tasks::ScheduledTask<ActorId>;

#[derive(Debug, Clone, Default, Encode, Decode, serde::Serialize)]
pub struct BlockHeader {
Expand Down
15 changes: 14 additions & 1 deletion ethexe/common/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use alloc::vec::Vec;
use gear_core::message::ReplyDetails;
use gear_core::message::{Message, ReplyDetails};
use gprimitives::{ActorId, CodeId, MessageId, H256};
use parity_scale_codec::{Decode, Encode};

Expand Down Expand Up @@ -72,6 +72,19 @@ pub struct OutgoingMessage {
pub reply_details: Option<ReplyDetails>,
}

impl From<Message> for OutgoingMessage {
fn from(value: Message) -> Self {
let (id, _source, destination, payload, _gas_limit, value, details) = value.into_parts();
Self {
id,
destination,
payload: payload.into_vec(),
value,
reply_details: details.and_then(|v| v.to_reply_details()),
}
}
}

/* Events section */

#[derive(Clone, Debug, Encode, Decode, PartialEq, Eq)]
Expand Down
Loading
Loading