@@ -15,10 +15,10 @@ use fedimint_core::api::InviteCode;
1515use fedimint_core:: config:: FederationId ;
1616use futures:: { pin_mut, select, FutureExt } ;
1717use lightning:: util:: logger:: Logger ;
18- use lightning:: { log_error, log_info, log_warn} ;
18+ use lightning:: { log_debug , log_error, log_info, log_warn} ;
1919use lightning_invoice:: Bolt11Invoice ;
2020use 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 } ;
2222use nostr:: { prelude:: decrypt_received_private_zap_message, EventBuilder } ;
2323use nostr:: { Filter , Kind , Timestamp } ;
2424use 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