@@ -2010,7 +2010,7 @@ async def pay_invoice(
20102010 util .trigger_callback ('payment_succeeded' , self .wallet , key )
20112011 elif self .has_unresolved_sent_htlcs (payment_hash ):
20122012 # The invoice stays PR_INFLIGHT until the htlcs resolve.
2013- self .logger .info (f "pay_invoice: htlcs are still unresolved. " )
2013+ self .logger .info ("pay_invoice: htlcs are still unresolved." )
20142014 else :
20152015 self .set_invoice_status (key , PR_UNPAID ) # allows retries
20162016 util .trigger_callback ('payment_failed' , self .wallet , key , reason )
@@ -3218,6 +3218,27 @@ def notify_upstream_peer(self, htlc_key: str) -> None:
32183218 upstream_peer .downstream_htlc_resolved_event .set ()
32193219 upstream_peer .downstream_htlc_resolved_event .clear ()
32203220
3221+ def _set_sent_payment_succeeded (self , payment_hash : bytes ) -> None :
3222+ key = payment_hash .hex ()
3223+ info = self .get_payment_info (payment_hash , direction = SENT )
3224+ if info is not None and info .status != PR_PAID :
3225+ self .set_invoice_status (key , PR_PAID )
3226+ util .trigger_callback ('payment_succeeded' , self .wallet , key )
3227+
3228+ def _set_sent_payment_failed (self , payment_hash : bytes ) -> None :
3229+ key = payment_hash .hex ()
3230+ if self .has_unresolved_sent_htlcs (payment_hash ):
3231+ return
3232+ if self .get_preimage (payment_hash ) and self .wallet .get_request (key ) is None :
3233+ # if we know the preimage don't consider the payment failed (unless we pay ourselves).
3234+ # maybe another htlc of the same mpp got fulfilled, or we saw a htlc-success tx in the mempool
3235+ # before claiming a revoked htlc with a justice tx which ultimately would make LNWatcher try to fail the htlc here
3236+ return
3237+ info = self .get_payment_info (payment_hash , direction = SENT )
3238+ if info is not None and (info .status != PR_UNPAID or key in self .inflight_payments ):
3239+ self .set_invoice_status (key , PR_UNPAID )
3240+ util .trigger_callback ('payment_failed' , self .wallet , key , '' )
3241+
32213242 def htlc_fulfilled (self , chan : Channel , payment_hash : bytes , htlc_id : int ):
32223243 """Called when an HTLC *WE proposed* becomes irrevocably fulfilled."""
32233244 # note: this may be called several times for the same htlc
@@ -3249,15 +3270,13 @@ def htlc_fulfilled(self, chan: Channel, payment_hash: bytes, htlc_id: int):
32493270 paysession_active = False
32503271 else :
32513272 paysession_active = True
3273+ if not fw_key and not paysession .is_active :
3274+ self ._set_sent_payment_succeeded (payment_hash )
32523275 else :
32533276 if fw_key :
32543277 paysession_active = False
32553278 else :
3256- key = payment_hash .hex ()
3257- info = self .get_payment_info (payment_hash , direction = SENT )
3258- if info is not None and info .status != PR_PAID :
3259- self .set_invoice_status (key , PR_PAID )
3260- util .trigger_callback ('payment_succeeded' , self .wallet , key )
3279+ self ._set_sent_payment_succeeded (payment_hash )
32613280
32623281 if fw_key :
32633282 fw_htlcs = self .active_forwardings [fw_key ]
@@ -3328,20 +3347,14 @@ def htlc_failed(
33283347 paysession_active = False
33293348 else :
33303349 paysession_active = True
3350+ if not fw_key and not paysession .is_active :
3351+ self ._set_sent_payment_failed (payment_hash )
33313352 else :
33323353 if fw_key :
33333354 paysession_active = False
33343355 else :
33353356 self .logger .info (f"received unknown htlc_failed, probably from previous session (phash={ payment_hash .hex ()} )" )
3336- key = payment_hash .hex ()
3337- invoice = self .wallet .get_invoice (key )
3338- if invoice and self .get_invoice_status (invoice ) != PR_UNPAID \
3339- and (self .get_preimage (payment_hash ) is None or self .wallet .get_request (key ) is not None ):
3340- # if we know the preimage don't consider the payment failed (unless we pay ourselves).
3341- # maybe another htlc of the same mpp got fulfilled, or we saw a htlc-success tx in the mempool
3342- # before claiming a revoked htlc with a justice tx which ultimately would make LNWatcher try to fail the htlc here
3343- self .set_invoice_status (key , PR_UNPAID )
3344- util .trigger_callback ('payment_failed' , self .wallet , key , '' )
3357+ self ._set_sent_payment_failed (payment_hash )
33453358
33463359 if fw_key :
33473360 fw_htlcs = self .active_forwardings [fw_key ]
0 commit comments