-
Notifications
You must be signed in to change notification settings - Fork 19
Perform the deduplication of the validators set #1621
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
MOZGIII
merged 7 commits into
integrate-fixed-validators-set
from
integrate-fixed-validators-set-next
Oct 9, 2025
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
15cc8b3
Add dedupe utils
MOZGIII c66dc16
Implement validator deduplication based on account id and the first-i…
MOZGIII bec07a1
Fix the doc comment on the DedupeKeyExtractor::Output
MOZGIII 0a46684
Fixed doc comment on DedupeIter::dedupe_key_extractor
MOZGIII cc5f068
Apply suggestion from @dmitrylavrenov
MOZGIII 1c1a362
Add a test
MOZGIII f5776c3
Remove the broken impl (doesn't work either way)
MOZGIII File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| //! Utilities. | ||
|
|
||
| mod dedupe_iter; | ||
|
|
||
| pub use self::dedupe_iter::*; |
119 changes: 119 additions & 0 deletions
119
crates/pallet-humanode-session/src/utils/dedupe_iter.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| //! Deduplicating iterator wrapper. | ||
|
|
||
| use alloc::collections::BTreeSet; | ||
|
|
||
| /// Dedupe key extraction. | ||
| /// | ||
| /// Provides a dedupe key for type `T`. | ||
| pub trait DedupeKeyExtractor<T> { | ||
| /// The type this extractor extracts; the dedupe key. | ||
| type Output; | ||
|
|
||
| /// Perform the dedupe key extraction from the given value. | ||
| fn extract_key(&self, value: &T) -> Self::Output; | ||
| } | ||
|
|
||
| /// The dedupe iterator. | ||
| pub struct DedupeIter<Source, DedupeKeyExtractor> | ||
| where | ||
| Source: Iterator, | ||
| DedupeKeyExtractor: self::DedupeKeyExtractor<Source::Item>, | ||
| { | ||
| /// The source iterator. | ||
| pub source: Source, | ||
|
|
||
| /// The dedupe key extractor. | ||
| pub dedupe_key_extractor: DedupeKeyExtractor, | ||
|
|
||
| /// The state for tracking the duplicates. | ||
| pub dedupe_state: | ||
| BTreeSet<<DedupeKeyExtractor as self::DedupeKeyExtractor<Source::Item>>::Output>, | ||
| } | ||
|
|
||
| impl<Source, DedupeKeyExtractor> DedupeIter<Source, DedupeKeyExtractor> | ||
| where | ||
| Source: Iterator, | ||
| DedupeKeyExtractor: self::DedupeKeyExtractor<Source::Item>, | ||
| { | ||
| /// Create a new dedupe iterator from the given iterator. | ||
| pub fn new(source: Source, dedupe_key_extractor: DedupeKeyExtractor) -> Self { | ||
| Self { | ||
| source, | ||
| dedupe_key_extractor, | ||
| dedupe_state: Default::default(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<Source, DedupeKeyExtractor> Iterator for DedupeIter<Source, DedupeKeyExtractor> | ||
| where | ||
| Source: Iterator, | ||
| DedupeKeyExtractor: self::DedupeKeyExtractor<Source::Item>, | ||
| DedupeKeyExtractor::Output: Ord, | ||
| { | ||
| type Item = Source::Item; | ||
|
|
||
| fn next(&mut self) -> Option<Self::Item> { | ||
| loop { | ||
| let item = self.source.next()?; | ||
| let dedupe_key = self.dedupe_key_extractor.extract_key(&item); | ||
| let was_new = self.dedupe_state.insert(dedupe_key); | ||
| if !was_new { | ||
| continue; | ||
| } | ||
| return Some(item); | ||
| } | ||
| } | ||
|
|
||
| fn size_hint(&self) -> (usize, Option<usize>) { | ||
| let (_source_lower, source_higher) = self.source.size_hint(); | ||
|
|
||
| // Lower bound is always unpredictably `0`. | ||
| (0, source_higher) | ||
| } | ||
| } | ||
|
|
||
| /// [`Iterator`] extension trait for `dedupe` fn. | ||
| pub trait DedupeIteratorExt: Iterator + Sized { | ||
| /// Deduplicate the iterator. | ||
| fn dedupe<DedupeKeyExtractor: self::DedupeKeyExtractor<Self::Item>>( | ||
| self, | ||
| dedupe_key_extractor: DedupeKeyExtractor, | ||
| ) -> DedupeIter<Self, DedupeKeyExtractor>; | ||
| } | ||
|
|
||
| impl<T> DedupeIteratorExt for T | ||
| where | ||
| T: Iterator, | ||
| { | ||
| fn dedupe<DedupeKeyExtractor: self::DedupeKeyExtractor<Self::Item>>( | ||
| self, | ||
| dedupe_key_extractor: DedupeKeyExtractor, | ||
| ) -> DedupeIter<Self, DedupeKeyExtractor> { | ||
| DedupeIter::new(self, dedupe_key_extractor) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| struct IdentityCopy; | ||
|
|
||
| impl<T: Copy> DedupeKeyExtractor<T> for IdentityCopy { | ||
| type Output = T; | ||
|
|
||
| fn extract_key(&self, value: &T) -> Self::Output { | ||
| *value | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn dedupe() { | ||
| let iter = vec![1usize, 2, 1].into_iter(); | ||
|
|
||
| let deduped_iter = iter.dedupe(IdentityCopy); | ||
|
|
||
| assert_eq!(deduped_iter.collect::<Vec<_>>(), vec![1, 2]); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.