Skip to content

Commit

Permalink
feat(runtime): Introduce PerformanceMultiplier as Config constant (
Browse files Browse the repository at this point in the history
  • Loading branch information
DennisInSky authored Oct 3, 2023
1 parent 54ad445 commit 4c43268
Show file tree
Hide file tree
Showing 22 changed files with 120 additions and 2 deletions.
1 change: 1 addition & 0 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@ heck = "0.4.1" # gsdk
etc = "0.1.16" # gcli
scale-decode = "0.7.0" # gsdk
directories = "5.0.1" # utils/key-finder
num-traits = { version = "0.2", default-features = false } # gear-core

[profile.release]
panic = "unwind"
Expand Down
4 changes: 4 additions & 0 deletions core-backend/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use gear_core::{
memory::{Memory, MemoryError, MemoryInterval},
message::{HandlePacket, InitPacket, ReplyPacket},
pages::{PageNumber, PageU32Size, WasmPage, WASM_PAGE_SIZE},
percent::Percent,
};
use gear_core_errors::{ReplyCode, SignalCode};
use gear_lazy_pages_common::ProcessAccessError;
Expand Down Expand Up @@ -120,6 +121,9 @@ impl Externalities for MockExt {
fn block_timestamp(&self) -> Result<u64, Self::UnrecoverableError> {
Ok(0)
}
fn performance_multiplier(&self) -> Result<Percent, Self::UnrecoverableError> {
Ok(Percent::new(100))
}
fn send_init(&mut self) -> Result<u32, Self::UnrecoverableError> {
Ok(0)
}
Expand Down
5 changes: 5 additions & 0 deletions core-processor/src/configs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use alloc::{collections::BTreeSet, vec::Vec};
use gear_core::{
costs::{CostPerPage, HostFnWeights},
pages::{GearPage, WasmPage},
percent::Percent,
};
use gear_lazy_pages_common::LazyPagesWeights;
use gear_wasm_instrument::syscalls::SysCallName;
Expand Down Expand Up @@ -144,6 +145,8 @@ impl PageCosts {
pub struct ExecutionSettings {
/// Contextual block information.
pub block_info: BlockInfo,
/// Performance multiplier.
pub performance_multiplier: Percent,
/// Max amount of pages in program memory during execution.
pub max_pages: WasmPage,
/// Pages costs.
Expand Down Expand Up @@ -176,6 +179,8 @@ pub struct ExecutionSettings {
pub struct BlockConfig {
/// Block info.
pub block_info: BlockInfo,
/// Performance multiplier.
pub performance_multiplier: Percent,
/// Max allowed page numbers for wasm program.
pub max_pages: WasmPage,
/// Allocations config.
Expand Down
3 changes: 3 additions & 0 deletions core-processor/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ use gear_core::{
WasmEntryPoint,
},
pages::{PageU32Size, WasmPage},
percent::Percent,
program::Program,
reservation::GasReserver,
};
Expand Down Expand Up @@ -205,6 +206,7 @@ where
allocations_context,
message_context,
block_info: settings.block_info,
performance_multiplier: settings.performance_multiplier,
max_pages: settings.max_pages,
page_costs: settings.page_costs,
existential_deposit: settings.existential_deposit,
Expand Down Expand Up @@ -399,6 +401,7 @@ where
ContextSettings::new(0, 0, 0, 0, 0, 0),
),
block_info,
performance_multiplier: Percent::new(100),
max_pages: 512.into(),
page_costs: Default::default(),
existential_deposit: Default::default(),
Expand Down
8 changes: 8 additions & 0 deletions core-processor/src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ use gear_core::{
MessageContext, Packet, ReplyPacket,
},
pages::{GearPage, PageU32Size, WasmPage},
percent::Percent,
reservation::GasReserver,
};
use gear_core_backend::{
Expand Down Expand Up @@ -75,6 +76,8 @@ pub struct ProcessorContext {
pub message_context: MessageContext,
/// Block info.
pub block_info: BlockInfo,
/// Performance multiplier.
pub performance_multiplier: Percent,
/// Max allowed wasm memory pages.
pub max_pages: WasmPage,
/// Allocations config.
Expand Down Expand Up @@ -135,6 +138,7 @@ impl ProcessorContext {
ContextSettings::new(0, 0, 0, 0, 0, 0),
),
block_info: Default::default(),
performance_multiplier: Percent::new(100),
max_pages: 512.into(),
page_costs: Default::default(),
existential_deposit: 0,
Expand Down Expand Up @@ -725,6 +729,10 @@ impl Externalities for Ext {
Ok(self.context.block_info.timestamp)
}

fn performance_multiplier(&self) -> Result<Percent, Self::UnrecoverableError> {
Ok(self.context.performance_multiplier)
}

fn send_init(&mut self) -> Result<u32, Self::FallibleError> {
let handle = self.context.message_context.send_init()?;
Ok(handle)
Expand Down
2 changes: 2 additions & 0 deletions core-processor/src/processing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ where

let BlockConfig {
block_info,
performance_multiplier,
max_pages,
page_costs,
existential_deposit,
Expand All @@ -75,6 +76,7 @@ where

let execution_settings = ExecutionSettings {
block_info,
performance_multiplier,
existential_deposit,
max_pages,
page_costs,
Expand Down
5 changes: 3 additions & 2 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ wasmparser.workspace = true
hex = { workspace = true, features = ["alloc"] }
hashbrown.workspace = true
static_assertions.workspace = true
paste = { workspace = true }
paste.workspace = true
enum-iterator.workspace = true
byteorder.workspace = true
num-traits.workspace = true

# Optional dependencies
serde = { workspace = true, features = [ "derive" ], optional = true }
serde = { workspace = true, features = ["derive"], optional = true }

[dev-dependencies]
wabt.workspace = true
Expand Down
4 changes: 4 additions & 0 deletions core/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use crate::{
memory::Memory,
message::{HandlePacket, InitPacket, MessageContext, Payload, ReplyPacket},
pages::WasmPage,
percent::Percent,
};
use alloc::collections::BTreeSet;
use core::{fmt::Display, mem};
Expand Down Expand Up @@ -203,6 +204,9 @@ pub trait Externalities {
/// Get the current block timestamp.
fn block_timestamp(&self) -> Result<u64, Self::UnrecoverableError>;

/// Get current performance multiplier.
fn performance_multiplier(&self) -> Result<Percent, Self::UnrecoverableError>;

/// Initialize a new incomplete message for another program and return its handle.
fn send_init(&mut self) -> Result<u32, Self::FallibleError>;

Expand Down
1 change: 1 addition & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub mod ids;
pub mod memory;
pub mod message;
pub mod pages;
pub mod percent;
pub mod program;
pub mod reservation;

Expand Down
52 changes: 52 additions & 0 deletions core/src/percent.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// This file is part of Gear.

// Copyright (C) 2021-2023 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
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! Basic struct for working with integer percentages.
use core::cmp::Ord;
use num_traits::{cast::NumCast, Num};
use parity_scale_codec::{Decode, Encode};
use scale_info::TypeInfo;

/// Basic struct for working with integer percentages.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Encode, Decode, TypeInfo)]
pub struct Percent(u32);

impl Percent {
/// Creates a new `Percent` from a `u32` value. The value can be
/// greater than 100.
pub fn new(value: u32) -> Self {
Self(value)
}

/// Returns the inner `u16` value.
pub fn value(self) -> u32 {
self.0
}

/// Applies the percentage to the given value.
pub fn apply_to<T: Num + Ord + Copy + NumCast>(&self, value: T) -> T {
(value * NumCast::from(self.0).unwrap()) / NumCast::from(100).unwrap()
}
}

impl From<u32> for Percent {
fn from(value: u32) -> Self {
Self::new(value)
}
}
11 changes: 11 additions & 0 deletions gsdk/src/metadata/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,17 @@ pub mod runtime_types {
)]
pub struct WasmPage(pub ::core::primitive::u32);
}
pub mod percent {
use super::runtime_types;
#[derive(
::subxt::ext::codec::CompactAs,
Debug,
crate::gp::Decode,
crate::gp::DecodeAsType,
crate::gp::Encode,
)]
pub struct Percent(pub ::core::primitive::u32);
}
pub mod reservation {
use super::runtime_types;
#[derive(Debug, crate::gp::Decode, crate::gp::DecodeAsType, crate::gp::Encode)]
Expand Down
2 changes: 2 additions & 0 deletions gtest/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use gear_core::{
StoredMessage,
},
pages::{GearPage, PageU32Size, WasmPage},
percent::Percent,
program::Program as CoreProgram,
reservation::{GasReservationMap, GasReserver},
};
Expand Down Expand Up @@ -817,6 +818,7 @@ impl ExtManager {
.expect("Unable to find gas limit for message");
let block_config = BlockConfig {
block_info: self.block_info,
performance_multiplier: Percent::new(100),
max_pages: TESTS_MAX_PAGES_NUMBER.into(),
page_costs: PageCosts::new_for_tests(),
existential_deposit: EXISTENTIAL_DEPOSIT,
Expand Down
2 changes: 2 additions & 0 deletions pallets/gear-debug/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ parameter_types! {
pub const MinimumPeriod: u64 = 500;
pub const OutgoingLimit: u32 = 1024;
pub const BlockGasLimit: u64 = 100_000_000_000;
pub const PerformanceMultiplier: u32 = 100;
}

impl pallet_timestamp::Config for Test {
Expand Down Expand Up @@ -150,6 +151,7 @@ impl pallet_gear::Config for Test {
type Randomness = TestRandomness<Self>;
type WeightInfo = ();
type OutgoingLimit = OutgoingLimit;
type PerformanceMultiplier = PerformanceMultiplier;
type DebugInfo = super::Pallet<Test>;
type Schedule = ();
type CodeStorage = GearProgram;
Expand Down
2 changes: 2 additions & 0 deletions pallets/gear-scheduler/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ impl pallet_gear_program::Config for Test {
parameter_types! {
pub const BlockGasLimit: u64 = 100_000_000_000;
pub const OutgoingLimit: u32 = 1024;
pub const PerformanceMultiplier: u32 = 100;
pub GearSchedule: pallet_gear::Schedule<Test> = <pallet_gear::Schedule<Test>>::default();
pub RentFreePeriod: BlockNumber = 1_000;
pub RentCostPerBlock: Balance = 11;
Expand All @@ -142,6 +143,7 @@ impl pallet_gear::Config for Test {
type WeightInfo = ();
type Schedule = GearSchedule;
type OutgoingLimit = OutgoingLimit;
type PerformanceMultiplier = PerformanceMultiplier;
type DebugInfo = ();
type CodeStorage = GearProgram;
type ProgramStorage = GearProgram;
Expand Down
2 changes: 2 additions & 0 deletions pallets/gear/src/benchmarking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ use gear_core::{
memory::{AllocationsContext, Memory, PageBuf},
message::{ContextSettings, DispatchKind, IncomingDispatch, MessageContext},
pages::{GearPage, PageU32Size, WasmPage, GEAR_PAGE_SIZE, WASM_PAGE_SIZE},
percent::Percent,
reservation::GasReserver,
};
use gear_core_backend::{
Expand Down Expand Up @@ -187,6 +188,7 @@ fn default_processor_context<T: Config>() -> ProcessorContext {
ContextSettings::new(0, 0, 0, 0, 0, 0),
),
block_info: Default::default(),
performance_multiplier: Percent::new(100),
max_pages: TESTS_MAX_PAGES_NUMBER.into(),
page_costs: PageCosts::new_for_tests(),
existential_deposit: 0,
Expand Down
1 change: 1 addition & 0 deletions pallets/gear/src/benchmarking/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ where

BlockConfig {
block_info,
performance_multiplier: T::PerformanceMultiplier::get(),
max_pages: T::Schedule::get().limits.memory_pages.into(),
page_costs: T::Schedule::get().memory_weights.into(),
existential_deposit,
Expand Down
6 changes: 6 additions & 0 deletions pallets/gear/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ use gear_core::{
memory::PageBuf,
message::*,
pages::{GearPage, WasmPage},
percent::Percent,
};
use manager::{CodeInfo, QueuePostProcessingData};
use primitive_types::H256;
Expand Down Expand Up @@ -167,6 +168,10 @@ pub mod pallet {
#[pallet::constant]
type OutgoingLimit: Get<u32>;

/// Performance multiplier.
#[pallet::constant]
type PerformanceMultiplier: Get<Percent>;

type DebugInfo: DebugInfo;

/// Implementation of a storage for program binary codes.
Expand Down Expand Up @@ -1028,6 +1033,7 @@ pub mod pallet {

BlockConfig {
block_info,
performance_multiplier: T::PerformanceMultiplier::get(),
max_pages: schedule.limits.memory_pages.into(),
page_costs: schedule.memory_weights.clone().into(),
existential_deposit,
Expand Down
2 changes: 2 additions & 0 deletions pallets/gear/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ parameter_types! {
pub RentCostPerBlock: Balance = 11;
pub ResumeMinimalPeriod: BlockNumber = 100;
pub ResumeSessionDuration: BlockNumber = 1_000;
pub const PerformanceMultiplier: u32 = 100;
}

thread_local! {
Expand Down Expand Up @@ -217,6 +218,7 @@ impl pallet_gear::Config for Test {
type WeightInfo = pallet_gear::weights::SubstrateWeight<Self>;
type Schedule = DynamicSchedule;
type OutgoingLimit = OutgoingLimit;
type PerformanceMultiplier = PerformanceMultiplier;
type DebugInfo = ();
type CodeStorage = GearProgram;
type ProgramStorage = GearProgram;
Expand Down
2 changes: 2 additions & 0 deletions pallets/payment/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ impl pallet_transaction_payment::Config for Test {
parameter_types! {
pub const BlockGasLimit: u64 = 500_000;
pub const OutgoingLimit: u32 = 1024;
pub const PerformanceMultiplier: u32 = 100;
pub GearSchedule: pallet_gear::Schedule<Test> = <pallet_gear::Schedule<Test>>::default();
pub RentFreePeriod: BlockNumber = 1_000;
pub RentCostPerBlock: Balance = 11;
Expand All @@ -188,6 +189,7 @@ impl pallet_gear::Config for Test {
type WeightInfo = ();
type Schedule = GearSchedule;
type OutgoingLimit = OutgoingLimit;
type PerformanceMultiplier = PerformanceMultiplier;
type DebugInfo = ();
type CodeStorage = GearProgram;
type ProgramStorage = GearProgram;
Expand Down
3 changes: 3 additions & 0 deletions runtime/gear/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,8 @@ parameter_types! {

pub const OutgoingLimit: u32 = 1024;
pub const MailboxThreshold: u64 = 3000;

pub const PerformanceMultiplier: u32 = 100;
}

parameter_types! {
Expand All @@ -470,6 +472,7 @@ impl pallet_gear::Config for Runtime {
type WeightInfo = weights::pallet_gear::SubstrateWeight<Runtime>;
type Schedule = Schedule;
type OutgoingLimit = OutgoingLimit;
type PerformanceMultiplier = PerformanceMultiplier;
type DebugInfo = DebugInfo;
type CodeStorage = GearProgram;
type ProgramStorage = GearProgram;
Expand Down
Loading

0 comments on commit 4c43268

Please sign in to comment.