@@ -499,18 +499,17 @@ fn main() {
499499 payment_id, payment_hash, amount_msat
500500 ) ;
501501
502- let proto_custom_records: Vec <_> = custom_records
503- . iter( )
504- . map( node_to_proto_custom_tlv)
505- . collect( ) ;
506-
507502 send_payment_event(
508503 & payment_id,
509- move |payment_ref| {
504+ move |payment| {
505+ let custom_records = custom_records
506+ . iter( )
507+ . map( node_to_proto_custom_tlv)
508+ . collect( ) ;
510509 event_envelope:: Event :: PaymentReceived ( events:: PaymentReceived {
511510 payment_id: payment_id. to_string( ) ,
512- payment: Some ( payment_ref . clone ( ) ) ,
513- custom_records: proto_custom_records ,
511+ payment: Some ( payment ) ,
512+ custom_records,
514513 } )
515514 } ,
516515 & event_node,
@@ -522,17 +521,19 @@ fn main() {
522521 }
523522 } ,
524523 Event :: PaymentSuccessful { payment_id, payment_preimage, bolt12_invoice, .. } => {
525- let payment_preimage = payment_preimage. map( |p| p. to_string( ) ) ;
526- let bolt12_invoice = bolt12_invoice. as_ref( ) . and_then( |invoice| {
527- invoice. bolt12_invoice( ) . map( |i| i. encode( ) . to_lower_hex_string( ) )
528- } ) ;
529524 send_payment_event( & payment_id,
530- |payment_ref| event_envelope:: Event :: PaymentSuccessful ( events:: PaymentSuccessful {
531- payment_id: payment_id. to_string( ) ,
532- payment: Some ( payment_ref. clone( ) ) ,
533- payment_preimage,
534- bolt12_invoice,
535- } ) ,
525+ move |payment| {
526+ let payment_preimage = payment_preimage. map( |p| p. to_string( ) ) ;
527+ let bolt12_invoice = bolt12_invoice. as_ref( ) . and_then( |invoice| {
528+ invoice. bolt12_invoice( ) . map( |i| i. encode( ) . to_lower_hex_string( ) )
529+ } ) ;
530+ event_envelope:: Event :: PaymentSuccessful ( events:: PaymentSuccessful {
531+ payment_id: payment_id. to_string( ) ,
532+ payment: Some ( payment) ,
533+ payment_preimage,
534+ bolt12_invoice,
535+ } )
536+ } ,
536537 & event_node,
537538 & event_sender) ;
538539
@@ -544,9 +545,9 @@ fn main() {
544545 Event :: PaymentFailed { payment_id, reason, ..} => {
545546 let proto_reason = reason. as_ref( ) . map( payment_failure_reason_to_proto) ;
546547 send_payment_event( & payment_id,
547- move |payment_ref | event_envelope:: Event :: PaymentFailed ( events:: PaymentFailed {
548+ move |payment | event_envelope:: Event :: PaymentFailed ( events:: PaymentFailed {
548549 payment_id: payment_id. to_string( ) ,
549- payment: Some ( payment_ref . clone ( ) ) ,
550+ payment: Some ( payment ) ,
550551 reason: proto_reason. map( |r| r as i32 ) ,
551552 } ) ,
552553 & event_node,
@@ -559,10 +560,10 @@ fn main() {
559560 Event :: PaymentClaimable { payment_id, custom_records, claim_deadline, .. } => {
560561 send_payment_event(
561562 & payment_id,
562- |payment_ref | {
563+ |payment | {
563564 event_envelope:: Event :: PaymentClaimable (
564565 build_payment_claimable_proto(
565- payment_ref ,
566+ payment ,
566567 & custom_records,
567568 claim_deadline,
568569 payment_id. to_string( ) ,
@@ -705,14 +706,22 @@ fn main() {
705706}
706707
707708fn send_payment_event (
708- payment_id : & PaymentId , payment_to_event : impl FnOnce ( & Payment ) -> event_envelope:: Event ,
709+ payment_id : & PaymentId , payment_to_event : impl FnOnce ( Payment ) -> event_envelope:: Event ,
709710 event_node : & Node , event_sender : & broadcast:: Sender < EventEnvelope > ,
710711) {
712+ if event_sender. receiver_count ( ) == 0 {
713+ debug ! ( "No event subscribers connected, skipping payment event" ) ;
714+ if let Err ( e) = event_node. event_handled ( ) {
715+ error ! ( "Failed to mark event as handled: {e}" ) ;
716+ }
717+ return ;
718+ }
719+
711720 match event_node. payment ( payment_id) {
712721 Ok ( Some ( payment_details) ) => {
713722 let payment = payment_to_proto ( payment_details) ;
714723
715- let event = payment_to_event ( & payment) ;
724+ let event = payment_to_event ( payment) ;
716725 if let Err ( e) = event_sender. send ( EventEnvelope { event : Some ( event) } ) {
717726 debug ! ( "No event subscribers connected, skipping event: {e}" ) ;
718727 }
@@ -925,14 +934,14 @@ fn load_or_generate_api_key(storage_dir: &Path) -> std::io::Result<String> {
925934}
926935
927936fn build_payment_claimable_proto (
928- payment_ref : & Payment , custom_records : & [ CustomTlvRecord ] , claim_deadline : Option < u32 > ,
937+ payment : Payment , custom_records : & [ CustomTlvRecord ] , claim_deadline : Option < u32 > ,
929938 payment_id : String ,
930939) -> events:: PaymentClaimable {
931940 let proto_custom_records: Vec < _ > =
932941 custom_records. iter ( ) . map ( node_to_proto_custom_tlv) . collect ( ) ;
933942 events:: PaymentClaimable {
934943 payment_id,
935- payment : Some ( payment_ref . clone ( ) ) ,
944+ payment : Some ( payment ) ,
936945 custom_records : proto_custom_records,
937946 claim_deadline,
938947 }
@@ -1046,7 +1055,7 @@ mod tests {
10461055 CustomTlvRecord { type_num: 65537 , value: vec![ 1 , 2 , 3 ] } ,
10471056 CustomTlvRecord { type_num: 65538 , value: Vec :: new( ) } ,
10481057 ] ;
1049- let proto = build_payment_claimable_proto ( & payment, & records, None , "abc123" . to_string ( ) ) ;
1058+ let proto = build_payment_claimable_proto ( payment, & records, None , "abc123" . to_string ( ) ) ;
10501059 assert_eq ! ( proto. payment_id, "abc123" ) ;
10511060 assert_eq ! ( proto. custom_records. len( ) , 2 ) ;
10521061 assert_eq ! ( proto. custom_records[ 0 ] . type_num, 65537 ) ;
0 commit comments