From d3730345675dbb9bfd9650f6cc12b13bb951cbec Mon Sep 17 00:00:00 2001
From: bgallois <benjamin@gallois.cc>
Date: Wed, 5 Apr 2023 13:57:09 +0200
Subject: [PATCH] feat(pallet_certification): add weights info

---
 pallets/certification/src/lib.rs     | 15 ++++--
 pallets/certification/src/mock.rs    |  1 +
 pallets/certification/src/weights.rs | 77 ++++++++++++++++++++++++++++
 pallets/duniter-wot/src/mock.rs      |  2 +
 4 files changed, 90 insertions(+), 5 deletions(-)
 create mode 100644 pallets/certification/src/weights.rs

diff --git a/pallets/certification/src/lib.rs b/pallets/certification/src/lib.rs
index 12d6852a0..9b423da0d 100644
--- a/pallets/certification/src/lib.rs
+++ b/pallets/certification/src/lib.rs
@@ -16,10 +16,12 @@
 
 #![cfg_attr(not(feature = "std"), no_std)]
 
-mod benchmarking;
+#[cfg(feature = "runtime-benchmarks")]
+pub mod benchmarking;
 
 pub mod traits;
 mod types;
+pub mod weights;
 
 #[cfg(test)]
 mod mock;
@@ -29,6 +31,7 @@ mod tests;
 
 pub use pallet::*;
 pub use types::*;
+pub use weights::WeightInfo;
 
 use crate::traits::*;
 use codec::Codec;
@@ -85,6 +88,8 @@ pub mod pallet {
         /// Because this pallet emits events, it depends on the runtime's definition of an event.
         type RuntimeEvent: From<Event<Self, I>>
             + IsType<<Self as frame_system::Config>::RuntimeEvent>;
+        /// Type representing the weight of this pallet
+        type WeightInfo: WeightInfo;
         #[pallet::constant]
         /// Duration of validity of a certification
         type ValidityPeriod: Get<Self::BlockNumber>;
@@ -272,7 +277,7 @@ pub mod pallet {
     #[pallet::call]
     impl<T: Config<I>, I: 'static> Pallet<T, I> {
         /// add a certification without checks (only root)
-        #[pallet::weight(1_000_000_000)]
+        #[pallet::weight(T::WeightInfo::force_add_cert())]
         pub fn force_add_cert(
             origin: OriginFor<T>,
             issuer: T::IdtyIndex,
@@ -315,7 +320,7 @@ pub mod pallet {
         /// - `receiver`: the account receiving the certification from the origin
         ///
         /// The origin must be allow to certify.
-        #[pallet::weight(1_000_000_000)]
+        #[pallet::weight(T::WeightInfo::add_cert())]
         pub fn add_cert(
             origin: OriginFor<T>,
             issuer: T::IdtyIndex,
@@ -359,7 +364,7 @@ pub mod pallet {
         }
 
         /// remove a certification (only root)
-        #[pallet::weight(1_000_000_000)]
+        #[pallet::weight(T::WeightInfo::del_cert())]
         pub fn del_cert(
             origin: OriginFor<T>,
             issuer: T::IdtyIndex,
@@ -371,7 +376,7 @@ pub mod pallet {
         }
 
         /// remove all certifications received by an identity (only root)
-        #[pallet::weight(1_000_000_000)]
+        #[pallet::weight(T::WeightInfo::remove_all_certs_received_by(CertsByReceiver::<T, I>::get(idty_index).len() as u32))]
         pub fn remove_all_certs_received_by(
             origin: OriginFor<T>,
             idty_index: T::IdtyIndex,
diff --git a/pallets/certification/src/mock.rs b/pallets/certification/src/mock.rs
index 3096b3a06..f5cf93032 100644
--- a/pallets/certification/src/mock.rs
+++ b/pallets/certification/src/mock.rs
@@ -108,6 +108,7 @@ impl pallet_certification::Config for Test {
     type OnNewcert = ();
     type OnRemovedCert = ();
     type RuntimeEvent = RuntimeEvent;
+    type WeightInfo = ();
     type ValidityPeriod = ValidityPeriod;
 }
 
diff --git a/pallets/certification/src/weights.rs b/pallets/certification/src/weights.rs
new file mode 100644
index 000000000..54665716e
--- /dev/null
+++ b/pallets/certification/src/weights.rs
@@ -0,0 +1,77 @@
+// Copyright 2021-2022 Axiom-Team
+//
+// This file is part of Duniter-v2S.
+//
+// Duniter-v2S 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.
+//
+// Duniter-v2S 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 Duniter-v2S. If not, see <https://www.gnu.org/licenses/>.
+
+#![allow(clippy::unnecessary_cast)]
+
+use frame_support::weights::{constants::RocksDbWeight, Weight};
+
+/// Weight functions needed for pallet_universal_dividend.
+pub trait WeightInfo {
+    fn force_add_cert() -> Weight;
+    fn add_cert() -> Weight;
+    fn del_cert() -> Weight;
+    fn remove_all_certs_received_by(i: u32) -> Weight;
+}
+
+// Insecure weights implementation, use it for tests only!
+impl WeightInfo for () {
+    // Storage: Cert StorageIdtyCertMeta (r:2 w:2)
+    // Storage: Parameters ParametersStorage (r:1 w:0)
+    // Storage: Cert StorageCertsRemovableOn (r:1 w:1)
+    // Storage: Cert CertsByReceiver (r:1 w:1)
+    fn force_add_cert() -> Weight {
+        // Minimum execution time: 221_467 nanoseconds.
+        Weight::from_ref_time(227_833_000 as u64)
+            .saturating_add(RocksDbWeight::get().reads(5 as u64))
+            .saturating_add(RocksDbWeight::get().writes(4 as u64))
+    }
+    // Storage: Identity Identities (r:2 w:0)
+    // Storage: Cert StorageIdtyCertMeta (r:2 w:2)
+    // Storage: Parameters ParametersStorage (r:1 w:0)
+    // Storage: Cert StorageCertsRemovableOn (r:1 w:1)
+    // Storage: Cert CertsByReceiver (r:1 w:1)
+    fn add_cert() -> Weight {
+        // Minimum execution time: 259_247 nanoseconds.
+        Weight::from_ref_time(269_348_000 as u64)
+            .saturating_add(RocksDbWeight::get().reads(7 as u64))
+            .saturating_add(RocksDbWeight::get().writes(4 as u64))
+    }
+    // Storage: Cert CertsByReceiver (r:1 w:1)
+    // Storage: Cert StorageIdtyCertMeta (r:2 w:2)
+    // Storage: Parameters ParametersStorage (r:1 w:0)
+    // Storage: Membership Membership (r:1 w:0)
+    fn del_cert() -> Weight {
+        // Minimum execution time: 216_762 nanoseconds.
+        Weight::from_ref_time(222_570_000 as u64)
+            .saturating_add(RocksDbWeight::get().reads(5 as u64))
+            .saturating_add(RocksDbWeight::get().writes(3 as u64))
+    }
+    // Storage: Cert CertsByReceiver (r:1 w:1)
+    // Storage: Cert StorageIdtyCertMeta (r:2 w:2)
+    // Storage: Parameters ParametersStorage (r:1 w:0)
+    // Storage: Membership Membership (r:1 w:0)
+    /// The range of component `i` is `[2, 1000]`.
+    fn remove_all_certs_received_by(i: u32) -> Weight {
+        // Minimum execution time: 223_292 nanoseconds.
+        Weight::from_ref_time(233_586_000 as u64)
+            // Standard Error: 598_929
+            .saturating_add(Weight::from_ref_time(53_659_501 as u64).saturating_mul(i as u64))
+            .saturating_add(RocksDbWeight::get().reads(3 as u64))
+            .saturating_add(RocksDbWeight::get().reads((1 as u64).saturating_mul(i as u64)))
+            .saturating_add(RocksDbWeight::get().writes(1 as u64))
+            .saturating_add(RocksDbWeight::get().writes((1 as u64).saturating_mul(i as u64)))
+    }
+}
diff --git a/pallets/duniter-wot/src/mock.rs b/pallets/duniter-wot/src/mock.rs
index ef15c5518..06226d5b4 100644
--- a/pallets/duniter-wot/src/mock.rs
+++ b/pallets/duniter-wot/src/mock.rs
@@ -170,6 +170,7 @@ impl pallet_certification::Config<Instance1> for Test {
     type OnNewcert = DuniterWot;
     type OnRemovedCert = DuniterWot;
     type RuntimeEvent = RuntimeEvent;
+    type WeightInfo = ();
     type ValidityPeriod = ValidityPeriod;
 }
 
@@ -222,6 +223,7 @@ impl pallet_certification::Config<Instance2> for Test {
     type OnNewcert = SmithsSubWot;
     type OnRemovedCert = SmithsSubWot;
     type RuntimeEvent = RuntimeEvent;
+    type WeightInfo = ();
     type ValidityPeriod = SmithsValidityPeriod;
 }
 
-- 
GitLab