referrerpolicy=no-referrer-when-downgrade

pallet_example_tasks/
lib.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//! This pallet demonstrates the use of the `pallet::task` api for service work.
19#![cfg_attr(not(feature = "std"), no_std)]
20
21use frame_support::dispatch::DispatchResult;
22use frame_system::offchain::CreateBare;
23#[cfg(feature = "experimental")]
24use frame_system::offchain::SubmitTransaction;
25// Re-export pallet items so that they can be accessed from the crate namespace.
26pub use pallet::*;
27
28pub mod mock;
29pub mod tests;
30
31#[cfg(feature = "runtime-benchmarks")]
32mod benchmarking;
33
34pub mod weights;
35pub use weights::*;
36
37#[cfg(feature = "experimental")]
38const LOG_TARGET: &str = "pallet-example-tasks";
39
40#[frame_support::pallet(dev_mode)]
41pub mod pallet {
42	use super::*;
43	use frame_support::pallet_prelude::*;
44	use frame_system::pallet_prelude::*;
45
46	#[pallet::error]
47	pub enum Error<T> {
48		/// The referenced task was not found.
49		NotFound,
50	}
51
52	#[pallet::tasks_experimental]
53	impl<T: Config> Pallet<T> {
54		/// Add a pair of numbers into the totals and remove them.
55		#[pallet::task_list(Numbers::<T>::iter_keys())]
56		#[pallet::task_condition(|i| Numbers::<T>::contains_key(i))]
57		#[pallet::task_weight(T::WeightInfo::add_number_into_total())]
58		#[pallet::task_index(0)]
59		pub fn add_number_into_total(i: u32) -> DispatchResult {
60			let v = Numbers::<T>::take(i).ok_or(Error::<T>::NotFound)?;
61			Total::<T>::mutate(|(total_keys, total_values)| {
62				*total_keys += i;
63				*total_values += v;
64			});
65			Ok(())
66		}
67	}
68
69	#[pallet::hooks]
70	impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
71		#[cfg(feature = "experimental")]
72		fn offchain_worker(_block_number: BlockNumberFor<T>) {
73			if let Some(key) = Numbers::<T>::iter_keys().next() {
74				// Create a valid task
75				let task = Task::<T>::AddNumberIntoTotal { i: key };
76				let runtime_task = <T as Config>::RuntimeTask::from(task);
77				let call = frame_system::Call::<T>::do_task { task: runtime_task.into() };
78
79				// Submit the task as an unsigned transaction
80				let xt = <T as CreateBare<frame_system::Call<T>>>::create_bare(call.into());
81				let res = SubmitTransaction::<T, frame_system::Call<T>>::submit_transaction(xt);
82				match res {
83					Ok(_) => log::info!(target: LOG_TARGET, "Submitted the task."),
84					Err(e) => log::error!(target: LOG_TARGET, "Error submitting task: {:?}", e),
85				}
86			}
87		}
88
89		#[cfg(not(feature = "experimental"))]
90		fn offchain_worker(_block_number: BlockNumberFor<T>) {}
91	}
92
93	#[pallet::config]
94	pub trait Config: CreateBare<frame_system::Call<Self>> + frame_system::Config {
95		type RuntimeTask: frame_support::traits::Task
96			+ IsType<<Self as frame_system::Config>::RuntimeTask>
97			+ From<Task<Self>>;
98		type WeightInfo: WeightInfo;
99	}
100
101	#[pallet::pallet]
102	pub struct Pallet<T>(_);
103
104	/// Some running total.
105	#[pallet::storage]
106	pub type Total<T: Config> = StorageValue<_, (u32, u32), ValueQuery>;
107
108	/// Numbers to be added into the total.
109	#[pallet::storage]
110	pub type Numbers<T: Config> = StorageMap<_, Twox64Concat, u32, u32, OptionQuery>;
111}