mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 21:37:56 +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>
146 lines
4.4 KiB
Rust
146 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.
|
|
|
|
//! Minimal pallet without `frame_system::Config`-super trait.
|
|
|
|
// Make sure we fail compilation on warnings
|
|
#![warn(missing_docs)]
|
|
#![deny(warnings)]
|
|
|
|
pub use frame_support::dispatch::RawOrigin;
|
|
use frame_system::pallet_prelude::BlockNumberFor;
|
|
|
|
pub use self::pallet::*;
|
|
|
|
#[frame_support::pallet(dev_mode)]
|
|
pub mod pallet {
|
|
use super::*;
|
|
use crate::{self as frame_system, pallet_prelude::*};
|
|
use frame_support::pallet_prelude::*;
|
|
|
|
#[pallet::pallet]
|
|
pub struct Pallet<T>(_);
|
|
|
|
/// The configuration trait.
|
|
#[pallet::config]
|
|
#[pallet::disable_frame_system_supertrait_check]
|
|
pub trait Config: 'static + Eq + Clone {
|
|
/// The block number type.
|
|
type BlockNumber: Parameter + Member + Default + MaybeSerializeDeserialize + MaxEncodedLen;
|
|
/// The account type.
|
|
type AccountId: Parameter + Member + MaxEncodedLen;
|
|
/// The basic call filter to use in Origin.
|
|
type BaseCallFilter: frame_support::traits::Contains<Self::RuntimeCall>;
|
|
/// The runtime origin type.
|
|
type RuntimeOrigin: Into<Result<RawOrigin<Self::AccountId>, Self::RuntimeOrigin>>
|
|
+ From<RawOrigin<Self::AccountId>>;
|
|
/// The runtime call type.
|
|
type RuntimeCall;
|
|
/// Contains an aggregation of all tasks in this runtime.
|
|
type RuntimeTask;
|
|
/// The runtime event type.
|
|
type RuntimeEvent: Parameter
|
|
+ Member
|
|
+ IsType<<Self as frame_system::Config>::RuntimeEvent>
|
|
+ From<Event<Self>>;
|
|
/// The information about the pallet setup in the runtime.
|
|
type PalletInfo: frame_support::traits::PalletInfo;
|
|
/// The db weights.
|
|
type DbWeight: Get<frame_support::weights::RuntimeDbWeight>;
|
|
}
|
|
|
|
#[pallet::call]
|
|
impl<T: Config> Pallet<T> {
|
|
/// A noop call.
|
|
pub fn noop(_origin: OriginFor<T>) -> DispatchResult {
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
impl<T: Config> Pallet<T> {
|
|
/// A empty method.
|
|
pub fn deposit_event(_event: impl Into<T::RuntimeEvent>) {}
|
|
}
|
|
|
|
/// The origin type.
|
|
#[pallet::origin]
|
|
pub type Origin<T> = RawOrigin<<T as Config>::AccountId>;
|
|
|
|
/// The error type.
|
|
#[pallet::error]
|
|
pub enum Error<T> {
|
|
/// Test error documentation
|
|
TestError,
|
|
/// Error documentation
|
|
/// with multiple lines
|
|
AnotherError,
|
|
/// Required by construct_runtime
|
|
CallFiltered,
|
|
}
|
|
|
|
/// The event type.
|
|
#[pallet::event]
|
|
pub enum Event<T: Config> {
|
|
/// The extrinsic is successful
|
|
ExtrinsicSuccess,
|
|
/// The extrinsic is failed
|
|
ExtrinsicFailed,
|
|
/// The ignored error
|
|
Ignore(<T as Config>::BlockNumber),
|
|
}
|
|
}
|
|
|
|
/// Ensure that the origin `o` represents the root. Returns `Ok` or an `Err` otherwise.
|
|
pub fn ensure_root<OuterOrigin, AccountId>(o: OuterOrigin) -> Result<(), &'static str>
|
|
where
|
|
OuterOrigin: Into<Result<RawOrigin<AccountId>, OuterOrigin>>,
|
|
{
|
|
o.into().map(|_| ()).map_err(|_| "bad origin: expected to be a root origin")
|
|
}
|
|
|
|
/// Same semantic as [`frame_system`].
|
|
// Note: we cannot use [`frame_system`] here since the pallet does not depend on
|
|
// [`frame_system::Config`].
|
|
pub mod pallet_prelude {
|
|
pub use crate::ensure_root;
|
|
|
|
/// Type alias for the `Origin` associated type of system config.
|
|
pub type OriginFor<T> = <T as crate::Config>::RuntimeOrigin;
|
|
|
|
/// Type alias for the `BlockNumber` associated type of system config.
|
|
pub type BlockNumberFor<T> = <T as super::Config>::BlockNumber;
|
|
}
|
|
|
|
/// Provides an implementation of [`frame_support::traits::Randomness`] that should only be used in
|
|
/// tests!
|
|
pub struct TestRandomness<T>(sp_std::marker::PhantomData<T>);
|
|
|
|
impl<Output: codec::Decode + Default, T>
|
|
frame_support::traits::Randomness<Output, BlockNumberFor<T>> for TestRandomness<T>
|
|
where
|
|
T: frame_system::Config,
|
|
{
|
|
fn random(subject: &[u8]) -> (Output, BlockNumberFor<T>) {
|
|
use sp_runtime::traits::TrailingZeroInput;
|
|
|
|
(
|
|
Output::decode(&mut TrailingZeroInput::new(subject)).unwrap_or_default(),
|
|
frame_system::Pallet::<T>::block_number(),
|
|
)
|
|
}
|
|
}
|