From 6347cf1ae68886ee6233b7bf1d9e4062818ca129 Mon Sep 17 00:00:00 2001 From: Ankan Date: Sat, 16 Sep 2023 23:32:03 +0200 Subject: [PATCH 1/4] fix derive_impl docs --- substrate/frame/support/procedural/src/lib.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/substrate/frame/support/procedural/src/lib.rs b/substrate/frame/support/procedural/src/lib.rs index 9957cf1cff85..69f2bce66f9a 100644 --- a/substrate/frame/support/procedural/src/lib.rs +++ b/substrate/frame/support/procedural/src/lib.rs @@ -554,9 +554,10 @@ pub fn storage_alias(attributes: TokenStream, input: TokenStream) -> TokenStream } /// This attribute can be used to derive a full implementation of a trait based on a local partial -/// impl and an external impl containing defaults that can be overriden in the local impl. +/// impl and an external impl containing defaults that can be overridden in the local impl. /// -/// For a full end-to-end example, see [below](#use-case-auto-derive-test-pallet-config-traits). +/// For a full end-to-end example, see +/// [below](#use-case-example-auto-derive-test-pallet-config-traits). /// /// # Usage /// From 89cd8f3d6cbf7959b4b69cecdd8a6c0e8d4c24d2 Mon Sep 17 00:00:00 2001 From: Ankan Date: Sun, 17 Sep 2023 00:38:16 +0200 Subject: [PATCH 2/4] create test default config for staking pallet --- substrate/frame/staking/src/pallet/mod.rs | 48 ++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/substrate/frame/staking/src/pallet/mod.rs b/substrate/frame/staking/src/pallet/mod.rs index c6c75326b80c..f35de7a4ec4f 100644 --- a/substrate/frame/staking/src/pallet/mod.rs +++ b/substrate/frame/staking/src/pallet/mod.rs @@ -82,9 +82,36 @@ pub mod pallet { Remove, } - #[pallet::config] + /// Some default provided implementations of [`DefaultConfig`], which can be used to implement + /// [`Config`] in the runtime. + pub mod config_preludes { + use super::*; + use frame_support::derive_impl; + + /// Provides a simple default config of staking pallet to derive a testing pallet config in + /// mocked runtimes. + pub struct TestDefaultConfig; + + #[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig, no_aggregated_types)] + impl frame_system::DefaultConfig for TestDefaultConfig {} + + #[frame_support::register_default_impl(TestDefaultConfig)] + impl DefaultConfig for TestDefaultConfig { + type CurrencyBalance = u64; + type SessionsPerEra = ConstU32<3>; + type BondingDuration = ConstU32<3>; + type SlashDeferDuration = ConstU32<0>; + type MaxNominatorRewardedPerValidator = ConstU32<64>; + type MaxUnlockingChunks = ConstU32<32>; + type HistoryDepth = ConstU32<84>; + type WeightInfo = (); + } + } + + #[pallet::config(with_default)] pub trait Config: frame_system::Config { /// The staking balance. + #[pallet::no_default] type Currency: LockableCurrency< Self::AccountId, Moment = BlockNumberFor, @@ -101,10 +128,12 @@ pub mod pallet { + From + TypeInfo + MaxEncodedLen; + /// Time used for computing era duration. /// /// It is guaranteed to start being called from the first `on_finalize`. Thus value at /// genesis is not used. + #[pallet::no_default] type UnixTime: UnixTime; /// Convert a balance into a number used for election calculation. This must fit into a @@ -113,9 +142,11 @@ pub mod pallet { /// in 128. /// Consequently, the backward convert is used convert the u128s from sp-elections back to a /// [`BalanceOf`]. + #[pallet::no_default] type CurrencyToVote: sp_staking::currency_to_vote::CurrencyToVote>; /// Something that provides the election functionality. + #[pallet::no_default] type ElectionProvider: ElectionProvider< AccountId = Self::AccountId, BlockNumber = BlockNumberFor, @@ -123,6 +154,7 @@ pub mod pallet { DataProvider = Pallet, >; /// Something that provides the election functionality at genesis. + #[pallet::no_default] type GenesisElectionProvider: ElectionProvider< AccountId = Self::AccountId, BlockNumber = BlockNumberFor, @@ -130,6 +162,7 @@ pub mod pallet { >; /// Something that defines the maximum number of nominations per nominator. + #[pallet::no_default] type NominationsQuota: NominationsQuota>; /// Number of eras to keep in history. @@ -157,17 +190,21 @@ pub mod pallet { /// Tokens have been minted and are unused for validator-reward. /// See [Era payout](./index.html#era-payout). + #[pallet::no_default] type RewardRemainder: OnUnbalanced>; /// The overarching event type. + #[pallet::no_default] type RuntimeEvent: From> + IsType<::RuntimeEvent>; /// Handler for the unbalanced reduction when slashing a staker. + #[pallet::no_default] type Slash: OnUnbalanced>; /// Handler for the unbalanced increment when rewarding a staker. /// NOTE: in most cases, the implementation of `OnUnbalanced` should modify the total /// issuance. + #[pallet::no_default] type Reward: OnUnbalanced>; /// Number of sessions per era. @@ -188,17 +225,21 @@ pub mod pallet { /// The origin which can manage less critical staking parameters that does not require root. /// /// Supported actions: (1) cancel deferred slash, (2) set minimum commission. + #[pallet::no_default] type AdminOrigin: EnsureOrigin; /// Interface for interacting with a session pallet. + #[pallet::no_default] type SessionInterface: SessionInterface; /// The payout for validators and the system for the current era. /// See [Era payout](./index.html#era-payout). + #[pallet::no_default] type EraPayout: EraPayout>; /// Something that can estimate the next session change, accurately or as a best effort /// guess. + #[pallet::no_default] type NextNewSession: EstimateNextNewSession>; /// The maximum number of nominators rewarded for each validator. @@ -210,6 +251,7 @@ pub mod pallet { /// The fraction of the validator set that is safe to be offending. /// After the threshold is reached a new era will be forced. + #[pallet::no_default] type OffendingValidatorsThreshold: Get; /// Something that provides a best-effort sorted list of voters aka electing nominators, @@ -223,6 +265,7 @@ pub mod pallet { /// staker. In case of `bags-list`, this always means using `rebag` and `putInFrontOf`. /// /// Invariant: what comes out of this list will always be a nominator. + #[pallet::no_default] type VoterList: SortedListProvider; /// WIP: This is a noop as of now, the actual business logic that's described below is going @@ -245,6 +288,7 @@ pub mod pallet { /// validators, they can chill at any point, and their approval stakes will still be /// recorded. This implies that what comes out of iterating this list MIGHT NOT BE AN ACTIVE /// VALIDATOR. + #[pallet::no_default] type TargetList: SortedListProvider>; /// The maximum number of `unlocking` chunks a [`StakingLedger`] can @@ -264,9 +308,11 @@ pub mod pallet { /// receives. /// /// WARNING: this only reports slashing events for the time being. + #[pallet::no_default] type EventListeners: sp_staking::OnStakingUpdate>; /// Some parameters of the benchmarking. + #[pallet::no_default] type BenchmarkingConfig: BenchmarkingConfig; /// Weight information for extrinsics in this pallet. From 4853cc6f16551523059f1000d0ae9c84b2de7c3b Mon Sep 17 00:00:00 2001 From: Ankan Date: Sun, 17 Sep 2023 00:54:36 +0200 Subject: [PATCH 3/4] all compile --- substrate/frame/babe/src/mock.rs | 39 ++++++-------------------------- 1 file changed, 7 insertions(+), 32 deletions(-) diff --git a/substrate/frame/babe/src/mock.rs b/substrate/frame/babe/src/mock.rs index dbffe9f312e6..4759ded65f67 100644 --- a/substrate/frame/babe/src/mock.rs +++ b/substrate/frame/babe/src/mock.rs @@ -43,6 +43,7 @@ use sp_runtime::{ BuildStorage, Perbill, }; use sp_staking::{EraIndex, SessionIndex}; +use frame_support::derive_impl; type DummyValidatorId = u64; @@ -63,30 +64,17 @@ frame_support::construct_runtime!( } ); +#[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig)] impl frame_system::Config for Test { type BaseCallFilter = frame_support::traits::Everything; - type BlockWeights = (); - type BlockLength = (); - type DbWeight = (); type RuntimeOrigin = RuntimeOrigin; type Nonce = u64; type RuntimeCall = RuntimeCall; - type Hash = H256; - type Version = (); - type Hashing = sp_runtime::traits::BlakeTwo256; - type AccountId = DummyValidatorId; - type Lookup = IdentityLookup; type Block = Block; type RuntimeEvent = RuntimeEvent; type BlockHashCount = ConstU64<250>; - type PalletInfo = PalletInfo; - type AccountData = pallet_balances::AccountData; - type OnNewAccount = (); - type OnKilledAccount = (); - type SystemWeightInfo = (); - type SS58Prefix = (); + type AccountData = pallet_balances::AccountData; type OnSetCode = (); - type MaxConsumers = frame_support::traits::ConstU32<16>; } impl frame_system::offchain::SendTransactionTypes for Test @@ -116,7 +104,7 @@ impl pallet_session::Config for Test { } impl pallet_session::historical::Config for Test { - type FullIdentification = pallet_staking::Exposure; + type FullIdentification = pallet_staking::Exposure; type FullIdentificationOf = pallet_staking::ExposureOf; } @@ -132,20 +120,14 @@ impl pallet_timestamp::Config for Test { type WeightInfo = (); } +#[derive_impl(pallet_balances::config_preludes::TestDefaultConfig as pallet_balances::DefaultConfig)] impl pallet_balances::Config for Test { - type MaxLocks = (); - type MaxReserves = (); type ReserveIdentifier = [u8; 8]; - type Balance = u128; type DustRemoval = (); type RuntimeEvent = RuntimeEvent; - type ExistentialDeposit = ConstU128<1>; + type ExistentialDeposit = ConstU64<1>; type AccountStore = System; - type WeightInfo = (); - type FreezeIdentifier = (); - type MaxFreezes = (); type RuntimeHoldReason = (); - type MaxHolds = (); } pallet_staking_reward_curve::build! { @@ -177,23 +159,19 @@ impl onchain::Config for OnChainSeqPhragmen { type MaxWinners = ConstU32<100>; type Bounds = ElectionsBounds; } - +#[derive_impl(pallet_staking::config_preludes::TestDefaultConfig as pallet_staking::DefaultConfig)] impl pallet_staking::Config for Test { type RewardRemainder = (); type CurrencyToVote = (); type RuntimeEvent = RuntimeEvent; type Currency = Balances; - type CurrencyBalance = ::Balance; type Slash = (); type Reward = (); - type SessionsPerEra = SessionsPerEra; - type BondingDuration = BondingDuration; type SlashDeferDuration = SlashDeferDuration; type AdminOrigin = frame_system::EnsureRoot; type SessionInterface = Self; type UnixTime = pallet_timestamp::Pallet; type EraPayout = pallet_staking::ConvertCurve; - type MaxNominatorRewardedPerValidator = ConstU32<64>; type OffendingValidatorsThreshold = OffendingValidatorsThreshold; type NextNewSession = Session; type ElectionProvider = onchain::OnChainExecution; @@ -201,11 +179,8 @@ impl pallet_staking::Config for Test { type VoterList = pallet_staking::UseNominatorsAndValidatorsMap; type TargetList = pallet_staking::UseValidatorsMap; type NominationsQuota = FixedNominationsQuota<16>; - type MaxUnlockingChunks = ConstU32<32>; - type HistoryDepth = ConstU32<84>; type EventListeners = (); type BenchmarkingConfig = pallet_staking::TestBenchmarkingConfig; - type WeightInfo = (); } impl pallet_offences::Config for Test { From 110523b61bf2761791d9143d7fa1c43e76b84385 Mon Sep 17 00:00:00 2001 From: Ankan Date: Sun, 17 Sep 2023 00:58:58 +0200 Subject: [PATCH 4/4] fmt --- substrate/frame/babe/src/mock.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/substrate/frame/babe/src/mock.rs b/substrate/frame/babe/src/mock.rs index 4759ded65f67..b6ff5f9b3cb2 100644 --- a/substrate/frame/babe/src/mock.rs +++ b/substrate/frame/babe/src/mock.rs @@ -24,7 +24,7 @@ use frame_election_provider_support::{ onchain, SequentialPhragmen, }; use frame_support::{ - parameter_types, + derive_impl, parameter_types, traits::{ConstU128, ConstU32, ConstU64, KeyOwnerProofSystem, OnInitialize}, }; use pallet_session::historical as pallet_session_historical; @@ -43,7 +43,6 @@ use sp_runtime::{ BuildStorage, Perbill, }; use sp_staking::{EraIndex, SessionIndex}; -use frame_support::derive_impl; type DummyValidatorId = u64;