difftreelog
fix only root can set/change scheduled ops priorities
in: master
1 file changed
pallets/scheduler/src/lib.rsdiffbeforeafterboth86use sp_std::{borrow::Borrow, cmp::Ordering, marker::PhantomData, prelude::*};86use sp_std::{borrow::Borrow, cmp::Ordering, marker::PhantomData, prelude::*};878788use frame_support::{88use frame_support::{89 dispatch::{DispatchError, DispatchResult, Dispatchable, Parameter, GetDispatchInfo},89 dispatch::{DispatchError, DispatchResult, Dispatchable, UnfilteredDispatchable, Parameter},90 traits::{90 traits::{91 schedule::{self, DispatchTime, MaybeHashed},91 schedule::{self, DispatchTime, MaybeHashed},92 NamedReservableCurrency, EnsureOrigin, Get, IsType, OriginTrait, PrivilegeCmp,92 NamedReservableCurrency, EnsureOrigin, Get, IsType, OriginTrait, PrivilegeCmp,135pub type Scheduled<Call, BlockNumber, PalletsOrigin, AccountId> =135pub type Scheduled<Call, BlockNumber, PalletsOrigin, AccountId> =136 ScheduledV3<Call, BlockNumber, PalletsOrigin, AccountId>;136 ScheduledV3<Call, BlockNumber, PalletsOrigin, AccountId>;137138pub enum ScheduledEnsureOriginSuccess<AccountId> {139 Root,140 Signed(AccountId),141 Unsigned,142}137143138#[cfg(feature = "runtime-benchmarks")]144#[cfg(feature = "runtime-benchmarks")]139mod preimage_provider {145mod preimage_provider {191 use frame_support::{197 use frame_support::{192 dispatch::PostDispatchInfo,198 dispatch::PostDispatchInfo,193 pallet_prelude::*,199 pallet_prelude::*,194 traits::{schedule::LookupError, PreimageProvider},200 traits::{schedule::{LookupError, LOWEST_PRIORITY}, PreimageProvider},195 };201 };196 use frame_system::pallet_prelude::*;202 use frame_system::pallet_prelude::*;197203222228223 /// The aggregated call type.229 /// The aggregated call type.224 type RuntimeCall: Parameter230 type RuntimeCall: Parameter225 + Dispatchable<231 + Dispatchable<Origin = <Self as Config>::RuntimeOrigin, PostInfo = PostDispatchInfo>226 RuntimeOrigin = <Self as Config>::RuntimeOrigin,232 + UnfilteredDispatchable<Origin = <Self as system::Config>::RuntimeOrigin>227 PostInfo = PostDispatchInfo,228 > + GetDispatchInfo233 + GetDispatchInfo229 + From<system::Call<Self>>;234 + From<system::RuntimeCall<Self>>;230235231 /// The maximum weight that may be scheduled per block for any dispatchables of less236 /// The maximum weight that may be scheduled per block for any dispatchables of less232 /// priority than `schedule::HARD_DEADLINE`.237 /// priority than `schedule::HARD_DEADLINE`.233 #[pallet::constant]238 #[pallet::constant]234 type MaximumWeight: Get<Weight>;239 type MaximumWeight: Get<Weight>;235240236 /// Required origin to schedule or cancel calls.241 /// Required origin to schedule or cancel calls.237 type ScheduleOrigin: EnsureOrigin<<Self as system::Config>::RuntimeOrigin>;242 type ScheduleOrigin: EnsureOrigin<<Self as system::Config>::RuntimeOrigin, Success = ScheduledEnsureOriginSuccess<Self::AccountId>>;243244 /// Required origin to set/change calls' priority.245 type PrioritySetOrigin: EnsureOrigin<<Self as system::Config>::RuntimeOrigin>;238246239 /// Compare the privileges of origins.247 /// Compare the privileges of origins.240 ///248 ///285293286 /// Resolve the call dispatch, including any post-dispatch operations.294 /// Resolve the call dispatch, including any post-dispatch operations.287 fn dispatch_call(295 fn dispatch_call(288 signer: T::AccountId,296 signer: Option<T::AccountId>,289 function: <T as Config>::RuntimeCall,297 function: <T as Config>::RuntimeCall,290 ) -> Result<298 ) -> Result<291 Result<PostDispatchInfo, DispatchErrorWithPostInfo<PostDispatchInfo>>,299 Result<PostDispatchInfo, DispatchErrorWithPostInfo<PostDispatchInfo>>,317 Scheduled { when: T::BlockNumber, index: u32 },325 Scheduled { when: T::BlockNumber, index: u32 },318 /// Canceled some task.326 /// Canceled some task.319 Canceled { when: T::BlockNumber, index: u32 },327 Canceled { when: T::BlockNumber, index: u32 },328 /// Scheduled task's priority has changed329 PriorityChanged { 330 when: T::BlockNumber,331 index: u32,332 priority: schedule::Priority,333 },320 /// Dispatched some task.334 /// Dispatched some task.321 Dispatched {335 Dispatched {322 task: TaskAddress<T::BlockNumber>,336 task: TaskAddress<T::BlockNumber>,432 continue;446 continue;433 }447 }434448435 let sender = ensure_signed(436 <<T as Config>::RuntimeOrigin as From<T::PalletsOrigin>>::from(449 let scheduled_origin = <<T as Config>::RuntimeOrigin as From<T::PalletsOrigin>>::from(s.origin.clone());437 s.origin.clone(),438 )439 .into(),450 let ensured_origin = T::ScheduleOrigin::ensure_origin(scheduled_origin.into()).unwrap();440 )451441 .unwrap();452 let r;442453 match ensured_origin {443 // // if call have id it was be reserved454 ScheduledEnsureOriginSuccess::Root => {444 // if s.maybe_id.is_some() {455 r = Ok(call.dispatch_bypass_filter(frame_system::RawOrigin::Root.into()));445 // let _ = T::CallExecutor::pay_for_call(456 },446 // s.maybe_id.unwrap(),457 ScheduledEnsureOriginSuccess::Signed(sender) => {447 // sender.clone(),448 // call.clone(),449 // );450 // }451452 // Execute transaction via chain default pipeline458 // Execute transaction via chain default pipeline453 // That means dispatch will be processed like any user's extrinsic e.g. transaction fees will be taken459 // That means dispatch will be processed like any user's extrinsic e.g. transaction fees will be taken454 let r = T::CallExecutor::dispatch_call(sender, call.clone());460 r = T::CallExecutor::dispatch_call(Some(sender), call.clone());461 },462 ScheduledEnsureOriginSuccess::Unsigned => {463 // Unsigned version of the above464 r = T::CallExecutor::dispatch_call(None, call.clone());465 }466 }455467456 let mut actual_call_weight: Weight = item_weight;468 let mut actual_call_weight: Weight = item_weight;457 let result: Result<_, DispatchError> = match r {469 let result: Result<_, DispatchError> = match r {521 id: ScheduledId,533 id: ScheduledId,522 when: T::BlockNumber,534 when: T::BlockNumber,523 maybe_periodic: Option<schedule::Period<T::BlockNumber>>,535 maybe_periodic: Option<schedule::Period<T::BlockNumber>>,524 priority: schedule::Priority,536 priority: Option<schedule::Priority>,525 call: Box<CallOrHashOf<T>>,537 call: Box<CallOrHashOf<T>>,526 ) -> DispatchResult {538 ) -> DispatchResult {527 T::ScheduleOrigin::ensure_origin(origin.clone())?;539 T::ScheduleOrigin::ensure_origin(origin.clone())?;540541 if priority.is_some() {542 T::PrioritySetOrigin::ensure_origin(origin.clone())?;543 }544528 let origin = <T as Config>::RuntimeOrigin::from(origin);545 let origin = <T as Config>::RuntimeOrigin::from(origin);529 Self::do_schedule_named(546 Self::do_schedule_named(530 id,547 id,531 DispatchTime::At(when),548 DispatchTime::At(when),532 maybe_periodic,549 maybe_periodic,533 priority,550 priority.unwrap_or(LOWEST_PRIORITY),534 origin.caller().clone(),551 origin.caller().clone(),535 *call,552 *call,536 )?;553 )?;557 id: ScheduledId,574 id: ScheduledId,558 after: T::BlockNumber,575 after: T::BlockNumber,559 maybe_periodic: Option<schedule::Period<T::BlockNumber>>,576 maybe_periodic: Option<schedule::Period<T::BlockNumber>>,560 priority: schedule::Priority,577 priority: Option<schedule::Priority>,561 call: Box<CallOrHashOf<T>>,578 call: Box<CallOrHashOf<T>>,562 ) -> DispatchResult {579 ) -> DispatchResult {563 T::ScheduleOrigin::ensure_origin(origin.clone())?;580 T::ScheduleOrigin::ensure_origin(origin.clone())?;581582 if priority.is_some() {583 T::PrioritySetOrigin::ensure_origin(origin.clone())?;584 }585564 let origin = <T as Config>::RuntimeOrigin::from(origin);586 let origin = <T as Config>::RuntimeOrigin::from(origin);565 Self::do_schedule_named(587 Self::do_schedule_named(566 id,588 id,567 DispatchTime::After(after),589 DispatchTime::After(after),568 maybe_periodic,590 maybe_periodic,569 priority,591 priority.unwrap_or(LOWEST_PRIORITY),570 origin.caller().clone(),592 origin.caller().clone(),571 *call,593 *call,572 )?;594 )?;573 Ok(())595 Ok(())574 }596 }597598 #[pallet::weight(<T as Config>::WeightInfo::change_named_priority(T::MaxScheduledPerBlock::get()))]599 pub fn change_named_priority(600 origin: OriginFor<T>,601 id: ScheduledId,602 priority: schedule::Priority,603 ) -> DispatchResult {604 T::PrioritySetOrigin::ensure_origin(origin.clone())?;605 let origin = <T as Config>::Origin::from(origin);606 Self::do_change_named_priority(origin.caller().clone(), id, priority)607 }575 }608 }576}609}577610725 })758 })726 }759 }760761 fn do_change_named_priority(762 origin: T::PalletsOrigin,763 id: ScheduledId,764 priority: schedule::Priority,765 ) -> DispatchResult {766 match Lookup::<T>::get(id) {767 Some((when, index)) => {768 let i = index as usize;769 Agenda::<T>::try_mutate(when, |agenda| {770 if let Some(Some(s)) = agenda.get_mut(i) {771 if matches!(772 T::OriginPrivilegeCmp::cmp_privilege(&origin, &s.origin),773 Some(Ordering::Less) | None774 ) {775 return Err(BadOrigin.into());776 }777778 s.priority = priority;779 Self::deposit_event(Event::PriorityChanged { when, index, priority });780 }781 Ok(())782 })783 },784 None => Err(Error::<T>::NotFound.into())785 }786 }727}787}728788