From 79e0fd4bf3b0579279fc957da5e2fdfc6d8a17fa Mon Sep 17 00:00:00 2001
From: librelois <c@elo.tf>
Date: Sun, 12 Jun 2022 23:36:26 +0200
Subject: [PATCH] feat(pallet_ud): add weights info

---
 pallets/universal-dividend/src/lib.rs     | 23 ++++++-----
 pallets/universal-dividend/src/mock.rs    |  1 +
 pallets/universal-dividend/src/weights.rs | 48 +++++++++++++++++++++++
 3 files changed, 63 insertions(+), 9 deletions(-)
 create mode 100644 pallets/universal-dividend/src/weights.rs

diff --git a/pallets/universal-dividend/src/lib.rs b/pallets/universal-dividend/src/lib.rs
index 27b54cd28..ab6fa5361 100644
--- a/pallets/universal-dividend/src/lib.rs
+++ b/pallets/universal-dividend/src/lib.rs
@@ -16,13 +16,15 @@
 
 #![cfg_attr(not(feature = "std"), no_std)]
 
-pub use pallet::*;
-
 mod benchmarking;
 #[cfg(test)]
 mod mock;
 #[cfg(test)]
 mod tests;
+mod weights;
+
+pub use pallet::*;
+pub use weights::WeightInfo;
 
 use frame_support::traits::{tokens::ExistenceRequirement, Currency};
 use sp_arithmetic::{
@@ -80,6 +82,8 @@ pub mod pallet {
         /// Example: If you wish to express the UD amounts with a maximum precision of the order
         /// of the milliUD, choose 1000
         type UnitsPerUd: Get<BalanceOf<Self>>;
+        /// Pallet weights info
+        type WeightInfo: WeightInfo;
     }
 
     // STORAGE //
@@ -136,20 +140,21 @@ pub mod pallet {
     #[pallet::hooks]
     impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
         fn on_initialize(n: T::BlockNumber) -> Weight {
+            let mut total_weight = T::WeightInfo::on_initialize();
             if (n % T::UdCreationPeriod::get()).is_zero() {
                 let current_members_count = T::MembersCount::get();
                 let next_reeval = NextReeval::<T>::get();
                 if n >= next_reeval {
                     NextReeval::<T>::put(next_reeval.saturating_add(T::UdReevalPeriod::get()));
-                    Self::reeval_ud(current_members_count)
+                    total_weight += Self::reeval_ud(current_members_count)
                         + Self::create_ud(current_members_count, n)
-                        + T::DbWeight::get().reads_writes(2, 1)
+                        + T::DbWeight::get().reads_writes(2, 1);
                 } else {
-                    Self::create_ud(current_members_count, n) + T::DbWeight::get().reads(2)
+                    total_weight +=
+                        Self::create_ud(current_members_count, n) + T::DbWeight::get().reads(2);
                 }
-            } else {
-                0
             }
+            total_weight
         }
     }
 
@@ -274,7 +279,7 @@ pub mod pallet {
     #[pallet::call]
     impl<T: Config> Pallet<T> {
         /// Transfer some liquid free balance to another account, in milliUD.
-        #[pallet::weight(1_000_000_000)]
+        #[pallet::weight(T::WeightInfo::transfer_ud())]
         pub fn transfer_ud(
             origin: OriginFor<T>,
             dest: <T::Lookup as StaticLookup>::Source,
@@ -284,7 +289,7 @@ pub mod pallet {
         }
 
         /// Transfer some liquid free balance to another account, in milliUD.
-        #[pallet::weight(1_000_000_000)]
+        #[pallet::weight(T::WeightInfo::transfer_ud_keep_alive())]
         pub fn transfer_ud_keep_alive(
             origin: OriginFor<T>,
             dest: <T::Lookup as StaticLookup>::Source,
diff --git a/pallets/universal-dividend/src/mock.rs b/pallets/universal-dividend/src/mock.rs
index a9ca7b297..668069164 100644
--- a/pallets/universal-dividend/src/mock.rs
+++ b/pallets/universal-dividend/src/mock.rs
@@ -119,6 +119,7 @@ impl pallet_universal_dividend::Config for Test {
     type UdCreationPeriod = UdCreationPeriod;
     type UdReevalPeriod = UdReevalPeriod;
     type UnitsPerUd = frame_support::traits::ConstU64<1_000>;
+    type WeightInfo = ();
 }
 
 // Build genesis storage according to the mock runtime.
diff --git a/pallets/universal-dividend/src/weights.rs b/pallets/universal-dividend/src/weights.rs
new file mode 100644
index 000000000..cf8078d50
--- /dev/null
+++ b/pallets/universal-dividend/src/weights.rs
@@ -0,0 +1,48 @@
+// 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/>.
+
+use frame_support::weights::{constants::RocksDbWeight, Weight};
+
+/// Weight functions needed for pallet_universal_dividend.
+pub trait WeightInfo {
+    fn on_initialize() -> Weight;
+    fn transfer_ud() -> Weight;
+    fn transfer_ud_keep_alive() -> Weight;
+}
+
+// Insecure weights implementation, use it for tests only!
+impl WeightInfo for () {
+    // Storage: (r:0 w:0)
+    fn on_initialize() -> Weight {
+        2_260_000 as Weight
+    }
+    // Storage: UniversalDividend CurrentUd (r:1 w:0)
+    // Storage: System Account (r:1 w:1)
+    // Storage: Account PendingNewAccounts (r:0 w:1)
+    fn transfer_ud() -> Weight {
+        (53_401_000 as Weight)
+            .saturating_add(RocksDbWeight::get().reads(2 as Weight))
+            .saturating_add(RocksDbWeight::get().writes(2 as Weight))
+    }
+    // Storage: UniversalDividend CurrentUd (r:1 w:0)
+    // Storage: System Account (r:1 w:1)
+    // Storage: Account PendingNewAccounts (r:0 w:1)
+    fn transfer_ud_keep_alive() -> Weight {
+        (33_420_000 as Weight)
+            .saturating_add(RocksDbWeight::get().reads(2 as Weight))
+            .saturating_add(RocksDbWeight::get().writes(2 as Weight))
+    }
+}
-- 
GitLab