From 2552fd8fc9538c375e4c4795d1a2381bfaaa7017 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Thu, 22 Oct 2020 15:36:37 -0300 Subject: [PATCH] internal payments get reported on async listeners. --- lnbits/app.py | 3 ++- lnbits/core/services.py | 17 +++++++++++++---- lnbits/tasks.py | 8 ++++++++ 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/lnbits/app.py b/lnbits/app.py index 7a30e1529..8528b898a 100644 --- a/lnbits/app.py +++ b/lnbits/app.py @@ -12,7 +12,7 @@ from .core import core_app from .db import open_db, open_ext_db from .helpers import get_valid_extensions, get_js_vendored, get_css_vendored, url_for_vendored from .proxy_fix import ASGIProxyFix -from .tasks import run_deferred_async, invoice_listener, webhook_handler, grab_app_for_later +from .tasks import run_deferred_async, invoice_listener, internal_invoice_listener, webhook_handler, grab_app_for_later from .settings import WALLET secure_headers = SecureHeaders(hsts=False) @@ -128,6 +128,7 @@ def register_async_tasks(app): async def listeners(): run_deferred_async(app.nursery) app.nursery.start_soon(invoice_listener) + app.nursery.start_soon(internal_invoice_listener) @app.after_serving async def stop_listeners(): diff --git a/lnbits/core/services.py b/lnbits/core/services.py index e374d2d3a..cda16d22a 100644 --- a/lnbits/core/services.py +++ b/lnbits/core/services.py @@ -1,3 +1,4 @@ +import trio # type: ignore import httpx from typing import Optional, Tuple, Dict from quart import g @@ -89,8 +90,8 @@ def pay_invoice( ) # check_internal() returns the checking_id of the invoice we're waiting for - internal = check_internal(invoice.payment_hash) - if internal: + internal_checking_id = check_internal(invoice.payment_hash) + if internal_checking_id: # create a new payment from this wallet create_payment(checking_id=internal_id, fee=0, pending=False, **payment_kwargs) else: @@ -108,11 +109,19 @@ def pay_invoice( else: g.db.commit() - if internal: + if internal_checking_id: # mark the invoice from the other side as not pending anymore # so the other side only has access to his new money when we are sure # the payer has enough to deduct from - update_payment_status(checking_id=internal, pending=False) + update_payment_status(checking_id=internal_checking_id, pending=False) + + # notify receiver asynchronously + from lnbits.tasks import internal_invoice_paid + + try: + internal_invoice_paid.send_nowait(internal_checking_id) + except trio.WouldBlock: + pass else: # actually pay the external invoice payment: PaymentResponse = WALLET.pay_invoice(payment_request) diff --git a/lnbits/tasks.py b/lnbits/tasks.py index 8a1a6a127..285b7126f 100644 --- a/lnbits/tasks.py +++ b/lnbits/tasks.py @@ -77,6 +77,14 @@ async def webhook_handler(): return "", HTTPStatus.NO_CONTENT +internal_invoice_paid, internal_invoice_received = trio.open_memory_channel(0) + + +async def internal_invoice_listener(): + async for checking_id in internal_invoice_received: + await run_on_pseudo_request(invoice_callback_dispatcher, checking_id) + + async def invoice_listener(): async for checking_id in WALLET.paid_invoices_stream(): await run_on_pseudo_request(invoice_callback_dispatcher, checking_id)