From cb6e27c9e5e2dc30c32bafce57f01b7d6aa3bc23 Mon Sep 17 00:00:00 2001 From: Daniel Shiposha Date: Fri, 28 Oct 2022 11:22:58 +0000 Subject: [PATCH] feat: add testUtils batchAll --- --- a/Cargo.lock +++ b/Cargo.lock @@ -6729,6 +6729,7 @@ "pallet-unique-scheduler-v2", "parity-scale-codec 3.2.1", "scale-info", + "sp-std", ] [[package]] --- a/runtime/common/config/test_pallets.rs +++ b/runtime/common/config/test_pallets.rs @@ -14,8 +14,9 @@ // You should have received a copy of the GNU General Public License // along with Unique Network. If not, see . -use crate::{Runtime, RuntimeEvent}; +use crate::{Runtime, RuntimeEvent, RuntimeCall}; impl pallet_test_utils::Config for Runtime { type RuntimeEvent = RuntimeEvent; + type RuntimeCall = RuntimeCall; } --- a/test-pallets/utils/Cargo.toml +++ b/test-pallets/utils/Cargo.toml @@ -12,6 +12,7 @@ frame-system = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" } # pallet-unique-scheduler = { path = '../../pallets/scheduler', default-features = false } pallet-unique-scheduler-v2 = { path = '../../pallets/scheduler-v2', default-features = false } +sp-std = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" } [features] default = ["std"] @@ -21,5 +22,6 @@ "frame-support/std", "frame-system/std", "pallet-unique-scheduler-v2/std", + "sp-std/std", ] try-runtime = ["frame-support/try-runtime", "pallet-unique-scheduler/try-runtime"] --- a/test-pallets/utils/src/lib.rs +++ b/test-pallets/utils/src/lib.rs @@ -22,13 +22,23 @@ #[frame_support::pallet] pub mod pallet { - use frame_support::pallet_prelude::*; + use frame_support::{pallet_prelude::*, dispatch::{Dispatchable, GetDispatchInfo, PostDispatchInfo}, traits::{UnfilteredDispatchable, IsSubType, OriginTrait}}; use frame_system::pallet_prelude::*; + use sp_std::vec::Vec; use pallet_unique_scheduler_v2::{TaskName, Pallet as SchedulerPallet}; #[pallet::config] pub trait Config: frame_system::Config + pallet_unique_scheduler_v2::Config { type RuntimeEvent: From> + IsType<::RuntimeEvent>; + + /// The overarching call type. + type RuntimeCall: Parameter + + Dispatchable::RuntimeOrigin, PostInfo = PostDispatchInfo> + + GetDispatchInfo + + From> + + UnfilteredDispatchable::RuntimeOrigin> + + IsSubType> + + IsType<::RuntimeCall>; } #[pallet::event] @@ -36,6 +46,7 @@ pub enum Event { ValueIsSet, ShouldRollback, + BatchCompleted, } #[pallet::pallet] @@ -112,6 +123,35 @@ Self::ensure_origin_and_enabled(origin)?; Ok(()) } + + #[pallet::weight(10_000)] + pub fn batch_all( + origin: OriginFor, + calls: Vec<::RuntimeCall>, + ) -> DispatchResultWithPostInfo { + Self::ensure_origin_and_enabled(origin.clone())?; + + let is_root = ensure_root(origin.clone()).is_ok(); + + for call in calls { + if is_root { + call.dispatch_bypass_filter(origin.clone())?; + } else { + let mut filtered_origin = origin.clone(); + // Don't allow users to nest `batch_all` calls. + filtered_origin.add_filter( + move |c: &::RuntimeCall| { + let c = ::RuntimeCall::from_ref(c); + !matches!(c.is_sub_type(), Some(Call::batch_all { .. })) + }, + ); + call.dispatch(filtered_origin)?; + } + } + + Self::deposit_event(Event::BatchCompleted); + Ok(None::.into()) + } } } -- gitstuff