difftreelog
feat add testUtils batchAll
in: master
4 files changed
Cargo.lockdiffbeforeafterboth--- 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]]
runtime/common/config/test_pallets.rsdiffbeforeafterboth14// You should have received a copy of the GNU General Public License14// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.161617use crate::{Runtime, RuntimeEvent};17use crate::{Runtime, RuntimeEvent, RuntimeCall};181819impl pallet_test_utils::Config for Runtime {19impl pallet_test_utils::Config for Runtime {20 type RuntimeEvent = RuntimeEvent;20 type RuntimeEvent = RuntimeEvent;21 type RuntimeCall = RuntimeCall;21}22}2223test-pallets/utils/Cargo.tomldiffbeforeafterboth--- 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"]
test-pallets/utils/src/lib.rsdiffbeforeafterboth--- 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<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
+
+ /// The overarching call type.
+ type RuntimeCall: Parameter
+ + Dispatchable<RuntimeOrigin = <Self as frame_system::Config>::RuntimeOrigin, PostInfo = PostDispatchInfo>
+ + GetDispatchInfo
+ + From<frame_system::Call<Self>>
+ + UnfilteredDispatchable<RuntimeOrigin = <Self as frame_system::Config>::RuntimeOrigin>
+ + IsSubType<Call<Self>>
+ + IsType<<Self as frame_system::Config>::RuntimeCall>;
}
#[pallet::event]
@@ -36,6 +46,7 @@
pub enum Event<T: Config> {
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<T>,
+ calls: Vec<<T as Config>::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: &<T as frame_system::Config>::RuntimeCall| {
+ let c = <T as Config>::RuntimeCall::from_ref(c);
+ !matches!(c.is_sub_type(), Some(Call::batch_all { .. }))
+ },
+ );
+ call.dispatch(filtered_origin)?;
+ }
+ }
+
+ Self::deposit_event(Event::BatchCompleted);
+ Ok(None::<Weight>.into())
+ }
}
}