Skip to content
Snippets Groups Projects
Unverified Commit 483a1570 authored by bgallois's avatar bgallois
Browse files

feat: add pallet duniter-account benchmark

parent d2443ff5
No related branches found
No related tags found
1 merge request!140Duniter-account pallet benchmark
......@@ -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
// 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); }
}
// 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::*;
......
......@@ -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>]
......
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