Files
pezkuwi-subxt/substrate/frame/support/test/compile_pass/src/lib.rs
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

90 lines
2.9 KiB
Rust

// This file is part of Substrate.
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//! Test that `construct_runtime!` also works when `frame-support` or `frame-system` are renamed in
//! the `Cargo.toml`.
#![cfg_attr(not(feature = "std"), no_std)]
use renamed_frame_support::{
construct_runtime, derive_impl, parameter_types,
traits::{ConstU16, ConstU32, ConstU64, Everything},
};
use sp_core::{sr25519, H256};
use sp_runtime::{
create_runtime_str, generic,
traits::{BlakeTwo256, IdentityLookup, Verify},
};
use sp_version::RuntimeVersion;
pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("frame-support-test-compile-pass"),
impl_name: create_runtime_str!("substrate-frame-support-test-compile-pass-runtime"),
authoring_version: 0,
spec_version: 0,
impl_version: 0,
apis: sp_version::create_apis_vec!([]),
transaction_version: 0,
state_version: 0,
};
pub type Signature = sr25519::Signature;
pub type AccountId = <Signature as Verify>::Signer;
pub type BlockNumber = u64;
parameter_types! {
pub const Version: RuntimeVersion = VERSION;
}
#[derive_impl(renamed_frame_system::config_preludes::TestDefaultConfig as renamed_frame_system::DefaultConfig)]
impl renamed_frame_system::Config for Runtime {
type BaseCallFilter = Everything;
type BlockWeights = ();
type BlockLength = ();
type Nonce = u128;
type Hash = H256;
type Hashing = BlakeTwo256;
type Block = Block;
type Lookup = IdentityLookup<Self::AccountId>;
type BlockHashCount = ConstU64<2400>;
type Version = Version;
type AccountData = ();
type RuntimeOrigin = RuntimeOrigin;
type AccountId = AccountId;
type RuntimeEvent = RuntimeEvent;
type PalletInfo = PalletInfo;
type RuntimeCall = RuntimeCall;
type DbWeight = ();
type OnNewAccount = ();
type OnKilledAccount = ();
type OnSetCode = ();
type MaxConsumers = ConstU32<16>;
type SystemWeightInfo = ();
type SS58Prefix = ConstU16<0>;
}
pub type Header = generic::Header<BlockNumber, BlakeTwo256>;
pub type Block = generic::Block<Header, UncheckedExtrinsic>;
pub type UncheckedExtrinsic = generic::UncheckedExtrinsic<u32, RuntimeCall, Signature, ()>;
construct_runtime!(
pub struct Runtime {
System: renamed_frame_system,
}
);