From 25ab5fdf2e5f10606eb32191344d218554024923 Mon Sep 17 00:00:00 2001 From: Reckless_Satoshi Date: Mon, 24 Jan 2022 10:34:52 -0800 Subject: [PATCH] Add invoice expiry paddingas temporary fix for order expire at invoice expire. --- api/lightning/node.py | 6 +++++- api/logics.py | 9 ++++++--- api/models.py | 3 ++- frontend/src/components/OrderPage.js | 3 ++- .../assets/sounds/{successful.mp3 => sucessful.mp3} | Bin 5 files changed, 15 insertions(+), 6 deletions(-) rename frontend/static/assets/sounds/{successful.mp3 => sucessful.mp3} (100%) diff --git a/api/lightning/node.py b/api/lightning/node.py index 8ba82abd..f0a1146f 100644 --- a/api/lightning/node.py +++ b/api/lightning/node.py @@ -80,7 +80,7 @@ class LNNode(): memo=description, value=num_satoshis, hash=r_hash, - expiry=invoice_expiry, + expiry=int(invoice_expiry*1.15), # actual expiry is padded by 15% cltv_expiry=cltv_expiry_blocks, ) response = cls.invoicesstub.AddHoldInvoice(request, metadata=[('macaroon', MACAROON.hex())]) @@ -91,6 +91,7 @@ class LNNode(): hold_payment['payment_hash'] = payreq_decoded.payment_hash hold_payment['created_at'] = timezone.make_aware(datetime.fromtimestamp(payreq_decoded.timestamp)) hold_payment['expires_at'] = hold_payment['created_at'] + timedelta(seconds=payreq_decoded.expiry) + hold_payment['cltv_expiry'] = cltv_expiry_blocks return hold_payment @@ -103,6 +104,9 @@ class LNNode(): print(response.state) # TODO ERROR HANDLING + # Will fail if 'unable to locate invoice'. Happens if invoice expiry + # time has passed (but these are 15% padded at the moment). Should catch it + # and report back that the invoice has expired (better robustness) if response.state == 0: # OPEN print('STATUS: OPEN') pass diff --git a/api/logics.py b/api/logics.py index e21c88dc..a3a069cd 100644 --- a/api/logics.py +++ b/api/logics.py @@ -517,7 +517,8 @@ class Logics(): description = description, payment_hash = hold_payment['payment_hash'], created_at = hold_payment['created_at'], - expires_at = hold_payment['expires_at']) + expires_at = hold_payment['expires_at'], + cltv_expiry = hold_payment['cltv_expiry']) order.save() return True, {'bond_invoice':hold_payment['invoice'], 'bond_satoshis':bond_satoshis} @@ -597,7 +598,8 @@ class Logics(): description = description, payment_hash = hold_payment['payment_hash'], created_at = hold_payment['created_at'], - expires_at = hold_payment['expires_at']) + expires_at = hold_payment['expires_at'], + cltv_expiry = hold_payment['cltv_expiry']) order.expires_at = timezone.now() + timedelta(seconds=Order.t_to_expire[Order.Status.TAK]) order.save() @@ -663,7 +665,8 @@ class Logics(): description = description, payment_hash = hold_payment['payment_hash'], created_at = hold_payment['created_at'], - expires_at = hold_payment['expires_at']) + expires_at = hold_payment['expires_at'], + cltv_expiry = hold_payment['cltv_expiry']) order.save() return True, {'escrow_invoice':hold_payment['invoice'],'escrow_satoshis': escrow_satoshis} diff --git a/api/models.py b/api/models.py index c40457d0..43482c78 100644 --- a/api/models.py +++ b/api/models.py @@ -71,6 +71,7 @@ class LNPayment(models.Model): num_satoshis = models.PositiveBigIntegerField(validators=[MinValueValidator(MIN_TRADE*BOND_SIZE), MaxValueValidator(MAX_TRADE*(1+BOND_SIZE+FEE))]) created_at = models.DateTimeField() expires_at = models.DateTimeField() + cltv_expiry = models.PositiveSmallIntegerField(null=True, default=None, blank=True) # routing routing_attempts = models.PositiveSmallIntegerField(null=False, default=0) @@ -183,7 +184,7 @@ class Order(models.Model): 13 : 24*60*60, # 'Sending satoshis to buyer' 14 : 24*60*60, # 'Sucessful trade' 15 : 24*60*60, # 'Failed lightning network routing' - 16 : 10*24*60*60, # 'Wait for dispute resolution' + 16 : 10*24*60*60, # 'Wait for dispute resolution' 17 : 24*60*60, # 'Maker lost dispute' 18 : 24*60*60, # 'Taker lost dispute' } diff --git a/frontend/src/components/OrderPage.js b/frontend/src/components/OrderPage.js index 951060d4..2ec1454a 100644 --- a/frontend/src/components/OrderPage.js +++ b/frontend/src/components/OrderPage.js @@ -52,7 +52,7 @@ export default class OrderPage extends Component { // Refresh delays according to Order status this.statusToDelay = { "0": 2000, //'Waiting for maker bond' - "1": 45000, //'Public' + "1": 25000, //'Public' "2": 9999999, //'Deleted' "3": 2000, //'Waiting for taker bond' "4": 9999999, //'Cancelled' @@ -86,6 +86,7 @@ export default class OrderPage extends Component { loading: false, delay: this.setDelay(newStateVars.status), currencyCode: this.getCurrencyCode(newStateVars.currency), + penalty: newStateVars.penalty, // in case penalty time has finished, it goes back to null }; var completeStateVars = Object.assign({}, newStateVars, otherStateVars); diff --git a/frontend/static/assets/sounds/successful.mp3 b/frontend/static/assets/sounds/sucessful.mp3 similarity index 100% rename from frontend/static/assets/sounds/successful.mp3 rename to frontend/static/assets/sounds/sucessful.mp3