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

feat(pallet_ud): add benchmarking

parent abf39880
No related branches found
No related tags found
No related merge requests found
...@@ -10,7 +10,10 @@ version = '3.0.0' ...@@ -10,7 +10,10 @@ version = '3.0.0'
[features] [features]
default = ['std'] default = ['std']
runtime-benchmarks = ['frame-benchmarking'] runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"pallet-balances"
]
std = [ std = [
'codec/std', 'codec/std',
'frame-support/std', 'frame-support/std',
...@@ -27,18 +30,16 @@ try-runtime = ['frame-support/try-runtime'] ...@@ -27,18 +30,16 @@ try-runtime = ['frame-support/try-runtime']
# substrate # substrate
scale-info = { version = "1.0", default-features = false, features = ["derive"] } scale-info = { version = "1.0", default-features = false, features = ["derive"] }
# substrate bencharks
frame-benchmarking = { git = 'https://github.com/librelois/substrate.git', branch = 'duniter-monthly-2022-02', optional = true, default-features = false }
pallet-balances = { git = 'https://github.com/librelois/substrate.git', branch = 'duniter-monthly-2022-02', optional = true, default-features = false }
[dependencies.codec] [dependencies.codec]
default-features = false default-features = false
features = ['derive'] features = ['derive']
package = 'parity-scale-codec' package = 'parity-scale-codec'
version = '2.3.1' version = '2.3.1'
[dependencies.frame-benchmarking]
default-features = false
git = 'https://github.com/librelois/substrate.git'
optional = true
branch = 'duniter-monthly-2022-02'
[dependencies.frame-support] [dependencies.frame-support]
default-features = false default-features = false
git = 'https://github.com/librelois/substrate.git' git = 'https://github.com/librelois/substrate.git'
......
// 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/>.
#![cfg(feature = "runtime-benchmarks")]
use super::*;
use frame_benchmarking::{account, benchmarks, whitelisted_caller};
use frame_support::pallet_prelude::IsType;
use frame_support::traits::{Get, OnInitialize};
use frame_system::RawOrigin;
use pallet_balances::Pallet as Balances;
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)
on_initialize {
//let n in 1 .. 10_000;
//fill_storage::<T>(n)?;
}: { 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> }
transfer_ud {
let existential_deposit = T::ExistentialDeposit::get();
let caller = whitelisted_caller();
// Give some multiple of the existential deposit
let balance = existential_deposit.saturating_mul(ED_MULTIPLIER.into());
let _ = T::Currency::make_free_balance_be(&caller, balance.into());
// Transfer `e - 1` existential deposits + 1 unit, which guarantees to create one account,
// and reap this user.
let recipient: T::AccountId = account("recipient", 0, SEED);
let recipient_lookup: <T::Lookup as StaticLookup>::Source = T::Lookup::unlookup(recipient.clone());
let transfer_amount = existential_deposit.saturating_mul((ED_MULTIPLIER - 1).into()) + 1u32.into();
let transfer_amount_ud = transfer_amount.saturating_mul(1_000.into()) / Pallet::<T>::current_ud().into();
}: _(RawOrigin::Signed(caller.clone()), recipient_lookup, transfer_amount_ud.into())
verify {
assert_eq!(Balances::<T>::free_balance(&caller), Zero::zero());
assert_eq!(Balances::<T>::free_balance(&recipient), transfer_amount);
}
// Benchmark `transfer_ud_keep_alive` with the worst possible condition:
// * The recipient account is created.
where_clause { where T: pallet_balances::Config, T::Balance: From<u64>, <T::Currency as Currency<T::AccountId>>::Balance: IsType<T::Balance> }
transfer_ud_keep_alive {
let caller = whitelisted_caller();
let recipient: T::AccountId = account("recipient", 0, SEED);
let recipient_lookup: <T::Lookup as StaticLookup>::Source = T::Lookup::unlookup(recipient.clone());
// Give the sender account max funds, thus a transfer will not kill account.
let _ = T::Currency::make_free_balance_be(&caller, <T::Currency as Currency<T::AccountId>>::Balance::max_value());
let existential_deposit = T::ExistentialDeposit::get();
let transfer_amount = existential_deposit.saturating_mul(ED_MULTIPLIER.into());
let transfer_amount_ud = transfer_amount.saturating_mul(1_000.into()) / Pallet::<T>::current_ud().into();
}: _(RawOrigin::Signed(caller.clone()), recipient_lookup, transfer_amount_ud.into())
verify {
assert!(!Balances::<T>::free_balance(&caller).is_zero());
assert_eq!(Balances::<T>::free_balance(&recipient), transfer_amount);
}
impl_benchmark_test_suite!(
Pallet,
crate::mock::new_test_ext(crate::mock::UniversalDividendConfig {
first_reeval: 8,
first_ud: 1_000,
initial_monetary_mass: 0,
}),
crate::mock::Test
);
}
...@@ -18,15 +18,12 @@ ...@@ -18,15 +18,12 @@
pub use pallet::*; pub use pallet::*;
mod benchmarking;
#[cfg(test)] #[cfg(test)]
mod mock; mod mock;
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
/*#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;*/
use frame_support::traits::{tokens::ExistenceRequirement, Currency}; use frame_support::traits::{tokens::ExistenceRequirement, Currency};
use sp_arithmetic::{ use sp_arithmetic::{
per_things::Perbill, per_things::Perbill,
......
...@@ -79,7 +79,7 @@ impl system::Config for Test { ...@@ -79,7 +79,7 @@ impl system::Config for Test {
} }
parameter_types! { parameter_types! {
pub const ExistentialDeposit: Balance = 1; pub const ExistentialDeposit: Balance = 10;
pub const MaxLocks: u32 = 50; pub const MaxLocks: u32 = 50;
} }
......
...@@ -235,12 +235,13 @@ macro_rules! runtime_apis { ...@@ -235,12 +235,13 @@ macro_rules! runtime_apis {
use frame_benchmarking::baseline::Pallet as Baseline; use frame_benchmarking::baseline::Pallet as Baseline;
let mut list = Vec::<BenchmarkList>::new(); let mut list = Vec::<BenchmarkList>::new();
list_benchmark!(list, extra, frame_system, SystemBench::<Runtime>);
list_benchmark!(list, extra, pallet_balances, Balances); list_benchmark!(list, extra, pallet_balances, Balances);
list_benchmark!(list, extra, pallet_multisig, Multisig); list_benchmark!(list, extra, pallet_multisig, Multisig);
list_benchmark!(list, extra, pallet_proxy, Proxy); list_benchmark!(list, extra, pallet_proxy, Proxy);
list_benchmark!(list, extra, pallet_scheduler, Scheduler); list_benchmark!(list, extra, pallet_scheduler, Scheduler);
list_benchmark!(list, extra, pallet_timestamp, Timestamp); list_benchmark!(list, extra, pallet_timestamp, Timestamp);
list_benchmark!(list, extra, frame_system, SystemBench::<Runtime>); list_benchmark!(list, extra, pallet_universal_dividend, UniversalDividend);
list_benchmark!(list, extra, pallet_utility, Utility); list_benchmark!(list, extra, pallet_utility, Utility);
let storage_info = AllPalletsWithSystem::storage_info(); let storage_info = AllPalletsWithSystem::storage_info();
...@@ -279,12 +280,13 @@ macro_rules! runtime_apis { ...@@ -279,12 +280,13 @@ macro_rules! runtime_apis {
let mut batches = Vec::<BenchmarkBatch>::new(); let mut batches = Vec::<BenchmarkBatch>::new();
let params = (&config, &whitelist); let params = (&config, &whitelist);
add_benchmark!(params, batches, frame_system, SystemBench::<Runtime>);
add_benchmark!(params, batches, pallet_balances, Balances); add_benchmark!(params, batches, pallet_balances, Balances);
add_benchmark!(params, batches, pallet_multisig, Multisig); add_benchmark!(params, batches, pallet_multisig, Multisig);
add_benchmark!(params, batches, pallet_proxy, Proxy); add_benchmark!(params, batches, pallet_proxy, Proxy);
add_benchmark!(params, batches, pallet_scheduler, Scheduler); add_benchmark!(params, batches, pallet_scheduler, Scheduler);
add_benchmark!(params, batches, frame_system, SystemBench::<Runtime>);
add_benchmark!(params, batches, pallet_timestamp, Timestamp); add_benchmark!(params, batches, pallet_timestamp, Timestamp);
add_benchmark!(params, batches, pallet_universal_dividend, UniversalDividend);
add_benchmark!(params, batches, pallet_utility, Utility); add_benchmark!(params, batches, pallet_utility, Utility);
if batches.is_empty() { return Err("Benchmark not found for this pallet.".into()) } if batches.is_empty() { return Err("Benchmark not found for this pallet.".into()) }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment