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

fix pallet_provide_randomness on_initialize weight

parent 8ab552d9
No related branches found
No related tags found
No related merge requests found
This commit is part of merge request !197. Comments created here will be created in the context of that merge request.
...@@ -44,11 +44,27 @@ fn add_requests_next_block<T: Config>(i: u32) -> Result<(), &'static str> { ...@@ -44,11 +44,27 @@ fn add_requests_next_block<T: Config>(i: u32) -> Result<(), &'static str> {
Ok(()) 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! { benchmarks! {
where_clause { where where_clause { where
T: pallet_balances::Config, T: pallet_balances::Config,
T::Balance: From<u64>, 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 { request {
// Get account // Get account
...@@ -70,16 +86,30 @@ benchmarks! { ...@@ -70,16 +86,30 @@ benchmarks! {
assert_has_event::<T>(Event::RequestedRandomness { assert_has_event::<T>(Event::RequestedRandomness {
request_id: request_id, salt: salt, r#type: random }.into() ); 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 { on_initialize {
let i in 1 .. T::MaxRequests::get() => add_requests_next_block::<T>(i)?; let i in 1 .. T::MaxRequests::get() => add_requests_next_block::<T>(i)?;
ensure!(RequestsIds::<T>::count() == i, "List not filled properly."); 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()); } }: { Pallet::<T>::on_initialize(T::BlockNumber::one()); }
verify { verify {
ensure!(RequestsIds::<T>::count() == 0, "List not processed."); 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.");
} }
} }
...@@ -172,11 +172,9 @@ pub mod pallet { ...@@ -172,11 +172,9 @@ pub mod pallet {
#[pallet::hooks] #[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> { impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_initialize(_: T::BlockNumber) -> Weight { 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() { for Request { request_id, salt } in RequestsReadyAtNextBlock::<T>::take() {
let randomness = T::ParentBlockRandomness::random(salt.as_ref()) let randomness = T::ParentBlockRandomness::random(salt.as_ref())
.0 .0
...@@ -187,14 +185,15 @@ pub mod pallet { ...@@ -187,14 +185,15 @@ pub mod pallet {
request_id, request_id,
randomness, 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| { let next_epoch_hook_in = NexEpochHookIn::<T>::mutate(|next_in| {
core::mem::replace(next_in, next_in.saturating_sub(1)) core::mem::replace(next_in, next_in.saturating_sub(1))
}); });
if next_epoch_hook_in == 1 { if next_epoch_hook_in == 1 {
total_weight += request_weight;
for Request { request_id, salt } in for Request { request_id, salt } in
RequestsReadyAtEpoch::<T>::take(T::GetCurrentEpochIndex::get()) RequestsReadyAtEpoch::<T>::take(T::GetCurrentEpochIndex::get())
{ {
...@@ -206,7 +205,9 @@ pub mod pallet { ...@@ -206,7 +205,9 @@ pub mod pallet {
request_id, request_id,
randomness, 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));
} }
} }
......
...@@ -21,6 +21,7 @@ use frame_support::weights::{constants::RocksDbWeight, Weight}; ...@@ -21,6 +21,7 @@ use frame_support::weights::{constants::RocksDbWeight, Weight};
/// Weight functions needed for pallet_universal_dividend. /// Weight functions needed for pallet_universal_dividend.
pub trait WeightInfo { pub trait WeightInfo {
fn on_initialize(i: u32) -> Weight; fn on_initialize(i: u32) -> Weight;
fn on_initialize_epoch(i: u32) -> Weight;
fn request() -> Weight; fn request() -> Weight;
} }
...@@ -55,4 +56,14 @@ impl WeightInfo for () { ...@@ -55,4 +56,14 @@ impl WeightInfo for () {
.saturating_add(RocksDbWeight::get().writes(3 as u64)) .saturating_add(RocksDbWeight::get().writes(3 as u64))
.saturating_add(RocksDbWeight::get().writes((1 as u64).saturating_mul(i 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)))
}
} }
// 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` //! Autogenerated weights for `pallet_provide_randomness`
//! //!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev //! 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` //! WORST CASE MAP SIZE: `1000000`
//! HOSTNAME: `benjamin-xps139380`, CPU: `Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz` //! HOSTNAME: `bgallois-ms7d43`, CPU: `12th Gen Intel(R) Core(TM) i3-12100F`
//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("gdev-benchmark"), DB CACHE: 1024 //! EXECUTION: None, WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024
// Executed Command: // Executed Command:
// target/release/duniter // ./target/release/duniter
// benchmark // benchmark
// pallet // pallet
// --chain=gdev-benchmark // --chain
// --steps=5 // dev
// --repeat=2
// --pallet=*
// --extrinsic=*
// --execution=wasm
// --wasm-execution=compiled // --wasm-execution=compiled
// --heap-pages=4096 // --pallet
// --header=./file_header.txt // pallet-provide-randomness
// --output=./runtime/common/src/weights/ // --extrinsic
// *
// --steps
// 50
// --repeat
// 20
// --output=runtime/common/src/weights/
#![cfg_attr(rustfmt, rustfmt_skip)] #![cfg_attr(rustfmt, rustfmt_skip)]
#![allow(unused_parens)] #![allow(unused_parens)]
...@@ -64,8 +51,8 @@ impl<T: frame_system::Config> pallet_provide_randomness::WeightInfo for WeightIn ...@@ -64,8 +51,8 @@ impl<T: frame_system::Config> pallet_provide_randomness::WeightInfo for WeightIn
// Proof Size summary in bytes: // Proof Size summary in bytes:
// Measured: `235` // Measured: `235`
// Estimated: `3700` // Estimated: `3700`
// Minimum execution time: 364_880_000 picoseconds. // Minimum execution time: 17_882_000 picoseconds.
Weight::from_parts(390_089_000, 0) Weight::from_parts(18_516_000, 0)
.saturating_add(Weight::from_parts(0, 3700)) .saturating_add(Weight::from_parts(0, 3700))
.saturating_add(T::DbWeight::get().reads(6)) .saturating_add(T::DbWeight::get().reads(6))
.saturating_add(T::DbWeight::get().writes(4)) .saturating_add(T::DbWeight::get().writes(4))
...@@ -85,17 +72,51 @@ impl<T: frame_system::Config> pallet_provide_randomness::WeightInfo for WeightIn ...@@ -85,17 +72,51 @@ impl<T: frame_system::Config> pallet_provide_randomness::WeightInfo for WeightIn
/// The range of component `i` is `[1, 100]`. /// The range of component `i` is `[1, 100]`.
fn on_initialize(i: u32, ) -> Weight { fn on_initialize(i: u32, ) -> Weight {
// Proof Size summary in bytes: // Proof Size summary in bytes:
// Measured: `375 + i * (59 ±0)` // Measured: `383 + i * (59 ±0)`
// Estimated: `1866 + i * (2535 ±0)` // Estimated: `1869 + i * (2535 ±0)`
// Minimum execution time: 155_829_000 picoseconds. // Minimum execution time: 12_923_000 picoseconds.
Weight::from_parts(250_060_279, 0) Weight::from_parts(11_245_683, 0)
.saturating_add(Weight::from_parts(0, 1866)) .saturating_add(Weight::from_parts(0, 1869))
// Standard Error: 1_564_408 // Standard Error: 5_906
.saturating_add(Weight::from_parts(28_734_558, 0).saturating_mul(i.into())) .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(4))
.saturating_add(T::DbWeight::get().reads((2_u64).saturating_mul(i.into()))) .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(3))
.saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(i.into()))) .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(i.into())))
.saturating_add(Weight::from_parts(0, 2535).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()))
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment