This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
FRAME: General System for Recognizing and Executing Service Work #14329
Draft
sam0x17
wants to merge
49
commits into
master
Choose a base branch
from
sam-tasks
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 10 commits
Commits
Show all changes
49 commits
Select commit
Hold shift + click to select a range
71e16aa
initial API for Task trait
sam0x17 9a70eaf
Merge remote-tracking branch 'origin/master' into sam-tasks
sam0x17 01e3bbe
remove lifetime as per basti's suggestion
sam0x17 28f13cf
bound Task on FullCodec
sam0x17 f22054b
fix imports
sam0x17 4682e6f
set task_index to const
sam0x17 389131c
change to an associated const
sam0x17 4756ade
Merge remote-tracking branch 'origin/master' into sam-tasks
sam0x17 19f6ef6
compiles using wrapper PalletTask<T: Config> trait ... WIP
sam0x17 f967a80
example task impl using a wrapper task possibly working (compiles)
sam0x17 777c0c8
Merge remote-tracking branch 'origin/master' into sam-tasks
sam0x17 e07c25a
use generic ExampleTask<T: Config>
sam0x17 b4c57ba
use frame_support::Never
sam0x17 32bbd4e
refactor to be more like Call
sam0x17 ba54c4e
first attempt at implementing do_task
sam0x17 acf0d27
bind T and Task by TypeInfo
sam0x17 2b1083a
Merge remote-tracking branch 'origin/master' into sam-tasks
sam0x17 ddc2e9d
add parsing for task-related attributes
sam0x17 f7ca7d5
add tests for task related attribute macros
sam0x17 4d30f6c
first stab at validate_unsigned within tasks_example pallet
sam0x17 34e0990
add comments
sam0x17 f29f0a0
clean up
sam0x17 36dae43
improve scaffold a bit
sam0x17 d20f312
rename inner `#[pallet::tasks()]` to #[pallet::task_list(..)]` to red…
sam0x17 e7092b7
Merge remote-tracking branch 'origin/master' into sam-tasks
sam0x17 af70bd9
add Task as a composite enum
sam0x17 260a715
hook up RuntimeTask to construct_runtime
sam0x17 a0fc4c8
do_task system pallet extrinsic WIP (solved RuntimeTask issue)
sam0x17 49353a7
do_task extrinsic on system pallet done
sam0x17 4ac3fc7
fix comment
sam0x17 b26602d
suppress warning
sam0x17 a40a03e
context information on #[pallet::error] = bad
sam0x17 131766f
very granular task-related events (will probably reduce this)
sam0x17 6be3ce3
clean up old code
sam0x17 b553a3b
remove TaskInvalid event based on feedback
sam0x17 45e69f8
add AggregatedTask trait which we can use in RuntimeTask
sam0x17 af5f469
add tasks-example pallet to construct_runtime temporarily for testing
sam0x17 de82efe
refine tasks re-export paths
sam0x17 923cfc8
inject `RuntimeTask` into all construct_runtime!'s
sam0x17 0f62953
clean up
sam0x17 b704c87
AggregatedTask impl WIP
sam0x17 77f5629
include RuntimeTask in tasks-example
sam0x17 d74cc95
aggregation almost working, issue with `decl.find_part("Task")`
sam0x17 5f6e016
identify that there is an issue with decl.find_part WRT tasks
sam0x17 f823fc4
try Task not Tasks
sam0x17 b551c34
reference Task and other pallet parts in the runtime
sam0x17 cfda435
fix handling of tasks in expand_tt_default_parts
sam0x17 da2a524
item_enum
sam0x17 fa2b824
Fixes build
gupnik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| [package] | ||
| name = "tasks-example" | ||
| version = "4.0.0-dev" | ||
| authors = ["Parity Technologies <[email protected]>"] | ||
| edition = "2021" | ||
| license = "MIT-0" | ||
| homepage = "https://substrate.io" | ||
| repository = "https://github.com/paritytech/substrate/" | ||
| description = "FRAME example pallet" | ||
|
|
||
| [package.metadata.docs.rs] | ||
| targets = ["x86_64-unknown-linux-gnu"] | ||
|
|
||
| [dependencies] | ||
| codec = { package = "parity-scale-codec", version = "3.6.1", default-features = false } | ||
| log = { version = "0.4.17", default-features = false } | ||
| scale-info = { version = "2.5.0", default-features = false, features = ["derive"] } | ||
| frame-support = { version = "4.0.0-dev", default-features = false, path = "../../support" } | ||
| frame-system = { version = "4.0.0-dev", default-features = false, path = "../../system" } | ||
| sp-io = { version = "23.0.0", default-features = false, path = "../../../primitives/io" } | ||
| sp-runtime = { version = "24.0.0", default-features = false, path = "../../../primitives/runtime" } | ||
| sp-std = { version = "8.0.0", default-features = false, path = "../../../primitives/std" } | ||
| sp-core = { version = "21.0.0", default-features = false, path = "../../../primitives/core" } | ||
|
|
||
| [features] | ||
| default = ["std"] | ||
| std = [ | ||
| "codec/std", | ||
| "frame-support/std", | ||
| "frame-system/std", | ||
| "log/std", | ||
| "scale-info/std", | ||
| "sp-io/std", | ||
| "sp-runtime/std", | ||
| "sp-std/std", | ||
| "sp-core/std", | ||
| ] | ||
| try-runtime = ["frame-support/try-runtime"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| // 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. | ||
|
|
||
| #![cfg_attr(not(feature = "std"), no_std)] | ||
|
|
||
| use codec::{Decode, Encode}; | ||
| use frame_support::{dispatch::DispatchResult, traits::Task}; | ||
| // Re-export pallet items so that they can be accessed from the crate namespace. | ||
| pub use pallet::*; | ||
| use sp_runtime::DispatchError; | ||
|
|
||
| #[derive(Clone, PartialEq, Eq, Encode, Decode)] | ||
| pub enum ExampleTask { | ||
| Increment, | ||
| Decrement, | ||
| } | ||
|
|
||
| pub trait PalletTask<T: Config>: Task { | ||
| type Config: frame_system::Config; | ||
| fn is_valid(&self, config: &Self::Config) -> bool; | ||
| fn run(&self, config: &Self::Config) -> Result<(), DispatchError>; | ||
| } | ||
|
|
||
| impl Task for ExampleTask { | ||
| type Enumeration = std::vec::IntoIter<ExampleTask>; | ||
|
|
||
| const TASK_INDEX: usize = 0; | ||
|
|
||
| fn enumerate() -> Self::Enumeration { | ||
| vec![ExampleTask::Increment, ExampleTask::Decrement].into_iter() | ||
| } | ||
|
|
||
| fn is_valid(&self) -> bool { | ||
| unimplemented!() | ||
| } | ||
|
|
||
| fn run(&self) -> Result<(), DispatchError> { | ||
| unimplemented!() | ||
| } | ||
| } | ||
|
|
||
| impl<T: Config> PalletTask<T> for ExampleTask { | ||
| type Config = T; | ||
|
|
||
| fn is_valid(&self, _config: &Self::Config) -> bool { | ||
| let value = Value::<T>::get().unwrap(); | ||
| match self { | ||
| ExampleTask::Increment => value < 255, | ||
| ExampleTask::Decrement => value > 0, | ||
| } | ||
| } | ||
|
|
||
| fn run(&self, _config: &Self::Config) -> Result<(), DispatchError> { | ||
| match self { | ||
| ExampleTask::Increment => { | ||
| // Increment the value and emit an event | ||
| let new_val = Value::<T>::get().unwrap().checked_add(1).ok_or("Value overflow")?; | ||
| Value::<T>::put(new_val); | ||
| Pallet::<T>::deposit_event(Event::Incremented { new_val }); | ||
| }, | ||
| ExampleTask::Decrement => { | ||
| // Decrement the value and emit an event | ||
| let new_val = Value::<T>::get().unwrap().checked_sub(1).ok_or("Value underflow")?; | ||
| Value::<T>::put(new_val); | ||
| Pallet::<T>::deposit_event(Event::Decremented { new_val }); | ||
| }, | ||
| } | ||
| Ok(()) | ||
| } | ||
| } | ||
|
sam0x17 marked this conversation as resolved.
Outdated
|
||
|
|
||
| #[frame_support::pallet(dev_mode)] | ||
| pub mod pallet { | ||
| use super::*; | ||
| use frame_support::pallet_prelude::*; | ||
| use frame_system::pallet_prelude::*; | ||
|
|
||
| #[pallet::config] | ||
| pub trait Config: frame_system::Config { | ||
| type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>; | ||
| } | ||
|
|
||
| #[pallet::pallet] | ||
| pub struct Pallet<T>(_); | ||
|
|
||
| #[pallet::storage] | ||
| #[pallet::getter(fn value)] | ||
| pub type Value<T: Config> = StorageValue<_, u8>; | ||
|
|
||
| #[pallet::call] | ||
| impl<T: Config> Pallet<T> { | ||
| pub fn increment(origin: OriginFor<T>) -> DispatchResult { | ||
| ensure_root(origin)?; | ||
|
|
||
| // Increment the value and emit an event | ||
| let new_val = Value::<T>::get().unwrap().checked_add(1).ok_or("Value overflow")?; | ||
| Value::<T>::put(new_val); | ||
| Self::deposit_event(Event::Incremented { new_val }); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| pub fn decrement(origin: OriginFor<T>) -> DispatchResult { | ||
| ensure_root(origin)?; | ||
|
|
||
| // Decrement the value and emit an event | ||
| let new_val = Value::<T>::get().unwrap().checked_sub(1).ok_or("Value underflow")?; | ||
| Value::<T>::put(new_val); | ||
| Self::deposit_event(Event::Decremented { new_val }); | ||
|
|
||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| #[pallet::event] | ||
| #[pallet::generate_deposit(pub(super) fn deposit_event)] | ||
| pub enum Event<T: Config> { | ||
| Incremented { new_val: u8 }, | ||
| Decremented { new_val: u8 }, | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| // 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 sp_runtime::DispatchError; | ||
| use sp_std::iter::Iterator; | ||
|
|
||
| /// 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 { | ||
| type Enumeration: Iterator<Item = Self>; | ||
|
sam0x17 marked this conversation as resolved.
|
||
|
|
||
| /// A unique value representing this `Task`. Analogous to `call_index`, but for tasks. | ||
| const TASK_INDEX: usize; | ||
|
|
||
| /// Inspects the pallet's state and enumerates tasks of this type. | ||
| fn enumerate() -> 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>; | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.