-
Notifications
You must be signed in to change notification settings - Fork 740
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
feat: add hooks for utility batch #6517
base: master
Are you sure you want to change the base?
feat: add hooks for utility batch #6517
Conversation
My labels are the following, kindly request to add them, thank you.
|
Since the unit type offers a default none behaviour, therefore no testing is required in polkadot-sdk. It has to be tested in the downstream parachains where custom logic might be added. |
Could you give some more details on what you are injecting? |
Within Hydration, we decided to implement a unified Swapped event emitted by all our AMMs. For each event, we need to understand its context—whether it originates from our DCA, Router, ICE, or Utility Batch. To achieve this, we need to track the context, and this tracking logic would be injected both before and after Utility batch calls. Here is the exact code that we want to inject, we are already doing this in a PR with the fork of polkadot-sdk |
Any update on this @bkchr ? Let us know if you need any further info to proceed with it, thank you. |
substrate/frame/utility/src/lib.rs
Outdated
pub trait BatchPreHook { | ||
fn on_batch_start() -> sp_runtime::DispatchResult; | ||
} | ||
|
||
pub trait BatchPostHook { | ||
fn on_batch_end() -> sp_runtime::DispatchResult; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could just be one trait.
I actually started to review, but forgot to press the button. Generally I think it is fine, we should just combine the functionality into one trait. |
One thing that came to my mind while looking into this, you could use |
/cmd prdoc --audience runtime_dev |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me, maybe one trait BatchHook
.
Also we can have a small prdoc as people still have to add the associated type in their runtime.
…' into feat/add-hooks-for-utility-batch
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me. I might consider wrapping the error from the hook into a pallet specific error, but may not be needed.
Consider it will def be easier to trace when an error is appearing.
@@ -100,6 +115,9 @@ pub mod pallet { | |||
Into<<Self as frame_system::Config>::RuntimeOrigin> + | |||
IsType<<<Self as frame_system::Config>::RuntimeOrigin as frame_support::traits::OriginTrait>::PalletsOrigin>; | |||
|
|||
///Hook to be called before any batch operation |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
///Hook to be called before any batch operation | |
/// Hook to be called before any batch operation. |
pub trait BatchHook { | ||
fn on_batch_start() -> sp_runtime::DispatchResult; | ||
fn on_batch_end() -> sp_runtime::DispatchResult; | ||
|
||
} | ||
impl BatchHook for () { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pub trait BatchHook { | |
fn on_batch_start() -> sp_runtime::DispatchResult; | |
fn on_batch_end() -> sp_runtime::DispatchResult; | |
} | |
impl BatchHook for () { | |
/// Hooks that will be called for `batch` calls. | |
#[impl_trait_for_tuples::impl_for_tuples(30)] | |
pub trait BatchHook { | |
/// Will be called before a batch is executed. | |
fn on_batch_start() -> sp_runtime::DispatchResult; | |
/// Will be called after the batch was executed. | |
/// | |
/// Depending on the exact batch call used, it may not be called when a batch item failed. | |
fn on_batch_end() -> sp_runtime::DispatchResult; | |
} | |
impl BatchHook for () { |
Description
In the Hydration parachain, we want to inject custom functionalities into all utility batch calls, both before and after their execution. We believe this feature could be useful for other parachains as well, where, by default, no behavior exists for custom functionalities.
Integration
This PR introduces custom hooks to the Polkadot SDK, allowing parachains to configure and extend the SDK’s behavior according to their specific requirements. The changes include:
Custom Hook Integration:
Downstream projects can now define custom hooks for specific functionality within the SDK.
These hooks can be registered during the initialization phase of the parachain.
Configuration Details:
Hooks should be defined in a configuration object and passed to the relevant utlity pallet config setup implementation.
Example usage:
Backward Compatibility:
These changes are fully backward compatible. Parachains that do not implement the new hooks will continue to operate as before without any modifications.
Testing Recommendations:
Projects should test their custom hooks thoroughly to ensure the expected behavior when integrated with the Polkadot SDK.
By providing these hooks, parachains can gain finer control over SDK functionalities while maintaining flexibility for future updates.
Review Notes
The
BatchPreHook
is invoked before every batch call (batch, batch_all, and force_batch). It is triggered immediately after ensuring the origin.The
BatchPostHook
is invoked after every batch call (batch, batch_all, and force_batch). It is triggered right before collecting the weights to be returned.To establish a default behavior, one can set the unit type in the configuration, which simply returns an Ok result, so doing nothing:
Checklist
T
required)You can remove the "Checklist" section once all have been checked. Thank you for your contribution!
✄ -----------------------------------------------------------------------------