12345678910111213141516171819202122232425262728293031323334353637use super::*;3839use crate as scheduler;40use frame_support::{41 ord_parameter_types, parameter_types,42 traits::{ConstU32, ConstU64, Contains, EqualPrivilegeOnly, OnFinalize, OnInitialize},43 weights::constants::RocksDbWeight,44};45use frame_system::{EnsureRoot, RawOrigin};46use sp_core::H256;47use sp_runtime::{48 testing::Header,49 traits::{BlakeTwo256, IdentityLookup},50 Perbill,51};525354#[frame_support::pallet]55pub mod logger {56 use super::{OriginCaller, OriginTrait};57 use frame_support::{pallet_prelude::*, parameter_types};58 use frame_system::pallet_prelude::*;5960 parameter_types! {61 static Log: Vec<(OriginCaller, u32)> = Vec::new();62 }63 pub fn log() -> Vec<(OriginCaller, u32)> {64 Log::get().clone()65 }6667 #[pallet::pallet]68 #[pallet::generate_store(pub(super) trait Store)]69 pub struct Pallet<T>(PhantomData<T>);7071 #[pallet::hooks]72 impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}7374 #[pallet::config]75 pub trait Config: frame_system::Config {76 type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;77 }7879 #[pallet::event]80 #[pallet::generate_deposit(pub(super) fn deposit_event)]81 pub enum Event<T: Config> {82 Logged(u32, Weight),83 }8485 #[pallet::call]86 impl<T: Config> Pallet<T>87 where88 <T as frame_system::Config>::RuntimeOrigin: OriginTrait<PalletsOrigin = OriginCaller>,89 {90 #[pallet::weight(*weight)]91 pub fn log(origin: OriginFor<T>, i: u32, weight: Weight) -> DispatchResult {92 Self::deposit_event(Event::Logged(i, weight));93 Log::mutate(|log| {94 log.push((origin.caller().clone(), i));95 });96 Ok(())97 }9899 #[pallet::weight(*weight)]100 pub fn log_without_filter(origin: OriginFor<T>, i: u32, weight: Weight) -> DispatchResult {101 Self::deposit_event(Event::Logged(i, weight));102 Log::mutate(|log| {103 log.push((origin.caller().clone(), i));104 });105 Ok(())106 }107 }108}109110type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;111type Block = frame_system::mocking::MockBlock<Test>;112113frame_support::construct_runtime!(114 pub enum Test where115 Block = Block,116 NodeBlock = Block,117 UncheckedExtrinsic = UncheckedExtrinsic,118 {119 System: frame_system::{Pallet, Call, Config, Storage, Event<T>},120 Logger: logger::{Pallet, Call, Event<T>},121 Scheduler: scheduler::{Pallet, Call, Storage, Event<T>},122 }123);124125126pub struct BaseFilter;127impl Contains<RuntimeCall> for BaseFilter {128 fn contains(call: &RuntimeCall) -> bool {129 !matches!(call, RuntimeCall::Logger(LoggerCall::log { .. }))130 }131}132133parameter_types! {134 pub BlockWeights: frame_system::limits::BlockWeights =135 frame_system::limits::BlockWeights::simple_max(136 Weight::from_ref_time(2_000_000_000_000)137 138 );139}140impl system::Config for Test {141 type BaseCallFilter = BaseFilter;142 type BlockWeights = BlockWeights;143 type BlockLength = ();144 type DbWeight = RocksDbWeight;145 type RuntimeOrigin = RuntimeOrigin;146 type RuntimeCall = RuntimeCall;147 type Index = u64;148 type BlockNumber = u64;149 type Hash = H256;150 type Hashing = BlakeTwo256;151 type AccountId = u64;152 type Lookup = IdentityLookup<Self::AccountId>;153 type Header = Header;154 type RuntimeEvent = RuntimeEvent;155 type BlockHashCount = ConstU64<250>;156 type Version = ();157 type PalletInfo = PalletInfo;158 type AccountData = ();159 type OnNewAccount = ();160 type OnKilledAccount = ();161 type SystemWeightInfo = ();162 type SS58Prefix = ();163 type OnSetCode = ();164 type MaxConsumers = ConstU32<16>;165}166impl logger::Config for Test {167 type RuntimeEvent = RuntimeEvent;168}169ord_parameter_types! {170 pub const One: u64 = 1;171}172173pub struct TestWeightInfo;174impl WeightInfo for TestWeightInfo {175 fn service_agendas_base() -> Weight {176 Weight::from_ref_time(0b0000_0001)177 }178 fn service_agenda_base(i: u32) -> Weight {179 Weight::from_ref_time((i << 8) as u64 + 0b0000_0010)180 }181 fn service_task_base() -> Weight {182 Weight::from_ref_time(0b0000_0100)183 }184 fn service_task_periodic() -> Weight {185 Weight::from_ref_time(0b0000_1100)186 }187 fn service_task_named() -> Weight {188 Weight::from_ref_time(0b0001_0100)189 }190 fn service_task_fetched(s: u32) -> Weight {191 Weight::from_ref_time((s << 8) as u64 + 0b0010_0100)192 }193 fn execute_dispatch_signed() -> Weight {194 Weight::from_ref_time(0b0100_0000)195 }196 fn execute_dispatch_unsigned() -> Weight {197 Weight::from_ref_time(0b1000_0000)198 }199 fn schedule(_s: u32) -> Weight {200 Weight::from_ref_time(50)201 }202 fn cancel(_s: u32) -> Weight {203 Weight::from_ref_time(50)204 }205 fn schedule_named(_s: u32) -> Weight {206 Weight::from_ref_time(50)207 }208 fn cancel_named(_s: u32) -> Weight {209 Weight::from_ref_time(50)210 }211 fn change_named_priority(_s: u32) -> Weight {212 Weight::from_ref_time(50)213 }214}215parameter_types! {216 pub MaximumSchedulerWeight: Weight = Perbill::from_percent(80) *217 BlockWeights::get().max_block;218}219220pub struct EnsureSignedOneOrRoot;221impl<O: Into<Result<RawOrigin<u64>, O>> + From<RawOrigin<u64>>> EnsureOrigin<O>222 for EnsureSignedOneOrRoot223{224 type Success = ScheduledEnsureOriginSuccess<u64>;225 fn try_origin(o: O) -> Result<Self::Success, O> {226 o.into().and_then(|o| match o {227 RawOrigin::Root => Ok(ScheduledEnsureOriginSuccess::Root),228 RawOrigin::Signed(1) => Ok(ScheduledEnsureOriginSuccess::Signed(1)),229 r => Err(O::from(r)),230 })231 }232}233234pub struct Executor;235impl DispatchCall<Test, sp_core::H160> for Executor {236 fn dispatch_call(237 signer: Option<u64>,238 function: RuntimeCall,239 ) -> Result<240 Result<PostDispatchInfo, DispatchErrorWithPostInfo<PostDispatchInfo>>,241 TransactionValidityError,242 > {243 let origin = match signer {244 Some(who) => RuntimeOrigin::signed(who),245 None => RuntimeOrigin::none(),246 };247 Ok(function.dispatch(origin))248 }249}250251impl Config for Test {252 type RuntimeEvent = RuntimeEvent;253 type RuntimeOrigin = RuntimeOrigin;254 type PalletsOrigin = OriginCaller;255 type RuntimeCall = RuntimeCall;256 type MaximumWeight = MaximumSchedulerWeight;257 type ScheduleOrigin = EnsureSignedOneOrRoot;258 type MaxScheduledPerBlock = ConstU32<10>;259 type WeightInfo = TestWeightInfo;260 type OriginPrivilegeCmp = EqualPrivilegeOnly;261 type Preimages = ();262 type PrioritySetOrigin = EnsureRoot<u64>;263 type CallExecutor = Executor;264}265266pub type LoggerCall = logger::Call<Test>;267268pub fn new_test_ext() -> sp_io::TestExternalities {269 let t = system::GenesisConfig::default()270 .build_storage::<Test>()271 .unwrap();272 t.into()273}274275pub fn run_to_block(n: u64) {276 while System::block_number() < n {277 Scheduler::on_finalize(System::block_number());278 System::set_block_number(System::block_number() + 1);279 Scheduler::on_initialize(System::block_number());280 }281}282283pub fn root() -> OriginCaller {284 system::RawOrigin::Root.into()285}