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

feat: add hooks for utility batch #6517

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,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 @@ -472,6 +472,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 @@ -543,6 +543,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 @@ -519,6 +519,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 @@ -264,6 +264,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 @@ -274,6 +274,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 @@ -583,6 +583,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 @@ -584,6 +584,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 @@ -526,6 +526,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 @@ -525,6 +525,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 @@ -748,6 +748,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 @@ -985,6 +985,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 @@ -374,6 +374,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
3 changes: 3 additions & 0 deletions substrate/frame/utility/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `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.

dmoka marked this conversation as resolved.
Show resolved Hide resolved
## Interface

### Dispatchable Functions
Expand Down
41 changes: 40 additions & 1 deletion substrate/frame/utility/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,30 @@
use sp_io::hashing::blake2_256;
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)]

Check failure on line 74 in substrate/frame/utility/src/lib.rs

View workflow job for this annotation

GitHub Actions / cargo-check-all-crate-macos

failed to resolve: use of undeclared crate or module `impl_trait_for_tuples`
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
#[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(())
}

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

}
Comment on lines +88 to +97
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
impl BatchHook for () {
fn on_batch_start() -> sp_runtime::DispatchResult {
Ok(())
}
fn on_batch_end() -> sp_runtime::DispatchResult {
Ok(())
}
}
#[impl_trait_for_tuples::impl_for_tuples(30)]
impl BatchHook for Tuple {
fn on_batch_start() -> sp_runtime::DispatchResult {
for_tuples!( #( Tuple::on_batch_start()?; )* );
Ok(())
}
fn on_batch_end() -> sp_runtime::DispatchResult {
for_tuples!( #( Tuple::on_batch_end()?; )* );
Ok(())
}
}


#[frame_support::pallet]
pub mod pallet {
use super::*;
Expand Down Expand Up @@ -100,6 +121,9 @@
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 @@ -196,6 +220,8 @@
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 @@ -225,6 +251,9 @@
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.saturating_add(weight)).into())
}
Expand Down Expand Up @@ -305,6 +334,8 @@
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 @@ -339,6 +370,9 @@
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 @@ -401,6 +435,8 @@
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 @@ -431,6 +467,9 @@
} 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 @@ -222,6 +222,7 @@ impl Config for Test {
type RuntimeEvent = RuntimeEvent;
type RuntimeCall = RuntimeCall;
type PalletsOrigin = OriginCaller;
type BatchHook = ();
type WeightInfo = ();
}

Expand Down
Loading