Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FRAME] Simplify pallet config definition: remove RuntimeEvent associated type #7229

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

dastansam
Copy link
Contributor

@dastansam dastansam commented Jan 17, 2025

part of #3743

Motivation

This PR removes the need for defining RuntimeEvent in the Config trait of a pallet. It uses associated type bound feature under the hood to make sure that Event of the pallet is convertible to the aggregated runtime event type frame_system::RuntimeEvent.

This is an initial PR for RuntimeEvent type and will be followed with other types, e.g RuntimeCall. As a demo, example pallets' config definition is updated to use this feature.

With this change, we can do this:

#[pallet::config]
pub trait Config: frame_system::Config {
}

instead of this:

#[pallet::config]
pub trait Config: frame_system::Config {
        /// Overarching event type.
       type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
}

or alternatively, it is possible to explicitly define the type bound:

#[pallet::config]
pub trait Config: frame_system::Config<RuntimeEvent: From<Event<Self>>> {
}

polkadot address: 16FqwPZ8GRC5U5D4Fu7W33nA55ZXzXGWHwmbnE1eT6pxuqcT

@dastansam dastansam marked this pull request as ready for review January 17, 2025 15:57
@dastansam dastansam requested a review from a team as a code owner January 17, 2025 15:57
Copy link
Member

@bkchr bkchr left a comment

Choose a reason for hiding this comment

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

Ty for doing this!

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>;
pub trait Config: frame_system::Config<RuntimeEvent: From<Event<Self>>> {
Copy link
Member

Choose a reason for hiding this comment

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

IMO it would be much "cooler" 👀 if the pallet::config macro would auto generate all of these. And then we could put a deprecation warning over RuntimeEvent, RuntimeCall etc inclusion in the Config trait later.

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree, for the implementation all pallets except frame-system can bound this From<Event> automatically.

For frame-system pallet, I guess we need something so that the macro can know that the pallet is frame-system, I suggest a new attribute somewhere (maybe on pallet::config) like #[pallet::is_frame_system].
But if you find a better way, please do.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, I have made the changes to automatically append the type bound to system supertrait. I did this on the macro expansion level instead of parsing, since we need to know if the pallet Event struct is actually defined.

For frame-system pallet, I guess we need something so that the macro can know that the pallet is frame-system, I suggest a new attribute somewhere (maybe on pallet::config) like #[pallet::is_frame_system].
But if you find a better way, please do.

For this, I simply opted for checking if the pallet config contains a system supertrait, i.e frame_system::Config. If it doesn't, it's a system pallet.

@paritytech-review-bot paritytech-review-bot bot requested a review from a team January 24, 2025 12:27
@dastansam dastansam requested review from gui1117 and bkchr January 24, 2025 12:36
@dastansam dastansam changed the title [FRAME] Simplify pallet config definition: RuntimeEvent as an associated type bound [FRAME] Simplify pallet config definition: remove RuntimeEvent associated type Jan 24, 2025
@dastansam
Copy link
Contributor Author

@bkchr bunch of CI is failing because they treat the RuntimeEvent deprecation warnings as error. Do you think it's worth fixing all those deprecation warnings in this PR? I thought it would be a lot of changes and also would feel more comfortable doing it once this change is carefully reviewed.

Comment on lines 481 to 483
type_event
.attrs
.push(syn::parse_quote!(#[deprecated(note = "`RuntimeEvent` associated type is deprecated, there is no need to define it in the pallet config.")]));
Copy link
Member

Choose a reason for hiding this comment

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

We should use proc_macro_warning for issuing the warning to the pallet builder. Besides that we should provide an "allow(deprecated)" above the type to not print the warning.

Then we basically need to update all pallets in the repo to accept the deprecated RuntimeEvent. At some later point we then need to remove it. There is sadly no real better way for downstream as removing them at some point.

Comment on lines 443 to 445
let (has_frame_system_supertrait, has_event_bound) = item
.supertraits
.iter()
Copy link
Member

Choose a reason for hiding this comment

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

We should do what @gui1117 proposed and introduce a frame_system_pallet attribute macro that we will use in frame_system. Every other pallet will then be assumed to not be frame-system.

/// 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>;

/// The overarching task type.
#[pallet::no_default]
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
#[pallet::no_default]
#[pallet::no_default] // optional. `RuntimeEvent` is automatically excluded as well.

/// 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>;

/// The overarching task type.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/// The overarching task type.
/// The overarching task type. This is coming from the runtime, and cannot have a default.
/// In general, `Runtime*`-oriented types cannot have a sensible default.

@paritytech-review-bot paritytech-review-bot bot requested a review from a team January 25, 2025 09:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants