From 92d5e8f675571edf18ef9af689beed51db12a1c8 Mon Sep 17 00:00:00 2001
From: bgallois <benjamin@gallois.cc>
Date: Fri, 24 Mar 2023 16:33:00 +0100
Subject: [PATCH] feat(pallet_duniter_account): add weights info

---
 pallets/duniter-account/src/lib.rs     | 21 ++++--
 pallets/duniter-account/src/weights.rs | 91 ++++++++++++++++++++++++++
 2 files changed, 107 insertions(+), 5 deletions(-)
 create mode 100644 pallets/duniter-account/src/weights.rs

diff --git a/pallets/duniter-account/src/lib.rs b/pallets/duniter-account/src/lib.rs
index 5ca4548ef..391efe709 100644
--- a/pallets/duniter-account/src/lib.rs
+++ b/pallets/duniter-account/src/lib.rs
@@ -20,9 +20,11 @@
 mod benchmarking;
 
 mod types;
+pub mod weights;
 
 pub use pallet::*;
 pub use types::*;
+pub use weights::WeightInfo;
 
 use frame_support::pallet_prelude::*;
 use frame_support::traits::{OnUnbalanced, StoredMap};
@@ -61,6 +63,8 @@ pub mod pallet {
         type NewAccountPrice: Get<Self::Balance>;
         /// The overarching event type.
         type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
+        /// Type representing the weight of this pallet
+        type WeightInfo: WeightInfo;
     }
 
     // STORAGE //
@@ -162,7 +166,9 @@ pub mod pallet {
                         H256(T::AccountIdToSalt::convert(account_id.clone())),
                     );
                     PendingRandomIdAssignments::<T>::insert(request_id, account_id);
-                    total_weight += Weight::from_ref_time(100_000);
+                    total_weight += <T as pallet::Config>::WeightInfo::on_initialize_sufficient(
+                        T::MaxNewAccountsPerBlock::get(),
+                    );
                 } else {
                     // If the account is not self-sufficient, it must pay the account creation fees
                     let account_data = frame_system::Pallet::<T>::get(&account_id);
@@ -198,7 +204,10 @@ pub mod pallet {
                                 H256(T::AccountIdToSalt::convert(account_id.clone())),
                             );
                             PendingRandomIdAssignments::<T>::insert(request_id, account_id);
-                            total_weight += Weight::from_ref_time(200_000);
+                            total_weight +=
+                                <T as pallet::Config>::WeightInfo::on_initialize_with_balance(
+                                    T::MaxNewAccountsPerBlock::get(),
+                                );
                         }
                     } else {
                         // The charges could not be deducted, we must destroy the account
@@ -213,7 +222,9 @@ pub mod pallet {
                         T::OnUnbalanced::on_unbalanced(pallet_balances::NegativeImbalance::new(
                             balance_to_suppr,
                         ));
-                        total_weight += Weight::from_ref_time(300_000);
+                        total_weight += <T as pallet::Config>::WeightInfo::on_initialize_no_balance(
+                            T::MaxNewAccountsPerBlock::get(),
+                        );
                     }
                 }
             }
@@ -236,9 +247,9 @@ where
                 who: account_id,
                 random_id: randomness,
             });
-            Weight::from_ref_time(200_000)
+            <T as pallet::Config>::WeightInfo::on_filled_randomness_pending()
         } else {
-            Weight::from_ref_time(100_000)
+            <T as pallet::Config>::WeightInfo::on_filled_randomness_no_pending()
         }
     }
 }
diff --git a/pallets/duniter-account/src/weights.rs b/pallets/duniter-account/src/weights.rs
new file mode 100644
index 000000000..54386244f
--- /dev/null
+++ b/pallets/duniter-account/src/weights.rs
@@ -0,0 +1,91 @@
+// Copyright 2021-2023 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 on_initialize_sufficient(i: u32) -> Weight;
+    fn on_initialize_with_balance(i: u32) -> Weight;
+    fn on_initialize_no_balance(i: u32) -> Weight;
+    fn on_filled_randomness_pending() -> Weight;
+    fn on_filled_randomness_no_pending() -> Weight;
+}
+
+// Insecure weights implementation, use it for tests only!
+impl WeightInfo for () {
+    // Storage: Account PendingNewAccounts (r:1 w:0)
+    // Storage: ProvideRandomness RequestIdProvider (r:1 w:1)
+    // Storage: ProvideRandomness RequestsIds (r:1 w:1)
+    // Storage: ProvideRandomness CounterForRequestsIds (r:1 w:1)
+    // Storage: Babe EpochIndex (r:1 w:0)
+    // Storage: ProvideRandomness NexEpochHookIn (r:1 w:0)
+    // Storage: ProvideRandomness RequestsReadyAtEpoch (r:1 w:1)
+    // Storage: Account PendingRandomIdAssignments (r:0 w:1)
+    /// The range of component `i` is `[0, 1]`.
+    fn on_initialize_sufficient(i: u32) -> Weight {
+        // Minimum execution time: 12_958 nanoseconds.
+        Weight::from_ref_time(14_907_902 as u64)
+            // Standard Error: 550_025
+            .saturating_add(Weight::from_ref_time(79_482_297 as u64).saturating_mul(i as u64))
+            .saturating_add(RocksDbWeight::get().reads(1 as u64))
+            .saturating_add(RocksDbWeight::get().reads((6 as u64).saturating_mul(i as u64)))
+            .saturating_add(RocksDbWeight::get().writes((6 as u64).saturating_mul(i as u64)))
+    }
+    // Storage: Account PendingNewAccounts (r:1 w:0)
+    // Storage: ProvideRandomness RequestIdProvider (r:1 w:1)
+    // Storage: ProvideRandomness RequestsIds (r:1 w:1)
+    // Storage: ProvideRandomness CounterForRequestsIds (r:1 w:1)
+    // Storage: Babe EpochIndex (r:1 w:0)
+    // Storage: ProvideRandomness NexEpochHookIn (r:1 w:0)
+    // Storage: ProvideRandomness RequestsReadyAtEpoch (r:1 w:1)
+    // Storage: Account PendingRandomIdAssignments (r:0 w:1)
+    /// The range of component `i` is `[0, 1]`.
+    fn on_initialize_with_balance(i: u32) -> Weight {
+        // Minimum execution time: 12_965 nanoseconds.
+        Weight::from_ref_time(16_754_718 as u64)
+            // Standard Error: 1_790_537
+            .saturating_add(Weight::from_ref_time(164_043_481 as u64).saturating_mul(i as u64))
+            .saturating_add(RocksDbWeight::get().reads(1 as u64))
+            .saturating_add(RocksDbWeight::get().reads((6 as u64).saturating_mul(i as u64)))
+            .saturating_add(RocksDbWeight::get().writes((6 as u64).saturating_mul(i as u64)))
+    }
+    // Storage: Account PendingNewAccounts (r:1 w:0)
+    /// The range of component `i` is `[0, 1]`.
+    fn on_initialize_no_balance(i: u32) -> Weight {
+        // Minimum execution time: 12_912 nanoseconds.
+        Weight::from_ref_time(13_846_469 as u64)
+            // Standard Error: 115_598
+            .saturating_add(Weight::from_ref_time(67_524_530 as u64).saturating_mul(i as u64))
+            .saturating_add(RocksDbWeight::get().reads(1 as u64))
+            .saturating_add(RocksDbWeight::get().writes((1 as u64).saturating_mul(i as u64)))
+    }
+    // Storage: Account PendingRandomIdAssignments (r:1 w:1)
+    fn on_filled_randomness_pending() -> Weight {
+        // Minimum execution time: 66_963 nanoseconds.
+        Weight::from_ref_time(69_757_000 as u64)
+            .saturating_add(RocksDbWeight::get().reads(1 as u64))
+            .saturating_add(RocksDbWeight::get().writes(1 as u64))
+    }
+    // Storage: Account PendingRandomIdAssignments (r:1 w:0)
+    fn on_filled_randomness_no_pending() -> Weight {
+        // Minimum execution time: 16_088 nanoseconds.
+        Weight::from_ref_time(27_963_000 as u64)
+            .saturating_add(RocksDbWeight::get().reads(1 as u64))
+    }
+}
-- 
GitLab