diff --git a/pallets/provide-randomness/src/benchmarking.rs b/pallets/provide-randomness/src/benchmarking.rs index 1646363241fdd7be7cfc4ab9e15806fee1d6a941..4562461eef743de897ebec43ee2dcd682b2ec2cf 100644 --- a/pallets/provide-randomness/src/benchmarking.rs +++ b/pallets/provide-randomness/src/benchmarking.rs @@ -44,11 +44,27 @@ fn add_requests_next_block<T: Config>(i: u32) -> Result<(), &'static str> { Ok(()) } +fn add_requests_next_epoch<T: Config>(i: u32) -> Result<(), &'static str> { + for _ in 0..i { + let salt: H256 = H256([0; 32]); + let request_id = RequestIdProvider::<T>::mutate(|next_request_id| { + core::mem::replace(next_request_id, next_request_id.saturating_add(1)) + }); + RequestsIds::<T>::insert(request_id, ()); + RequestsReadyAtEpoch::<T>::append( + T::GetCurrentEpochIndex::get(), + Request { request_id, salt }, + ); + } + Ok(()) +} + benchmarks! { where_clause { where T: pallet_balances::Config, T::Balance: From<u64>, - <T::Currency as Currency<T::AccountId>>::Balance: IsType<T::Balance> + <T::Currency as Currency<T::AccountId>>::Balance: IsType<T::Balance>, + T::BlockNumber: From<u32>, } request { // Get account @@ -70,16 +86,30 @@ benchmarks! { assert_has_event::<T>(Event::RequestedRandomness { request_id: request_id, salt: salt, r#type: random }.into() ); } - - // Complexity depends on number of requests in RequestsReadyAtNextBlock and - // in the RequestsReadyAtEpoch(at current epoch) and the sum of the two are bounded by MaxRequests. - // The complexity is reduced to the number of elements in RequestsIds since the processing - // of the two lists is quasi-identical. on_initialize { let i in 1 .. T::MaxRequests::get() => add_requests_next_block::<T>(i)?; ensure!(RequestsIds::<T>::count() == i, "List not filled properly."); + ensure!(RequestsReadyAtNextBlock::<T>::get().len() == i as usize, "List not filled properly."); + let next_epoch_hook_in = NexEpochHookIn::<T>::mutate(|next_in| { + core::mem::replace(next_in, next_in.saturating_sub(1)) + }); + ensure!(next_epoch_hook_in != 1, "Will be next epoch."); }: { Pallet::<T>::on_initialize(T::BlockNumber::one()); } verify { ensure!(RequestsIds::<T>::count() == 0, "List not processed."); + ensure!(RequestsReadyAtNextBlock::<T>::get().len() == 0, "List not processed."); + } + on_initialize_epoch { + let i in 1 .. T::MaxRequests::get() => add_requests_next_epoch::<T>(i)?; + ensure!(RequestsReadyAtNextBlock::<T>::get().len() == 0, "List not filled properly."); + ensure!(RequestsIds::<T>::count() == i, "List not filled properly."); + ensure!(RequestsReadyAtEpoch::<T>::get(T::GetCurrentEpochIndex::get()).len() == i as usize, "List not filled properly."); + let next_epoch_hook_in = NexEpochHookIn::<T>::mutate(|next_in| { + core::mem::replace(next_in, 1) + }); + }: { Pallet::<T>::on_initialize(1.into()); } + verify { + ensure!(RequestsIds::<T>::count() == 0, "List not processed."); + ensure!(RequestsReadyAtEpoch::<T>::get(T::GetCurrentEpochIndex::get()).len() == 0, "List not processed properly."); } } diff --git a/pallets/provide-randomness/src/lib.rs b/pallets/provide-randomness/src/lib.rs index 8abba63a029b6deca2ca98db31522092acfafafe..bc671396043a80d689e83cf8108d985e34ee493e 100644 --- a/pallets/provide-randomness/src/lib.rs +++ b/pallets/provide-randomness/src/lib.rs @@ -172,11 +172,9 @@ pub mod pallet { #[pallet::hooks] impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> { fn on_initialize(_: T::BlockNumber) -> Weight { - let request_weight = T::WeightInfo::on_initialize(T::MaxRequests::get()); + // Overhead to process an empty request + let mut total_weight = T::WeightInfo::on_initialize(0); - let mut total_weight = Weight::zero(); - - total_weight += request_weight; for Request { request_id, salt } in RequestsReadyAtNextBlock::<T>::take() { let randomness = T::ParentBlockRandomness::random(salt.as_ref()) .0 @@ -187,14 +185,15 @@ pub mod pallet { request_id, randomness, }); - total_weight += request_weight; + // Weight to process on request + total_weight += + T::WeightInfo::on_initialize(2).saturating_sub(T::WeightInfo::on_initialize(1)); } let next_epoch_hook_in = NexEpochHookIn::<T>::mutate(|next_in| { core::mem::replace(next_in, next_in.saturating_sub(1)) }); if next_epoch_hook_in == 1 { - total_weight += request_weight; for Request { request_id, salt } in RequestsReadyAtEpoch::<T>::take(T::GetCurrentEpochIndex::get()) { @@ -206,7 +205,9 @@ pub mod pallet { request_id, randomness, }); - total_weight += request_weight; + // Weight to process on request + total_weight += T::WeightInfo::on_initialize_epoch(2) + .saturating_sub(T::WeightInfo::on_initialize_epoch(1)); } } diff --git a/pallets/provide-randomness/src/weights.rs b/pallets/provide-randomness/src/weights.rs index cc3edaf1708323fdfa11e159ef212c15038b97c7..be2f03e8710d8f6e329d3e29d40e5320a8894e2d 100644 --- a/pallets/provide-randomness/src/weights.rs +++ b/pallets/provide-randomness/src/weights.rs @@ -21,6 +21,7 @@ use frame_support::weights::{constants::RocksDbWeight, Weight}; /// Weight functions needed for pallet_universal_dividend. pub trait WeightInfo { fn on_initialize(i: u32) -> Weight; + fn on_initialize_epoch(i: u32) -> Weight; fn request() -> Weight; } @@ -55,4 +56,14 @@ impl WeightInfo for () { .saturating_add(RocksDbWeight::get().writes(3 as u64)) .saturating_add(RocksDbWeight::get().writes((1 as u64).saturating_mul(i as u64))) } + fn on_initialize_epoch(i: u32) -> Weight { + // Minimum execution time: 175_645 nanoseconds. + Weight::from_parts(461_442_906 as u64, 0) + // Standard Error: 1_523_561 + .saturating_add(Weight::from_parts(43_315_015 as u64, 0).saturating_mul(i as u64)) + .saturating_add(RocksDbWeight::get().reads(4 as u64)) + .saturating_add(RocksDbWeight::get().reads((2 as u64).saturating_mul(i as u64))) + .saturating_add(RocksDbWeight::get().writes(3 as u64)) + .saturating_add(RocksDbWeight::get().writes((1 as u64).saturating_mul(i as u64))) + } } diff --git a/runtime/common/src/weights/pallet_provide_randomness.rs b/runtime/common/src/weights/pallet_provide_randomness.rs index d615e841a3b6de8c97bfada4bc677bba50300a7d..202f4f5d0c5302f2e783fe7c37b54409be719f76 100644 --- a/runtime/common/src/weights/pallet_provide_randomness.rs +++ b/runtime/common/src/weights/pallet_provide_randomness.rs @@ -1,41 +1,28 @@ -// 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_provide_randomness` //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2023-06-08, STEPS: `5`, REPEAT: `2`, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2023-11-22, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]` //! WORST CASE MAP SIZE: `1000000` -//! HOSTNAME: `benjamin-xps139380`, CPU: `Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz` -//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("gdev-benchmark"), DB CACHE: 1024 +//! HOSTNAME: `bgallois-ms7d43`, CPU: `12th Gen Intel(R) Core(TM) i3-12100F` +//! EXECUTION: None, WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024 // Executed Command: -// target/release/duniter +// ./target/release/duniter // benchmark // pallet -// --chain=gdev-benchmark -// --steps=5 -// --repeat=2 -// --pallet=* -// --extrinsic=* -// --execution=wasm +// --chain +// dev // --wasm-execution=compiled -// --heap-pages=4096 -// --header=./file_header.txt -// --output=./runtime/common/src/weights/ +// --pallet +// pallet-provide-randomness +// --extrinsic +// * +// --steps +// 50 +// --repeat +// 20 +// --output=runtime/common/src/weights/ #![cfg_attr(rustfmt, rustfmt_skip)] #![allow(unused_parens)] @@ -64,8 +51,8 @@ impl<T: frame_system::Config> pallet_provide_randomness::WeightInfo for WeightIn // Proof Size summary in bytes: // Measured: `235` // Estimated: `3700` - // Minimum execution time: 364_880_000 picoseconds. - Weight::from_parts(390_089_000, 0) + // Minimum execution time: 17_882_000 picoseconds. + Weight::from_parts(18_516_000, 0) .saturating_add(Weight::from_parts(0, 3700)) .saturating_add(T::DbWeight::get().reads(6)) .saturating_add(T::DbWeight::get().writes(4)) @@ -85,17 +72,51 @@ impl<T: frame_system::Config> pallet_provide_randomness::WeightInfo for WeightIn /// The range of component `i` is `[1, 100]`. fn on_initialize(i: u32, ) -> Weight { // Proof Size summary in bytes: - // Measured: `375 + i * (59 ±0)` - // Estimated: `1866 + i * (2535 ±0)` - // Minimum execution time: 155_829_000 picoseconds. - Weight::from_parts(250_060_279, 0) - .saturating_add(Weight::from_parts(0, 1866)) - // Standard Error: 1_564_408 - .saturating_add(Weight::from_parts(28_734_558, 0).saturating_mul(i.into())) + // Measured: `383 + i * (59 ±0)` + // Estimated: `1869 + i * (2535 ±0)` + // Minimum execution time: 12_923_000 picoseconds. + Weight::from_parts(11_245_683, 0) + .saturating_add(Weight::from_parts(0, 1869)) + // Standard Error: 5_906 + .saturating_add(Weight::from_parts(3_623_077, 0).saturating_mul(i.into())) .saturating_add(T::DbWeight::get().reads(4)) .saturating_add(T::DbWeight::get().reads((2_u64).saturating_mul(i.into()))) .saturating_add(T::DbWeight::get().writes(3)) .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(i.into()))) .saturating_add(Weight::from_parts(0, 2535).saturating_mul(i.into())) } + /// Storage: ProvideRandomness RequestsReadyAtNextBlock (r:1 w:0) + /// Proof Skipped: ProvideRandomness RequestsReadyAtNextBlock (max_values: Some(1), max_size: None, mode: Measured) + /// Storage: ProvideRandomness NexEpochHookIn (r:1 w:1) + /// Proof Skipped: ProvideRandomness NexEpochHookIn (max_values: Some(1), max_size: None, mode: Measured) + /// Storage: Babe EpochIndex (r:1 w:0) + /// Proof: Babe EpochIndex (max_values: Some(1), max_size: Some(8), added: 503, mode: MaxEncodedLen) + /// Storage: ProvideRandomness RequestsReadyAtEpoch (r:1 w:1) + /// Proof Skipped: ProvideRandomness RequestsReadyAtEpoch (max_values: None, max_size: None, mode: Measured) + /// Storage: Babe NextRandomness (r:1 w:0) + /// Proof: Babe NextRandomness (max_values: Some(1), max_size: Some(32), added: 527, mode: MaxEncodedLen) + /// Storage: Babe EpochStart (r:1 w:0) + /// Proof: Babe EpochStart (max_values: Some(1), max_size: Some(8), added: 503, mode: MaxEncodedLen) + /// Storage: ProvideRandomness RequestsIds (r:100 w:100) + /// Proof Skipped: ProvideRandomness RequestsIds (max_values: None, max_size: None, mode: Measured) + /// Storage: ProvideRandomness CounterForRequestsIds (r:1 w:1) + /// Proof: ProvideRandomness CounterForRequestsIds (max_values: Some(1), max_size: Some(4), added: 499, mode: MaxEncodedLen) + /// Storage: Account PendingRandomIdAssignments (r:100 w:0) + /// Proof Skipped: Account PendingRandomIdAssignments (max_values: None, max_size: None, mode: Measured) + /// The range of component `i` is `[1, 100]`. + fn on_initialize_epoch(i: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `393 + i * (59 ±0)` + // Estimated: `3859 + i * (2535 ±0)` + // Minimum execution time: 12_898_000 picoseconds. + Weight::from_parts(11_213_265, 0) + .saturating_add(Weight::from_parts(0, 3859)) + // Standard Error: 4_684 + .saturating_add(Weight::from_parts(3_741_329, 0).saturating_mul(i.into())) + .saturating_add(T::DbWeight::get().reads(7)) + .saturating_add(T::DbWeight::get().reads((2_u64).saturating_mul(i.into()))) + .saturating_add(T::DbWeight::get().writes(3)) + .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(i.into()))) + .saturating_add(Weight::from_parts(0, 2535).saturating_mul(i.into())) + } }