1// This file is part of Substrate.
23// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
56// 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.
1718//! This pallet demonstrates the use of the `pallet::task` api for service work.
19#![cfg_attr(not(feature = "std"), no_std)]
2021use 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::*;
2728pub mod mock;
29pub mod tests;
3031#[cfg(feature = "runtime-benchmarks")]
32mod benchmarking;
3334pub mod weights;
35pub use weights::*;
3637#[cfg(feature = "experimental")]
38const LOG_TARGET: &str = "pallet-example-tasks";
3940#[frame_support::pallet(dev_mode)]
41pub mod pallet {
42use super::*;
43use frame_support::pallet_prelude::*;
44use frame_system::pallet_prelude::*;
4546#[pallet::error]
47pub enum Error<T> {
48/// The referenced task was not found.
49NotFound,
50 }
5152#[pallet::tasks_experimental]
53impl<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)]
59pub fn add_number_into_total(i: u32) -> DispatchResult {
60let 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 });
65Ok(())
66 }
67 }
6869#[pallet::hooks]
70impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
71#[cfg(feature = "experimental")]
72fn offchain_worker(_block_number: BlockNumberFor<T>) {
73if let Some(key) = Numbers::<T>::iter_keys().next() {
74// Create a valid task
75let task = Task::<T>::AddNumberIntoTotal { i: key };
76let runtime_task = <T as Config>::RuntimeTask::from(task);
77let call = frame_system::Call::<T>::do_task { task: runtime_task.into() };
7879// Submit the task as an unsigned transaction
80let xt = <T as CreateBare<frame_system::Call<T>>>::create_bare(call.into());
81let res = SubmitTransaction::<T, frame_system::Call<T>>::submit_transaction(xt);
82match res {
83Ok(_) => log::info!(target: LOG_TARGET, "Submitted the task."),
84Err(e) => log::error!(target: LOG_TARGET, "Error submitting task: {:?}", e),
85 }
86 }
87 }
8889#[cfg(not(feature = "experimental"))]
90fn offchain_worker(_block_number: BlockNumberFor<T>) {}
91 }
9293#[pallet::config]
94pub trait Config: CreateBare<frame_system::Call<Self>> + frame_system::Config {
95type RuntimeTask: frame_support::traits::Task
96 + IsType<<Self as frame_system::Config>::RuntimeTask>
97 + From<Task<Self>>;
98type WeightInfo: WeightInfo;
99 }
100101#[pallet::pallet]
102pub struct Pallet<T>(_);
103104/// Some running total.
105#[pallet::storage]
106pub type Total<T: Config> = StorageValue<_, (u32, u32), ValueQuery>;
107108/// Numbers to be added into the total.
109#[pallet::storage]
110pub type Numbers<T: Config> = StorageMap<_, Twox64Concat, u32, u32, OptionQuery>;
111}