add iter-from for bags-list (#11104)

* add iter-from for bags-list

* Fix

* Apply suggestions from code review

Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Fix

Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
This commit is contained in:
Kian Paimani
2022-03-31 15:16:34 +01:00
committed by GitHub
parent 6ec4ee1647
commit c2d3d488b8
5 changed files with 79 additions and 2 deletions
+17 -1
View File
@@ -1308,7 +1308,6 @@ impl<T: Config> SortedListProvider<T::AccountId> for UseNominatorsAndValidatorsM
type Error = ();
type Score = VoteWeight;
/// Returns iterator over voter list, which can have `take` called on it.
fn iter() -> Box<dyn Iterator<Item = T::AccountId>> {
Box::new(
Validators::<T>::iter()
@@ -1316,6 +1315,23 @@ impl<T: Config> SortedListProvider<T::AccountId> for UseNominatorsAndValidatorsM
.chain(Nominators::<T>::iter().map(|(n, _)| n)),
)
}
fn iter_from(
start: &T::AccountId,
) -> Result<Box<dyn Iterator<Item = T::AccountId>>, Self::Error> {
if Validators::<T>::contains_key(start) {
let start_key = Validators::<T>::hashed_key_for(start);
Ok(Box::new(
Validators::<T>::iter_from(start_key)
.map(|(n, _)| n)
.chain(Nominators::<T>::iter().map(|(x, _)| x)),
))
} else if Nominators::<T>::contains_key(start) {
let start_key = Nominators::<T>::hashed_key_for(start);
Ok(Box::new(Nominators::<T>::iter_from(start_key).map(|(n, _)| n)))
} else {
Err(())
}
}
fn count() -> u32 {
Nominators::<T>::count().saturating_add(Validators::<T>::count())
}