From abf39880c8fc03738019a8a3308fdad68991dc63 Mon Sep 17 00:00:00 2001
From: librelois <c@elo.tf>
Date: Sun, 12 Jun 2022 19:56:30 +0200
Subject: [PATCH] =?UTF-8?q?fix(weights):=C2=A0use=20our=20own=20extrinsic?=
 =?UTF-8?q?=20base=20weights=20constant?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 runtime/common/src/constants.rs | 50 +++++++++++++++++++++++++++------
 runtime/g1/src/parameters.rs    |  3 +-
 runtime/gdev/src/parameters.rs  |  3 +-
 runtime/gtest/src/parameters.rs |  3 +-
 4 files changed, 44 insertions(+), 15 deletions(-)

diff --git a/runtime/common/src/constants.rs b/runtime/common/src/constants.rs
index 6b8d00591..b3c5e22fc 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 4275a5ef1..9c4db0ceb 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 024568027..274f9ce76 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 df70c7bf6..fb0c3c801 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;
-- 
GitLab