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

Commit 0bac4f2

Browse files
committed
WIP bounding to_supports
1 parent c606d87 commit 0bac4f2

File tree

1 file changed

+15
-6
lines changed
  • primitives/npos-elections/src

1 file changed

+15
-6
lines changed

primitives/npos-elections/src/lib.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ pub enum Error {
133133
ArithmeticError(&'static str),
134134
/// The data provided to create support map was invalid.
135135
InvalidSupportEdge,
136+
/// The solution has more targets than desired.
137+
SolutionExceedsDesiredTargets,
136138
}
137139

138140
/// 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> {
346348
/// Total support.
347349
pub total: ExtendedBalance,
348350
/// Support from voters.
349-
pub voters: Vec<(AccountId, ExtendedBalance)>,
351+
pub voters: Vec<(AccountId, ExtendedBalance)>, // TODO this needs to Bounded
350352
}
351353

352354
/// A target-major representation of the the election outcome.
@@ -356,6 +358,9 @@ pub struct Support<AccountId> {
356358
/// The main advantage of this is that it is encodable.
357359
pub type Supports<A> = Vec<(A, Support<A>)>;
358360

361+
/// Same as [`Supports`], but bounded.
362+
pub type SupportsBounded<A, B: Get<u32>> = BoundVec<(A, Support<A>), B>;
363+
359364
/// Linkage from a winner to their [`Support`].
360365
///
361366
/// This is more helpful than a normal [`Supports`] as it allows faster error checking.
@@ -367,9 +372,10 @@ pub trait FlattenSupportMap<A> {
367372
fn flatten(self) -> Supports<A>;
368373
}
369374

370-
impl<A> FlattenSupportMap<A> for SupportMap<A> {
375+
impl<A, B> FlattenSupportMap<A, B> for SupportMap<A> {
371376
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<_>>()
373379
}
374380
}
375381

@@ -405,11 +411,14 @@ pub fn to_support_map<AccountId: IdentifierT>(
405411
/// flat vector.
406412
///
407413
/// 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>>(
409415
winners: &[AccountId],
410416
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)
413422
}
414423

415424
/// Extension trait for evaluating a support map or vector.

0 commit comments

Comments
 (0)