Add async background catches expiry height

This commit is contained in:
Reckless_Satoshi
2022-01-25 07:20:56 -08:00
parent 5a677067f7
commit d2dbc0d249
3 changed files with 13 additions and 8 deletions

View File

@ -9,6 +9,7 @@ from base64 import b64decode
from datetime import timedelta, datetime from datetime import timedelta, datetime
from django.utils import timezone from django.utils import timezone
from api.models import LNPayment
####### #######
# Should work with LND (c-lightning in the future if there are features that deserve the work) # Should work with LND (c-lightning in the future if there are features that deserve the work)
####### #######
@ -117,6 +118,7 @@ class LNNode():
if response.state == 3: # ACCEPTED (LOCKED) if response.state == 3: # ACCEPTED (LOCKED)
print('STATUS: ACCEPTED') print('STATUS: ACCEPTED')
lnpayment.expiry_height = response.htlcs[0].expiry_height lnpayment.expiry_height = response.htlcs[0].expiry_height
lnpayment.status = LNPayment.Status.LOCKED
lnpayment.save() lnpayment.save()
return True return True

View File

@ -479,8 +479,6 @@ class Logics():
if order.maker_bond.status == LNPayment.Status.LOCKED: if order.maker_bond.status == LNPayment.Status.LOCKED:
return True return True
elif LNNode.validate_hold_invoice_locked(order.maker_bond): elif LNNode.validate_hold_invoice_locked(order.maker_bond):
order.maker_bond.status = LNPayment.Status.LOCKED
order.maker_bond.save()
cls.publish_order(order) cls.publish_order(order)
return True return True
return False return False
@ -628,8 +626,6 @@ class Logics():
if order.trade_escrow.status == LNPayment.Status.LOCKED: if order.trade_escrow.status == LNPayment.Status.LOCKED:
return True return True
elif LNNode.validate_hold_invoice_locked(order.trade_escrow): elif LNNode.validate_hold_invoice_locked(order.trade_escrow):
order.trade_escrow.status = LNPayment.Status.LOCKED
order.trade_escrow.save()
cls.trade_escrow_received(order) cls.trade_escrow_received(order)
return True return True
return False return False

View File

@ -65,12 +65,18 @@ class Command(BaseCommand):
for idx, hold_lnpayment in enumerate(queryset): for idx, hold_lnpayment in enumerate(queryset):
old_status = LNPayment.Status(hold_lnpayment.status).label old_status = LNPayment.Status(hold_lnpayment.status).label
try: try:
# this is similar to LNNnode.validate_hold_invoice_locked
request = LNNode.invoicesrpc.LookupInvoiceMsg(payment_hash=bytes.fromhex(hold_lnpayment.payment_hash)) request = LNNode.invoicesrpc.LookupInvoiceMsg(payment_hash=bytes.fromhex(hold_lnpayment.payment_hash))
response = stub.LookupInvoiceV2(request, metadata=[('macaroon', MACAROON.hex())]) response = stub.LookupInvoiceV2(request, metadata=[('macaroon', MACAROON.hex())])
hold_lnpayment.status = lnd_state_to_lnpayment_status[response.state] hold_lnpayment.status = lnd_state_to_lnpayment_status[response.state]
hold_lnpayment.expiry_height = response.htlcs[0].expiry_height
# try saving expiry height
if hasattr(response, 'htlcs' ):
try:
hold_lnpayment.expiry_height = response.htlcs[0].expiry_height
except:
pass
except Exception as e: except Exception as e:
# If it fails at finding the invoice: it has been canceled. # If it fails at finding the invoice: it has been canceled.
@ -78,9 +84,10 @@ class Command(BaseCommand):
if 'unable to locate invoice' in str(e): if 'unable to locate invoice' in str(e):
self.stdout.write(str(e)) self.stdout.write(str(e))
hold_lnpayment.status = LNPayment.Status.CANCEL hold_lnpayment.status = LNPayment.Status.CANCEL
# LND restarted. # LND restarted.
if 'wallet locked, unlock it' in str(e): if 'wallet locked, unlock it' in str(e):
self.stdout.write(str(timezone.now())+':: Wallet Locked') self.stdout.write(str(timezone.now())+' :: Wallet Locked')
# Other write to logs # Other write to logs
else: else:
self.stdout.write(str(e)) self.stdout.write(str(e))