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.rsdiffbeforeafterboth--- 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 <http://www.gnu.org/licenses/>.
-use crate::{Runtime, RuntimeEvent};
+use crate::{Runtime, RuntimeEvent, RuntimeCall};
impl pallet_test_utils::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
+ type RuntimeCall = RuntimeCall;
}
test-pallets/utils/Cargo.tomldiffbeforeafterboth1[package]2name = "pallet-test-utils"3version = "0.1.0"4license = "GPLv3"5edition = "2021"6publish = false78[dependencies]9codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] }10scale-info = { version = "2.1.1", default-features = false, features = ["derive"] }11frame-support = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }12frame-system = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }13# pallet-unique-scheduler = { path = '../../pallets/scheduler', default-features = false }14pallet-unique-scheduler-v2 = { path = '../../pallets/scheduler-v2', default-features = false }1516[features]17default = ["std"]18std = [19 "codec/std",20 "scale-info/std",21 "frame-support/std",22 "frame-system/std",23 "pallet-unique-scheduler-v2/std",24]25try-runtime = ["frame-support/try-runtime", "pallet-unique-scheduler/try-runtime"]1[package]2name = "pallet-test-utils"3version = "0.1.0"4license = "GPLv3"5edition = "2021"6publish = false78[dependencies]9codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] }10scale-info = { version = "2.1.1", default-features = false, features = ["derive"] }11frame-support = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }12frame-system = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }13# pallet-unique-scheduler = { path = '../../pallets/scheduler', default-features = false }14pallet-unique-scheduler-v2 = { path = '../../pallets/scheduler-v2', default-features = false }15sp-std = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }1617[features]18default = ["std"]19std = [20 "codec/std",21 "scale-info/std",22 "frame-support/std",23 "frame-system/std",24 "pallet-unique-scheduler-v2/std",25 "sp-std/std",26]27try-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())
+ }
}
}