mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-07-25 00:05:46 +00:00
Adds example for dev_mode and updates doc (#13944)
* Adds example for dev_mode and updates doc * Addresses review comments * Update frame/examples/dev-mode/src/lib.rs Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> * Addresses review comment --------- Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
This commit is contained in:
Generated
+16
@@ -5986,6 +5986,22 @@ dependencies = [
|
|||||||
"sp-std",
|
"sp-std",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "pallet-dev-mode"
|
||||||
|
version = "4.0.0-dev"
|
||||||
|
dependencies = [
|
||||||
|
"frame-support",
|
||||||
|
"frame-system",
|
||||||
|
"log",
|
||||||
|
"pallet-balances",
|
||||||
|
"parity-scale-codec",
|
||||||
|
"scale-info",
|
||||||
|
"sp-core",
|
||||||
|
"sp-io",
|
||||||
|
"sp-runtime",
|
||||||
|
"sp-std",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "pallet-election-provider-multi-phase"
|
name = "pallet-election-provider-multi-phase"
|
||||||
version = "4.0.0-dev"
|
version = "4.0.0-dev"
|
||||||
|
|||||||
@@ -106,6 +106,7 @@ members = [
|
|||||||
"frame/election-provider-support/solution-type/fuzzer",
|
"frame/election-provider-support/solution-type/fuzzer",
|
||||||
"frame/examples/basic",
|
"frame/examples/basic",
|
||||||
"frame/examples/offchain-worker",
|
"frame/examples/offchain-worker",
|
||||||
|
"frame/examples/dev-mode",
|
||||||
"frame/executive",
|
"frame/executive",
|
||||||
"frame/nis",
|
"frame/nis",
|
||||||
"frame/grandpa",
|
"frame/grandpa",
|
||||||
|
|||||||
@@ -0,0 +1,42 @@
|
|||||||
|
[package]
|
||||||
|
name = "pallet-dev-mode"
|
||||||
|
version = "4.0.0-dev"
|
||||||
|
authors = ["Parity Technologies <admin@parity.io>"]
|
||||||
|
edition = "2021"
|
||||||
|
license = "MIT-0"
|
||||||
|
homepage = "https://substrate.io"
|
||||||
|
repository = "https://github.com/paritytech/substrate/"
|
||||||
|
description = "FRAME example pallet"
|
||||||
|
readme = "README.md"
|
||||||
|
|
||||||
|
[package.metadata.docs.rs]
|
||||||
|
targets = ["x86_64-unknown-linux-gnu"]
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
codec = { package = "parity-scale-codec", version = "3.2.2", 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" }
|
||||||
|
pallet-balances = { version = "4.0.0-dev", default-features = false, path = "../../balances" }
|
||||||
|
sp-io = { version = "7.0.0", default-features = false, path = "../../../primitives/io" }
|
||||||
|
sp-runtime = { version = "7.0.0", default-features = false, path = "../../../primitives/runtime" }
|
||||||
|
sp-std = { version = "5.0.0", default-features = false, path = "../../../primitives/std" }
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
sp-core = { version = "7.0.0", default-features = false, path = "../../../primitives/core" }
|
||||||
|
|
||||||
|
[features]
|
||||||
|
default = ["std"]
|
||||||
|
std = [
|
||||||
|
"codec/std",
|
||||||
|
"frame-support/std",
|
||||||
|
"frame-system/std",
|
||||||
|
"log/std",
|
||||||
|
"pallet-balances/std",
|
||||||
|
"scale-info/std",
|
||||||
|
"sp-io/std",
|
||||||
|
"sp-runtime/std",
|
||||||
|
"sp-std/std",
|
||||||
|
]
|
||||||
|
try-runtime = ["frame-support/try-runtime"]
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
<!-- markdown-link-check-disable -->
|
||||||
|
# Dev Mode Example Pallet
|
||||||
|
|
||||||
|
A simple example of a FRAME pallet demonstrating
|
||||||
|
the ease of requirements for a pallet in dev mode.
|
||||||
|
|
||||||
|
Run `cargo doc --package pallet-dev-mode --open` to view this pallet's documentation.
|
||||||
|
|
||||||
|
**Dev mode is not meant to be used in production.**
|
||||||
|
|
||||||
|
License: MIT-0
|
||||||
@@ -0,0 +1,118 @@
|
|||||||
|
// 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.
|
||||||
|
|
||||||
|
//! <!-- markdown-link-check-disable -->
|
||||||
|
//! # Dev Mode Example Pallet
|
||||||
|
//!
|
||||||
|
//! A simple example of a FRAME pallet demonstrating
|
||||||
|
//! the ease of requirements for a pallet in dev mode.
|
||||||
|
//!
|
||||||
|
//! Run `cargo doc --package pallet-dev-mode --open` to view this pallet's documentation.
|
||||||
|
//!
|
||||||
|
//! **Dev mode is not meant to be used in production.**
|
||||||
|
|
||||||
|
// Ensure we're `no_std` when compiling for Wasm.
|
||||||
|
#![cfg_attr(not(feature = "std"), no_std)]
|
||||||
|
|
||||||
|
use frame_support::dispatch::DispatchResult;
|
||||||
|
use frame_system::ensure_signed;
|
||||||
|
|
||||||
|
// Re-export pallet items so that they can be accessed from the crate namespace.
|
||||||
|
pub use pallet::*;
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests;
|
||||||
|
|
||||||
|
/// A type alias for the balance type from this pallet's point of view.
|
||||||
|
type BalanceOf<T> = <T as pallet_balances::Config>::Balance;
|
||||||
|
|
||||||
|
/// Enable `dev_mode` for this pallet.
|
||||||
|
#[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: pallet_balances::Config + frame_system::Config {
|
||||||
|
/// The overarching event type.
|
||||||
|
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Simple declaration of the `Pallet` type. It is placeholder we use to implement traits and
|
||||||
|
// method.
|
||||||
|
#[pallet::pallet]
|
||||||
|
pub struct Pallet<T>(_);
|
||||||
|
|
||||||
|
#[pallet::call]
|
||||||
|
impl<T: Config> Pallet<T> {
|
||||||
|
#[pallet::call_index(0)]
|
||||||
|
/// No need to define a `weight` attribute here because of `dev_mode`.
|
||||||
|
pub fn add_dummy(origin: OriginFor<T>, id: T::AccountId) -> DispatchResult {
|
||||||
|
ensure_root(origin)?;
|
||||||
|
|
||||||
|
if let Some(mut dummies) = Dummy::<T>::get() {
|
||||||
|
dummies.push(id.clone());
|
||||||
|
Dummy::<T>::set(Some(dummies));
|
||||||
|
} else {
|
||||||
|
Dummy::<T>::set(Some(vec![id.clone()]));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Let's deposit an event to let the outside world know this happened.
|
||||||
|
Self::deposit_event(Event::AddDummy { account: id });
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[pallet::call_index(1)]
|
||||||
|
/// No need to define a `weight` attribute here because of `dev_mode`.
|
||||||
|
pub fn set_bar(
|
||||||
|
origin: OriginFor<T>,
|
||||||
|
#[pallet::compact] new_value: T::Balance,
|
||||||
|
) -> DispatchResult {
|
||||||
|
let sender = ensure_signed(origin)?;
|
||||||
|
|
||||||
|
// Put the new value into storage.
|
||||||
|
<Bar<T>>::insert(&sender, new_value);
|
||||||
|
|
||||||
|
Self::deposit_event(Event::SetBar { account: sender, balance: new_value });
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[pallet::event]
|
||||||
|
#[pallet::generate_deposit(pub(super) fn deposit_event)]
|
||||||
|
pub enum Event<T: Config> {
|
||||||
|
AddDummy { account: T::AccountId },
|
||||||
|
SetBar { account: T::AccountId, balance: BalanceOf<T> },
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The MEL requirement for bounded pallets is skipped by `dev_mode`.
|
||||||
|
/// This means that all storages are marked as unbounded.
|
||||||
|
/// This is equivalent to specifying `#[pallet::unbounded]` on this type definitions.
|
||||||
|
/// When the dev_mode is removed, we would need to implement implement `MaxEncodedLen`.
|
||||||
|
#[pallet::storage]
|
||||||
|
pub type Dummy<T: Config> = StorageValue<_, Vec<T::AccountId>>;
|
||||||
|
|
||||||
|
/// The Hasher requirement is skipped by `dev_mode`. So, second parameter can be `_`
|
||||||
|
/// and `Blake2_128Concat` is used as a default.
|
||||||
|
/// When the dev_mode is removed, we would need to specify the hasher like so:
|
||||||
|
/// `pub type Bar<T: Config> = StorageMap<_, Blake2_128Concat, T::AccountId, T::Balance>;`.
|
||||||
|
#[pallet::storage]
|
||||||
|
pub type Bar<T: Config> = StorageMap<_, _, T::AccountId, T::Balance>;
|
||||||
|
}
|
||||||
@@ -0,0 +1,130 @@
|
|||||||
|
// 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.
|
||||||
|
|
||||||
|
//! Tests for pallet-dev-mode.
|
||||||
|
|
||||||
|
use crate::*;
|
||||||
|
use frame_support::{assert_ok, traits::ConstU64};
|
||||||
|
use sp_core::H256;
|
||||||
|
use sp_runtime::{
|
||||||
|
testing::Header,
|
||||||
|
traits::{BlakeTwo256, IdentityLookup},
|
||||||
|
BuildStorage,
|
||||||
|
};
|
||||||
|
// Reexport crate as its pallet name for construct_runtime.
|
||||||
|
use crate as pallet_dev_mode;
|
||||||
|
|
||||||
|
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
|
||||||
|
type Block = frame_system::mocking::MockBlock<Test>;
|
||||||
|
|
||||||
|
// For testing the pallet, we construct a mock runtime.
|
||||||
|
frame_support::construct_runtime!(
|
||||||
|
pub enum Test where
|
||||||
|
Block = Block,
|
||||||
|
NodeBlock = Block,
|
||||||
|
UncheckedExtrinsic = UncheckedExtrinsic,
|
||||||
|
{
|
||||||
|
System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
|
||||||
|
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
|
||||||
|
Example: pallet_dev_mode::{Pallet, Call, Storage, Event<T>},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
impl frame_system::Config for Test {
|
||||||
|
type BaseCallFilter = frame_support::traits::Everything;
|
||||||
|
type BlockWeights = ();
|
||||||
|
type BlockLength = ();
|
||||||
|
type DbWeight = ();
|
||||||
|
type RuntimeOrigin = RuntimeOrigin;
|
||||||
|
type Index = u64;
|
||||||
|
type BlockNumber = u64;
|
||||||
|
type Hash = H256;
|
||||||
|
type RuntimeCall = RuntimeCall;
|
||||||
|
type Hashing = BlakeTwo256;
|
||||||
|
type AccountId = u64;
|
||||||
|
type Lookup = IdentityLookup<Self::AccountId>;
|
||||||
|
type Header = Header;
|
||||||
|
type RuntimeEvent = RuntimeEvent;
|
||||||
|
type BlockHashCount = ConstU64<250>;
|
||||||
|
type Version = ();
|
||||||
|
type PalletInfo = PalletInfo;
|
||||||
|
type AccountData = pallet_balances::AccountData<u64>;
|
||||||
|
type OnNewAccount = ();
|
||||||
|
type OnKilledAccount = ();
|
||||||
|
type SystemWeightInfo = ();
|
||||||
|
type SS58Prefix = ();
|
||||||
|
type OnSetCode = ();
|
||||||
|
type MaxConsumers = frame_support::traits::ConstU32<16>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl pallet_balances::Config for Test {
|
||||||
|
type MaxLocks = ();
|
||||||
|
type MaxReserves = ();
|
||||||
|
type ReserveIdentifier = [u8; 8];
|
||||||
|
type Balance = u64;
|
||||||
|
type DustRemoval = ();
|
||||||
|
type RuntimeEvent = RuntimeEvent;
|
||||||
|
type ExistentialDeposit = ConstU64<1>;
|
||||||
|
type AccountStore = System;
|
||||||
|
type WeightInfo = ();
|
||||||
|
type FreezeIdentifier = ();
|
||||||
|
type MaxFreezes = ();
|
||||||
|
type HoldIdentifier = ();
|
||||||
|
type MaxHolds = ();
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Config for Test {
|
||||||
|
type RuntimeEvent = RuntimeEvent;
|
||||||
|
}
|
||||||
|
|
||||||
|
// This function basically just builds a genesis storage key/value store according to
|
||||||
|
// our desired mockup.
|
||||||
|
pub fn new_test_ext() -> sp_io::TestExternalities {
|
||||||
|
let t = GenesisConfig {
|
||||||
|
// We use default for brevity, but you can configure as desired if needed.
|
||||||
|
system: Default::default(),
|
||||||
|
balances: Default::default(),
|
||||||
|
}
|
||||||
|
.build_storage()
|
||||||
|
.unwrap();
|
||||||
|
t.into()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn it_works_for_optional_value() {
|
||||||
|
new_test_ext().execute_with(|| {
|
||||||
|
assert_eq!(Dummy::<Test>::get(), None);
|
||||||
|
|
||||||
|
let val1 = 42;
|
||||||
|
assert_ok!(Example::add_dummy(RuntimeOrigin::root(), val1));
|
||||||
|
assert_eq!(Dummy::<Test>::get(), Some(vec![val1]));
|
||||||
|
|
||||||
|
// Check that accumulate works when we have Some value in Dummy already.
|
||||||
|
let val2 = 27;
|
||||||
|
assert_ok!(Example::add_dummy(RuntimeOrigin::root(), val2));
|
||||||
|
assert_eq!(Dummy::<Test>::get(), Some(vec![val1, val2]));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn set_dummy_works() {
|
||||||
|
new_test_ext().execute_with(|| {
|
||||||
|
let test_val = 133;
|
||||||
|
assert_ok!(Example::set_bar(RuntimeOrigin::signed(1), test_val.into()));
|
||||||
|
assert_eq!(Bar::<Test>::get(1), Some(test_val));
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -465,6 +465,8 @@ pub fn construct_runtime(input: TokenStream) -> TokenStream {
|
|||||||
/// * All storages are marked as unbounded, meaning you do not need to implement `MaxEncodedLen` on
|
/// * All storages are marked as unbounded, meaning you do not need to implement `MaxEncodedLen` on
|
||||||
/// storage types. This is equivalent to specifying `#[pallet::unbounded]` on all storage type
|
/// storage types. This is equivalent to specifying `#[pallet::unbounded]` on all storage type
|
||||||
/// definitions.
|
/// definitions.
|
||||||
|
/// * Storage hashers no longer need to be specified and can be replaced by `_`. In dev mode, these
|
||||||
|
/// will be replaced by `Blake2_128Concat`.
|
||||||
///
|
///
|
||||||
/// Note that the `dev_mode` argument can only be supplied to the `#[pallet]` or
|
/// Note that the `dev_mode` argument can only be supplied to the `#[pallet]` or
|
||||||
/// `#[frame_support::pallet]` attribute macro that encloses your pallet module. This argument
|
/// `#[frame_support::pallet]` attribute macro that encloses your pallet module. This argument
|
||||||
|
|||||||
Reference in New Issue
Block a user