Skip to content

Commit

Permalink
add batch hook associated type so we can support custom logic such as…
Browse files Browse the repository at this point in the history
… unified events ie. in hydration parachain
  • Loading branch information
dmoka committed Dec 27, 2024
1 parent ae76255 commit 688f8b3
Show file tree
Hide file tree
Showing 18 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@ impl pallet_utility::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type RuntimeCall = RuntimeCall;
type PalletsOrigin = OriginCaller;
type BatchHook = ();
type WeightInfo = weights::pallet_utility::WeightInfo<Runtime>;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ impl pallet_utility::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type RuntimeCall = RuntimeCall;
type PalletsOrigin = OriginCaller;
type BatchHook = ();
type WeightInfo = weights::pallet_utility::WeightInfo<Runtime>;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,7 @@ impl pallet_utility::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type RuntimeCall = RuntimeCall;
type PalletsOrigin = OriginCaller;
type BatchHook = ();
type WeightInfo = weights::pallet_utility::WeightInfo<Runtime>;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@ impl pallet_utility::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type RuntimeCall = RuntimeCall;
type PalletsOrigin = OriginCaller;
type BatchHook = ();
type WeightInfo = weights::pallet_utility::WeightInfo<Runtime>;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ impl pallet_utility::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type RuntimeCall = RuntimeCall;
type PalletsOrigin = OriginCaller;
type BatchHook = ();
type WeightInfo = weights::pallet_utility::WeightInfo<Runtime>;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ impl pallet_utility::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type RuntimeCall = RuntimeCall;
type PalletsOrigin = OriginCaller;
type BatchHook = ();
type WeightInfo = pallet_utility::weights::SubstrateWeight<Runtime>;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ impl pallet_utility::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type RuntimeCall = RuntimeCall;
type PalletsOrigin = OriginCaller;
type BatchHook = ();
type WeightInfo = weights::pallet_utility::WeightInfo<Runtime>;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@ impl pallet_utility::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type RuntimeCall = RuntimeCall;
type PalletsOrigin = OriginCaller;
type BatchHook = ();
type WeightInfo = weights::pallet_utility::WeightInfo<Runtime>;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ impl pallet_utility::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type RuntimeCall = RuntimeCall;
type PalletsOrigin = OriginCaller;
type BatchHook = ();
type WeightInfo = weights::pallet_utility::WeightInfo<Runtime>;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ impl pallet_utility::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type RuntimeCall = RuntimeCall;
type PalletsOrigin = OriginCaller;
type BatchHook = ();
type WeightInfo = weights::pallet_utility::WeightInfo<Runtime>;
}

Expand Down
1 change: 1 addition & 0 deletions polkadot/runtime/rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,7 @@ impl pallet_utility::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type RuntimeCall = RuntimeCall;
type PalletsOrigin = OriginCaller;
type BatchHook = ();
type WeightInfo = weights::pallet_utility::WeightInfo<Runtime>;
}

Expand Down
1 change: 1 addition & 0 deletions polkadot/runtime/westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,7 @@ impl pallet_utility::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type RuntimeCall = RuntimeCall;
type PalletsOrigin = OriginCaller;
type BatchHook = ();
type WeightInfo = weights::pallet_utility::WeightInfo<Runtime>;
}

Expand Down
1 change: 1 addition & 0 deletions substrate/bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ impl pallet_utility::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type RuntimeCall = RuntimeCall;
type PalletsOrigin = OriginCaller;
type BatchHook = ();
type WeightInfo = pallet_utility::weights::SubstrateWeight<Runtime>;
}

Expand Down
2 changes: 2 additions & 0 deletions substrate/frame/utility/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ sp-io = { path = "../../primitives/io", default-features = false }
sp-runtime = { path = "../../primitives/runtime", default-features = false }
sp-std = { path = "../../primitives/std", default-features = false }

impl-trait-for-tuples = "0.2.2"

[dev-dependencies]
pallet-balances = { path = "../balances" }
pallet-root-testing = { path = "../root-testing" }
Expand Down
4 changes: 4 additions & 0 deletions substrate/frame/utility/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ 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 `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
Expand Down
41 changes: 41 additions & 0 deletions substrate/frame/utility/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,28 @@ pub use weights::WeightInfo;

pub use pallet::*;

/// Hooks that will be called for `batch` calls.
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(())
}

fn on_batch_end() -> sp_runtime::DispatchResult {
Ok(())
}

}


#[frame_support::pallet]
pub mod pallet {
use super::*;
Expand Down Expand Up @@ -98,6 +120,10 @@ 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.
type BatchHook: BatchHook;

/// Weight information for extrinsics in this pallet.
type WeightInfo: WeightInfo;
}
Expand Down Expand Up @@ -207,6 +233,8 @@ pub mod pallet {
return Err(BadOrigin.into())
}

T::BatchHook::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::<T>::TooManyCalls);
Expand Down Expand Up @@ -236,6 +264,9 @@ pub mod pallet {
Self::deposit_event(Event::ItemCompleted);
}
Self::deposit_event(Event::BatchCompleted);

T::BatchHook::on_batch_end()?;

let base_weight = T::WeightInfo::batch(calls_len as u32);
Ok(Some(base_weight + weight).into())
}
Expand Down Expand Up @@ -329,6 +360,8 @@ pub mod pallet {
return Err(BadOrigin.into())
}

T::BatchHook::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::<T>::TooManyCalls);
Expand Down Expand Up @@ -363,6 +396,9 @@ pub mod pallet {
Self::deposit_event(Event::ItemCompleted);
}
Self::deposit_event(Event::BatchCompleted);

T::BatchHook::on_batch_end()?;

let base_weight = T::WeightInfo::batch_all(calls_len as u32);
Ok(Some(base_weight.saturating_add(weight)).into())
}
Expand Down Expand Up @@ -438,6 +474,8 @@ pub mod pallet {
return Err(BadOrigin.into())
}

T::BatchHook::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::<T>::TooManyCalls);
Expand Down Expand Up @@ -468,6 +506,9 @@ pub mod pallet {
} else {
Self::deposit_event(Event::BatchCompleted);
}

T::BatchHook::on_batch_end()?;

let base_weight = T::WeightInfo::batch(calls_len as u32);
Ok(Some(base_weight.saturating_add(weight)).into())
}
Expand Down
1 change: 1 addition & 0 deletions substrate/frame/utility/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ impl Config for Test {
type RuntimeEvent = RuntimeEvent;
type RuntimeCall = RuntimeCall;
type PalletsOrigin = OriginCaller;
type BatchHook = ();
type WeightInfo = ();
}

Expand Down

0 comments on commit 688f8b3

Please sign in to comment.