mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-21 19:11:05 +00:00
ac3f14d23b
`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>
166 lines
4.4 KiB
Rust
166 lines
4.4 KiB
Rust
// 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.
|
|
|
|
use super::helper;
|
|
use quote::ToTokens;
|
|
use syn::spanned::Spanned;
|
|
|
|
pub mod keyword {
|
|
use super::*;
|
|
|
|
syn::custom_keyword!(FreezeReason);
|
|
syn::custom_keyword!(HoldReason);
|
|
syn::custom_keyword!(LockId);
|
|
syn::custom_keyword!(SlashReason);
|
|
syn::custom_keyword!(Task);
|
|
|
|
pub enum CompositeKeyword {
|
|
FreezeReason(FreezeReason),
|
|
HoldReason(HoldReason),
|
|
LockId(LockId),
|
|
SlashReason(SlashReason),
|
|
Task(Task),
|
|
}
|
|
|
|
impl ToTokens for CompositeKeyword {
|
|
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
|
|
use CompositeKeyword::*;
|
|
match self {
|
|
FreezeReason(inner) => inner.to_tokens(tokens),
|
|
HoldReason(inner) => inner.to_tokens(tokens),
|
|
LockId(inner) => inner.to_tokens(tokens),
|
|
SlashReason(inner) => inner.to_tokens(tokens),
|
|
Task(inner) => inner.to_tokens(tokens),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl syn::parse::Parse for CompositeKeyword {
|
|
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
|
|
let lookahead = input.lookahead1();
|
|
if lookahead.peek(FreezeReason) {
|
|
Ok(Self::FreezeReason(input.parse()?))
|
|
} else if lookahead.peek(HoldReason) {
|
|
Ok(Self::HoldReason(input.parse()?))
|
|
} else if lookahead.peek(LockId) {
|
|
Ok(Self::LockId(input.parse()?))
|
|
} else if lookahead.peek(SlashReason) {
|
|
Ok(Self::SlashReason(input.parse()?))
|
|
} else if lookahead.peek(Task) {
|
|
Ok(Self::Task(input.parse()?))
|
|
} else {
|
|
Err(lookahead.error())
|
|
}
|
|
}
|
|
}
|
|
|
|
impl std::fmt::Display for CompositeKeyword {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
use CompositeKeyword::*;
|
|
write!(
|
|
f,
|
|
"{}",
|
|
match self {
|
|
FreezeReason(_) => "FreezeReason",
|
|
HoldReason(_) => "HoldReason",
|
|
Task(_) => "Task",
|
|
LockId(_) => "LockId",
|
|
SlashReason(_) => "SlashReason",
|
|
}
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
pub struct CompositeDef {
|
|
/// The index of the CompositeDef item in the pallet module.
|
|
pub index: usize,
|
|
/// The composite keyword used (contains span).
|
|
pub composite_keyword: keyword::CompositeKeyword,
|
|
/// The span of the pallet::composite_enum attribute.
|
|
pub attr_span: proc_macro2::Span,
|
|
}
|
|
|
|
impl CompositeDef {
|
|
pub fn try_from(
|
|
attr_span: proc_macro2::Span,
|
|
index: usize,
|
|
scrate: &syn::Path,
|
|
item: &mut syn::Item,
|
|
) -> syn::Result<Self> {
|
|
let item = if let syn::Item::Enum(item) = item {
|
|
item
|
|
} else {
|
|
return Err(syn::Error::new(
|
|
item.span(),
|
|
"Invalid pallet::composite_enum, expected enum item",
|
|
))
|
|
};
|
|
|
|
if !matches!(item.vis, syn::Visibility::Public(_)) {
|
|
let msg = format!("Invalid pallet::composite_enum, `{}` must be public", item.ident);
|
|
return Err(syn::Error::new(item.span(), msg))
|
|
}
|
|
|
|
let has_instance = if item.generics.params.first().is_some() {
|
|
helper::check_config_def_gen(&item.generics, item.ident.span())?;
|
|
true
|
|
} else {
|
|
false
|
|
};
|
|
|
|
let has_derive_attr = item.attrs.iter().any(|attr| {
|
|
if let syn::Meta::List(syn::MetaList { path, .. }) = &attr.meta {
|
|
path.get_ident().map(|ident| ident == "derive").unwrap_or(false)
|
|
} else {
|
|
false
|
|
}
|
|
});
|
|
|
|
if !has_derive_attr {
|
|
let derive_attr: syn::Attribute = syn::parse_quote! {
|
|
#[derive(
|
|
Copy, Clone, Eq, PartialEq,
|
|
#scrate::__private::codec::Encode, #scrate::__private::codec::Decode, #scrate::__private::codec::MaxEncodedLen,
|
|
#scrate::__private::scale_info::TypeInfo,
|
|
#scrate::__private::RuntimeDebug,
|
|
)]
|
|
};
|
|
item.attrs.push(derive_attr);
|
|
}
|
|
|
|
if has_instance {
|
|
item.attrs.push(syn::parse_quote! {
|
|
#[scale_info(skip_type_params(I))]
|
|
});
|
|
|
|
item.variants.push(syn::parse_quote! {
|
|
#[doc(hidden)]
|
|
#[codec(skip)]
|
|
__Ignore(
|
|
#scrate::__private::sp_std::marker::PhantomData<I>,
|
|
)
|
|
});
|
|
}
|
|
|
|
let composite_keyword =
|
|
syn::parse2::<keyword::CompositeKeyword>(item.ident.to_token_stream())?;
|
|
|
|
Ok(CompositeDef { index, composite_keyword, attr_span })
|
|
}
|
|
}
|