From 0f7bb4adb1802bc36e080200fd41e751c438f433 Mon Sep 17 00:00:00 2001
From: kozyrevdev <73348153+kozyrevdev@users.noreply.github.com>
Date: Fri, 03 Jun 2022 14:22:14 +0000
Subject: [PATCH] Merge pull request #341 from UniqueNetwork/feature/simple-scheduler
Feature/simple scheduler
---
--- a/pallets/scheduler/Cargo.toml
+++ b/pallets/scheduler/Cargo.toml
@@ -15,11 +15,13 @@
scale-info = { version = "2.0.1", default-features = false, features = [
"derive",
] }
+
frame-support = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.22" }
frame-system = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.22" }
sp-runtime = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.22" }
sp-std = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.22" }
sp-io = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.22" }
+sp-core = { default-features = false, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.22' }
frame-benchmarking = { default-features = false, optional = true, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.22" }
up-sponsorship = { version = "0.1.0", default-features = false, git = "https://github.com/uniquenetwork/pallet-sponsoring", branch = "polkadot-v0.9.22" }
@@ -40,6 +42,7 @@
"up-sponsorship/std",
"sp-io/std",
"sp-std/std",
+ "sp-core/std",
"log/std",
]
runtime-benchmarks = [
--- a/pallets/scheduler/src/lib.rs
+++ b/pallets/scheduler/src/lib.rs
@@ -1,23 +1,6 @@
-// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.
-// This file is part of Unique Network.
-
-// Unique Network is free software: you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-
-// Unique Network is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-
-// You should have received a copy of the GNU General Public License
-// along with Unique Network. If not, see .
-
-// Original license
// This file is part of Substrate.
-// Copyright (C) 2017-2021 Parity Technologies (UK) Ltd.
+// Copyright (C) 2017-2022 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
@@ -32,16 +15,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.
-//! # Scheduler
-//! A module for scheduling dispatches.
-//!
-//! - [`Config`]
-//! - [`Call`]
-//! - [`Module`]
-//!
-//! ## Overview
+//! # Schedulerdo_reschedule
//!
-//! This module exposes capabilities for scheduling dispatches to occur at a
+//! This Pallet exposes capabilities for scheduling dispatches to occur at a
//! specified block number or at a specified period. These scheduled dispatches
//! may be named or anonymous and may be canceled.
//!
@@ -57,106 +33,59 @@
//!
//! ### Dispatchable Functions
//!
-//! * `schedule` - schedule a dispatch, which may be periodic, to occur at a
-//! specified block and with a specified priority.
-//! * `cancel` - cancel a scheduled dispatch, specified by block number and
-//! index.
-//! * `schedule_named` - augments the `schedule` interface with an additional
-//! `Vec` parameter that can be used for identification.
+//! * `schedule` - schedule a dispatch, which may be periodic, to occur at a specified block and
+//! with a specified priority.
+//! * `cancel` - cancel a scheduled dispatch, specified by block number and index.
+//! * `schedule_named` - augments the `schedule` interface with an additional `Vec` parameter
+//! that can be used for identification.
//! * `cancel_named` - the named complement to the cancel function.
// Ensure we're `no_std` when compiling for Wasm.
#![cfg_attr(not(feature = "std"), no_std)]
-#![allow(clippy::type_complexity, clippy::boxed_local, clippy::unused_unit)]
+#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
+
pub mod weights;
-use sp_std::{prelude::*, marker::PhantomData, borrow::Borrow};
-use codec::{Encode, Decode, Codec};
+use sp_core::H160;
+use codec::{Codec, Decode, Encode};
+use frame_system::{self as system, ensure_signed};
+pub use pallet::*;
+use scale_info::TypeInfo;
use sp_runtime::{
- RuntimeDebug,
- traits::{Zero, One, BadOrigin, Saturating},
+ traits::{BadOrigin, One, Saturating, Zero},
+ RuntimeDebug, DispatchErrorWithPostInfo,
};
+use sp_std::{borrow::Borrow, cmp::Ordering, marker::PhantomData, prelude::*};
+
use frame_support::{
- decl_module, decl_storage, decl_event, decl_error,
- dispatch::{Dispatchable, DispatchError, DispatchResult, Parameter},
+ dispatch::{DispatchError, DispatchResult, Dispatchable, Parameter},
traits::{
- Get,
- schedule::{self, DispatchTime},
- OriginTrait, EnsureOrigin, IsType,
+ schedule::{self, DispatchTime, MaybeHashed},
+ NamedReservableCurrency, EnsureOrigin, Get, IsType, OriginTrait, PrivilegeCmp,
+ StorageVersion,
},
weights::{GetDispatchInfo, Weight},
};
-use frame_system::{self as system, ensure_signed};
-pub use weights::WeightInfo;
-use up_sponsorship::SponsorshipHandler;
-use scale_info::TypeInfo;
-/// Our pallet's configuration trait. All our types and constants go in here. If the
-/// pallet is dependent on specific other pallets, then their configuration traits
-/// should be added to our implied traits list.
-///
-/// `system::Config` should always be included in our implied traits.
-/// //
-pub trait Config: system::Config {
- /// The overarching event type.
- type Event: From> + Into<::Event>;
-
- /// The aggregated origin which the dispatch will take.
- type Origin: OriginTrait
- + From
- + IsType<::Origin>;
-
- /// The caller origin, overarching type of all pallets origins.
- type PalletsOrigin: From> + Codec + TypeInfo + Clone + Eq;
-
- /// The aggregated call type.
- type Call: Parameter
- + Dispatchable::Origin>
- + GetDispatchInfo
- + From>;
-
- /// The maximum weight that may be scheduled per block for any dispatchables of less priority
- /// than `schedule::HARD_DEADLINE`.
- type MaximumWeight: Get;
-
- /// Required origin to schedule or cancel calls.
- type ScheduleOrigin: EnsureOrigin<::Origin>;
-
- /// The maximum number of scheduled calls in the queue for a single block.
- /// Not strictly enforced, but used for weight estimation.
- type MaxScheduledPerBlock: Get;
-
- /// Sponsoring function
- type SponsorshipHandler: SponsorshipHandler::Call>;
-
- /// Weight information for extrinsics in this pallet.
- type WeightInfo: WeightInfo;
-}
+pub use weights::WeightInfo;
-// pub type SelfWeightInfo = ::WeightInfo;
-
/// Just a simple index for naming period tasks.
pub type PeriodicIndex = u32;
/// The location of a scheduled task that can be used to remove it.
pub type TaskAddress = (BlockNumber, u32);
+pub const MAX_TASK_ID_LENGTH_IN_BYTES: u8 = 16;
-#[cfg_attr(any(feature = "std", test), derive(PartialEq, Eq))]
-#[derive(Clone, RuntimeDebug, Encode, Decode)]
-struct ScheduledV1 {
- maybe_id: Option>,
- priority: schedule::Priority,
- call: Call,
- maybe_periodic: Option>,
-}
+type ScheduledId = [u8; MAX_TASK_ID_LENGTH_IN_BYTES as usize];
+pub type CallOrHashOf = MaybeHashed<::Call, ::Hash>;
/// Information regarding an item to be executed in the future.
#[cfg_attr(any(feature = "std", test), derive(PartialEq, Eq))]
#[derive(Clone, RuntimeDebug, Encode, Decode, TypeInfo)]
-pub struct ScheduledV2 {
+pub struct ScheduledV3 {
/// The unique identity for this task, if there is one.
- maybe_id: Option>,
+ maybe_id: Option,
/// This task's priority.
priority: schedule::Priority,
/// The call to be dispatched.
@@ -168,63 +97,213 @@
_phantom: PhantomData,
}
+pub type ScheduledV3Of = ScheduledV3<
+ CallOrHashOf,
+ ::BlockNumber,
+ ::PalletsOrigin,
+ ::AccountId,
+>;
+
+pub type ScheduledOf = ScheduledV3Of;
+
/// The current version of Scheduled struct.
pub type Scheduled =
- ScheduledV2;
+ ScheduledV3;
-// A value placed in storage that represents the current version of the Scheduler storage.
-// This value is used by the `on_runtime_upgrade` logic to determine whether we run
-// storage migration logic.
-#[derive(Encode, Decode, Clone, Copy, PartialEq, Eq, RuntimeDebug, TypeInfo)]
-enum Releases {
- V1,
- V2,
+#[cfg(feature = "runtime-benchmarks")]
+mod preimage_provider {
+ use frame_support::traits::PreimageRecipient;
+ pub trait PreimageProviderAndMaybeRecipient: PreimageRecipient {}
+ impl> PreimageProviderAndMaybeRecipient for T {}
}
-impl Default for Releases {
- fn default() -> Self {
- Releases::V1
- }
+#[cfg(not(feature = "runtime-benchmarks"))]
+mod preimage_provider {
+ use frame_support::traits::PreimageProvider;
+ pub trait PreimageProviderAndMaybeRecipient: PreimageProvider {}
+ impl> PreimageProviderAndMaybeRecipient for T {}
}
-#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo)]
-pub struct CallSpec {
- module: u32,
- method: u32,
+pub use preimage_provider::PreimageProviderAndMaybeRecipient;
+
+pub(crate) trait MarginalWeightInfo: WeightInfo {
+ fn item(periodic: bool, named: bool, resolved: Option) -> Weight {
+ match (periodic, named, resolved) {
+ (_, false, None) => Self::on_initialize_aborted(2) - Self::on_initialize_aborted(1),
+ (_, true, None) => {
+ Self::on_initialize_named_aborted(2) - Self::on_initialize_named_aborted(1)
+ }
+ (false, false, Some(false)) => Self::on_initialize(2) - Self::on_initialize(1),
+ (false, true, Some(false)) => {
+ Self::on_initialize_named(2) - Self::on_initialize_named(1)
+ }
+ (true, false, Some(false)) => {
+ Self::on_initialize_periodic(2) - Self::on_initialize_periodic(1)
+ }
+ (true, true, Some(false)) => {
+ Self::on_initialize_periodic_named(2) - Self::on_initialize_periodic_named(1)
+ }
+ (false, false, Some(true)) => {
+ Self::on_initialize_resolved(2) - Self::on_initialize_resolved(1)
+ }
+ (false, true, Some(true)) => {
+ Self::on_initialize_named_resolved(2) - Self::on_initialize_named_resolved(1)
+ }
+ (true, false, Some(true)) => {
+ Self::on_initialize_periodic_resolved(2) - Self::on_initialize_periodic_resolved(1)
+ }
+ (true, true, Some(true)) => {
+ Self::on_initialize_periodic_named_resolved(2)
+ - Self::on_initialize_periodic_named_resolved(1)
+ }
+ }
+ }
}
+impl MarginalWeightInfo for T {}
-decl_storage! {
- trait Store for Module as Scheduler {
- /// Items to be executed, indexed by the block number that they should be executed on.
- pub Agenda: map hasher(twox_64_concat) T::BlockNumber
- => Vec