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
--- a/pallets/scheduler-v2/src/lib.rs
+++ b/pallets/scheduler-v2/src/lib.rs
@@ -79,7 +79,7 @@
 use frame_support::{
 	dispatch::{DispatchError, DispatchResult, Dispatchable, GetDispatchInfo, Parameter},
 	traits::{
-		schedule::{self, DispatchTime},
+		schedule::{self, DispatchTime, LOWEST_PRIORITY},
 		EnsureOrigin, Get, IsType, OriginTrait, PrivilegeCmp, StorageVersion, PreimageRecipient,
 		ConstU32, UnfilteredDispatchable,
 	},
@@ -354,6 +354,9 @@
 
 		/// The helper type used for custom transaction fee logic.
 		type CallExecutor: DispatchCall<Self, H160>;
+
+		/// Required origin to set/change calls' priority.
+		type PrioritySetOrigin: EnsureOrigin<<Self as system::Config>::Origin>;
 	}
 
 	#[pallet::storage]
@@ -388,6 +391,12 @@
 			id: Option<[u8; 32]>,
 			result: DispatchResult,
 		},
+		/// Scheduled task's priority has changed
+		PriorityChanged {
+			when: T::BlockNumber,
+			index: u32,
+			priority: schedule::Priority,
+		},
 		/// The call for the provided hash was not found so the task has been aborted.
 		CallUnavailable {
 			task: TaskAddress<T::BlockNumber>,
@@ -448,15 +457,20 @@
 			origin: OriginFor<T>,
 			when: T::BlockNumber,
 			maybe_periodic: Option<schedule::Period<T::BlockNumber>>,
-			priority: schedule::Priority,
+			priority: Option<schedule::Priority>,
 			call: Box<<T as Config>::Call>,
 		) -> DispatchResult {
 			T::ScheduleOrigin::ensure_origin(origin.clone())?;
+
+			if priority.is_some() {
+				T::PrioritySetOrigin::ensure_origin(origin.clone())?;
+			}
+
 			let origin = <T as Config>::Origin::from(origin);
 			Self::do_schedule(
 				DispatchTime::At(when),
 				maybe_periodic,
-				priority,
+				priority.unwrap_or(LOWEST_PRIORITY),
 				origin.caller().clone(),
 				<ScheduledCall<T>>::new(*call)?,
 			)?;
@@ -479,16 +493,21 @@
 			id: TaskName,
 			when: T::BlockNumber,
 			maybe_periodic: Option<schedule::Period<T::BlockNumber>>,
-			priority: schedule::Priority,
+			priority: Option<schedule::Priority>,
 			call: Box<<T as Config>::Call>,
 		) -> DispatchResult {
 			T::ScheduleOrigin::ensure_origin(origin.clone())?;
+
+			if priority.is_some() {
+				T::PrioritySetOrigin::ensure_origin(origin.clone())?;
+			}
+
 			let origin = <T as Config>::Origin::from(origin);
 			Self::do_schedule_named(
 				id,
 				DispatchTime::At(when),
 				maybe_periodic,
-				priority,
+				priority.unwrap_or(LOWEST_PRIORITY),
 				origin.caller().clone(),
 				<ScheduledCall<T>>::new(*call)?,
 			)?;
@@ -514,15 +533,20 @@
 			origin: OriginFor<T>,
 			after: T::BlockNumber,
 			maybe_periodic: Option<schedule::Period<T::BlockNumber>>,
-			priority: schedule::Priority,
+			priority: Option<schedule::Priority>,
 			call: Box<<T as Config>::Call>,
 		) -> DispatchResult {
 			T::ScheduleOrigin::ensure_origin(origin.clone())?;
+
+			if priority.is_some() {
+				T::PrioritySetOrigin::ensure_origin(origin.clone())?;
+			}
+
 			let origin = <T as Config>::Origin::from(origin);
 			Self::do_schedule(
 				DispatchTime::After(after),
 				maybe_periodic,
-				priority,
+				priority.unwrap_or(LOWEST_PRIORITY),
 				origin.caller().clone(),
 				<ScheduledCall<T>>::new(*call)?,
 			)?;
@@ -540,21 +564,37 @@
 			id: TaskName,
 			after: T::BlockNumber,
 			maybe_periodic: Option<schedule::Period<T::BlockNumber>>,
-			priority: schedule::Priority,
+			priority: Option<schedule::Priority>,
 			call: Box<<T as Config>::Call>,
 		) -> DispatchResult {
 			T::ScheduleOrigin::ensure_origin(origin.clone())?;
+
+			if priority.is_some() {
+				T::PrioritySetOrigin::ensure_origin(origin.clone())?;
+			}
+
 			let origin = <T as Config>::Origin::from(origin);
 			Self::do_schedule_named(
 				id,
 				DispatchTime::After(after),
 				maybe_periodic,
-				priority,
+				priority.unwrap_or(LOWEST_PRIORITY),
 				origin.caller().clone(),
 				<ScheduledCall<T>>::new(*call)?,
 			)?;
 			Ok(())
 		}
+
+		#[pallet::weight(<T as Config>::WeightInfo::change_named_priority(T::MaxScheduledPerBlock::get()))]
+		pub fn change_named_priority(
+			origin: OriginFor<T>,
+			id: TaskName,
+			priority: schedule::Priority,
+		) -> DispatchResult {
+			T::PrioritySetOrigin::ensure_origin(origin.clone())?;
+			let origin = <T as Config>::Origin::from(origin);
+			Self::do_change_named_priority(origin.caller().clone(), id, priority)
+		}
 	}
 }
 
@@ -730,6 +770,37 @@
 			}
 		})
 	}
+
+	fn do_change_named_priority(
+		origin: T::PalletsOrigin,
+		id: TaskName,
+		priority: schedule::Priority,
+	) -> DispatchResult {
+		match Lookup::<T>::get(id) {
+			Some((when, index)) => {
+				let i = index as usize;
+				Agenda::<T>::try_mutate(when, |agenda| {
+					if let Some(Some(s)) = agenda.get_mut(i) {
+						if matches!(
+							T::OriginPrivilegeCmp::cmp_privilege(&origin, &s.origin),
+							Some(Ordering::Less) | None
+						) {
+							return Err(BadOrigin.into());
+						}
+
+						s.priority = priority;
+						Self::deposit_event(Event::PriorityChanged {
+							when,
+							index,
+							priority,
+						});
+					}
+					Ok(())
+				})
+			}
+			None => Err(Error::<T>::NotFound.into()),
+		}
+	}
 }
 
 enum ServiceTaskError {
modifiedpallets/scheduler-v2/src/weights.rsdiffbeforeafterboth
before · pallets/scheduler-v2/src/weights.rs
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617// Original license:18// This file is part of Substrate.1920// Copyright (C) 2022 Parity Technologies (UK) Ltd.21// SPDX-License-Identifier: Apache-2.02223// Licensed under the Apache License, Version 2.0 (the "License");24// you may not use this file except in compliance with the License.25// You may obtain a copy of the License at26//27// http://www.apache.org/licenses/LICENSE-2.028//29// Unless required by applicable law or agreed to in writing, software30// distributed under the License is distributed on an "AS IS" BASIS,31// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.32// See the License for the specific language governing permissions and33// limitations under the License.3435//! Autogenerated weights for pallet_scheduler36//!37//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev38//! DATE: 2022-10-03, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]`39//! HOSTNAME: `bm3`, CPU: `Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz`40//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 10244142// Executed Command:43// /home/benchbot/cargo_target_dir/production/substrate44// benchmark45// pallet46// --steps=5047// --repeat=2048// --extrinsic=*49// --execution=wasm50// --wasm-execution=compiled51// --heap-pages=409652// --pallet=pallet_scheduler53// --chain=dev54// --output=./frame/scheduler/src/weights.rs55// --template=./.maintain/frame-weight-template.hbs5657#![cfg_attr(rustfmt, rustfmt_skip)]58#![allow(unused_parens)]59#![allow(unused_imports)]6061use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}};62use sp_std::marker::PhantomData;6364/// Weight functions needed for pallet_scheduler.65pub trait WeightInfo {66	fn service_agendas_base() -> Weight;67	fn service_agenda_base(s: u32, ) -> Weight;68	fn service_task_base() -> Weight;69	fn service_task_fetched(s: u32, ) -> Weight;70	fn service_task_named() -> Weight;71	fn service_task_periodic() -> Weight;72	fn execute_dispatch_signed() -> Weight;73	fn execute_dispatch_unsigned() -> Weight;74	fn schedule(s: u32, ) -> Weight;75	fn cancel(s: u32, ) -> Weight;76	fn schedule_named(s: u32, ) -> Weight;77	fn cancel_named(s: u32, ) -> Weight;78}7980/// Weights for pallet_scheduler using the Substrate node and recommended hardware.81pub struct SubstrateWeight<T>(PhantomData<T>);82impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {83	// Storage: Scheduler IncompleteSince (r:1 w:1)84	fn service_agendas_base() -> Weight {85		Weight::from_ref_time(4_992_000 as u64)86			.saturating_add(T::DbWeight::get().reads(1 as u64))87			.saturating_add(T::DbWeight::get().writes(1 as u64))88	}89	// Storage: Scheduler Agenda (r:1 w:1)90	/// The range of component `s` is `[0, 512]`.91	fn service_agenda_base(s: u32, ) -> Weight {92		Weight::from_ref_time(4_320_000 as u64)93			// Standard Error: 61994			.saturating_add(Weight::from_ref_time(336_713 as u64).saturating_mul(s as u64))95			.saturating_add(T::DbWeight::get().reads(1 as u64))96			.saturating_add(T::DbWeight::get().writes(1 as u64))97	}98	fn service_task_base() -> Weight {99		Weight::from_ref_time(10_864_000 as u64)100	}101	// Storage: Preimage PreimageFor (r:1 w:1)102	// Storage: Preimage StatusFor (r:1 w:1)103	/// The range of component `s` is `[128, 4194304]`.104	fn service_task_fetched(s: u32, ) -> Weight {105		Weight::from_ref_time(24_586_000 as u64)106			// Standard Error: 1107			.saturating_add(Weight::from_ref_time(1_138 as u64).saturating_mul(s as u64))108			.saturating_add(T::DbWeight::get().reads(2 as u64))109			.saturating_add(T::DbWeight::get().writes(2 as u64))110	}111	// Storage: Scheduler Lookup (r:0 w:1)112	fn service_task_named() -> Weight {113		Weight::from_ref_time(13_127_000 as u64)114			.saturating_add(T::DbWeight::get().writes(1 as u64))115	}116	fn service_task_periodic() -> Weight {117		Weight::from_ref_time(11_053_000 as u64)118	}119	fn execute_dispatch_signed() -> Weight {120		Weight::from_ref_time(4_158_000 as u64)121	}122	fn execute_dispatch_unsigned() -> Weight {123		Weight::from_ref_time(4_104_000 as u64)124	}125	// Storage: Scheduler Agenda (r:1 w:1)126	/// The range of component `s` is `[0, 511]`.127	fn schedule(s: u32, ) -> Weight {128		Weight::from_ref_time(20_074_000 as u64)129			// Standard Error: 765130			.saturating_add(Weight::from_ref_time(343_285 as u64).saturating_mul(s as u64))131			.saturating_add(T::DbWeight::get().reads(1 as u64))132			.saturating_add(T::DbWeight::get().writes(1 as u64))133	}134	// Storage: Scheduler Agenda (r:1 w:1)135	// Storage: Scheduler Lookup (r:0 w:1)136	/// The range of component `s` is `[1, 512]`.137	fn cancel(s: u32, ) -> Weight {138		Weight::from_ref_time(21_509_000 as u64)139			// Standard Error: 708140			.saturating_add(Weight::from_ref_time(323_013 as u64).saturating_mul(s as u64))141			.saturating_add(T::DbWeight::get().reads(1 as u64))142			.saturating_add(T::DbWeight::get().writes(2 as u64))143	}144	// Storage: Scheduler Lookup (r:1 w:1)145	// Storage: Scheduler Agenda (r:1 w:1)146	/// The range of component `s` is `[0, 511]`.147	fn schedule_named(s: u32, ) -> Weight {148		Weight::from_ref_time(22_427_000 as u64)149			// Standard Error: 850150			.saturating_add(Weight::from_ref_time(357_265 as u64).saturating_mul(s as u64))151			.saturating_add(T::DbWeight::get().reads(2 as u64))152			.saturating_add(T::DbWeight::get().writes(2 as u64))153	}154	// Storage: Scheduler Lookup (r:1 w:1)155	// Storage: Scheduler Agenda (r:1 w:1)156	/// The range of component `s` is `[1, 512]`.157	fn cancel_named(s: u32, ) -> Weight {158		Weight::from_ref_time(22_875_000 as u64)159			// Standard Error: 693160			.saturating_add(Weight::from_ref_time(336_643 as u64).saturating_mul(s as u64))161			.saturating_add(T::DbWeight::get().reads(2 as u64))162			.saturating_add(T::DbWeight::get().writes(2 as u64))163	}164}165166// For backwards compatibility and tests167impl WeightInfo for () {168	// Storage: Scheduler IncompleteSince (r:1 w:1)169	fn service_agendas_base() -> Weight {170		Weight::from_ref_time(4_992_000 as u64)171			.saturating_add(RocksDbWeight::get().reads(1 as u64))172			.saturating_add(RocksDbWeight::get().writes(1 as u64))173	}174	// Storage: Scheduler Agenda (r:1 w:1)175	/// The range of component `s` is `[0, 512]`.176	fn service_agenda_base(s: u32, ) -> Weight {177		Weight::from_ref_time(4_320_000 as u64)178			// Standard Error: 619179			.saturating_add(Weight::from_ref_time(336_713 as u64).saturating_mul(s as u64))180			.saturating_add(RocksDbWeight::get().reads(1 as u64))181			.saturating_add(RocksDbWeight::get().writes(1 as u64))182	}183	fn service_task_base() -> Weight {184		Weight::from_ref_time(10_864_000 as u64)185	}186	// Storage: Preimage PreimageFor (r:1 w:1)187	// Storage: Preimage StatusFor (r:1 w:1)188	/// The range of component `s` is `[128, 4194304]`.189	fn service_task_fetched(s: u32, ) -> Weight {190		Weight::from_ref_time(24_586_000 as u64)191			// Standard Error: 1192			.saturating_add(Weight::from_ref_time(1_138 as u64).saturating_mul(s as u64))193			.saturating_add(RocksDbWeight::get().reads(2 as u64))194			.saturating_add(RocksDbWeight::get().writes(2 as u64))195	}196	// Storage: Scheduler Lookup (r:0 w:1)197	fn service_task_named() -> Weight {198		Weight::from_ref_time(13_127_000 as u64)199			.saturating_add(RocksDbWeight::get().writes(1 as u64))200	}201	fn service_task_periodic() -> Weight {202		Weight::from_ref_time(11_053_000 as u64)203	}204	fn execute_dispatch_signed() -> Weight {205		Weight::from_ref_time(4_158_000 as u64)206	}207	fn execute_dispatch_unsigned() -> Weight {208		Weight::from_ref_time(4_104_000 as u64)209	}210	// Storage: Scheduler Agenda (r:1 w:1)211	/// The range of component `s` is `[0, 511]`.212	fn schedule(s: u32, ) -> Weight {213		Weight::from_ref_time(20_074_000 as u64)214			// Standard Error: 765215			.saturating_add(Weight::from_ref_time(343_285 as u64).saturating_mul(s as u64))216			.saturating_add(RocksDbWeight::get().reads(1 as u64))217			.saturating_add(RocksDbWeight::get().writes(1 as u64))218	}219	// Storage: Scheduler Agenda (r:1 w:1)220	// Storage: Scheduler Lookup (r:0 w:1)221	/// The range of component `s` is `[1, 512]`.222	fn cancel(s: u32, ) -> Weight {223		Weight::from_ref_time(21_509_000 as u64)224			// Standard Error: 708225			.saturating_add(Weight::from_ref_time(323_013 as u64).saturating_mul(s as u64))226			.saturating_add(RocksDbWeight::get().reads(1 as u64))227			.saturating_add(RocksDbWeight::get().writes(2 as u64))228	}229	// Storage: Scheduler Lookup (r:1 w:1)230	// Storage: Scheduler Agenda (r:1 w:1)231	/// The range of component `s` is `[0, 511]`.232	fn schedule_named(s: u32, ) -> Weight {233		Weight::from_ref_time(22_427_000 as u64)234			// Standard Error: 850235			.saturating_add(Weight::from_ref_time(357_265 as u64).saturating_mul(s as u64))236			.saturating_add(RocksDbWeight::get().reads(2 as u64))237			.saturating_add(RocksDbWeight::get().writes(2 as u64))238	}239	// Storage: Scheduler Lookup (r:1 w:1)240	// Storage: Scheduler Agenda (r:1 w:1)241	/// The range of component `s` is `[1, 512]`.242	fn cancel_named(s: u32, ) -> Weight {243		Weight::from_ref_time(22_875_000 as u64)244			// Standard Error: 693245			.saturating_add(Weight::from_ref_time(336_643 as u64).saturating_mul(s as u64))246			.saturating_add(RocksDbWeight::get().reads(2 as u64))247			.saturating_add(RocksDbWeight::get().writes(2 as u64))248	}249}
modifiedruntime/common/config/pallets/scheduler.rsdiffbeforeafterboth
--- a/runtime/common/config/pallets/scheduler.rs
+++ b/runtime/common/config/pallets/scheduler.rs
@@ -99,4 +99,5 @@
 	type WeightInfo = ();
 	type Preimages = ();
 	type CallExecutor = SchedulerPaymentExecutor;
+	type PrioritySetOrigin = EnsureRoot<AccountId>;
 }