From b1e9081ff89fa8a50b9d0b6daee165174554f99e Mon Sep 17 00:00:00 2001 From: Reckless_Satoshi Date: Tue, 25 Apr 2023 04:46:07 -0700 Subject: [PATCH] Handle expired invoices that are in_flight --- api/lightning/node.py | 43 +++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/api/lightning/node.py b/api/lightning/node.py index acd97155..142d096f 100644 --- a/api/lightning/node.py +++ b/api/lightning/node.py @@ -551,20 +551,35 @@ class LNNode: if "invoice expired" in str(e): print(f"Order: {order.id}. INVOICE EXPIRED. Hash: {hash}") - lnpayment.status = LNPayment.Status.EXPIRE - lnpayment.last_routing_time = timezone.now() - lnpayment.in_flight = False - lnpayment.save() - order.status = Order.Status.FAI - order.expires_at = timezone.now() + timedelta( - seconds=order.t_to_expire(Order.Status.FAI) - ) - order.save() - results = { - "succeded": False, - "context": "The payout invoice has expired", - } - return results + # An expired invoice can already be in-flight. Check. + try: + request = routerrpc.TrackPaymentRequest( + payment_hash=bytes.fromhex(hash) + ) + + for response in cls.routerstub.TrackPaymentV2(request): + handle_response(response, was_in_transit=True) + + except Exception as e: + if "payment isn't initiated" in str(e): + print( + f"Order: {order.id}. The expired invoice had not been initiated. Hash: {hash}" + ) + + lnpayment.status = LNPayment.Status.EXPIRE + lnpayment.last_routing_time = timezone.now() + lnpayment.in_flight = False + lnpayment.save() + order.status = Order.Status.FAI + order.expires_at = timezone.now() + timedelta( + seconds=order.t_to_expire(Order.Status.FAI) + ) + order.save() + results = { + "succeded": False, + "context": "The payout invoice has expired", + } + return results elif "payment is in transition" in str(e): print(f"Order: {order.id} ALREADY IN TRANSITION. Hash: {hash}.")