frame_support/traits/
tasks.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! Contains the [`Task`] trait, which defines a general-purpose way for defining and executing
19//! service work, and supporting types.
20
21use alloc::{vec, vec::IntoIter};
22use codec::FullCodec;
23use core::{fmt::Debug, iter::Iterator};
24use scale_info::TypeInfo;
25use sp_runtime::DispatchError;
26use sp_weights::Weight;
27
28/// Contain's re-exports of all the supporting types for the [`Task`] trait. Used in the macro
29/// expansion of `RuntimeTask`.
30#[doc(hidden)]
31pub mod __private {
32	pub use alloc::{vec, vec::IntoIter};
33	pub use codec::FullCodec;
34	pub use core::{fmt::Debug, iter::Iterator};
35	pub use scale_info::TypeInfo;
36	pub use sp_runtime::DispatchError;
37	pub use sp_weights::Weight;
38}
39
40/// A general-purpose trait which defines a type of service work (i.e., work to performed by an
41/// off-chain worker) including methods for enumerating, validating, indexing, and running
42/// tasks of this type.
43pub trait Task: Sized + FullCodec + TypeInfo + Clone + Debug + PartialEq + Eq {
44	/// An [`Iterator`] over tasks of this type used as the return type for `enumerate`.
45	type Enumeration: Iterator;
46
47	/// Inspects the pallet's state and enumerates tasks of this type.
48	fn iter() -> Self::Enumeration;
49
50	/// Checks if a particular instance of this `Task` variant is a valid piece of work.
51	///
52	/// This is used to validate tasks for unsigned execution. Hence, it MUST be cheap
53	/// with minimal to no storage reads. Else, it can make the blockchain vulnerable
54	/// to DoS attacks.
55	fn is_valid(&self) -> bool;
56
57	/// Performs the work for this particular `Task` variant.
58	fn run(&self) -> Result<(), DispatchError>;
59
60	/// Returns the weight of executing this `Task`.
61	fn weight(&self) -> Weight;
62
63	/// A unique value representing this `Task` within the current pallet. Analogous to
64	/// `call_index`, but for tasks.'
65	///
66	/// This value should be unique within the current pallet and can overlap with task indices
67	/// in other pallets.
68	fn task_index(&self) -> u32;
69}
70
71impl Task for () {
72	type Enumeration = IntoIter<Self>;
73
74	fn iter() -> Self::Enumeration {
75		vec![].into_iter()
76	}
77
78	fn is_valid(&self) -> bool {
79		true
80	}
81
82	fn run(&self) -> Result<(), DispatchError> {
83		Ok(())
84	}
85
86	fn weight(&self) -> Weight {
87		Weight::default()
88	}
89
90	fn task_index(&self) -> u32 {
91		0
92	}
93}