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 utility batch hooks #8

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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 @@ -437,6 +437,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<Runtime>;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,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<Runtime>;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,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<Runtime>;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,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<Runtime>;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,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<Runtime>;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,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<Runtime>;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,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<Runtime>;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,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<Runtime>;
}

Expand Down
2 changes: 2 additions & 0 deletions cumulus/parachains/runtimes/people/people-rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,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<Runtime>;
}

Expand Down
2 changes: 2 additions & 0 deletions cumulus/parachains/runtimes/people/people-westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,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<Runtime>;
}

Expand Down
2 changes: 2 additions & 0 deletions polkadot/runtime/rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,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<Runtime>;
}

Expand Down
2 changes: 2 additions & 0 deletions polkadot/runtime/westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,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<Runtime>;
}

Expand Down
2 changes: 2 additions & 0 deletions substrate/bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,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<Runtime>;
}

Expand Down
42 changes: 41 additions & 1 deletion substrate/frame/utility/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,28 @@ use sp_io::hashing::blake2_256;
use sp_runtime::traits::{BadOrigin, Dispatchable, TrailingZeroInput};
use sp_std::prelude::*;
pub use weights::WeightInfo;

pub use pallet::*;

pub trait BatchPreHook {
fn on_batch_start() -> sp_runtime::DispatchResult;
}

pub trait BatchPostHook {
fn on_batch_end() -> sp_runtime::DispatchResult;
}

impl BatchPreHook for () {
fn on_batch_start() -> sp_runtime::DispatchResult {
Ok(())
}
}

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

#[frame_support::pallet]
pub mod pallet {
use super::*;
Expand Down Expand Up @@ -98,6 +117,12 @@ 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 BatchPreHook: BatchPreHook;

///Hook to be called after any batch operation
type BatchPostHook: BatchPostHook;

/// Weight information for extrinsics in this pallet.
type WeightInfo: WeightInfo;
}
Expand Down Expand Up @@ -207,6 +232,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::<T>::TooManyCalls);
Expand Down Expand Up @@ -236,6 +263,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 + weight).into())
}
Expand Down Expand Up @@ -329,6 +359,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::<T>::TooManyCalls);
Expand Down Expand Up @@ -363,6 +395,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())
}
Expand Down Expand Up @@ -438,6 +473,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::<T>::TooManyCalls);
Expand Down Expand Up @@ -468,6 +505,9 @@ pub mod pallet {
} else {
Self::deposit_event(Event::BatchCompleted);
}

T::BatchPostHook::on_batch_end()?;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we maybe split success and fail end?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what would be the reason for splitting?


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

Expand Down
Loading