internal payments get reported on async listeners.

This commit is contained in:
fiatjaf 2020-10-22 15:36:37 -03:00
parent a773584f6f
commit 2552fd8fc9
3 changed files with 23 additions and 5 deletions

View File

@ -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():

View File

@ -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)

View File

@ -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)