Skip to content

Commit 71ae683

Browse files
committed
sdk: track last_check timestamps separately for NIP-17 and NIP-65 relay lists
Closes https://gitworkshop.dev/yukikishimoto.com/nostr/issues/note15mhvsqdntu8w3njkh7urf92jlvak5rxdq7z9snfjtf2678kd9qvsgut0hz Signed-off-by: Yuki Kishimoto <[email protected]>
1 parent 77089c0 commit 71ae683

File tree

2 files changed

+36
-22
lines changed

2 files changed

+36
-22
lines changed

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,21 +1326,21 @@ impl Client {
13261326
async fn check_and_update_gossip<I>(
13271327
&self,
13281328
public_keys: I,
1329-
kind: GossipKind,
1329+
gossip_kind: GossipKind,
13301330
) -> Result<(), Error>
13311331
where
13321332
I: IntoIterator<Item = PublicKey>,
13331333
{
13341334
let outdated_public_keys: HashSet<PublicKey> =
1335-
self.gossip.check_outdated(public_keys, &kind).await;
1335+
self.gossip.check_outdated(public_keys, &gossip_kind).await;
13361336

13371337
// No outdated public keys, immediately return.
13381338
if outdated_public_keys.is_empty() {
13391339
return Ok(());
13401340
}
13411341

13421342
// Get kind
1343-
let kind: Kind = kind.to_event_kind();
1343+
let kind: Kind = gossip_kind.to_event_kind();
13441344

13451345
// Compose database filter
13461346
let db_filter: Filter = Filter::default()
@@ -1402,7 +1402,9 @@ impl Client {
14021402
missing_public_keys.remove(&event.pubkey);
14031403

14041404
// Update the last check for this public key
1405-
self.gossip.update_last_check([event.pubkey]).await;
1405+
self.gossip
1406+
.update_last_check([event.pubkey], &gossip_kind)
1407+
.await;
14061408
}
14071409

14081410
updated_events = updated_events.merge(events);
@@ -1418,7 +1420,9 @@ impl Client {
14181420
.await?;
14191421

14201422
// Update the last check for the missing public keys
1421-
self.gossip.update_last_check(missing_public_keys).await;
1423+
self.gossip
1424+
.update_last_check(missing_public_keys, &gossip_kind)
1425+
.await;
14221426

14231427
// Merge all the events
14241428
let merged: Events = stored_events.merge(updated_events).merge(missing_events);

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

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ pub(crate) enum GossipKind {
2424
}
2525

2626
impl GossipKind {
27-
#[allow(clippy::wrong_self_convention)]
28-
pub(crate) fn to_event_kind(self) -> Kind {
27+
pub(crate) fn to_event_kind(&self) -> Kind {
2928
match self {
3029
Self::Nip17 => Kind::InboxRelays,
3130
Self::Nip65 => Kind::RelayList,
@@ -48,6 +47,8 @@ struct RelayList<T> {
4847
pub collection: T,
4948
/// Timestamp of when the event metadata was created
5049
pub event_created_at: Timestamp,
50+
/// Timestamp of the last check
51+
pub last_check: Option<Timestamp>,
5152
/// Timestamp of when the metadata was updated
5253
pub last_update: Timestamp,
5354
}
@@ -56,8 +57,6 @@ struct RelayList<T> {
5657
struct RelayLists {
5758
pub nip17: RelayList<HashSet<RelayUrl>>,
5859
pub nip65: RelayList<HashMap<RelayUrl, Option<RelayMetadata>>>,
59-
/// Timestamp of the last check
60-
pub last_check: Timestamp,
6160
}
6261

6362
type PublicKeyMap = HashMap<PublicKey, RelayLists>;
@@ -117,6 +116,7 @@ impl Gossip {
117116
MAX_RELAYS_PER_NIP65_MARKER,
118117
),
119118
event_created_at: event.created_at,
119+
last_check: None,
120120
last_update: Timestamp::now(),
121121
};
122122
}
@@ -125,6 +125,7 @@ impl Gossip {
125125
nip65: RelayList {
126126
collection: extract_nip65_relay_list(event, MAX_RELAYS_PER_NIP65_MARKER),
127127
event_created_at: event.created_at,
128+
last_check: None,
128129
last_update: Timestamp::now(),
129130
},
130131
..Default::default()
@@ -141,6 +142,7 @@ impl Gossip {
141142
.cloned()
142143
.collect(),
143144
event_created_at: event.created_at,
145+
last_check: None,
144146
last_update: Timestamp::now(),
145147
};
146148
}
@@ -152,6 +154,7 @@ impl Gossip {
152154
.cloned()
153155
.collect(),
154156
event_created_at: event.created_at,
157+
last_check: None,
155158
last_update: Timestamp::now(),
156159
},
157160
..Default::default()
@@ -172,11 +175,7 @@ impl Gossip {
172175
for public_key in public_keys.into_iter() {
173176
match map.get(&public_key) {
174177
Some(lists) => {
175-
if lists.last_check + CHECK_OUTDATED_INTERVAL > now {
176-
continue;
177-
}
178-
179-
let (empty, expired) = match kind {
178+
let (last_check, empty, expired) = match kind {
180179
GossipKind::Nip17 => {
181180
// Check if the collection is empty
182181
let empty: bool = lists.nip17.collection.is_empty();
@@ -185,7 +184,7 @@ impl Gossip {
185184
let expired: bool =
186185
lists.nip17.last_update + PUBKEY_METADATA_OUTDATED_AFTER < now;
187186

188-
(empty, expired)
187+
(lists.nip17.last_check.unwrap_or_default(), empty, expired)
189188
}
190189
GossipKind::Nip65 => {
191190
// Check if the collection is empty
@@ -195,10 +194,14 @@ impl Gossip {
195194
let expired: bool =
196195
lists.nip65.last_update + PUBKEY_METADATA_OUTDATED_AFTER < now;
197196

198-
(empty, expired)
197+
(lists.nip65.last_check.unwrap_or_default(), empty, expired)
199198
}
200199
};
201200

201+
if last_check + CHECK_OUTDATED_INTERVAL > now {
202+
continue;
203+
}
204+
202205
if empty || expired {
203206
outdated.insert(public_key);
204207
}
@@ -213,7 +216,7 @@ impl Gossip {
213216
outdated
214217
}
215218

216-
pub async fn update_last_check<I>(&self, public_keys: I)
219+
pub async fn update_last_check<I>(&self, public_keys: I, kind: &GossipKind)
217220
where
218221
I: IntoIterator<Item = PublicKey>,
219222
{
@@ -222,12 +225,19 @@ impl Gossip {
222225

223226
for public_key in public_keys.into_iter() {
224227
map.entry(public_key)
225-
.and_modify(|lists| {
226-
lists.last_check = now;
228+
.and_modify(|lists| match kind {
229+
GossipKind::Nip17 => lists.nip17.last_check = Some(now),
230+
GossipKind::Nip65 => lists.nip65.last_check = Some(now),
227231
})
228-
.or_insert_with(|| RelayLists {
229-
last_check: now,
230-
..Default::default()
232+
.or_insert_with(|| {
233+
let mut lists = RelayLists::default();
234+
235+
match kind {
236+
GossipKind::Nip17 => lists.nip17.last_check = Some(now),
237+
GossipKind::Nip65 => lists.nip65.last_check = Some(now),
238+
}
239+
240+
lists
231241
});
232242
}
233243
}

0 commit comments

Comments
 (0)