Skip to content

Commit a60ecc9

Browse files
kianenigmagupnikcommand-bot
authored andcommitted
add pallet macro kitchensink example/template (paritytech#14052)
* 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 <[email protected]> * Update frame/examples/kitchensink/src/lib.rs Co-authored-by: Kian Paimani <[email protected]> * Update frame/examples/kitchensink/src/lib.rs Co-authored-by: Kian Paimani <[email protected]> * Addresses review comments * Addresses review comments * ".git/.scripts/commands/fmt/fmt.sh" --------- Co-authored-by: Nikhil Gupta <[email protected]> Co-authored-by: command-bot <> Co-authored-by: command-bot <[email protected]>
1 parent 6006715 commit a60ecc9

File tree

10 files changed

+725
-0
lines changed

10 files changed

+725
-0
lines changed

Cargo.lock

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,10 @@ members = [
106106
"frame/election-provider-support/benchmarking",
107107
"frame/election-provider-support/solution-type",
108108
"frame/election-provider-support/solution-type/fuzzer",
109+
"frame/examples",
109110
"frame/examples/basic",
110111
"frame/examples/offchain-worker",
112+
"frame/examples/kitchensink",
111113
"frame/examples/dev-mode",
112114
"frame/examples/default-config",
113115
"frame/executive",

frame/examples/Cargo.toml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
[package]
2+
name = "pallet-examples"
3+
version = "4.0.0-dev"
4+
authors = ["Parity Technologies <[email protected]>"]
5+
edition = "2021"
6+
license = "Apache-2.0"
7+
homepage = "https://substrate.io"
8+
repository = "https://github.com/paritytech/substrate/"
9+
description = "The single package with various examples for frame pallets"
10+
11+
[package.metadata.docs.rs]
12+
targets = ["x86_64-unknown-linux-gnu"]
13+
14+
[dependencies]
15+
pallet-example-basic = { default-features = false, path = "./basic" }
16+
pallet-default-config-example = { default-features = false, path = "./default-config" }
17+
pallet-example-offchain-worker = { default-features = false, path = "./offchain-worker" }
18+
pallet-example-kitchensink = { default-features = false, path = "./kitchensink" }
19+
pallet-dev-mode = { default-features = false, path = "./dev-mode" }
20+
21+
[features]
22+
default = [ "std" ]
23+
std = [
24+
"pallet-example-basic/std",
25+
"pallet-default-config-example/std",
26+
"pallet-example-offchain-worker/std",
27+
"pallet-example-kitchensink/std",
28+
"pallet-dev-mode/std",
29+
]
30+
try-runtime = [
31+
"pallet-example-basic/try-runtime",
32+
"pallet-default-config-example/try-runtime",
33+
"pallet-example-offchain-worker/try-runtime",
34+
"pallet-example-kitchensink/try-runtime",
35+
"pallet-dev-mode/try-runtime",
36+
]

frame/examples/kitchensink/Cargo.toml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
[package]
2+
name = "pallet-example-kitchensink"
3+
version = "4.0.0-dev"
4+
authors = ["Parity Technologies <[email protected]>"]
5+
edition = "2021"
6+
license = "MIT-0"
7+
homepage = "https://substrate.io"
8+
repository = "https://github.com/paritytech/substrate/"
9+
description = "FRAME example kitchensink pallet"
10+
readme = "README.md"
11+
12+
[package.metadata.docs.rs]
13+
targets = ["x86_64-unknown-linux-gnu"]
14+
15+
[dependencies]
16+
codec = { package = "parity-scale-codec", version = "3.2.2", default-features = false }
17+
log = { version = "0.4.17", default-features = false }
18+
scale-info = { version = "2.5.0", default-features = false, features = ["derive"] }
19+
20+
frame-support = { version = "4.0.0-dev", default-features = false, path = "../../support" }
21+
frame-system = { version = "4.0.0-dev", default-features = false, path = "../../system" }
22+
23+
sp-io = { version = "23.0.0", default-features = false, path = "../../../primitives/io" }
24+
sp-runtime = { version = "24.0.0", default-features = false, path = "../../../primitives/runtime" }
25+
sp-std = { version = "8.0.0", default-features = false, path = "../../../primitives/std" }
26+
27+
frame-benchmarking = { version = "4.0.0-dev", default-features = false, optional = true, path = "../../benchmarking" }
28+
29+
pallet-balances = { version = "4.0.0-dev", default-features = false, path = "../../balances" }
30+
31+
[dev-dependencies]
32+
sp-core = { version = "21.0.0", default-features = false, path = "../../../primitives/core" }
33+
34+
[features]
35+
default = ["std"]
36+
std = [
37+
"codec/std",
38+
"log/std",
39+
"scale-info/std",
40+
41+
"frame-support/std",
42+
"frame-system/std",
43+
44+
"sp-io/std",
45+
"sp-runtime/std",
46+
"sp-std/std",
47+
48+
"frame-benchmarking?/std",
49+
50+
"pallet-balances/std",
51+
]
52+
runtime-benchmarks = ["frame-benchmarking/runtime-benchmarks"]
53+
try-runtime = ["frame-support/try-runtime"]
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// This file is part of Substrate.
2+
3+
// Copyright (C) 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+
//! Benchmarking for `pallet-example-kitchensink`.
19+
20+
// Only enable this module for benchmarking.
21+
#![cfg(feature = "runtime-benchmarks")]
22+
use super::*;
23+
24+
#[allow(unused)]
25+
use crate::Pallet as Kitchensink;
26+
27+
use frame_benchmarking::v2::*;
28+
use frame_system::RawOrigin;
29+
30+
// To actually run this benchmark on pallet-example-kitchensink, we need to put this pallet into the
31+
// runtime and compile it with `runtime-benchmarks` feature. The detail procedures are
32+
// documented at:
33+
// https://docs.substrate.io/reference/how-to-guides/weights/add-benchmarks/
34+
//
35+
// The auto-generated weight estimate of this pallet is copied over to the `weights.rs` file.
36+
// The exact command of how the estimate generated is printed at the top of the file.
37+
38+
// Details on using the benchmarks macro can be seen at:
39+
// https://paritytech.github.io/substrate/master/frame_benchmarking/trait.Benchmarking.html#tymethod.benchmarks
40+
#[benchmarks]
41+
mod benchmarks {
42+
use super::*;
43+
44+
// This will measure the execution time of `set_foo`.
45+
#[benchmark]
46+
fn set_foo_benchmark() {
47+
// This is the benchmark setup phase.
48+
// `set_foo` is a constant time function, hence we hard-code some random value here.
49+
let value = 1000u32.into();
50+
#[extrinsic_call]
51+
set_foo(RawOrigin::Root, value, 10u128); // The execution phase is just running `set_foo` extrinsic call
52+
53+
// This is the optional benchmark verification phase, asserting certain states.
54+
assert_eq!(Pallet::<T>::foo(), Some(value))
55+
}
56+
57+
// This line generates test cases for benchmarking, and could be run by:
58+
// `cargo test -p pallet-example-kitchensink --all-features`, you will see one line per case:
59+
// `test benchmarking::bench_sort_vector ... ok`
60+
// `test benchmarking::bench_accumulate_dummy ... ok`
61+
// `test benchmarking::bench_set_dummy_benchmark ... ok` in the result.
62+
//
63+
// The line generates three steps per benchmark, with repeat=1 and the three steps are
64+
// [low, mid, high] of the range.
65+
impl_benchmark_test_suite!(Kitchensink, crate::tests::new_test_ext(), crate::tests::Test);
66+
}

0 commit comments

Comments
 (0)