Add pallet attribute macro to declare pallets#6877
Conversation
|
Does everything need to be wrapped in a single module? It would be really nice to be able to write a pallet's logic across multiple files I think. |
Everything with a Note that most of our pallet have all those defined in one module. At some point we could allow to have some stuff defined outside, and maybe referenced to like I don't know some: #[frame_support::pallet(Example)]
#[pallet::external_event_def(my_path_to_event)]
mod pallet {
}but that might be tricky in some cases and also it would be more difficult for implementation (as information from one macro to the other needs to be send through regular rust types) |
|
Just a note: So right now we derive Eq, PartialEq, Debug and Clone for Event and for Call without doing any generic bound. (for event this is anyway required because frame_system::Trait::Event requires it) If we derive Eq without where clause we have a not so bad error message (at least it contains the corresponding associated type) but parity-scale-codec will derive with a where clause and the error message doesn't give information: I guess as we need anyway codec on them, we could derive codec without any where clause. This would give a better error message. For this we just need to add it to codec-derive macro. |
|
so with this
EDIT: I think we should go for last option and maybe improve construct_runtime later. That means if pallet_treasury crate want to make its pallet definition available at the root namespace then it needs to write |
cd12184 to
b099875
Compare
|
I am implementing the helper for storage getter: But if storage is private then it leaks private type into public interface. Thus solutions are:
I opted for second method. |
2130f53 to
36ef3bc
Compare
| #[pallet::storage] | ||
| type Foo<T: Trait<I>, I: Instance = DefaultInstance> = | ||
| StorageValueType<_, T::Balance, ValueQuery, OnFooEmpty<T, I>>; | ||
| #[pallet::type_value] pub fn OnFooEmpty<T: Trait<I>, I: Instance>() -> T::Balance { 3.into() } |
There was a problem hiding this comment.
the idea came from Gav #5678 (comment) as an easy way to implement a struct which implements Get.
It is same as parameter_types but it allows to have some generic. another possibility is to extend parameter type to allow generic, and it should be directly usable here.
The macro check that generic are consistent (i.e. instance generic is used or not depending on trait) but anyway code will not compile if user mixed himself.
There was a problem hiding this comment.
Ahh it's for setting the default value, I see. Hmm.
pallet attribute macro to declare pallets
|
This doesn't touch any existing code but just add for a new pallet macro. Thus I think we can merge. if any contradictory opinion we can still revert without any issue. |
|
bot merge |
|
Waiting for commit status. |
Fix #2782
Fix #5040
Related #4288
Related #5148
Fix #5678
Related #6494 (will be automatically generated)
Fix #5913
TL;DR;
This PR only introduce attribute macro and its tests, though it doesn't migrate any pallet, not even frame-system
The macro doc is in frame/support/src/lib.rs: https://github.com/paritytech/substrate/pull/6877/files#diff-31d6476b566f793c17f32f9fdefeb487988af58c61ba752d0275b91f6c8aaccaR983
Rendered doc can be found at https://thiolliere.github.io/doc/frame_support/attr.pallet.html
macro code is at frame/support/procedural/src/pallet/* and basically parse the macro to some
Defstruct. And then expand from thieDefstruct.See controversial point below to make further your opinion.
PR commit description and further PR
Breaking change for migrated pallet (no pallet are migrated in this PR)
Traitis renamedConfigTChanges
Addition
Controversial points: (or just points which needs review)
in the pallet attribute macro: the generic names are enforced. i.e. you can't write
#[pallet::call] impl<G: Config> ...instead macro enforce to useT: Config.justification:
I can work on allowing to use any ident instead of just
Tif ppl feel the need.We could also accept either
T: ConfigorC: Configand ask it to be consistent in all declaration, thus where_clause are easily mergeable. Anyway I would rather do that in another PR.#[pallet::genesis_config]and#[pallet::genesis_build]will add the attribute#[cfg(features = "std")]. This result in code such as:Another possibilty (thanks Bryan for the idea) could be to wrap them in a module:
But I feel more annoying than helpful as it adds even more indentation and namespace
Because storages now use PalletInfo for prefixes, tests shouldn't use
()as PalletInfo as allstorages will use the same module_prefix "test" which can lead to collision. Anyway in the not
to far future, everybody should use construct_runtime in their pallet mock/tests.
Becaues storages now use PalletInfo for prefixes, modifying the name of a pallet in
construct_runtime require a migration, which might not be expected.
Also live chain it means instantiable pallet might need to be renamed:
For instance Council should be renamed migrated from "Instance1Collective" to "Council".
Becaues storages now use PalletInfo for prefixes, when upgrading a pallet from old decl_* macro
to new one. every runtime must be careful to rename their pallet or migrate.
(except for System pallet as its name is enforced to be System anyway)
upgrade to new macro guidelines: https://thiolliere.github.io/doc/frame_support/attr.pallet.html#upgrade-guidelines
checking upgrade guidelines: https://thiolliere.github.io/doc/frame_support/attr.pallet.html#checking-upgrade-guidelines