Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 90e379a

Browse files
kianenigmagupnik
andauthored
add frame_system::DefaultConfig to individual pallet DefaultConfigs (#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 <[email protected]> Co-authored-by: command-bot <>
1 parent ca379d7 commit 90e379a

File tree

12 files changed

+201
-133
lines changed

12 files changed

+201
-133
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frame/balances/src/lib.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,33 @@ pub mod pallet {
213213

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

216-
#[pallet::config]
216+
/// Default implementations of [`DefaultConfig`], which can be used to implement [`Config`].
217+
pub mod config_preludes {
218+
use super::*;
219+
use frame_support::derive_impl;
220+
221+
pub struct TestDefaultConfig;
222+
223+
#[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig)]
224+
impl frame_system::DefaultConfig for TestDefaultConfig {}
225+
226+
#[frame_support::register_default_impl(TestDefaultConfig)]
227+
impl DefaultConfig for TestDefaultConfig {
228+
type Balance = u64;
229+
230+
type ReserveIdentifier = ();
231+
type FreezeIdentifier = ();
232+
233+
type MaxLocks = ();
234+
type MaxReserves = ();
235+
type MaxFreezes = ();
236+
type MaxHolds = ();
237+
238+
type WeightInfo = ();
239+
}
240+
}
241+
242+
#[pallet::config(with_default)]
217243
pub trait Config<I: 'static = ()>: frame_system::Config {
218244
/// The overarching event type.
219245
type RuntimeEvent: From<Event<Self, I>>
@@ -236,6 +262,7 @@ pub mod pallet {
236262
+ FixedPointOperand;
237263

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

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

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

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

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

263293
/// The ID type for freezes.

frame/examples/default-config/src/lib.rs

Lines changed: 52 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,20 @@ pub mod pallet {
5555
/// in our tests below.
5656
type OverwrittenDefaultValue: Get<u32>;
5757

58-
/// An input parameter that relies on `<Self as frame_system::Config>::AccountId`. As of
59-
/// now, such types cannot have defaults and need to be annotated as such, iff
60-
/// `#[pallet::config(with_default)]` is enabled:
58+
/// An input parameter that relies on `<Self as frame_system::Config>::AccountId`. This can
59+
/// too have a default, as long as as it is present in `frame_system::DefaultConfig`.
60+
type CanDeriveDefaultFromSystem: Get<Self::AccountId>;
61+
62+
/// We might chose to declare as one that doesn't have a default, for whatever semantical
63+
/// reason.
6164
#[pallet::no_default]
62-
type CannotHaveDefault: Get<Self::AccountId>;
65+
type HasNoDefault: Get<u32>;
66+
67+
/// Some types can technically have no default, such as those the rely on
68+
/// `frame_system::Config` but are not present in `frame_system::DefaultConfig`. For
69+
/// example, a `RuntimeCall` cannot reasonably have a default.
70+
#[pallet::no_default] // if we skip this, there will be a compiler error.
71+
type CannotHaveDefault: Get<Self::RuntimeCall>;
6372

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

7787
/// A type providing default configurations for this pallet in testing environment.
7888
pub struct TestDefaultConfig;
89+
90+
#[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig)]
91+
impl frame_system::DefaultConfig for TestDefaultConfig {}
92+
7993
#[frame_support::register_default_impl(TestDefaultConfig)]
8094
impl DefaultConfig for TestDefaultConfig {
8195
type WithDefaultValue = frame_support::traits::ConstU32<42>;
8296
type OverwrittenDefaultValue = frame_support::traits::ConstU32<42>;
8397

98+
// `frame_system::config_preludes::TestDefaultConfig` declares account-id as u64.
99+
type CanDeriveDefaultFromSystem = frame_support::traits::ConstU64<42>;
100+
84101
type WithDefaultType = u32;
85102
type OverwrittenDefaultType = u32;
86103
}
87104

88-
/// A type providing default configurations for this pallet in a parachain environment.
89-
pub struct ParachainDefaultConfig;
90-
#[frame_support::register_default_impl(ParachainDefaultConfig)]
91-
impl DefaultConfig for ParachainDefaultConfig {
105+
/// A type providing default configurations for this pallet in another environment. Examples
106+
/// could be a parachain, or a solo-chain.
107+
///
108+
/// Appropriate derive for `frame_system::DefaultConfig` needs to be provided. In this
109+
/// example, we simple derive `frame_system::config_preludes::TestDefaultConfig` again.
110+
pub struct OtherDefaultConfig;
111+
112+
#[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig)]
113+
impl frame_system::DefaultConfig for OtherDefaultConfig {}
114+
115+
#[frame_support::register_default_impl(OtherDefaultConfig)]
116+
impl DefaultConfig for OtherDefaultConfig {
92117
type WithDefaultValue = frame_support::traits::ConstU32<66>;
93118
type OverwrittenDefaultValue = frame_support::traits::ConstU32<66>;
119+
type CanDeriveDefaultFromSystem = frame_support::traits::ConstU64<42>;
94120
type WithDefaultType = u32;
95121
type OverwrittenDefaultType = u32;
96122
}
@@ -106,28 +132,25 @@ pub mod pallet {
106132
#[cfg(any(test, doc))]
107133
pub mod tests {
108134
use super::*;
109-
use frame_support::derive_impl;
110-
use sp_runtime::traits::ConstU64;
111-
112-
use super::pallet as pallet_default_config_example;
135+
use frame_support::{derive_impl, parameter_types};
136+
use pallet::{self as pallet_default_config_example, config_preludes::*};
113137

114-
type Block = frame_system::mocking::MockBlock<Test>;
138+
type Block = frame_system::mocking::MockBlock<Runtime>;
115139

116140
frame_support::construct_runtime!(
117-
pub enum Test
118-
{
141+
pub struct Runtime {
119142
System: frame_system,
120143
DefaultPallet: pallet_default_config_example,
121144
}
122145
);
123146

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

140163
// type Nonce = u32;
141164
// type BlockNumber = u32;
142-
// type Header =
143-
// sp_runtime::generic::Header<frame_system::pallet_prelude::BlockNumberFor<Self>,
144-
// Self::Hashing>;
145165
// type Hash = sp_core::hash::H256;
146166
// type Hashing = sp_runtime::traits::BlakeTwo256;
147167
// type AccountId = u64;
@@ -162,15 +182,17 @@ pub mod tests {
162182
type SS58Prefix = frame_support::traits::ConstU16<456>;
163183
}
164184

165-
// Similarly, we use the defaults provided by own crate as well.
166-
use pallet::config_preludes::*;
185+
parameter_types! {
186+
pub const SomeCall: RuntimeCall = RuntimeCall::System(frame_system::Call::<Runtime>::remark { remark: vec![] });
187+
}
188+
167189
#[derive_impl(TestDefaultConfig as pallet::DefaultConfig)]
168-
impl crate::pallet::Config for Test {
190+
impl pallet_default_config_example::Config for Runtime {
169191
// These two both cannot have defaults.
170192
type RuntimeEvent = RuntimeEvent;
171-
// Note that the default account-id type in
172-
// `frame_system::config_preludes::TestDefaultConfig` is `u64`.
173-
type CannotHaveDefault = frame_support::traits::ConstU64<1>;
193+
194+
type HasNoDefault = frame_support::traits::ConstU32<1>;
195+
type CannotHaveDefault = SomeCall;
174196

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

184206
// assert one of the value types that is not overwritten.
185207
assert_eq!(
186-
<<Test as Config>::WithDefaultValue as Get<u32>>::get(),
208+
<<Runtime as Config>::WithDefaultValue as Get<u32>>::get(),
187209
<<TestDefaultConfig as DefaultConfig>::WithDefaultValue as Get<u32>>::get()
188210
);
189211

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

193215
// assert one of the types that is not overwritten.
194216
assert_eq!(
195-
std::any::TypeId::of::<<Test as Config>::WithDefaultType>(),
217+
std::any::TypeId::of::<<Runtime as Config>::WithDefaultType>(),
196218
std::any::TypeId::of::<<TestDefaultConfig as DefaultConfig>::WithDefaultType>()
197219
);
198220

199221
// assert one of the types that is overwritten.
200222
assert_eq!(
201-
std::any::TypeId::of::<<Test as Config>::OverwrittenDefaultType>(),
223+
std::any::TypeId::of::<<Runtime as Config>::OverwrittenDefaultType>(),
202224
std::any::TypeId::of::<u128>()
203225
)
204226
}

frame/multisig/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ log = { version = "0.4.17", default-features = false }
2727

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

3231
[features]
3332
default = ["std"]

frame/multisig/src/tests.rs

Lines changed: 14 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,12 @@ use super::*;
2323

2424
use crate as pallet_multisig;
2525
use frame_support::{
26-
assert_noop, assert_ok,
26+
assert_noop, assert_ok, derive_impl,
2727
traits::{ConstU32, ConstU64, Contains},
2828
};
29-
use sp_core::H256;
30-
use sp_runtime::{
31-
traits::{BlakeTwo256, IdentityLookup},
32-
BuildStorage, TokenError,
33-
};
29+
use sp_runtime::{BuildStorage, TokenError};
3430

35-
type Block = frame_system::mocking::MockBlock<Test>;
31+
type Block = frame_system::mocking::MockBlockU32<Test>;
3632

3733
frame_support::construct_runtime!(
3834
pub enum Test
@@ -43,46 +39,28 @@ frame_support::construct_runtime!(
4339
}
4440
);
4541

42+
#[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig)]
4643
impl frame_system::Config for Test {
47-
type BaseCallFilter = TestBaseCallFilter;
48-
type BlockWeights = ();
49-
type BlockLength = ();
50-
type DbWeight = ();
44+
type Block = Block;
45+
type BlockHashCount = ConstU32<250>;
5146
type RuntimeOrigin = RuntimeOrigin;
52-
type Nonce = u64;
53-
type Hash = H256;
5447
type RuntimeCall = RuntimeCall;
55-
type Hashing = BlakeTwo256;
56-
type AccountId = u64;
57-
type Lookup = IdentityLookup<Self::AccountId>;
58-
type Block = Block;
5948
type RuntimeEvent = RuntimeEvent;
60-
type BlockHashCount = ConstU64<250>;
61-
type Version = ();
49+
type BaseCallFilter = TestBaseCallFilter;
6250
type PalletInfo = PalletInfo;
63-
type AccountData = pallet_balances::AccountData<u64>;
64-
type OnNewAccount = ();
65-
type OnKilledAccount = ();
66-
type SystemWeightInfo = ();
67-
type SS58Prefix = ();
6851
type OnSetCode = ();
69-
type MaxConsumers = ConstU32<16>;
52+
53+
type AccountData = pallet_balances::AccountData<u64>;
7054
}
7155

56+
#[derive_impl(pallet_balances::config_preludes::TestDefaultConfig as pallet_balances::DefaultConfig)]
7257
impl pallet_balances::Config for Test {
73-
type MaxLocks = ();
74-
type MaxReserves = ();
75-
type ReserveIdentifier = [u8; 8];
76-
type Balance = u64;
7758
type RuntimeEvent = RuntimeEvent;
59+
type RuntimeHoldReason = ();
60+
type ReserveIdentifier = [u8; 8];
7861
type DustRemoval = ();
79-
type ExistentialDeposit = ConstU64<1>;
8062
type AccountStore = System;
81-
type WeightInfo = ();
82-
type FreezeIdentifier = ();
83-
type MaxFreezes = ();
84-
type RuntimeHoldReason = ();
85-
type MaxHolds = ();
63+
type ExistentialDeposit = ConstU64<1>;
8664
}
8765

8866
pub struct TestBaseCallFilter;
@@ -120,7 +98,7 @@ pub fn new_test_ext() -> sp_io::TestExternalities {
12098
ext
12199
}
122100

123-
fn now() -> Timepoint<u64> {
101+
fn now() -> Timepoint<u32> {
124102
Multisig::timepoint()
125103
}
126104

0 commit comments

Comments
 (0)