Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
add pallet macro kitchensink example/template (#14052)
Browse files Browse the repository at this point in the history
* add pallet macro kitchen-sink pallet

* update

* Adds benchmarking setup

* Updates APIs

* Fixes benchmark

* Uses derive_impl for frame_system

* Adds benchmarks

* Minor update

* Adds license

* Adds examples crate

* ".git/.scripts/commands/fmt/fmt.sh"

* Update frame/examples/kitchensink/src/tests.rs

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* Update frame/examples/kitchensink/src/lib.rs

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* Update frame/examples/kitchensink/src/lib.rs

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* Addresses review comments

* Addresses review comments

* ".git/.scripts/commands/fmt/fmt.sh"

---------

Co-authored-by: Nikhil Gupta <17176722+gupnik@users.noreply.github.com>
Co-authored-by: command-bot <>
Co-authored-by: command-bot <ci@gitlab.parity.io>
  • Loading branch information
3 people authored and agryaznov committed Jun 9, 2023
1 parent d7b3421 commit 72352a0
Show file tree
Hide file tree
Showing 10 changed files with 725 additions and 0 deletions.
28 changes: 28 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,10 @@ members = [
"frame/election-provider-support/benchmarking",
"frame/election-provider-support/solution-type",
"frame/election-provider-support/solution-type/fuzzer",
"frame/examples",
"frame/examples/basic",
"frame/examples/offchain-worker",
"frame/examples/kitchensink",
"frame/examples/dev-mode",
"frame/examples/default-config",
"frame/executive",
Expand Down
36 changes: 36 additions & 0 deletions frame/examples/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
[package]
name = "pallet-examples"
version = "4.0.0-dev"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2021"
license = "Apache-2.0"
homepage = "https://substrate.io"
repository = "https://github.com/paritytech/substrate/"
description = "The single package with various examples for frame pallets"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
pallet-example-basic = { default-features = false, path = "./basic" }
pallet-default-config-example = { default-features = false, path = "./default-config" }
pallet-example-offchain-worker = { default-features = false, path = "./offchain-worker" }
pallet-example-kitchensink = { default-features = false, path = "./kitchensink" }
pallet-dev-mode = { default-features = false, path = "./dev-mode" }

[features]
default = [ "std" ]
std = [
"pallet-example-basic/std",
"pallet-default-config-example/std",
"pallet-example-offchain-worker/std",
"pallet-example-kitchensink/std",
"pallet-dev-mode/std",
]
try-runtime = [
"pallet-example-basic/try-runtime",
"pallet-default-config-example/try-runtime",
"pallet-example-offchain-worker/try-runtime",
"pallet-example-kitchensink/try-runtime",
"pallet-dev-mode/try-runtime",
]
53 changes: 53 additions & 0 deletions frame/examples/kitchensink/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
[package]
name = "pallet-example-kitchensink"
version = "4.0.0-dev"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2021"
license = "MIT-0"
homepage = "https://substrate.io"
repository = "https://github.com/paritytech/substrate/"
description = "FRAME example kitchensink pallet"
readme = "README.md"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { package = "parity-scale-codec", version = "3.2.2", default-features = false }
log = { version = "0.4.17", default-features = false }
scale-info = { version = "2.5.0", default-features = false, features = ["derive"] }

frame-support = { version = "4.0.0-dev", default-features = false, path = "../../support" }
frame-system = { version = "4.0.0-dev", default-features = false, path = "../../system" }

sp-io = { version = "23.0.0", default-features = false, path = "../../../primitives/io" }
sp-runtime = { version = "24.0.0", default-features = false, path = "../../../primitives/runtime" }
sp-std = { version = "8.0.0", default-features = false, path = "../../../primitives/std" }

frame-benchmarking = { version = "4.0.0-dev", default-features = false, optional = true, path = "../../benchmarking" }

pallet-balances = { version = "4.0.0-dev", default-features = false, path = "../../balances" }

[dev-dependencies]
sp-core = { version = "21.0.0", default-features = false, path = "../../../primitives/core" }

[features]
default = ["std"]
std = [
"codec/std",
"log/std",
"scale-info/std",

"frame-support/std",
"frame-system/std",

"sp-io/std",
"sp-runtime/std",
"sp-std/std",

"frame-benchmarking?/std",

"pallet-balances/std",
]
runtime-benchmarks = ["frame-benchmarking/runtime-benchmarks"]
try-runtime = ["frame-support/try-runtime"]
66 changes: 66 additions & 0 deletions frame/examples/kitchensink/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// This file is part of Substrate.

// Copyright (C) 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.

//! Benchmarking for `pallet-example-kitchensink`.
// Only enable this module for benchmarking.
#![cfg(feature = "runtime-benchmarks")]
use super::*;

#[allow(unused)]
use crate::Pallet as Kitchensink;

use frame_benchmarking::v2::*;
use frame_system::RawOrigin;

// To actually run this benchmark on pallet-example-kitchensink, we need to put this pallet into the
// runtime and compile it with `runtime-benchmarks` feature. The detail procedures are
// documented at:
// https://docs.substrate.io/reference/how-to-guides/weights/add-benchmarks/
//
// The auto-generated weight estimate of this pallet is copied over to the `weights.rs` file.
// The exact command of how the estimate generated is printed at the top of the file.

// Details on using the benchmarks macro can be seen at:
// https://paritytech.github.io/substrate/master/frame_benchmarking/trait.Benchmarking.html#tymethod.benchmarks
#[benchmarks]
mod benchmarks {
use super::*;

// This will measure the execution time of `set_foo`.
#[benchmark]
fn set_foo_benchmark() {
// This is the benchmark setup phase.
// `set_foo` is a constant time function, hence we hard-code some random value here.
let value = 1000u32.into();
#[extrinsic_call]
set_foo(RawOrigin::Root, value, 10u128); // The execution phase is just running `set_foo` extrinsic call

// This is the optional benchmark verification phase, asserting certain states.
assert_eq!(Pallet::<T>::foo(), Some(value))
}

// This line generates test cases for benchmarking, and could be run by:
// `cargo test -p pallet-example-kitchensink --all-features`, you will see one line per case:
// `test benchmarking::bench_sort_vector ... ok`
// `test benchmarking::bench_accumulate_dummy ... ok`
// `test benchmarking::bench_set_dummy_benchmark ... ok` in the result.
//
// The line generates three steps per benchmark, with repeat=1 and the three steps are
// [low, mid, high] of the range.
impl_benchmark_test_suite!(Kitchensink, crate::tests::new_test_ext(), crate::tests::Test);
}
Loading

0 comments on commit 72352a0

Please sign in to comment.