Skip to content

Commit

Permalink
add frame_system::DefaultConfig to individual pallet `DefaultConfig…
Browse files Browse the repository at this point in the history
…s` (paritytech#14453)

* add frame_system::DefaultConfig to individual pallet DefaultConfigs

* Fixes tests

* Minor fix

* ".git/.scripts/commands/fmt/fmt.sh"

* Adds UI Tests

---------

Co-authored-by: Nikhil Gupta <17176722+gupnik@users.noreply.github.com>
Co-authored-by: command-bot <>
  • Loading branch information
kianenigma and gupnik authored Aug 14, 2023
1 parent ca379d7 commit 90e379a
Show file tree
Hide file tree
Showing 12 changed files with 201 additions and 133 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 31 additions & 1 deletion frame/balances/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,33 @@ pub mod pallet {

pub type CreditOf<T, I> = Credit<<T as frame_system::Config>::AccountId, Pallet<T, I>>;

#[pallet::config]
/// Default implementations of [`DefaultConfig`], which can be used to implement [`Config`].
pub mod config_preludes {
use super::*;
use frame_support::derive_impl;

pub struct TestDefaultConfig;

#[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig)]
impl frame_system::DefaultConfig for TestDefaultConfig {}

#[frame_support::register_default_impl(TestDefaultConfig)]
impl DefaultConfig for TestDefaultConfig {
type Balance = u64;

type ReserveIdentifier = ();
type FreezeIdentifier = ();

type MaxLocks = ();
type MaxReserves = ();
type MaxFreezes = ();
type MaxHolds = ();

type WeightInfo = ();
}
}

#[pallet::config(with_default)]
pub trait Config<I: 'static = ()>: frame_system::Config {
/// The overarching event type.
type RuntimeEvent: From<Event<Self, I>>
Expand All @@ -236,6 +262,7 @@ pub mod pallet {
+ FixedPointOperand;

/// Handler for the unbalanced reduction when removing a dust account.
#[pallet::no_default]
type DustRemoval: OnUnbalanced<CreditOf<Self, I>>;

/// The minimum amount required to keep an account open. MUST BE GREATER THAN ZERO!
Expand All @@ -247,9 +274,11 @@ pub mod pallet {
///
/// Bottom line: Do yourself a favour and make it at least one!
#[pallet::constant]
#[pallet::no_default]
type ExistentialDeposit: Get<Self::Balance>;

/// The means of storing the balances of an account.
#[pallet::no_default]
type AccountStore: StoredMap<Self::AccountId, AccountData<Self::Balance>>;

/// The ID type for reserves.
Expand All @@ -258,6 +287,7 @@ pub mod pallet {
type ReserveIdentifier: Parameter + Member + MaxEncodedLen + Ord + Copy;

/// The overarching hold reason.
#[pallet::no_default]
type RuntimeHoldReason: Parameter + Member + MaxEncodedLen + Ord + Copy;

/// The ID type for freezes.
Expand Down
82 changes: 52 additions & 30 deletions frame/examples/default-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,20 @@ pub mod pallet {
/// in our tests below.
type OverwrittenDefaultValue: Get<u32>;

/// An input parameter that relies on `<Self as frame_system::Config>::AccountId`. As of
/// now, such types cannot have defaults and need to be annotated as such, iff
/// `#[pallet::config(with_default)]` is enabled:
/// An input parameter that relies on `<Self as frame_system::Config>::AccountId`. This can
/// too have a default, as long as as it is present in `frame_system::DefaultConfig`.
type CanDeriveDefaultFromSystem: Get<Self::AccountId>;

/// We might chose to declare as one that doesn't have a default, for whatever semantical
/// reason.
#[pallet::no_default]
type CannotHaveDefault: Get<Self::AccountId>;
type HasNoDefault: Get<u32>;

/// Some types can technically have no default, such as those the rely on
/// `frame_system::Config` but are not present in `frame_system::DefaultConfig`. For
/// example, a `RuntimeCall` cannot reasonably have a default.
#[pallet::no_default] // if we skip this, there will be a compiler error.
type CannotHaveDefault: Get<Self::RuntimeCall>;

/// Something that is a normal type, with default.
type WithDefaultType;
Expand All @@ -73,24 +82,41 @@ pub mod pallet {
pub mod config_preludes {
// This will help use not need to disambiguate anything when using `derive_impl`.
use super::*;
use frame_support::derive_impl;

/// A type providing default configurations for this pallet in testing environment.
pub struct TestDefaultConfig;

#[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig)]
impl frame_system::DefaultConfig for TestDefaultConfig {}

#[frame_support::register_default_impl(TestDefaultConfig)]
impl DefaultConfig for TestDefaultConfig {
type WithDefaultValue = frame_support::traits::ConstU32<42>;
type OverwrittenDefaultValue = frame_support::traits::ConstU32<42>;

// `frame_system::config_preludes::TestDefaultConfig` declares account-id as u64.
type CanDeriveDefaultFromSystem = frame_support::traits::ConstU64<42>;

type WithDefaultType = u32;
type OverwrittenDefaultType = u32;
}

/// A type providing default configurations for this pallet in a parachain environment.
pub struct ParachainDefaultConfig;
#[frame_support::register_default_impl(ParachainDefaultConfig)]
impl DefaultConfig for ParachainDefaultConfig {
/// A type providing default configurations for this pallet in another environment. Examples
/// could be a parachain, or a solo-chain.
///
/// Appropriate derive for `frame_system::DefaultConfig` needs to be provided. In this
/// example, we simple derive `frame_system::config_preludes::TestDefaultConfig` again.
pub struct OtherDefaultConfig;

#[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig)]
impl frame_system::DefaultConfig for OtherDefaultConfig {}

#[frame_support::register_default_impl(OtherDefaultConfig)]
impl DefaultConfig for OtherDefaultConfig {
type WithDefaultValue = frame_support::traits::ConstU32<66>;
type OverwrittenDefaultValue = frame_support::traits::ConstU32<66>;
type CanDeriveDefaultFromSystem = frame_support::traits::ConstU64<42>;
type WithDefaultType = u32;
type OverwrittenDefaultType = u32;
}
Expand All @@ -106,28 +132,25 @@ pub mod pallet {
#[cfg(any(test, doc))]
pub mod tests {
use super::*;
use frame_support::derive_impl;
use sp_runtime::traits::ConstU64;

use super::pallet as pallet_default_config_example;
use frame_support::{derive_impl, parameter_types};
use pallet::{self as pallet_default_config_example, config_preludes::*};

type Block = frame_system::mocking::MockBlock<Test>;
type Block = frame_system::mocking::MockBlock<Runtime>;

frame_support::construct_runtime!(
pub enum Test
{
pub struct Runtime {
System: frame_system,
DefaultPallet: pallet_default_config_example,
}
);

#[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig)]
impl frame_system::Config for Test {
impl frame_system::Config for Runtime {
// these items are defined by frame-system as `no_default`, so we must specify them here.
// Note that these are types that actually rely on the outer runtime, and can't sensibly
// have an _independent_ default.
type Block = Block;
type BlockHashCount = ConstU64<10>;
type BlockHashCount = frame_support::traits::ConstU64<10>;
type BaseCallFilter = frame_support::traits::Everything;
type RuntimeOrigin = RuntimeOrigin;
type RuntimeCall = RuntimeCall;
Expand All @@ -139,9 +162,6 @@ pub mod tests {

// type Nonce = u32;
// type BlockNumber = u32;
// type Header =
// sp_runtime::generic::Header<frame_system::pallet_prelude::BlockNumberFor<Self>,
// Self::Hashing>;
// type Hash = sp_core::hash::H256;
// type Hashing = sp_runtime::traits::BlakeTwo256;
// type AccountId = u64;
Expand All @@ -162,15 +182,17 @@ pub mod tests {
type SS58Prefix = frame_support::traits::ConstU16<456>;
}

// Similarly, we use the defaults provided by own crate as well.
use pallet::config_preludes::*;
parameter_types! {
pub const SomeCall: RuntimeCall = RuntimeCall::System(frame_system::Call::<Runtime>::remark { remark: vec![] });
}

#[derive_impl(TestDefaultConfig as pallet::DefaultConfig)]
impl crate::pallet::Config for Test {
impl pallet_default_config_example::Config for Runtime {
// These two both cannot have defaults.
type RuntimeEvent = RuntimeEvent;
// Note that the default account-id type in
// `frame_system::config_preludes::TestDefaultConfig` is `u64`.
type CannotHaveDefault = frame_support::traits::ConstU64<1>;

type HasNoDefault = frame_support::traits::ConstU32<1>;
type CannotHaveDefault = SomeCall;

type OverwrittenDefaultValue = frame_support::traits::ConstU32<678>;
type OverwrittenDefaultType = u128;
Expand All @@ -183,22 +205,22 @@ pub mod tests {

// assert one of the value types that is not overwritten.
assert_eq!(
<<Test as Config>::WithDefaultValue as Get<u32>>::get(),
<<Runtime as Config>::WithDefaultValue as Get<u32>>::get(),
<<TestDefaultConfig as DefaultConfig>::WithDefaultValue as Get<u32>>::get()
);

// assert one of the value types that is overwritten.
assert_eq!(<<Test as Config>::OverwrittenDefaultValue as Get<u32>>::get(), 678u32);
assert_eq!(<<Runtime as Config>::OverwrittenDefaultValue as Get<u32>>::get(), 678u32);

// assert one of the types that is not overwritten.
assert_eq!(
std::any::TypeId::of::<<Test as Config>::WithDefaultType>(),
std::any::TypeId::of::<<Runtime as Config>::WithDefaultType>(),
std::any::TypeId::of::<<TestDefaultConfig as DefaultConfig>::WithDefaultType>()
);

// assert one of the types that is overwritten.
assert_eq!(
std::any::TypeId::of::<<Test as Config>::OverwrittenDefaultType>(),
std::any::TypeId::of::<<Runtime as Config>::OverwrittenDefaultType>(),
std::any::TypeId::of::<u128>()
)
}
Expand Down
1 change: 0 additions & 1 deletion frame/multisig/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ log = { version = "0.4.17", default-features = false }

[dev-dependencies]
pallet-balances = { version = "4.0.0-dev", path = "../balances" }
sp-core = { version = "21.0.0", path = "../../primitives/core" }

[features]
default = ["std"]
Expand Down
50 changes: 14 additions & 36 deletions frame/multisig/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,12 @@ use super::*;

use crate as pallet_multisig;
use frame_support::{
assert_noop, assert_ok,
assert_noop, assert_ok, derive_impl,
traits::{ConstU32, ConstU64, Contains},
};
use sp_core::H256;
use sp_runtime::{
traits::{BlakeTwo256, IdentityLookup},
BuildStorage, TokenError,
};
use sp_runtime::{BuildStorage, TokenError};

type Block = frame_system::mocking::MockBlock<Test>;
type Block = frame_system::mocking::MockBlockU32<Test>;

frame_support::construct_runtime!(
pub enum Test
Expand All @@ -43,46 +39,28 @@ frame_support::construct_runtime!(
}
);

#[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig)]
impl frame_system::Config for Test {
type BaseCallFilter = TestBaseCallFilter;
type BlockWeights = ();
type BlockLength = ();
type DbWeight = ();
type Block = Block;
type BlockHashCount = ConstU32<250>;
type RuntimeOrigin = RuntimeOrigin;
type Nonce = u64;
type Hash = H256;
type RuntimeCall = RuntimeCall;
type Hashing = BlakeTwo256;
type AccountId = u64;
type Lookup = IdentityLookup<Self::AccountId>;
type Block = Block;
type RuntimeEvent = RuntimeEvent;
type BlockHashCount = ConstU64<250>;
type Version = ();
type BaseCallFilter = TestBaseCallFilter;
type PalletInfo = PalletInfo;
type AccountData = pallet_balances::AccountData<u64>;
type OnNewAccount = ();
type OnKilledAccount = ();
type SystemWeightInfo = ();
type SS58Prefix = ();
type OnSetCode = ();
type MaxConsumers = ConstU32<16>;

type AccountData = pallet_balances::AccountData<u64>;
}

#[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 = u64;
type RuntimeEvent = RuntimeEvent;
type RuntimeHoldReason = ();
type ReserveIdentifier = [u8; 8];
type DustRemoval = ();
type ExistentialDeposit = ConstU64<1>;
type AccountStore = System;
type WeightInfo = ();
type FreezeIdentifier = ();
type MaxFreezes = ();
type RuntimeHoldReason = ();
type MaxHolds = ();
type ExistentialDeposit = ConstU64<1>;
}

pub struct TestBaseCallFilter;
Expand Down Expand Up @@ -120,7 +98,7 @@ pub fn new_test_ext() -> sp_io::TestExternalities {
ext
}

fn now() -> Timepoint<u64> {
fn now() -> Timepoint<u32> {
Multisig::timepoint()
}

Expand Down
Loading

0 comments on commit 90e379a

Please sign in to comment.