mirror of
https://github.com/lnbits/lnbits.git
synced 2025-06-10 17:01:36 +02:00
fix: guard internal payments
This commit is contained in:
parent
205190682b
commit
4eb8ca5ac6
@ -46,6 +46,8 @@ from .notifications import send_payment_notification
|
|||||||
|
|
||||||
outgoing_payments_processing: list[str] = []
|
outgoing_payments_processing: list[str] = []
|
||||||
|
|
||||||
|
internal_payment_lock = asyncio.Lock()
|
||||||
|
|
||||||
|
|
||||||
async def pay_invoice(
|
async def pay_invoice(
|
||||||
*,
|
*,
|
||||||
@ -443,15 +445,20 @@ async def get_payments_daily_stats(
|
|||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
async def _pay_invoice(wallet, create_payment_model, conn):
|
async def _pay_invoice(
|
||||||
payment = await _pay_internal_invoice(wallet, create_payment_model, conn)
|
wallet: Wallet,
|
||||||
|
create_payment_model: CreatePayment,
|
||||||
|
conn: Optional[Connection] = None,
|
||||||
|
):
|
||||||
|
async with internal_payment_lock:
|
||||||
|
payment = await _pay_internal_invoice(wallet.id, create_payment_model, conn)
|
||||||
if not payment:
|
if not payment:
|
||||||
payment = await _pay_external_invoice(wallet, create_payment_model, conn)
|
payment = await _pay_external_invoice(wallet, create_payment_model, conn)
|
||||||
return payment
|
return payment
|
||||||
|
|
||||||
|
|
||||||
async def _pay_internal_invoice(
|
async def _pay_internal_invoice(
|
||||||
wallet: Wallet,
|
wallet_id: str,
|
||||||
create_payment_model: CreatePayment,
|
create_payment_model: CreatePayment,
|
||||||
conn: Optional[Connection] = None,
|
conn: Optional[Connection] = None,
|
||||||
) -> Optional[Payment]:
|
) -> Optional[Payment]:
|
||||||
@ -485,6 +492,11 @@ async def _pay_internal_invoice(
|
|||||||
fee_reserve_total_msat = fee_reserve_total(amount_msat, internal=True)
|
fee_reserve_total_msat = fee_reserve_total(amount_msat, internal=True)
|
||||||
create_payment_model.fee = abs(fee_reserve_total_msat)
|
create_payment_model.fee = abs(fee_reserve_total_msat)
|
||||||
|
|
||||||
|
# get the wallet again to make sure we have the latest balance
|
||||||
|
wallet = await get_wallet(wallet_id, conn=conn)
|
||||||
|
if not wallet:
|
||||||
|
raise PaymentError(f"Could not fetch wallet '{wallet_id}'.", status="failed")
|
||||||
|
print("### wallet.balance", wallet.balance_msat, wallet.balance)
|
||||||
if wallet.balance_msat < abs(amount_msat) + fee_reserve_total_msat:
|
if wallet.balance_msat < abs(amount_msat) + fee_reserve_total_msat:
|
||||||
raise PaymentError("Insufficient balance.", status="failed")
|
raise PaymentError("Insufficient balance.", status="failed")
|
||||||
|
|
||||||
|
@ -158,7 +158,7 @@ async def test_pay_twice_fast_b():
|
|||||||
payment_request=payment_b.bolt11,
|
payment_request=payment_b.bolt11,
|
||||||
)
|
)
|
||||||
|
|
||||||
payments = await asyncio.gather(pay_first(), pay_second())
|
await asyncio.gather(pay_first(), pay_second())
|
||||||
|
|
||||||
wallet_one_after = await get_wallet(wallet_one.id)
|
wallet_one_after = await get_wallet(wallet_one.id)
|
||||||
assert wallet_one_after
|
assert wallet_one_after
|
||||||
@ -167,6 +167,7 @@ async def test_pay_twice_fast_b():
|
|||||||
assert wallet_two_after
|
assert wallet_two_after
|
||||||
print("### wallet_two", wallet_two_after.balance)
|
print("### wallet_two", wallet_two_after.balance)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
@pytest.mark.anyio
|
||||||
async def test_fake_wallet_pay_external(
|
async def test_fake_wallet_pay_external(
|
||||||
to_wallet: Wallet, external_funding_source: FakeWallet
|
to_wallet: Wallet, external_funding_source: FakeWallet
|
||||||
|
Loading…
x
Reference in New Issue
Block a user