Skip to content
Snippets Groups Projects
Commit 79e0fd4b authored by Éloïs's avatar Éloïs
Browse files

feat(pallet_ud): add weights info

parent 31057e37
No related branches found
No related tags found
No related merge requests found
...@@ -16,13 +16,15 @@ ...@@ -16,13 +16,15 @@
#![cfg_attr(not(feature = "std"), no_std)] #![cfg_attr(not(feature = "std"), no_std)]
pub use pallet::*;
mod benchmarking; mod benchmarking;
#[cfg(test)] #[cfg(test)]
mod mock; mod mock;
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
mod weights;
pub use pallet::*;
pub use weights::WeightInfo;
use frame_support::traits::{tokens::ExistenceRequirement, Currency}; use frame_support::traits::{tokens::ExistenceRequirement, Currency};
use sp_arithmetic::{ use sp_arithmetic::{
...@@ -80,6 +82,8 @@ pub mod pallet { ...@@ -80,6 +82,8 @@ pub mod pallet {
/// Example: If you wish to express the UD amounts with a maximum precision of the order /// Example: If you wish to express the UD amounts with a maximum precision of the order
/// of the milliUD, choose 1000 /// of the milliUD, choose 1000
type UnitsPerUd: Get<BalanceOf<Self>>; type UnitsPerUd: Get<BalanceOf<Self>>;
/// Pallet weights info
type WeightInfo: WeightInfo;
} }
// STORAGE // // STORAGE //
...@@ -136,20 +140,21 @@ pub mod pallet { ...@@ -136,20 +140,21 @@ pub mod pallet {
#[pallet::hooks] #[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> { impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_initialize(n: T::BlockNumber) -> Weight { fn on_initialize(n: T::BlockNumber) -> Weight {
let mut total_weight = T::WeightInfo::on_initialize();
if (n % T::UdCreationPeriod::get()).is_zero() { if (n % T::UdCreationPeriod::get()).is_zero() {
let current_members_count = T::MembersCount::get(); let current_members_count = T::MembersCount::get();
let next_reeval = NextReeval::<T>::get(); let next_reeval = NextReeval::<T>::get();
if n >= next_reeval { if n >= next_reeval {
NextReeval::<T>::put(next_reeval.saturating_add(T::UdReevalPeriod::get())); 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) + Self::create_ud(current_members_count, n)
+ T::DbWeight::get().reads_writes(2, 1) + T::DbWeight::get().reads_writes(2, 1);
} else { } 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 { ...@@ -274,7 +279,7 @@ pub mod pallet {
#[pallet::call] #[pallet::call]
impl<T: Config> Pallet<T> { impl<T: Config> Pallet<T> {
/// Transfer some liquid free balance to another account, in milliUD. /// 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( pub fn transfer_ud(
origin: OriginFor<T>, origin: OriginFor<T>,
dest: <T::Lookup as StaticLookup>::Source, dest: <T::Lookup as StaticLookup>::Source,
...@@ -284,7 +289,7 @@ pub mod pallet { ...@@ -284,7 +289,7 @@ pub mod pallet {
} }
/// Transfer some liquid free balance to another account, in milliUD. /// 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( pub fn transfer_ud_keep_alive(
origin: OriginFor<T>, origin: OriginFor<T>,
dest: <T::Lookup as StaticLookup>::Source, dest: <T::Lookup as StaticLookup>::Source,
......
...@@ -119,6 +119,7 @@ impl pallet_universal_dividend::Config for Test { ...@@ -119,6 +119,7 @@ impl pallet_universal_dividend::Config for Test {
type UdCreationPeriod = UdCreationPeriod; type UdCreationPeriod = UdCreationPeriod;
type UdReevalPeriod = UdReevalPeriod; type UdReevalPeriod = UdReevalPeriod;
type UnitsPerUd = frame_support::traits::ConstU64<1_000>; type UnitsPerUd = frame_support::traits::ConstU64<1_000>;
type WeightInfo = ();
} }
// Build genesis storage according to the mock runtime. // Build genesis storage according to the mock runtime.
......
// 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))
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment