From eb1ec3600fe7640dd3f16979348b7bb3d6b63a81 Mon Sep 17 00:00:00 2001 From: kianenigma Date: Mon, 1 May 2023 11:57:44 +0200 Subject: [PATCH 01/17] add pallet macro kitchen-sink pallet --- Cargo.lock | 16 ++ Cargo.toml | 1 + frame/examples/kitchensink/Cargo.toml | 49 +++++ frame/examples/kitchensink/src/lib.rs | 281 ++++++++++++++++++++++++++ frame/support/src/lib.rs | 1 + 5 files changed, 348 insertions(+) create mode 100644 frame/examples/kitchensink/Cargo.toml create mode 100644 frame/examples/kitchensink/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index aa7d22daccb80..d60fc728149f8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6110,6 +6110,22 @@ dependencies = [ "sp-std", ] +[[package]] +name = "pallet-example-kitchensink" +version = "4.0.0-dev" +dependencies = [ + "frame-benchmarking", + "frame-support", + "frame-system", + "log", + "parity-scale-codec", + "scale-info", + "sp-core", + "sp-io", + "sp-runtime", + "sp-std", +] + [[package]] name = "pallet-example-offchain-worker" version = "4.0.0-dev" diff --git a/Cargo.toml b/Cargo.toml index 82e2264a3a2c6..4ddf2dd35b705 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -106,6 +106,7 @@ members = [ "frame/election-provider-support/solution-type/fuzzer", "frame/examples/basic", "frame/examples/offchain-worker", + "frame/examples/kitchensink", "frame/examples/dev-mode", "frame/executive", "frame/nis", diff --git a/frame/examples/kitchensink/Cargo.toml b/frame/examples/kitchensink/Cargo.toml new file mode 100644 index 0000000000000..9b0941536f0bc --- /dev/null +++ b/frame/examples/kitchensink/Cargo.toml @@ -0,0 +1,49 @@ +[package] +name = "pallet-example-kitchensink" +version = "4.0.0-dev" +authors = ["Parity Technologies "] +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 = "7.0.0", default-features = false, path = "../../../primitives/io" } +sp-runtime = { version = "7.0.0", default-features = false, path = "../../../primitives/runtime" } +sp-std = { version = "5.0.0", default-features = false, path = "../../../primitives/std" } + +frame-benchmarking = { version = "4.0.0-dev", default-features = false, optional = true, path = "../../benchmarking" } + +[dev-dependencies] +sp-core = { version = "7.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", +] +runtime-benchmarks = ["frame-benchmarking/runtime-benchmarks"] +try-runtime = ["frame-support/try-runtime"] diff --git a/frame/examples/kitchensink/src/lib.rs b/frame/examples/kitchensink/src/lib.rs new file mode 100644 index 0000000000000..e8b07821fe375 --- /dev/null +++ b/frame/examples/kitchensink/src/lib.rs @@ -0,0 +1,281 @@ +/// The kitchen-sink catalog of the the FRAME macros. +/// +/// This example does not focus on pallet instancing, and does nto include any where clauses on `T`. +/// +/// This has all the possibly functionalities that a pallet can have in the normal mode (except +/// `dev_mode`). +#[frame_support::pallet] +pub mod pallet { + use super::*; + use frame_support::pallet_prelude::*; + use frame_system::pallet_prelude::*; + + /// The config trait of the pallet. You can basically do anything with the config trait that you + /// can do with a normal rust trait: import items consisting of types, constants and functions. + /// + /// A very common pattern is for a pallet to import implementations of traits such as + /// [`frame_support::traits::Currency`], [`frame_support::traits::fungibles::Inspect`] and + /// [`frame_support::traits::Get`]. These are all types that the pallet is delegating to the top + /// level runtime to provide to it. + /// + /// The `FRAME`-specific functionality are: + /// + /// * the use of `#[pallet::constant]`, which places a `Get` implementation in the metadata. + /// * `type RuntimeEvent`, which is mandatory if your pallet has events. See TODO. + /// * Needless to say, because `Config` is bounded by `frame_system::Config`, you can use all + /// the items from `frame_system::Config` as well, such as `AccountId`. + /// * `#[pallet::disable_frame_system_supertrait_check]` would remove the need for + /// `frame_system::Config` to exist, which you should almost never need. + #[pallet::config] + pub trait Config: frame_system::Config { + /// The overarching runtime event type. + type RuntimeEvent: From> + IsType<::RuntimeEvent>; + + /// This is a normal Rust type, nothing specific to FRAME here. + type Currency: frame_support::traits::tokens::fungible::Inspect; + + /// Similarly, let the runtime decide this. + fn some_function() -> u32; + + /// And this + const FOO: u32; + + /// This is a FRAME-specific item. It will be placed in the metadata of the pallet, and + /// therefore can be queried by offchain applications. + #[pallet::constant] + type InMetadata: Get; + } + + #[pallet::extra_constants] + impl Pallet { + #[allow(non_snake_case)] + fn SomeValue() -> u32 { + unimplemented!() + } + + #[pallet::constant_name(OtherValue)] + fn arbitrary_name() -> u32 { + unimplemented!() + } + } + + const STORAGE_VERSION: frame_support::traits::StorageVersion = StorageVersion::new(1); + + /// The pallet struct. There's nothing special to FRAME about this; it can implement functions + /// in an impl blocks, traits and so on. + #[pallet::pallet] + // #[pallet::generate_storage_info] // TODO: should be removed. + #[pallet::without_storage_info] + #[pallet::storage_version(STORAGE_VERSION)] + pub struct Pallet(_); + + #[pallet::origin] + pub type Origin = frame_system::RawOrigin<::AccountId>; + + // first, we showcase all the possible storage types, with most of their details. + + /// A storage value. We mark this as unbounded, alter its prefix, and define a custom storage + /// getter for it. + /// + /// The value is stored a single trie node, and therefore can be retrieved with a single + /// database access. + #[pallet::storage] + #[pallet::unbounded] // optional + #[pallet::storage_prefix = "OtherFoo"] // optional + #[pallet::getter(fn foo)] // optional + pub type Foo = StorageValue; + + #[pallet::type_value] + pub fn DefaultForFoo() -> u32 { + 1 + } + + #[pallet::storage] + pub type FooWithDefault = + StorageValue; + + /// A storage map. This creates a mapping from keys of type `u32` to values of type `u32`. + /// + /// Keys and values can be iterated, albeit each value is stored under a unique trie key, + /// meaning that an iteration consists of many database accesses. + #[pallet::storage] + pub type Bar = StorageMap; + + /// Conceptually same as `StorageMap<>` where the key is a tuple of `(u32, u32)`. On top, it + /// provides some functions to iterate or remove items based on only the first key. + #[pallet::storage] + pub type Qux = StorageDoubleMap< + Hasher1 = Blake2_128Concat, + Key1 = u32, + Hasher2 = Blake2_128Concat, + Key2 = u32, + Value = u32, + >; + + /// Same as `StorageDoubleMap`, but with arbitrary number of keys. + #[pallet::storage] + pub type Quux = StorageNMap< + Key = ( + NMapKey, + NMapKey, + NMapKey, + ), + Value = u64, + >; + + /// In all of these examples, we chose a syntax where the storage item is defined using the + /// explicit generic syntax (`X = Y`). Alternatively: + #[pallet::storage] + pub type AlternativeSyntax = StorageMap<_, Blake2_128Concat, u32, u32>; + + /// Lastly, all storage items, as you saw, had to be generic over `T`. If they want to use an + /// item from `Config`, `` should be used. + #[pallet::storage] + pub type AlternativeSyntax2 = StorageMap<_, Blake2_128Concat, T::AccountId, u32>; + + /// The genesis config type. This allows the pallet to define how it should initialized upon + /// genesis. + /// + /// It can be generic over `T` or not, depending on whether it is or not. + #[pallet::genesis_config] + pub struct GenesisConfig { + pub foo: u32, + pub bar: T::BlockNumber, + } + + #[cfg(feature = "std")] + impl Default for GenesisConfig { + fn default() -> Self { + unimplemented!() + } + } + + #[pallet::genesis_build] + impl GenesisBuild for GenesisConfig { + fn build(&self) { + unimplemented!() + } + } + + #[pallet::call] + impl Pallet { + #[pallet::call_index(0)] + #[pallet::weight(0)] + pub fn set_foo( + _: OriginFor, + _new_foo: u32, + #[pallet::compact] _other_compact: u128, + ) -> DispatchResult { + unimplemented!() + } + } + + /// The event type. This exactly like a normal Rust enum. + /// + /// It can or cannot be generic over ``. Note that unlike a normal enum, if none of + /// the variants actually use ``, the macro will generate a hidden `PhantomData` + /// variant. + /// + /// The `generate_deposit` macro generates a function on `Pallet` called `deposit_event` which + /// will properly convert the error type of your pallet into `RuntimeEvent` (recall `type + /// RuntimeEvent: From>`, so it can be converted) and deposit it via + /// `frame_system::Pallet::deposit_event`. + #[pallet::event] + #[pallet::generate_deposit(pub fn deposit_event)] + pub enum Event { + /// A simple tuple style variant. + SomethingHappened(u32), + /// A simple struct-style variant. Note that we use `AccountId` from `T` because `T: + /// Config`, which by extension implies `T: frame_system::Config`. + SomethingDetailedHappened { + at: u32, + to: T::AccountId, + }, + // /// Another variant. + SomeoneJoined(T::AccountId), + } + + /// The error enum. Must always be generic over ``, which is expanded to ``. + #[pallet::error] + pub enum Error { + SomethingWentWrong, + SomethingBroke, + } + + /// All the possible hooks that a pallet can have. See [`frame_support::traits::Hooks`] for more + /// info. + #[pallet::hooks] + impl Hooks for Pallet { + fn integrity_test() { + unimplemented!() + } + + fn offchain_worker(_n: T::BlockNumber) { + unimplemented!() + } + + fn on_initialize(_n: T::BlockNumber) -> Weight { + unimplemented!() + } + + fn on_finalize(_n: T::BlockNumber) { + unimplemented!() + } + + fn on_idle(_n: T::BlockNumber, _remaining_weight: Weight) -> Weight { + unimplemented!() + } + + fn on_runtime_upgrade() -> Weight { + unimplemented!() + } + + #[cfg(feature = "try-runtime")] + fn pre_upgrade() -> Result, &'static str> { + unimplemented!() + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade(_state: Vec) -> Result<(), &'static str> { + unimplemented!() + } + + #[cfg(feature = "try-runtime")] + fn try_state(_n: _) -> Result<(), &'static str> { + unimplemented!() + } + } + + #[pallet::composite_enum] + pub enum HoldReason { + Staking, + } + + #[pallet::validate_unsigned] + impl ValidateUnsigned for Pallet { + type Call = Call; + fn validate_unsigned(_: TransactionSource, _: &Self::Call) -> TransactionValidity { + unimplemented!() + } + + fn pre_dispatch(_: &Self::Call) -> Result<(), TransactionValidityError> { + unimplemented!() + } + } + + #[pallet::inherent] + impl ProvideInherent for Pallet { + type Call = Call; + type Error = MakeFatalError<()>; + + const INHERENT_IDENTIFIER: [u8; 8] = *b"test1234"; + + fn create_inherent(_data: &InherentData) -> Option { + unimplemented!(); + } + + fn is_inherent(_call: &Self::Call) -> bool { + unimplemented!() + } + } +} diff --git a/frame/support/src/lib.rs b/frame/support/src/lib.rs index 036bd93464b9f..1d173d5736cd9 100644 --- a/frame/support/src/lib.rs +++ b/frame/support/src/lib.rs @@ -1478,6 +1478,7 @@ pub mod pallet_prelude { pub use codec::{Decode, Encode, MaxEncodedLen}; pub use frame_support::pallet_macros::*; pub use scale_info::TypeInfo; + pub use sp_inherents::MakeFatalError; pub use sp_runtime::{ traits::{MaybeSerializeDeserialize, Member, ValidateUnsigned}, transaction_validity::{ From 14214076766c5417cdc871b030ec8ec783d09c85 Mon Sep 17 00:00:00 2001 From: kianenigma Date: Tue, 9 May 2023 20:40:51 +0200 Subject: [PATCH 02/17] update --- frame/examples/kitchensink/src/lib.rs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/frame/examples/kitchensink/src/lib.rs b/frame/examples/kitchensink/src/lib.rs index e8b07821fe375..4f2f4e7228574 100644 --- a/frame/examples/kitchensink/src/lib.rs +++ b/frame/examples/kitchensink/src/lib.rs @@ -1,9 +1,9 @@ -/// The kitchen-sink catalog of the the FRAME macros. +/// The kitchen-sink catalog of the the FRAME macros and their various syntax options. /// -/// This example does not focus on pallet instancing, and does nto include any where clauses on `T`. -/// -/// This has all the possibly functionalities that a pallet can have in the normal mode (except -/// `dev_mode`). +/// This example does not focus on pallet instancing, `dev_mode`, and does nto include any 'where' +/// clauses on `T`. These will both incur additional complexity to the syntax, but are not discussed +/// here. +// TODO: link to a particular example pallet that highlights each of these. https://github.com/paritytech/substrate/issues/13951 #[frame_support::pallet] pub mod pallet { use super::*; @@ -18,12 +18,13 @@ pub mod pallet { /// [`frame_support::traits::Get`]. These are all types that the pallet is delegating to the top /// level runtime to provide to it. /// - /// The `FRAME`-specific functionality are: + /// The `FRAME`-specific syntax are: /// - /// * the use of `#[pallet::constant]`, which places a `Get` implementation in the metadata. + /// * the use of `#[pallet::constant]`([`frame_support::procedural`]), which places a `Get` + /// implementation in the metadata. /// * `type RuntimeEvent`, which is mandatory if your pallet has events. See TODO. - /// * Needless to say, because `Config` is bounded by `frame_system::Config`, you can use all - /// the items from `frame_system::Config` as well, such as `AccountId`. + /// * Needless to say, because [`Config`] is bounded by [`frame_system::Config`], you can use + /// all the items from [`frame_system::Config`] as well, such as `AccountId`. /// * `#[pallet::disable_frame_system_supertrait_check]` would remove the need for /// `frame_system::Config` to exist, which you should almost never need. #[pallet::config] From 3e15142c122b09ae9bf18c6d1c56c8be100cf2f1 Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Wed, 31 May 2023 12:28:23 +0530 Subject: [PATCH 03/17] Adds benchmarking setup --- Cargo.lock | 1 + frame/examples/kitchensink/Cargo.toml | 4 + .../examples/kitchensink/src/benchmarking.rs | 63 +++++++++ frame/examples/kitchensink/src/lib.rs | 20 ++- frame/examples/kitchensink/src/tests.rs | 123 ++++++++++++++++++ 5 files changed, 207 insertions(+), 4 deletions(-) create mode 100644 frame/examples/kitchensink/src/benchmarking.rs create mode 100644 frame/examples/kitchensink/src/tests.rs diff --git a/Cargo.lock b/Cargo.lock index d60fc728149f8..67e78b35e8e14 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6118,6 +6118,7 @@ dependencies = [ "frame-support", "frame-system", "log", + "pallet-balances", "parity-scale-codec", "scale-info", "sp-core", diff --git a/frame/examples/kitchensink/Cargo.toml b/frame/examples/kitchensink/Cargo.toml index 9b0941536f0bc..7b528b86a34b5 100644 --- a/frame/examples/kitchensink/Cargo.toml +++ b/frame/examples/kitchensink/Cargo.toml @@ -26,6 +26,8 @@ sp-std = { version = "5.0.0", default-features = false, path = "../../../primiti 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 = "7.0.0", default-features = false, path = "../../../primitives/core" } @@ -44,6 +46,8 @@ std = [ "sp-std/std", "frame-benchmarking?/std", + + "pallet-balances/std", ] runtime-benchmarks = ["frame-benchmarking/runtime-benchmarks"] try-runtime = ["frame-support/try-runtime"] diff --git a/frame/examples/kitchensink/src/benchmarking.rs b/frame/examples/kitchensink/src/benchmarking.rs new file mode 100644 index 0000000000000..af7c5ce39424d --- /dev/null +++ b/frame/examples/kitchensink/src/benchmarking.rs @@ -0,0 +1,63 @@ +// 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 crate::*; +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::::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!(Pallet, crate::tests::new_test_ext(), crate::tests::Test); +} diff --git a/frame/examples/kitchensink/src/lib.rs b/frame/examples/kitchensink/src/lib.rs index 4f2f4e7228574..eded4dbaef83c 100644 --- a/frame/examples/kitchensink/src/lib.rs +++ b/frame/examples/kitchensink/src/lib.rs @@ -3,7 +3,17 @@ /// This example does not focus on pallet instancing, `dev_mode`, and does nto include any 'where' /// clauses on `T`. These will both incur additional complexity to the syntax, but are not discussed /// here. -// TODO: link to a particular example pallet that highlights each of these. https://github.com/paritytech/substrate/issues/13951 +// TODO: link to a particular example pallet that highlights each of these. +// https://github.com/paritytech/substrate/issues/13951 + +// Re-export pallet items so that they can be accessed from the crate namespace. +pub use pallet::*; + +#[cfg(test)] +mod tests; + +mod benchmarking; + #[frame_support::pallet] pub mod pallet { use super::*; @@ -164,10 +174,12 @@ pub mod pallet { #[pallet::weight(0)] pub fn set_foo( _: OriginFor, - _new_foo: u32, + new_foo: u32, #[pallet::compact] _other_compact: u128, ) -> DispatchResult { - unimplemented!() + Foo::::set(Some(new_foo)); + + Ok(()) } } @@ -242,7 +254,7 @@ pub mod pallet { } #[cfg(feature = "try-runtime")] - fn try_state(_n: _) -> Result<(), &'static str> { + fn try_state(_n: T::BlockNumber) -> Result<(), &'static str> { unimplemented!() } } diff --git a/frame/examples/kitchensink/src/tests.rs b/frame/examples/kitchensink/src/tests.rs new file mode 100644 index 0000000000000..2f3c5e2cc5fd9 --- /dev/null +++ b/frame/examples/kitchensink/src/tests.rs @@ -0,0 +1,123 @@ +// 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. + +//! Tests for pallet-example-basic. + +use crate::*; +use frame_support::{traits::ConstU64, parameter_types}; +use sp_core::H256; +// The testing primitives are very useful for avoiding having to work with signatures +// or public keys. `u64` is used as the `AccountId` and no `Signature`s are required. +use sp_runtime::{ + testing::Header, + traits::{BlakeTwo256, IdentityLookup}, + BuildStorage, +}; +// Reexport crate as its pallet name for construct_runtime. +use crate as pallet_example_kitchensink; + +type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; +type Block = frame_system::mocking::MockBlock; + +// For testing the pallet, we construct a mock runtime. +frame_support::construct_runtime!( + pub enum Test where + Block = Block, + NodeBlock = Block, + UncheckedExtrinsic = UncheckedExtrinsic, + { + System: frame_system::{Pallet, Call, Config, Storage, Event}, + Balances: pallet_balances::{Pallet, Call, Storage, Config, Event}, + Kitchensink: pallet_example_kitchensink::{Pallet, Call, Storage, Config, Event}, + } +); + +impl frame_system::Config for Test { + type BaseCallFilter = frame_support::traits::Everything; + type BlockWeights = (); + type BlockLength = (); + type DbWeight = (); + type RuntimeOrigin = RuntimeOrigin; + type Index = u64; + type BlockNumber = u64; + type Hash = H256; + type RuntimeCall = RuntimeCall; + type Hashing = BlakeTwo256; + type AccountId = u64; + type Lookup = IdentityLookup; + type Header = Header; + type RuntimeEvent = RuntimeEvent; + type BlockHashCount = ConstU64<250>; + type Version = (); + type PalletInfo = PalletInfo; + type AccountData = pallet_balances::AccountData; + type OnNewAccount = (); + type OnKilledAccount = (); + type SystemWeightInfo = (); + type SS58Prefix = (); + type OnSetCode = (); + type MaxConsumers = frame_support::traits::ConstU32<16>; +} + +impl pallet_balances::Config for Test { + type MaxLocks = (); + type MaxReserves = (); + type ReserveIdentifier = [u8; 8]; + type Balance = u64; + type DustRemoval = (); + type RuntimeEvent = RuntimeEvent; + type ExistentialDeposit = ConstU64<1>; + type AccountStore = System; + type WeightInfo = (); + type FreezeIdentifier = (); + type MaxFreezes = (); + type HoldIdentifier = (); + type MaxHolds = (); +} + +parameter_types! { + pub const InMetadata: u32 = 30; +} + +impl Config for Test { + type RuntimeEvent = RuntimeEvent; + type Currency = Balances; + type InMetadata = InMetadata; + + const FOO:u32 = 100; + + fn some_function() -> u32 { + 5u32 + } +} + +// This function basically just builds a genesis storage key/value store according to +// our desired mockup. +pub fn new_test_ext() -> sp_io::TestExternalities { + let t = GenesisConfig { + // We use default for brevity, but you can configure as desired if needed. + system: Default::default(), + balances: Default::default(), + kitchensink: pallet_example_kitchensink::GenesisConfig { + bar: 32, + foo: 24, + }, + } + .build_storage() + .unwrap(); + t.into() +} \ No newline at end of file From a2c1a8eb699fb807fc8f9a5d3c07d010072b233d Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Thu, 1 Jun 2023 19:03:09 +0530 Subject: [PATCH 04/17] Updates APIs --- frame/examples/kitchensink/Cargo.toml | 8 ++++---- frame/examples/kitchensink/src/lib.rs | 9 ++++++--- frame/examples/kitchensink/src/tests.rs | 2 +- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/frame/examples/kitchensink/Cargo.toml b/frame/examples/kitchensink/Cargo.toml index 7b528b86a34b5..783a5276e43eb 100644 --- a/frame/examples/kitchensink/Cargo.toml +++ b/frame/examples/kitchensink/Cargo.toml @@ -20,16 +20,16 @@ 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 = "7.0.0", default-features = false, path = "../../../primitives/io" } -sp-runtime = { version = "7.0.0", default-features = false, path = "../../../primitives/runtime" } -sp-std = { version = "5.0.0", default-features = false, path = "../../../primitives/std" } +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 = "7.0.0", default-features = false, path = "../../../primitives/core" } +sp-core = { version = "21.0.0", default-features = false, path = "../../../primitives/core" } [features] default = ["std"] diff --git a/frame/examples/kitchensink/src/lib.rs b/frame/examples/kitchensink/src/lib.rs index eded4dbaef83c..0ce7ca17282d6 100644 --- a/frame/examples/kitchensink/src/lib.rs +++ b/frame/examples/kitchensink/src/lib.rs @@ -14,6 +14,9 @@ mod tests; mod benchmarking; +#[cfg(feature = "try-runtime")] +use sp_runtime::TryRuntimeError; + #[frame_support::pallet] pub mod pallet { use super::*; @@ -244,17 +247,17 @@ pub mod pallet { } #[cfg(feature = "try-runtime")] - fn pre_upgrade() -> Result, &'static str> { + fn pre_upgrade() -> Result, TryRuntimeError> { unimplemented!() } #[cfg(feature = "try-runtime")] - fn post_upgrade(_state: Vec) -> Result<(), &'static str> { + fn post_upgrade(_state: Vec) -> Result<(), TryRuntimeError> { unimplemented!() } #[cfg(feature = "try-runtime")] - fn try_state(_n: T::BlockNumber) -> Result<(), &'static str> { + fn try_state(_n: T::BlockNumber) -> Result<(), TryRuntimeError> { unimplemented!() } } diff --git a/frame/examples/kitchensink/src/tests.rs b/frame/examples/kitchensink/src/tests.rs index 2f3c5e2cc5fd9..cab188845d1c6 100644 --- a/frame/examples/kitchensink/src/tests.rs +++ b/frame/examples/kitchensink/src/tests.rs @@ -85,7 +85,7 @@ impl pallet_balances::Config for Test { type WeightInfo = (); type FreezeIdentifier = (); type MaxFreezes = (); - type HoldIdentifier = (); + type RuntimeHoldReason = (); type MaxHolds = (); } From 5828752d794044708c6f2648a0c68fc77bf2c431 Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Thu, 1 Jun 2023 19:06:19 +0530 Subject: [PATCH 05/17] Fixes benchmark --- frame/examples/kitchensink/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frame/examples/kitchensink/src/lib.rs b/frame/examples/kitchensink/src/lib.rs index 0ce7ca17282d6..eed4ca9c73bfa 100644 --- a/frame/examples/kitchensink/src/lib.rs +++ b/frame/examples/kitchensink/src/lib.rs @@ -167,7 +167,7 @@ pub mod pallet { #[pallet::genesis_build] impl GenesisBuild for GenesisConfig { fn build(&self) { - unimplemented!() + } } @@ -223,7 +223,7 @@ pub mod pallet { #[pallet::hooks] impl Hooks for Pallet { fn integrity_test() { - unimplemented!() + } fn offchain_worker(_n: T::BlockNumber) { From c0851f12cbd8f4248a66d879a6d397dd8fde3491 Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Thu, 1 Jun 2023 19:13:08 +0530 Subject: [PATCH 06/17] Uses derive_impl for frame_system --- frame/examples/kitchensink/src/tests.rs | 37 +++++++------------------ 1 file changed, 10 insertions(+), 27 deletions(-) diff --git a/frame/examples/kitchensink/src/tests.rs b/frame/examples/kitchensink/src/tests.rs index cab188845d1c6..85245cecfaf29 100644 --- a/frame/examples/kitchensink/src/tests.rs +++ b/frame/examples/kitchensink/src/tests.rs @@ -15,18 +15,14 @@ // See the License for the specific language governing permissions and // limitations under the License. -//! Tests for pallet-example-basic. +//! Tests for pallet-example-kitchensink. use crate::*; use frame_support::{traits::ConstU64, parameter_types}; -use sp_core::H256; -// The testing primitives are very useful for avoiding having to work with signatures -// or public keys. `u64` is used as the `AccountId` and no `Signature`s are required. -use sp_runtime::{ - testing::Header, - traits::{BlakeTwo256, IdentityLookup}, - BuildStorage, -}; +use sp_runtime::BuildStorage; +use frame_support::macro_magic::use_attr; +#[use_attr] +use frame_support::derive_impl; // Reexport crate as its pallet name for construct_runtime. use crate as pallet_example_kitchensink; @@ -46,31 +42,18 @@ frame_support::construct_runtime!( } ); +/// Using a default config for [`frame_system`] in tests. See `default-config` example for more +/// details. +#[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig)] impl frame_system::Config for Test { type BaseCallFilter = frame_support::traits::Everything; - type BlockWeights = (); - type BlockLength = (); - type DbWeight = (); type RuntimeOrigin = RuntimeOrigin; - type Index = u64; - type BlockNumber = u64; - type Hash = H256; type RuntimeCall = RuntimeCall; - type Hashing = BlakeTwo256; - type AccountId = u64; - type Lookup = IdentityLookup; - type Header = Header; type RuntimeEvent = RuntimeEvent; - type BlockHashCount = ConstU64<250>; - type Version = (); type PalletInfo = PalletInfo; - type AccountData = pallet_balances::AccountData; - type OnNewAccount = (); - type OnKilledAccount = (); - type SystemWeightInfo = (); - type SS58Prefix = (); type OnSetCode = (); - type MaxConsumers = frame_support::traits::ConstU32<16>; + + type AccountData = pallet_balances::AccountData; } impl pallet_balances::Config for Test { From 93d5aa8022cbf6bf30e48c587b1b763861a9f891 Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Fri, 2 Jun 2023 14:20:42 +0530 Subject: [PATCH 07/17] Adds benchmarks --- .../examples/kitchensink/src/benchmarking.rs | 7 +- frame/examples/kitchensink/src/lib.rs | 19 ++++-- frame/examples/kitchensink/src/tests.rs | 2 + frame/examples/kitchensink/src/weights.rs | 68 +++++++++++++++++++ 4 files changed, 88 insertions(+), 8 deletions(-) create mode 100644 frame/examples/kitchensink/src/weights.rs diff --git a/frame/examples/kitchensink/src/benchmarking.rs b/frame/examples/kitchensink/src/benchmarking.rs index af7c5ce39424d..24da581fc967b 100644 --- a/frame/examples/kitchensink/src/benchmarking.rs +++ b/frame/examples/kitchensink/src/benchmarking.rs @@ -19,8 +19,11 @@ // Only enable this module for benchmarking. #![cfg(feature = "runtime-benchmarks")] +use super::*; + +#[allow(unused)] +use crate::Pallet as Kitchensink; -use crate::*; use frame_benchmarking::v2::*; use frame_system::RawOrigin; @@ -59,5 +62,5 @@ mod benchmarks { // // 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!(Pallet, crate::tests::new_test_ext(), crate::tests::Test); + impl_benchmark_test_suite!(Kitchensink, crate::tests::new_test_ext(), crate::tests::Test); } diff --git a/frame/examples/kitchensink/src/lib.rs b/frame/examples/kitchensink/src/lib.rs index eed4ca9c73bfa..bb5ab7de20cd3 100644 --- a/frame/examples/kitchensink/src/lib.rs +++ b/frame/examples/kitchensink/src/lib.rs @@ -1,3 +1,4 @@ +#![cfg_attr(not(feature = "std"), no_std)] /// The kitchen-sink catalog of the the FRAME macros and their various syntax options. /// /// This example does not focus on pallet instancing, `dev_mode`, and does nto include any 'where' @@ -12,11 +13,15 @@ pub use pallet::*; #[cfg(test)] mod tests; +#[cfg(feature = "runtime-benchmarks")] mod benchmarking; #[cfg(feature = "try-runtime")] use sp_runtime::TryRuntimeError; +pub mod weights; +pub use weights::*; + #[frame_support::pallet] pub mod pallet { use super::*; @@ -45,6 +50,9 @@ pub mod pallet { /// The overarching runtime event type. type RuntimeEvent: From> + IsType<::RuntimeEvent>; + /// Type representing the weight of this pallet + type WeightInfo: WeightInfo; + /// This is a normal Rust type, nothing specific to FRAME here. type Currency: frame_support::traits::tokens::fungible::Inspect; @@ -83,8 +91,8 @@ pub mod pallet { #[pallet::storage_version(STORAGE_VERSION)] pub struct Pallet(_); - #[pallet::origin] - pub type Origin = frame_system::RawOrigin<::AccountId>; + // #[pallet::origin] + // pub type Origin = frame_system::RawOrigin<::AccountId>; // first, we showcase all the possible storage types, with most of their details. @@ -157,24 +165,23 @@ pub mod pallet { pub bar: T::BlockNumber, } - #[cfg(feature = "std")] impl Default for GenesisConfig { fn default() -> Self { - unimplemented!() + Self { foo: 0, bar: Default::default() } } } #[pallet::genesis_build] impl GenesisBuild for GenesisConfig { fn build(&self) { - + Foo::::put(self.foo); } } #[pallet::call] impl Pallet { #[pallet::call_index(0)] - #[pallet::weight(0)] + #[pallet::weight(T::WeightInfo::set_foo_benchmark())] pub fn set_foo( _: OriginFor, new_foo: u32, diff --git a/frame/examples/kitchensink/src/tests.rs b/frame/examples/kitchensink/src/tests.rs index 85245cecfaf29..47897c1b8bd3a 100644 --- a/frame/examples/kitchensink/src/tests.rs +++ b/frame/examples/kitchensink/src/tests.rs @@ -78,6 +78,8 @@ parameter_types! { impl Config for Test { type RuntimeEvent = RuntimeEvent; + type WeightInfo = (); + type Currency = Balances; type InMetadata = InMetadata; diff --git a/frame/examples/kitchensink/src/weights.rs b/frame/examples/kitchensink/src/weights.rs new file mode 100644 index 0000000000000..1d083a9b80eea --- /dev/null +++ b/frame/examples/kitchensink/src/weights.rs @@ -0,0 +1,68 @@ + +//! Autogenerated weights for `pallet_example_kitchensink` +//! +//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev +//! DATE: 2023-06-02, STEPS: `20`, REPEAT: `10`, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! WORST CASE MAP SIZE: `1000000` +//! HOSTNAME: `MacBook.local`, CPU: `` +//! EXECUTION: None, WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024 + +// Executed Command: +// ./target/release/node-template +// benchmark +// pallet +// --chain +// dev +// --pallet +// pallet_example_kitchensink +// --extrinsic +// * +// --steps +// 20 +// --repeat +// 10 +// --output +// frame/examples/kitchensink/src/weights.rs + +#![cfg_attr(rustfmt, rustfmt_skip)] +#![allow(unused_parens)] +#![allow(unused_imports)] +#![allow(missing_docs)] + +use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}}; +use core::marker::PhantomData; + +/// Weight functions needed for pallet_template. +pub trait WeightInfo { + fn set_foo_benchmark() -> Weight; +} + +/// Weight functions for `pallet_example_kitchensink`. +pub struct SubstrateWeight(PhantomData); +impl WeightInfo for SubstrateWeight { + /// Storage: Kitchensink OtherFoo (r:0 w:1) + /// Proof Skipped: Kitchensink OtherFoo (max_values: Some(1), max_size: None, mode: Measured) + fn set_foo_benchmark() -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 1_000_000 picoseconds. + Weight::from_parts(1_000_000, 0) + .saturating_add(Weight::from_parts(0, 0)) + .saturating_add(T::DbWeight::get().writes(1)) + } +} + +impl WeightInfo for () { + /// Storage: Kitchensink OtherFoo (r:0 w:1) + /// Proof Skipped: Kitchensink OtherFoo (max_values: Some(1), max_size: None, mode: Measured) + fn set_foo_benchmark() -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 1_000_000 picoseconds. + Weight::from_parts(1_000_000, 0) + .saturating_add(Weight::from_parts(0, 0)) + .saturating_add(RocksDbWeight::get().writes(1)) + } +} From 765612074134afad86da77400d30ef76ceef98c2 Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Fri, 2 Jun 2023 14:21:51 +0530 Subject: [PATCH 08/17] Minor update --- frame/examples/kitchensink/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frame/examples/kitchensink/src/lib.rs b/frame/examples/kitchensink/src/lib.rs index bb5ab7de20cd3..f51a2da5209c3 100644 --- a/frame/examples/kitchensink/src/lib.rs +++ b/frame/examples/kitchensink/src/lib.rs @@ -91,8 +91,8 @@ pub mod pallet { #[pallet::storage_version(STORAGE_VERSION)] pub struct Pallet(_); - // #[pallet::origin] - // pub type Origin = frame_system::RawOrigin<::AccountId>; + #[pallet::origin] + pub type Origin = frame_system::RawOrigin<::AccountId>; // first, we showcase all the possible storage types, with most of their details. From 81f75b041e178b3e3e1c2909844225f0299d1ddf Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Sat, 3 Jun 2023 10:41:55 +0530 Subject: [PATCH 09/17] Adds license --- frame/examples/kitchensink/src/lib.rs | 36 +++++++++++++++++++++------ 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/frame/examples/kitchensink/src/lib.rs b/frame/examples/kitchensink/src/lib.rs index f51a2da5209c3..82e48dc100022 100644 --- a/frame/examples/kitchensink/src/lib.rs +++ b/frame/examples/kitchensink/src/lib.rs @@ -1,11 +1,33 @@ +// 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. + +//! +//! # Kitchensink Example Pallet +//! +//! **This pallet serves as an example and is not meant to be used in production.** +//! +//! The kitchen-sink catalog of the the FRAME macros and their various syntax options. +//! +//! This example does not focus on pallet instancing, `dev_mode`, and does nto include any 'where' +//! clauses on `T`. These will both incur additional complexity to the syntax, but are not discussed +//! here. + +// Ensure we're `no_std` when compiling for Wasm. #![cfg_attr(not(feature = "std"), no_std)] -/// The kitchen-sink catalog of the the FRAME macros and their various syntax options. -/// -/// This example does not focus on pallet instancing, `dev_mode`, and does nto include any 'where' -/// clauses on `T`. These will both incur additional complexity to the syntax, but are not discussed -/// here. -// TODO: link to a particular example pallet that highlights each of these. -// https://github.com/paritytech/substrate/issues/13951 // Re-export pallet items so that they can be accessed from the crate namespace. pub use pallet::*; From b62fba0edd379bdbb5f00f9202df042949c23818 Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Sat, 3 Jun 2023 10:57:05 +0530 Subject: [PATCH 10/17] Adds examples crate --- Cargo.lock | 11 +++++++++++ Cargo.toml | 1 + frame/examples/Cargo.toml | 36 ++++++++++++++++++++++++++++++++++++ frame/examples/src/lib.rs | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+) create mode 100644 frame/examples/Cargo.toml create mode 100644 frame/examples/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 40d8ef8b2c196..88d7033a35d12 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6527,6 +6527,17 @@ dependencies = [ "sp-std", ] +[[package]] +name = "pallet-examples" +version = "4.0.0-dev" +dependencies = [ + "pallet-default-config-example", + "pallet-dev-mode", + "pallet-example-basic", + "pallet-example-kitchensink", + "pallet-example-offchain-worker", +] + [[package]] name = "pallet-fast-unstake" version = "4.0.0-dev" diff --git a/Cargo.toml b/Cargo.toml index 7e82df1d5aebb..cb0417de788c9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -106,6 +106,7 @@ 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", diff --git a/frame/examples/Cargo.toml b/frame/examples/Cargo.toml new file mode 100644 index 0000000000000..57def091b1f63 --- /dev/null +++ b/frame/examples/Cargo.toml @@ -0,0 +1,36 @@ +[package] +name = "pallet-examples" +version = "4.0.0-dev" +authors = ["Parity Technologies "] +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", +] \ No newline at end of file diff --git a/frame/examples/src/lib.rs b/frame/examples/src/lib.rs new file mode 100644 index 0000000000000..fb366b271e605 --- /dev/null +++ b/frame/examples/src/lib.rs @@ -0,0 +1,38 @@ +// 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. + +//! # FRAME Pallet Examples +//! +//! This crate contains examples of FRAME pallets. It is not intended to be used in production. +//! +//! ## Pallets +//! +//! - [**`pallet_example_basic`**](./basic): A simple example of a FRAME pallet demonstrating +//! concepts, APIs and structures common to most FRAME runtimes. +//! +//! - [**`pallet_example_offchain_worker`**](./offchain-worker): A simple example of a FRAME pallet +//! demonstrating concepts, APIs and structures common to most offchain workers. +//! +//! - [**`pallet-default-config-example`**](./default-config): A simple example of a FRAME pallet +//! demonstrating the simpler way to implement `Config` trait of pallets. +//! +//! - [**`pallet-dev-mode`**](./dev-mode): A simple example of a FRAME pallet demonstrating +//! the ease of requirements for a pallet in dev mode. +//! +//! - [**`pallet-example-kitchensink`**](./kitchensink): A simple example of a FRAME pallet +//! demonstrating a catalog of the the FRAME macros and their various syntax options. +//! \ No newline at end of file From 57bde939950d18a20fc3c58d3dd6a876f7354fe0 Mon Sep 17 00:00:00 2001 From: command-bot <> Date: Sat, 3 Jun 2023 05:29:11 +0000 Subject: [PATCH 11/17] ".git/.scripts/commands/fmt/fmt.sh" --- frame/examples/kitchensink/src/lib.rs | 8 +++----- frame/examples/kitchensink/src/tests.rs | 18 +++++++----------- frame/examples/src/lib.rs | 21 ++++++++++----------- 3 files changed, 20 insertions(+), 27 deletions(-) diff --git a/frame/examples/kitchensink/src/lib.rs b/frame/examples/kitchensink/src/lib.rs index 82e48dc100022..55984faba38b2 100644 --- a/frame/examples/kitchensink/src/lib.rs +++ b/frame/examples/kitchensink/src/lib.rs @@ -19,9 +19,9 @@ //! # Kitchensink Example Pallet //! //! **This pallet serves as an example and is not meant to be used in production.** -//! +//! //! The kitchen-sink catalog of the the FRAME macros and their various syntax options. -//! +//! //! This example does not focus on pallet instancing, `dev_mode`, and does nto include any 'where' //! clauses on `T`. These will both incur additional complexity to the syntax, but are not discussed //! here. @@ -251,9 +251,7 @@ pub mod pallet { /// info. #[pallet::hooks] impl Hooks for Pallet { - fn integrity_test() { - - } + fn integrity_test() {} fn offchain_worker(_n: T::BlockNumber) { unimplemented!() diff --git a/frame/examples/kitchensink/src/tests.rs b/frame/examples/kitchensink/src/tests.rs index 47897c1b8bd3a..08de7941e9d21 100644 --- a/frame/examples/kitchensink/src/tests.rs +++ b/frame/examples/kitchensink/src/tests.rs @@ -18,11 +18,10 @@ //! Tests for pallet-example-kitchensink. use crate::*; -use frame_support::{traits::ConstU64, parameter_types}; -use sp_runtime::BuildStorage; -use frame_support::macro_magic::use_attr; #[use_attr] use frame_support::derive_impl; +use frame_support::{macro_magic::use_attr, parameter_types, traits::ConstU64}; +use sp_runtime::BuildStorage; // Reexport crate as its pallet name for construct_runtime. use crate as pallet_example_kitchensink; @@ -42,7 +41,7 @@ frame_support::construct_runtime!( } ); -/// Using a default config for [`frame_system`] in tests. See `default-config` example for more +/// Using a default config for [`frame_system`] in tests. See `default-config` example for more /// details. #[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig)] impl frame_system::Config for Test { @@ -79,11 +78,11 @@ parameter_types! { impl Config for Test { type RuntimeEvent = RuntimeEvent; type WeightInfo = (); - + type Currency = Balances; type InMetadata = InMetadata; - const FOO:u32 = 100; + const FOO: u32 = 100; fn some_function() -> u32 { 5u32 @@ -97,12 +96,9 @@ pub fn new_test_ext() -> sp_io::TestExternalities { // We use default for brevity, but you can configure as desired if needed. system: Default::default(), balances: Default::default(), - kitchensink: pallet_example_kitchensink::GenesisConfig { - bar: 32, - foo: 24, - }, + kitchensink: pallet_example_kitchensink::GenesisConfig { bar: 32, foo: 24 }, } .build_storage() .unwrap(); t.into() -} \ No newline at end of file +} diff --git a/frame/examples/src/lib.rs b/frame/examples/src/lib.rs index fb366b271e605..3ff1aa7b9f1f2 100644 --- a/frame/examples/src/lib.rs +++ b/frame/examples/src/lib.rs @@ -16,23 +16,22 @@ // limitations under the License. //! # FRAME Pallet Examples -//! +//! //! This crate contains examples of FRAME pallets. It is not intended to be used in production. -//! +//! //! ## Pallets -//! +//! //! - [**`pallet_example_basic`**](./basic): A simple example of a FRAME pallet demonstrating //! concepts, APIs and structures common to most FRAME runtimes. -//! +//! //! - [**`pallet_example_offchain_worker`**](./offchain-worker): A simple example of a FRAME pallet //! demonstrating concepts, APIs and structures common to most offchain workers. -//! +//! //! - [**`pallet-default-config-example`**](./default-config): A simple example of a FRAME pallet -//! demonstrating the simpler way to implement `Config` trait of pallets. -//! -//! - [**`pallet-dev-mode`**](./dev-mode): A simple example of a FRAME pallet demonstrating -//! the ease of requirements for a pallet in dev mode. -//! +//! demonstrating the simpler way to implement `Config` trait of pallets. +//! +//! - [**`pallet-dev-mode`**](./dev-mode): A simple example of a FRAME pallet demonstrating the ease +//! of requirements for a pallet in dev mode. +//! //! - [**`pallet-example-kitchensink`**](./kitchensink): A simple example of a FRAME pallet //! demonstrating a catalog of the the FRAME macros and their various syntax options. -//! \ No newline at end of file From a7eda47d3549617a90dcd7cbf180b96d21c8e806 Mon Sep 17 00:00:00 2001 From: gupnik <17176722+gupnik@users.noreply.github.com> Date: Tue, 6 Jun 2023 10:40:45 +0530 Subject: [PATCH 12/17] Update frame/examples/kitchensink/src/tests.rs Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> --- frame/examples/kitchensink/src/tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/examples/kitchensink/src/tests.rs b/frame/examples/kitchensink/src/tests.rs index 08de7941e9d21..d6c0891df89d7 100644 --- a/frame/examples/kitchensink/src/tests.rs +++ b/frame/examples/kitchensink/src/tests.rs @@ -92,7 +92,7 @@ impl Config for Test { // This function basically just builds a genesis storage key/value store according to // our desired mockup. pub fn new_test_ext() -> sp_io::TestExternalities { - let t = GenesisConfig { + let t = RuntimeGenesisConfig { // We use default for brevity, but you can configure as desired if needed. system: Default::default(), balances: Default::default(), From e41760c9c3bf60f4a87502db934ca424c34a4f62 Mon Sep 17 00:00:00 2001 From: gupnik <17176722+gupnik@users.noreply.github.com> Date: Tue, 6 Jun 2023 10:40:57 +0530 Subject: [PATCH 13/17] Update frame/examples/kitchensink/src/lib.rs Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> --- frame/examples/kitchensink/src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/frame/examples/kitchensink/src/lib.rs b/frame/examples/kitchensink/src/lib.rs index 55984faba38b2..1a736ee30b20e 100644 --- a/frame/examples/kitchensink/src/lib.rs +++ b/frame/examples/kitchensink/src/lib.rs @@ -26,7 +26,6 @@ //! clauses on `T`. These will both incur additional complexity to the syntax, but are not discussed //! here. -// Ensure we're `no_std` when compiling for Wasm. #![cfg_attr(not(feature = "std"), no_std)] // Re-export pallet items so that they can be accessed from the crate namespace. From 5a76abac67067f9cb6340d1dccce103ba04fa3f2 Mon Sep 17 00:00:00 2001 From: gupnik <17176722+gupnik@users.noreply.github.com> Date: Tue, 6 Jun 2023 10:41:28 +0530 Subject: [PATCH 14/17] Update frame/examples/kitchensink/src/lib.rs Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> --- frame/examples/kitchensink/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/examples/kitchensink/src/lib.rs b/frame/examples/kitchensink/src/lib.rs index 1a736ee30b20e..58d683a449423 100644 --- a/frame/examples/kitchensink/src/lib.rs +++ b/frame/examples/kitchensink/src/lib.rs @@ -235,7 +235,7 @@ pub mod pallet { at: u32, to: T::AccountId, }, - // /// Another variant. + /// Another variant. SomeoneJoined(T::AccountId), } From e15a8bf26c696657bd49df906f6e3b07f7e987de Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Tue, 6 Jun 2023 14:15:05 +0530 Subject: [PATCH 15/17] Addresses review comments --- frame/examples/kitchensink/src/lib.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/frame/examples/kitchensink/src/lib.rs b/frame/examples/kitchensink/src/lib.rs index 82e48dc100022..d30a16d774320 100644 --- a/frame/examples/kitchensink/src/lib.rs +++ b/frame/examples/kitchensink/src/lib.rs @@ -15,7 +15,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -//! //! # Kitchensink Example Pallet //! //! **This pallet serves as an example and is not meant to be used in production.** @@ -90,6 +89,7 @@ pub mod pallet { type InMetadata: Get; } + /// Allows you to define some extra constants to be added into constant metadata. #[pallet::extra_constants] impl Pallet { #[allow(non_snake_case)] @@ -108,11 +108,11 @@ pub mod pallet { /// The pallet struct. There's nothing special to FRAME about this; it can implement functions /// in an impl blocks, traits and so on. #[pallet::pallet] - // #[pallet::generate_storage_info] // TODO: should be removed. #[pallet::without_storage_info] #[pallet::storage_version(STORAGE_VERSION)] pub struct Pallet(_); + /// Allows you to define some origin for the pallet. #[pallet::origin] pub type Origin = frame_system::RawOrigin<::AccountId>; @@ -291,11 +291,16 @@ pub mod pallet { } } + /// Allows you to define an enum on the pallet which will then instruct + /// `construct_runtime` to amalgamate all similarly-named enums from other + /// pallets into an aggregate enum. #[pallet::composite_enum] pub enum HoldReason { Staking, } + /// Allows the pallet to validate some unsigned transaction. See + /// [`sp_runtime::traits::ValidateUnsigned`] for more info. #[pallet::validate_unsigned] impl ValidateUnsigned for Pallet { type Call = Call; @@ -308,6 +313,8 @@ pub mod pallet { } } + /// Allows the pallet to provide some inherent. See [`frame_support::inherent::ProvideInherent`] + /// for more info. #[pallet::inherent] impl ProvideInherent for Pallet { type Call = Call; From ab68822091d35955025ede9242fc152959cdbba9 Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Tue, 6 Jun 2023 14:20:15 +0530 Subject: [PATCH 16/17] Addresses review comments --- frame/examples/kitchensink/src/lib.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/frame/examples/kitchensink/src/lib.rs b/frame/examples/kitchensink/src/lib.rs index 7d604e88c9443..08bf327f00886 100644 --- a/frame/examples/kitchensink/src/lib.rs +++ b/frame/examples/kitchensink/src/lib.rs @@ -192,6 +192,7 @@ pub mod pallet { } } + /// Allows you to define how `genesis_configuration is built. #[pallet::genesis_build] impl GenesisBuild for GenesisConfig { fn build(&self) { @@ -199,6 +200,8 @@ pub mod pallet { } } + /// The call declaration. This states the entry points that we handle. The + /// macro takes care of the marshalling of arguments and dispatch. #[pallet::call] impl Pallet { #[pallet::call_index(0)] From 65489f59be3c472fb8d1f9681226ad8f5b953ef4 Mon Sep 17 00:00:00 2001 From: command-bot Date: Tue, 6 Jun 2023 09:13:28 +0000 Subject: [PATCH 17/17] ".git/.scripts/commands/fmt/fmt.sh" --- frame/examples/kitchensink/src/lib.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/frame/examples/kitchensink/src/lib.rs b/frame/examples/kitchensink/src/lib.rs index 08bf327f00886..c60e126181a95 100644 --- a/frame/examples/kitchensink/src/lib.rs +++ b/frame/examples/kitchensink/src/lib.rs @@ -192,7 +192,7 @@ pub mod pallet { } } - /// Allows you to define how `genesis_configuration is built. + /// Allows you to define how `genesis_configuration is built. #[pallet::genesis_build] impl GenesisBuild for GenesisConfig { fn build(&self) { @@ -234,10 +234,7 @@ pub mod pallet { SomethingHappened(u32), /// A simple struct-style variant. Note that we use `AccountId` from `T` because `T: /// Config`, which by extension implies `T: frame_system::Config`. - SomethingDetailedHappened { - at: u32, - to: T::AccountId, - }, + SomethingDetailedHappened { at: u32, to: T::AccountId }, /// Another variant. SomeoneJoined(T::AccountId), } @@ -291,7 +288,7 @@ pub mod pallet { } } - /// Allows you to define an enum on the pallet which will then instruct + /// Allows you to define an enum on the pallet which will then instruct /// `construct_runtime` to amalgamate all similarly-named enums from other /// pallets into an aggregate enum. #[pallet::composite_enum]