git.delta.rocks / unique-network / refs/commits / feb58b8452cd

difftreelog

feat scheduler v2, priority change

Daniel Shiposha2022-10-21parent: #f1b93a3.patch.diff
in: master

3 files changed

modifiedpallets/scheduler-v2/src/lib.rsdiffbeforeafterboth
79use frame_support::{79use frame_support::{
80 dispatch::{DispatchError, DispatchResult, Dispatchable, GetDispatchInfo, Parameter},80 dispatch::{DispatchError, DispatchResult, Dispatchable, GetDispatchInfo, Parameter},
81 traits::{81 traits::{
82 schedule::{self, DispatchTime},82 schedule::{self, DispatchTime, LOWEST_PRIORITY},
83 EnsureOrigin, Get, IsType, OriginTrait, PrivilegeCmp, StorageVersion, PreimageRecipient,83 EnsureOrigin, Get, IsType, OriginTrait, PrivilegeCmp, StorageVersion, PreimageRecipient,
84 ConstU32, UnfilteredDispatchable,84 ConstU32, UnfilteredDispatchable,
85 },85 },
355 /// The helper type used for custom transaction fee logic.355 /// The helper type used for custom transaction fee logic.
356 type CallExecutor: DispatchCall<Self, H160>;356 type CallExecutor: DispatchCall<Self, H160>;
357
358 /// Required origin to set/change calls' priority.
359 type PrioritySetOrigin: EnsureOrigin<<Self as system::Config>::Origin>;
357 }360 }
358361
359 #[pallet::storage]362 #[pallet::storage]
388 id: Option<[u8; 32]>,391 id: Option<[u8; 32]>,
389 result: DispatchResult,392 result: DispatchResult,
390 },393 },
394 /// Scheduled task's priority has changed
395 PriorityChanged {
396 when: T::BlockNumber,
397 index: u32,
398 priority: schedule::Priority,
399 },
391 /// The call for the provided hash was not found so the task has been aborted.400 /// The call for the provided hash was not found so the task has been aborted.
392 CallUnavailable {401 CallUnavailable {
393 task: TaskAddress<T::BlockNumber>,402 task: TaskAddress<T::BlockNumber>,
448 origin: OriginFor<T>,457 origin: OriginFor<T>,
449 when: T::BlockNumber,458 when: T::BlockNumber,
450 maybe_periodic: Option<schedule::Period<T::BlockNumber>>,459 maybe_periodic: Option<schedule::Period<T::BlockNumber>>,
451 priority: schedule::Priority,460 priority: Option<schedule::Priority>,
452 call: Box<<T as Config>::Call>,461 call: Box<<T as Config>::Call>,
453 ) -> DispatchResult {462 ) -> DispatchResult {
454 T::ScheduleOrigin::ensure_origin(origin.clone())?;463 T::ScheduleOrigin::ensure_origin(origin.clone())?;
464
465 if priority.is_some() {
466 T::PrioritySetOrigin::ensure_origin(origin.clone())?;
467 }
468
455 let origin = <T as Config>::Origin::from(origin);469 let origin = <T as Config>::Origin::from(origin);
456 Self::do_schedule(470 Self::do_schedule(
457 DispatchTime::At(when),471 DispatchTime::At(when),
458 maybe_periodic,472 maybe_periodic,
459 priority,473 priority.unwrap_or(LOWEST_PRIORITY),
460 origin.caller().clone(),474 origin.caller().clone(),
461 <ScheduledCall<T>>::new(*call)?,475 <ScheduledCall<T>>::new(*call)?,
462 )?;476 )?;
479 id: TaskName,493 id: TaskName,
480 when: T::BlockNumber,494 when: T::BlockNumber,
481 maybe_periodic: Option<schedule::Period<T::BlockNumber>>,495 maybe_periodic: Option<schedule::Period<T::BlockNumber>>,
482 priority: schedule::Priority,496 priority: Option<schedule::Priority>,
483 call: Box<<T as Config>::Call>,497 call: Box<<T as Config>::Call>,
484 ) -> DispatchResult {498 ) -> DispatchResult {
485 T::ScheduleOrigin::ensure_origin(origin.clone())?;499 T::ScheduleOrigin::ensure_origin(origin.clone())?;
500
501 if priority.is_some() {
502 T::PrioritySetOrigin::ensure_origin(origin.clone())?;
503 }
504
486 let origin = <T as Config>::Origin::from(origin);505 let origin = <T as Config>::Origin::from(origin);
487 Self::do_schedule_named(506 Self::do_schedule_named(
488 id,507 id,
489 DispatchTime::At(when),508 DispatchTime::At(when),
490 maybe_periodic,509 maybe_periodic,
491 priority,510 priority.unwrap_or(LOWEST_PRIORITY),
492 origin.caller().clone(),511 origin.caller().clone(),
493 <ScheduledCall<T>>::new(*call)?,512 <ScheduledCall<T>>::new(*call)?,
494 )?;513 )?;
514 origin: OriginFor<T>,533 origin: OriginFor<T>,
515 after: T::BlockNumber,534 after: T::BlockNumber,
516 maybe_periodic: Option<schedule::Period<T::BlockNumber>>,535 maybe_periodic: Option<schedule::Period<T::BlockNumber>>,
517 priority: schedule::Priority,536 priority: Option<schedule::Priority>,
518 call: Box<<T as Config>::Call>,537 call: Box<<T as Config>::Call>,
519 ) -> DispatchResult {538 ) -> DispatchResult {
520 T::ScheduleOrigin::ensure_origin(origin.clone())?;539 T::ScheduleOrigin::ensure_origin(origin.clone())?;
540
541 if priority.is_some() {
542 T::PrioritySetOrigin::ensure_origin(origin.clone())?;
543 }
544
521 let origin = <T as Config>::Origin::from(origin);545 let origin = <T as Config>::Origin::from(origin);
522 Self::do_schedule(546 Self::do_schedule(
523 DispatchTime::After(after),547 DispatchTime::After(after),
524 maybe_periodic,548 maybe_periodic,
525 priority,549 priority.unwrap_or(LOWEST_PRIORITY),
526 origin.caller().clone(),550 origin.caller().clone(),
527 <ScheduledCall<T>>::new(*call)?,551 <ScheduledCall<T>>::new(*call)?,
528 )?;552 )?;
540 id: TaskName,564 id: TaskName,
541 after: T::BlockNumber,565 after: T::BlockNumber,
542 maybe_periodic: Option<schedule::Period<T::BlockNumber>>,566 maybe_periodic: Option<schedule::Period<T::BlockNumber>>,
543 priority: schedule::Priority,567 priority: Option<schedule::Priority>,
544 call: Box<<T as Config>::Call>,568 call: Box<<T as Config>::Call>,
545 ) -> DispatchResult {569 ) -> DispatchResult {
546 T::ScheduleOrigin::ensure_origin(origin.clone())?;570 T::ScheduleOrigin::ensure_origin(origin.clone())?;
571
572 if priority.is_some() {
573 T::PrioritySetOrigin::ensure_origin(origin.clone())?;
574 }
575
547 let origin = <T as Config>::Origin::from(origin);576 let origin = <T as Config>::Origin::from(origin);
548 Self::do_schedule_named(577 Self::do_schedule_named(
549 id,578 id,
550 DispatchTime::After(after),579 DispatchTime::After(after),
551 maybe_periodic,580 maybe_periodic,
552 priority,581 priority.unwrap_or(LOWEST_PRIORITY),
553 origin.caller().clone(),582 origin.caller().clone(),
554 <ScheduledCall<T>>::new(*call)?,583 <ScheduledCall<T>>::new(*call)?,
555 )?;584 )?;
556 Ok(())585 Ok(())
557 }586 }
587
588 #[pallet::weight(<T as Config>::WeightInfo::change_named_priority(T::MaxScheduledPerBlock::get()))]
589 pub fn change_named_priority(
590 origin: OriginFor<T>,
591 id: TaskName,
592 priority: schedule::Priority,
593 ) -> DispatchResult {
594 T::PrioritySetOrigin::ensure_origin(origin.clone())?;
595 let origin = <T as Config>::Origin::from(origin);
596 Self::do_change_named_priority(origin.caller().clone(), id, priority)
597 }
558 }598 }
559}599}
560600
731 })771 })
732 }772 }
773
774 fn do_change_named_priority(
775 origin: T::PalletsOrigin,
776 id: TaskName,
777 priority: schedule::Priority,
778 ) -> DispatchResult {
779 match Lookup::<T>::get(id) {
780 Some((when, index)) => {
781 let i = index as usize;
782 Agenda::<T>::try_mutate(when, |agenda| {
783 if let Some(Some(s)) = agenda.get_mut(i) {
784 if matches!(
785 T::OriginPrivilegeCmp::cmp_privilege(&origin, &s.origin),
786 Some(Ordering::Less) | None
787 ) {
788 return Err(BadOrigin.into());
789 }
790
791 s.priority = priority;
792 Self::deposit_event(Event::PriorityChanged {
793 when,
794 index,
795 priority,
796 });
797 }
798 Ok(())
799 })
800 }
801 None => Err(Error::<T>::NotFound.into()),
802 }
803 }
733}804}
734805
735enum ServiceTaskError {806enum ServiceTaskError {
modifiedpallets/scheduler-v2/src/weights.rsdiffbeforeafterboth
75 fn cancel(s: u32, ) -> Weight;75 fn cancel(s: u32, ) -> Weight;
76 fn schedule_named(s: u32, ) -> Weight;76 fn schedule_named(s: u32, ) -> Weight;
77 fn cancel_named(s: u32, ) -> Weight;77 fn cancel_named(s: u32, ) -> Weight;
78 fn change_named_priority(s: u32, ) -> Weight;
78}79}
7980
80/// Weights for pallet_scheduler using the Substrate node and recommended hardware.81/// Weights for pallet_scheduler using the Substrate node and recommended hardware.
162 .saturating_add(T::DbWeight::get().writes(2 as u64))163 .saturating_add(T::DbWeight::get().writes(2 as u64))
163 }164 }
165
166 // Storage: Scheduler Lookup (r:1 w:1)
167 // Storage: Scheduler Agenda (r:1 w:1)
168 fn change_named_priority(s: u32, ) -> Weight {
169 Weight::from_ref_time(8_642_000)
170 // Standard Error: 0
171 .saturating_add(Weight::from_ref_time(431_000).saturating_mul(s as u64))
172 .saturating_add(T::DbWeight::get().reads(2 as u64))
173 .saturating_add(T::DbWeight::get().writes(2 as u64))
174 }
164}175}
165176
166// For backwards compatibility and tests177// For backwards compatibility and tests
247 .saturating_add(RocksDbWeight::get().writes(2 as u64))258 .saturating_add(RocksDbWeight::get().writes(2 as u64))
248 }259 }
260
261 // Storage: Scheduler Lookup (r:1 w:1)
262 // Storage: Scheduler Agenda (r:1 w:1)
263 fn change_named_priority(s: u32, ) -> Weight {
264 Weight::from_ref_time(8_642_000)
265 // Standard Error: 0
266 .saturating_add(Weight::from_ref_time(431_000).saturating_mul(s as u64))
267 .saturating_add(RocksDbWeight::get().reads(2 as u64))
268 .saturating_add(RocksDbWeight::get().writes(2 as u64))
269 }
249}270}
250271
modifiedruntime/common/config/pallets/scheduler.rsdiffbeforeafterboth
99 type WeightInfo = ();99 type WeightInfo = ();
100 type Preimages = ();100 type Preimages = ();
101 type CallExecutor = SchedulerPaymentExecutor;101 type CallExecutor = SchedulerPaymentExecutor;
102 type PrioritySetOrigin = EnsureRoot<AccountId>;
102}103}
103104