90
90
//!
91
91
//! pub trait Config: Sized {
92
92
//! type ElectionProvider: ElectionProvider<
93
- //! AccountId,
94
- //! BlockNumber,
95
- //! DataProvider = Module <Self>,
93
+ //! AccountId = AccountId ,
94
+ //! BlockNumber = BlockNumber ,
95
+ //! DataProvider = Pallet <Self>,
96
96
//! >;
97
97
//! }
98
98
//!
99
- //! pub struct Module <T: Config>(std::marker::PhantomData<T>);
99
+ //! pub struct Pallet <T: Config>(std::marker::PhantomData<T>);
100
100
//!
101
- //! impl<T: Config> ElectionDataProvider<AccountId, BlockNumber> for Module<T> {
101
+ //! impl<T: Config> ElectionDataProvider for Pallet<T> {
102
+ //! type AccountId = AccountId;
103
+ //! type BlockNumber = BlockNumber;
102
104
//! const MAXIMUM_VOTES_PER_VOTER: u32 = 1;
105
+ //!
103
106
//! fn desired_targets() -> data_provider::Result<u32> {
104
107
//! Ok(1)
105
108
//! }
106
109
//! fn voters(maybe_max_len: Option<usize>)
107
- //! -> data_provider::Result<Vec<(AccountId, VoteWeight, Vec<AccountId>)>>
110
+ //! -> data_provider::Result<Vec<(AccountId, VoteWeight, Vec<AccountId>)>>
108
111
//! {
109
112
//! Ok(Default::default())
110
113
//! }
124
127
//! pub struct GenericElectionProvider<T: Config>(std::marker::PhantomData<T>);
125
128
//!
126
129
//! pub trait Config {
127
- //! type DataProvider: ElectionDataProvider<AccountId, BlockNumber>;
130
+ //! type DataProvider: ElectionDataProvider<AccountId=AccountId, BlockNumber = BlockNumber>;
128
131
//! }
129
132
//!
130
- //! impl<T: Config> ElectionProvider<AccountId, BlockNumber> for GenericElectionProvider<T> {
133
+ //! impl<T: Config> ElectionProvider for GenericElectionProvider<T> {
134
+ //! type AccountId = AccountId;
135
+ //! type BlockNumber = BlockNumber;
131
136
//! type Error = &'static str;
132
137
//! type DataProvider = T::DataProvider;
133
138
//!
146
151
//!
147
152
//! struct Runtime;
148
153
//! impl generic_election_provider::Config for Runtime {
149
- //! type DataProvider = data_provider_mod::Module <Runtime>;
154
+ //! type DataProvider = data_provider_mod::Pallet <Runtime>;
150
155
//! }
151
156
//!
152
157
//! impl data_provider_mod::Config for Runtime {
@@ -178,7 +183,13 @@ pub mod data_provider {
178
183
}
179
184
180
185
/// Something that can provide the data to an [`ElectionProvider`].
181
- pub trait ElectionDataProvider < AccountId , BlockNumber > {
186
+ pub trait ElectionDataProvider {
187
+ /// The account identifier type.
188
+ type AccountId ;
189
+
190
+ /// The block number type.
191
+ type BlockNumber ;
192
+
182
193
/// Maximum number of votes per voter that this data provider is providing.
183
194
const MAXIMUM_VOTES_PER_VOTER : u32 ;
184
195
@@ -189,7 +200,7 @@ pub trait ElectionDataProvider<AccountId, BlockNumber> {
189
200
///
190
201
/// This should be implemented as a self-weighing function. The implementor should register its
191
202
/// appropriate weight at the end of execution with the system pallet directly.
192
- fn targets ( maybe_max_len : Option < usize > ) -> data_provider:: Result < Vec < AccountId > > ;
203
+ fn targets ( maybe_max_len : Option < usize > ) -> data_provider:: Result < Vec < Self :: AccountId > > ;
193
204
194
205
/// All possible voters for the election.
195
206
///
@@ -202,7 +213,7 @@ pub trait ElectionDataProvider<AccountId, BlockNumber> {
202
213
/// appropriate weight at the end of execution with the system pallet directly.
203
214
fn voters (
204
215
maybe_max_len : Option < usize > ,
205
- ) -> data_provider:: Result < Vec < ( AccountId , VoteWeight , Vec < AccountId > ) > > ;
216
+ ) -> data_provider:: Result < Vec < ( Self :: AccountId , VoteWeight , Vec < Self :: AccountId > ) > > ;
206
217
207
218
/// The number of targets to elect.
208
219
///
@@ -216,14 +227,14 @@ pub trait ElectionDataProvider<AccountId, BlockNumber> {
216
227
/// [`ElectionProvider::elect`].
217
228
///
218
229
/// This is only useful for stateful election providers.
219
- fn next_election_prediction ( now : BlockNumber ) -> BlockNumber ;
230
+ fn next_election_prediction ( now : Self :: BlockNumber ) -> Self :: BlockNumber ;
220
231
221
232
/// Utility function only to be used in benchmarking scenarios, to be implemented optionally,
222
233
/// else a noop.
223
234
#[ cfg( any( feature = "runtime-benchmarks" , test) ) ]
224
235
fn put_snapshot (
225
- _voters : Vec < ( AccountId , VoteWeight , Vec < AccountId > ) > ,
226
- _targets : Vec < AccountId > ,
236
+ _voters : Vec < ( Self :: AccountId , VoteWeight , Vec < Self :: AccountId > ) > ,
237
+ _targets : Vec < Self :: AccountId > ,
227
238
_target_stake : Option < VoteWeight > ,
228
239
) {
229
240
}
@@ -233,22 +244,29 @@ pub trait ElectionDataProvider<AccountId, BlockNumber> {
233
244
///
234
245
/// Same as `put_snapshot`, but can add a single voter one by one.
235
246
#[ cfg( any( feature = "runtime-benchmarks" , test) ) ]
236
- fn add_voter ( _voter : AccountId , _weight : VoteWeight , _targets : Vec < AccountId > ) { }
247
+ fn add_voter ( _voter : Self :: AccountId , _weight : VoteWeight , _targets : Vec < Self :: AccountId > ) { }
237
248
238
249
/// Utility function only to be used in benchmarking scenarios, to be implemented optionally,
239
250
/// else a noop.
240
251
///
241
252
/// Same as `put_snapshot`, but can add a single voter one by one.
242
253
#[ cfg( any( feature = "runtime-benchmarks" , test) ) ]
243
- fn add_target ( _target : AccountId ) { }
254
+ fn add_target ( _target : Self :: AccountId ) { }
244
255
245
256
/// Clear all voters and targets.
246
257
#[ cfg( any( feature = "runtime-benchmarks" , test) ) ]
247
258
fn clear ( ) { }
248
259
}
249
260
261
+ /// An election data provider that should only be used for testing.
250
262
#[ cfg( feature = "std" ) ]
251
- impl < AccountId , BlockNumber > ElectionDataProvider < AccountId , BlockNumber > for ( ) {
263
+ pub struct TestDataProvider < X > ( sp_std:: marker:: PhantomData < X > ) ;
264
+
265
+ #[ cfg( feature = "std" ) ]
266
+ impl < AccountId , BlockNumber > ElectionDataProvider for TestDataProvider < ( AccountId , BlockNumber ) > {
267
+ type AccountId = AccountId ;
268
+ type BlockNumber = BlockNumber ;
269
+
252
270
const MAXIMUM_VOTES_PER_VOTER : u32 = 0 ;
253
271
fn targets ( _maybe_max_len : Option < usize > ) -> data_provider:: Result < Vec < AccountId > > {
254
272
Ok ( Default :: default ( ) )
@@ -271,29 +289,44 @@ impl<AccountId, BlockNumber> ElectionDataProvider<AccountId, BlockNumber> for ()
271
289
/// This trait only provides an interface to _request_ an election, i.e.
272
290
/// [`ElectionProvider::elect`]. That data required for the election need to be passed to the
273
291
/// implemented of this trait through [`ElectionProvider::DataProvider`].
274
- pub trait ElectionProvider < AccountId , BlockNumber > {
292
+ pub trait ElectionProvider {
293
+ /// The account identifier type.
294
+ type AccountId ;
295
+
296
+ /// The block number type.
297
+ type BlockNumber ;
298
+
275
299
/// The error type that is returned by the provider.
276
300
type Error : Debug ;
277
301
278
302
/// The data provider of the election.
279
- type DataProvider : ElectionDataProvider < AccountId , BlockNumber > ;
303
+ type DataProvider : ElectionDataProvider <
304
+ AccountId = Self :: AccountId ,
305
+ BlockNumber = Self :: BlockNumber ,
306
+ > ;
280
307
281
308
/// Elect a new set of winners.
282
309
///
283
310
/// The result is returned in a target major format, namely as vector of supports.
284
311
///
285
312
/// This should be implemented as a self-weighing function. The implementor should register its
286
313
/// appropriate weight at the end of execution with the system pallet directly.
287
- fn elect ( ) -> Result < Supports < AccountId > , Self :: Error > ;
314
+ fn elect ( ) -> Result < Supports < Self :: AccountId > , Self :: Error > ;
288
315
}
289
316
317
+ /// An election provider to be used only for testing.
290
318
#[ cfg( feature = "std" ) ]
291
- impl < AccountId , BlockNumber > ElectionProvider < AccountId , BlockNumber > for ( ) {
319
+ pub struct NoElection < X > ( sp_std:: marker:: PhantomData < X > ) ;
320
+
321
+ #[ cfg( feature = "std" ) ]
322
+ impl < AccountId , BlockNumber > ElectionProvider for NoElection < ( AccountId , BlockNumber ) > {
323
+ type AccountId = AccountId ;
324
+ type BlockNumber = BlockNumber ;
292
325
type Error = & ' static str ;
293
- type DataProvider = ( ) ;
326
+ type DataProvider = TestDataProvider < ( AccountId , BlockNumber ) > ;
294
327
295
328
fn elect ( ) -> Result < Supports < AccountId > , Self :: Error > {
296
- Err ( "<() as ElectionProvider> cannot do anything." )
329
+ Err ( "<NoElection as ElectionProvider> cannot do anything." )
297
330
}
298
331
}
299
332
0 commit comments