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

[FRAME Core] Default Pallet Config Trait / derive_impl #13454

Merged
merged 124 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
124 commits
Select commit Hold shift + click to select a range
83ebbbe
first draft, probably won't work
kianenigma Feb 15, 2023
8597691
first draft, probably won't work
kianenigma Feb 15, 2023
8090642
good progress..
kianenigma Feb 15, 2023
6294102
good milestone, still a lot to do.
kianenigma Feb 21, 2023
a0a6423
EVERYTHING WORKS
kianenigma Feb 23, 2023
749df49
Update frame/support/procedural/src/derive_impl.rs
kianenigma Mar 7, 2023
0777136
Update frame/support/procedural/src/derive_impl.rs
kianenigma Mar 7, 2023
7d5d50f
clean up + cargo fmt
sam0x17 Mar 7, 2023
6c6f8c5
import tokens WIP
sam0x17 Mar 8, 2023
9abec0a
export_tokens working with impl Trait
sam0x17 Mar 13, 2023
930cc25
WIP / notes
sam0x17 Mar 14, 2023
01c88ad
use macro_magic 0.2.0's export_tokens to access foreign items
sam0x17 Mar 24, 2023
4b481be
token importing working properly using macro_magic 0.2.5
sam0x17 Mar 28, 2023
4ff18f4
combine_impls almost working
sam0x17 Mar 28, 2023
cd0753c
successfully get foreign path via macro_magic 0.2.6
sam0x17 Mar 29, 2023
07d80f5
combine_impls using implementing_type generics
sam0x17 Mar 29, 2023
7dc8022
working + clean up
sam0x17 Mar 29, 2023
c8924cc
more clean up
sam0x17 Mar 29, 2023
b409576
decrease rightwards drift and add docs to combine_impls
sam0x17 Mar 29, 2023
d72da33
add support for macros to impl_item_ident in case we hit that
sam0x17 Mar 29, 2023
d27b824
add docs for impl_item_ident method
sam0x17 Mar 29, 2023
3e4c923
fix no_std issues
sam0x17 Mar 30, 2023
d178975
re-export of macro_magic working in pallets :tada:
sam0x17 Mar 30, 2023
046b0b8
clean up + fully resolve no_std issue with macro_magic with v0.2.11
sam0x17 Mar 31, 2023
e3566dd
remove trait item code for different trait item types since this
sam0x17 Mar 31, 2023
bd68211
clean up
sam0x17 Apr 3, 2023
dce288d
remove dev comments
sam0x17 Apr 5, 2023
e061bbb
only generate default trait if #[pallet::default_trait] is attached
sam0x17 Apr 5, 2023
8b8a11e
authorship and most other pallets now compiling
sam0x17 Apr 6, 2023
271186a
compiling :tada:
sam0x17 Apr 6, 2023
48506f7
add check for more than two pallet attributes on Config trait
sam0x17 Apr 6, 2023
d5bda5f
remove unused import in nomination-pool
sam0x17 Apr 6, 2023
7fc7665
clean up debug code
sam0x17 Apr 6, 2023
04fd313
Merge remote-tracking branch 'origin/master' into kiz-simpkified-runtime
sam0x17 Apr 6, 2023
9e31858
upgrade to macro_magic v0.2.12
sam0x17 Apr 6, 2023
5b8019b
add neater #[register_default_config(SomeIdent)] macro
sam0x17 Apr 7, 2023
b7a5a3f
Merge remote-tracking branch 'origin/master' into kiz-simpkified-runtime
sam0x17 Apr 18, 2023
ef9d107
upgrade to macro_magic 0.3.1
sam0x17 Apr 18, 2023
bc3237c
rewrite parsing to be compatible with syn 2.x, compiling :tada:
sam0x17 Apr 19, 2023
d17c807
remove unused keywords
sam0x17 Apr 19, 2023
4d089e5
macro stubs for the new pallet:: macros, preliminary docs
sam0x17 Apr 22, 2023
879de64
upgrade to macro_magic v0.3.2
sam0x17 Apr 22, 2023
d697ee1
rename register_default_config => register_default_impl
sam0x17 Apr 25, 2023
f6c232e
bump to macro_magic v0.3.3
sam0x17 Apr 26, 2023
727a4e7
custom disambiguation_path working as 2nd arg to derive_impl
sam0x17 Apr 26, 2023
df3b5ce
overhaul docs
sam0x17 Apr 26, 2023
4a33e99
fixes, ident-style paths shortcut working
sam0x17 Apr 27, 2023
a9413d3
remove ident-style shortcut because it makes testing difficult
sam0x17 Apr 27, 2023
ec34f58
add passing UI tests for derive_impl
sam0x17 Apr 27, 2023
52152a5
switch to `ForeignPath as DisambiguationPath` syntax + update docs
sam0x17 Apr 27, 2023
e191aa9
add UI test for bad foreign path
sam0x17 Apr 28, 2023
cc9ee82
add UI test for bad disambiguation path
sam0x17 Apr 28, 2023
5119cf5
add UI test for missing disambiguation path
sam0x17 Apr 28, 2023
2fd5cc5
add UI test for attached to non impl
sam0x17 Apr 28, 2023
814176e
fix derive_impl_attr_args_parsing test
sam0x17 Apr 28, 2023
d4cf31e
move tests to bottom
sam0x17 Apr 28, 2023
53bfe6d
fix nightly issue
sam0x17 Apr 28, 2023
d80bbdd
Merge remote-tracking branch 'origin/master' into kiz-simpkified-runtime
Apr 28, 2023
9bf5328
add doc notes on importing/re-exporting
sam0x17 Apr 28, 2023
97de112
remove explicit use of macro_magic::use_attr
sam0x17 Apr 28, 2023
194da7f
use explicit macro_magic::use_attr
sam0x17 Apr 28, 2023
4dd4440
remove unneeded {}
sam0x17 Apr 28, 2023
e32e8a1
remove unneeded collect
sam0x17 Apr 28, 2023
f90f13d
add docs for TestDefaultConfig
sam0x17 Apr 28, 2023
edfc83b
remove unneeded `#[export_tokens]` on `DefaultConfig`
sam0x17 Apr 28, 2023
efd9222
add docs for auto-generated `DefaultConfig`
sam0x17 Apr 28, 2023
78ac0d5
no need to clone
sam0x17 Apr 28, 2023
1dae299
clean up combine_impls + compiling again
sam0x17 Apr 28, 2023
faa8dce
remove unused dependency
sam0x17 Apr 28, 2023
8374ed8
simplify struct definition
sam0x17 Apr 28, 2023
8b22ea6
fix register_default_impl docs
sam0x17 May 2, 2023
d7f3f6a
reduce rightward drift / refactor
sam0x17 May 2, 2023
5b90575
fix derive_impl after keith's changes
sam0x17 May 2, 2023
afae21c
simplify disambiguation_path calculation
sam0x17 May 2, 2023
f784efc
compiling again
sam0x17 May 3, 2023
763a93a
simplify parsing of trait item
sam0x17 May 3, 2023
16b4d5a
rename preludes => prelude
sam0x17 May 3, 2023
bc688f9
fix other places where we used preludes instead of prelude
sam0x17 May 3, 2023
8077b7b
fix indents
sam0x17 May 3, 2023
a399ecd
simplify PalletAttr parsing
sam0x17 May 3, 2023
c825367
go back to having no_default and constant as keywords
sam0x17 May 3, 2023
b373b52
make it more clear that disambiguation_path is optional
sam0x17 May 3, 2023
af16dac
make default_trait_items just a Vec instead of Option<Vec>
sam0x17 May 4, 2023
ce06d17
rename foreign_path => default_impl_path within substrate
sam0x17 May 4, 2023
e7c9056
fix docs
sam0x17 May 4, 2023
655657e
Change {} to ;
sam0x17 May 4, 2023
9bcf5e8
highlight full end-to-end example with link
sam0x17 May 4, 2023
4449600
add pallet-default-config-example, start by copying dev mode code
sam0x17 May 12, 2023
238dc9c
update dev-mode specific docs
sam0x17 May 17, 2023
7a7f74e
use Person and Points instead of Dummy and Bar
sam0x17 May 17, 2023
07a0555
add docs to example pallet
sam0x17 May 17, 2023
a99aca0
Merge remote-tracking branch 'origin/master' into kiz-simpkified-runtime
sam0x17 May 17, 2023
cc7bd84
revert changes to pallets other than the default config example
sam0x17 May 18, 2023
b4b7de8
fix outdated references to basic example pallet
sam0x17 May 18, 2023
13d5ab4
re-order docs to be a bit more clear
sam0x17 May 18, 2023
b9113c4
better errors for extra attributes
sam0x17 May 18, 2023
9d4cce6
add UI tests for duplicate/extra attributes on trait items
sam0x17 May 19, 2023
167cbbd
change `#[pallet::default_config]` to option on `#[pallet::config()]`
sam0x17 May 22, 2023
c8640d4
add note about new optional conventions
sam0x17 May 23, 2023
bb4f8fd
improve docs about `DefaultConfig` and link to these from a few places
sam0x17 May 23, 2023
1029ff6
fix doc comment
sam0x17 May 23, 2023
86b649c
fix old comment referencing `pallet::default_config`
sam0x17 May 23, 2023
fc7b8aa
use u32 instead of u64 for block number
sam0x17 May 24, 2023
09c30c1
use () instead of u32 for `AccountData`
sam0x17 May 24, 2023
0b21ac1
use ConstU32<10> for BlockHashCount instead of ConstU64<10>
sam0x17 May 24, 2023
144c09d
people are not dummies
sam0x17 May 24, 2023
d86021e
fix wording
sam0x17 May 24, 2023
db8587f
Person => People and compiling again
sam0x17 May 24, 2023
4ffda9b
add docs for `prelude` module in frame_system
sam0x17 May 24, 2023
0decc58
Merge remote-tracking branch 'origin/master' into kiz-simpkified-runtime
sam0x17 May 24, 2023
2f8f4a5
update Cargo.lock
sam0x17 May 24, 2023
ba426b2
Merge branch 'master' of github.com:paritytech/substrate into kiz-sim…
kianenigma May 26, 2023
7356072
cleaner example
kianenigma May 26, 2023
85f20d0
tweaks
kianenigma May 26, 2023
19540f6
update docs more
kianenigma May 26, 2023
b36a086
update docs more
kianenigma May 26, 2023
68b76d2
update docs more
kianenigma May 26, 2023
298352b
update docs more
kianenigma May 26, 2023
e141483
fix ui tests
kianenigma May 28, 2023
4b6fe4a
err
kianenigma May 28, 2023
d3d7d93
Merge remote-tracking branch 'origin/master' into kiz-simpkified-runtime
May 29, 2023
9ee1b09
Update frame/support/test/tests/pallet_ui.rs
kianenigma May 29, 2023
41da6cd
Master.into()
kianenigma May 29, 2023
638c1fa
update ui tests
kianenigma May 29, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ members = [
"frame/examples/basic",
"frame/examples/offchain-worker",
"frame/examples/dev-mode",
"frame/examples/default-config",
"frame/executive",
"frame/nis",
"frame/grandpa",
Expand Down
38 changes: 38 additions & 0 deletions frame/examples/default-config/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[package]
name = "pallet-default-config-example"
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 pallet demonstrating derive_impl / default_config in action"
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 = { default-features = false, path = "../../support" }
frame-system = { default-features = false, path = "../../system" }

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

[features]
default = ["std"]
std = [
"codec/std",
"frame-support/std",
"frame-system/std",
"log/std",
"scale-info/std",
"sp-io/std",
"sp-runtime/std",
"sp-std/std",
]
try-runtime = ["frame-support/try-runtime"]
8 changes: 8 additions & 0 deletions frame/examples/default-config/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Default Config Example Pallet

An example pallet demonstrating the ability to derive default testing configs via
`#[derive_impl]` and `#[pallet::config(with_default)]`.

Run `cargo doc --package pallet-default-config-example --open` to view this pallet's documentation.

License: MIT-0
210 changes: 210 additions & 0 deletions frame/examples/default-config/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
// 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.

//! # Default Config Pallet Example
//!
//! A simple example of a FRAME pallet that utilizes [`frame_support::derive_impl`] to demonstrate
//! the simpler way to implement `Config` trait of pallets. This example only showcases this in a
//! `mock.rs` environment, but the same applies to a real runtime as well.
//!
//! See the source code of [`tests`] for a real examples.
//!
//! Study the following types:
//!
//! - [`pallet::DefaultConfig`], and how it differs from [`pallet::Config`].
//! - [`pallet::config_preludes::TestDefaultConfig`] and how it implements
//! [`pallet::DefaultConfig`].
//! - Notice how [`pallet::DefaultConfig`] is independent of [`frame_system::Config`].

#![cfg_attr(not(feature = "std"), no_std)]

#[frame_support::pallet]
pub mod pallet {
use frame_support::pallet_prelude::*;

/// This pallet is annotated to have a default config. This will auto-generate
/// [`DefaultConfig`].
///
/// It will be an identical, but won't have anything that is `#[pallet::no_default]`.
#[pallet::config(with_default)]
pub trait Config: frame_system::Config {
/// The overarching event type. This is coming from the runtime, and cannot have a default.
/// In general, `Runtime*`-oriented types cannot have a sensible default.
#[pallet::no_default] // optional. `RuntimeEvent` is automatically excluded as well.
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
kianenigma marked this conversation as resolved.
Show resolved Hide resolved
sam0x17 marked this conversation as resolved.
Show resolved Hide resolved

/// An input parameter to this pallet. This value can have a default, because it is not
/// reliant on `frame_system::Config` or the overarching runtime in any way.
type WithDefaultValue: Get<u32>;

/// Same as [`Config::WithDefaultValue`], but we don't intend to define a default for this
/// in our tests below.
type OverwrittenDefaultValue: Get<u32>;

/// An input parameter that relies on `<Self as frame_system::Config>::AccountId`. As of
/// now, such types cannot have defaults and need to be annotated as such, iff
/// `#[pallet::config(with_default)]` is enabled:
#[pallet::no_default]
type CannotHaveDefault: Get<Self::AccountId>;

/// Something that is a normal type, with default.
type WithDefaultType;

/// Same as [`Config::WithDefaultType`], but we don't intend to define a default for this
/// in our tests below.
type OverwrittenDefaultType;
}

/// Container for different types that implement [`DefaultConfig`]` of this pallet.
pub mod config_preludes {
// This will help use not need to disambiguate anything when using `derive_impl`.
use super::*;

/// A type providing default configurations for this pallet in testing environment.
pub struct TestDefaultConfig;
#[frame_support::register_default_impl(TestDefaultConfig)]
impl DefaultConfig for TestDefaultConfig {
type WithDefaultValue = frame_support::traits::ConstU32<42>;
type OverwrittenDefaultValue = frame_support::traits::ConstU32<42>;

type WithDefaultType = u32;
type OverwrittenDefaultType = u32;
}

/// A type providing default configurations for this pallet in a parachain environment.
pub struct ParachainDefaultConfig;
#[frame_support::register_default_impl(ParachainDefaultConfig)]
impl DefaultConfig for ParachainDefaultConfig {
type WithDefaultValue = frame_support::traits::ConstU32<66>;
type OverwrittenDefaultValue = frame_support::traits::ConstU32<66>;
type WithDefaultType = u32;
type OverwrittenDefaultType = u32;
}
}

#[pallet::pallet]
pub struct Pallet<T>(_);

#[pallet::event]
pub enum Event<T: Config> {}
}

#[cfg(any(test, doc))]
pub mod tests {
use super::*;

use frame_support::macro_magic::use_attr;
// Because `derive_impl` is a [macro_magic](https://crates.io/crates/macro_magic) attribute
// macro, [`#[use_attr]`](`frame_support::macro_magic::use_attr`) must be attached to any use
// statement that brings it into scope.
#[use_attr]
use frame_support::derive_impl;

use super::pallet as pallet_default_config_example;

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

frame_support::construct_runtime!(
pub enum Test where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic,
{
System: frame_system,
DefaultPallet: pallet_default_config_example,
}
);

#[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This as DefaultConfig syntax still confuses me, in the sense that I have no idea why it's necessary and what it does. It would just seem to me that most of time, you can just omit it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

impl frame_system::Config for Test {
// these items are defined by frame-system as `no_default`, so we must specify them here.
// Note that these are types that actually rely on the outer runtime, and can't sensibly
// have an _independent_ default.
type BaseCallFilter = frame_support::traits::Everything;
type RuntimeOrigin = RuntimeOrigin;
type RuntimeCall = RuntimeCall;
type RuntimeEvent = RuntimeEvent;
type PalletInfo = PalletInfo;
type OnSetCode = ();

// all of this is coming from `frame_system::config_preludes::TestDefaultConfig`.

// type Index = u32;
// type BlockNumber = u32;
// type Header = sp_runtime::generic::Header<Self::BlockNumber, Self::Hashing>;
// type Hash = sp_core::hash::H256;
// type Hashing = sp_runtime::traits::BlakeTwo256;
// type AccountId = u64;
// type Lookup = sp_runtime::traits::IdentityLookup<u64>;
// type BlockHashCount = frame_support::traits::ConstU32<10>;
// type MaxConsumers = frame_support::traits::ConstU32<16>;
// type AccountData = ();
// type OnNewAccount = ();
// type OnKilledAccount = ();
// type SystemWeightInfo = ();
// type SS58Prefix = ();
// type Version = ();
// type BlockWeights = ();
// type BlockLength = ();
// type DbWeight = ();

// you could still overwrite any of them if desired.
type SS58Prefix = frame_support::traits::ConstU16<456>;
}

// Similarly, we use the defaults provided by own crate as well.
use pallet::config_preludes::TestDefaultConfig;
#[derive_impl(TestDefaultConfig as pallet::DefaultConfig)]
impl crate::pallet::Config for Test {
// These two both cannot have defaults.
type RuntimeEvent = RuntimeEvent;
// Note that the default account-id type in
// `frame_system::config_preludes::TestDefaultConfig` is `u64`.
type CannotHaveDefault = frame_support::traits::ConstU64<1>;

type OverwrittenDefaultValue = frame_support::traits::ConstU32<678>;
type OverwrittenDefaultType = u128;
}

#[test]
fn it_works() {
use frame_support::traits::Get;
use pallet::{Config, DefaultConfig};

// assert one of the value types that is not overwritten.
assert_eq!(
<<Test as Config>::WithDefaultValue as Get<u32>>::get(),
<<TestDefaultConfig as DefaultConfig>::WithDefaultValue as Get<u32>>::get()
);

// assert one of the value types that is overwritten.
assert_eq!(<<Test as Config>::OverwrittenDefaultValue as Get<u32>>::get(), 678u32);

// assert one of the types that is not overwritten.
assert_eq!(
std::any::TypeId::of::<<Test as Config>::WithDefaultType>(),
std::any::TypeId::of::<<TestDefaultConfig as DefaultConfig>::WithDefaultType>()
);

// assert one of the types that is overwritten.
assert_eq!(
std::any::TypeId::of::<<Test as Config>::OverwrittenDefaultType>(),
std::any::TypeId::of::<u128>()
)
}
}
1 change: 1 addition & 0 deletions frame/support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ sp-staking = { version = "4.0.0-dev", default-features = false, path = "../../pr
sp-weights = { version = "5.0.0", default-features = false, path = "../../primitives/weights" }
sp-debug-derive = { default-features = false, path = "../../primitives/debug-derive" }
tt-call = "1.0.8"
macro_magic = "0.3.3"
frame-support-procedural = { version = "4.0.0-dev", default-features = false, path = "./procedural" }
paste = "1.0"
once_cell = { version = "1", default-features = false, optional = true }
Expand Down
1 change: 1 addition & 0 deletions frame/support/procedural/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ quote = "1.0.26"
syn = { version = "2.0.16", features = ["full"] }
frame-support-procedural-tools = { version = "4.0.0-dev", path = "./tools" }
proc-macro-warning = { version = "0.4.1", default-features = false }
macro_magic = { version = "0.3.3", features = ["proc_support"] }

[features]
default = ["std"]
Expand Down
2 changes: 1 addition & 1 deletion frame/support/procedural/src/construct_runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
//! `::{Call, ...}` or implicitly.
//!
//! In case a pallet defines its parts implicitly, then the pallet must provide the
//! `tt_default_parts` macro. `construct_rutime` will generate some code which utilizes `tt_call`
//! `tt_default_parts` macro. `construct_runtime` will generate some code which utilizes `tt_call`
//! to call the `tt_default_parts` macro of the pallet. `tt_default_parts` will then return the
//! default pallet parts as input tokens to the `match_and_replace` macro, which ultimately
//! generates a call to `construct_runtime` again, this time with all the pallet parts explicitly
Expand Down
Loading