@@ -133,6 +133,8 @@ pub enum Error {
133
133
ArithmeticError ( & ' static str ) ,
134
134
/// The data provided to create support map was invalid.
135
135
InvalidSupportEdge ,
136
+ /// The solution has more targets than desired.
137
+ SolutionExceedsDesiredTargets ,
136
138
}
137
139
138
140
/// A type which is used in the API of this crate as a numeric weight of a vote, most often the
@@ -346,7 +348,7 @@ pub struct Support<AccountId> {
346
348
/// Total support.
347
349
pub total : ExtendedBalance ,
348
350
/// Support from voters.
349
- pub voters : Vec < ( AccountId , ExtendedBalance ) > ,
351
+ pub voters : Vec < ( AccountId , ExtendedBalance ) > , // TODO this needs to Bounded
350
352
}
351
353
352
354
/// A target-major representation of the the election outcome.
@@ -356,6 +358,9 @@ pub struct Support<AccountId> {
356
358
/// The main advantage of this is that it is encodable.
357
359
pub type Supports < A > = Vec < ( A , Support < A > ) > ;
358
360
361
+ /// Same as [`Supports`], but bounded.
362
+ pub type SupportsBounded < A , B : Get < u32 > > = BoundVec < ( A , Support < A > ) , B > ;
363
+
359
364
/// Linkage from a winner to their [`Support`].
360
365
///
361
366
/// This is more helpful than a normal [`Supports`] as it allows faster error checking.
@@ -367,9 +372,10 @@ pub trait FlattenSupportMap<A> {
367
372
fn flatten ( self ) -> Supports < A > ;
368
373
}
369
374
370
- impl < A > FlattenSupportMap < A > for SupportMap < A > {
375
+ impl < A , B > FlattenSupportMap < A , B > for SupportMap < A > {
371
376
fn flatten ( self ) -> Supports < A > {
372
- self . into_iter ( ) . collect :: < Vec < _ > > ( )
377
+ // TODO maybe there is a way to do this as bounded before collecting into a Vec
378
+ self . into_iter ( ) . collect :: < Vec < _ > > ( )
373
379
}
374
380
}
375
381
@@ -405,11 +411,14 @@ pub fn to_support_map<AccountId: IdentifierT>(
405
411
/// flat vector.
406
412
///
407
413
/// Similar to [`to_support_map`], `winners` is used for error checking.
408
- pub fn to_supports < AccountId : IdentifierT > (
414
+ pub fn to_supports < AccountId : IdentifierT , DesiredTargets : Get < u32 > > (
409
415
winners : & [ AccountId ] ,
410
416
assignments : & [ StakedAssignment < AccountId > ] ,
411
- ) -> Result < Supports < AccountId > , Error > {
412
- to_support_map ( winners, assignments) . map ( FlattenSupportMap :: flatten)
417
+ ) -> Result < SupportsBounded < AccountId , DesiredTargets > , Error > {
418
+ to_support_map ( winners, assignments)
419
+ . map ( FlattenSupportMap :: flatten) // TODO should probably bound inside of flatten
420
+ . try_into ( )
421
+ . map_err ( |_| Error :: SolutionExceedsDesiredTargets )
413
422
}
414
423
415
424
/// Extension trait for evaluating a support map or vector.
0 commit comments