diff --git a/runtime/common/src/constants.rs b/runtime/common/src/constants.rs index 6b8d00591a83819589a4d34739bc5673dcf11472..b3c5e22fce5f2391198cc57be257111fd784b57e 100644 --- a/runtime/common/src/constants.rs +++ b/runtime/common/src/constants.rs @@ -38,15 +38,6 @@ const SECS_PER_YEAR: u64 = 31_557_600; // (365.25 * 24 * 60 * 60) pub const MONTHS: BlockNumber = (SECS_PER_YEAR / (12 * SECS_PER_BLOCK)) as BlockNumber; pub const YEARS: BlockNumber = (SECS_PER_YEAR / SECS_PER_BLOCK) as BlockNumber; -pub const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75); - -frame_support::parameter_types! { - pub const DbWeight: frame_support::weights::RuntimeDbWeight = frame_support::weights::RuntimeDbWeight { - read: 250 * frame_support::weights::constants::WEIGHT_PER_MICROS, // ~25 µs - write: 1_000 * frame_support::weights::constants::WEIGHT_PER_MICROS, // ~100 µs - }; -} - // 1 in 4 blocks (on average, not counting collisions) will be primary babe blocks. pub const PRIMARY_PROBABILITY: (u64, u64) = (1, 4); @@ -64,3 +55,44 @@ pub const DEPOSIT_PER_ITEM: Balance = 100; pub const fn deposit(items: u32, bytes: u32) -> Balance { items as Balance * DEPOSIT_PER_ITEM + (bytes as Balance * DEPOSIT_PER_BYTE) } + +// WEIGHTS COMSTANTS // + +// Execution cost of everything outside of the call itself: +// signature verification, pre_dispatch and post_dispatch +pub const EXTRINSIC_BASE_WEIGHTS: frame_support::weights::Weight = 1_000_000_000; + +// Maximal weight proportion of normal extrinsics per block +pub const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75); + +// DB weights +frame_support::parameter_types! { + pub const DbWeight: frame_support::weights::RuntimeDbWeight = frame_support::weights::RuntimeDbWeight { + read: 250 * frame_support::weights::constants::WEIGHT_PER_MICROS, // ~25 µs + write: 1_000 * frame_support::weights::constants::WEIGHT_PER_MICROS, // ~100 µs + }; +} + +// Block weights limits +pub fn block_weights( + expected_block_weight: frame_support::weights::Weight, + normal_ratio: sp_arithmetic::Perbill, +) -> frame_system::limits::BlockWeights { + let normal_weight = normal_ratio * expected_block_weight; + frame_system::limits::BlockWeights::builder() + .for_class(frame_support::weights::DispatchClass::Normal, |weights| { + weights.base_extrinsic = EXTRINSIC_BASE_WEIGHTS; + weights.max_total = normal_weight.into(); + }) + .for_class( + frame_support::weights::DispatchClass::Operational, + |weights| { + weights.base_extrinsic = EXTRINSIC_BASE_WEIGHTS; + weights.max_total = expected_block_weight.into(); + weights.reserved = (expected_block_weight - normal_weight).into(); + }, + ) + .avg_block_initialization(sp_arithmetic::Perbill::from_percent(10)) + .build() + .expect("Fatal error: invalid BlockWeights configuration") +} diff --git a/runtime/g1/src/parameters.rs b/runtime/g1/src/parameters.rs index 4275a5ef17fcdae9038002d361f9873d8dce0ace..9c4db0ceb75071ffe556ef77a1516216ef128972 100644 --- a/runtime/g1/src/parameters.rs +++ b/runtime/g1/src/parameters.rs @@ -26,8 +26,7 @@ use sp_runtime::transaction_validity::TransactionPriority; parameter_types! { pub const BlockHashCount: BlockNumber = 2400; /// We allow for 2 seconds of compute with a 6 second average block time. - pub BlockWeights: frame_system::limits::BlockWeights = frame_system::limits::BlockWeights - ::with_sensible_defaults(2 * WEIGHT_PER_SECOND, NORMAL_DISPATCH_RATIO); + pub BlockWeights: frame_system::limits::BlockWeights = block_weights(2 * WEIGHT_PER_SECOND, NORMAL_DISPATCH_RATIO); pub BlockLength: frame_system::limits::BlockLength = frame_system::limits::BlockLength ::max_with_normal_ratio(5 * 1024 * 1024, NORMAL_DISPATCH_RATIO); pub const SS58Prefix: u16 = 3682; diff --git a/runtime/gdev/src/parameters.rs b/runtime/gdev/src/parameters.rs index 024568027d5c619abd49a5a5c3d8dfc2972de310..274f9ce76ff72693864d4fdcede20921590be57c 100644 --- a/runtime/gdev/src/parameters.rs +++ b/runtime/gdev/src/parameters.rs @@ -26,8 +26,7 @@ use sp_runtime::transaction_validity::TransactionPriority; parameter_types! { pub const BlockHashCount: BlockNumber = 2400; /// We allow for 2 seconds of compute with a 6 second average block time. - pub BlockWeights: frame_system::limits::BlockWeights = frame_system::limits::BlockWeights - ::with_sensible_defaults(2 * WEIGHT_PER_SECOND, NORMAL_DISPATCH_RATIO); + pub BlockWeights: frame_system::limits::BlockWeights = block_weights(2 * WEIGHT_PER_SECOND, NORMAL_DISPATCH_RATIO); pub BlockLength: frame_system::limits::BlockLength = frame_system::limits::BlockLength ::max_with_normal_ratio(5 * 1024 * 1024, NORMAL_DISPATCH_RATIO); pub const SS58Prefix: u16 = 42; diff --git a/runtime/gtest/src/parameters.rs b/runtime/gtest/src/parameters.rs index df70c7bf699294dda5938e286629f3ffdf9865e2..fb0c3c801ce9b559e50d05320bda3971f9bdf978 100644 --- a/runtime/gtest/src/parameters.rs +++ b/runtime/gtest/src/parameters.rs @@ -26,8 +26,7 @@ use sp_runtime::transaction_validity::TransactionPriority; parameter_types! { pub const BlockHashCount: BlockNumber = 2400; /// We allow for 2 seconds of compute with a 6 second average block time. - pub BlockWeights: frame_system::limits::BlockWeights = frame_system::limits::BlockWeights - ::with_sensible_defaults(2 * WEIGHT_PER_SECOND, NORMAL_DISPATCH_RATIO); + pub BlockWeights: frame_system::limits::BlockWeights = block_weights(2 * WEIGHT_PER_SECOND, NORMAL_DISPATCH_RATIO); pub BlockLength: frame_system::limits::BlockLength = frame_system::limits::BlockLength ::max_with_normal_ratio(5 * 1024 * 1024, NORMAL_DISPATCH_RATIO); pub const SS58Prefix: u16 = 42;