git.delta.rocks / unique-network / refs/commits / cb6e27c9e5e2

difftreelog

feat add testUtils batchAll

Daniel Shiposha2022-10-28parent: #ea5b83e.patch.diff
in: master

4 files changed

modifiedCargo.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]]
modifiedruntime/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;
 }
modifiedtest-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"]
modifiedtest-pallets/utils/src/lib.rsdiffbeforeafterboth
before · test-pallets/utils/src/lib.rs
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617#![cfg_attr(not(feature = "std"), no_std)]1819pub use pallet::*;20use frame_support::pallet_prelude::*;21use frame_system::pallet_prelude::*;2223#[frame_support::pallet]24pub mod pallet {25	use frame_support::pallet_prelude::*;26	use frame_system::pallet_prelude::*;27	use pallet_unique_scheduler_v2::{TaskName, Pallet as SchedulerPallet};2829	#[pallet::config]30	pub trait Config: frame_system::Config + pallet_unique_scheduler_v2::Config {31		type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;32	}3334	#[pallet::event]35	#[pallet::generate_deposit(pub(super) fn deposit_event)]36	pub enum Event<T: Config> {37		ValueIsSet,38		ShouldRollback,39	}4041	#[pallet::pallet]42	#[pallet::generate_store(pub(super) trait Store)]43	pub struct Pallet<T>(_);4445	#[pallet::storage]46	#[pallet::getter(fn is_enabled)]47	pub type Enabled<T> = StorageValue<_, bool, ValueQuery>;4849	#[pallet::storage]50	#[pallet::getter(fn test_value)]51	pub type TestValue<T> = StorageValue<_, u32, ValueQuery>;5253	#[pallet::error]54	pub enum Error<T> {55		TestPalletDisabled,56		TriggerRollback,57	}5859	#[pallet::call]60	impl<T: Config> Pallet<T> {61		#[pallet::weight(10_000)]62		pub fn enable(origin: OriginFor<T>) -> DispatchResult {63			ensure_root(origin)?;64			<Enabled<T>>::set(true);6566			Ok(())67		}6869		#[pallet::weight(10_000)]70		pub fn set_test_value(origin: OriginFor<T>, value: u32) -> DispatchResult {71			Self::ensure_origin_and_enabled(origin)?;7273			<TestValue<T>>::put(value);7475			Self::deposit_event(Event::ValueIsSet);7677			Ok(())78		}7980		#[pallet::weight(10_000)]81		pub fn set_test_value_and_rollback(origin: OriginFor<T>, value: u32) -> DispatchResult {82			Self::set_test_value(origin, value)?;8384			Self::deposit_event(Event::ShouldRollback);8586			Err(<Error<T>>::TriggerRollback.into())87		}8889		#[pallet::weight(10_000)]90		pub fn inc_test_value(origin: OriginFor<T>) -> DispatchResult {91			Self::set_test_value(origin, <TestValue<T>>::get() + 1)92		}9394		#[pallet::weight(10_000)]95		pub fn self_canceling_inc(96			origin: OriginFor<T>,97			id: TaskName,98			max_test_value: u32,99		) -> DispatchResult {100			Self::ensure_origin_and_enabled(origin.clone())?;101			Self::inc_test_value(origin.clone())?;102103			if <TestValue<T>>::get() == max_test_value {104				SchedulerPallet::<T>::cancel_named(origin, id)?;105			}106107			Ok(())108		}109110		#[pallet::weight(100_000_000)]111		pub fn just_take_fee(origin: OriginFor<T>) -> DispatchResult {112			Self::ensure_origin_and_enabled(origin)?;113			Ok(())114		}115	}116}117118impl<T: Config> Pallet<T> {119	fn ensure_origin_and_enabled(origin: OriginFor<T>) -> DispatchResult {120		ensure_signed(origin)?;121		<Enabled<T>>::get()122			.then(|| ())123			.ok_or(<Error<T>>::TestPalletDisabled.into())124	}125}
after · test-pallets/utils/src/lib.rs
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617#![cfg_attr(not(feature = "std"), no_std)]1819pub use pallet::*;20use frame_support::pallet_prelude::*;21use frame_system::pallet_prelude::*;2223#[frame_support::pallet]24pub mod pallet {25	use frame_support::{pallet_prelude::*, dispatch::{Dispatchable, GetDispatchInfo, PostDispatchInfo}, traits::{UnfilteredDispatchable, IsSubType, OriginTrait}};26	use frame_system::pallet_prelude::*;27	use sp_std::vec::Vec;28	use pallet_unique_scheduler_v2::{TaskName, Pallet as SchedulerPallet};2930	#[pallet::config]31	pub trait Config: frame_system::Config + pallet_unique_scheduler_v2::Config {32		type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;3334		/// The overarching call type.35		type RuntimeCall: Parameter36			+ Dispatchable<RuntimeOrigin = <Self as frame_system::Config>::RuntimeOrigin, PostInfo = PostDispatchInfo>37			+ GetDispatchInfo38			+ From<frame_system::Call<Self>>39			+ UnfilteredDispatchable<RuntimeOrigin = <Self as frame_system::Config>::RuntimeOrigin>40			+ IsSubType<Call<Self>>41			+ IsType<<Self as frame_system::Config>::RuntimeCall>;42	}4344	#[pallet::event]45	#[pallet::generate_deposit(pub(super) fn deposit_event)]46	pub enum Event<T: Config> {47		ValueIsSet,48		ShouldRollback,49		BatchCompleted,50	}5152	#[pallet::pallet]53	#[pallet::generate_store(pub(super) trait Store)]54	pub struct Pallet<T>(_);5556	#[pallet::storage]57	#[pallet::getter(fn is_enabled)]58	pub type Enabled<T> = StorageValue<_, bool, ValueQuery>;5960	#[pallet::storage]61	#[pallet::getter(fn test_value)]62	pub type TestValue<T> = StorageValue<_, u32, ValueQuery>;6364	#[pallet::error]65	pub enum Error<T> {66		TestPalletDisabled,67		TriggerRollback,68	}6970	#[pallet::call]71	impl<T: Config> Pallet<T> {72		#[pallet::weight(10_000)]73		pub fn enable(origin: OriginFor<T>) -> DispatchResult {74			ensure_root(origin)?;75			<Enabled<T>>::set(true);7677			Ok(())78		}7980		#[pallet::weight(10_000)]81		pub fn set_test_value(origin: OriginFor<T>, value: u32) -> DispatchResult {82			Self::ensure_origin_and_enabled(origin)?;8384			<TestValue<T>>::put(value);8586			Self::deposit_event(Event::ValueIsSet);8788			Ok(())89		}9091		#[pallet::weight(10_000)]92		pub fn set_test_value_and_rollback(origin: OriginFor<T>, value: u32) -> DispatchResult {93			Self::set_test_value(origin, value)?;9495			Self::deposit_event(Event::ShouldRollback);9697			Err(<Error<T>>::TriggerRollback.into())98		}99100		#[pallet::weight(10_000)]101		pub fn inc_test_value(origin: OriginFor<T>) -> DispatchResult {102			Self::set_test_value(origin, <TestValue<T>>::get() + 1)103		}104105		#[pallet::weight(10_000)]106		pub fn self_canceling_inc(107			origin: OriginFor<T>,108			id: TaskName,109			max_test_value: u32,110		) -> DispatchResult {111			Self::ensure_origin_and_enabled(origin.clone())?;112			Self::inc_test_value(origin.clone())?;113114			if <TestValue<T>>::get() == max_test_value {115				SchedulerPallet::<T>::cancel_named(origin, id)?;116			}117118			Ok(())119		}120121		#[pallet::weight(100_000_000)]122		pub fn just_take_fee(origin: OriginFor<T>) -> DispatchResult {123			Self::ensure_origin_and_enabled(origin)?;124			Ok(())125		}126127		#[pallet::weight(10_000)]128		pub fn batch_all(129			origin: OriginFor<T>,130			calls: Vec<<T as Config>::RuntimeCall>,131		) -> DispatchResultWithPostInfo {132			Self::ensure_origin_and_enabled(origin.clone())?;133134			let is_root = ensure_root(origin.clone()).is_ok();135136			for call in calls {137				if is_root {138					call.dispatch_bypass_filter(origin.clone())?;139				} else {140					let mut filtered_origin = origin.clone();141					// Don't allow users to nest `batch_all` calls.142					filtered_origin.add_filter(143						move |c: &<T as frame_system::Config>::RuntimeCall| {144							let c = <T as Config>::RuntimeCall::from_ref(c);145							!matches!(c.is_sub_type(), Some(Call::batch_all { .. }))146						},147					);148					call.dispatch(filtered_origin)?;149				}150			}151152			Self::deposit_event(Event::BatchCompleted);153			Ok(None::<Weight>.into())154		}155	}156}157158impl<T: Config> Pallet<T> {159	fn ensure_origin_and_enabled(origin: OriginFor<T>) -> DispatchResult {160		ensure_signed(origin)?;161		<Enabled<T>>::get()162			.then(|| ())163			.ok_or(<Error<T>>::TestPalletDisabled.into())164	}165}