diff --git a/pallets/duniter-account/Cargo.toml b/pallets/duniter-account/Cargo.toml index 169cbff1bb02631ea420adeb35f37ebdd7cb9f74..1631473ecd3de557774322a7de442c9592268054 100644 --- a/pallets/duniter-account/Cargo.toml +++ b/pallets/duniter-account/Cargo.toml @@ -11,7 +11,9 @@ version = '3.0.0' [features] default = ['std'] -runtime-benchmarks = ['frame-benchmarking'] +runtime-benchmarks = [ + 'frame-benchmarking/runtime-benchmarks', +] std = [ 'codec/std', 'frame-support/std', @@ -108,4 +110,4 @@ version = '1.0.119' [dev-dependencies.sp-io] git = 'https://github.com/duniter/substrate' -branch = 'duniter-substrate-v0.9.32' +branch = 'duniter-substrate-v0.9.32' \ No newline at end of file diff --git a/pallets/duniter-account/src/benchmarking.rs b/pallets/duniter-account/src/benchmarking.rs new file mode 100644 index 0000000000000000000000000000000000000000..f78891665a283a292d92428c3eb4e06711821307 --- /dev/null +++ b/pallets/duniter-account/src/benchmarking.rs @@ -0,0 +1,87 @@ +// 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/>. + +#![cfg(feature = "runtime-benchmarks")] + +use super::*; + +use frame_benchmarking::{benchmarks, whitelisted_caller}; +use frame_support::sp_runtime::{traits::One, Saturating}; +use frame_support::traits::{Currency, Get}; +use pallet_provide_randomness::OnFilledRandomness; + +use crate::Pallet; + +fn assert_has_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) { + frame_system::Pallet::<T>::assert_has_event(generic_event.into()); +} + +fn create_pending_accounts<T: Config>( + i: u32, + is_balance: bool, + is_sufficient: bool, +) -> Result<(), &'static str> { + for _ in 0..i { + let caller: T::AccountId = whitelisted_caller(); + if is_balance { + let existential_deposit = T::ExistentialDeposit::get(); + let balance = existential_deposit.saturating_mul((200u32).into()); + let _ = <pallet_balances::Pallet<T> as Currency<T::AccountId>>::make_free_balance_be( + &caller, + balance.into(), + ); + } else { + assert!( + frame_system::Pallet::<T>::get(&caller).free + < T::NewAccountPrice::get() + T::ExistentialDeposit::get() + ); + } + if is_sufficient { + frame_system::Pallet::<T>::inc_sufficients(&caller); + } else { + assert!(frame_system::Pallet::<T>::sufficients(&caller) == 0); + } + PendingNewAccounts::<T>::insert(caller, ()); + } + Ok(()) +} + +benchmarks! { + on_initialize_sufficient { + let i in 0 .. T::MaxNewAccountsPerBlock::get() => create_pending_accounts::<T>(i, false, true)?; + }: { Pallet::<T>::on_initialize(T::BlockNumber::one()); } + on_initialize_with_balance { + let i in 0 .. T::MaxNewAccountsPerBlock::get() => create_pending_accounts::<T>(i, true, false)?; + }: { Pallet::<T>::on_initialize(T::BlockNumber::one()); } + on_initialize_no_balance { + let i in 0 .. T::MaxNewAccountsPerBlock::get() => create_pending_accounts::<T>(i, false, false)?; + }: { Pallet::<T>::on_initialize(T::BlockNumber::one()); } + on_filled_randomness_pending { + let caller: T::AccountId = whitelisted_caller(); + let randomness = H256(T::AccountIdToSalt::convert(caller.clone())); + let request_id = pallet_provide_randomness::Pallet::<T>::force_request(pallet_provide_randomness::RandomnessType::RandomnessFromTwoEpochsAgo, randomness); + PendingRandomIdAssignments::<T>::insert(request_id, caller.clone()); + }: { Pallet::<T>::on_filled_randomness(request_id, randomness); } + verify { + assert_has_event::<T>(Event::<T>::RandomIdAssigned { who: caller, random_id: randomness }.into()); + } + on_filled_randomness_no_pending { + let caller: T::AccountId = whitelisted_caller(); + let randomness = H256(T::AccountIdToSalt::convert(caller.clone())); + let request_id = pallet_provide_randomness::Pallet::<T>::force_request(pallet_provide_randomness::RandomnessType::RandomnessFromTwoEpochsAgo, randomness); + assert!(!PendingRandomIdAssignments::<T>::contains_key(request_id)); + }: { Pallet::<T>::on_filled_randomness(request_id, randomness); } +} diff --git a/pallets/duniter-account/src/lib.rs b/pallets/duniter-account/src/lib.rs index 221e0ce62ff2781dcb3f07983778bebefe4f3ace..5ca4548ef43cc1596ba9d790461f3909629b1c32 100644 --- a/pallets/duniter-account/src/lib.rs +++ b/pallets/duniter-account/src/lib.rs @@ -1,4 +1,4 @@ -// Copyright 2021 Axiom-Team +// Copyright 2021-2023 Axiom-Team // // This file is part of Duniter-v2S. // @@ -16,6 +16,9 @@ #![cfg_attr(not(feature = "std"), no_std)] +#[cfg(feature = "runtime-benchmarks")] +mod benchmarking; + mod types; pub use pallet::*; diff --git a/runtime/gdev/src/lib.rs b/runtime/gdev/src/lib.rs index 7620c2ce8650e1fa89c1c0177309923dd412dd89..e8f0bf6a709bfb448132780ce505ff7eb1f1210b 100644 --- a/runtime/gdev/src/lib.rs +++ b/runtime/gdev/src/lib.rs @@ -146,6 +146,7 @@ mod benches { [common_runtime::universal_dividend, UniversalDividend] [common_runtime::provide_randomness, ProvideRandomness] [common_runtime::upgrade_origin, UpgradeOrigin] + [common_runtime::duniter_account, Account] // Substrate [pallet_balances, Balances] [frame_benchmarking::baseline, Baseline::<Runtime>]