diff --git a/Cargo.lock b/Cargo.lock index 6e2559994c044b30c296cf7be4df1ce2808e242a..582ce61c7d1bb2da5971ab36f4e0cfe6f02b1a1a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5849,6 +5849,7 @@ dependencies = [ "frame-system", "pallet-authority-members", "pallet-authorship", + "pallet-balances", "pallet-identity", "pallet-membership", "pallet-session", @@ -5857,6 +5858,8 @@ dependencies = [ "sp-core", "sp-distance", "sp-inherents", + "sp-io", + "sp-keystore", "sp-runtime", "sp-std 5.0.0", ] diff --git a/pallets/distance/Cargo.toml b/pallets/distance/Cargo.toml index b1910ffba55fb1d69ad8907e3cb9f6b577a7a3ce..5a04dab80072c91c3d51f535ba443d4581341a23 100644 --- a/pallets/distance/Cargo.toml +++ b/pallets/distance/Cargo.toml @@ -11,7 +11,9 @@ version = '1.0.0' [features] default = ['std'] -runtime-benchmarks = ['frame-benchmarking/runtime-benchmarks'] +runtime-benchmarks = ["frame-benchmarking/runtime-benchmarks", +"pallet-balances", +] std = [ 'codec/std', 'frame-support/std', @@ -39,11 +41,7 @@ scale-info = { version = "2.1.1", default-features = false, features = [ "derive", ] } -[dependencies.frame-benchmarking] -default-features = false -git = 'https://github.com/duniter/substrate' -optional = true -branch = 'duniter-substrate-v0.9.42' +pallet-balances = { git = 'https://github.com/duniter/substrate', branch = 'duniter-substrate-v0.9.42', optional = true, default-features = false } [dependencies.codec] default-features = false @@ -51,6 +49,12 @@ features = ['derive'] package = 'parity-scale-codec' version = '3.1.5' +[dependencies.frame-benchmarking] +default-features = false +git = 'https://github.com/duniter/substrate' +optional = true +branch = 'duniter-substrate-v0.9.42' + [dependencies.frame-support] default-features = false git = 'https://github.com/duniter/substrate' @@ -95,3 +99,8 @@ branch = 'duniter-substrate-v0.9.42' [package.metadata.docs.rs] targets = ['x86_64-unknown-linux-gnu'] + +[dev-dependencies] +pallet-balances = { git = 'https://github.com/duniter/substrate', branch = 'duniter-substrate-v0.9.42' } +sp-io = { git = 'https://github.com/duniter/substrate', branch = 'duniter-substrate-v0.9.42' } +sp-keystore = { git = 'https://github.com/duniter/substrate', branch = 'duniter-substrate-v0.9.42' } diff --git a/pallets/distance/src/benchmarking.rs b/pallets/distance/src/benchmarking.rs new file mode 100644 index 0000000000000000000000000000000000000000..be6df00c20868fbd4c777307b4d14a1d3f2055bd --- /dev/null +++ b/pallets/distance/src/benchmarking.rs @@ -0,0 +1,64 @@ +// 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, Vec}; +use frame_support::traits::Currency; +use frame_system::RawOrigin; +use pallet_balances::Pallet as Balances; +use sp_runtime::traits::{Bounded, One}; +use sp_runtime::Perbill; + +use crate::Pallet; + +benchmarks! { + where_clause { + where + T: pallet_balances::Config, T::Balance: From<u64>, + T::BlockNumber: From<u32>, + } + request_distance_evaluation { + let idty = T::IdtyIndex::one(); + let caller: T::AccountId = pallet_identity::Identities::<T>::get(idty).unwrap().owner_key; + let caller_origin: <T as frame_system::Config>::RuntimeOrigin = RawOrigin::Signed(caller.clone()).into(); + let _ = <Balances<T> as Currency<_>>::make_free_balance_be(&caller, T::Balance::max_value()); + }: _<T::RuntimeOrigin>(caller_origin.clone()) + verify { + assert!(IdentityDistanceStatus::<T>::get(&idty) == Some((caller, DistanceStatus::Pending)), "Request not added"); + } + update_evaluation { + let idty = T::IdtyIndex::one(); + let caller: T::AccountId = pallet_identity::Identities::<T>::get(idty).unwrap().owner_key; + let caller_origin: <T as frame_system::Config>::RuntimeOrigin = RawOrigin::Signed(caller.clone()).into(); + let computation_result = ComputationResult{distances: Vec::<Perbill>::default()}; + }: _<T::RuntimeOrigin>(RawOrigin::None.into(), computation_result) + force_update_evaluation { + let idty = T::IdtyIndex::one(); + let caller: T::AccountId = pallet_identity::Identities::<T>::get(idty).unwrap().owner_key; + let computation_result = ComputationResult{distances: Vec::<Perbill>::default()}; + }: _<T::RuntimeOrigin>(RawOrigin::Root.into(), caller, computation_result) + force_set_distance_status { + let idty = T::IdtyIndex::one(); + let caller: T::AccountId = pallet_identity::Identities::<T>::get(idty).unwrap().owner_key; + let status = Some((caller.clone(), DistanceStatus::Valid)); + }: _<T::RuntimeOrigin>(RawOrigin::Root.into(), idty, status) + verify { + assert!(IdentityDistanceStatus::<T>::get(&idty) == Some((caller, DistanceStatus::Valid)), "Status not set"); + } +} diff --git a/runtime/common/src/pallets_config.rs b/runtime/common/src/pallets_config.rs index e5aaa8c3ff5d82da3d25b61f03e694f0422ec58b..c701a56c9ecdf5838ea77d4726a22bef912c79e5 100644 --- a/runtime/common/src/pallets_config.rs +++ b/runtime/common/src/pallets_config.rs @@ -219,8 +219,11 @@ macro_rules! pallets_config { type WeightInfo = common_runtime::weights::pallet_authority_members::WeightInfo<Runtime>; } impl pallet_authorship::Config for Runtime { - type FindAuthor = pallet_session::FindAccountFromAuthorIndex<Self, Babe>; type EventHandler = ImOnline; + #[cfg(feature = "runtime-benchmarks")] + type FindAuthor = common_runtime::providers::ConstantAuthor<Self::AccountId>; + #[cfg(not(feature = "runtime-benchmarks"))] + type FindAuthor = pallet_session::FindAccountFromAuthorIndex<Self, Babe>; } impl pallet_im_online::Config for Runtime { type AuthorityId = ImOnlineId; diff --git a/runtime/common/src/providers.rs b/runtime/common/src/providers.rs index e8ddf8102dbae4d471b35a370260bdba7c76b6f8..789db5092527434a6de370b6a038ab7a6d94708f 100644 --- a/runtime/common/src/providers.rs +++ b/runtime/common/src/providers.rs @@ -152,3 +152,16 @@ macro_rules! impl_benchmark_setup_handler { impl_benchmark_setup_handler!(pallet_membership::SetDistance<<T as pallet_identity::Config>::IdtyIndex, T::AccountId>); #[cfg(feature = "runtime-benchmarks")] impl_benchmark_setup_handler!(pallet_identity::traits::SetDistance<<T as pallet_identity::Config>::IdtyIndex, T::AccountId>); + +#[cfg(feature = "runtime-benchmarks")] +pub struct ConstantAuthor<T>(PhantomData<T>); + +#[cfg(feature = "runtime-benchmarks")] +impl<T: From<[u8; 32]>> frame_support::traits::FindAuthor<T> for ConstantAuthor<T> { + fn find_author<'a, I>(_: I) -> Option<T> + where + I: 'a + IntoIterator<Item = (sp_runtime::ConsensusEngineId, &'a [u8])>, + { + Some([0; 32].into()) + } +} diff --git a/runtime/gdev/Cargo.toml b/runtime/gdev/Cargo.toml index c7a84e4994097901c544a920207e75ab31806e3f..58faf3aecbf7124d737e1b7fdf92672b59a91d78 100644 --- a/runtime/gdev/Cargo.toml +++ b/runtime/gdev/Cargo.toml @@ -27,6 +27,7 @@ runtime-benchmarks = [ 'pallet-balances/runtime-benchmarks', 'pallet-certification/runtime-benchmarks', 'pallet-collective/runtime-benchmarks', + 'pallet-distance/runtime-benchmarks', 'pallet-duniter-test-parameters/runtime-benchmarks', 'pallet-duniter-account/runtime-benchmarks', 'pallet-duniter-wot/runtime-benchmarks', diff --git a/runtime/gdev/src/lib.rs b/runtime/gdev/src/lib.rs index 5c4491398802f43fae43c87551fbad5483c5ca92..ac89084e6a7b3d012c36a0cac7c2fcd6b7c8e5f9 100644 --- a/runtime/gdev/src/lib.rs +++ b/runtime/gdev/src/lib.rs @@ -143,6 +143,7 @@ mod benches { define_benchmarks!( [pallet_certification, Cert] [pallet_certification, SmithCert] + [pallet_distance, Distance] [pallet_oneshot_account, OneshotAccount] [pallet_universal_dividend, UniversalDividend] [pallet_provide_randomness, ProvideRandomness]