From 859e8204fd863c1ab29d0ac5df2b9c10de24fe8a Mon Sep 17 00:00:00 2001
From: bgallois <benjamin@gallois.cc>
Date: Thu, 2 Nov 2023 12:24:22 +0100
Subject: [PATCH] use our weights

---
 pallets/distance/src/lib.rs                   |  20 +--
 pallets/distance/src/mock.rs                  |   3 +
 pallets/distance/src/weights.rs               | 104 +++++++++++++-
 runtime/common/src/pallets_config.rs          |   1 +
 runtime/common/src/weights.rs                 |   1 +
 runtime/common/src/weights/pallet_distance.rs | 132 ++++++++++++++++++
 runtime/g1/src/lib.rs                         |   1 +
 runtime/gtest/src/lib.rs                      |   1 +
 8 files changed, 246 insertions(+), 17 deletions(-)
 create mode 100644 runtime/common/src/weights/pallet_distance.rs

diff --git a/pallets/distance/src/lib.rs b/pallets/distance/src/lib.rs
index 5a9cbda49..3326e24b4 100644
--- a/pallets/distance/src/lib.rs
+++ b/pallets/distance/src/lib.rs
@@ -45,7 +45,7 @@ pub const MAX_EVALUATIONS_PER_SESSION: u32 = 600;
 /// Maximum number of evaluators in a session
 pub const MAX_EVALUATORS_PER_SESSION: u32 = 100;
 
-#[frame_support::pallet(dev_mode)] // dev mode while waiting for benchmarks
+#[frame_support::pallet()]
 pub mod pallet {
     use super::*;
     use frame_support::{pallet_prelude::*, traits::ReservableCurrency};
@@ -56,7 +56,6 @@ pub mod pallet {
     const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);
 
     #[pallet::pallet]
-    // #[pallet::generate_store(pub(super) trait Store)] // deprecated
     #[pallet::storage_version(STORAGE_VERSION)]
     #[pallet::without_storage_info]
     pub struct Pallet<T>(PhantomData<T>);
@@ -78,7 +77,7 @@ pub mod pallet {
         /// Number of session to keep a positive evaluation result
         type ResultExpiration: Get<u32>;
         // /// Type representing the weight of this pallet
-        // type WeightInfo: WeightInfo;
+        type WeightInfo: WeightInfo;
     }
 
     // STORAGE //
@@ -182,8 +181,7 @@ pub mod pallet {
         /// dummy `on_initialize` to return the weight used in `on_finalize`.
         fn on_initialize(_n: BlockNumberFor<T>) -> Weight {
             // weight of `on_finalize`
-            //T::WeightInfo::on_finalize()// TODO uncomment when benchmarking
-            Weight::zero()
+            <T as pallet::Config>::WeightInfo::on_finalize()
         }
 
         /// # <weight>
@@ -201,8 +199,7 @@ pub mod pallet {
     impl<T: Config> Pallet<T> {
         /// Request an identity to be evaluated
         #[pallet::call_index(0)]
-        #[pallet::weight(0)]
-        // #[pallet::weight(T::WeightInfo::request_distance_evaluation())]
+        #[pallet::weight(<T as pallet::Config>::WeightInfo::request_distance_evaluation())]
         pub fn request_distance_evaluation(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
             let who = ensure_signed(origin)?;
 
@@ -220,8 +217,7 @@ pub mod pallet {
 
         /// (Inherent) Push an evaluation result to the pool
         #[pallet::call_index(1)]
-        #[pallet::weight(0)]
-        // #[pallet::weight(T::WeightInfo::update_evaluation())]
+        #[pallet::weight(<T as pallet::Config>::WeightInfo::update_evaluation(MAX_EVALUATIONS_PER_SESSION))]
         pub fn update_evaluation(
             origin: OriginFor<T>,
             computation_result: ComputationResult,
@@ -241,8 +237,7 @@ pub mod pallet {
 
         /// Push an evaluation result to the pool
         #[pallet::call_index(2)]
-        #[pallet::weight(0)]
-        // #[pallet::weight(T::WeightInfo::force_update_evaluation())]
+        #[pallet::weight(<T as pallet::Config>::WeightInfo::force_update_evaluation(MAX_EVALUATIONS_PER_SESSION))]
         pub fn force_update_evaluation(
             origin: OriginFor<T>,
             evaluator: <T as frame_system::Config>::AccountId,
@@ -261,8 +256,7 @@ pub mod pallet {
         ///   when the evaluation completes.
         /// * `status.1` is the status of the evaluation.
         #[pallet::call_index(3)]
-        #[pallet::weight(0)]
-        // #[pallet::weight(T::WeightInfo::force_set_distance_status())]
+        #[pallet::weight(<T as pallet::Config>::WeightInfo::force_set_distance_status())]
         pub fn force_set_distance_status(
             origin: OriginFor<T>,
             identity: <T as pallet_identity::Config>::IdtyIndex,
diff --git a/pallets/distance/src/mock.rs b/pallets/distance/src/mock.rs
index 33387c125..0222f8bf5 100644
--- a/pallets/distance/src/mock.rs
+++ b/pallets/distance/src/mock.rs
@@ -245,6 +245,8 @@ impl pallet_identity::Config for Test {
     type RevocationSignature = TestSignature;
     type RuntimeEvent = RuntimeEvent;
     type WeightInfo = ();
+    #[cfg(feature = "runtime-benchmarks")]
+    type BenchmarkSetupHandler = ();
 }
 
 parameter_types! {
@@ -255,6 +257,7 @@ impl pallet_distance::Config for Test {
     type EvaluationPrice = frame_support::traits::ConstU64<1000>;
     type MinAccessibleReferees = MinAccessibleReferees;
     type ResultExpiration = frame_support::traits::ConstU32<720>;
+    type WeightInfo = ();
 }
 
 pub const NAMES: [&str; 6] = ["Alice", "Bob", "Charlie", "Dave", "Eve", "Ferdie"];
diff --git a/pallets/distance/src/weights.rs b/pallets/distance/src/weights.rs
index 9177a415a..b0808361a 100644
--- a/pallets/distance/src/weights.rs
+++ b/pallets/distance/src/weights.rs
@@ -16,12 +16,108 @@
 
 #![allow(clippy::unnecessary_cast)]
 
-use frame_support::weights::Weight;
+use frame_support::weights::{constants::RocksDbWeight, Weight};
 
-/// Weight functions needed for pallet_universal_dividend.
 pub trait WeightInfo {
     fn request_distance_evaluation() -> Weight;
-    fn update_evaluation() -> Weight;
-    fn force_update_evaluation() -> Weight;
+    fn update_evaluation(i: u32) -> Weight;
+    fn force_update_evaluation(i: u32) -> Weight;
     fn force_set_distance_status() -> Weight;
+    fn on_finalize() -> Weight;
+}
+
+// Insecure weights implementation, use it for tests only!
+impl WeightInfo for () {
+    /// Storage: Identity IdentityIndexOf (r:1 w:0)
+    /// Proof Skipped: Identity IdentityIndexOf (max_values: None, max_size: None, mode: Measured)
+    /// Storage: Distance IdentityDistanceStatus (r:1 w:1)
+    /// Proof Skipped: Distance IdentityDistanceStatus (max_values: None, max_size: None, mode: Measured)
+    /// Storage: Session CurrentIndex (r:1 w:0)
+    /// Proof Skipped: Session CurrentIndex (max_values: Some(1), max_size: None, mode: Measured)
+    /// Storage: Distance EvaluationPool2 (r:1 w:1)
+    /// Proof Skipped: Distance EvaluationPool2 (max_values: Some(1), max_size: None, mode: Measured)
+    /// Storage: System Account (r:1 w:1)
+    /// Proof: System Account (max_values: None, max_size: Some(121), added: 2596, mode: MaxEncodedLen)
+    /// Storage: Distance DistanceStatusExpireOn (r:1 w:1)
+    /// Proof Skipped: Distance DistanceStatusExpireOn (max_values: None, max_size: None, mode: Measured)
+    fn request_distance_evaluation() -> Weight {
+        // Proof Size summary in bytes:
+        //  Measured:  `935`
+        //  Estimated: `4400`
+        // Minimum execution time: 28_469_000 picoseconds.
+        Weight::from_parts(30_905_000, 0)
+            .saturating_add(Weight::from_parts(0, 4400))
+            .saturating_add(RocksDbWeight::get().reads(6))
+            .saturating_add(RocksDbWeight::get().writes(4))
+    }
+    /// Storage: Distance DidUpdate (r:1 w:1)
+    /// Proof Skipped: Distance DidUpdate (max_values: Some(1), max_size: None, mode: Measured)
+    /// Storage: Authorship Author (r:1 w:1)
+    /// Proof: Authorship Author (max_values: Some(1), max_size: Some(32), added: 527, mode: MaxEncodedLen)
+    /// Storage: System Digest (r:1 w:0)
+    /// Proof Skipped: System Digest (max_values: Some(1), max_size: None, mode: Measured)
+    /// Storage: Session CurrentIndex (r:1 w:0)
+    /// Proof Skipped: Session CurrentIndex (max_values: Some(1), max_size: None, mode: Measured)
+    /// Storage: Distance EvaluationPool0 (r:1 w:1)
+    /// Proof Skipped: Distance EvaluationPool0 (max_values: Some(1), max_size: None, mode: Measured)
+    /// The range of component `i` is `[1, 600]`.
+    fn update_evaluation(i: u32) -> Weight {
+        // Proof Size summary in bytes:
+        //  Measured:  `744 + i * (10 ±0)`
+        //  Estimated: `2228 + i * (10 ±0)`
+        // Minimum execution time: 13_870_000 picoseconds.
+        Weight::from_parts(17_116_748, 0)
+            .saturating_add(Weight::from_parts(0, 2228))
+            // Standard Error: 684
+            .saturating_add(Weight::from_parts(128_989, 0).saturating_mul(i.into()))
+            .saturating_add(RocksDbWeight::get().reads(5))
+            .saturating_add(RocksDbWeight::get().writes(3))
+            .saturating_add(Weight::from_parts(0, 10).saturating_mul(i.into()))
+    }
+    /// Storage: Session CurrentIndex (r:1 w:0)
+    /// Proof Skipped: Session CurrentIndex (max_values: Some(1), max_size: None, mode: Measured)
+    /// Storage: Distance EvaluationPool0 (r:1 w:1)
+    /// Proof Skipped: Distance EvaluationPool0 (max_values: Some(1), max_size: None, mode: Measured)
+    /// The range of component `i` is `[1, 600]`.
+    fn force_update_evaluation(i: u32) -> Weight {
+        // Proof Size summary in bytes:
+        //  Measured:  `612 + i * (10 ±0)`
+        //  Estimated: `2096 + i * (10 ±0)`
+        // Minimum execution time: 8_392_000 picoseconds.
+        Weight::from_parts(10_825_908, 0)
+            .saturating_add(Weight::from_parts(0, 2096))
+            // Standard Error: 326
+            .saturating_add(Weight::from_parts(123_200, 0).saturating_mul(i.into()))
+            .saturating_add(RocksDbWeight::get().reads(2))
+            .saturating_add(RocksDbWeight::get().writes(1))
+            .saturating_add(Weight::from_parts(0, 10).saturating_mul(i.into()))
+    }
+    /// Storage: Session CurrentIndex (r:1 w:0)
+    /// Proof Skipped: Session CurrentIndex (max_values: Some(1), max_size: None, mode: Measured)
+    /// Storage: Distance DistanceStatusExpireOn (r:1 w:1)
+    /// Proof Skipped: Distance DistanceStatusExpireOn (max_values: None, max_size: None, mode: Measured)
+    /// Storage: Distance IdentityDistanceStatus (r:0 w:1)
+    /// Proof Skipped: Distance IdentityDistanceStatus (max_values: None, max_size: None, mode: Measured)
+    fn force_set_distance_status() -> Weight {
+        // Proof Size summary in bytes:
+        //  Measured:  `586`
+        //  Estimated: `4051`
+        // Minimum execution time: 8_099_000 picoseconds.
+        Weight::from_parts(8_786_000, 0)
+            .saturating_add(Weight::from_parts(0, 4051))
+            .saturating_add(RocksDbWeight::get().reads(2))
+            .saturating_add(RocksDbWeight::get().writes(2))
+    }
+    /// Storage: Distance DidUpdate (r:1 w:1)
+    /// Proof Skipped: Distance DidUpdate (max_values: Some(1), max_size: None, mode: Measured)
+    fn on_finalize() -> Weight {
+        // Proof Size summary in bytes:
+        //  Measured:  `170`
+        //  Estimated: `1655`
+        // Minimum execution time: 3_904_000 picoseconds.
+        Weight::from_parts(4_132_000, 0)
+            .saturating_add(Weight::from_parts(0, 1655))
+            .saturating_add(RocksDbWeight::get().reads(1))
+            .saturating_add(RocksDbWeight::get().writes(1))
+    }
 }
diff --git a/runtime/common/src/pallets_config.rs b/runtime/common/src/pallets_config.rs
index c701a56c9..561ea867f 100644
--- a/runtime/common/src/pallets_config.rs
+++ b/runtime/common/src/pallets_config.rs
@@ -497,6 +497,7 @@ parameter_types! {
             type EvaluationPrice = frame_support::traits::ConstU64<1000>;
             type MinAccessibleReferees = MinAccessibleReferees;
             type ResultExpiration = frame_support::traits::ConstU32<720>;
+            type WeightInfo = common_runtime::weights::pallet_distance::WeightInfo<Runtime>;
         }
 
         // SMITHS SUB-WOT //
diff --git a/runtime/common/src/weights.rs b/runtime/common/src/weights.rs
index 952423d90..c6998f583 100644
--- a/runtime/common/src/weights.rs
+++ b/runtime/common/src/weights.rs
@@ -25,6 +25,7 @@ pub mod frame_system;
 pub mod pallet_babe;
 pub mod pallet_balances;
 pub mod pallet_collective;
+pub mod pallet_distance;
 pub mod pallet_grandpa;
 pub mod pallet_im_online;
 pub mod pallet_multisig;
diff --git a/runtime/common/src/weights/pallet_distance.rs b/runtime/common/src/weights/pallet_distance.rs
new file mode 100644
index 000000000..0dbf1514f
--- /dev/null
+++ b/runtime/common/src/weights/pallet_distance.rs
@@ -0,0 +1,132 @@
+
+//! Autogenerated weights for `pallet_distance`
+//!
+//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
+//! DATE: 2023-11-02, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]`
+//! WORST CASE MAP SIZE: `1000000`
+//! HOSTNAME: `bgallois-ms7d43`, CPU: `12th Gen Intel(R) Core(TM) i3-12100F`
+//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("gdev-benchmark"), DB CACHE: 1024
+
+// Executed Command:
+// ./target/release/duniter
+// benchmark
+// pallet
+// --chain
+// gdev-benchmark
+// --execution=wasm
+// --wasm-execution=compiled
+// --pallet
+// pallet-distance
+// --extrinsic
+// *
+// --steps
+// 50
+// --repeat
+// 20
+// --output
+// runtime/common/src/weights/
+
+#![cfg_attr(rustfmt, rustfmt_skip)]
+#![allow(unused_parens)]
+#![allow(unused_imports)]
+#![allow(missing_docs)]
+
+use frame_support::{traits::Get, weights::Weight};
+use core::marker::PhantomData;
+
+/// Weight functions for `pallet_distance`.
+pub struct WeightInfo<T>(PhantomData<T>);
+impl<T: frame_system::Config> pallet_distance::WeightInfo for WeightInfo<T> {
+	/// Storage: Identity IdentityIndexOf (r:1 w:0)
+	/// Proof Skipped: Identity IdentityIndexOf (max_values: None, max_size: None, mode: Measured)
+	/// Storage: Distance IdentityDistanceStatus (r:1 w:1)
+	/// Proof Skipped: Distance IdentityDistanceStatus (max_values: None, max_size: None, mode: Measured)
+	/// Storage: Session CurrentIndex (r:1 w:0)
+	/// Proof Skipped: Session CurrentIndex (max_values: Some(1), max_size: None, mode: Measured)
+	/// Storage: Distance EvaluationPool2 (r:1 w:1)
+	/// Proof Skipped: Distance EvaluationPool2 (max_values: Some(1), max_size: None, mode: Measured)
+	/// Storage: System Account (r:1 w:1)
+	/// Proof: System Account (max_values: None, max_size: Some(121), added: 2596, mode: MaxEncodedLen)
+	/// Storage: Distance DistanceStatusExpireOn (r:1 w:1)
+	/// Proof Skipped: Distance DistanceStatusExpireOn (max_values: None, max_size: None, mode: Measured)
+	fn request_distance_evaluation() -> Weight {
+		// Proof Size summary in bytes:
+		//  Measured:  `935`
+		//  Estimated: `4400`
+		// Minimum execution time: 28_469_000 picoseconds.
+		Weight::from_parts(30_905_000, 0)
+			.saturating_add(Weight::from_parts(0, 4400))
+			.saturating_add(T::DbWeight::get().reads(6))
+			.saturating_add(T::DbWeight::get().writes(4))
+	}
+	/// Storage: Distance DidUpdate (r:1 w:1)
+	/// Proof Skipped: Distance DidUpdate (max_values: Some(1), max_size: None, mode: Measured)
+	/// Storage: Authorship Author (r:1 w:1)
+	/// Proof: Authorship Author (max_values: Some(1), max_size: Some(32), added: 527, mode: MaxEncodedLen)
+	/// Storage: System Digest (r:1 w:0)
+	/// Proof Skipped: System Digest (max_values: Some(1), max_size: None, mode: Measured)
+	/// Storage: Session CurrentIndex (r:1 w:0)
+	/// Proof Skipped: Session CurrentIndex (max_values: Some(1), max_size: None, mode: Measured)
+	/// Storage: Distance EvaluationPool0 (r:1 w:1)
+	/// Proof Skipped: Distance EvaluationPool0 (max_values: Some(1), max_size: None, mode: Measured)
+	/// The range of component `i` is `[1, 600]`.
+	fn update_evaluation(i: u32, ) -> Weight {
+		// Proof Size summary in bytes:
+		//  Measured:  `744 + i * (10 ±0)`
+		//  Estimated: `2228 + i * (10 ±0)`
+		// Minimum execution time: 13_870_000 picoseconds.
+		Weight::from_parts(17_116_748, 0)
+			.saturating_add(Weight::from_parts(0, 2228))
+			// Standard Error: 684
+			.saturating_add(Weight::from_parts(128_989, 0).saturating_mul(i.into()))
+			.saturating_add(T::DbWeight::get().reads(5))
+			.saturating_add(T::DbWeight::get().writes(3))
+			.saturating_add(Weight::from_parts(0, 10).saturating_mul(i.into()))
+	}
+	/// Storage: Session CurrentIndex (r:1 w:0)
+	/// Proof Skipped: Session CurrentIndex (max_values: Some(1), max_size: None, mode: Measured)
+	/// Storage: Distance EvaluationPool0 (r:1 w:1)
+	/// Proof Skipped: Distance EvaluationPool0 (max_values: Some(1), max_size: None, mode: Measured)
+	/// The range of component `i` is `[1, 600]`.
+	fn force_update_evaluation(i: u32, ) -> Weight {
+		// Proof Size summary in bytes:
+		//  Measured:  `612 + i * (10 ±0)`
+		//  Estimated: `2096 + i * (10 ±0)`
+		// Minimum execution time: 8_392_000 picoseconds.
+		Weight::from_parts(10_825_908, 0)
+			.saturating_add(Weight::from_parts(0, 2096))
+			// Standard Error: 326
+			.saturating_add(Weight::from_parts(123_200, 0).saturating_mul(i.into()))
+			.saturating_add(T::DbWeight::get().reads(2))
+			.saturating_add(T::DbWeight::get().writes(1))
+			.saturating_add(Weight::from_parts(0, 10).saturating_mul(i.into()))
+	}
+	/// Storage: Session CurrentIndex (r:1 w:0)
+	/// Proof Skipped: Session CurrentIndex (max_values: Some(1), max_size: None, mode: Measured)
+	/// Storage: Distance DistanceStatusExpireOn (r:1 w:1)
+	/// Proof Skipped: Distance DistanceStatusExpireOn (max_values: None, max_size: None, mode: Measured)
+	/// Storage: Distance IdentityDistanceStatus (r:0 w:1)
+	/// Proof Skipped: Distance IdentityDistanceStatus (max_values: None, max_size: None, mode: Measured)
+	fn force_set_distance_status() -> Weight {
+		// Proof Size summary in bytes:
+		//  Measured:  `586`
+		//  Estimated: `4051`
+		// Minimum execution time: 8_099_000 picoseconds.
+		Weight::from_parts(8_786_000, 0)
+			.saturating_add(Weight::from_parts(0, 4051))
+			.saturating_add(T::DbWeight::get().reads(2))
+			.saturating_add(T::DbWeight::get().writes(2))
+	}
+	/// Storage: Distance DidUpdate (r:1 w:1)
+	/// Proof Skipped: Distance DidUpdate (max_values: Some(1), max_size: None, mode: Measured)
+	fn on_finalize() -> Weight {
+		// Proof Size summary in bytes:
+		//  Measured:  `170`
+		//  Estimated: `1655`
+		// Minimum execution time: 3_904_000 picoseconds.
+		Weight::from_parts(4_132_000, 0)
+			.saturating_add(Weight::from_parts(0, 1655))
+			.saturating_add(T::DbWeight::get().reads(1))
+			.saturating_add(T::DbWeight::get().writes(1))
+	}
+}
diff --git a/runtime/g1/src/lib.rs b/runtime/g1/src/lib.rs
index 11d4afe1e..9b372bc19 100644
--- a/runtime/g1/src/lib.rs
+++ b/runtime/g1/src/lib.rs
@@ -134,6 +134,7 @@ mod benches {
     define_benchmarks!(
         [pallet_certification, Cert]
         [pallet_certification, SmithCert]
+        [pallet_distance, Distance]
         [pallet_oneshot_account, OneshotAccount]
         [pallet_universal_dividend, UniversalDividend]
         [pallet_provide_randomness, ProvideRandomness]
diff --git a/runtime/gtest/src/lib.rs b/runtime/gtest/src/lib.rs
index b04fd581e..bd48d7085 100644
--- a/runtime/gtest/src/lib.rs
+++ b/runtime/gtest/src/lib.rs
@@ -139,6 +139,7 @@ mod benches {
     define_benchmarks!(
         [pallet_certification, Cert]
         [pallet_certification, SmithCert]
+        [pallet_distance, Distance]
         [pallet_oneshot_account, OneshotAccount]
         [pallet_universal_dividend, UniversalDividend]
         [pallet_provide_randomness, ProvideRandomness]
-- 
GitLab