Skip to content

Commit 876840f

Browse files
committed
Review fixes
1 parent 3a28225 commit 876840f

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

crates/bcr-ebill-api/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ pub struct NostrConfig {
9090
pub only_known_contacts: bool,
9191
/// All relays we want to publish our messages to and receive messages from.
9292
pub relays: Vec<url::Url>,
93-
/// Maximum number of contact relays to connect to (user relays are exempt).
93+
/// Maximum number of contact relays to add (in addition to user relays which are always included).
9494
/// Defaults to 50 if not specified.
9595
pub max_relays: Option<usize>,
9696
}

crates/bcr-ebill-transport/src/nostr.rs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,11 +1380,13 @@ fn calculate_relay_set_internal(
13801380
_ => 2, // unreachable due to filter
13811381
});
13821382

1383-
let limit = max_relays.unwrap_or(usize::MAX);
1383+
let contact_relay_limit = max_relays.unwrap_or(usize::MAX);
1384+
let user_relay_count = relay_set.len();
13841385

13851386
// Pass 2: Add first relay from each contact (priority order)
13861387
for contact in &eligible_contacts {
1387-
if relay_set.len() >= limit {
1388+
let contact_relays_added = relay_set.len() - user_relay_count;
1389+
if contact_relays_added >= contact_relay_limit {
13881390
break;
13891391
}
13901392
if let Some(first_relay) = contact.relays.first() {
@@ -1395,7 +1397,8 @@ fn calculate_relay_set_internal(
13951397
// Pass 3: Fill remaining slots with additional contact relays
13961398
for contact in &eligible_contacts {
13971399
for relay in contact.relays.iter().skip(1) {
1398-
if relay_set.len() >= limit {
1400+
let contact_relays_added = relay_set.len() - user_relay_count;
1401+
if contact_relays_added >= contact_relay_limit {
13991402
return relay_set;
14001403
}
14011404
relay_set.insert(relay.clone());
@@ -1458,6 +1461,30 @@ mod relay_calculation_tests {
14581461
assert!(result.contains(&url::Url::parse("wss://trusted.com").unwrap()));
14591462
}
14601463

1464+
#[test]
1465+
fn test_contact_relays_added_when_user_relays_exceed_limit() {
1466+
let user_relays = vec![
1467+
url::Url::parse("wss://user1.com").unwrap(),
1468+
url::Url::parse("wss://user2.com").unwrap(),
1469+
url::Url::parse("wss://user3.com").unwrap(),
1470+
];
1471+
let contacts = vec![
1472+
create_test_contact(TrustLevel::Trusted, vec!["wss://contact1.com"]),
1473+
create_test_contact(TrustLevel::Trusted, vec!["wss://contact2.com"]),
1474+
];
1475+
let max_relays = Some(2); // Lower than user relay count
1476+
1477+
let result = calculate_relay_set_internal(&user_relays, &contacts, max_relays);
1478+
1479+
// Should have all 3 user relays + 2 contact relays (user relays exempt from limit)
1480+
assert_eq!(result.len(), 5);
1481+
assert!(result.contains(&url::Url::parse("wss://user1.com").unwrap()));
1482+
assert!(result.contains(&url::Url::parse("wss://user2.com").unwrap()));
1483+
assert!(result.contains(&url::Url::parse("wss://user3.com").unwrap()));
1484+
assert!(result.contains(&url::Url::parse("wss://contact1.com").unwrap()));
1485+
assert!(result.contains(&url::Url::parse("wss://contact2.com").unwrap()));
1486+
}
1487+
14611488
#[test]
14621489
fn test_one_relay_per_contact_guaranteed() {
14631490
let user_relays = vec![];

crates/bcr-ebill-wasm/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ pub struct Config {
3737
pub esplora_base_url: String,
3838
pub nostr_relays: Vec<String>,
3939
pub nostr_only_known_contacts: Option<bool>,
40+
pub nostr_max_relays: Option<usize>,
4041
pub job_runner_initial_delay_seconds: u32,
4142
pub job_runner_check_interval_seconds: u32,
4243
pub transport_initial_subscription_delay_seconds: Option<u32>,
@@ -131,7 +132,7 @@ pub async fn initialize_api(
131132
nostr_config: NostrConfig {
132133
relays: nostr_relays,
133134
only_known_contacts: config.nostr_only_known_contacts.unwrap_or(false),
134-
max_relays: Some(50),
135+
max_relays: config.nostr_max_relays.or(Some(50)),
135136
},
136137
mint_config: MintConfig::new(config.default_mint_url, mint_node_id)?,
137138
payment_config: PaymentConfig {

0 commit comments

Comments
 (0)