diff --git a/pallets/authority-members/Cargo.toml b/pallets/authority-members/Cargo.toml index 485f2b203f216037df1876df0cac737700ced2ab..01c379921721f796598d9d5fd5085ceb09d719a0 100644 --- a/pallets/authority-members/Cargo.toml +++ b/pallets/authority-members/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', diff --git a/pallets/authority-members/src/benchmarking.rs b/pallets/authority-members/src/benchmarking.rs new file mode 100644 index 0000000000000000000000000000000000000000..dc7ae8358f72918d3e81b0e68241340fd92065e8 --- /dev/null +++ b/pallets/authority-members/src/benchmarking.rs @@ -0,0 +1,77 @@ +// 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; +use frame_system::RawOrigin; + +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()); +} + +benchmarks! { + where_clause { + where + T::MemberId: From<u32>, + } + go_offline { + let id: T::MemberId = OnlineAuthorities::<T>::get()[0]; + let caller: T::AccountId = Members::<T>::get(id).unwrap().owner_key; + let caller_origin: <T as frame_system::Config>::RuntimeOrigin = RawOrigin::Signed(caller.clone()).into(); + }: _<T::RuntimeOrigin>(caller_origin) + verify { + assert_has_event::<T>(Event::<T>::MemberGoOffline(id).into()); + } + go_online { + let id: T::MemberId = OnlineAuthorities::<T>::get()[0]; + let caller: T::AccountId = Members::<T>::get(id).unwrap().owner_key; + let caller_origin: <T as frame_system::Config>::RuntimeOrigin = RawOrigin::Signed(caller.clone()).into(); + OnlineAuthorities::<T>::mutate(|ids| { + ids.retain(|&x| x != id); + }); + OutgoingAuthorities::<T>::mutate(|ids| { + ids.retain(|&x| x != id); + }); + }: _<T::RuntimeOrigin>(caller_origin) + verify { + assert_has_event::<T>(Event::<T>::MemberGoOnline(id).into()); + } + set_session_keys { + let id: T::MemberId = OnlineAuthorities::<T>::get()[0]; + let caller: T::AccountId = Members::<T>::get(id).unwrap().owner_key; + let caller_origin: <T as frame_system::Config>::RuntimeOrigin = RawOrigin::Signed(caller.clone()).into(); + let validator_id = T::ValidatorIdOf::convert(caller.clone()).unwrap(); + let session_keys: T::KeysWrapper = pallet_session::NextKeys::<T>::get(validator_id).unwrap().into(); + }: _<T::RuntimeOrigin>(caller_origin, session_keys) + remove_member { + let id: T::MemberId = OnlineAuthorities::<T>::get()[0]; + let caller_origin = RawOrigin::Root.into(); + }: _<T::RuntimeOrigin>(caller_origin, id.clone()) + verify { + assert_has_event::<T>(Event::<T>::MemberRemoved(id).into()); + } + + impl_benchmark_test_suite!( + Pallet, + crate::mock::new_test_ext(2), + crate::mock::Test + ); +} diff --git a/pallets/authority-members/src/lib.rs b/pallets/authority-members/src/lib.rs index 21b23b64d4783b169da1618cf9f9b7ec656c99a5..22865875fbea14af0c2a236a9fcbec64393425df 100644 --- a/pallets/authority-members/src/lib.rs +++ b/pallets/authority-members/src/lib.rs @@ -19,6 +19,7 @@ pub mod traits; mod types; +pub mod weights; #[cfg(test)] mod mock; @@ -26,11 +27,12 @@ mod mock; #[cfg(test)] mod tests; -/*#[cfg(feature = "runtime-benchmarks")] -mod benchmarking;*/ +#[cfg(feature = "runtime-benchmarks")] +mod benchmarking; pub use pallet::*; pub use types::*; +pub use weights::WeightInfo; use self::traits::*; use frame_support::traits::Get; @@ -63,7 +65,7 @@ pub mod pallet { pub trait Config: frame_system::Config + pallet_session::Config + pallet_session::historical::Config { - type KeysWrapper: Parameter + Into<Self::Keys>; + type KeysWrapper: Parameter + Into<Self::Keys> + From<Self::Keys>; type IsMember: IsMember<Self::MemberId>; type OnNewSession: OnNewSession; type OnRemovedMember: OnRemovedMember<Self::MemberId>; @@ -78,6 +80,7 @@ pub mod pallet { type MemberIdOf: Convert<Self::AccountId, Option<Self::MemberId>>; type RemoveMemberOrigin: EnsureOrigin<Self::RuntimeOrigin>; type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>; + type WeightInfo: WeightInfo; } // GENESIS STUFFĂ‚ // @@ -227,8 +230,8 @@ pub mod pallet { #[pallet::call] impl<T: Config> Pallet<T> { - #[pallet::weight(1_000_000_000)] /// ask to leave the set of validators two sessions after + #[pallet::weight(<T as pallet::Config>::WeightInfo::go_offline())] pub fn go_offline(origin: OriginFor<T>) -> DispatchResultWithPostInfo { // Verification phase // let who = ensure_signed(origin)?; @@ -254,8 +257,8 @@ pub mod pallet { Ok(().into()) } - #[pallet::weight(1_000_000_000)] /// ask to join the set of validators two sessions after + #[pallet::weight(<T as pallet::Config>::WeightInfo::go_online())] pub fn go_online(origin: OriginFor<T>) -> DispatchResultWithPostInfo { // Verification phase // let who = ensure_signed(origin)?; @@ -291,8 +294,8 @@ pub mod pallet { Ok(().into()) } - #[pallet::weight(1_000_000_000)] /// declare new session keys to replace current ones + #[pallet::weight(<T as pallet::Config>::WeightInfo::set_session_keys())] pub fn set_session_keys( origin: OriginFor<T>, keys: T::KeysWrapper, @@ -324,8 +327,8 @@ pub mod pallet { Ok(().into()) } - #[pallet::weight(1_000_000_000)] /// remove an identity from the set of authorities + #[pallet::weight(<T as pallet::Config>::WeightInfo::remove_member())] pub fn remove_member( origin: OriginFor<T>, member_id: T::MemberId, diff --git a/pallets/authority-members/src/mock.rs b/pallets/authority-members/src/mock.rs index d28782a4a0673e0038329f9bc0ad6e2371eee9d2..15a820816b7eadce72f35f9fd44b30ced8d111a2 100644 --- a/pallets/authority-members/src/mock.rs +++ b/pallets/authority-members/src/mock.rs @@ -159,6 +159,7 @@ impl pallet_authority_members::Config for Test { type OnRemovedMember = (); type RemoveMemberOrigin = system::EnsureRoot<u64>; type RuntimeEvent = RuntimeEvent; + type WeightInfo = (); } // Build genesis storage according to the mock runtime. diff --git a/pallets/authority-members/src/weights.rs b/pallets/authority-members/src/weights.rs new file mode 100644 index 0000000000000000000000000000000000000000..3b9d8a03ce258e4542ad4829ba45410f0f16dc5d --- /dev/null +++ b/pallets/authority-members/src/weights.rs @@ -0,0 +1,88 @@ +// 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/>. + +#![allow(clippy::unnecessary_cast)] + +use frame_support::weights::{constants::RocksDbWeight, Weight}; + +/// Weight functions needed for pallet_universal_dividend. +pub trait WeightInfo { + fn go_offline() -> Weight; + fn go_online() -> Weight; + fn set_session_keys() -> Weight; + fn remove_member() -> Weight; +} + +// Insecure weights implementation, use it for tests only! +impl WeightInfo for () { + // Storage: Identity IdentityIndexOf (r:1 w:0) + // Storage: SmithMembership Membership (r:1 w:0) + // Storage: AuthorityMembers Members (r:1 w:0) + // Storage: AuthorityMembers OutgoingAuthorities (r:1 w:1) + // Storage: AuthorityMembers IncomingAuthorities (r:1 w:0) + // Storage: AuthorityMembers OnlineAuthorities (r:1 w:0) + // Storage: AuthorityMembers AuthoritiesCounter (r:1 w:1) + fn go_offline() -> Weight { + // Minimum execution time: 120_876 nanoseconds. + Weight::from_ref_time(122_190_000 as u64) + .saturating_add(RocksDbWeight::get().reads(7 as u64)) + .saturating_add(RocksDbWeight::get().writes(2 as u64)) + } + // Storage: Identity IdentityIndexOf (r:1 w:0) + // Storage: SmithMembership Membership (r:1 w:0) + // Storage: AuthorityMembers Members (r:1 w:0) + // Storage: Session NextKeys (r:1 w:0) + // Storage: AuthorityMembers IncomingAuthorities (r:1 w:1) + // Storage: AuthorityMembers OutgoingAuthorities (r:1 w:0) + // Storage: AuthorityMembers OnlineAuthorities (r:1 w:0) + // Storage: AuthorityMembers AuthoritiesCounter (r:1 w:1) + fn go_online() -> Weight { + // Minimum execution time: 145_521 nanoseconds. + Weight::from_ref_time(157_428_000 as u64) + .saturating_add(RocksDbWeight::get().reads(8 as u64)) + .saturating_add(RocksDbWeight::get().writes(2 as u64)) + } + // Storage: Identity IdentityIndexOf (r:1 w:0) + // Storage: SmithMembership Membership (r:1 w:0) + // Storage: System Account (r:1 w:0) + // Storage: Session NextKeys (r:1 w:1) + // Storage: Session KeyOwner (r:4 w:0) + // Storage: Session CurrentIndex (r:1 w:0) + // Storage: AuthorityMembers Members (r:1 w:1) + // Storage: AuthorityMembers MustRotateKeysBefore (r:1 w:1) + fn set_session_keys() -> Weight { + // Minimum execution time: 181_682 nanoseconds. + Weight::from_ref_time(192_995_000 as u64) + .saturating_add(RocksDbWeight::get().reads(11 as u64)) + .saturating_add(RocksDbWeight::get().writes(3 as u64)) + } + // Storage: AuthorityMembers Members (r:1 w:1) + // Storage: AuthorityMembers OnlineAuthorities (r:1 w:1) + // Storage: AuthorityMembers OutgoingAuthorities (r:1 w:1) + // Storage: AuthorityMembers AuthoritiesCounter (r:1 w:1) + // Storage: AuthorityMembers IncomingAuthorities (r:1 w:1) + // Storage: Session NextKeys (r:1 w:1) + // Storage: System Account (r:1 w:1) + // Storage: SmithMembership Membership (r:1 w:1) + // Storage: SmithMembership CounterForMembership (r:1 w:1) + // Storage: Session KeyOwner (r:0 w:4) + fn remove_member() -> Weight { + // Minimum execution time: 246_592 nanoseconds. + Weight::from_ref_time(256_761_000 as u64) + .saturating_add(RocksDbWeight::get().reads(9 as u64)) + .saturating_add(RocksDbWeight::get().writes(13 as u64)) + } +} diff --git a/runtime/common/src/entities.rs b/runtime/common/src/entities.rs index 3cfb7bbb3d6dd32b949b2e10652c3c0189639a6c..08e84207968fd181235faf5efd6cccb92970c694 100644 --- a/runtime/common/src/entities.rs +++ b/runtime/common/src/entities.rs @@ -43,6 +43,11 @@ macro_rules! declare_session_keys { keys_wrapper.0 } } + impl From<SessionKeys> for SessionKeysWrapper { + fn from(session_keys: SessionKeys) -> SessionKeysWrapper { + SessionKeysWrapper(session_keys) + } + } impl scale_info::TypeInfo for SessionKeysWrapper { type Identity = [u8; 128]; diff --git a/runtime/common/src/pallets_config.rs b/runtime/common/src/pallets_config.rs index ffde06ea2e9fb48828c26e71a9d66eec9e71ed8c..048b1af472a4eba3bee679606e35e722eae59538 100644 --- a/runtime/common/src/pallets_config.rs +++ b/runtime/common/src/pallets_config.rs @@ -206,6 +206,7 @@ macro_rules! pallets_config { type MaxOfflineSessions = frame_support::pallet_prelude::ConstU32<2_400>; type RemoveMemberOrigin = EnsureRoot<Self::AccountId>; type RuntimeEvent = RuntimeEvent; + type WeightInfo = common_runtime::weights::pallet_authority_members::WeightInfo<Runtime>; } impl pallet_authorship::Config for Runtime { type FindAuthor = pallet_session::FindAccountFromAuthorIndex<Self, Babe>; diff --git a/runtime/common/src/weights.rs b/runtime/common/src/weights.rs index c595875addcc6407cac9e2bebaafff634c838c9f..ae297fe44d04d82f81136a38c06df053b74e70a6 100644 --- a/runtime/common/src/weights.rs +++ b/runtime/common/src/weights.rs @@ -38,4 +38,5 @@ pub mod pallet_certification_cert; pub mod pallet_certification_smith_cert; pub mod pallet_membership_membership; pub mod pallet_membership_smith_membership; +pub mod pallet_authority_members; pub mod paritydb_weights; diff --git a/runtime/common/src/weights/pallet_authority_members.rs b/runtime/common/src/weights/pallet_authority_members.rs new file mode 100644 index 0000000000000000000000000000000000000000..06b47a3e58d335f225d10f2b6461bb28b5525976 --- /dev/null +++ b/runtime/common/src/weights/pallet_authority_members.rs @@ -0,0 +1,106 @@ +// 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/>. + +//! Autogenerated weights for `pallet_authority_members` +//! +//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev +//! DATE: 2023-04-25, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! HOSTNAME: `benjamin-xps139380`, CPU: `Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz` +//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024 + +// Executed Command: +// target/release/duniter +// benchmark +// pallet +// --chain=dev +// --steps=50 +// --repeat=20 +// --pallet=pallet_authority_members +// --extrinsic=* +// --execution=wasm +// --wasm-execution=compiled +// --heap-pages=4096 +// --header=./file_header.txt +// --output=./runtime/common/src/weights/ + +#![cfg_attr(rustfmt, rustfmt_skip)] +#![allow(unused_parens)] +#![allow(unused_imports)] + +use frame_support::{traits::Get, weights::Weight}; +use sp_std::marker::PhantomData; + +/// Weight functions for `pallet_authority_members`. +pub struct WeightInfo<T>(PhantomData<T>); +impl<T: frame_system::Config> pallet_authority_members::WeightInfo for WeightInfo<T> { + // Storage: Identity IdentityIndexOf (r:1 w:0) + // Storage: SmithMembership Membership (r:1 w:0) + // Storage: AuthorityMembers Members (r:1 w:0) + // Storage: AuthorityMembers OutgoingAuthorities (r:1 w:1) + // Storage: AuthorityMembers IncomingAuthorities (r:1 w:0) + // Storage: AuthorityMembers OnlineAuthorities (r:1 w:0) + // Storage: AuthorityMembers AuthoritiesCounter (r:1 w:1) + fn go_offline() -> Weight { + // Minimum execution time: 116_372 nanoseconds. + Weight::from_ref_time(120_732_000 as u64) + .saturating_add(T::DbWeight::get().reads(7 as u64)) + .saturating_add(T::DbWeight::get().writes(2 as u64)) + } + // Storage: Identity IdentityIndexOf (r:1 w:0) + // Storage: SmithMembership Membership (r:1 w:0) + // Storage: AuthorityMembers Members (r:1 w:0) + // Storage: Session NextKeys (r:1 w:0) + // Storage: AuthorityMembers IncomingAuthorities (r:1 w:1) + // Storage: AuthorityMembers OutgoingAuthorities (r:1 w:0) + // Storage: AuthorityMembers OnlineAuthorities (r:1 w:0) + // Storage: AuthorityMembers AuthoritiesCounter (r:1 w:1) + fn go_online() -> Weight { + // Minimum execution time: 144_006 nanoseconds. + Weight::from_ref_time(157_859_000 as u64) + .saturating_add(T::DbWeight::get().reads(8 as u64)) + .saturating_add(T::DbWeight::get().writes(2 as u64)) + } + // Storage: Identity IdentityIndexOf (r:1 w:0) + // Storage: SmithMembership Membership (r:1 w:0) + // Storage: System Account (r:1 w:0) + // Storage: Session NextKeys (r:1 w:1) + // Storage: Session KeyOwner (r:4 w:0) + // Storage: Session CurrentIndex (r:1 w:0) + // Storage: AuthorityMembers Members (r:1 w:1) + // Storage: AuthorityMembers MustRotateKeysBefore (r:1 w:1) + fn set_session_keys() -> Weight { + // Minimum execution time: 175_589 nanoseconds. + Weight::from_ref_time(178_397_000 as u64) + .saturating_add(T::DbWeight::get().reads(11 as u64)) + .saturating_add(T::DbWeight::get().writes(3 as u64)) + } + // Storage: AuthorityMembers Members (r:1 w:1) + // Storage: AuthorityMembers OnlineAuthorities (r:1 w:1) + // Storage: AuthorityMembers OutgoingAuthorities (r:1 w:1) + // Storage: AuthorityMembers AuthoritiesCounter (r:1 w:1) + // Storage: AuthorityMembers IncomingAuthorities (r:1 w:1) + // Storage: Session NextKeys (r:1 w:1) + // Storage: System Account (r:1 w:1) + // Storage: SmithMembership Membership (r:1 w:1) + // Storage: SmithMembership CounterForMembership (r:1 w:1) + // Storage: Session KeyOwner (r:0 w:4) + fn remove_member() -> Weight { + // Minimum execution time: 242_728 nanoseconds. + Weight::from_ref_time(296_778_000 as u64) + .saturating_add(T::DbWeight::get().reads(9 as u64)) + .saturating_add(T::DbWeight::get().writes(13 as u64)) + } +} diff --git a/runtime/gdev/src/lib.rs b/runtime/gdev/src/lib.rs index cfaada2adca2c558b26b7e4b3a78dc9c8be558e0..a6653571ce405a2083b5a0c3a1a94aa8f96ec8ec 100644 --- a/runtime/gdev/src/lib.rs +++ b/runtime/gdev/src/lib.rs @@ -149,6 +149,7 @@ mod benches { [pallet_identity, Identity] [pallet_membership, Membership] [pallet_membership, SmithMembership] + [pallet_authority_members, AuthorityMembers] // Substrate [pallet_balances, Balances] [frame_benchmarking::baseline, Baseline::<Runtime>]