From 5e0639cfb331d24d7a250ce5c824ffe5f55d9a3e Mon Sep 17 00:00:00 2001 From: Reckless_Satoshi Date: Tue, 18 Jan 2022 08:57:55 -0800 Subject: [PATCH] Make payment_hash the primary key of LNpayment model --- api/admin.py | 5 +-- api/management/commands/follow_invoices.py | 36 +++++++++++++--------- api/models.py | 17 +++++++--- 3 files changed, 37 insertions(+), 21 deletions(-) diff --git a/api/admin.py b/api/admin.py index db50098f..ff7f2c68 100644 --- a/api/admin.py +++ b/api/admin.py @@ -22,6 +22,7 @@ class EUserAdmin(UserAdmin): def avatar_tag(self, obj): return obj.profile.avatar_tag() + @admin.register(Order) class OrderAdmin(AdminChangeLinksMixin, admin.ModelAdmin): list_display = ('id','type','maker_link','taker_link','status','amount','currency_link','t0_satoshis','is_disputed','is_fiat_sent','created_at','expires_at', 'buyer_invoice_link','maker_bond_link','taker_bond_link','trade_escrow_link') @@ -31,8 +32,8 @@ class OrderAdmin(AdminChangeLinksMixin, admin.ModelAdmin): @admin.register(LNPayment) class LNPaymentAdmin(AdminChangeLinksMixin, admin.ModelAdmin): - list_display = ('id','concept','status','num_satoshis','type','expires_at','sender_link','receiver_link','order_made','order_taken','order_escrow','order_paid') - list_display_links = ('id','concept','order_made','order_taken','order_escrow','order_paid') + list_display = ('hash','concept','status','num_satoshis','type','expires_at','sender_link','receiver_link','order_made','order_taken','order_escrow','order_paid') + list_display_links = ('hash','concept','order_made','order_taken','order_escrow','order_paid') change_links = ('sender','receiver') list_filter = ('type','concept','status') diff --git a/api/management/commands/follow_invoices.py b/api/management/commands/follow_invoices.py index 6a590cc7..d00c5e68 100644 --- a/api/management/commands/follow_invoices.py +++ b/api/management/commands/follow_invoices.py @@ -97,23 +97,31 @@ class Command(BaseCommand): def update_order_status(self, lnpayment): ''' Background process following LND hold invoices can catch LNpayments changing status. If they do, - the order status might have to change status too.''' + the order status might have to change too.''' # If the LNPayment goes to LOCKED (ACCEPTED) if lnpayment.status == LNPayment.Status.LOCKED: - # It is a maker bond => Publish order. - if not lnpayment.order_made == None: - Logics.publish_order(lnpayment.order_made) - return - - # It is a taker bond => close contract. - elif not lnpayment.order_taken == None: - if lnpayment.order_taken.status == Order.Status.TAK: - Logics.finalize_contract(lnpayment.order_taken) + try: + # It is a maker bond => Publish order. + if not lnpayment.order_made == None: + Logics.publish_order(lnpayment.order_made) return - # It is a trade escrow => move foward order status. - elif not lnpayment.order_escrow == None: - Logics.trade_escrow_received(lnpayment.order_escrow) - return \ No newline at end of file + # It is a taker bond => close contract. + elif not lnpayment.order_taken == None: + if lnpayment.order_taken.status == Order.Status.TAK: + Logics.finalize_contract(lnpayment.order_taken) + return + + # It is a trade escrow => move foward order status. + elif not lnpayment.order_escrow == None: + Logics.trade_escrow_received(lnpayment.order_escrow) + return + except Exception as e: + self.stdout.write(str(e)) + + # TODO If an lnpayment goes from LOCKED to INVGED. Totally weird + # halt the order + if lnpayment.status == LNPayment.Status.LOCKED: + pass \ No newline at end of file diff --git a/api/models.py b/api/models.py index 2285a928..673e25d8 100644 --- a/api/models.py +++ b/api/models.py @@ -2,6 +2,7 @@ from django.db import models from django.contrib.auth.models import User from django.core.validators import MaxValueValidator, MinValueValidator, validate_comma_separated_integer_list from django.db.models.signals import post_save, pre_delete +from django.template.defaultfilters import truncatechars from django.dispatch import receiver from django.utils.html import mark_safe import uuid @@ -59,15 +60,14 @@ class LNPayment(models.Model): # payment use details - id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) type = models.PositiveSmallIntegerField(choices=Types.choices, null=False, default=Types.HOLD) concept = models.PositiveSmallIntegerField(choices=Concepts.choices, null=False, default=Concepts.MAKEBOND) status = models.PositiveSmallIntegerField(choices=Status.choices, null=False, default=Status.INVGEN) routing_retries = models.PositiveSmallIntegerField(null=False, default=0) # payment info + payment_hash = models.CharField(max_length=100, unique=True, default=None, blank=True, primary_key=True) invoice = models.CharField(max_length=1200, unique=True, null=True, default=None, blank=True) # Some invoices with lots of routing hints might be long - payment_hash = models.CharField(max_length=100, unique=True, null=True, default=None, blank=True) preimage = models.CharField(max_length=64, unique=True, null=True, default=None, blank=True) description = models.CharField(max_length=500, unique=False, null=True, default=None, blank=True) num_satoshis = models.PositiveBigIntegerField(validators=[MinValueValidator(MIN_TRADE*BOND_SIZE), MaxValueValidator(MAX_TRADE*(1+BOND_SIZE+FEE))]) @@ -79,12 +79,19 @@ class LNPayment(models.Model): receiver = models.ForeignKey(User, related_name='receiver', on_delete=models.CASCADE, null=True, default=None) def __str__(self): - return (f'LN-{str(self.id)[:8]}: {self.Concepts(self.concept).label} - {self.Status(self.status).label}') + return (f'LN-{str(self.payment_hash)[:8]}: {self.Concepts(self.concept).label} - {self.Status(self.status).label}') class Meta: verbose_name = 'Lightning payment' verbose_name_plural = 'Lightning payments' + @property + def hash(self): + # Payment hash is the primary key of LNpayments + # However it is too long for the admin panel. + # We created a truncated property for display 'hash' + return truncatechars(self.payment_hash, 10) + class Order(models.Model): class Types(models.IntegerChoices): @@ -141,8 +148,8 @@ class Order(models.Model): # in dispute is_disputed = models.BooleanField(default=False, null=False) - maker_statement = models.TextField(max_length=5000, unique=True, null=True, default=None, blank=True) - taker_statement = models.TextField(max_length=5000, unique=True, null=True, default=None, blank=True) + maker_statement = models.TextField(max_length=5000, null=True, default=None, blank=True) + taker_statement = models.TextField(max_length=5000, null=True, default=None, blank=True) # LNpayments # Order collateral