-
Notifications
You must be signed in to change notification settings - Fork 104
Add core RelaySelector #1696
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
Open
bc1cindy
wants to merge
2
commits into
payjoin:master
Choose a base branch
from
bc1cindy:relay-selector
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+147
−22
Open
Add core RelaySelector #1696
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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,126 @@ | ||
| use bitcoin::secp256k1::rand::seq::IteratorRandom; | ||
| use bitcoin::secp256k1::rand::{self}; | ||
|
|
||
| use crate::Url; | ||
|
|
||
| /// Picks an OHTTP relay, excluding relays marked failed, so clients share one | ||
| /// selection policy instead of each diverging. | ||
| #[derive(Clone, Debug)] | ||
| pub struct RelaySelector { | ||
| relays: Vec<Url>, | ||
| failed: Vec<Url>, | ||
| } | ||
|
|
||
| impl RelaySelector { | ||
| /// Deduplicates `relays` (preserving order) so uniform selection isn't | ||
| /// skewed by a relay listed more than once. | ||
| pub fn new(relays: Vec<Url>) -> Self { | ||
| let mut deduped: Vec<Url> = Vec::new(); | ||
| for relay in relays { | ||
| if !deduped.contains(&relay) { | ||
| deduped.push(relay); | ||
| } | ||
| } | ||
| Self { relays: deduped, failed: Vec::new() } | ||
| } | ||
|
|
||
| /// Pick a relay, never one marked failed, or `None` when none remain. | ||
| pub fn select<R: rand::Rng>(&self, rng: &mut R) -> Option<Url> { | ||
| self.relays.iter().filter(|r| !self.failed.contains(r)).choose(rng).cloned() | ||
| } | ||
|
|
||
| /// Record a relay transport failure so `select` avoids it. | ||
| pub fn mark_failed(&mut self, relay: &Url) { | ||
| if !self.failed.contains(relay) { | ||
| self.failed.push(relay.clone()); | ||
| } | ||
| } | ||
|
|
||
| /// Clear all recorded failures so every configured relay is selectable again. | ||
| pub fn clear_failed(&mut self) { self.failed.clear(); } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use bitcoin::secp256k1::rand::rngs::StdRng; | ||
| use bitcoin::secp256k1::rand::SeedableRng; | ||
|
|
||
| use super::*; | ||
|
|
||
| fn relays() -> Vec<Url> { | ||
| ["https://a.example", "https://b.example", "https://c.example"] | ||
| .iter() | ||
| .map(|s| Url::parse(s).unwrap()) | ||
| .collect() | ||
| } | ||
|
|
||
| #[test] | ||
| fn select_returns_a_configured_relay() { | ||
| let selector = RelaySelector::new(relays()); | ||
| let mut rng = StdRng::seed_from_u64(1); | ||
| let picked = selector.select(&mut rng).expect("a relay"); | ||
| assert!(relays().contains(&picked)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn select_never_returns_a_failed_relay() { | ||
| let mut selector = RelaySelector::new(relays()); | ||
| let mut rng = StdRng::seed_from_u64(2); | ||
| let failed = Url::parse("https://a.example").unwrap(); | ||
| selector.mark_failed(&failed); | ||
| for _ in 0..50 { | ||
| assert_ne!(selector.select(&mut rng), Some(failed.clone())); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn select_is_none_when_all_failed() { | ||
| let mut selector = RelaySelector::new(relays()); | ||
| for r in relays() { | ||
| selector.mark_failed(&r); | ||
| } | ||
| let mut rng = StdRng::seed_from_u64(3); | ||
| assert_eq!(selector.select(&mut rng), None); | ||
| } | ||
|
|
||
| #[test] | ||
| fn clear_failed_restores_all_relays() { | ||
| let mut selector = RelaySelector::new(relays()); | ||
| for r in relays() { | ||
| selector.mark_failed(&r); | ||
| } | ||
| let mut rng = StdRng::seed_from_u64(6); | ||
| assert_eq!(selector.select(&mut rng), None); | ||
| selector.clear_failed(); | ||
| assert!(selector.select(&mut rng).is_some()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn new_dedups_relays_preserving_order() { | ||
| let a = Url::parse("https://a.example").unwrap(); | ||
| let b = Url::parse("https://b.example").unwrap(); | ||
| let selector = RelaySelector::new(vec![a.clone(), a.clone(), b.clone()]); | ||
| assert_eq!(selector.relays, vec![a, b]); | ||
| } | ||
|
|
||
| #[test] | ||
| fn select_is_none_when_empty() { | ||
| let selector = RelaySelector::new(Vec::new()); | ||
| let mut rng = StdRng::seed_from_u64(4); | ||
| assert_eq!(selector.select(&mut rng), None); | ||
| } | ||
|
|
||
| // Selection is uniform across all relays. | ||
| #[test] | ||
| fn select_is_uniform_across_relays() { | ||
| let selector = RelaySelector::new(relays()); | ||
| let mut seen = std::collections::BTreeSet::new(); | ||
| let mut rng = StdRng::seed_from_u64(5); | ||
| for _ in 0..200 { | ||
| if let Some(r) = selector.select(&mut rng) { | ||
| seen.insert(r.to_string()); | ||
| } | ||
| } | ||
| assert_eq!(seen.len(), relays().len(), "random must reach every relay"); | ||
| } | ||
| } | ||
Oops, something went wrong.
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.