From f207f238ee4fd6c30f2da6372cd2007e5de4c6af Mon Sep 17 00:00:00 2001 From: librelois <c@elo.tf> Date: Sun, 3 Jul 2022 17:06:39 +0200 Subject: [PATCH] pallet ud on_initialize weights --- .cargo/config | 1 + .../universal-dividend/src/benchmarking.rs | 52 ++++++++++++++----- pallets/universal-dividend/src/lib.rs | 26 ++++------ pallets/universal-dividend/src/mock.rs | 19 ------- pallets/universal-dividend/src/weights.rs | 23 ++++++++ .../src/weights/pallet_universal_dividend.rs | 23 ++++++++ 6 files changed, 96 insertions(+), 48 deletions(-) diff --git a/.cargo/config b/.cargo/config index e07889471..3a03e9a4f 100644 --- a/.cargo/config +++ b/.cargo/config @@ -2,5 +2,6 @@ cucumber = "test -p duniter-end2end-tests --test cucumber_tests --" sanity-gdev = "test -p duniter-live-tests --test sanity_gdev -- --nocapture" tu = "test --workspace --exclude duniter-end2end-tests" +tb = "test --features runtime-benchmarks -p" xtask = "run --package xtask --" diff --git a/pallets/universal-dividend/src/benchmarking.rs b/pallets/universal-dividend/src/benchmarking.rs index 530065d03..0d4cfafc6 100644 --- a/pallets/universal-dividend/src/benchmarking.rs +++ b/pallets/universal-dividend/src/benchmarking.rs @@ -27,28 +27,55 @@ use sp_runtime::traits::Bounded; use crate::Pallet; -const BLOCK_NUMBER: u32 = 2; const ED_MULTIPLIER: u32 = 10; const SEED: u32 = 0; -/*fn fill_storage<T: Config>( - members_count: u32, -) -> Result<(), &'static str> { - Ok(()) -}*/ - benchmarks! { - // TODO add on_initialize_ud_created and on_initialize_ud_reevalued (after manual UD impl) + where_clause { + where + T: pallet_balances::Config, T::Balance: From<u64>, + <T::Currency as Currency<T::AccountId>>::Balance: IsType<T::Balance> + } on_initialize { - //let n in 1 .. 10_000; - //fill_storage::<T>(n)?; - }: { Pallet::<T>::on_initialize(BLOCK_NUMBER.into()); } + let total_money_created = Pallet::<T>::total_money_created(); + }: { Pallet::<T>::on_initialize(1_u32.into()); } + verify { + assert_eq!(Pallet::<T>::total_money_created(), total_money_created); + } + where_clause { + where + T: pallet_balances::Config, T::Balance: From<u64>, + <T::Currency as Currency<T::AccountId>>::Balance: IsType<T::Balance> + } + on_initialize_ud_created { + let block_number = T::UdCreationPeriod::get(); + let block_number_plus_one: T::BlockNumber = block_number + One::one(); + NextReeval::<T>::put(block_number_plus_one); + }: { Pallet::<T>::on_initialize(block_number.into()); } + verify { + } + where_clause { + where + T: pallet_balances::Config, T::Balance: From<u64>, + <T::Currency as Currency<T::AccountId>>::Balance: IsType<T::Balance> + } + on_initialize_ud_reevalued { + let block_number = T::UdCreationPeriod::get(); + let block_number_plus_one: T::BlockNumber = block_number + One::one(); + NextReeval::<T>::put(block_number_plus_one); + Pallet::<T>::on_initialize(block_number.into()); + NextReeval::<T>::put(block_number); + }: { Pallet::<T>::on_initialize(block_number.into()); } verify { } // Benchmark `transfer_ud` extrinsic with the worst possible conditions: // * Transfer will kill the sender account. // * Transfer will create the recipient account. - where_clause { where T: pallet_balances::Config, T::Balance: From<u64>, <T::Currency as Currency<T::AccountId>>::Balance: IsType<T::Balance> } + where_clause { + where + T: pallet_balances::Config, T::Balance: From<u64>, + <T::Currency as Currency<T::AccountId>>::Balance: IsType<T::Balance> + } transfer_ud { let existential_deposit = T::ExistentialDeposit::get(); let caller = whitelisted_caller(); @@ -94,6 +121,7 @@ benchmarks! { first_reeval: 8, first_ud: 1_000, initial_monetary_mass: 0, + initial_members: Vec::new() }), crate::mock::Test ); diff --git a/pallets/universal-dividend/src/lib.rs b/pallets/universal-dividend/src/lib.rs index 3e12e61f7..d6b181a26 100644 --- a/pallets/universal-dividend/src/lib.rs +++ b/pallets/universal-dividend/src/lib.rs @@ -188,21 +188,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())); - total_weight += Self::reeval_ud(current_members_count) - + Self::create_ud(current_members_count) - + T::DbWeight::get().reads_writes(2, 1); + Self::reeval_ud(current_members_count); + Self::create_ud(current_members_count); + T::WeightInfo::on_initialize_ud_reevalued() } else { - total_weight += - Self::create_ud(current_members_count) + T::DbWeight::get().reads(2); + Self::create_ud(current_members_count); + T::WeightInfo::on_initialize_ud_created() } + } else { + T::WeightInfo::on_initialize() } - total_weight } } @@ -250,9 +250,7 @@ pub mod pallet { // INTERNAL FUNCTIONS // impl<T: Config> Pallet<T> { - fn create_ud(members_count: BalanceOf<T>) -> Weight { - let total_weight: Weight = 0; - + fn create_ud(members_count: BalanceOf<T>) { let ud_amount = <CurrentUd<T>>::get(); let monetary_mass = <MonetaryMass<T>>::get(); @@ -270,8 +268,6 @@ pub mod pallet { members_count, monetary_mass: new_monetary_mass, }); - - total_weight } fn do_claim_uds(who: &T::AccountId) -> DispatchResultWithPostInfo { T::MembersStorage::try_mutate_exists(who, |maybe_first_eligible_ud| { @@ -321,9 +317,7 @@ pub mod pallet { )?; Ok(().into()) } - fn reeval_ud(members_count: BalanceOf<T>) -> Weight { - let total_weight: Weight = 0; - + fn reeval_ud(members_count: BalanceOf<T>) { let ud_amount = <CurrentUd<T>>::get(); let monetary_mass = <MonetaryMass<T>>::get(); @@ -353,8 +347,6 @@ pub mod pallet { monetary_mass, members_count, }); - - total_weight } fn reeval_ud_formula( ud_t: BalanceOf<T>, diff --git a/pallets/universal-dividend/src/mock.rs b/pallets/universal-dividend/src/mock.rs index e424d7670..238a3d596 100644 --- a/pallets/universal-dividend/src/mock.rs +++ b/pallets/universal-dividend/src/mock.rs @@ -102,25 +102,6 @@ parameter_types! { pub const UdReevalPeriod: BlockNumber = 8; } -pub struct FakeWot; -impl frame_support::traits::StoredMap<u64, FirstEligibleUd> for FakeWot { - fn get(key: &u64) -> FirstEligibleUd { - match key { - 1 | 2 | 3 => FirstEligibleUd::min(), - _ => FirstEligibleUd(None), - } - } - fn try_mutate_exists<R, E: From<sp_runtime::DispatchError>>( - key: &u64, - f: impl FnOnce(&mut Option<FirstEligibleUd>) -> Result<R, E>, - ) -> Result<R, E> { - match key { - 1 | 2 | 3 => f(&mut Some(FirstEligibleUd::min())), - _ => f(&mut None), - } - } -} - impl pallet_universal_dividend::Config for Test { type BlockNumberIntoBalance = sp_runtime::traits::ConvertInto; type Currency = pallet_balances::Pallet<Test>; diff --git a/pallets/universal-dividend/src/weights.rs b/pallets/universal-dividend/src/weights.rs index 6dfab2bbd..d82e24dab 100644 --- a/pallets/universal-dividend/src/weights.rs +++ b/pallets/universal-dividend/src/weights.rs @@ -21,6 +21,8 @@ use frame_support::weights::{constants::RocksDbWeight, Weight}; /// Weight functions needed for pallet_universal_dividend. pub trait WeightInfo { fn on_initialize() -> Weight; + fn on_initialize_ud_created() -> Weight; + fn on_initialize_ud_reevalued() -> Weight; fn transfer_ud() -> Weight; fn transfer_ud_keep_alive() -> Weight; } @@ -31,6 +33,27 @@ impl WeightInfo for () { fn on_initialize() -> Weight { 2_260_000 as Weight } + // Storage: Membership CounterForMembership (r:1 w:0) + // Storage: UniversalDividend NextReeval (r:1 w:0) + // Storage: UniversalDividend CurrentUd (r:1 w:0) + // Storage: UniversalDividend MonetaryMass (r:1 w:1) + // Storage: UniversalDividend CurrentUdIndex (r:1 w:1) + fn on_initialize_ud_created() -> Weight { + (20_160_000 as Weight) + .saturating_add(RocksDbWeight::get().reads(5 as Weight)) + .saturating_add(RocksDbWeight::get().writes(2 as Weight)) + } + // Storage: Membership CounterForMembership (r:1 w:0) + // Storage: UniversalDividend NextReeval (r:1 w:1) + // Storage: UniversalDividend CurrentUd (r:1 w:1) + // Storage: UniversalDividend MonetaryMass (r:1 w:1) + // Storage: UniversalDividend PastReevals (r:1 w:1) + // Storage: UniversalDividend CurrentUdIndex (r:1 w:1) + fn on_initialize_ud_reevalued() -> Weight { + (32_770_000 as Weight) + .saturating_add(RocksDbWeight::get().reads(6 as Weight)) + .saturating_add(RocksDbWeight::get().writes(5 as Weight)) + } // Storage: UniversalDividend CurrentUd (r:1 w:0) // Storage: System Account (r:1 w:1) // Storage: Account PendingNewAccounts (r:0 w:1) diff --git a/runtime/common/src/weights/pallet_universal_dividend.rs b/runtime/common/src/weights/pallet_universal_dividend.rs index 1aab206a0..81571c5e4 100644 --- a/runtime/common/src/weights/pallet_universal_dividend.rs +++ b/runtime/common/src/weights/pallet_universal_dividend.rs @@ -50,6 +50,29 @@ impl<T: frame_system::Config> pallet_universal_dividend::WeightInfo for WeightIn (104_055_000 as Weight) .saturating_add(T::DbWeight::get().reads(1 as Weight)) } + // Storage: Parameters ParametersStorage (r:1 w:0) + // Storage: Membership CounterForMembership (r:1 w:0) + // Storage: UniversalDividend NextReeval (r:1 w:0) + // Storage: UniversalDividend CurrentUd (r:1 w:0) + // Storage: UniversalDividend MonetaryMass (r:1 w:1) + // Storage: UniversalDividend CurrentUdIndex (r:1 w:1) + fn on_initialize_ud_created() -> Weight { + (1_000_000_000 as Weight) + .saturating_add(T::DbWeight::get().reads(6 as Weight)) + .saturating_add(T::DbWeight::get().writes(2 as Weight)) + } + // Storage: Parameters ParametersStorage (r:1 w:0) + // Storage: Membership CounterForMembership (r:1 w:0) + // Storage: UniversalDividend NextReeval (r:1 w:1) + // Storage: UniversalDividend CurrentUd (r:1 w:1) + // Storage: UniversalDividend MonetaryMass (r:1 w:1) + // Storage: UniversalDividend PastReevals (r:1 w:1) + // Storage: UniversalDividend CurrentUdIndex (r:1 w:1) + fn on_initialize_ud_reevalued() -> Weight { + (1_500_000_000 as Weight) + .saturating_add(T::DbWeight::get().reads(7 as Weight)) + .saturating_add(T::DbWeight::get().writes(5 as Weight)) + } // Storage: UniversalDividend CurrentUd (r:1 w:0) // Storage: System Account (r:1 w:1) // Storage: Account PendingNewAccounts (r:0 w:1) -- GitLab