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 @@
#![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,
......
......@@ -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.
......
// 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.
Finish editing this message first!
Please register or to comment