Skip to content
This repository was archived by the owner on Feb 3, 2025. It is now read-only.

Commit 542a212

Browse files
Merge pull request #1203 from MutinyWallet/batched-hermes
Claim zaps in batches
2 parents 12d3a05 + 94ef7e8 commit 542a212

File tree

1 file changed

+46
-9
lines changed

1 file changed

+46
-9
lines changed

mutiny-core/src/hermes.rs

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ use fedimint_core::api::InviteCode;
1515
use fedimint_core::config::FederationId;
1616
use futures::{pin_mut, select, FutureExt};
1717
use lightning::util::logger::Logger;
18-
use lightning::{log_error, log_info, log_warn};
18+
use lightning::{log_debug, log_error, log_info, log_warn};
1919
use lightning_invoice::Bolt11Invoice;
2020
use nostr::secp256k1::SecretKey;
21-
use nostr::{nips::nip04::decrypt, Event, JsonUtil, Keys, Tag, ToBech32};
21+
use nostr::{nips::nip04::decrypt, Event, JsonUtil, Keys, RelayMessage, Tag, ToBech32};
2222
use nostr::{prelude::decrypt_received_private_zap_message, EventBuilder};
2323
use nostr::{Filter, Kind, Timestamp};
2424
use nostr_sdk::{Client, RelayPoolNotification};
@@ -102,7 +102,12 @@ impl<S: MutinyStorage> HermesClient<S> {
102102
let client = Client::new(&keys);
103103

104104
client
105-
.add_relays(RELAYS)
105+
.add_relays(
106+
// we are listening only, so no need to connect to blastr
107+
RELAYS
108+
.into_iter()
109+
.filter(|r| *r != "wss://nostr.mutinywallet.com"),
110+
)
106111
.await
107112
.expect("Failed to add relays");
108113

@@ -267,6 +272,9 @@ impl<S: MutinyStorage> HermesClient<S> {
267272

268273
client.subscribe(vec![received_dm_filter], None).await;
269274

275+
let mut has_received_eose = false;
276+
let mut batch_notifications: Vec<(EcashNotification, Timestamp)> = Vec::new();
277+
270278
let mut notifications = client.notifications();
271279

272280
loop {
@@ -283,8 +291,17 @@ impl<S: MutinyStorage> HermesClient<S> {
283291
Kind::EncryptedDirectMessage => {
284292
match decrypt_ecash_notification(&dm_key, event.pubkey, &event.content) {
285293
Ok(notification) => {
286-
if let Err(e) = handle_ecash_notification(notification, event.created_at, &federations, &storage, &claim_key, profile_key.as_ref(), &logger).await {
287-
log_error!(logger, "Error handling ecash notification: {e}");
294+
// if we have received an EOSE, we should immediately handle the notification
295+
// otherwise we should wait until we have received an EOSE so we can do the initial batch
296+
if has_received_eose {
297+
if let Err(e) = handle_ecash_notification(notification, event.created_at, &federations, &storage, &claim_key, profile_key.as_ref(), &logger).await {
298+
log_error!(logger, "Error handling ecash notification: {e}");
299+
} else if let Err(e) = storage.set_dm_sync_time(event.created_at.as_u64(), true) { // save the last sync time after processing the notification
300+
log_error!(logger, "Error saving last sync time: {e}");
301+
}
302+
} else {
303+
log_debug!(logger, "Received ecash notification, adding to batch");
304+
batch_notifications.push((notification, event.created_at));
288305
}
289306
},
290307
Err(e) => {
@@ -296,7 +313,30 @@ impl<S: MutinyStorage> HermesClient<S> {
296313
}
297314
}
298315
},
299-
Ok(RelayPoolNotification::Message { .. }) => {}, // ignore messages
316+
Ok(RelayPoolNotification::Message { message, .. }) => {
317+
// if we receive an EOSE, we have received all the notifications from the relay
318+
// and can now handle the batch
319+
if let RelayMessage::EndOfStoredEvents(_) = message {
320+
has_received_eose = true;
321+
if !batch_notifications.is_empty() {
322+
let mut max_created_at: Option<Timestamp> = None;
323+
for (notification, created_at) in batch_notifications.drain(..) {
324+
if let Err(e) = handle_ecash_notification(notification, created_at, &federations, &storage, &claim_key, profile_key.as_ref(), &logger).await {
325+
log_error!(logger, "Error handling ecash notification: {e}");
326+
} else if max_created_at.is_none() || max_created_at.is_some_and(|x| x < created_at) {
327+
max_created_at = Some(created_at);
328+
}
329+
}
330+
331+
// save the last sync time after the batch
332+
if let Some(max_created_at) = max_created_at {
333+
if let Err(e) = storage.set_dm_sync_time(max_created_at.as_u64(), true) {
334+
log_error!(logger, "Error saving last sync time: {e}");
335+
}
336+
}
337+
}
338+
}
339+
},
300340
Ok(RelayPoolNotification::Shutdown) => break, // if we disconnect, we restart to reconnect
301341
Ok(RelayPoolNotification::Stop) => {}, // Currently unused
302342
Ok(RelayPoolNotification::RelayStatus { .. }) => {}, // Currently unused
@@ -731,9 +771,6 @@ async fn handle_ecash_notification<S: MutinyStorage>(
731771
);
732772
}
733773

734-
// save the last sync time
735-
storage.set_dm_sync_time(created_at.as_u64(), true)?;
736-
737774
Ok(())
738775
}
739776

0 commit comments

Comments
 (0)