Skip to content

Commit 3d09606

Browse files
committed
gossip-memory: add bounded and unbounded constructors to NostrGossipMemory
Signed-off-by: Yuki Kishimoto <[email protected]>
1 parent 5e6a1f6 commit 3d09606

File tree

3 files changed

+20
-19
lines changed

3 files changed

+20
-19
lines changed

crates/nostr-sdk/examples/gossip.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ async fn main() -> Result<()> {
1212
tracing_subscriber::fmt::init();
1313

1414
let keys = Keys::parse("nsec1ufnus6pju578ste3v90xd5m2decpuzpql2295m3sknqcjzyys9ls0qlc85")?;
15-
let gossip = NostrGossipMemory::new();
15+
let gossip = NostrGossipMemory::unbounded();
1616
let client = Client::builder().signer(keys).gossip(gossip).build();
1717

1818
client.add_discovery_relay("wss://relay.damus.io").await?;

crates/nostr-sdk/src/gossip/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ mod tests {
333333
}
334334

335335
async fn setup() -> GossipWrapper {
336-
let db = NostrGossipMemory::new();
336+
let db = NostrGossipMemory::unbounded();
337337

338338
let events = vec![
339339
build_relay_list_event(SECRET_KEY_A, KEY_A_RELAYS.to_vec()),

gossip/nostr-gossip-memory/src/store.rs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use std::cmp::Ordering;
44
use std::collections::HashSet;
5+
use std::num::NonZeroUsize;
56
use std::sync::Arc;
67

78
use indexmap::IndexMap;
@@ -46,21 +47,21 @@ pub struct NostrGossipMemory {
4647
public_keys: Arc<Mutex<LruCache<PublicKey, PkData>>>,
4748
}
4849

49-
impl Default for NostrGossipMemory {
50-
fn default() -> Self {
51-
Self::new()
52-
}
53-
}
54-
5550
impl NostrGossipMemory {
56-
/// Construct a new instance
57-
pub fn new() -> Self {
51+
/// Construct a new **unbounded** instance
52+
pub fn unbounded() -> Self {
5853
Self {
59-
// TODO: allow to make this bounded
6054
public_keys: Arc::new(Mutex::new(LruCache::unbounded())),
6155
}
6256
}
6357

58+
/// Construct a new **bounded** instance
59+
pub fn bounded(limit: NonZeroUsize) -> Self {
60+
Self {
61+
public_keys: Arc::new(Mutex::new(LruCache::new(limit))),
62+
}
63+
}
64+
6465
async fn process_event(&self, event: &Event, relay_url: Option<&RelayUrl>) {
6566
let mut public_keys = self.public_keys.lock().await;
6667

@@ -378,7 +379,7 @@ mod tests {
378379

379380
#[tokio::test]
380381
async fn test_process_event() {
381-
let store = NostrGossipMemory::default();
382+
let store = NostrGossipMemory::unbounded();
382383

383384
let json = r#"{"id":"b7b1fb52ad8461a03e949820ae29a9ea07e35bcd79c95c4b59b0254944f62805","pubkey":"aa4fc8665f5696e33db7e1a572e3b0f5b3d615837b0f362dcb1c8068b098c7b4","created_at":1704644581,"kind":1,"tags":[],"content":"Text note","sig":"ed73a8a4e7c26cd797a7b875c634d9ecb6958c57733305fed23b978109d0411d21b3e182cb67c8ad750884e30ca383b509382ae6187b36e76ee76e6a142c4284"}"#;
384385
let event = Event::from_json(json).unwrap();
@@ -392,7 +393,7 @@ mod tests {
392393

393394
#[tokio::test]
394395
async fn test_process_nip65_relay_list() {
395-
let store = NostrGossipMemory::default();
396+
let store = NostrGossipMemory::unbounded();
396397

397398
// NIP-65 relay list event with read and write relays
398399
let json = r#"{"id":"0a49bed4a1eb0973a68a0d43b7ca62781ffd4e052b91bbadef09e5cf756f6e68","pubkey":"68d81165918100b7da43fc28f7d1fc12554466e1115886b9e7bb326f65ec4272","created_at":1759351841,"kind":10002,"tags":[["alt","Relay list to discover the user's content"],["r","wss://relay.damus.io/"],["r","wss://nostr.wine/"],["r","wss://nostr.oxtr.dev/"],["r","wss://relay.nostr.wirednet.jp/"]],"content":"","sig":"f5bc6c18b0013214588d018c9086358fb76a529aa10867d4d02a75feb239412ae1c94ac7c7917f6e6e2303d72f00dc4e9b03b168ef98f3c3c0dec9a457ce0304"}"#;
@@ -419,7 +420,7 @@ mod tests {
419420

420421
#[tokio::test]
421422
async fn test_process_nip17_inbox_relays() {
422-
let store = NostrGossipMemory::default();
423+
let store = NostrGossipMemory::unbounded();
423424

424425
// NIP-17 inbox relays event
425426
let json = r#"{"id":"8d9b40907f80bd7d5014bdc6a2541227b92f4ae20cbff59792b4746a713da81e","pubkey":"68d81165918100b7da43fc28f7d1fc12554466e1115886b9e7bb326f65ec4272","created_at":1756718818,"kind":10050,"tags":[["relay","wss://auth.nostr1.com/"],["relay","wss://nostr.oxtr.dev/"],["relay","wss://nip17.com"]],"content":"","sig":"05611df32f5c4e55bb8d74ab2840378b7707ad162f785a78f8bdaecee5b872667e4e43bcbbf3c6c638335c637f001155b48b7a7040ce2695660467be62f142d5"}"#;
@@ -439,7 +440,7 @@ mod tests {
439440

440441
#[tokio::test]
441442
async fn test_process_hints_from_p_tags() {
442-
let store = NostrGossipMemory::default();
443+
let store = NostrGossipMemory::unbounded();
443444

444445
let public_key =
445446
PublicKey::parse("npub1drvpzev3syqt0kjrls50050uzf25gehpz9vgdw08hvex7e0vgfeq0eseet")
@@ -471,7 +472,7 @@ mod tests {
471472

472473
#[tokio::test]
473474
async fn test_received_events_tracking() {
474-
let store = NostrGossipMemory::default();
475+
let store = NostrGossipMemory::unbounded();
475476

476477
let keys = Keys::generate();
477478
let relay_url = RelayUrl::parse("wss://test.relay.io").unwrap();
@@ -499,7 +500,7 @@ mod tests {
499500

500501
#[tokio::test]
501502
async fn test_best_relays_all_selection() {
502-
let store = NostrGossipMemory::default();
503+
let store = NostrGossipMemory::unbounded();
503504

504505
let public_key =
505506
PublicKey::from_hex("68d81165918100b7da43fc28f7d1fc12554466e1115886b9e7bb326f65ec4272")
@@ -555,7 +556,7 @@ mod tests {
555556

556557
#[tokio::test]
557558
async fn test_status_tracking() {
558-
let store = NostrGossipMemory::default();
559+
let store = NostrGossipMemory::unbounded();
559560

560561
let public_key =
561562
PublicKey::from_hex("68d81165918100b7da43fc28f7d1fc12554466e1115886b9e7bb326f65ec4272")
@@ -582,7 +583,7 @@ mod tests {
582583

583584
#[tokio::test]
584585
async fn test_empty_results() {
585-
let store = NostrGossipMemory::default();
586+
let store = NostrGossipMemory::unbounded();
586587

587588
// Random public key with no data
588589
let public_key =

0 commit comments

Comments
 (0)