Skip to content

Commit 5cbc619

Browse files
committed
feat: add solution for ex06
1 parent 29a4e35 commit 5cbc619

File tree

4 files changed

+131
-45
lines changed

4 files changed

+131
-45
lines changed

exercises/ex06-weights/weights/src/benchmarking.rs

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,44 +10,29 @@ use frame_system::RawOrigin;
1010
benchmarks! {
1111
/////////////////////// Part 2 - benchmarks ///////////////////////
1212

13-
//TODO: change this generic benchmark to benchmark the duplicate_and_store extrinsic
14-
benchmark_name {
15-
//this variable is a range, meaning the benchmark will be run with the different values of
16-
//s, to evaluate the weight of this specific parameter
17-
let s in 0 .. 1;
18-
todo("change this range to something that makes sense for your benchmark");
19-
20-
let root = todo!("get the root origin, to sign our transactions");
21-
22-
23-
// Now that we have all the parameters we need for our extrinsic's benchmark, we can call
24-
// it:
25-
}: extrinsic_name(root, 0, s)
13+
duplicate_and_store_benchmark {
14+
let caller = RawOrigin::Signed(get_account::<T>("caller"));
15+
let s in 0 .. 10000;
16+
}: duplicate_and_store(caller, 0, s)
2617
verify {
27-
// Run some verifications here.
28-
// If something isn't right, the benchmark will throw an error and wont output values
29-
assert_eq!(1, 0);
18+
assert!(VecDup::<T>::get().unwrap().len() == s as usize);
3019
}
3120

3221
/////////////////////// Part 3.A - conditional benchmarks ///////////////////////
3322
store_maybe_hashed_true {
34-
//TODO: prepare the datas for this benchmark (the account, the data, and the hash)
35-
let root = todo!("get the root origin, to sign our transactions");
36-
let data = todo!();
37-
let hash = todo!();
38-
}: store_maybe_hashed(root, data, hash)
23+
let caller = RawOrigin::Signed(get_account::<T>("caller"));
24+
let data = vec![1; 100_000];
25+
let hash = true;
26+
}: benchmarked_store_maybe_hashed(caller, data, hash)
3927
verify {
40-
//TODO: do some verification that your extrinsic did what it was supposed to do
4128
}
4229

4330
store_maybe_hashed_false {
44-
//TODO: prepare the datas for this benchmark (the account, the data, and the hash)
45-
let root = todo!("get the root origin, to sign our transactions");
46-
let data = todo!();
47-
let hash = todo!();
48-
}: store_maybe_hashed(root, data, hash)
31+
let caller = RawOrigin::Signed(get_account::<T>("caller"));
32+
let data = vec![1; 100_000];
33+
let hash = false;
34+
}: benchmarked_store_maybe_hashed(caller, data, hash)
4935
verify {
50-
//TODO: do some verification that your extrinsic did what it was supposed to do
5136
}
5237

5338
impl_benchmark_test_suite!(Weights, crate::mock::new_test_ext(), crate::mock::Test);

exercises/ex06-weights/weights/src/lib.rs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ pub use pallet::*;
44

55
pub use sp_core::hashing::blake2_256;
66

7+
mod weights;
8+
pub use weights::WeightInfo;
9+
710
#[cfg(test)]
811
mod mock;
912

1013
#[cfg(test)]
1114
mod tests;
1215

13-
// uncomment the following lines to include the benchmarking.rs file in the module tree, if the
14-
// runtime-benchmarks feature is activated
15-
//
16-
// #[cfg(feature = "runtime-benchmarks")]
17-
// mod benchmarking;
16+
#[cfg(feature = "runtime-benchmarks")]
17+
mod benchmarking;
1818

1919
#[frame_support::pallet]
2020
pub mod pallet {
@@ -25,7 +25,7 @@ pub mod pallet {
2525
#[pallet::config]
2626
pub trait Config: frame_system::Config {
2727
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
28-
type WeightInfo;
28+
type WeightInfo: WeightInfo;
2929
}
3030

3131
#[pallet::pallet]
@@ -60,8 +60,7 @@ pub mod pallet {
6060
#[pallet::call]
6161
impl<T: Config> Pallet<T> {
6262
/////////////////////// Part 1 - arbitrary weights ///////////////////////
63-
//TODO give this exctrinsic an arbitrary weight !
64-
#[pallet::weight(0)]
63+
#[pallet::weight(10_000 + T::DbWeight::get().reads(1))]
6564
pub fn verify_address(origin: OriginFor<T>) -> DispatchResult {
6665
let who = ensure_signed(origin.clone())?;
6766
ensure_root(origin)?;
@@ -79,8 +78,7 @@ pub mod pallet {
7978
}
8079

8180
/////////////////////// Part 2 - benchmarks ///////////////////////
82-
//TODO write a benchmark for this extrinsic in benchmarking.rs
83-
#[pallet::weight(0)]
81+
#[pallet::weight(T::WeightInfo::duplicate_and_store_benchmark(*count))]
8482
pub fn duplicate_and_store(origin: OriginFor<T>, elem: u32, count: u32) -> DispatchResult {
8583
ensure_signed(origin)?;
8684

@@ -94,8 +92,11 @@ pub mod pallet {
9492
}
9593

9694
/////////////////////// Part 3.A - conditional arbitrary weight ///////////////////////
97-
//TODO give this extrinsic a weight of 100_000 if `hash` is true, or 10_000 otherwise
98-
#[pallet::weight(0)]
95+
#[pallet::weight(if *hash {
96+
100_000
97+
} else {
98+
10_000
99+
})]
99100
pub fn store_maybe_hashed(
100101
origin: OriginFor<T>,
101102
data: Vec<u8>,
@@ -114,10 +115,11 @@ pub mod pallet {
114115
}
115116

116117
/////////////////////// Part 3.B - conditional benchmark ///////////////////////
117-
//TODO write two benchmarks for this extrinsic in benchmarking.rs, and then choose the
118-
//corresponding one depending on the value of `hash`
119-
//hint: look at this pallet's weights macros: https://github.com/paritytech/substrate/blob/master/frame/utility/src/lib.rs
120-
#[pallet::weight(0)]
118+
#[pallet::weight(if *hash {
119+
T::WeightInfo::store_maybe_hashed_true()
120+
} else {
121+
T::WeightInfo::store_maybe_hashed_false()
122+
})]
121123
pub fn benchmarked_store_maybe_hashed(
122124
origin: OriginFor<T>,
123125
data: Vec<u8>,

exercises/ex06-weights/weights/src/mock.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use sp_runtime::{
1010
testing::Header,
1111
traits::{BlakeTwo256, IdentityLookup},
1212
};
13-
1413
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
1514
type Block = frame_system::mocking::MockBlock<Test>;
1615

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// This file is part of Substrate.
2+
3+
// Copyright (C) 2022 Parity Technologies (UK) Ltd.
4+
// SPDX-License-Identifier: Apache-2.0
5+
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
18+
//! Autogenerated weights for pallet_weights
19+
//!
20+
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
21+
//! DATE: 2022-11-04, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]`
22+
//! HOSTNAME: ``, CPU: ``
23+
//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024
24+
25+
// Executed Command:
26+
// ./target/release/node-template
27+
// benchmark
28+
// pallet
29+
// --chain
30+
// dev
31+
// --execution=wasm
32+
// --wasm-execution=compiled
33+
// --pallet
34+
// pallet_weights
35+
// --extrinsic
36+
// *
37+
// --steps
38+
// 50
39+
// --repeat
40+
// 20
41+
// --template=./.maintain/frame-weight-template.hbs
42+
// --output
43+
// pallets/weights/src/weights.rs
44+
45+
#![cfg_attr(rustfmt, rustfmt_skip)]
46+
#![allow(unused_parens)]
47+
#![allow(unused_imports)]
48+
49+
use frame_benchmarking::sp_std::marker::PhantomData;
50+
use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}};
51+
52+
/// Weight functions needed for pallet_weights.
53+
pub trait WeightInfo {
54+
fn duplicate_and_store_benchmark(s: u32, ) -> Weight;
55+
fn store_maybe_hashed_true() -> Weight;
56+
fn store_maybe_hashed_false() -> Weight;
57+
}
58+
59+
/// Weights for pallet_weights using the Substrate node and recommended hardware.
60+
pub struct SubstrateWeight<T>(PhantomData<T>);
61+
impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
62+
// Storage: WeightsModule VecDup (r:0 w:1)
63+
fn duplicate_and_store_benchmark(s: u32, ) -> Weight {
64+
(1_466_000 as Weight)
65+
// Standard Error: 0
66+
.saturating_add((3_000 as Weight).saturating_mul(s as Weight))
67+
.saturating_add(T::DbWeight::get().writes(1 as Weight))
68+
}
69+
// Storage: WeightsModule Data (r:0 w:1)
70+
fn store_maybe_hashed_true() -> Weight {
71+
(115_844_000 as Weight)
72+
.saturating_add(T::DbWeight::get().writes(1 as Weight))
73+
}
74+
// Storage: WeightsModule Data (r:0 w:1)
75+
fn store_maybe_hashed_false() -> Weight {
76+
(41_443_000 as Weight)
77+
.saturating_add(T::DbWeight::get().writes(1 as Weight))
78+
}
79+
}
80+
81+
// For backwards compatibility and tests
82+
impl WeightInfo for () {
83+
// Storage: WeightsModule VecDup (r:0 w:1)
84+
fn duplicate_and_store_benchmark(s: u32, ) -> Weight {
85+
(1_466_000 as Weight)
86+
// Standard Error: 0
87+
.saturating_add((3_000 as Weight).saturating_mul(s as Weight))
88+
.saturating_add(RocksDbWeight::get().writes(1 as Weight))
89+
}
90+
// Storage: WeightsModule Data (r:0 w:1)
91+
fn store_maybe_hashed_true() -> Weight {
92+
(115_844_000 as Weight)
93+
.saturating_add(RocksDbWeight::get().writes(1 as Weight))
94+
}
95+
// Storage: WeightsModule Data (r:0 w:1)
96+
fn store_maybe_hashed_false() -> Weight {
97+
(41_443_000 as Weight)
98+
.saturating_add(RocksDbWeight::get().writes(1 as Weight))
99+
}
100+
}

0 commit comments

Comments
 (0)