Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default Config for Staking #1599

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 7 additions & 33 deletions substrate/frame/babe/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -63,30 +63,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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RuntimeOrigin, RuntimeCall and RuntimeEvent can also be removed now.

type Nonce = u64;
type RuntimeCall = RuntimeCall;
type Hash = H256;
type Version = ();
type Hashing = sp_runtime::traits::BlakeTwo256;
type AccountId = DummyValidatorId;
type Lookup = IdentityLookup<Self::AccountId>;
type Block = Block;
type RuntimeEvent = RuntimeEvent;
type BlockHashCount = ConstU64<250>;
type PalletInfo = PalletInfo;
type AccountData = pallet_balances::AccountData<u128>;
type OnNewAccount = ();
type OnKilledAccount = ();
type SystemWeightInfo = ();
type SS58Prefix = ();
type AccountData = pallet_balances::AccountData<u64>;
type OnSetCode = ();
type MaxConsumers = frame_support::traits::ConstU32<16>;
}

impl<C> frame_system::offchain::SendTransactionTypes<C> for Test
Expand Down Expand Up @@ -116,7 +103,7 @@ impl pallet_session::Config for Test {
}

impl pallet_session::historical::Config for Test {
type FullIdentification = pallet_staking::Exposure<u64, u128>;
type FullIdentification = pallet_staking::Exposure<u64, u64>;
type FullIdentificationOf = pallet_staking::ExposureOf<Self>;
}

Expand All @@ -132,20 +119,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! {
Expand Down Expand Up @@ -177,35 +158,28 @@ 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 = <Self as pallet_balances::Config>::Balance;
type Slash = ();
type Reward = ();
type SessionsPerEra = SessionsPerEra;
type BondingDuration = BondingDuration;
type SlashDeferDuration = SlashDeferDuration;
type AdminOrigin = frame_system::EnsureRoot<Self::AccountId>;
type SessionInterface = Self;
type UnixTime = pallet_timestamp::Pallet<Test>;
type EraPayout = pallet_staking::ConvertCurve<RewardCurve>;
type MaxNominatorRewardedPerValidator = ConstU32<64>;
type OffendingValidatorsThreshold = OffendingValidatorsThreshold;
type NextNewSession = Session;
type ElectionProvider = onchain::OnChainExecution<OnChainSeqPhragmen>;
type GenesisElectionProvider = Self::ElectionProvider;
type VoterList = pallet_staking::UseNominatorsAndValidatorsMap<Self>;
type TargetList = pallet_staking::UseValidatorsMap<Self>;
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 {
Expand Down
48 changes: 47 additions & 1 deletion substrate/frame/staking/src/pallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,36 @@ pub mod pallet {
Remove,
}

#[pallet::config]
/// Some default provided implementations of [`DefaultConfig`], which can be used to implement
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be then applies in a couple of pallets, like grandpa etc?

/// [`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]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can try using no_default_bounds for a number of them.

type Currency: LockableCurrency<
Self::AccountId,
Moment = BlockNumberFor<Self>,
Expand All @@ -101,10 +128,12 @@ pub mod pallet {
+ From<u64>
+ 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
Expand All @@ -113,23 +142,27 @@ 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<BalanceOf<Self>>;

/// Something that provides the election functionality.
#[pallet::no_default]
type ElectionProvider: ElectionProvider<
AccountId = Self::AccountId,
BlockNumber = BlockNumberFor<Self>,
// we only accept an election provider that has staking as data provider.
DataProvider = Pallet<Self>,
>;
/// Something that provides the election functionality at genesis.
#[pallet::no_default]
type GenesisElectionProvider: ElectionProvider<
AccountId = Self::AccountId,
BlockNumber = BlockNumberFor<Self>,
DataProvider = Pallet<Self>,
>;

/// Something that defines the maximum number of nominations per nominator.
#[pallet::no_default]
type NominationsQuota: NominationsQuota<BalanceOf<Self>>;

/// Number of eras to keep in history.
Expand Down Expand Up @@ -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<NegativeImbalanceOf<Self>>;

/// The overarching event type.
#[pallet::no_default]
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;

/// Handler for the unbalanced reduction when slashing a staker.
#[pallet::no_default]
type Slash: OnUnbalanced<NegativeImbalanceOf<Self>>;

/// 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<PositiveImbalanceOf<Self>>;

/// Number of sessions per era.
Expand All @@ -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<Self::RuntimeOrigin>;

/// Interface for interacting with a session pallet.
#[pallet::no_default]
type SessionInterface: SessionInterface<Self::AccountId>;

/// The payout for validators and the system for the current era.
/// See [Era payout](./index.html#era-payout).
#[pallet::no_default]
type EraPayout: EraPayout<BalanceOf<Self>>;

/// Something that can estimate the next session change, accurately or as a best effort
/// guess.
#[pallet::no_default]
type NextNewSession: EstimateNextNewSession<BlockNumberFor<Self>>;

/// The maximum number of nominators rewarded for each validator.
Expand All @@ -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<Perbill>;

/// Something that provides a best-effort sorted list of voters aka electing nominators,
Expand All @@ -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<Self::AccountId, Score = VoteWeight>;

/// WIP: This is a noop as of now, the actual business logic that's described below is going
Expand All @@ -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<Self::AccountId, Score = BalanceOf<Self>>;

/// The maximum number of `unlocking` chunks a [`StakingLedger`] can
Expand All @@ -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<Self::AccountId, BalanceOf<Self>>;

/// Some parameters of the benchmarking.
#[pallet::no_default]
type BenchmarkingConfig: BenchmarkingConfig;

/// Weight information for extrinsics in this pallet.
Expand Down
5 changes: 3 additions & 2 deletions substrate/frame/support/procedural/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
Expand Down
Loading