diff --git a/api/lightning/node.py b/api/lightning/node.py index f24e17ff..826764fb 100644 --- a/api/lightning/node.py +++ b/api/lightning/node.py @@ -9,6 +9,7 @@ from base64 import b64decode from datetime import timedelta, datetime 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) ####### @@ -117,6 +118,7 @@ class LNNode(): if response.state == 3: # ACCEPTED (LOCKED) print('STATUS: ACCEPTED') lnpayment.expiry_height = response.htlcs[0].expiry_height + lnpayment.status = LNPayment.Status.LOCKED lnpayment.save() return True diff --git a/api/logics.py b/api/logics.py index 75f9d7bc..391507b1 100644 --- a/api/logics.py +++ b/api/logics.py @@ -479,8 +479,6 @@ class Logics(): if order.maker_bond.status == LNPayment.Status.LOCKED: return True elif LNNode.validate_hold_invoice_locked(order.maker_bond): - order.maker_bond.status = LNPayment.Status.LOCKED - order.maker_bond.save() cls.publish_order(order) return True return False @@ -628,8 +626,6 @@ class Logics(): if order.trade_escrow.status == LNPayment.Status.LOCKED: return True 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) return True return False diff --git a/api/management/commands/follow_invoices.py b/api/management/commands/follow_invoices.py index 4f2d6124..7f7dbec9 100644 --- a/api/management/commands/follow_invoices.py +++ b/api/management/commands/follow_invoices.py @@ -65,12 +65,18 @@ class Command(BaseCommand): for idx, hold_lnpayment in enumerate(queryset): 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)) response = stub.LookupInvoiceV2(request, metadata=[('macaroon', MACAROON.hex())]) 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: # 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): self.stdout.write(str(e)) hold_lnpayment.status = LNPayment.Status.CANCEL + # LND restarted. 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 else: self.stdout.write(str(e))