Skip to content

Commit 05ab131

Browse files
committed
pool: use Events::force_insert when fetching events from relays
Replaced `Events::insert` with `Events::force_insert` to ensure events are not discarded when exceeding filter limits, accommodating policies like `WaitForEventsAfterEOSE`. Signed-off-by: Yuki Kishimoto <[email protected]>
1 parent 2363ee6 commit 05ab131

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

crates/nostr-relay-pool/src/pool/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -996,7 +996,8 @@ impl RelayPool {
996996
.stream_events_from(urls, filter, timeout, policy)
997997
.await?;
998998
while let Some(event) = stream.next().await {
999-
events.insert(event);
999+
// To find out more about why the `force_insert` was used, search for EVENTS_FORCE_INSERT ine the code.
1000+
events.force_insert(event);
10001001
}
10011002

10021003
Ok(events)

crates/nostr-relay-pool/src/relay/mod.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,18 @@ impl Relay {
609609
) -> Result<Events, Error> {
610610
let mut events: Events = Events::new(&filter);
611611
self.fetch_events_with_callback(filter, timeout, policy, |event| {
612-
events.insert(event);
612+
// Use force insert here!
613+
// Due to the configurable REQ exit policy, the user may want to wait for events after EOSE.
614+
// If the filter had a limit, the force insert allows adding events post-EOSE.
615+
//
616+
// For example, if we use `Events::insert` here,
617+
// if the filter is '{"kinds":[1],"limit":3}' and the policy `ReqExitPolicy::WaitForEventsAfterEOSE(1)`,
618+
// the events collection will discard 1 event because the filter limit is 3 and the total received events are 4.
619+
//
620+
// Events::force_insert automatically increases the capacity if needed, without discarding events.
621+
//
622+
// LOOKUP_ID: EVENTS_FORCE_INSERT
623+
events.force_insert(event);
613624
})
614625
.await?;
615626
Ok(events)

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1505,7 +1505,8 @@ impl Client {
15051505
self.gossip_stream_events(filter, timeout, policy).await?;
15061506

15071507
while let Some(event) = stream.next().await {
1508-
events.insert(event);
1508+
// To find out more about why the `force_insert` was used, search for EVENTS_FORCE_INSERT ine the code.
1509+
events.force_insert(event);
15091510
}
15101511

15111512
Ok(events)

0 commit comments

Comments
 (0)