mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-25 14:11:14 +00:00
ac3f14d23b
`polkadot-sdk` version of original tasks PR located here: https://github.com/paritytech/substrate/pull/14329 Fixes #206 ## Status - [x] Generic `Task` trait - [x] `RuntimeTask` aggregated enum, compatible with `construct_runtime!` - [x] Casting between `Task` and `RuntimeTask` without needing `dyn` or `Box` - [x] Tasks Example pallet - [x] Runtime tests for Tasks example pallet - [x] Parsing for task-related macros - [x] Retrofit parsing to make macros optional - [x] Expansion for task-related macros - [x] Adds support for args in tasks - [x] Retrofit tasks example pallet to use macros instead of manual syntax - [x] Weights - [x] Cleanup - [x] UI tests - [x] Docs ## Target Syntax Adapted from https://github.com/paritytech/polkadot-sdk/issues/206#issue-1865172283 ```rust // NOTE: this enum is optional and is auto-generated by the other macros if not present #[pallet::task] pub enum Task<T: Config> { AddNumberIntoTotal { i: u32, } } /// Some running total. #[pallet::storage] pub(super) type Total<T: Config<I>, I: 'static = ()> = StorageValue<_, (u32, u32), ValueQuery>; /// Numbers to be added into the total. #[pallet::storage] pub(super) type Numbers<T: Config<I>, I: 'static = ()> = StorageMap<_, Twox64Concat, u32, u32, OptionQuery>; #[pallet::tasks_experimental] impl<T: Config<I>, I: 'static> Pallet<T, I> { /// Add a pair of numbers into the totals and remove them. #[pallet::task_list(Numbers::<T, I>::iter_keys())] #[pallet::task_condition(|i| Numbers::<T, I>::contains_key(i))] #[pallet::task_index(0)] pub fn add_number_into_total(i: u32) -> DispatchResult { let v = Numbers::<T, I>::take(i).ok_or(Error::<T, I>::NotFound)?; Total::<T, I>::mutate(|(total_keys, total_values)| { *total_keys += i; *total_values += v; }); Ok(()) } } ``` --------- Co-authored-by: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Co-authored-by: kianenigma <kian@parity.io> Co-authored-by: Nikhil Gupta <> Co-authored-by: Gavin Wood <gavin@parity.io> Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> Co-authored-by: gupnik <nikhilgupta.iitk@gmail.com>
88 lines
2.7 KiB
Rust
88 lines
2.7 KiB
Rust
// This file is part of Substrate.
|
|
|
|
// Copyright (C) Parity Technologies (UK) Ltd.
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
//! Contains the [`Task`] trait, which defines a general-purpose way for defining and executing
|
|
//! service work, and supporting types.
|
|
|
|
use codec::FullCodec;
|
|
use scale_info::TypeInfo;
|
|
use sp_runtime::DispatchError;
|
|
use sp_std::{fmt::Debug, iter::Iterator, vec, vec::IntoIter};
|
|
use sp_weights::Weight;
|
|
|
|
/// Contain's re-exports of all the supporting types for the [`Task`] trait. Used in the macro
|
|
/// expansion of `RuntimeTask`.
|
|
#[doc(hidden)]
|
|
pub mod __private {
|
|
pub use codec::FullCodec;
|
|
pub use scale_info::TypeInfo;
|
|
pub use sp_runtime::DispatchError;
|
|
pub use sp_std::{fmt::Debug, iter::Iterator, vec, vec::IntoIter};
|
|
pub use sp_weights::Weight;
|
|
}
|
|
|
|
/// A general-purpose trait which defines a type of service work (i.e., work to performed by an
|
|
/// off-chain worker) including methods for enumerating, validating, indexing, and running
|
|
/// tasks of this type.
|
|
pub trait Task: Sized + FullCodec + TypeInfo + Clone + Debug + PartialEq + Eq {
|
|
/// An [`Iterator`] over tasks of this type used as the return type for `enumerate`.
|
|
type Enumeration: Iterator;
|
|
|
|
/// Inspects the pallet's state and enumerates tasks of this type.
|
|
fn iter() -> Self::Enumeration;
|
|
|
|
/// Checks if a particular instance of this `Task` variant is a valid piece of work.
|
|
fn is_valid(&self) -> bool;
|
|
|
|
/// Performs the work for this particular `Task` variant.
|
|
fn run(&self) -> Result<(), DispatchError>;
|
|
|
|
/// Returns the weight of executing this `Task`.
|
|
fn weight(&self) -> Weight;
|
|
|
|
/// A unique value representing this `Task` within the current pallet. Analogous to
|
|
/// `call_index`, but for tasks.'
|
|
///
|
|
/// This value should be unique within the current pallet and can overlap with task indices
|
|
/// in other pallets.
|
|
fn task_index(&self) -> u32;
|
|
}
|
|
|
|
impl Task for () {
|
|
type Enumeration = IntoIter<Self>;
|
|
|
|
fn iter() -> Self::Enumeration {
|
|
vec![].into_iter()
|
|
}
|
|
|
|
fn is_valid(&self) -> bool {
|
|
true
|
|
}
|
|
|
|
fn run(&self) -> Result<(), DispatchError> {
|
|
Ok(())
|
|
}
|
|
|
|
fn weight(&self) -> Weight {
|
|
Weight::default()
|
|
}
|
|
|
|
fn task_index(&self) -> u32 {
|
|
0
|
|
}
|
|
}
|