Skip to content

Commit

Permalink
feat: add solution for ex06
Browse files Browse the repository at this point in the history
  • Loading branch information
tdelabro committed Dec 27, 2022
1 parent 29a4e35 commit 5cbc619
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 45 deletions.
41 changes: 13 additions & 28 deletions exercises/ex06-weights/weights/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<T>("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::<T>::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::<T>("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::<T>("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);
Expand Down
34 changes: 18 additions & 16 deletions exercises/ex06-weights/weights/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -25,7 +25,7 @@ pub mod pallet {
#[pallet::config]
pub trait Config: frame_system::Config {
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
type WeightInfo;
type WeightInfo: WeightInfo;
}

#[pallet::pallet]
Expand Down Expand Up @@ -60,8 +60,7 @@ pub mod pallet {
#[pallet::call]
impl<T: Config> Pallet<T> {
/////////////////////// 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<T>) -> DispatchResult {
let who = ensure_signed(origin.clone())?;
ensure_root(origin)?;
Expand All @@ -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<T>, elem: u32, count: u32) -> DispatchResult {
ensure_signed(origin)?;

Expand All @@ -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<T>,
data: Vec<u8>,
Expand All @@ -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<T>,
data: Vec<u8>,
Expand Down
1 change: 0 additions & 1 deletion exercises/ex06-weights/weights/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use sp_runtime::{
testing::Header,
traits::{BlakeTwo256, IdentityLookup},
};

type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
type Block = frame_system::mocking::MockBlock<Test>;

Expand Down
100 changes: 100 additions & 0 deletions exercises/ex06-weights/weights/src/weights.rs
Original file line number Diff line number Diff line change
@@ -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<T>(PhantomData<T>);
impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// 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))
}
}

0 comments on commit 5cbc619

Please sign in to comment.