This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
[FRAME Core] Adds ability to split a pallet across multiple files #13950
Merged
Merged
Changes from all commits
Commits
Show all changes
68 commits
Select commit
Hold shift + click to select a range
01513b9
Initial setup
gupnik 063ed10
Updates macro_magic version and refactors accordingly
gupnik c1a1342
Removes unwrap from macro
gupnik 6fe645b
Splits into multiple sections
gupnik b582bd0
Merge branch 'master' of github.com:paritytech/substrate into gupnik/…
gupnik e403600
Uses call_site to fix macro hygiene issue
gupnik 666f548
Initial setup
gupnik 7bee41a
Removes unnecessary changes
gupnik 2f9a178
Moves template palet back
gupnik 6e18ed6
Updates cargo.lock
gupnik 93d1db4
Merge branch 'master' of github.com:paritytech/substrate into gupnik/…
gupnik edfe22b
Moves BagsList inside mod
gupnik 9ced318
Comments access to internal functions for now
gupnik d3f8f24
Updates tests
gupnik 29c54c1
Uncomments code
gupnik 8b4ca57
Fixes test
gupnik dd119f9
Moves bags-list to separate crate
gupnik d4b1969
Initial setup
gupnik 596cff5
Removes bags-list changes
gupnik 5d7dd02
Merge branch 'gupnik/pallet-example-split' of github.com:paritytech/s…
gupnik 03f639c
Fix structure
gupnik a00bd76
Minor update
gupnik 2dda8bf
Addresses review comment
gupnik d0f83d1
Adds a couple of UI tests. More to be added
gupnik 67cccae
Adds err files
gupnik fb8d770
Adds test for no pallet
gupnik da28299
Merge branch 'master' of github.com:paritytech/substrate into gupnik/…
gupnik 58ae2a1
Adds doc
gupnik 56888af
Updates versions
gupnik 7d34a97
Adds benchmarking
gupnik 0ce4726
Updates doc link
gupnik c604737
".git/.scripts/commands/fmt/fmt.sh"
b83b886
Minor update
gupnik 38f9fc3
Merge branch 'gupnik/pallet-split' of github.com:paritytech/substrate…
gupnik 32a06f1
Merge branch 'master' of github.com:paritytech/substrate into gupnik/…
gupnik 4896f97
Adds missing changes
gupnik c72d3fc
".git/.scripts/commands/fmt/fmt.sh"
8d4c528
Update frame/support/procedural/src/lib.rs
gupnik bdb17e7
Addresses review comments
gupnik 29b6ba4
Merge branch 'gupnik/pallet-split' of github.com:paritytech/substrate…
gupnik eead306
Addresses review comments
gupnik 5f950ad
".git/.scripts/commands/fmt/fmt.sh"
8e83b2b
Merge branch 'master' of github.com:paritytech/substrate into gupnik/…
gupnik 6fc2ff9
Update frame/support/procedural/src/lib.rs
gupnik a230ef1
Update frame/support/procedural/src/lib.rs
gupnik 39be12e
Update frame/support/procedural/src/lib.rs
gupnik 3db7f3f
Adds UI test for disambiguation
gupnik d14fb1a
".git/.scripts/commands/fmt/fmt.sh"
8c44128
Makes clippy happy
gupnik 5e72737
".git/.scripts/commands/fmt/fmt.sh"
ac9fd8d
Fixes frame support test
gupnik 04999d6
Merge branch 'gupnik/pallet-split' of github.com:paritytech/substrate…
gupnik dc297c1
Fixes frame support test
gupnik 64f464b
Split items other than storage
gupnik 800794a
Merge branch 'master' of github.com:paritytech/substrate into gupnik/…
gupnik f3cd005
Updates versions
gupnik e52690a
Merge branch 'master' of github.com:paritytech/substrate into gupnik/…
gupnik 0c444ce
Fixes some review comments
gupnik 5219370
Addresses review comments
gupnik 72d4204
".git/.scripts/commands/fmt/fmt.sh"
17b9cdb
Updates docs
gupnik 125b5dc
Adds experimental disclaimer
gupnik 3fa8d5c
".git/.scripts/commands/fmt/fmt.sh"
ebf806d
Update frame/support/test/tests/split_ui/no_section_found.rs
gupnik 04aefd2
Addresses review comments
gupnik 0ee6184
Merge branch 'gupnik/pallet-split' of github.com:paritytech/substrate…
gupnik 90ff1bc
Merge branch 'master' of github.com:paritytech/substrate into gupnik/…
gupnik dd96d1a
Fixes test
gupnik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
[package] | ||
name = "pallet-example-split" | ||
version = "4.0.0-dev" | ||
authors = ["Parity Technologies <[email protected]>"] | ||
edition = "2021" | ||
license = "MIT-0" | ||
homepage = "https://substrate.io" | ||
repository = "https://github.com/paritytech/substrate/" | ||
description = "FRAME example splitted 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-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" } | ||
|
||
[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-std/std", | ||
|
||
"frame-benchmarking?/std", | ||
] | ||
runtime-benchmarks = ["frame-benchmarking/runtime-benchmarks"] | ||
try-runtime = ["frame-support/try-runtime"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<!-- markdown-link-check-disable --> | ||
# Basic Example For Splitting A Pallet | ||
A simple example of a FRAME pallet demonstrating the ability to split sections across multiple | ||
files. | ||
|
||
Note that this is purely experimental at this point. | ||
|
||
Run `cargo doc --package pallet-example-split --open` to view this pallet's documentation. | ||
|
||
License: MIT-0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// 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 setup for pallet-example-split | ||
|
||
// Only enable this module for benchmarking. | ||
#![cfg(feature = "runtime-benchmarks")] | ||
use super::*; | ||
|
||
#[allow(unused)] | ||
use crate::Pallet as Template; | ||
use frame_benchmarking::v2::*; | ||
use frame_system::RawOrigin; | ||
|
||
#[benchmarks] | ||
mod benchmarks { | ||
use super::*; | ||
|
||
#[benchmark] | ||
fn do_something() { | ||
let value = 100u32.into(); | ||
let caller: T::AccountId = whitelisted_caller(); | ||
#[extrinsic_call] | ||
do_something(RawOrigin::Signed(caller), value); | ||
|
||
assert_eq!(Something::<T>::get(), Some(value)); | ||
} | ||
|
||
#[benchmark] | ||
fn cause_error() { | ||
Something::<T>::put(100u32); | ||
let caller: T::AccountId = whitelisted_caller(); | ||
#[extrinsic_call] | ||
cause_error(RawOrigin::Signed(caller)); | ||
|
||
assert_eq!(Something::<T>::get(), Some(101u32)); | ||
} | ||
|
||
impl_benchmark_test_suite!(Template, crate::mock::new_test_ext(), crate::mock::Test); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// 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. | ||
|
||
use frame_support::pallet_macros::*; | ||
|
||
/// A [`pallet_section`] that defines the events for a pallet. | ||
/// This can later be imported into the pallet using [`import_section`]. | ||
#[pallet_section] | ||
mod events { | ||
#[pallet::event] | ||
#[pallet::generate_deposit(pub(super) fn deposit_event)] | ||
pub enum Event<T: Config> { | ||
/// Event documentation should end with an array that provides descriptive names for event | ||
/// parameters. [something, who] | ||
SomethingStored { something: u32, who: T::AccountId }, | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
|
||
//! # Split Example Pallet | ||
//! | ||
//! **This pallet serves as an example and is not meant to be used in production.** | ||
//! | ||
//! A FRAME pallet demonstrating the ability to split sections across multiple files. | ||
//! | ||
//! Note that this is purely experimental at this point. | ||
|
||
#![cfg_attr(not(feature = "std"), no_std)] | ||
gupnik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Re-export pallet items so that they can be accessed from the crate namespace. | ||
pub use pallet::*; | ||
|
||
#[cfg(test)] | ||
mod mock; | ||
|
||
#[cfg(test)] | ||
mod tests; | ||
|
||
#[cfg(feature = "runtime-benchmarks")] | ||
mod benchmarking; | ||
mod events; | ||
|
||
pub mod weights; | ||
pub use weights::*; | ||
|
||
use frame_support::pallet_macros::*; | ||
|
||
/// Imports a [`pallet_section`] defined at [`events::events`]. | ||
/// This brings the events defined in that section into the pallet's namespace. | ||
#[import_section(events::events)] | ||
#[frame_support::pallet] | ||
pub mod pallet { | ||
use super::*; | ||
use frame_support::pallet_prelude::*; | ||
use frame_system::pallet_prelude::*; | ||
|
||
#[pallet::pallet] | ||
pub struct Pallet<T>(_); | ||
|
||
/// Configure the pallet by specifying the parameters and types on which it depends. | ||
#[pallet::config] | ||
pub trait Config: frame_system::Config { | ||
/// Because this pallet emits events, it depends on the runtime's definition of an event. | ||
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>; | ||
/// Type representing the weight of this pallet | ||
type WeightInfo: WeightInfo; | ||
} | ||
|
||
// The pallet's runtime storage items. | ||
#[pallet::storage] | ||
pub type Something<T> = StorageValue<_, u32>; | ||
|
||
// Errors inform users that something went wrong. | ||
#[pallet::error] | ||
pub enum Error<T> { | ||
/// Error names should be descriptive. | ||
NoneValue, | ||
/// Errors should have helpful documentation associated with them. | ||
StorageOverflow, | ||
} | ||
|
||
// Dispatchable functions allows users to interact with the pallet and invoke state changes. | ||
// These functions materialize as "extrinsics", which are often compared to transactions. | ||
// Dispatchable functions must be annotated with a weight and must return a DispatchResult. | ||
#[pallet::call] | ||
impl<T: Config> Pallet<T> { | ||
/// An example dispatchable that takes a singles value as a parameter, writes the value to | ||
/// storage and emits an event. This function must be dispatched by a signed extrinsic. | ||
#[pallet::call_index(0)] | ||
#[pallet::weight(T::WeightInfo::do_something())] | ||
pub fn do_something(origin: OriginFor<T>, something: u32) -> DispatchResult { | ||
// Check that the extrinsic was signed and get the signer. | ||
// This function will return an error if the extrinsic is not signed. | ||
let who = ensure_signed(origin)?; | ||
|
||
// Update storage. | ||
<Something<T>>::put(something); | ||
|
||
// Emit an event. | ||
Self::deposit_event(Event::SomethingStored { something, who }); | ||
// Return a successful DispatchResultWithPostInfo | ||
Ok(()) | ||
} | ||
|
||
/// An example dispatchable that may throw a custom error. | ||
#[pallet::call_index(1)] | ||
#[pallet::weight(T::WeightInfo::cause_error())] | ||
pub fn cause_error(origin: OriginFor<T>) -> DispatchResult { | ||
let _who = ensure_signed(origin)?; | ||
|
||
// Read a value from storage. | ||
match <Something<T>>::get() { | ||
// Return an error if the value has not been set. | ||
None => return Err(Error::<T>::NoneValue.into()), | ||
Some(old) => { | ||
// Increment the value read from storage; will error in the event of overflow. | ||
let new = old.checked_add(1).ok_or(Error::<T>::StorageOverflow)?; | ||
// Update the value in storage with the incremented result. | ||
<Something<T>>::put(new); | ||
Ok(()) | ||
}, | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,56 @@ | ||||
// 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. | ||||
|
||||
use crate as pallet_template; | ||||
use frame_support::derive_impl; | ||||
|
||||
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>; | ||||
type Block = frame_system::mocking::MockBlock<Test>; | ||||
|
||||
// Configure a mock runtime to test the pallet. | ||||
frame_support::construct_runtime!( | ||||
pub enum Test where | ||||
Block = Block, | ||||
NodeBlock = Block, | ||||
UncheckedExtrinsic = UncheckedExtrinsic, | ||||
{ | ||||
System: frame_system, | ||||
TemplatePallet: pallet_template, | ||||
} | ||||
); | ||||
|
||||
/// 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 { | ||||
gupnik marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
type BaseCallFilter = frame_support::traits::Everything; | ||||
type RuntimeOrigin = RuntimeOrigin; | ||||
type RuntimeCall = RuntimeCall; | ||||
type RuntimeEvent = RuntimeEvent; | ||||
type PalletInfo = PalletInfo; | ||||
type OnSetCode = (); | ||||
} | ||||
|
||||
impl pallet_template::Config for Test { | ||||
type RuntimeEvent = RuntimeEvent; | ||||
type WeightInfo = (); | ||||
} | ||||
|
||||
// Build genesis storage according to the mock runtime. | ||||
pub fn new_test_ext() -> sp_io::TestExternalities { | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. isn't For now, you can add and re-export it here substrate/frame/support/src/lib.rs Line 1529 in ae1a608
(possibly a follow-up, related to #14137) |
||||
frame_system::GenesisConfig::default().build_storage::<Test>().unwrap().into() | ||||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.