paritytech / polkadot-sdk

The Parity Polkadot Blockchain SDK
https://polkadot.com/
1.92k stars 707 forks source link

feat: add hooks for utility batch #6517

Open dmoka opened 1 week ago

dmoka commented 1 week ago

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:

impl pallet_utility::Config for Runtime {
    type RuntimeEvent = RuntimeEvent;
    type RuntimeCall = RuntimeCall;
    type PalletsOrigin = OriginCaller;
    type BatchPreHook = PushBatchExecutionTypeForUnifiedEvent;
    type BatchPostHook = PopBatchExecutionTypeForUnifiedEvent;
    type WeightInfo = weights::pallet_utility::HydraWeight<Runtime>;
}

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:

impl pallet_utility::Config for Runtime {
    type RuntimeEvent = RuntimeEvent;
    type RuntimeCall = RuntimeCall;
    type PalletsOrigin = OriginCaller;
    type BatchPreHook = ();
    type BatchPostHook = ();
    type WeightInfo = weights::pallet_utility::HydraWeight<Runtime>;
}

Checklist

You can remove the "Checklist" section once all have been checked. Thank you for your contribution!

✄ -----------------------------------------------------------------------------

cla-bot-2021[bot] commented 1 week ago

User @dmoka, please sign the CLA here.

dmoka commented 1 week ago

My labels are the following, kindly request to add them, thank you.

dmoka commented 1 week ago

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.

bkchr commented 6 days ago

In the Hydration parachain, we want to inject custom functionalities into all utility batch calls, both before and after their execution.

Could you give some more details on what you are injecting?

dmoka commented 4 days ago

In the Hydration parachain, we want to inject custom functionalities into all utility batch calls, both before and after their execution.

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

https://github.com/galacticcouncil/hydration-node/blob/04c19428d8b76eb13dbccfd0bbaf1fc95d77cbf0/runtime/hydradx/src/system.rs#L320-L337