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 pub struct Pallet<T>(PhantomData<T>);6970 #[pallet::hooks]71 impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}7273 #[pallet::config]74 pub trait Config: frame_system::Config {75 type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;76 }7778 #[pallet::event]79 #[pallet::generate_deposit(pub(super) fn deposit_event)]80 pub enum Event<T: Config> {81 Logged(u32, Weight),82 }8384 #[pallet::call]85 impl<T: Config> Pallet<T>86 where87 <T as frame_system::Config>::RuntimeOrigin: OriginTrait<PalletsOrigin = OriginCaller>,88 {89 #[pallet::call_index(0)]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::call_index(1)]100 #[pallet::weight(*weight)]101 pub fn log_without_filter(origin: OriginFor<T>, i: u32, weight: Weight) -> DispatchResult {102 Self::deposit_event(Event::Logged(i, weight));103 Log::mutate(|log| {104 log.push((origin.caller().clone(), i));105 });106 Ok(())107 }108 }109}110111type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;112type Block = frame_system::mocking::MockBlock<Test>;113114frame_support::construct_runtime!(115 pub enum Test where116 Block = Block,117 NodeBlock = Block,118 UncheckedExtrinsic = UncheckedExtrinsic,119 {120 System: frame_system::{Pallet, Call, Config, Storage, Event<T>},121 Logger: logger::{Pallet, Call, Event<T>},122 Scheduler: scheduler::{Pallet, Call, Storage, Event<T>},123 }124);125126127pub struct BaseFilter;128impl Contains<RuntimeCall> for BaseFilter {129 fn contains(call: &RuntimeCall) -> bool {130 !matches!(call, RuntimeCall::Logger(LoggerCall::log { .. }))131 }132}133134parameter_types! {135 pub BlockWeights: frame_system::limits::BlockWeights =136 frame_system::limits::BlockWeights::simple_max(137 Weight::from_ref_time(2_000_000_000_000).set_proof_size(u64::MAX)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 191 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 type SystemCall = frame_system::Call<Test>;269270pub fn new_test_ext() -> sp_io::TestExternalities {271 let t = system::GenesisConfig::default()272 .build_storage::<Test>()273 .unwrap();274 t.into()275}276277pub fn run_to_block(n: u64) {278 while System::block_number() < n {279 Scheduler::on_finalize(System::block_number());280 System::set_block_number(System::block_number() + 1);281 Scheduler::on_initialize(System::block_number());282 }283}284285pub fn root() -> OriginCaller {286 system::RawOrigin::Root.into()287}