Refactor expiry_height into follow_invoices

This commit is contained in:
Reckless_Satoshi 2023-05-13 08:51:22 -07:00
parent 70b6d88321
commit b0ffe150c0
No known key found for this signature in database
GPG Key ID: 9C4585B561315571
2 changed files with 10 additions and 7 deletions

View File

@ -265,6 +265,7 @@ class LNNode:
from api.models import LNPayment
status = lnpayment.status
expiry_height = 0
lnd_response_state_to_lnpayment_status = {
0: LNPayment.Status.INVGEN, # OPEN
@ -280,15 +281,16 @@ class LNNode:
)
response = cls.invoicesstub.LookupInvoiceV2(request)
# try saving expiry height
status = lnd_response_state_to_lnpayment_status[response.state]
# get expiry height
if hasattr(response, "htlcs"):
try:
lnpayment.expiry_height = response.htlcs[0].expiry_height
for htlc in response.htlcs:
expiry_height = max(expiry_height, htlc.expiry_height)
except Exception:
pass
status = lnd_response_state_to_lnpayment_status[response.state]
except Exception as e:
# If it fails at finding the invoice: it has been canceled.
# In RoboSats DB we make a distinction between cancelled and returned (LND does not)
@ -304,7 +306,7 @@ class LNNode:
else:
print(str(e))
return status
return status, expiry_height
@classmethod
def resetmc(cls):

View File

@ -77,7 +77,7 @@ class Command(BaseCommand):
for idx, hold_lnpayment in enumerate(queryset):
old_status = hold_lnpayment.status
new_status = LNNode.lookup_invoice_status(hold_lnpayment)
new_status, expiry_height = LNNode.lookup_invoice_status(hold_lnpayment)
# Only save the hold_payments that change (otherwise this function does not scale)
changed = not old_status == new_status
@ -94,8 +94,9 @@ class Command(BaseCommand):
# if these are still different, we update the lnpayment with its new status.
lnpayment.status = new_status
lnpayment.expiry_height = expiry_height
self.update_order_status(lnpayment)
lnpayment.save(update_fields=["status"])
lnpayment.save(update_fields=["status", "expiry_height"])
# Report for debugging
old = LNPayment.Status(old_status).label