From b386b2a1ac98658fe1845cf69a811373d4e48f86 Mon Sep 17 00:00:00 2001 From: clearloop <26088946+clearloop@users.noreply.github.com> Date: Wed, 20 Sep 2023 01:07:10 +0800 Subject: [PATCH] chore(core): move GasInfo to core (#3130) --- Cargo.lock | 1 + core/Cargo.toml | 5 +++++ core/src/gas.rs | 26 +++++++++++++++++++++++++- gsdk/Cargo.toml | 2 +- gsdk/src/lib.rs | 15 ++------------- pallets/gear/rpc/Cargo.toml | 2 +- pallets/gear/src/lib.rs | 22 +--------------------- 7 files changed, 36 insertions(+), 37 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9fe9a6d4da1..ab33d6e89a9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3936,6 +3936,7 @@ dependencies = [ "paste", "proptest", "scale-info", + "serde", "static_assertions", "wabt", "wasmparser-nostd 0.100.1", diff --git a/core/Cargo.toml b/core/Cargo.toml index 666e400433f..6fb6635d08e 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -26,10 +26,15 @@ paste = { workspace = true } enum-iterator.workspace = true byteorder.workspace = true +# Optional dependencies +serde = { workspace = true, features = [ "derive" ], optional = true } + [dev-dependencies] wabt.workspace = true env_logger.workspace = true proptest.workspace = true [features] +default = [] strict = [] +std = ["serde/std"] diff --git a/core/src/gas.rs b/core/src/gas.rs index 8cfe903e66b..022d10116d3 100644 --- a/core/src/gas.rs +++ b/core/src/gas.rs @@ -20,7 +20,10 @@ use crate::costs::RuntimeCosts; use enum_iterator::Sequence; -use scale_info::scale::{Decode, Encode}; +use scale_info::{ + scale::{Decode, Encode}, + TypeInfo, +}; /// The id of the gas lock. #[derive(Debug, Clone, Copy, PartialEq, Eq, Sequence)] @@ -329,6 +332,27 @@ impl From<(i64, i64)> for GasLeft { } } +/// The struct contains results of gas calculation required to process +/// a message. +#[derive(Clone, Debug, Decode, Encode, PartialEq, Eq, TypeInfo)] +#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))] +pub struct GasInfo { + /// Represents minimum gas limit required for execution. + pub min_limit: u64, + /// Gas amount that we reserve for some other on-chain interactions. + pub reserved: u64, + /// Contains number of gas burned during message processing. + pub burned: u64, + /// The value may be returned if a program happens to be executed + /// the second or next time in a block. + pub may_be_returned: u64, + /// Was the message placed into waitlist at the end of calculating. + /// + /// This flag shows, that `min_limit` makes sense and have some guarantees + /// only before insertion into waitlist. + pub waited: bool, +} + #[cfg(test)] mod tests { use super::{ChargeResult, GasCounter}; diff --git a/gsdk/Cargo.toml b/gsdk/Cargo.toml index 3985dbc5494..34946780c3d 100644 --- a/gsdk/Cargo.toml +++ b/gsdk/Cargo.toml @@ -15,7 +15,7 @@ anyhow.workspace = true base64.workspace = true futures-util.workspace = true futures.workspace = true -gear-core.workspace = true +gear-core = { workspace = true, features = [ "std" ] } gear-core-errors.workspace = true hex.workspace = true jsonrpsee = { workspace = true, features = [ "http-client", "ws-client" ] } diff --git a/gsdk/src/lib.rs b/gsdk/src/lib.rs index f4705522088..ea229d6adf6 100644 --- a/gsdk/src/lib.rs +++ b/gsdk/src/lib.rs @@ -31,9 +31,9 @@ use crate::{ ActiveProgram, }, }; +pub use gear_core::gas::GasInfo; use gear_core::ids::{MessageId, ReservationId}; -use parity_scale_codec::{Decode, Encode}; -use serde::{Deserialize, Serialize}; +use parity_scale_codec::Decode; use sp_runtime::AccountId32; use std::collections::HashMap; pub use subxt::dynamic::Value; @@ -71,17 +71,6 @@ pub mod gp { /// Block number type pub type BlockNumber = u32; -/// Information of gas -#[derive(Clone, Debug, Decode, Encode, PartialEq, Eq, Serialize, Deserialize)] -pub struct GasInfo { - /// Represents minimum gas limit required for execution. - pub min_limit: u64, - /// Gas amount that we reserve for some other on-chain interactions. - pub reserved: u64, - /// Contains number of gas burned during message processing. - pub burned: u64, -} - /// Gear gas node id. pub type GearGasNodeId = GasNodeId; diff --git a/pallets/gear/rpc/Cargo.toml b/pallets/gear/rpc/Cargo.toml index 366c3bb3a48..505792028f9 100644 --- a/pallets/gear/rpc/Cargo.toml +++ b/pallets/gear/rpc/Cargo.toml @@ -18,7 +18,7 @@ sp-rpc.workspace = true sp-runtime.workspace = true # Local packages -gear-core.workspace = true +gear-core = { workspace = true, features = ["std"] } gear-core-errors = { workspace = true, features = ["codec"] } gear-common.workspace = true pallet-gear-rpc-runtime-api.workspace = true diff --git a/pallets/gear/src/lib.rs b/pallets/gear/src/lib.rs index 1363b7166a2..61409700809 100644 --- a/pallets/gear/src/lib.rs +++ b/pallets/gear/src/lib.rs @@ -43,6 +43,7 @@ pub use crate::{ pallet::*, schedule::{HostFnWeights, InstructionWeights, Limits, MemoryWeights, Schedule}, }; +pub use gear_core::gas::GasInfo; pub use weights::WeightInfo; use alloc::{format, string::String}; @@ -139,27 +140,6 @@ impl DebugInfo for () { } } -/// The struct contains results of gas calculation required to process -/// a message. -#[derive(Clone, Debug, Decode, Encode, PartialEq, Eq, TypeInfo)] -#[cfg_attr(feature = "std", derive(serde::Deserialize, serde::Serialize))] -pub struct GasInfo { - /// Represents minimum gas limit required for execution. - pub min_limit: u64, - /// Gas amount that we reserve for some other on-chain interactions. - pub reserved: u64, - /// Contains number of gas burned during message processing. - pub burned: u64, - /// The value may be returned if a program happens to be executed - /// the second or next time in a block. - pub may_be_returned: u64, - /// Was the message placed into waitlist at the end of calculating. - /// - /// This flag shows, that `min_limit` makes sense and have some guarantees - /// only before insertion into waitlist. - pub waited: bool, -} - #[frame_support::pallet] pub mod pallet { use super::*;