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).set_proof_size(u64::MAX)137 );138}139impl system::Config for Test {140 type BaseCallFilter = BaseFilter;141 type BlockWeights = BlockWeights;142 type BlockLength = ();143 type DbWeight = RocksDbWeight;144 type RuntimeOrigin = RuntimeOrigin;145 type RuntimeCall = RuntimeCall;146 type Index = u64;147 type BlockNumber = u64;148 type Hash = H256;149 type Hashing = BlakeTwo256;150 type AccountId = u64;151 type Lookup = IdentityLookup<Self::AccountId>;152 type Header = Header;153 type RuntimeEvent = RuntimeEvent;154 type BlockHashCount = ConstU64<250>;155 type Version = ();156 type PalletInfo = PalletInfo;157 type AccountData = ();158 type OnNewAccount = ();159 type OnKilledAccount = ();160 type SystemWeightInfo = ();161 type SS58Prefix = ();162 type OnSetCode = ();163 type MaxConsumers = ConstU32<16>;164}165impl logger::Config for Test {166 type RuntimeEvent = RuntimeEvent;167}168ord_parameter_types! {169 pub const One: u64 = 1;170}171172pub struct TestWeightInfo;173impl WeightInfo for TestWeightInfo {174 fn service_agendas_base() -> Weight {175 Weight::from_ref_time(0b0000_0001)176 }177 fn service_agenda_base(i: u32) -> Weight {178 Weight::from_ref_time((i << 8) as u64 + 0b0000_0010)179 }180 fn service_task_base() -> Weight {181 Weight::from_ref_time(0b0000_0100)182 }183 fn service_task_periodic() -> Weight {184 Weight::from_ref_time(0b0000_1100)185 }186 fn service_task_named() -> Weight {187 Weight::from_ref_time(0b0001_0100)188 }189 190 191 192 fn execute_dispatch_signed() -> Weight {193 Weight::from_ref_time(0b0100_0000)194 }195 fn execute_dispatch_unsigned() -> Weight {196 Weight::from_ref_time(0b1000_0000)197 }198 fn schedule(_s: u32) -> Weight {199 Weight::from_ref_time(50)200 }201 fn cancel(_s: u32) -> Weight {202 Weight::from_ref_time(50)203 }204 fn schedule_named(_s: u32) -> Weight {205 Weight::from_ref_time(50)206 }207 fn cancel_named(_s: u32) -> Weight {208 Weight::from_ref_time(50)209 }210 fn change_named_priority(_s: u32) -> Weight {211 Weight::from_ref_time(50)212 }213}214parameter_types! {215 pub MaximumSchedulerWeight: Weight = Perbill::from_percent(80) *216 BlockWeights::get().max_block;217}218219pub struct EnsureSignedOneOrRoot;220impl<O: Into<Result<RawOrigin<u64>, O>> + From<RawOrigin<u64>>> EnsureOrigin<O>221 for EnsureSignedOneOrRoot222{223 type Success = ScheduledEnsureOriginSuccess<u64>;224 fn try_origin(o: O) -> Result<Self::Success, O> {225 o.into().and_then(|o| match o {226 RawOrigin::Root => Ok(ScheduledEnsureOriginSuccess::Root),227 RawOrigin::Signed(1) => Ok(ScheduledEnsureOriginSuccess::Signed(1)),228 r => Err(O::from(r)),229 })230 }231}232233pub struct Executor;234impl DispatchCall<Test, sp_core::H160> for Executor {235 fn dispatch_call(236 signer: Option<u64>,237 function: RuntimeCall,238 ) -> Result<239 Result<PostDispatchInfo, DispatchErrorWithPostInfo<PostDispatchInfo>>,240 TransactionValidityError,241 > {242 let origin = match signer {243 Some(who) => RuntimeOrigin::signed(who),244 None => RuntimeOrigin::none(),245 };246 Ok(function.dispatch(origin))247 }248}249250impl Config for Test {251 type RuntimeEvent = RuntimeEvent;252 type RuntimeOrigin = RuntimeOrigin;253 type PalletsOrigin = OriginCaller;254 type RuntimeCall = RuntimeCall;255 type MaximumWeight = MaximumSchedulerWeight;256 type ScheduleOrigin = EnsureSignedOneOrRoot;257 type MaxScheduledPerBlock = ConstU32<10>;258 type WeightInfo = TestWeightInfo;259 type OriginPrivilegeCmp = EqualPrivilegeOnly;260 type Preimages = ();261 type PrioritySetOrigin = EnsureRoot<u64>;262 type CallExecutor = Executor;263}264265pub type LoggerCall = logger::Call<Test>;266267pub type SystemCall = frame_system::Call<Test>;268269pub fn new_test_ext() -> sp_io::TestExternalities {270 let t = system::GenesisConfig::default()271 .build_storage::<Test>()272 .unwrap();273 t.into()274}275276pub fn run_to_block(n: u64) {277 while System::block_number() < n {278 Scheduler::on_finalize(System::block_number());279 System::set_block_number(System::block_number() + 1);280 Scheduler::on_initialize(System::block_number());281 }282}283284pub fn root() -> OriginCaller {285 system::RawOrigin::Root.into()286}