Mint: watchdog balance log and killswitch (#705)

* wip store balance

* store balances in watchdog worker

* move mint_auth_database setting

* auth db

* balances returned as Amount (instead of int)

* add test for balance change on invoice receive

* fix 1 test

* cancel tasks on shutdown

* watchdog can now abort

* remove wallet api server

* fix lndgrpc

* fix lnbits balance

* disable watchdog

* balance lnbits msat

* test db watcher with its own database connection

* init superclass only once

* wip: log balance in keysets table

* check max balance using new keyset balance

* fix test

* fix another test

* store fees in keysets

* format

* cleanup

* shorter

* add keyset migration to auth server

* fix fakewallet

* fix db tests

* fix postgres problems during migration 26 (mint)

* fix cln

* ledger

* working with pending

* super fast watchdog, errors

* test new pipeline

* delete walletapi

* delete unneeded files

* revert workflows
This commit is contained in:
callebtc
2025-05-11 20:29:13 +02:00
committed by GitHub
parent 38bdb9ce76
commit fc0e3fe663
41 changed files with 938 additions and 960 deletions

View File

@@ -51,7 +51,8 @@ settings.mint_lnd_enable_mpp = True
settings.mint_clnrest_enable_mpp = True
settings.mint_input_fee_ppk = 0
settings.db_connection_pool = True
# settings.mint_require_auth = False
settings.mint_require_auth = False
settings.mint_watchdog_enabled = False
assert "test" in settings.cashu_dir
shutil.rmtree(settings.cashu_dir, ignore_errors=True)

View File

@@ -220,4 +220,4 @@ async def pay_if_regtest(bolt11: str) -> None:
pay_real_invoice(bolt11)
if is_fake:
await asyncio.sleep(settings.fakewallet_delay_incoming_payment or 0)
await asyncio.sleep(0.1)
await asyncio.sleep(0.5)

View File

@@ -2,7 +2,7 @@ from typing import List
import pytest
from cashu.core.base import BlindedMessage, MintKeyset, Proof, Unit
from cashu.core.base import BlindedMessage, Proof, Unit
from cashu.core.crypto.b_dhke import step1_alice
from cashu.core.helpers import calculate_number_of_blank_outputs
from cashu.core.models import PostMintQuoteRequest
@@ -219,11 +219,9 @@ async def test_generate_change_promises_returns_empty_if_no_outputs(ledger: Ledg
@pytest.mark.asyncio
async def test_get_balance(ledger: Ledger):
unit = Unit["sat"]
active_keyset: MintKeyset = next(
filter(lambda k: k.active and k.unit == unit, ledger.keysets.values())
)
balance = await ledger.get_balance(active_keyset)
balance, fees_paid = await ledger.get_balance(unit)
assert balance == 0
assert fees_paid == 0
@pytest.mark.asyncio

View File

@@ -45,13 +45,13 @@ async def test_mint_proofs_pending(wallet: Wallet, ledger: Ledger):
proofs_states_before_split = await wallet.check_proof_state(proofs)
assert all([s.unspent for s in proofs_states_before_split.states])
await ledger.db_write._verify_spent_proofs_and_set_pending(proofs)
await ledger.db_write._verify_spent_proofs_and_set_pending(proofs, ledger.keysets)
proof_states = await wallet.check_proof_state(proofs)
assert all([s.pending for s in proof_states.states])
await assert_err(wallet.split(wallet.proofs, 20), "proofs are pending.")
await ledger.db_write._unset_proofs_pending(proofs)
await ledger.db_write._unset_proofs_pending(proofs, ledger.keysets)
await wallet.split(proofs, 20)

View File

@@ -75,9 +75,20 @@ async def test_db_tables(ledger: Ledger):
"mint_quotes",
"mint_pubkeys",
"promises",
"balance_log",
"balance",
"balance_issued",
"balance_redeemed",
]
for table in tables_expected:
assert table in tables
tables.sort()
tables_expected.sort()
if ledger.db.type == db.SQLITE:
# SQLite does not return views
tables_expected.remove("balance")
tables_expected.remove("balance_issued")
tables_expected.remove("balance_redeemed")
assert tables == tables_expected
@pytest.mark.asyncio
@@ -202,8 +213,12 @@ async def test_db_verify_spent_proofs_and_set_pending_race_condition(
await assert_err_multiple(
asyncio.gather(
ledger.db_write._verify_spent_proofs_and_set_pending(wallet.proofs),
ledger.db_write._verify_spent_proofs_and_set_pending(wallet.proofs),
ledger.db_write._verify_spent_proofs_and_set_pending(
wallet.proofs, ledger.keysets
),
ledger.db_write._verify_spent_proofs_and_set_pending(
wallet.proofs, ledger.keysets
),
),
[
"failed to acquire database lock",
@@ -228,11 +243,15 @@ async def test_db_verify_spent_proofs_and_set_pending_delayed_no_race_condition(
async def delayed_verify_spent_proofs_and_set_pending():
await asyncio.sleep(0.1)
await ledger.db_write._verify_spent_proofs_and_set_pending(wallet.proofs)
await ledger.db_write._verify_spent_proofs_and_set_pending(
wallet.proofs, ledger.keysets
)
await assert_err(
asyncio.gather(
ledger.db_write._verify_spent_proofs_and_set_pending(wallet.proofs),
ledger.db_write._verify_spent_proofs_and_set_pending(
wallet.proofs, ledger.keysets
),
delayed_verify_spent_proofs_and_set_pending(),
),
"proofs are pending",
@@ -255,8 +274,12 @@ async def test_db_verify_spent_proofs_and_set_pending_no_race_condition_differen
assert len(wallet.proofs) == 2
asyncio.gather(
ledger.db_write._verify_spent_proofs_and_set_pending(wallet.proofs[:1]),
ledger.db_write._verify_spent_proofs_and_set_pending(wallet.proofs[1:]),
ledger.db_write._verify_spent_proofs_and_set_pending(
wallet.proofs[:1], ledger.keysets
),
ledger.db_write._verify_spent_proofs_and_set_pending(
wallet.proofs[1:], ledger.keysets
),
)
@@ -325,6 +348,8 @@ async def test_db_lock_table(wallet: Wallet, ledger: Ledger):
async with ledger.db.connect(lock_table="proofs_pending", lock_timeout=0.1) as conn:
assert isinstance(conn, Connection)
await assert_err(
ledger.db_write._verify_spent_proofs_and_set_pending(wallet.proofs),
ledger.db_write._verify_spent_proofs_and_set_pending(
wallet.proofs, ledger.keysets
),
"failed to acquire database lock",
)

View File

@@ -153,7 +153,7 @@ async def create_pending_melts(
quote=quote,
db=ledger.db,
)
pending_proof = Proof(amount=123, C="asdasd", secret="asdasd", id=quote_id)
pending_proof = Proof(amount=123, C="asdasd", secret="asdasd", id=ledger.keyset.id)
await ledger.crud.set_proof_pending(
db=ledger.db,
proof=pending_proof,

View File

@@ -68,7 +68,7 @@ async def create_pending_melts(
quote=quote,
db=ledger.db,
)
pending_proof = Proof(amount=123, C="asdasd", secret="asdasd", id=quote_id)
pending_proof = Proof(amount=123, C="asdasd", secret="asdasd", id=ledger.keyset.id)
await ledger.crud.set_proof_pending(
db=ledger.db,
proof=pending_proof,

View File

@@ -59,6 +59,44 @@ async def test_lightning_create_invoice(ledger: Ledger):
assert status.settled
@pytest.mark.asyncio
@pytest.mark.skipif(is_fake, reason="only regtest")
async def test_lightning_create_invoice_balance_change(ledger: Ledger):
invoice_amount = 1000 # sat
invoice = await ledger.backends[Method.bolt11][Unit.sat].create_invoice(
Amount(Unit.sat, invoice_amount)
)
assert invoice.ok
assert invoice.payment_request
assert invoice.checking_id
# TEST 2: check the invoice status
status = await ledger.backends[Method.bolt11][Unit.sat].get_invoice_status(
invoice.checking_id
)
assert status.pending
status = await ledger.backends[Method.bolt11][Unit.sat].status()
balance_before = status.balance
# settle the invoice
await pay_if_regtest(invoice.payment_request)
# cln takes some time to update the balance
await asyncio.sleep(SLEEP_TIME)
# TEST 3: check the invoice status
status = await ledger.backends[Method.bolt11][Unit.sat].get_invoice_status(
invoice.checking_id
)
assert status.settled
status = await ledger.backends[Method.bolt11][Unit.sat].status()
balance_after = status.balance
assert balance_after == balance_before + invoice_amount
@pytest.mark.asyncio
@pytest.mark.skipif(is_fake, reason="only regtest")
async def test_lightning_get_payment_quote(ledger: Ledger):

162
tests/test_mint_watchdog.py Normal file
View File

@@ -0,0 +1,162 @@
import pytest
import pytest_asyncio
from cashu.core.base import Amount, MeltQuoteState, Method, Unit
from cashu.core.models import PostMeltQuoteRequest
from cashu.core.settings import settings
from cashu.mint.ledger import Ledger
from cashu.wallet.wallet import Wallet
from tests.conftest import SERVER_ENDPOINT
from tests.helpers import (
get_real_invoice,
is_fake,
pay_if_regtest,
)
@pytest_asyncio.fixture(scope="function")
async def wallet():
wallet = await Wallet.with_db(
url=SERVER_ENDPOINT,
db="test_data/wallet",
name="wallet",
)
await wallet.load_mint()
yield wallet
@pytest.mark.asyncio
async def test_check_balances_and_abort(ledger: Ledger):
ok = await ledger.check_balances_and_abort(
ledger.backends[Method.bolt11][Unit.sat],
None,
Amount(Unit.sat, 0),
Amount(Unit.sat, 0),
Amount(Unit.sat, 0),
)
assert ok
@pytest.mark.asyncio
async def test_balance_update_on_mint(wallet: Wallet, ledger: Ledger):
balance_before, fees_paid_before = await ledger.get_unit_balance_and_fees(
Unit.sat, ledger.db
)
mint_quote = await wallet.request_mint(64)
await pay_if_regtest(mint_quote.request)
await wallet.mint(64, quote_id=mint_quote.quote)
assert wallet.balance == 64
balance_after, fees_paid_after = await ledger.get_unit_balance_and_fees(
Unit.sat, ledger.db
)
assert balance_after == balance_before + 64
assert fees_paid_after == fees_paid_before
@pytest.mark.asyncio
@pytest.mark.skipif(is_fake, reason="only works with Regtest")
async def test_balance_update_on_test_melt_internal(wallet: Wallet, ledger: Ledger):
settings.fakewallet_brr = False
# mint twice so we have enough to pay the second invoice back
mint_quote = await wallet.request_mint(128)
await pay_if_regtest(mint_quote.request)
await wallet.mint(128, quote_id=mint_quote.quote)
assert wallet.balance == 128
balance_before, fees_paid_before = await ledger.get_unit_balance_and_fees(
Unit.sat, ledger.db
)
# create a mint quote so that we can melt to it internally
payment_amount = 64
mint_quote_to_pay = await wallet.request_mint(payment_amount)
invoice_payment_request = mint_quote_to_pay.request
melt_quote = await ledger.melt_quote(
PostMeltQuoteRequest(request=invoice_payment_request, unit="sat")
)
if not settings.debug_mint_only_deprecated:
melt_quote_response_pre_payment = await wallet.get_melt_quote(melt_quote.quote)
assert (
not melt_quote_response_pre_payment.state == MeltQuoteState.paid.value
), "melt quote should not be paid"
assert melt_quote_response_pre_payment.amount == payment_amount
melt_quote_pre_payment = await ledger.get_melt_quote(melt_quote.quote)
assert not melt_quote_pre_payment.paid, "melt quote should not be paid"
assert melt_quote_pre_payment.unpaid
_, send_proofs = await wallet.swap_to_send(wallet.proofs, payment_amount)
await ledger.melt(proofs=send_proofs, quote=melt_quote.quote)
await wallet.invalidate(send_proofs, check_spendable=True)
assert wallet.balance == 64
melt_quote_post_payment = await ledger.get_melt_quote(melt_quote.quote)
assert melt_quote_post_payment.paid, "melt quote should be paid"
balance_after, fees_paid_after = await ledger.get_unit_balance_and_fees(
Unit.sat, ledger.db
)
# balance should have dropped
assert balance_after == balance_before - payment_amount
assert fees_paid_after == fees_paid_before
# now mint
await wallet.mint(payment_amount, quote_id=mint_quote_to_pay.quote)
assert wallet.balance == 128
balance_after, fees_paid_after = await ledger.get_unit_balance_and_fees(
Unit.sat, ledger.db
)
# balance should be back
assert balance_after == balance_before
assert fees_paid_after == fees_paid_before
@pytest.mark.asyncio
@pytest.mark.skipif(is_fake, reason="only works with Regtest")
async def test_balance_update_on_melt_external(wallet: Wallet, ledger: Ledger):
# mint twice so we have enough to pay the second invoice back
mint_quote = await wallet.request_mint(128)
await pay_if_regtest(mint_quote.request)
await wallet.mint(128, quote_id=mint_quote.quote)
assert wallet.balance == 128
balance_before, fees_paid_before = await ledger.get_unit_balance_and_fees(
Unit.sat, ledger.db
)
invoice_dict = get_real_invoice(64)
invoice_payment_request = invoice_dict["payment_request"]
mint_quote = await wallet.melt_quote(invoice_payment_request)
total_amount = mint_quote.amount + mint_quote.fee_reserve
_, send_proofs = await wallet.swap_to_send(wallet.proofs, total_amount)
melt_quote = await ledger.melt_quote(
PostMeltQuoteRequest(request=invoice_payment_request, unit="sat")
)
if not settings.debug_mint_only_deprecated:
melt_quote_response_pre_payment = await wallet.get_melt_quote(melt_quote.quote)
assert (
melt_quote_response_pre_payment.state == MeltQuoteState.unpaid.value
), "melt quote should not be paid"
assert melt_quote_response_pre_payment.amount == melt_quote.amount
melt_quote_resp = await ledger.melt(proofs=send_proofs, quote=melt_quote.quote)
fees_paid = melt_quote.fee_reserve - (
sum([b.amount for b in melt_quote_resp.change]) if melt_quote_resp.change else 0
)
melt_quote_post_payment = await ledger.get_melt_quote(melt_quote.quote)
assert melt_quote_post_payment.paid, "melt quote should be paid"
balance_after, fees_paid_after = await ledger.get_unit_balance_and_fees(
Unit.sat, ledger.db
)
assert balance_after == balance_before - 64 - fees_paid
assert fees_paid_after == fees_paid_before

View File

@@ -1,199 +0,0 @@
import asyncio
import pytest
import pytest_asyncio
from fastapi.testclient import TestClient
from cashu.lightning.base import InvoiceResponse, PaymentResult, PaymentStatus
from cashu.wallet.api.app import app
from cashu.wallet.wallet import Wallet
from tests.conftest import SERVER_ENDPOINT
from tests.helpers import is_regtest
@pytest_asyncio.fixture(scope="function")
async def wallet():
wallet = await Wallet.with_db(
url=SERVER_ENDPOINT,
db="test_data/wallet",
name="wallet",
)
await wallet.load_mint()
yield wallet
@pytest.mark.skipif(is_regtest, reason="regtest")
@pytest.mark.asyncio
async def test_invoice(wallet: Wallet):
with TestClient(app) as client:
response = client.post("/lightning/create_invoice?amount=100")
assert response.status_code == 200
invoice_response = InvoiceResponse.parse_obj(response.json())
state = PaymentStatus(result=PaymentResult.PENDING)
while state.pending:
print("checking invoice state")
response2 = client.get(
f"/lightning/invoice_state?payment_request={invoice_response.payment_request}"
)
state = PaymentStatus.parse_obj(response2.json())
await asyncio.sleep(0.1)
print("state:", state)
print("paid")
await wallet.load_proofs()
assert wallet.available_balance >= 100
@pytest.mark.skipif(is_regtest, reason="regtest")
@pytest.mark.asyncio
async def test_balance():
with TestClient(app) as client:
response = client.get("/balance")
assert response.status_code == 200
assert "balance" in response.json()
assert response.json()["keysets"]
assert response.json()["mints"]
@pytest.mark.skipif(is_regtest, reason="regtest")
@pytest.mark.asyncio
async def test_send(wallet: Wallet):
with TestClient(app) as client:
response = client.post("/send?amount=10")
assert response.status_code == 200
assert response.json()["balance"]
@pytest.mark.skipif(is_regtest, reason="regtest")
@pytest.mark.asyncio
async def test_send_without_split(wallet: Wallet):
with TestClient(app) as client:
response = client.post("/send?amount=2&offline=true")
assert response.status_code == 200
assert response.json()["balance"]
@pytest.mark.skipif(is_regtest, reason="regtest")
@pytest.mark.asyncio
async def test_send_too_much(wallet: Wallet):
with TestClient(app) as client:
response = client.post("/send?amount=110000")
assert response.status_code == 400
@pytest.mark.skipif(is_regtest, reason="regtest")
@pytest.mark.asyncio
async def test_pending():
with TestClient(app) as client:
response = client.get("/pending")
assert response.status_code == 200
@pytest.mark.skipif(is_regtest, reason="regtest")
@pytest.mark.asyncio
async def test_receive_all(wallet: Wallet):
with TestClient(app) as client:
response = client.post("/receive?all=true")
assert response.status_code == 200
assert response.json()["initial_balance"]
assert response.json()["balance"]
@pytest.mark.skipif(is_regtest, reason="regtest")
@pytest.mark.asyncio
async def test_burn_all(wallet: Wallet):
with TestClient(app) as client:
response = client.post("/send?amount=20")
assert response.status_code == 200
response = client.post("/burn?all=true")
assert response.status_code == 200
assert response.json()["balance"]
@pytest.mark.skipif(is_regtest, reason="regtest")
@pytest.mark.asyncio
async def test_pay():
with TestClient(app) as client:
invoice = (
"lnbc100n1pjjcqzfdq4gdshx6r4ypjx2ur0wd5hgpp58xvj8yn00d5"
"7uhshwzcwgy9uj3vwf5y2lr5fjf78s4w9l4vhr6xssp5stezsyty9r"
"hv3lat69g4mhqxqun56jyehhkq3y8zufh83xyfkmmq4usaqwrt5q4f"
"adm44g6crckp0hzvuyv9sja7t65hxj0ucf9y46qstkay7gfnwhuxgr"
"krf7djs38rml39l8wpn5ug9shp3n55quxhdecqfwxg23"
)
response = client.post(f"/lightning/pay_invoice?bolt11={invoice}")
assert response.status_code == 200
@pytest.mark.skipif(is_regtest, reason="regtest")
@pytest.mark.asyncio
async def test_lock():
with TestClient(app) as client:
response = client.get("/lock")
assert response.status_code == 200
@pytest.mark.skipif(is_regtest, reason="regtest")
@pytest.mark.asyncio
async def test_locks():
with TestClient(app) as client:
response = client.get("/locks")
assert response.status_code == 200
@pytest.mark.skipif(is_regtest, reason="regtest")
@pytest.mark.asyncio
async def test_invoices():
with TestClient(app) as client:
response = client.get("/invoices")
assert response.status_code == 200
@pytest.mark.skipif(is_regtest, reason="regtest")
@pytest.mark.asyncio
async def test_wallets():
with TestClient(app) as client:
response = client.get("/wallets")
assert response.status_code == 200
@pytest.mark.skipif(is_regtest, reason="regtest")
@pytest.mark.asyncio
async def test_info():
with TestClient(app) as client:
response = client.get("/info")
assert response.status_code == 200
assert response.json()["version"]
@pytest.mark.skipif(is_regtest, reason="regtest")
@pytest.mark.asyncio
async def test_flow(wallet: Wallet):
with TestClient(app) as client:
response = client.get("/balance")
initial_balance = response.json()["balance"]
response = client.post("/lightning/create_invoice?amount=100")
invoice_response = InvoiceResponse.parse_obj(response.json())
state = PaymentStatus(result=PaymentResult.PENDING)
while state.pending:
print("checking invoice state")
response2 = client.get(
f"/lightning/invoice_state?payment_request={invoice_response.payment_request}"
)
state = PaymentStatus.parse_obj(response2.json())
await asyncio.sleep(0.1)
print("state:", state)
response = client.get("/balance")
assert response.json()["balance"] == initial_balance + 100
response = client.post("/send?amount=50")
response = client.get("/balance")
assert response.json()["balance"] == initial_balance + 50
response = client.post("/send?amount=50")
response = client.get("/balance")
assert response.json()["balance"] == initial_balance
response = client.get("/pending")
token = response.json()["pending_token"]["0"]["token"]
amount = response.json()["pending_token"]["0"]["amount"]
response = client.post(f"/receive?token={token}")
response = client.get("/balance")
assert response.json()["balance"] == initial_balance + amount

View File

@@ -115,7 +115,7 @@ def test_balance(cli_prefix):
print("------ BALANCE ------")
print(result.output)
w = asyncio.run(init_wallet())
assert f"Balance: {w.available_balance} sat" in result.output
assert f"Balance: {w.available_balance}" in result.output
assert result.exit_code == 0