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

difftreelog

fix enable scheduler v2

Daniel Shiposha2022-10-20parent: #6930406.patch.diff
in: master

6 files changed

modifiedpallets/scheduler-v2/src/benchmarking.rsdiffbeforeafterboth
102 call,
103 maybe_periodic,
104 origin,
105 _phantom: PhantomData,
106 }
97}107}
98108
99fn bounded<T: Config>(len: u32) -> Option<ScheduledCall<T>> {109fn bounded<T: Config>(len: u32) -> Option<ScheduledCall<T>> {
100 let call =110 let call = <<T as Config>::Call>::from(SystemCall::remark {
101 <<T as Config>::Call>::from(SystemCall::remark { remark: vec![0; len as usize] });111 remark: vec![0; len as usize],
112 });
102 ScheduledCall::new(call).ok()113 ScheduledCall::new(call).ok()
103}114}
104115
105fn make_call<T: Config>(maybe_lookup_len: Option<u32>) -> ScheduledCall<T> {116fn make_call<T: Config>(maybe_lookup_len: Option<u32>) -> ScheduledCall<T> {
106 let bound = EncodedCall::bound() as u32;117 let bound = EncodedCall::bound() as u32;
107 let mut len = match maybe_lookup_len {118 let mut len = match maybe_lookup_len {
108 Some(len) => len.min(<T::Preimages as PreimageRecipient<T::Hash>>::MaxSize::get() - 2).max(bound) - 3,119 Some(len) => {
120 len.min(<T::Preimages as PreimageRecipient<T::Hash>>::MaxSize::get() - 2)
121 .max(bound) - 3
122 }
109 None => bound.saturating_sub(4),123 None => bound.saturating_sub(4),
110 };124 };
111125
114 Some(x) => x,128 Some(x) => x,
115 None => {129 None => {
116 len -= 1;130 len -= 1;
117 continue131 continue;
118 },132 }
119 };133 };
120 if c.lookup_needed() == maybe_lookup_len.is_some() {134 if c.lookup_needed() == maybe_lookup_len.is_some() {
121 break c135 break c;
122 }136 }
123 if maybe_lookup_len.is_some() {137 if maybe_lookup_len.is_some() {
124 len += 1;138 len += 1;
125 } else {139 } else {
126 if len > 0 {140 if len > 0 {
127 len -= 1;141 len -= 1;
128 } else {142 } else {
129 break c143 break c;
130 }144 }
131 }145 }
132 }146 }
142fn dummy_counter() -> WeightCounter {156fn dummy_counter() -> WeightCounter {
143 WeightCounter { used: Weight::zero(), limit: Weight::MAX }157 WeightCounter {
158 used: Weight::zero(),
159 limit: Weight::MAX,
160 }
144}161}
145162
modifiedpallets/scheduler-v2/src/lib.rsdiffbeforeafterboth
78use codec::{Codec, Decode, Encode, MaxEncodedLen};78use codec::{Codec, Decode, Encode, MaxEncodedLen};
79use frame_support::{79use frame_support::{
80 dispatch::{80 dispatch::{DispatchError, DispatchResult, Dispatchable, GetDispatchInfo, Parameter},
81 DispatchError, DispatchResult, Dispatchable, GetDispatchInfo, Parameter, RawOrigin,
82 },
83 ensure,
84 traits::{81 traits::{
85 schedule::{self, DispatchTime},82 schedule::{self, DispatchTime},
86 EnsureOrigin, Get, IsType, OriginTrait,83 EnsureOrigin, Get, IsType, OriginTrait, PrivilegeCmp, StorageVersion, PreimageRecipient,
87 PalletInfoAccess, PrivilegeCmp, StorageVersion,
88 PreimageProvider, PreimageRecipient, ConstU32,84 ConstU32,
89 },85 },
90 weights::Weight,86 weights::Weight,
91};87};
9288
93use frame_system::{self as system};89use frame_system::{self as system};
94use scale_info::TypeInfo;90use scale_info::TypeInfo;
95use sp_io::hashing::blake2_256;
96use sp_runtime::{91use sp_runtime::{
97 traits::{BadOrigin, One, Saturating, Zero, Hash},92 traits::{BadOrigin, One, Saturating, Zero, Hash},
98 BoundedVec, RuntimeDebug,93 BoundedVec, RuntimeDebug,
132128
133 Ok(Self::PreimageLookup { hash, unbounded_len: len as u32 })129 Ok(Self::PreimageLookup {
130 hash,
131 unbounded_len: len as u32,
132 })
134 }133 }
135 }134 }
171impl<T: Config, PP: PreimageRecipient<T::Hash>> SchedulerPreimages<T> for PP {174impl<T: Config, PP: PreimageRecipient<T::Hash>> SchedulerPreimages<T> for PP {
172 fn drop(call: &ScheduledCall<T>) {175 fn drop(call: &ScheduledCall<T>) {
173 match call {176 match call {
174 ScheduledCall::Inline(_) => {},177 ScheduledCall::Inline(_) => {}
175 ScheduledCall::PreimageLookup { hash, .. } => Self::unrequest_preimage(hash),178 ScheduledCall::PreimageLookup { hash, .. } => Self::unrequest_preimage(hash),
176 }179 }
177 }180 }
178181
179 fn peek(call: &ScheduledCall<T>) -> Result<(<T as pallet::Config>::Call, Option<u32>), DispatchError> {182 fn peek(
183 call: &ScheduledCall<T>,
184 ) -> Result<(<T as pallet::Config>::Call, Option<u32>), DispatchError> {
180 match call {185 match call {
181 ScheduledCall::Inline(data) => Ok((ScheduledCall::<T>::decode(data)?, None)),186 ScheduledCall::Inline(data) => Ok((ScheduledCall::<T>::decode(data)?, None)),
182 ScheduledCall::PreimageLookup { hash, unbounded_len } => {187 ScheduledCall::PreimageLookup {
188 hash,
189 unbounded_len,
190 } => {
183 let (preimage, len) = Self::get_preimage(hash)191 let (preimage, len) = Self::get_preimage(hash)
184 .ok_or(<Error<T>>::PreimageNotFound)192 .ok_or(<Error<T>>::PreimageNotFound)
185 .map(|preimage| (preimage, *unbounded_len))?;193 .map(|preimage| (preimage, *unbounded_len))?;
186194
187 Ok((ScheduledCall::<T>::decode(preimage.as_slice())?, Some(len)))195 Ok((ScheduledCall::<T>::decode(preimage.as_slice())?, Some(len)))
188 },196 }
189 }197 }
190 }198 }
191199
303 type Call: Parameter313 type Call: Parameter
304 + Dispatchable<314 + Dispatchable<Origin = <Self as Config>::Origin, PostInfo = PostDispatchInfo>
305 Origin = <Self as Config>::Origin,
306 PostInfo = PostDispatchInfo,
307 > + GetDispatchInfo315 + GetDispatchInfo
308 + From<system::Call<Self>>;316 + From<system::Call<Self>>;
309317
402 fn on_initialize(now: T::BlockNumber) -> Weight {419 fn on_initialize(now: T::BlockNumber) -> Weight {
403 let mut weight_counter =420 let mut weight_counter = WeightCounter {
404 WeightCounter { used: Weight::zero(), limit: T::MaximumWeight::get() };421 used: Weight::zero(),
422 limit: T::MaximumWeight::get(),
423 };
405 Self::service_agendas(&mut weight_counter, now, u32::max_value());424 Self::service_agendas(&mut weight_counter, now, u32::max_value());
406 weight_counter.used425 weight_counter.used
537 };556 };
538557
539 if when <= now {558 if when <= now {
540 return Err(Error::<T>::TargetBlockNumberInPast.into())559 return Err(Error::<T>::TargetBlockNumberInPast.into());
541 }560 }
542561
543 Ok(when)562 Ok(when)
555 }574 }
556 Self::deposit_event(Event::Scheduled { when: address.0, index: address.1 });575 Self::deposit_event(Event::Scheduled {
576 when: address.0,
577 index: address.1,
578 });
557 Ok(address)579 Ok(address)
558 }580 }
571 agenda[hole_index] = Some(what);593 agenda[hole_index] = Some(what);
572 hole_index as u32594 hole_index as u32
573 } else {595 } else {
574 return Err((<Error<T>>::AgendaIsExhausted.into(), what))596 return Err((<Error<T>>::AgendaIsExhausted.into(), what));
575 }597 }
576 };598 };
577 Agenda::<T>::insert(when, agenda);599 Agenda::<T>::insert(when, agenda);
616 T::OriginPrivilegeCmp::cmp_privilege(o, &s.origin),638 T::OriginPrivilegeCmp::cmp_privilege(o, &s.origin),
617 Some(Ordering::Less) | None639 Some(Ordering::Less) | None
618 ) {640 ) {
619 return Err(BadOrigin.into())641 return Err(BadOrigin.into());
620 }642 }
621 };643 };
622 Ok(s.take())644 Ok(s.take())
632 Self::deposit_event(Event::Canceled { when, index });654 Self::deposit_event(Event::Canceled { when, index });
633 Ok(())655 Ok(())
634 } else {656 } else {
635 return Err(Error::<T>::NotFound.into())657 return Err(Error::<T>::NotFound.into());
636 }658 }
637 }659 }
638660
646 ) -> Result<TaskAddress<T::BlockNumber>, DispatchError> {668 ) -> Result<TaskAddress<T::BlockNumber>, DispatchError> {
647 // ensure id it is unique669 // ensure id it is unique
648 if Lookup::<T>::contains_key(&id) {670 if Lookup::<T>::contains_key(&id) {
649 return Err(Error::<T>::FailedToSchedule.into())671 return Err(Error::<T>::FailedToSchedule.into());
650 }672 }
651673
652 let when = Self::resolve_time(when)?;674 let when = Self::resolve_time(when)?;
679 T::OriginPrivilegeCmp::cmp_privilege(o, &s.origin),701 T::OriginPrivilegeCmp::cmp_privilege(o, &s.origin),
680 Some(Ordering::Less) | None702 Some(Ordering::Less) | None
681 ) {703 ) {
682 return Err(BadOrigin.into())704 return Err(BadOrigin.into());
683 }705 }
684 T::Preimages::drop(&s.call);706 T::Preimages::drop(&s.call);
685 }707 }
690 Self::deposit_event(Event::Canceled { when, index });712 Self::deposit_event(Event::Canceled { when, index });
691 Ok(())713 Ok(())
692 } else {714 } else {
693 return Err(Error::<T>::NotFound.into())715 return Err(Error::<T>::NotFound.into());
694 }716 }
695 })717 })
696 }718 }
708 /// Service up to `max` agendas queue starting from earliest incompletely executed agenda.730 /// Service up to `max` agendas queue starting from earliest incompletely executed agenda.
709 fn service_agendas(weight: &mut WeightCounter, now: T::BlockNumber, max: u32) {731 fn service_agendas(weight: &mut WeightCounter, now: T::BlockNumber, max: u32) {
710 if !weight.check_accrue(T::WeightInfo::service_agendas_base()) {732 if !weight.check_accrue(T::WeightInfo::service_agendas_base()) {
711 return733 return;
712 }734 }
713735
714 let mut incomplete_since = now + One::one();736 let mut incomplete_since = now + One::one();
770 );797 );
771 if !weight.can_accrue(base_weight) {798 if !weight.can_accrue(base_weight) {
772 postponed += 1;799 postponed += 1;
773 break800 break;
774 }801 }
775 let result = Self::service_task(weight, now, when, agenda_index, *executed == 0, task);802 let result = Self::service_task(weight, now, when, agenda_index, *executed == 0, task);
776 agenda[agenda_index as usize] = match result {803 agenda[agenda_index as usize] = match result {
777 Err((Unavailable, slot)) => {804 Err((Unavailable, slot)) => {
778 dropped += 1;805 dropped += 1;
779 slot806 slot
780 },807 }
781 Err((Overweight, slot)) => {808 Err((Overweight, slot)) => {
782 postponed += 1;809 postponed += 1;
783 slot810 slot
784 },811 }
785 Ok(()) => {812 Ok(()) => {
786 *executed += 1;813 *executed += 1;
787 None814 None
788 },815 }
789 };816 };
790 }817 }
791 if postponed > 0 || dropped > 0 {818 if postponed > 0 || dropped > 0 {
833 id: task.maybe_id,860 id: task.maybe_id,
834 });861 });
835 Err((Unavailable, Some(task)))862 Err((Unavailable, Some(task)))
836 },863 }
837 Err(Overweight) if is_first => {864 Err(Overweight) if is_first => {
838 T::Preimages::drop(&task.call);865 T::Preimages::drop(&task.call);
839 Self::deposit_event(Event::PermanentlyOverweight {866 Self::deposit_event(Event::PermanentlyOverweight {
840 task: (when, agenda_index),867 task: (when, agenda_index),
841 id: task.maybe_id,868 id: task.maybe_id,
842 });869 });
843 Err((Unavailable, Some(task)))870 Err((Unavailable, Some(task)))
844 },871 }
845 Err(Overweight) => Err((Overweight, Some(task))),872 Err(Overweight) => Err((Overweight, Some(task))),
846 Ok(result) => {873 Ok(result) => {
847 Self::deposit_event(Event::Dispatched {874 Self::deposit_event(Event::Dispatched {
857 }884 }
858 let wake = now.saturating_add(period);885 let wake = now.saturating_add(period);
859 match Self::place_task(wake, task) {886 match Self::place_task(wake, task) {
860 Ok(_) => {},887 Ok(_) => {}
861 Err((_, task)) => {888 Err((_, task)) => {
862 // TODO: Leave task in storage somewhere for it to be rescheduled889 // TODO: Leave task in storage somewhere for it to be rescheduled
863 // manually.890 // manually.
866 task: (when, agenda_index),893 task: (when, agenda_index),
867 id: task.maybe_id,894 id: task.maybe_id,
868 });895 });
869 },896 }
870 }897 }
871 } else {898 } else {
872 T::Preimages::drop(&task.call);899 T::Preimages::drop(&task.call);
873 }900 }
874 Ok(())901 Ok(())
875 },902 }
876 }903 }
877 }904 }
878905
897 let max_weight = base_weight.saturating_add(call_weight);924 let max_weight = base_weight.saturating_add(call_weight);
898925
899 if !weight.can_accrue(max_weight) {926 if !weight.can_accrue(max_weight) {
900 return Err(Overweight)927 return Err(Overweight);
901 }928 }
902929
903 let (maybe_actual_call_weight, result) = match call.dispatch(dispatch_origin) {930 let (maybe_actual_call_weight, result) = match call.dispatch(dispatch_origin) {
904 Ok(post_info) => (post_info.actual_weight, Ok(())),931 Ok(post_info) => (post_info.actual_weight, Ok(())),
905 Err(error_and_info) =>932 Err(error_and_info) => (
906 (error_and_info.post_info.actual_weight, Err(error_and_info.error)),933 error_and_info.post_info.actual_weight,
934 Err(error_and_info.error),
935 ),
907 };936 };
908 let call_weight = maybe_actual_call_weight.unwrap_or(call_weight);937 let call_weight = maybe_actual_call_weight.unwrap_or(call_weight);
modifiedruntime/common/config/pallets/scheduler.rsdiffbeforeafterboth
70 }70 }
71}71}
7272
73impl pallet_unique_scheduler::Config for Runtime {73// impl pallet_unique_scheduler::Config for Runtime {
74 type RuntimeEvent = RuntimeEvent;74// type RuntimeEvent = RuntimeEvent;
75 type RuntimeOrigin = RuntimeOrigin;75// type RuntimeOrigin = RuntimeOrigin;
76 type Currency = Balances;76// type Currency = Balances;
77 type PalletsOrigin = OriginCaller;77// type PalletsOrigin = OriginCaller;
78 type RuntimeCall = RuntimeCall;78// type RuntimeCall = RuntimeCall;
79 type MaximumWeight = MaximumSchedulerWeight;79// type MaximumWeight = MaximumSchedulerWeight;
80 type ScheduleOrigin = EnsureSignedOrRoot<AccountId>;80// type ScheduleOrigin = EnsureSignedOrRoot<AccountId>;
81 type PrioritySetOrigin = EnsureRoot<AccountId>;81// type PrioritySetOrigin = EnsureRoot<AccountId>;
82 type MaxScheduledPerBlock = MaxScheduledPerBlock;82// type MaxScheduledPerBlock = MaxScheduledPerBlock;
83 type WeightInfo = ();83// type WeightInfo = ();
84 type CallExecutor = SchedulerPaymentExecutor;84// type CallExecutor = SchedulerPaymentExecutor;
85 type OriginPrivilegeCmp = EqualOrRootOnly;85// type OriginPrivilegeCmp = EqualOrRootOnly;
86 type PreimageProvider = ();86// type PreimageProvider = ();
87 type NoPreimagePostponement = NoPreimagePostponement;87// type NoPreimagePostponement = NoPreimagePostponement;
88}88// }
8989
90impl pallet_unique_scheduler_v2::Config for Runtime {90impl pallet_unique_scheduler_v2::Config for Runtime {
91 type Event = Event;91 type Event = Event;
modifiedruntime/common/construct_runtime/mod.rsdiffbeforeafterboth
57 Inflation: pallet_inflation::{Pallet, Call, Storage} = 60,57 Inflation: pallet_inflation::{Pallet, Call, Storage} = 60,
58 Unique: pallet_unique::{Pallet, Call, Storage, Event<T>} = 61,58 Unique: pallet_unique::{Pallet, Call, Storage, Event<T>} = 61,
5959
60 #[runtimes(opal)]60 // #[runtimes(opal)]
61 Scheduler: pallet_unique_scheduler::{Pallet, Call, Storage, Event<T>} = 62,61 // Scheduler: pallet_unique_scheduler::{Pallet, Call, Storage, Event<T>} = 62,
6262
63 Configuration: pallet_configuration::{Pallet, Call, Storage} = 63,63 Configuration: pallet_configuration::{Pallet, Call, Storage} = 63,
6464
97 Maintenance: pallet_maintenance::{Pallet, Call, Storage, Event<T>} = 154,97 Maintenance: pallet_maintenance::{Pallet, Call, Storage, Event<T>} = 154,
9898
99 #[runtimes(opal)]99 #[runtimes(opal)]
100 SchedulerV2: pallet_unique_scheduler_v2::{Pallet, Call, Storage, Event<T>} = 154,100 Scheduler: pallet_unique_scheduler_v2::{Pallet, Call, Storage, Event<T>} = 154,
101101
102 #[runtimes(opal)]102 #[runtimes(opal)]
103 TestUtils: pallet_test_utils = 255,103 TestUtils: pallet_test_utils = 255,
modifiedtest-pallets/utils/Cargo.tomldiffbeforeafterboth
10scale-info = { version = "2.1.1", default-features = false, features = ["derive"] }10scale-info = { version = "2.1.1", default-features = false, features = ["derive"] }
11frame-support = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }11frame-support = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
12frame-system = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }12frame-system = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
13# pallet-unique-scheduler = { path = '../../pallets/scheduler', default-features = false }
13pallet-unique-scheduler = { path = '../../pallets/scheduler', default-features = false }14pallet-unique-scheduler-v2 = { path = '../../pallets/scheduler-v2', default-features = false }
1415
15[features]16[features]
16default = ["std"]17default = ["std"]
19 "scale-info/std",20 "scale-info/std",
20 "frame-support/std",21 "frame-support/std",
21 "frame-system/std",22 "frame-system/std",
22 "pallet-unique-scheduler/std",23 "pallet-unique-scheduler-v2/std",
23]24]
24try-runtime = ["frame-support/try-runtime", "pallet-unique-scheduler/try-runtime"]25try-runtime = ["frame-support/try-runtime", "pallet-unique-scheduler/try-runtime"]
2526
modifiedtest-pallets/utils/src/lib.rsdiffbeforeafterboth
24pub mod pallet {24pub mod pallet {
25 use frame_support::pallet_prelude::*;25 use frame_support::pallet_prelude::*;
26 use frame_system::pallet_prelude::*;26 use frame_system::pallet_prelude::*;
27 use pallet_unique_scheduler::{ScheduledId, Pallet as SchedulerPallet};27 use pallet_unique_scheduler_v2::{TaskName, Pallet as SchedulerPallet};
2828
29 #[pallet::config]29 #[pallet::config]
30 pub trait Config: frame_system::Config + pallet_unique_scheduler::Config {30 pub trait Config: frame_system::Config + pallet_unique_scheduler::Config {
94 #[pallet::weight(10_000)]94 #[pallet::weight(10_000)]
95 pub fn self_canceling_inc(95 pub fn self_canceling_inc(
96 origin: OriginFor<T>,96 origin: OriginFor<T>,
97 id: ScheduledId,97 id: TaskName,
98 max_test_value: u32,98 max_test_value: u32,
99 ) -> DispatchResult {99 ) -> DispatchResult {
100 Self::ensure_origin_and_enabled(origin.clone())?;100 Self::ensure_origin_and_enabled(origin.clone())?;