Add pallet attribute macro to declare pallets (#6877)

* rename system Config to system Trait.

command used:
```
find frame/ bin/ test-utils/ utils/ -name *.rs -exec sed -i 's/system::Trait>::/system::Config>::/g' {} \;
find frame/ bin/ test-utils/ utils/ -name *.rs -exec sed -i 's/impl frame_system::Trait for /impl frame_system::Config for /g' {} \;
find frame/ bin/ test-utils/ utils/ -name *.rs -exec sed -i 's/impl system::Trait for /impl system::Config for /g' {} \;
```
plus some manual ones especially for frame-support tests and frame-system

* make construct_runtime handle Pallet and Module

pallets can now be implemented on struct named Pallet or Module, both
definition are valid.
This is because next macro will generate only Pallet placeholder.

* introduce pallet attribute macro

currently just with tests, frame_system and other example hasn't been
upgraded

* allow to print some upgrade helper from decl_storage

* Improved error msg, typo.

Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>

* Improved error msg, typo.

Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>

* Improved error message on unexpected attributes + ui test

* add test for transactional

* various typo

* some tips when spans are lost

* allow pallet to depend on other pallet instances

* make event type metadata consistent with call and constant

* error messages

* ignore doc example

* fix pallet upgrade template

* fixup

* fix doc

* fix indentation

* Apply suggestions code formatting

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* some renames + fix compilation

* remove unsupported genesis config type alias

* merge fixup

* fix ui tests

* additional doc

* implement StorageInstance with new syntax

* fix line width

* fix doc: because pallet doc goes below reexport doc

* Update frame/support/procedural/src/pallet/parse/event.rs

Co-authored-by: Andrew Jones <ascjones@gmail.com>

* Update frame/system/src/lib.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update frame/support/test/tests/pallet_ui.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* improve doc as suggested

* revert construct_runtime Pallet part.

This revert the changes on construct_runtime. Now construct_runtime is
unchanged and instead pallet macro create a type alias
`type Module<..> = Pallet<..>` to be used by construct_runtime

* refactor with less intricated code

* fix ui test with new image

* fix ui tests

* add minor tests

Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>
Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
Co-authored-by: Andrew Jones <ascjones@gmail.com>
Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
Guillaume Thiolliere
2020-12-24 12:33:40 +01:00
committed by GitHub
parent 8e3d8a6a09
commit 6dfad0921b
131 changed files with 9610 additions and 31 deletions
@@ -27,22 +27,17 @@ use frame_support::{
};
use sp_core::{H256, sr25519};
mod system;
/// A version that we will check for in the tests
const SOME_TEST_VERSION: PalletVersion = PalletVersion { major: 3000, minor: 30, patch: 13 };
/// Checks that `on_runtime_upgrade` sets the latest pallet version when being called without
/// being provided by the user.
mod module1 {
use super::*;
pub trait Config: system::Config {}
pub trait Config: frame_system::Config {}
frame_support::decl_module! {
pub struct Module<T: Config> for enum Call where
origin: <T as system::Config>::Origin,
system = system,
origin: <T as frame_system::Config>::Origin,
{}
}
}
@@ -52,12 +47,11 @@ mod module1 {
mod module2 {
use super::*;
pub trait Config<I=DefaultInstance>: system::Config {}
pub trait Config<I=DefaultInstance>: frame_system::Config {}
frame_support::decl_module! {
pub struct Module<T: Config<I>, I: Instance=DefaultInstance> for enum Call where
origin: <T as system::Config>::Origin,
system = system
origin: <T as frame_system::Config>::Origin,
{
fn on_runtime_upgrade() -> Weight {
assert_eq!(crate_to_pallet_version!(), Self::current_version());
@@ -82,26 +76,95 @@ mod module2 {
}
}
#[frame_support::pallet]
mod pallet3 {
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
#[pallet::config]
pub trait Config: frame_system::Config {
}
#[pallet::pallet]
pub struct Pallet<T>(PhantomData<T>);
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_runtime_upgrade() -> Weight {
return 3;
}
}
#[pallet::call]
impl<T: Config> Pallet<T> {
}
}
#[frame_support::pallet]
mod pallet4 {
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
#[pallet::config]
pub trait Config<I: 'static = ()>: frame_system::Config {
}
#[pallet::pallet]
pub struct Pallet<T, I=()>(PhantomData<(T, I)>);
#[pallet::hooks]
impl<T: Config<I>, I: 'static> Hooks<BlockNumberFor<T>> for Pallet<T, I> {
fn on_runtime_upgrade() -> Weight {
return 3;
}
}
#[pallet::call]
impl<T: Config<I>, I: 'static> Pallet<T, I> {
}
}
impl module1::Config for Runtime {}
impl module2::Config for Runtime {}
impl module2::Config<module2::Instance1> for Runtime {}
impl module2::Config<module2::Instance2> for Runtime {}
impl pallet3::Config for Runtime {}
impl pallet4::Config for Runtime {}
impl pallet4::Config<pallet4::Instance1> for Runtime {}
impl pallet4::Config<pallet4::Instance2> for Runtime {}
pub type Signature = sr25519::Signature;
pub type AccountId = <Signature as Verify>::Signer;
pub type BlockNumber = u64;
pub type Index = u64;
impl system::Config for Runtime {
type BaseCallFilter= ();
type Hash = H256;
frame_support::parameter_types!(
pub const BlockHashCount: u32 = 250;
);
impl frame_system::Config for Runtime {
type BaseCallFilter = ();
type Origin = Origin;
type Index = u64;
type BlockNumber = BlockNumber;
type AccountId = AccountId;
type Event = Event;
type PalletInfo = PalletInfo;
type Call = Call;
type Hash = H256;
type Hashing = sp_runtime::traits::BlakeTwo256;
type AccountId = AccountId;
type Lookup = sp_runtime::traits::IdentityLookup<Self::AccountId>;
type Header = Header;
type Event = Event;
type BlockHashCount = BlockHashCount;
type BlockWeights = ();
type BlockLength = ();
type DbWeight = ();
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = ();
type OnNewAccount = ();
type OnKilledAccount = ();
type SystemWeightInfo = ();
}
frame_support::construct_runtime!(
@@ -110,11 +173,15 @@ frame_support::construct_runtime!(
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic
{
System: system::{Module, Call, Event<T>},
System: frame_system::{Module, Call, Event<T>},
Module1: module1::{Module, Call},
Module2: module2::{Module, Call},
Module2_1: module2::<Instance1>::{Module, Call},
Module2_2: module2::<Instance2>::{Module, Call},
Pallet3: pallet3::{Module, Call},
Pallet4: pallet4::{Module, Call},
Pallet4_1: pallet4::<Instance1>::{Module, Call},
Pallet4_2: pallet4::<Instance2>::{Module, Call},
}
);
@@ -156,6 +223,10 @@ fn on_runtime_upgrade_sets_the_pallet_versions_in_storage() {
check_pallet_version("Module2");
check_pallet_version("Module2_1");
check_pallet_version("Module2_2");
check_pallet_version("Pallet3");
check_pallet_version("Pallet4");
check_pallet_version("Pallet4_1");
check_pallet_version("Pallet4_2");
});
}
@@ -171,6 +242,10 @@ fn on_runtime_upgrade_overwrites_old_version() {
check_pallet_version("Module2");
check_pallet_version("Module2_1");
check_pallet_version("Module2_2");
check_pallet_version("Pallet3");
check_pallet_version("Pallet4");
check_pallet_version("Pallet4_1");
check_pallet_version("Pallet4_2");
});
}
@@ -183,6 +258,10 @@ fn genesis_init_puts_pallet_version_into_storage() {
check_pallet_version("Module2");
check_pallet_version("Module2_1");
check_pallet_version("Module2_2");
check_pallet_version("Pallet3");
check_pallet_version("Pallet4");
check_pallet_version("Pallet4_1");
check_pallet_version("Pallet4_2");
let system_version = System::storage_version().expect("System version should be set");
assert_eq!(System::current_version(), system_version);