From 59ded47e49732c5182d209ade8a57fcb834f6e8f Mon Sep 17 00:00:00 2001
From: librelois <c@elo.tf>
Date: Fri, 13 May 2022 19:44:53 +0200
Subject: [PATCH] fix(weights): define safe manual weights for pallets babe &
 grandpa

---
 Cargo.lock                                    |  2 +-
 runtime/common/Cargo.toml                     |  4 +-
 runtime/common/src/pallets_config.rs          |  4 +-
 runtime/common/src/weights.rs                 |  3 +-
 runtime/common/src/weights/pallet_babe.rs     | 51 ++++++++++++
 runtime/common/src/weights/pallet_balances.rs | 81 -------------------
 runtime/common/src/weights/pallet_grandpa.rs  | 53 ++++++++++++
 7 files changed, 111 insertions(+), 87 deletions(-)
 create mode 100644 runtime/common/src/weights/pallet_babe.rs
 delete mode 100644 runtime/common/src/weights/pallet_balances.rs
 create mode 100644 runtime/common/src/weights/pallet_grandpa.rs

diff --git a/Cargo.lock b/Cargo.lock
index 97f877f89..2bdafcac4 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -832,10 +832,10 @@ dependencies = [
  "log",
  "pallet-authority-members",
  "pallet-babe",
- "pallet-balances",
  "pallet-certification",
  "pallet-duniter-account",
  "pallet-duniter-wot",
+ "pallet-grandpa",
  "pallet-identity",
  "pallet-membership",
  "pallet-provide-randomness",
diff --git a/runtime/common/Cargo.toml b/runtime/common/Cargo.toml
index 490178cf7..9d4a1b324 100644
--- a/runtime/common/Cargo.toml
+++ b/runtime/common/Cargo.toml
@@ -24,10 +24,10 @@ std = [
 	'log/std',
     'pallet-authority-members/std',
     'pallet-babe/std',
-    'pallet-balances/std',
     'pallet-certification/std',
     'pallet-duniter-account/std',
     'pallet-duniter-wot/std',
+    'pallet-grandpa/std',
     'pallet-identity/std',
     'pallet-membership/std',
     'pallet-provide-randomness/std',
@@ -79,7 +79,7 @@ default-features = false
 git = 'https://github.com/librelois/substrate.git'
 branch = 'duniter-monthly-2022-02'
 
-[dependencies.pallet-balances]
+[dependencies.pallet-grandpa]
 default-features = false
 git = 'https://github.com/librelois/substrate.git'
 branch = 'duniter-monthly-2022-02'
diff --git a/runtime/common/src/pallets_config.rs b/runtime/common/src/pallets_config.rs
index 9cabef4bc..9ae9bbbd2 100644
--- a/runtime/common/src/pallets_config.rs
+++ b/runtime/common/src/pallets_config.rs
@@ -133,7 +133,7 @@ macro_rules! pallets_config {
 			type HandleEquivocation =
 				pallet_babe::EquivocationHandler<Self::KeyOwnerIdentification, Offences, ReportLongevity>;
 
-			type WeightInfo = ();
+			type WeightInfo = common_runtime::weights::pallet_babe::WeightInfo<Runtime>;
 
 			type MaxAuthorities = MaxAuthorities;
 		}
@@ -252,7 +252,7 @@ macro_rules! pallets_config {
 
 			type HandleEquivocation = ();
 
-			type WeightInfo = ();
+			type WeightInfo = common_runtime::weights::pallet_grandpa::WeightInfo<Runtime>;
 
 			type MaxAuthorities = MaxAuthorities;
 		}
diff --git a/runtime/common/src/weights.rs b/runtime/common/src/weights.rs
index 300344c39..664b681d7 100644
--- a/runtime/common/src/weights.rs
+++ b/runtime/common/src/weights.rs
@@ -14,4 +14,5 @@
 // You should have received a copy of the GNU Affero General Public License
 // along with Substrate-Libre-Currency. If not, see <https://www.gnu.org/licenses/>.
 
-pub mod pallet_balances;
+pub mod pallet_babe;
+pub mod pallet_grandpa;
diff --git a/runtime/common/src/weights/pallet_babe.rs b/runtime/common/src/weights/pallet_babe.rs
new file mode 100644
index 000000000..ecea61a64
--- /dev/null
+++ b/runtime/common/src/weights/pallet_babe.rs
@@ -0,0 +1,51 @@
+// Copyright 2021 Axiom-Team
+//
+// This file is part of Substrate-Libre-Currency.
+//
+// Substrate-Libre-Currency is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, version 3 of the License.
+//
+// Substrate-Libre-Currency 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 Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with Substrate-Libre-Currency. If not, see <https://www.gnu.org/licenses/>.
+
+//! Manual weights for the GRANDPA Pallet in duniter runtimes
+//! This file was not auto-generated.
+
+use frame_support::{
+    traits::Get,
+    weights::{
+        constants::{WEIGHT_PER_MICROS, WEIGHT_PER_NANOS},
+        Weight,
+    },
+};
+use sp_std::marker::PhantomData;
+
+/// Weight functions for `pallet_babe`.
+pub struct WeightInfo<T>(PhantomData<T>);
+impl<T: frame_system::Config> pallet_babe::WeightInfo for WeightInfo<T> {
+    fn plan_config_change() -> Weight {
+        T::DbWeight::get().writes(1)
+    }
+
+    fn report_equivocation(validator_count: u32) -> Weight {
+        // we take the validator set count from the membership proof to
+        // calculate the weight but we set a floor of 100 validators.
+        let validator_count = validator_count.max(100) as u64;
+
+        // checking membership proof
+        (35 * WEIGHT_PER_MICROS)
+            .saturating_add((175 * WEIGHT_PER_NANOS).saturating_mul(validator_count))
+            .saturating_add(T::DbWeight::get().reads(5))
+            // check equivocation proof
+            .saturating_add(110 * WEIGHT_PER_MICROS)
+            // report offence
+            .saturating_add(110 * WEIGHT_PER_MICROS)
+            .saturating_add(T::DbWeight::get().writes(3))
+    }
+}
diff --git a/runtime/common/src/weights/pallet_balances.rs b/runtime/common/src/weights/pallet_balances.rs
deleted file mode 100644
index 00733f0ca..000000000
--- a/runtime/common/src/weights/pallet_balances.rs
+++ /dev/null
@@ -1,81 +0,0 @@
-// Copyright 2021 Axiom-Team
-//
-// This file is part of Substrate-Libre-Currency.
-//
-// Substrate-Libre-Currency is free software: you can redistribute it and/or modify
-// it under the terms of the GNU Affero General Public License as published by
-// the Free Software Foundation, version 3 of the License.
-//
-// Substrate-Libre-Currency 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 Affero General Public License for more details.
-//
-// You should have received a copy of the GNU Affero General Public License
-// along with Substrate-Libre-Currency. If not, see <https://www.gnu.org/licenses/>.
-
-//! Manually written file for temporary weights (polkadot x 10)
-
-// TODO regenerate thi file with benchmarks:
-//
-// Executed Command:
-// target/release/duniter
-// benchmark
-// --chain=dev
-// --steps=50
-// --repeat=20
-// --pallet=pallet_balances
-// --extrinsic=*
-// --execution=wasm
-// --wasm-execution=compiled
-// --heap-pages=4096
-// --header=./file_header.txt
-// --output=./runtime/gdev/src/weights/
-
-#![allow(unused_parens)]
-#![allow(unused_imports)]
-#![allow(clippy::unnecessary_cast)]
-
-use frame_support::{traits::Get, weights::Weight};
-use sp_std::marker::PhantomData;
-
-/// Weight functions for `pallet_balances`.
-pub struct WeightInfo<T>(PhantomData<T>);
-impl<T: frame_system::Config> pallet_balances::WeightInfo for WeightInfo<T> {
-    fn transfer() -> Weight {
-        (688_850_000 as Weight)
-            .saturating_add(T::DbWeight::get().reads(1 as Weight))
-            .saturating_add(T::DbWeight::get().writes(1 as Weight))
-    }
-    fn transfer_keep_alive() -> Weight {
-        (514_410_000 as Weight)
-            .saturating_add(T::DbWeight::get().reads(1 as Weight))
-            .saturating_add(T::DbWeight::get().writes(1 as Weight))
-    }
-    fn set_balance_creating() -> Weight {
-        (277_360_000 as Weight)
-            .saturating_add(T::DbWeight::get().reads(1 as Weight))
-            .saturating_add(T::DbWeight::get().writes(1 as Weight))
-    }
-    fn set_balance_killing() -> Weight {
-        (329_990_000 as Weight)
-            .saturating_add(T::DbWeight::get().reads(1 as Weight))
-            .saturating_add(T::DbWeight::get().writes(1 as Weight))
-    }
-    fn force_transfer() -> Weight {
-        (674_510_000 as Weight)
-            .saturating_add(T::DbWeight::get().reads(2 as Weight))
-            .saturating_add(T::DbWeight::get().writes(2 as Weight))
-    }
-    fn transfer_all() -> Weight {
-        (633_590_000 as Weight)
-            .saturating_add(T::DbWeight::get().reads(1 as Weight))
-            .saturating_add(T::DbWeight::get().writes(1 as Weight))
-    }
-    // Storage: System Account (r:1 w:1)
-    fn force_unreserve() -> Weight {
-        (277_660_000 as Weight)
-            .saturating_add(T::DbWeight::get().reads(1 as Weight))
-            .saturating_add(T::DbWeight::get().writes(1 as Weight))
-    }
-}
diff --git a/runtime/common/src/weights/pallet_grandpa.rs b/runtime/common/src/weights/pallet_grandpa.rs
new file mode 100644
index 000000000..06200adc1
--- /dev/null
+++ b/runtime/common/src/weights/pallet_grandpa.rs
@@ -0,0 +1,53 @@
+// Copyright 2021 Axiom-Team
+//
+// This file is part of Substrate-Libre-Currency.
+//
+// Substrate-Libre-Currency is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, version 3 of the License.
+//
+// Substrate-Libre-Currency 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 Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with Substrate-Libre-Currency. If not, see <https://www.gnu.org/licenses/>.
+
+//! Manual weights for the GRANDPA Pallet in duniter runtimes
+//! This file was not auto-generated.
+
+use frame_support::{
+    traits::Get,
+    weights::{
+        constants::{WEIGHT_PER_MICROS, WEIGHT_PER_NANOS},
+        Weight,
+    },
+};
+use sp_std::marker::PhantomData;
+
+/// Weight functions for `pallet_grandpa`.
+pub struct WeightInfo<T>(PhantomData<T>);
+impl<T: frame_system::Config> pallet_grandpa::WeightInfo for WeightInfo<T> {
+    fn report_equivocation(validator_count: u32) -> Weight {
+        // we take the validator set count from the membership proof to
+        // calculate the weight but we set a floor of 100 validators.
+        let validator_count = validator_count.max(100) as u64;
+
+        // checking membership proof
+        (35 * WEIGHT_PER_MICROS)
+            .saturating_add((175 * WEIGHT_PER_NANOS).saturating_mul(validator_count))
+            .saturating_add(T::DbWeight::get().reads(5))
+            // check equivocation proof
+            .saturating_add(95 * WEIGHT_PER_MICROS)
+            // report offence
+            .saturating_add(110 * WEIGHT_PER_MICROS)
+            .saturating_add(T::DbWeight::get().writes(3))
+            // fetching set id -> session index mappings
+            .saturating_add(T::DbWeight::get().reads(2))
+    }
+
+    fn note_stalled() -> Weight {
+        (3 * WEIGHT_PER_MICROS).saturating_add(T::DbWeight::get().writes(1))
+    }
+}
-- 
GitLab