@@ -55,11 +55,20 @@ pub mod pallet {
55
55
/// in our tests below.
56
56
type OverwrittenDefaultValue : Get < u32 > ;
57
57
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.
61
64
#[ 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 > ;
63
72
64
73
/// Something that is a normal type, with default.
65
74
type WithDefaultType ;
@@ -73,24 +82,41 @@ pub mod pallet {
73
82
pub mod config_preludes {
74
83
// This will help use not need to disambiguate anything when using `derive_impl`.
75
84
use super :: * ;
85
+ use frame_support:: derive_impl;
76
86
77
87
/// A type providing default configurations for this pallet in testing environment.
78
88
pub struct TestDefaultConfig ;
89
+
90
+ #[ derive_impl( frame_system:: config_preludes:: TestDefaultConfig as frame_system:: DefaultConfig ) ]
91
+ impl frame_system:: DefaultConfig for TestDefaultConfig { }
92
+
79
93
#[ frame_support:: register_default_impl( TestDefaultConfig ) ]
80
94
impl DefaultConfig for TestDefaultConfig {
81
95
type WithDefaultValue = frame_support:: traits:: ConstU32 < 42 > ;
82
96
type OverwrittenDefaultValue = frame_support:: traits:: ConstU32 < 42 > ;
83
97
98
+ // `frame_system::config_preludes::TestDefaultConfig` declares account-id as u64.
99
+ type CanDeriveDefaultFromSystem = frame_support:: traits:: ConstU64 < 42 > ;
100
+
84
101
type WithDefaultType = u32 ;
85
102
type OverwrittenDefaultType = u32 ;
86
103
}
87
104
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 {
92
117
type WithDefaultValue = frame_support:: traits:: ConstU32 < 66 > ;
93
118
type OverwrittenDefaultValue = frame_support:: traits:: ConstU32 < 66 > ;
119
+ type CanDeriveDefaultFromSystem = frame_support:: traits:: ConstU64 < 42 > ;
94
120
type WithDefaultType = u32 ;
95
121
type OverwrittenDefaultType = u32 ;
96
122
}
@@ -106,28 +132,25 @@ pub mod pallet {
106
132
#[ cfg( any( test, doc) ) ]
107
133
pub mod tests {
108
134
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:: * } ;
113
137
114
- type Block = frame_system:: mocking:: MockBlock < Test > ;
138
+ type Block = frame_system:: mocking:: MockBlock < Runtime > ;
115
139
116
140
frame_support:: construct_runtime!(
117
- pub enum Test
118
- {
141
+ pub struct Runtime {
119
142
System : frame_system,
120
143
DefaultPallet : pallet_default_config_example,
121
144
}
122
145
) ;
123
146
124
147
#[ 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 {
126
149
// these items are defined by frame-system as `no_default`, so we must specify them here.
127
150
// Note that these are types that actually rely on the outer runtime, and can't sensibly
128
151
// have an _independent_ default.
129
152
type Block = Block ;
130
- type BlockHashCount = ConstU64 < 10 > ;
153
+ type BlockHashCount = frame_support :: traits :: ConstU64 < 10 > ;
131
154
type BaseCallFilter = frame_support:: traits:: Everything ;
132
155
type RuntimeOrigin = RuntimeOrigin ;
133
156
type RuntimeCall = RuntimeCall ;
@@ -139,9 +162,6 @@ pub mod tests {
139
162
140
163
// type Nonce = u32;
141
164
// type BlockNumber = u32;
142
- // type Header =
143
- // sp_runtime::generic::Header<frame_system::pallet_prelude::BlockNumberFor<Self>,
144
- // Self::Hashing>;
145
165
// type Hash = sp_core::hash::H256;
146
166
// type Hashing = sp_runtime::traits::BlakeTwo256;
147
167
// type AccountId = u64;
@@ -162,15 +182,17 @@ pub mod tests {
162
182
type SS58Prefix = frame_support:: traits:: ConstU16 < 456 > ;
163
183
}
164
184
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
+
167
189
#[ derive_impl( TestDefaultConfig as pallet:: DefaultConfig ) ]
168
- impl crate :: pallet :: Config for Test {
190
+ impl pallet_default_config_example :: Config for Runtime {
169
191
// These two both cannot have defaults.
170
192
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 ;
174
196
175
197
type OverwrittenDefaultValue = frame_support:: traits:: ConstU32 < 678 > ;
176
198
type OverwrittenDefaultType = u128 ;
@@ -183,22 +205,22 @@ pub mod tests {
183
205
184
206
// assert one of the value types that is not overwritten.
185
207
assert_eq ! (
186
- <<Test as Config >:: WithDefaultValue as Get <u32 >>:: get( ) ,
208
+ <<Runtime as Config >:: WithDefaultValue as Get <u32 >>:: get( ) ,
187
209
<<TestDefaultConfig as DefaultConfig >:: WithDefaultValue as Get <u32 >>:: get( )
188
210
) ;
189
211
190
212
// 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 ) ;
192
214
193
215
// assert one of the types that is not overwritten.
194
216
assert_eq ! (
195
- std:: any:: TypeId :: of:: <<Test as Config >:: WithDefaultType >( ) ,
217
+ std:: any:: TypeId :: of:: <<Runtime as Config >:: WithDefaultType >( ) ,
196
218
std:: any:: TypeId :: of:: <<TestDefaultConfig as DefaultConfig >:: WithDefaultType >( )
197
219
) ;
198
220
199
221
// assert one of the types that is overwritten.
200
222
assert_eq ! (
201
- std:: any:: TypeId :: of:: <<Test as Config >:: OverwrittenDefaultType >( ) ,
223
+ std:: any:: TypeId :: of:: <<Runtime as Config >:: OverwrittenDefaultType >( ) ,
202
224
std:: any:: TypeId :: of:: <u128 >( )
203
225
)
204
226
}
0 commit comments