Automatic pallet parts in construct_runtime (#9681)

* implement automatic parts

* ui tests

* rename

* remove unnecessary exclude

* better doc

* better doc

* fix genesis config

* fix UI tests

* fix UI test

* Revert "fix UI test"

This reverts commit a910351c0b24cfe42195cfd97d83a416640e3259.

* implemented used_parts

* Update frame/support/procedural/src/construct_runtime/mod.rs

Co-authored-by: Keith Yeung <kungfukeith11@gmail.com>

* doc + fmt

* Update frame/support/procedural/src/construct_runtime/parse.rs

Co-authored-by: Keith Yeung <kungfukeith11@gmail.com>

* add doc in the macro

* remove yet some more parts

* fix ui test

* more determnistic error message + fix ui tests

* fix ui test

* Apply suggestions from code review

Co-authored-by: Keith Yeung <kungfukeith11@gmail.com>

* do refactor + fix ui tests

* fmt

* fix test

* fix test

* fix ui test

* Apply suggestions from code review

Co-authored-by: Keith Yeung <kungfukeith11@gmail.com>

* refactor

* remove even more part in node-runtime

* fix test

* Add flow chart for the construct_runtime! execution flow

* Fix typo

* Ignore snippets that don't contain code

* Refactor some code in expand_after

* Rename expand_after to match_and_insert

* cargo fmt

* Fix rename

* Remove frame_support argument to construct_runtime_parts

* Make use of tt-call to simplify intermediate expansions

* cargo fmt

* Update match_and_insert documentation

* Reset cursor to 0 when no matching patterns are found

* Reorder struct fields on MatchAndInsertDef

* Add test for dependency renames and fix frame-support import

* Add more doc comments

* Update frame/support/test/compile_pass/src/lib.rs

Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>

Co-authored-by: Keith Yeung <kungfukeith11@gmail.com>
Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>
This commit is contained in:
Guillaume Thiolliere
2021-10-31 14:55:10 +01:00
committed by GitHub
parent 0214fde9a6
commit 4292e18e50
31 changed files with 1340 additions and 223 deletions
@@ -0,0 +1,33 @@
use frame_support::construct_runtime;
use sp_runtime::{generic, traits::BlakeTwo256};
use sp_core::sr25519;
#[frame_support::pallet]
mod pallet {
#[pallet::config]
pub trait Config: frame_system::Config {}
#[pallet::pallet]
pub struct Pallet<T>(_);
}
pub type Signature = sr25519::Signature;
pub type BlockNumber = u64;
pub type Header = generic::Header<BlockNumber, BlakeTwo256>;
pub type Block = generic::Block<Header, UncheckedExtrinsic>;
pub type UncheckedExtrinsic = generic::UncheckedExtrinsic<u32, Call, Signature, ()>;
impl pallet::Config for Runtime {}
construct_runtime! {
pub enum Runtime where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic
{
System: system::{Pallet, Call, Storage, Config, Event<T>},
Pallet: pallet exclude_parts { Pallet } use_parts { Pallet },
}
}
fn main() {}
@@ -0,0 +1,28 @@
error: Unexpected tokens, expected one of `=`, `,`
--> $DIR/both_use_and_excluded_parts.rs:29:43
|
29 | Pallet: pallet exclude_parts { Pallet } use_parts { Pallet },
| ^^^^^^^^^
error[E0412]: cannot find type `Call` in this scope
--> $DIR/both_use_and_excluded_parts.rs:18:64
|
18 | pub type UncheckedExtrinsic = generic::UncheckedExtrinsic<u32, Call, Signature, ()>;
| ^^^^ not found in this scope
|
help: consider importing one of these items
|
1 | use crate::pallet::Call;
|
1 | use frame_support_test::Call;
|
1 | use frame_system::Call;
|
1 | use test_pallet::Call;
|
error[E0412]: cannot find type `Runtime` in this scope
--> $DIR/both_use_and_excluded_parts.rs:20:25
|
20 | impl pallet::Config for Runtime {}
| ^^^^^^^ not found in this scope
@@ -0,0 +1,13 @@
use frame_support::construct_runtime;
construct_runtime! {
pub enum Runtime where
UncheckedExtrinsic = UncheckedExtrinsic,
Block = Block,
NodeBlock = Block,
{
System: frame_system exclude_parts { Call, Call },
}
}
fn main() {}
@@ -0,0 +1,5 @@
error: `Call` was already declared before. Please remove the duplicate declaration
--> $DIR/duplicate_exclude.rs:9:46
|
9 | System: frame_system exclude_parts { Call, Call },
| ^^^^
@@ -0,0 +1,13 @@
use frame_support::construct_runtime;
construct_runtime! {
pub enum Runtime where
UncheckedExtrinsic = UncheckedExtrinsic,
Block = Block,
NodeBlock = Block,
{
System: frame_system exclude_part { Call },
}
}
fn main() {}
@@ -0,0 +1,5 @@
error: Unexpected tokens, expected one of `::$ident` `::{`, `exclude_parts`, `use_parts`, `=`, `,`
--> $DIR/exclude_missspell.rs:9:24
|
9 | System: frame_system exclude_part { Call },
| ^^^^^^^^^^^^
@@ -0,0 +1,38 @@
use frame_support::construct_runtime;
use sp_runtime::{generic, traits::BlakeTwo256};
use sp_core::sr25519;
#[frame_support::pallet]
mod pallet {
use frame_support::pallet_prelude::*;
#[pallet::config]
pub trait Config: frame_system::Config {}
#[pallet::pallet]
pub struct Pallet<T>(_);
#[pallet::storage]
type Foo<T> = StorageValue<Value=u8>;
}
pub type Signature = sr25519::Signature;
pub type BlockNumber = u64;
pub type Header = generic::Header<BlockNumber, BlakeTwo256>;
pub type Block = generic::Block<Header, UncheckedExtrinsic>;
pub type UncheckedExtrinsic = generic::UncheckedExtrinsic<u32, Call, Signature, ()>;
impl pallet::Config for Runtime {}
construct_runtime! {
pub enum Runtime where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic
{
System: system::{Pallet, Call, Storage, Config, Event<T>},
Pallet: pallet exclude_parts { Call },
}
}
fn main() {}
@@ -0,0 +1,28 @@
error: Invalid pallet part specified, the pallet `Pallet` doesn't have the `Call` part. Available parts are: `Pallet`, `Storage`.
--> $DIR/exclude_undefined_part.rs:34:34
|
34 | Pallet: pallet exclude_parts { Call },
| ^^^^
error[E0412]: cannot find type `Call` in this scope
--> $DIR/exclude_undefined_part.rs:23:64
|
23 | pub type UncheckedExtrinsic = generic::UncheckedExtrinsic<u32, Call, Signature, ()>;
| ^^^^ not found in this scope
|
help: consider importing one of these items
|
1 | use crate::pallet::Call;
|
1 | use frame_support_test::Call;
|
1 | use frame_system::Call;
|
1 | use test_pallet::Call;
|
error[E0412]: cannot find type `Runtime` in this scope
--> $DIR/exclude_undefined_part.rs:25:25
|
25 | impl pallet::Config for Runtime {}
| ^^^^^^^ not found in this scope
@@ -1,5 +1,5 @@
error: expected one of: identifier, curly braces, `<`
--> $DIR/invalid_module_details.rs:9:19
error: Unexpected tokens, expected one of `::$ident` `::{`, `exclude_parts`, `use_parts`, `=`, `,`
--> $DIR/invalid_module_details.rs:9:17
|
9 | system: System::(),
| ^^
| ^^
@@ -1,4 +1,4 @@
error: expected `::`
error: Unexpected tokens, expected one of `::$ident` `::{`, `exclude_parts`, `use_parts`, `=`, `,`
--> $DIR/invalid_token_after_module.rs:9:18
|
9 | system: System ?
@@ -0,0 +1,26 @@
use frame_support::construct_runtime;
mod pallet_old {
pub trait Config: frame_system::Config {}
decl_storage! {
trait Store for Module<T: Config> as Example {}
}
decl_module! {
pub struct Module<T: Config> for enum Call where origin: T::Origin {}
}
}
construct_runtime! {
pub enum Runtime where
UncheckedExtrinsic = UncheckedExtrinsic,
Block = Block,
NodeBlock = Block,
{
System: frame_system,
OldPallet: pallet_old,
}
}
fn main() {}
@@ -0,0 +1,31 @@
error[E0433]: failed to resolve: could not find `tt_default_parts` in `pallet_old`
--> $DIR/old_unsupported_pallet_decl.rs:15:1
|
15 | / construct_runtime! {
16 | | pub enum Runtime where
17 | | UncheckedExtrinsic = UncheckedExtrinsic,
18 | | Block = Block,
... |
23 | | }
24 | | }
| |_^ could not find `tt_default_parts` in `pallet_old`
|
= note: this error originates in the macro `construct_runtime` (in Nightly builds, run with -Z macro-backtrace for more info)
error: cannot find macro `decl_storage` in this scope
--> $DIR/old_unsupported_pallet_decl.rs:6:2
|
6 | decl_storage! {
| ^^^^^^^^^^^^
|
= note: consider importing this macro:
frame_support::decl_storage
error: cannot find macro `decl_module` in this scope
--> $DIR/old_unsupported_pallet_decl.rs:10:2
|
10 | decl_module! {
| ^^^^^^^^^^^
|
= note: consider importing this macro:
frame_support::decl_module
@@ -0,0 +1,38 @@
use frame_support::construct_runtime;
use sp_runtime::{generic, traits::BlakeTwo256};
use sp_core::sr25519;
#[frame_support::pallet]
mod pallet {
use frame_support::pallet_prelude::*;
#[pallet::config]
pub trait Config: frame_system::Config {}
#[pallet::pallet]
pub struct Pallet<T>(_);
#[pallet::storage]
type Foo<T> = StorageValue<Value=u8>;
}
pub type Signature = sr25519::Signature;
pub type BlockNumber = u64;
pub type Header = generic::Header<BlockNumber, BlakeTwo256>;
pub type Block = generic::Block<Header, UncheckedExtrinsic>;
pub type UncheckedExtrinsic = generic::UncheckedExtrinsic<u32, Call, Signature, ()>;
impl pallet::Config for Runtime {}
construct_runtime! {
pub enum Runtime where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic
{
System: system::{Pallet, Call, Storage, Config, Event<T>},
Pallet: pallet use_parts { Call },
}
}
fn main() {}
@@ -0,0 +1,28 @@
error: Invalid pallet part specified, the pallet `Pallet` doesn't have the `Call` part. Available parts are: `Pallet`, `Storage`.
--> $DIR/use_undefined_part.rs:34:30
|
34 | Pallet: pallet use_parts { Call },
| ^^^^
error[E0412]: cannot find type `Call` in this scope
--> $DIR/use_undefined_part.rs:23:64
|
23 | pub type UncheckedExtrinsic = generic::UncheckedExtrinsic<u32, Call, Signature, ()>;
| ^^^^ not found in this scope
|
help: consider importing one of these items
|
1 | use crate::pallet::Call;
|
1 | use frame_support_test::Call;
|
1 | use frame_system::Call;
|
1 | use test_pallet::Call;
|
error[E0412]: cannot find type `Runtime` in this scope
--> $DIR/use_undefined_part.rs:25:25
|
25 | impl pallet::Config for Runtime {}
| ^^^^^^^ not found in this scope