From ce484ba74da3c78c6d95f4022b24cd0d11ab4fba Mon Sep 17 00:00:00 2001 From: dmoka Date: Thu, 14 Nov 2024 07:59:32 +0100 Subject: [PATCH 01/12] add utility batch hooks --- substrate/frame/utility/src/lib.rs | 35 ++++++++++++++++++++++++++++ substrate/frame/utility/src/tests.rs | 2 ++ 2 files changed, 37 insertions(+) diff --git a/substrate/frame/utility/src/lib.rs b/substrate/frame/utility/src/lib.rs index ed5544fe55ca..713f08ba13c0 100644 --- a/substrate/frame/utility/src/lib.rs +++ b/substrate/frame/utility/src/lib.rs @@ -71,6 +71,22 @@ pub use weights::WeightInfo; pub use pallet::*; +pub trait BatchPreHook { + fn on_batch_start(); +} + +pub trait BatchPostHook { + fn on_batch_end(); +} + +impl BatchPreHook for () { + fn on_batch_start() {} +} + +impl BatchPostHook for () { + fn on_batch_end() {} +} + #[frame_support::pallet] pub mod pallet { use super::*; @@ -100,6 +116,10 @@ pub mod pallet { Into<::RuntimeOrigin> + IsType<<::RuntimeOrigin as frame_support::traits::OriginTrait>::PalletsOrigin>; + type BatchPreHook: BatchPreHook; + + type BatchPostHook: BatchPostHook; + /// Weight information for extrinsics in this pallet. type WeightInfo: WeightInfo; } @@ -196,6 +216,8 @@ pub mod pallet { return Err(BadOrigin.into()) } + T::BatchPreHook::on_batch_start(); + let is_root = ensure_root(origin.clone()).is_ok(); let calls_len = calls.len(); ensure!(calls_len <= Self::batched_calls_limit() as usize, Error::::TooManyCalls); @@ -225,6 +247,9 @@ pub mod pallet { Self::deposit_event(Event::ItemCompleted); } Self::deposit_event(Event::BatchCompleted); + + T::BatchPostHook::on_batch_end(); + let base_weight = T::WeightInfo::batch(calls_len as u32); Ok(Some(base_weight.saturating_add(weight)).into()) } @@ -305,6 +330,8 @@ pub mod pallet { return Err(BadOrigin.into()) } + T::BatchPreHook::on_batch_start(); + let is_root = ensure_root(origin.clone()).is_ok(); let calls_len = calls.len(); ensure!(calls_len <= Self::batched_calls_limit() as usize, Error::::TooManyCalls); @@ -339,6 +366,9 @@ pub mod pallet { Self::deposit_event(Event::ItemCompleted); } Self::deposit_event(Event::BatchCompleted); + + T::BatchPostHook::on_batch_end(); + let base_weight = T::WeightInfo::batch_all(calls_len as u32); Ok(Some(base_weight.saturating_add(weight)).into()) } @@ -401,6 +431,8 @@ pub mod pallet { return Err(BadOrigin.into()) } + T::BatchPreHook::on_batch_start(); + let is_root = ensure_root(origin.clone()).is_ok(); let calls_len = calls.len(); ensure!(calls_len <= Self::batched_calls_limit() as usize, Error::::TooManyCalls); @@ -431,6 +463,9 @@ pub mod pallet { } else { Self::deposit_event(Event::BatchCompleted); } + + T::BatchPostHook::on_batch_end(); + let base_weight = T::WeightInfo::batch(calls_len as u32); Ok(Some(base_weight.saturating_add(weight)).into()) } diff --git a/substrate/frame/utility/src/tests.rs b/substrate/frame/utility/src/tests.rs index 9755efaea41a..5728deafd902 100644 --- a/substrate/frame/utility/src/tests.rs +++ b/substrate/frame/utility/src/tests.rs @@ -222,6 +222,8 @@ impl Config for Test { type RuntimeEvent = RuntimeEvent; type RuntimeCall = RuntimeCall; type PalletsOrigin = OriginCaller; + type BatchPreHook = (); + type BatchPostHook = (); type WeightInfo = (); } From 1cfce6cc0da8dda56fcc379d9f3bdd952f79d4bd Mon Sep 17 00:00:00 2001 From: dmoka Date: Thu, 14 Nov 2024 08:11:43 +0100 Subject: [PATCH 02/12] add doc comments --- substrate/frame/utility/src/lib.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/substrate/frame/utility/src/lib.rs b/substrate/frame/utility/src/lib.rs index 713f08ba13c0..b12128585121 100644 --- a/substrate/frame/utility/src/lib.rs +++ b/substrate/frame/utility/src/lib.rs @@ -116,8 +116,10 @@ pub mod pallet { Into<::RuntimeOrigin> + IsType<<::RuntimeOrigin as frame_support::traits::OriginTrait>::PalletsOrigin>; + ///Hook to be called before any batch operation type BatchPreHook: BatchPreHook; + ///Hook to be called after any batch operation type BatchPostHook: BatchPostHook; /// Weight information for extrinsics in this pallet. From e804ec5601574362c7ccc61ff76937578dc16c8c Mon Sep 17 00:00:00 2001 From: dmoka Date: Thu, 14 Nov 2024 10:12:05 +0100 Subject: [PATCH 03/12] adding empty hook configs for all the requires utlity pallet config implementations --- cumulus/parachains/runtimes/assets/asset-hub-rococo/src/lib.rs | 2 ++ cumulus/parachains/runtimes/assets/asset-hub-westend/src/lib.rs | 2 ++ .../runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs | 2 ++ .../runtimes/bridge-hubs/bridge-hub-westend/src/lib.rs | 2 ++ .../runtimes/collectives/collectives-westend/src/lib.rs | 2 ++ cumulus/parachains/runtimes/coretime/coretime-rococo/src/lib.rs | 2 ++ .../parachains/runtimes/coretime/coretime-westend/src/lib.rs | 2 ++ cumulus/parachains/runtimes/people/people-rococo/src/lib.rs | 2 ++ cumulus/parachains/runtimes/people/people-westend/src/lib.rs | 2 ++ polkadot/runtime/rococo/src/lib.rs | 2 ++ polkadot/runtime/westend/src/lib.rs | 2 ++ 11 files changed, 22 insertions(+) diff --git a/cumulus/parachains/runtimes/assets/asset-hub-rococo/src/lib.rs b/cumulus/parachains/runtimes/assets/asset-hub-rococo/src/lib.rs index efe4a4052a99..842cd789beca 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-rococo/src/lib.rs +++ b/cumulus/parachains/runtimes/assets/asset-hub-rococo/src/lib.rs @@ -470,6 +470,8 @@ impl pallet_utility::Config for Runtime { type RuntimeEvent = RuntimeEvent; type RuntimeCall = RuntimeCall; type PalletsOrigin = OriginCaller; + type BatchPreHook = (); + type BatchPostHook = (); type WeightInfo = weights::pallet_utility::WeightInfo; } diff --git a/cumulus/parachains/runtimes/assets/asset-hub-westend/src/lib.rs b/cumulus/parachains/runtimes/assets/asset-hub-westend/src/lib.rs index 712b5c0ada94..62a975ea19cb 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-westend/src/lib.rs +++ b/cumulus/parachains/runtimes/assets/asset-hub-westend/src/lib.rs @@ -466,6 +466,8 @@ impl pallet_utility::Config for Runtime { type RuntimeEvent = RuntimeEvent; type RuntimeCall = RuntimeCall; type PalletsOrigin = OriginCaller; + type BatchPreHook = (); + type BatchPostHook = (); type WeightInfo = weights::pallet_utility::WeightInfo; } diff --git a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs index 6c6e2ec7efdd..18a08bb8d016 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs +++ b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs @@ -531,6 +531,8 @@ impl pallet_utility::Config for Runtime { type RuntimeEvent = RuntimeEvent; type RuntimeCall = RuntimeCall; type PalletsOrigin = OriginCaller; + type BatchPreHook = (); + type BatchPostHook = (); type WeightInfo = weights::pallet_utility::WeightInfo; } diff --git a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-westend/src/lib.rs b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-westend/src/lib.rs index ddd40dbf60e0..811a20c3cc96 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-westend/src/lib.rs +++ b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-westend/src/lib.rs @@ -497,6 +497,8 @@ impl pallet_utility::Config for Runtime { type RuntimeEvent = RuntimeEvent; type RuntimeCall = RuntimeCall; type PalletsOrigin = OriginCaller; + type BatchPreHook = (); + type BatchPostHook = (); type WeightInfo = weights::pallet_utility::WeightInfo; } diff --git a/cumulus/parachains/runtimes/collectives/collectives-westend/src/lib.rs b/cumulus/parachains/runtimes/collectives/collectives-westend/src/lib.rs index f22feb70382a..17aca8ad7760 100644 --- a/cumulus/parachains/runtimes/collectives/collectives-westend/src/lib.rs +++ b/cumulus/parachains/runtimes/collectives/collectives-westend/src/lib.rs @@ -260,6 +260,8 @@ impl pallet_utility::Config for Runtime { type RuntimeEvent = RuntimeEvent; type RuntimeCall = RuntimeCall; type PalletsOrigin = OriginCaller; + type BatchPreHook = (); + type BatchPostHook = (); type WeightInfo = weights::pallet_utility::WeightInfo; } diff --git a/cumulus/parachains/runtimes/coretime/coretime-rococo/src/lib.rs b/cumulus/parachains/runtimes/coretime/coretime-rococo/src/lib.rs index 0c9f9461f7f4..101e600b9c95 100644 --- a/cumulus/parachains/runtimes/coretime/coretime-rococo/src/lib.rs +++ b/cumulus/parachains/runtimes/coretime/coretime-rococo/src/lib.rs @@ -577,6 +577,8 @@ impl pallet_utility::Config for Runtime { type RuntimeEvent = RuntimeEvent; type RuntimeCall = RuntimeCall; type PalletsOrigin = OriginCaller; + type BatchPreHook = (); + type BatchPostHook = (); type WeightInfo = weights::pallet_utility::WeightInfo; } diff --git a/cumulus/parachains/runtimes/coretime/coretime-westend/src/lib.rs b/cumulus/parachains/runtimes/coretime/coretime-westend/src/lib.rs index 614eae895a74..2987b53ba8ce 100644 --- a/cumulus/parachains/runtimes/coretime/coretime-westend/src/lib.rs +++ b/cumulus/parachains/runtimes/coretime/coretime-westend/src/lib.rs @@ -577,6 +577,8 @@ impl pallet_utility::Config for Runtime { type RuntimeEvent = RuntimeEvent; type RuntimeCall = RuntimeCall; type PalletsOrigin = OriginCaller; + type BatchPreHook = (); + type BatchPostHook = (); type WeightInfo = weights::pallet_utility::WeightInfo; } diff --git a/cumulus/parachains/runtimes/people/people-rococo/src/lib.rs b/cumulus/parachains/runtimes/people/people-rococo/src/lib.rs index 9b251a90d678..b4bce0f52944 100644 --- a/cumulus/parachains/runtimes/people/people-rococo/src/lib.rs +++ b/cumulus/parachains/runtimes/people/people-rococo/src/lib.rs @@ -520,6 +520,8 @@ impl pallet_utility::Config for Runtime { type RuntimeEvent = RuntimeEvent; type RuntimeCall = RuntimeCall; type PalletsOrigin = OriginCaller; + type BatchPreHook = (); + type BatchPostHook = (); type WeightInfo = weights::pallet_utility::WeightInfo; } diff --git a/cumulus/parachains/runtimes/people/people-westend/src/lib.rs b/cumulus/parachains/runtimes/people/people-westend/src/lib.rs index 07bfba92c933..ed239395bea6 100644 --- a/cumulus/parachains/runtimes/people/people-westend/src/lib.rs +++ b/cumulus/parachains/runtimes/people/people-westend/src/lib.rs @@ -520,6 +520,8 @@ impl pallet_utility::Config for Runtime { type RuntimeEvent = RuntimeEvent; type RuntimeCall = RuntimeCall; type PalletsOrigin = OriginCaller; + type BatchPreHook = (); + type BatchPostHook = (); type WeightInfo = weights::pallet_utility::WeightInfo; } diff --git a/polkadot/runtime/rococo/src/lib.rs b/polkadot/runtime/rococo/src/lib.rs index 6ec49c5830f7..e29f095f429f 100644 --- a/polkadot/runtime/rococo/src/lib.rs +++ b/polkadot/runtime/rococo/src/lib.rs @@ -716,6 +716,8 @@ impl pallet_utility::Config for Runtime { type RuntimeEvent = RuntimeEvent; type RuntimeCall = RuntimeCall; type PalletsOrigin = OriginCaller; + type BatchPreHook = (); + type BatchPostHook = (); type WeightInfo = weights::pallet_utility::WeightInfo; } diff --git a/polkadot/runtime/westend/src/lib.rs b/polkadot/runtime/westend/src/lib.rs index d0c1cd89de32..3abc9abf382d 100644 --- a/polkadot/runtime/westend/src/lib.rs +++ b/polkadot/runtime/westend/src/lib.rs @@ -959,6 +959,8 @@ impl pallet_utility::Config for Runtime { type RuntimeEvent = RuntimeEvent; type RuntimeCall = RuntimeCall; type PalletsOrigin = OriginCaller; + type BatchPreHook = (); + type BatchPostHook = (); type WeightInfo = weights::pallet_utility::WeightInfo; } From 187fb9e31acb8c13bbc18e3d8387f09206da133d Mon Sep 17 00:00:00 2001 From: dmoka Date: Thu, 14 Nov 2024 13:43:41 +0100 Subject: [PATCH 04/12] add missing hooks --- substrate/bin/node/runtime/src/lib.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/substrate/bin/node/runtime/src/lib.rs b/substrate/bin/node/runtime/src/lib.rs index c8409078af57..a3843be55401 100644 --- a/substrate/bin/node/runtime/src/lib.rs +++ b/substrate/bin/node/runtime/src/lib.rs @@ -335,6 +335,8 @@ impl pallet_utility::Config for Runtime { type RuntimeEvent = RuntimeEvent; type RuntimeCall = RuntimeCall; type PalletsOrigin = OriginCaller; + type BatchPreHook = (); + type BatchPostHook = (); type WeightInfo = pallet_utility::weights::SubstrateWeight; } From 0ad2f2be608b7fbc867fb55b938874e0aad701da Mon Sep 17 00:00:00 2001 From: dmoka Date: Thu, 14 Nov 2024 13:50:16 +0100 Subject: [PATCH 05/12] add missing hooks --- .../parachains/runtimes/contracts/contracts-rococo/src/lib.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cumulus/parachains/runtimes/contracts/contracts-rococo/src/lib.rs b/cumulus/parachains/runtimes/contracts/contracts-rococo/src/lib.rs index 55770515d73f..cb495fe798e5 100644 --- a/cumulus/parachains/runtimes/contracts/contracts-rococo/src/lib.rs +++ b/cumulus/parachains/runtimes/contracts/contracts-rococo/src/lib.rs @@ -272,6 +272,8 @@ impl pallet_utility::Config for Runtime { type RuntimeEvent = RuntimeEvent; type RuntimeCall = RuntimeCall; type PalletsOrigin = OriginCaller; + type BatchPreHook = (); + type BatchPostHook = (); type WeightInfo = pallet_utility::weights::SubstrateWeight; } From 7e2c42c2c41b72d7ad1a0f6fb2614a8a6b48a81d Mon Sep 17 00:00:00 2001 From: dmoka Date: Thu, 14 Nov 2024 15:10:43 +0100 Subject: [PATCH 06/12] add dispatch result as return value for hooks --- substrate/frame/utility/src/lib.rs | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/substrate/frame/utility/src/lib.rs b/substrate/frame/utility/src/lib.rs index b12128585121..0236980fb38a 100644 --- a/substrate/frame/utility/src/lib.rs +++ b/substrate/frame/utility/src/lib.rs @@ -68,23 +68,26 @@ use sp_core::TypeId; use sp_io::hashing::blake2_256; use sp_runtime::traits::{BadOrigin, Dispatchable, TrailingZeroInput}; pub use weights::WeightInfo; - pub use pallet::*; pub trait BatchPreHook { - fn on_batch_start(); + fn on_batch_start() -> sp_runtime::DispatchResult; } pub trait BatchPostHook { - fn on_batch_end(); + fn on_batch_end() -> sp_runtime::DispatchResult; } impl BatchPreHook for () { - fn on_batch_start() {} + fn on_batch_start() -> sp_runtime::DispatchResult { + Ok(()) + } } impl BatchPostHook for () { - fn on_batch_end() {} + fn on_batch_end() -> sp_runtime::DispatchResult { + Ok(()) + } } #[frame_support::pallet] @@ -218,7 +221,7 @@ pub mod pallet { return Err(BadOrigin.into()) } - T::BatchPreHook::on_batch_start(); + T::BatchPreHook::on_batch_start()?; let is_root = ensure_root(origin.clone()).is_ok(); let calls_len = calls.len(); @@ -250,7 +253,7 @@ pub mod pallet { } Self::deposit_event(Event::BatchCompleted); - T::BatchPostHook::on_batch_end(); + T::BatchPostHook::on_batch_end()?; let base_weight = T::WeightInfo::batch(calls_len as u32); Ok(Some(base_weight.saturating_add(weight)).into()) @@ -332,7 +335,7 @@ pub mod pallet { return Err(BadOrigin.into()) } - T::BatchPreHook::on_batch_start(); + T::BatchPreHook::on_batch_start()?; let is_root = ensure_root(origin.clone()).is_ok(); let calls_len = calls.len(); @@ -369,7 +372,7 @@ pub mod pallet { } Self::deposit_event(Event::BatchCompleted); - T::BatchPostHook::on_batch_end(); + T::BatchPostHook::on_batch_end()?; let base_weight = T::WeightInfo::batch_all(calls_len as u32); Ok(Some(base_weight.saturating_add(weight)).into()) @@ -433,7 +436,7 @@ pub mod pallet { return Err(BadOrigin.into()) } - T::BatchPreHook::on_batch_start(); + T::BatchPreHook::on_batch_start()?; let is_root = ensure_root(origin.clone()).is_ok(); let calls_len = calls.len(); @@ -466,7 +469,7 @@ pub mod pallet { Self::deposit_event(Event::BatchCompleted); } - T::BatchPostHook::on_batch_end(); + T::BatchPostHook::on_batch_end()?; let base_weight = T::WeightInfo::batch(calls_len as u32); Ok(Some(base_weight.saturating_add(weight)).into()) From 9b62779bd41029bed3aca781b2fca74b0f2a76e7 Mon Sep 17 00:00:00 2001 From: dmoka Date: Mon, 18 Nov 2024 11:09:46 +0100 Subject: [PATCH 07/12] adjust readme with hook info --- substrate/frame/utility/README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/substrate/frame/utility/README.md b/substrate/frame/utility/README.md index 0a6769ae1c7c..ec9d881d4548 100644 --- a/substrate/frame/utility/README.md +++ b/substrate/frame/utility/README.md @@ -22,6 +22,9 @@ This module contains two basic pieces of functionality: Since proxy filters are respected in all dispatches of this module, it should never need to be filtered by any proxy. +Pre- and post-hooks can be configured via `BatchPostHook` and `BatchPostHook` associated types. +They are triggered for each batch call: `batch`, `batch_all`, and `force_batch`. Use the unit type `()` if no behavior is required. + ## Interface ### Dispatchable Functions From dc6e5f7ba235f8266891ebc518c9f99f65d69792 Mon Sep 17 00:00:00 2001 From: root Date: Sun, 8 Dec 2024 02:13:41 +0000 Subject: [PATCH 08/12] ubuntu as base for docerfile --- docker/dockerfiles/polkadot/polkadot_injected.Dockerfile | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docker/dockerfiles/polkadot/polkadot_injected.Dockerfile b/docker/dockerfiles/polkadot/polkadot_injected.Dockerfile index 3dbede4966a8..a148275f132f 100644 --- a/docker/dockerfiles/polkadot/polkadot_injected.Dockerfile +++ b/docker/dockerfiles/polkadot/polkadot_injected.Dockerfile @@ -1,4 +1,4 @@ -FROM docker.io/parity/base-bin +FROM ubuntu:22.04 # metadata ARG VCS_REF @@ -22,6 +22,8 @@ ENV RUST_BACKTRACE 1 USER root WORKDIR /app +RUN useradd -ms /bin/sh parity + # add polkadot and polkadot-*-worker binaries to the docker image COPY bin/* /usr/local/bin/ COPY entrypoint.sh . From be3c07feaca2fbf6beaa4af1f38dbe03f20c78ac Mon Sep 17 00:00:00 2001 From: dmoka Date: Fri, 20 Dec 2024 12:22:30 +0100 Subject: [PATCH 09/12] merge batch hook into one trait --- .../assets/asset-hub-rococo/src/lib.rs | 3 +- .../assets/asset-hub-westend/src/lib.rs | 3 +- .../bridge-hubs/bridge-hub-rococo/src/lib.rs | 3 +- .../bridge-hubs/bridge-hub-westend/src/lib.rs | 3 +- .../collectives-westend/src/lib.rs | 3 +- .../contracts/contracts-rococo/src/lib.rs | 3 +- .../coretime/coretime-rococo/src/lib.rs | 3 +- .../coretime/coretime-westend/src/lib.rs | 3 +- .../runtimes/people/people-rococo/src/lib.rs | 3 +- .../runtimes/people/people-westend/src/lib.rs | 3 +- polkadot/runtime/rococo/src/lib.rs | 3 +- polkadot/runtime/westend/src/lib.rs | 3 +- substrate/bin/node/runtime/src/lib.rs | 3 +- substrate/frame/utility/README.md | 2 +- substrate/frame/utility/src/lib.rs | 29 +++++++------------ substrate/frame/utility/src/tests.rs | 3 +- 16 files changed, 26 insertions(+), 47 deletions(-) diff --git a/cumulus/parachains/runtimes/assets/asset-hub-rococo/src/lib.rs b/cumulus/parachains/runtimes/assets/asset-hub-rococo/src/lib.rs index 842cd789beca..bd0967d19e26 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-rococo/src/lib.rs +++ b/cumulus/parachains/runtimes/assets/asset-hub-rococo/src/lib.rs @@ -470,8 +470,7 @@ impl pallet_utility::Config for Runtime { type RuntimeEvent = RuntimeEvent; type RuntimeCall = RuntimeCall; type PalletsOrigin = OriginCaller; - type BatchPreHook = (); - type BatchPostHook = (); + type BatchHook = (); type WeightInfo = weights::pallet_utility::WeightInfo; } diff --git a/cumulus/parachains/runtimes/assets/asset-hub-westend/src/lib.rs b/cumulus/parachains/runtimes/assets/asset-hub-westend/src/lib.rs index 62a975ea19cb..a24dd29028bc 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-westend/src/lib.rs +++ b/cumulus/parachains/runtimes/assets/asset-hub-westend/src/lib.rs @@ -466,8 +466,7 @@ impl pallet_utility::Config for Runtime { type RuntimeEvent = RuntimeEvent; type RuntimeCall = RuntimeCall; type PalletsOrigin = OriginCaller; - type BatchPreHook = (); - type BatchPostHook = (); + type BatchHook = (); type WeightInfo = weights::pallet_utility::WeightInfo; } diff --git a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs index 18a08bb8d016..3219057c7b96 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs +++ b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs @@ -531,8 +531,7 @@ impl pallet_utility::Config for Runtime { type RuntimeEvent = RuntimeEvent; type RuntimeCall = RuntimeCall; type PalletsOrigin = OriginCaller; - type BatchPreHook = (); - type BatchPostHook = (); + type BatchHook = (); type WeightInfo = weights::pallet_utility::WeightInfo; } diff --git a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-westend/src/lib.rs b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-westend/src/lib.rs index 811a20c3cc96..dd3e54094836 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-westend/src/lib.rs +++ b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-westend/src/lib.rs @@ -497,8 +497,7 @@ impl pallet_utility::Config for Runtime { type RuntimeEvent = RuntimeEvent; type RuntimeCall = RuntimeCall; type PalletsOrigin = OriginCaller; - type BatchPreHook = (); - type BatchPostHook = (); + type BatchHook = (); type WeightInfo = weights::pallet_utility::WeightInfo; } diff --git a/cumulus/parachains/runtimes/collectives/collectives-westend/src/lib.rs b/cumulus/parachains/runtimes/collectives/collectives-westend/src/lib.rs index 17aca8ad7760..b2a36c64a6bb 100644 --- a/cumulus/parachains/runtimes/collectives/collectives-westend/src/lib.rs +++ b/cumulus/parachains/runtimes/collectives/collectives-westend/src/lib.rs @@ -260,8 +260,7 @@ impl pallet_utility::Config for Runtime { type RuntimeEvent = RuntimeEvent; type RuntimeCall = RuntimeCall; type PalletsOrigin = OriginCaller; - type BatchPreHook = (); - type BatchPostHook = (); + type BatchHook = (); type WeightInfo = weights::pallet_utility::WeightInfo; } diff --git a/cumulus/parachains/runtimes/contracts/contracts-rococo/src/lib.rs b/cumulus/parachains/runtimes/contracts/contracts-rococo/src/lib.rs index cb495fe798e5..6222b09acc7d 100644 --- a/cumulus/parachains/runtimes/contracts/contracts-rococo/src/lib.rs +++ b/cumulus/parachains/runtimes/contracts/contracts-rococo/src/lib.rs @@ -272,8 +272,7 @@ impl pallet_utility::Config for Runtime { type RuntimeEvent = RuntimeEvent; type RuntimeCall = RuntimeCall; type PalletsOrigin = OriginCaller; - type BatchPreHook = (); - type BatchPostHook = (); + type BatchHook = (); type WeightInfo = pallet_utility::weights::SubstrateWeight; } diff --git a/cumulus/parachains/runtimes/coretime/coretime-rococo/src/lib.rs b/cumulus/parachains/runtimes/coretime/coretime-rococo/src/lib.rs index 101e600b9c95..247b9f3ff631 100644 --- a/cumulus/parachains/runtimes/coretime/coretime-rococo/src/lib.rs +++ b/cumulus/parachains/runtimes/coretime/coretime-rococo/src/lib.rs @@ -577,8 +577,7 @@ impl pallet_utility::Config for Runtime { type RuntimeEvent = RuntimeEvent; type RuntimeCall = RuntimeCall; type PalletsOrigin = OriginCaller; - type BatchPreHook = (); - type BatchPostHook = (); + type BatchHook = (); type WeightInfo = weights::pallet_utility::WeightInfo; } diff --git a/cumulus/parachains/runtimes/coretime/coretime-westend/src/lib.rs b/cumulus/parachains/runtimes/coretime/coretime-westend/src/lib.rs index 2987b53ba8ce..10434e3bcbfd 100644 --- a/cumulus/parachains/runtimes/coretime/coretime-westend/src/lib.rs +++ b/cumulus/parachains/runtimes/coretime/coretime-westend/src/lib.rs @@ -577,8 +577,7 @@ impl pallet_utility::Config for Runtime { type RuntimeEvent = RuntimeEvent; type RuntimeCall = RuntimeCall; type PalletsOrigin = OriginCaller; - type BatchPreHook = (); - type BatchPostHook = (); + type BatchHook = (); type WeightInfo = weights::pallet_utility::WeightInfo; } diff --git a/cumulus/parachains/runtimes/people/people-rococo/src/lib.rs b/cumulus/parachains/runtimes/people/people-rococo/src/lib.rs index b4bce0f52944..5eff541be91c 100644 --- a/cumulus/parachains/runtimes/people/people-rococo/src/lib.rs +++ b/cumulus/parachains/runtimes/people/people-rococo/src/lib.rs @@ -520,8 +520,7 @@ impl pallet_utility::Config for Runtime { type RuntimeEvent = RuntimeEvent; type RuntimeCall = RuntimeCall; type PalletsOrigin = OriginCaller; - type BatchPreHook = (); - type BatchPostHook = (); + type BatchHook = (); type WeightInfo = weights::pallet_utility::WeightInfo; } diff --git a/cumulus/parachains/runtimes/people/people-westend/src/lib.rs b/cumulus/parachains/runtimes/people/people-westend/src/lib.rs index ed239395bea6..a0f99fa68345 100644 --- a/cumulus/parachains/runtimes/people/people-westend/src/lib.rs +++ b/cumulus/parachains/runtimes/people/people-westend/src/lib.rs @@ -520,8 +520,7 @@ impl pallet_utility::Config for Runtime { type RuntimeEvent = RuntimeEvent; type RuntimeCall = RuntimeCall; type PalletsOrigin = OriginCaller; - type BatchPreHook = (); - type BatchPostHook = (); + type BatchHook = (); type WeightInfo = weights::pallet_utility::WeightInfo; } diff --git a/polkadot/runtime/rococo/src/lib.rs b/polkadot/runtime/rococo/src/lib.rs index e29f095f429f..365d236d028b 100644 --- a/polkadot/runtime/rococo/src/lib.rs +++ b/polkadot/runtime/rococo/src/lib.rs @@ -716,8 +716,7 @@ impl pallet_utility::Config for Runtime { type RuntimeEvent = RuntimeEvent; type RuntimeCall = RuntimeCall; type PalletsOrigin = OriginCaller; - type BatchPreHook = (); - type BatchPostHook = (); + type BatchHook = (); type WeightInfo = weights::pallet_utility::WeightInfo; } diff --git a/polkadot/runtime/westend/src/lib.rs b/polkadot/runtime/westend/src/lib.rs index 3abc9abf382d..b9765e1f8e28 100644 --- a/polkadot/runtime/westend/src/lib.rs +++ b/polkadot/runtime/westend/src/lib.rs @@ -959,8 +959,7 @@ impl pallet_utility::Config for Runtime { type RuntimeEvent = RuntimeEvent; type RuntimeCall = RuntimeCall; type PalletsOrigin = OriginCaller; - type BatchPreHook = (); - type BatchPostHook = (); + type BatchHook = (); type WeightInfo = weights::pallet_utility::WeightInfo; } diff --git a/substrate/bin/node/runtime/src/lib.rs b/substrate/bin/node/runtime/src/lib.rs index a3843be55401..08768a4b4b96 100644 --- a/substrate/bin/node/runtime/src/lib.rs +++ b/substrate/bin/node/runtime/src/lib.rs @@ -335,8 +335,7 @@ impl pallet_utility::Config for Runtime { type RuntimeEvent = RuntimeEvent; type RuntimeCall = RuntimeCall; type PalletsOrigin = OriginCaller; - type BatchPreHook = (); - type BatchPostHook = (); + type BatchHook = (); type WeightInfo = pallet_utility::weights::SubstrateWeight; } diff --git a/substrate/frame/utility/README.md b/substrate/frame/utility/README.md index ec9d881d4548..8dcd91984bac 100644 --- a/substrate/frame/utility/README.md +++ b/substrate/frame/utility/README.md @@ -22,7 +22,7 @@ This module contains two basic pieces of functionality: Since proxy filters are respected in all dispatches of this module, it should never need to be filtered by any proxy. -Pre- and post-hooks can be configured via `BatchPostHook` and `BatchPostHook` associated types. +Pre- and post-hooks can be configured via `BatchHook` associated type. They are triggered for each batch call: `batch`, `batch_all`, and `force_batch`. Use the unit type `()` if no behavior is required. ## Interface diff --git a/substrate/frame/utility/src/lib.rs b/substrate/frame/utility/src/lib.rs index 0236980fb38a..cf8c0403d661 100644 --- a/substrate/frame/utility/src/lib.rs +++ b/substrate/frame/utility/src/lib.rs @@ -70,24 +70,20 @@ use sp_runtime::traits::{BadOrigin, Dispatchable, TrailingZeroInput}; pub use weights::WeightInfo; pub use pallet::*; -pub trait BatchPreHook { +pub trait BatchHook { fn on_batch_start() -> sp_runtime::DispatchResult; -} - -pub trait BatchPostHook { fn on_batch_end() -> sp_runtime::DispatchResult; -} -impl BatchPreHook for () { +} +impl BatchHook for () { fn on_batch_start() -> sp_runtime::DispatchResult { Ok(()) } -} -impl BatchPostHook for () { fn on_batch_end() -> sp_runtime::DispatchResult { Ok(()) } + } #[frame_support::pallet] @@ -120,10 +116,7 @@ pub mod pallet { IsType<<::RuntimeOrigin as frame_support::traits::OriginTrait>::PalletsOrigin>; ///Hook to be called before any batch operation - type BatchPreHook: BatchPreHook; - - ///Hook to be called after any batch operation - type BatchPostHook: BatchPostHook; + type BatchHook: BatchHook; /// Weight information for extrinsics in this pallet. type WeightInfo: WeightInfo; @@ -221,7 +214,7 @@ pub mod pallet { return Err(BadOrigin.into()) } - T::BatchPreHook::on_batch_start()?; + T::BatchHook::on_batch_start()?; let is_root = ensure_root(origin.clone()).is_ok(); let calls_len = calls.len(); @@ -253,7 +246,7 @@ pub mod pallet { } Self::deposit_event(Event::BatchCompleted); - T::BatchPostHook::on_batch_end()?; + T::BatchHook::on_batch_end()?; let base_weight = T::WeightInfo::batch(calls_len as u32); Ok(Some(base_weight.saturating_add(weight)).into()) @@ -335,7 +328,7 @@ pub mod pallet { return Err(BadOrigin.into()) } - T::BatchPreHook::on_batch_start()?; + T::BatchHook::on_batch_start()?; let is_root = ensure_root(origin.clone()).is_ok(); let calls_len = calls.len(); @@ -372,7 +365,7 @@ pub mod pallet { } Self::deposit_event(Event::BatchCompleted); - T::BatchPostHook::on_batch_end()?; + T::BatchHook::on_batch_end()?; let base_weight = T::WeightInfo::batch_all(calls_len as u32); Ok(Some(base_weight.saturating_add(weight)).into()) @@ -436,7 +429,7 @@ pub mod pallet { return Err(BadOrigin.into()) } - T::BatchPreHook::on_batch_start()?; + T::BatchHook::on_batch_start()?; let is_root = ensure_root(origin.clone()).is_ok(); let calls_len = calls.len(); @@ -469,7 +462,7 @@ pub mod pallet { Self::deposit_event(Event::BatchCompleted); } - T::BatchPostHook::on_batch_end()?; + T::BatchHook::on_batch_end()?; let base_weight = T::WeightInfo::batch(calls_len as u32); Ok(Some(base_weight.saturating_add(weight)).into()) diff --git a/substrate/frame/utility/src/tests.rs b/substrate/frame/utility/src/tests.rs index 5728deafd902..8cea817f36d5 100644 --- a/substrate/frame/utility/src/tests.rs +++ b/substrate/frame/utility/src/tests.rs @@ -222,8 +222,7 @@ impl Config for Test { type RuntimeEvent = RuntimeEvent; type RuntimeCall = RuntimeCall; type PalletsOrigin = OriginCaller; - type BatchPreHook = (); - type BatchPostHook = (); + type BatchHook = (); type WeightInfo = (); } From 2ac1fabc3e2dd5c8b69992bbd3bc084679a3a845 Mon Sep 17 00:00:00 2001 From: Daniel Moka Date: Fri, 27 Dec 2024 12:05:46 +0100 Subject: [PATCH 10/12] Add tumpl implementation of BatchHook MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- substrate/frame/utility/src/lib.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/substrate/frame/utility/src/lib.rs b/substrate/frame/utility/src/lib.rs index 0d11ce7edd17..80373a2ef790 100644 --- a/substrate/frame/utility/src/lib.rs +++ b/substrate/frame/utility/src/lib.rs @@ -70,11 +70,17 @@ use sp_runtime::traits::{BadOrigin, Dispatchable, TrailingZeroInput}; pub use weights::WeightInfo; pub use pallet::*; +/// 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 () { fn on_batch_start() -> sp_runtime::DispatchResult { Ok(()) From a1ae1e0d46b10939b5bad1c2248db87b56772e01 Mon Sep 17 00:00:00 2001 From: Daniel Moka Date: Fri, 27 Dec 2024 12:05:57 +0100 Subject: [PATCH 11/12] format doc MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- substrate/frame/utility/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/substrate/frame/utility/src/lib.rs b/substrate/frame/utility/src/lib.rs index 80373a2ef790..151c3286f5bf 100644 --- a/substrate/frame/utility/src/lib.rs +++ b/substrate/frame/utility/src/lib.rs @@ -121,7 +121,7 @@ pub mod pallet { Into<::RuntimeOrigin> + IsType<<::RuntimeOrigin as frame_support::traits::OriginTrait>::PalletsOrigin>; - ///Hook to be called before any batch operation + /// Hook to be called before any batch operation. type BatchHook: BatchHook; /// Weight information for extrinsics in this pallet. From 6db77788349c391860f887573d6423f3ea1584bb Mon Sep 17 00:00:00 2001 From: dmoka Date: Mon, 13 Jan 2025 10:54:33 +0100 Subject: [PATCH 12/12] add doc comment to lib --- substrate/frame/utility/src/lib.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/substrate/frame/utility/src/lib.rs b/substrate/frame/utility/src/lib.rs index 151c3286f5bf..f1aa669b9e34 100644 --- a/substrate/frame/utility/src/lib.rs +++ b/substrate/frame/utility/src/lib.rs @@ -39,6 +39,10 @@ //! Since proxy filters are respected in all dispatches of this pallet, it should never need to be //! filtered by any proxy. //! +//! Pre- and post-hooks can be configured via `BatchHook` associated type. +//! They are triggered for each batch call: `batch`, `batch_all`, and `force_batch`. +//! Use the unit type `()` if no behavior is required. +//! //! ## Interface //! //! ### Dispatchable Functions