Skip to content

Commit 04627e3

Browse files
committed
Use RelaySelector in payjoin-cli
Share the lib selection policy instead of hand-rolling it in the cli.
1 parent 3b83f2f commit 04627e3

2 files changed

Lines changed: 15 additions & 22 deletions

File tree

payjoin-cli/src/app/v2/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ impl<Status: StatusText> fmt::Display for SessionHistoryRow<Status> {
130130
impl AppTrait for App {
131131
async fn new(config: Config) -> Result<Self> {
132132
let db = Arc::new(Database::create(&config.db_path)?);
133-
let mailroom_manager = MailroomManager::new(config.clone());
133+
let mailroom_manager = MailroomManager::new(config.clone())?;
134134
let (interrupt_tx, interrupt_rx) = watch::channel(());
135135
tokio::spawn(handle_interrupt(interrupt_tx));
136136
let wallet = BitcoindWallet::new(&config.bitcoind).await?;

payjoin-cli/src/app/v2/ohttp.rs

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,53 +14,46 @@
1414
use std::sync::{Arc, Mutex};
1515

1616
use anyhow::{anyhow, Result};
17+
use payjoin::relay::{RelaySelector, SelectContext};
1718
use payjoin::Url;
1819

1920
use super::Config;
2021

2122
#[derive(Debug, Clone)]
2223
pub struct MailroomManager {
2324
config: Config,
24-
failed_relays: Arc<Mutex<Vec<Url>>>,
25+
relay_selector: Arc<Mutex<RelaySelector>>,
2526
failed_directories: Arc<Mutex<Vec<Url>>>,
2627
}
2728

2829
impl MailroomManager {
29-
pub fn new(config: Config) -> Self {
30-
MailroomManager {
30+
pub fn new(config: Config) -> Result<Self> {
31+
let relay_selector = RelaySelector::new(config.v2()?.ohttp_relays.clone());
32+
Ok(MailroomManager {
3133
config,
32-
failed_relays: Arc::new(Mutex::new(Vec::new())),
34+
relay_selector: Arc::new(Mutex::new(relay_selector)),
3335
failed_directories: Arc::new(Mutex::new(Vec::new())),
34-
}
36+
})
3537
}
3638

3739
pub fn add_failed_relay(&self, relay: Url) {
38-
self.failed_relays.lock().expect("Lock should not be poisoned").push(relay);
40+
self.relay_selector.lock().expect("Lock should not be poisoned").mark_failed(&relay);
3941
}
4042

4143
pub fn clear_failed_relays(&self) {
42-
self.failed_relays.lock().expect("Lock should not be poisoned").clear();
44+
self.relay_selector.lock().expect("Lock should not be poisoned").clear_failed();
4345
}
4446

4547
pub fn add_failed_directory(&self, directory: Url) {
4648
self.failed_directories.lock().expect("Lock should not be poisoned").push(directory);
4749
}
4850

4951
pub fn choose_relay(&self) -> Result<Url> {
50-
use payjoin::bitcoin::secp256k1::rand::prelude::SliceRandom;
51-
let relays = &self.config.v2()?.ohttp_relays;
52-
let failed_relays = self.failed_relays.lock().expect("Lock should not be poisoned");
53-
let remaining_relays: Vec<_> =
54-
relays.iter().filter(|r| !failed_relays.contains(r)).cloned().collect();
55-
56-
if remaining_relays.is_empty() {
57-
return Err(anyhow!("No valid relays available"));
58-
}
59-
60-
remaining_relays
61-
.choose(&mut payjoin::bitcoin::key::rand::thread_rng())
62-
.cloned()
63-
.ok_or_else(|| anyhow!("Failed to select from remaining relays"))
52+
self.relay_selector
53+
.lock()
54+
.expect("Lock should not be poisoned")
55+
.select(&SelectContext::random(), &mut payjoin::bitcoin::key::rand::thread_rng())
56+
.ok_or_else(|| anyhow!("No valid relays available"))
6457
}
6558

6659
pub fn choose_directory(&self) -> Result<Url> {

0 commit comments

Comments
 (0)