Skip to content

Commit a7dfb02

Browse files
committed
Review fixes
1 parent e6103e1 commit a7dfb02

File tree

3 files changed

+26
-18
lines changed

3 files changed

+26
-18
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ pub const VALID_FILE_MIME_TYPES: [&str; 3] = ["image/jpeg", "image/png", "applic
88
// When subscribing events we subtract this from the last received event time
99
pub const NOSTR_EVENT_TIME_SLACK: u64 = 3600 * 24 * 7; // 1 week
1010
pub const DEFAULT_INITIAL_SUBSCRIPTION_DELAY_SECONDS: u32 = 1;
11+
pub const NOSTR_MAX_RELAYS: usize = 200;

crates/bcr-ebill-persistence/src/db/nostr_contact_store.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use bcr_ebill_core::{
1919
protocol::SecretKey,
2020
protocol::Timestamp,
2121
};
22+
use log::error;
2223
use serde::{Deserialize, Serialize};
2324
use surrealdb::sql::Thing;
2425

@@ -70,9 +71,15 @@ impl NostrContactStoreApi for SurrealNostrContactStore {
7071
let result: Vec<NostrContactDb> = self.db.select_all(Self::TABLE).await?;
7172
let values = result
7273
.into_iter()
73-
.map(|c| c.to_owned().try_into().ok())
74-
.collect::<Option<Vec<NostrContact>>>();
75-
Ok(values.unwrap_or_default())
74+
.filter_map(|c| match c.try_into() {
75+
Ok(v) => Some(v),
76+
Err(e) => {
77+
error!("Failed to convert NostrContactDb to NostrContact: {e}");
78+
None
79+
}
80+
})
81+
.collect::<Vec<NostrContact>>();
82+
Ok(values)
7683
}
7784

7885
/// Find a Nostr contact by the npub. This is the public Nostr key of the contact.

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ use crate::{
99
use async_trait::async_trait;
1010
use bcr_common::core::NodeId;
1111
use bcr_ebill_core::{
12-
protocol::Timestamp, protocol::blockchain::BlockchainType,
13-
protocol::blockchain::bill::participant::BillParticipant, protocol::crypto::BcrKeys,
12+
application::nostr_contact::{NostrContact, TrustLevel},
13+
protocol::{
14+
Timestamp,
15+
blockchain::{BlockchainType, bill::participant::BillParticipant},
16+
crypto::BcrKeys,
17+
},
1418
};
1519
use bitcoin::base58;
1620
use log::{debug, error, info, trace, warn};
@@ -27,7 +31,7 @@ use std::{
2731
};
2832

2933
use bcr_ebill_api::{
30-
constants::NOSTR_EVENT_TIME_SLACK,
34+
constants::{NOSTR_EVENT_TIME_SLACK, NOSTR_MAX_RELAYS},
3135
service::{
3236
contact_service::ContactServiceApi,
3337
transport_service::{
@@ -358,8 +362,8 @@ impl NostrClient {
358362
let current_relays: HashSet<url::Url> = client
359363
.relays()
360364
.await
361-
.into_iter()
362-
.filter_map(|(_, relay)| relay.url().as_str().parse::<url::Url>().ok())
365+
.keys()
366+
.map(|url| url.to_owned().into())
363367
.collect();
364368

365369
// Add new relays
@@ -1353,12 +1357,9 @@ mod tests {
13531357
/// Internal relay calculation function (pure function for testing)
13541358
fn calculate_relay_set_internal(
13551359
user_relays: &[url::Url],
1356-
contacts: &[bcr_ebill_core::application::nostr_contact::NostrContact],
1360+
contacts: &[NostrContact],
13571361
max_relays: Option<usize>,
13581362
) -> HashSet<url::Url> {
1359-
use bcr_ebill_core::application::nostr_contact::TrustLevel;
1360-
use std::collections::HashSet;
1361-
13621363
let mut relay_set = HashSet::new();
13631364

13641365
// Pass 1: Add all user relays (exempt from limit)
@@ -1367,11 +1368,10 @@ fn calculate_relay_set_internal(
13671368
}
13681369

13691370
// Filter and sort contacts by trust level
1370-
let mut eligible_contacts: Vec<&bcr_ebill_core::application::nostr_contact::NostrContact> =
1371-
contacts
1372-
.iter()
1373-
.filter(|c| matches!(c.trust_level, TrustLevel::Trusted | TrustLevel::Participant))
1374-
.collect();
1371+
let mut eligible_contacts: Vec<&NostrContact> = contacts
1372+
.iter()
1373+
.filter(|c| matches!(c.trust_level, TrustLevel::Trusted | TrustLevel::Participant))
1374+
.collect();
13751375

13761376
// Sort: Trusted (0) before Participant (1)
13771377
eligible_contacts.sort_by_key(|c| match c.trust_level {
@@ -1380,7 +1380,7 @@ fn calculate_relay_set_internal(
13801380
_ => 2, // unreachable due to filter
13811381
});
13821382

1383-
let contact_relay_limit = max_relays.unwrap_or(usize::MAX);
1383+
let contact_relay_limit = NOSTR_MAX_RELAYS.min(max_relays.unwrap_or(NOSTR_MAX_RELAYS));
13841384
let user_relay_count = relay_set.len();
13851385

13861386
// Pass 2: Add first relay from each contact (priority order)

0 commit comments

Comments
 (0)