1234567891011121314151617use frame_support::{18 traits::{PrivilegeCmp, EnsureOrigin},19 weights::Weight,20 parameter_types,21};22use frame_system::{EnsureRoot, RawOrigin};23use sp_runtime::Perbill;24use core::cmp::Ordering;25use codec::Decode;26use crate::{27 runtime_common::{scheduler::SchedulerPaymentExecutor, config::substrate::RuntimeBlockWeights},28 Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, OriginCaller,29};30use pallet_unique_scheduler_v2::ScheduledEnsureOriginSuccess;31use up_common::types::AccountId;3233parameter_types! {34 pub MaximumSchedulerWeight: Weight = Perbill::from_percent(50) *35 RuntimeBlockWeights::get().max_block;36 pub const MaxScheduledPerBlock: u32 = 50;3738 pub const NoPreimagePostponement: Option<u32> = Some(10);39 pub const Preimage: Option<u32> = Some(10);40}4142pub struct EnsureSignedOrRoot<AccountId>(sp_std::marker::PhantomData<AccountId>);43impl<O: Into<Result<RawOrigin<AccountId>, O>> + From<RawOrigin<AccountId>>, AccountId: Decode>44 EnsureOrigin<O> for EnsureSignedOrRoot<AccountId>45{46 type Success = ScheduledEnsureOriginSuccess<AccountId>;47 fn try_origin(o: O) -> Result<Self::Success, O> {48 o.into().and_then(|o| match o {49 RawOrigin::Root => Ok(ScheduledEnsureOriginSuccess::Root),50 RawOrigin::Signed(who) => Ok(ScheduledEnsureOriginSuccess::Signed(who)),51 r => Err(O::from(r)),52 })53 }54}5556pub struct EqualOrRootOnly;57impl PrivilegeCmp<OriginCaller> for EqualOrRootOnly {58 fn cmp_privilege(left: &OriginCaller, right: &OriginCaller) -> Option<Ordering> {59 use RawOrigin::*;6061 let left = left.clone().try_into().ok()?;62 let right = right.clone().try_into().ok()?;6364 match (left, right) {65 (Root, Root) => Some(Ordering::Equal),66 (Root, _) => Some(Ordering::Greater),67 (_, Root) => Some(Ordering::Less),68 lr @ _ => (lr.0 == lr.1).then(|| Ordering::Equal),69 }70 }71}7273impl pallet_unique_scheduler_v2::Config for Runtime {74 type RuntimeEvent = RuntimeEvent;75 type RuntimeOrigin = RuntimeOrigin;76 type PalletsOrigin = OriginCaller;77 type RuntimeCall = RuntimeCall;78 type MaximumWeight = MaximumSchedulerWeight;79 type ScheduleOrigin = EnsureSignedOrRoot<AccountId>;80 type OriginPrivilegeCmp = EqualOrRootOnly;81 type MaxScheduledPerBlock = MaxScheduledPerBlock;82 type WeightInfo = ();83 type Preimages = ();84 type CallExecutor = SchedulerPaymentExecutor;85 type PrioritySetOrigin = EnsureRoot<AccountId>;86}