diff --git a/exercises/ex06-weights/weights/src/benchmarking.rs b/exercises/ex06-weights/weights/src/benchmarking.rs index 130bd0d..ef39c8d 100644 --- a/exercises/ex06-weights/weights/src/benchmarking.rs +++ b/exercises/ex06-weights/weights/src/benchmarking.rs @@ -10,44 +10,29 @@ use frame_system::RawOrigin; benchmarks! { /////////////////////// Part 2 - benchmarks /////////////////////// - //TODO: change this generic benchmark to benchmark the duplicate_and_store extrinsic - benchmark_name { - //this variable is a range, meaning the benchmark will be run with the different values of - //s, to evaluate the weight of this specific parameter - let s in 0 .. 1; - todo("change this range to something that makes sense for your benchmark"); - - let root = todo!("get the root origin, to sign our transactions"); - - - // Now that we have all the parameters we need for our extrinsic's benchmark, we can call - // it: - }: extrinsic_name(root, 0, s) + duplicate_and_store_benchmark { + let caller = RawOrigin::Signed(get_account::("caller")); + let s in 0 .. 10000; + }: duplicate_and_store(caller, 0, s) verify { - // Run some verifications here. - // If something isn't right, the benchmark will throw an error and wont output values - assert_eq!(1, 0); + assert!(VecDup::::get().unwrap().len() == s as usize); } /////////////////////// Part 3.A - conditional benchmarks /////////////////////// store_maybe_hashed_true { - //TODO: prepare the datas for this benchmark (the account, the data, and the hash) - let root = todo!("get the root origin, to sign our transactions"); - let data = todo!(); - let hash = todo!(); - }: store_maybe_hashed(root, data, hash) + let caller = RawOrigin::Signed(get_account::("caller")); + let data = vec![1; 100_000]; + let hash = true; + }: benchmarked_store_maybe_hashed(caller, data, hash) verify { - //TODO: do some verification that your extrinsic did what it was supposed to do } store_maybe_hashed_false { - //TODO: prepare the datas for this benchmark (the account, the data, and the hash) - let root = todo!("get the root origin, to sign our transactions"); - let data = todo!(); - let hash = todo!(); - }: store_maybe_hashed(root, data, hash) + let caller = RawOrigin::Signed(get_account::("caller")); + let data = vec![1; 100_000]; + let hash = false; + }: benchmarked_store_maybe_hashed(caller, data, hash) verify { - //TODO: do some verification that your extrinsic did what it was supposed to do } impl_benchmark_test_suite!(Weights, crate::mock::new_test_ext(), crate::mock::Test); diff --git a/exercises/ex06-weights/weights/src/lib.rs b/exercises/ex06-weights/weights/src/lib.rs index d3d7fc4..e3969e5 100644 --- a/exercises/ex06-weights/weights/src/lib.rs +++ b/exercises/ex06-weights/weights/src/lib.rs @@ -4,17 +4,17 @@ pub use pallet::*; pub use sp_core::hashing::blake2_256; +mod weights; +pub use weights::WeightInfo; + #[cfg(test)] mod mock; #[cfg(test)] mod tests; -// uncomment the following lines to include the benchmarking.rs file in the module tree, if the -// runtime-benchmarks feature is activated -// -// #[cfg(feature = "runtime-benchmarks")] -// mod benchmarking; +#[cfg(feature = "runtime-benchmarks")] +mod benchmarking; #[frame_support::pallet] pub mod pallet { @@ -25,7 +25,7 @@ pub mod pallet { #[pallet::config] pub trait Config: frame_system::Config { type Event: From> + IsType<::Event>; - type WeightInfo; + type WeightInfo: WeightInfo; } #[pallet::pallet] @@ -60,8 +60,7 @@ pub mod pallet { #[pallet::call] impl Pallet { /////////////////////// Part 1 - arbitrary weights /////////////////////// - //TODO give this exctrinsic an arbitrary weight ! - #[pallet::weight(0)] + #[pallet::weight(10_000 + T::DbWeight::get().reads(1))] pub fn verify_address(origin: OriginFor) -> DispatchResult { let who = ensure_signed(origin.clone())?; ensure_root(origin)?; @@ -79,8 +78,7 @@ pub mod pallet { } /////////////////////// Part 2 - benchmarks /////////////////////// - //TODO write a benchmark for this extrinsic in benchmarking.rs - #[pallet::weight(0)] + #[pallet::weight(T::WeightInfo::duplicate_and_store_benchmark(*count))] pub fn duplicate_and_store(origin: OriginFor, elem: u32, count: u32) -> DispatchResult { ensure_signed(origin)?; @@ -94,8 +92,11 @@ pub mod pallet { } /////////////////////// Part 3.A - conditional arbitrary weight /////////////////////// - //TODO give this extrinsic a weight of 100_000 if `hash` is true, or 10_000 otherwise - #[pallet::weight(0)] + #[pallet::weight(if *hash { + 100_000 + } else { + 10_000 + })] pub fn store_maybe_hashed( origin: OriginFor, data: Vec, @@ -114,10 +115,11 @@ pub mod pallet { } /////////////////////// Part 3.B - conditional benchmark /////////////////////// - //TODO write two benchmarks for this extrinsic in benchmarking.rs, and then choose the - //corresponding one depending on the value of `hash` - //hint: look at this pallet's weights macros: https://github.com/paritytech/substrate/blob/master/frame/utility/src/lib.rs - #[pallet::weight(0)] + #[pallet::weight(if *hash { + T::WeightInfo::store_maybe_hashed_true() + } else { + T::WeightInfo::store_maybe_hashed_false() + })] pub fn benchmarked_store_maybe_hashed( origin: OriginFor, data: Vec, diff --git a/exercises/ex06-weights/weights/src/mock.rs b/exercises/ex06-weights/weights/src/mock.rs index abfafcc..e1c4956 100644 --- a/exercises/ex06-weights/weights/src/mock.rs +++ b/exercises/ex06-weights/weights/src/mock.rs @@ -10,7 +10,6 @@ use sp_runtime::{ testing::Header, traits::{BlakeTwo256, IdentityLookup}, }; - type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; type Block = frame_system::mocking::MockBlock; diff --git a/exercises/ex06-weights/weights/src/weights.rs b/exercises/ex06-weights/weights/src/weights.rs new file mode 100644 index 0000000..d4442b5 --- /dev/null +++ b/exercises/ex06-weights/weights/src/weights.rs @@ -0,0 +1,100 @@ +// This file is part of Substrate. + +// Copyright (C) 2022 Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! Autogenerated weights for pallet_weights +//! +//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev +//! DATE: 2022-11-04, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! HOSTNAME: ``, CPU: `` +//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024 + +// Executed Command: +// ./target/release/node-template +// benchmark +// pallet +// --chain +// dev +// --execution=wasm +// --wasm-execution=compiled +// --pallet +// pallet_weights +// --extrinsic +// * +// --steps +// 50 +// --repeat +// 20 +// --template=./.maintain/frame-weight-template.hbs +// --output +// pallets/weights/src/weights.rs + +#![cfg_attr(rustfmt, rustfmt_skip)] +#![allow(unused_parens)] +#![allow(unused_imports)] + +use frame_benchmarking::sp_std::marker::PhantomData; +use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}}; + +/// Weight functions needed for pallet_weights. +pub trait WeightInfo { + fn duplicate_and_store_benchmark(s: u32, ) -> Weight; + fn store_maybe_hashed_true() -> Weight; + fn store_maybe_hashed_false() -> Weight; +} + +/// Weights for pallet_weights using the Substrate node and recommended hardware. +pub struct SubstrateWeight(PhantomData); +impl WeightInfo for SubstrateWeight { + // Storage: WeightsModule VecDup (r:0 w:1) + fn duplicate_and_store_benchmark(s: u32, ) -> Weight { + (1_466_000 as Weight) + // Standard Error: 0 + .saturating_add((3_000 as Weight).saturating_mul(s as Weight)) + .saturating_add(T::DbWeight::get().writes(1 as Weight)) + } + // Storage: WeightsModule Data (r:0 w:1) + fn store_maybe_hashed_true() -> Weight { + (115_844_000 as Weight) + .saturating_add(T::DbWeight::get().writes(1 as Weight)) + } + // Storage: WeightsModule Data (r:0 w:1) + fn store_maybe_hashed_false() -> Weight { + (41_443_000 as Weight) + .saturating_add(T::DbWeight::get().writes(1 as Weight)) + } +} + +// For backwards compatibility and tests +impl WeightInfo for () { + // Storage: WeightsModule VecDup (r:0 w:1) + fn duplicate_and_store_benchmark(s: u32, ) -> Weight { + (1_466_000 as Weight) + // Standard Error: 0 + .saturating_add((3_000 as Weight).saturating_mul(s as Weight)) + .saturating_add(RocksDbWeight::get().writes(1 as Weight)) + } + // Storage: WeightsModule Data (r:0 w:1) + fn store_maybe_hashed_true() -> Weight { + (115_844_000 as Weight) + .saturating_add(RocksDbWeight::get().writes(1 as Weight)) + } + // Storage: WeightsModule Data (r:0 w:1) + fn store_maybe_hashed_false() -> Weight { + (41_443_000 as Weight) + .saturating_add(RocksDbWeight::get().writes(1 as Weight)) + } +}