Files
pezkuwi-subxt/prdoc/pr_1343.prdoc
T
Sam Johnson ac3f14d23b Tasks: general system for recognizing and executing service work (#1343)
`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>
2023-12-08 11:10:26 +05:30

30 lines
1.3 KiB
Plaintext

title: Tasks API - A general system for recognizing and executing service work
doc:
- audience: Runtime Dev
description: |
The Tasks API allows you to define some service work that can be recognized by a script or an off-chain worker.
Such a script can then create and submit all such work items at any given time.
`#[pallet:tasks_experimental]` provides a convenient way to define such work items. It can be attached to an
`impl` block inside a pallet, whose functions can then be annotated by the following attributes:
1. `#[pallet::task_list]`: Define an iterator over the available work items for a task
2. `#[pallet::task_condition]`: Define the conditions for a given work item to be valid
3. `#[pallet::task_weight]`: Define the weight of a given work item
4. `#[pallet::task_index]`: Define the index of a given work item
Each such function becomes a variant of the autogenerated enum `Task<T>` for this pallet.
All such enums are aggregated into a `RuntimeTask` by `construct_runtime`.
An example pallet that uses the Tasks API is available at `substrate/frame/example/tasks`.
migrations:
db: []
runtime: []
crates:
- name: frame-system
- name: frame-support
- name: frame-support-procedural
- name: pallet-example-tasks
host_functions: []