diff --git a/pallets/provide-randomness/src/lib.rs b/pallets/provide-randomness/src/lib.rs index 4985d5b42829907220e29724ce7bbfae04e5716a..bf851ebf11cf46062ccdc787ed066a323657d5ef 100644 --- a/pallets/provide-randomness/src/lib.rs +++ b/pallets/provide-randomness/src/lib.rs @@ -21,6 +21,7 @@ mod benchmarking; mod types; +pub mod weights; use frame_support::pallet_prelude::Weight; use sp_core::H256; @@ -28,6 +29,7 @@ use sp_std::prelude::*; pub use pallet::*; pub use types::*; +pub use weights::WeightInfo; pub type RequestId = u64; @@ -88,6 +90,8 @@ pub mod pallet { type RandomnessFromOneEpochAgo: Randomness<H256, Self::BlockNumber>; /// The overarching event type. type RuntimeEvent: From<Event> + IsType<<Self as frame_system::Config>::RuntimeEvent>; + /// Type representing the weight of this pallet + type WeightInfo: WeightInfo; } // STORAGE // @@ -143,7 +147,7 @@ pub mod pallet { #[pallet::call] impl<T: Config> Pallet<T> { /// Request a randomness - #[pallet::weight(500_000_000)] + #[pallet::weight(T::WeightInfo::request())] pub fn request( origin: OriginFor<T>, randomness_type: RandomnessType, @@ -168,7 +172,7 @@ pub mod pallet { #[pallet::hooks] impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> { fn on_initialize(_: T::BlockNumber) -> Weight { - let request_weight = Weight::from_ref_time(100_000); + let request_weight = T::WeightInfo::on_initialize(T::MaxRequests::get()); let mut total_weight = Weight::zero(); diff --git a/pallets/provide-randomness/src/weights.rs b/pallets/provide-randomness/src/weights.rs new file mode 100644 index 0000000000000000000000000000000000000000..7c2e0831860606114665a06e559b5da19f889800 --- /dev/null +++ b/pallets/provide-randomness/src/weights.rs @@ -0,0 +1,58 @@ +// 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 on_initialize(i: u32) -> Weight; + fn request() -> Weight; +} + +// Insecure weights implementation, use it for tests only! +impl WeightInfo for () { + // Storage: ProvideRandomness CounterForRequestsIds (r:1 w:1) + // Storage: ProvideRandomness RequestIdProvider (r:1 w:1) + // Storage: ProvideRandomness RequestsIds (r:1 w:1) + // Storage: Babe EpochIndex (r:1 w:0) + // Storage: ProvideRandomness NexEpochHookIn (r:1 w:0) + // Storage: ProvideRandomness RequestsReadyAtEpoch (r:1 w:1) + fn request() -> Weight { + // Minimum execution time: 321_822 nanoseconds. + Weight::from_ref_time(338_919_000 as u64) + .saturating_add(RocksDbWeight::get().reads(6 as u64)) + .saturating_add(RocksDbWeight::get().writes(4 as u64)) + } + // Storage: ProvideRandomness RequestsReadyAtNextBlock (r:1 w:1) + // Storage: Babe AuthorVrfRandomness (r:1 w:0) + // Storage: ProvideRandomness RequestsIds (r:1 w:1) + // Storage: ProvideRandomness CounterForRequestsIds (r:1 w:1) + // Storage: Account PendingRandomIdAssignments (r:1 w:0) + // Storage: ProvideRandomness NexEpochHookIn (r:1 w:1) + /// The range of component `i` is `[1, 100]`. + fn on_initialize(i: u32) -> Weight { + // Minimum execution time: 175_645 nanoseconds. + Weight::from_ref_time(461_442_906 as u64) + // Standard Error: 1_523_561 + .saturating_add(Weight::from_ref_time(43_315_015 as u64).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))) + } +}