mirror of
https://github.com/aljazceru/nutshell.git
synced 2026-01-06 18:34:20 +01:00
Wallet: store quotes (#657)
* wallet_quotes_wip * fix quote in db * fix subscription test * clean up api * fix api tests * fix balance check
This commit is contained in:
@@ -63,7 +63,7 @@ async def test_db_tables(ledger: Ledger):
|
||||
"SELECT table_name FROM information_schema.tables WHERE table_schema ="
|
||||
" 'public';"
|
||||
)
|
||||
tables_all: List[Tuple[str]] = tables_res.all()
|
||||
tables_all: List[Tuple[str]] = tables_res.all() # type: ignore
|
||||
tables = [t[0] for t in tables_all]
|
||||
tables_expected = [
|
||||
"dbversions",
|
||||
@@ -117,7 +117,7 @@ async def test_db_get_connection(ledger: Ledger):
|
||||
|
||||
# @pytest.mark.asyncio
|
||||
# async def test_db_get_connection_locked(wallet: Wallet, ledger: Ledger):
|
||||
# invoice = await wallet.request_mint(64)
|
||||
# mint_quote = await wallet.request_mint(64)
|
||||
|
||||
# async def get_connection():
|
||||
# """This code makes sure that only the error of the second connection is raised (which we check in the assert_err)"""
|
||||
@@ -129,7 +129,7 @@ async def test_db_get_connection(ledger: Ledger):
|
||||
# ) as conn2:
|
||||
# # write something with conn1, we never reach this point if the lock works
|
||||
# await conn2.execute(
|
||||
# f"INSERT INTO mint_quotes (quote, amount) VALUES ('{invoice.id}', 100);"
|
||||
# f"INSERT INTO mint_quotes (quote, amount) VALUES ('{mint_quote.quote}', 100);"
|
||||
# )
|
||||
# except Exception as exc:
|
||||
# # this is expected to raise
|
||||
@@ -149,28 +149,28 @@ async def test_db_get_connection_lock_row(wallet: Wallet, ledger: Ledger):
|
||||
if ledger.db.type == db.SQLITE:
|
||||
pytest.skip("SQLite does not support row locking")
|
||||
|
||||
invoice = await wallet.request_mint(64)
|
||||
mint_quote = await wallet.request_mint(64)
|
||||
|
||||
async def get_connection():
|
||||
"""This code makes sure that only the error of the second connection is raised (which we check in the assert_err)"""
|
||||
try:
|
||||
async with ledger.db.get_connection(
|
||||
lock_table="mint_quotes",
|
||||
lock_select_statement=f"quote='{invoice.id}'",
|
||||
lock_select_statement=f"quote='{mint_quote.quote}'",
|
||||
lock_timeout=0.1,
|
||||
) as conn1:
|
||||
await conn1.execute(
|
||||
f"UPDATE mint_quotes SET amount=100 WHERE quote='{invoice.id}';"
|
||||
f"UPDATE mint_quotes SET amount=100 WHERE quote='{mint_quote.quote}';"
|
||||
)
|
||||
try:
|
||||
async with ledger.db.get_connection(
|
||||
lock_table="mint_quotes",
|
||||
lock_select_statement=f"quote='{invoice.id}'",
|
||||
lock_select_statement=f"quote='{mint_quote.quote}'",
|
||||
lock_timeout=0.1,
|
||||
) as conn2:
|
||||
# write something with conn1, we never reach this point if the lock works
|
||||
await conn2.execute(
|
||||
f"UPDATE mint_quotes SET amount=101 WHERE quote='{invoice.id}';"
|
||||
f"UPDATE mint_quotes SET amount=101 WHERE quote='{mint_quote.quote}';"
|
||||
)
|
||||
except Exception as exc:
|
||||
# this is expected to raise
|
||||
@@ -189,9 +189,9 @@ async def test_db_verify_spent_proofs_and_set_pending_race_condition(
|
||||
wallet: Wallet, ledger: Ledger
|
||||
):
|
||||
# fill wallet
|
||||
invoice = await wallet.request_mint(64)
|
||||
await pay_if_regtest(invoice.bolt11)
|
||||
await wallet.mint(64, id=invoice.id)
|
||||
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
|
||||
|
||||
await assert_err_multiple(
|
||||
@@ -211,9 +211,9 @@ async def test_db_verify_spent_proofs_and_set_pending_delayed_no_race_condition(
|
||||
wallet: Wallet, ledger: Ledger
|
||||
):
|
||||
# fill wallet
|
||||
invoice = await wallet.request_mint(64)
|
||||
await pay_if_regtest(invoice.bolt11)
|
||||
await wallet.mint(64, id=invoice.id)
|
||||
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
|
||||
|
||||
async def delayed_verify_spent_proofs_and_set_pending():
|
||||
@@ -234,9 +234,9 @@ async def test_db_verify_spent_proofs_and_set_pending_no_race_condition_differen
|
||||
wallet: Wallet, ledger: Ledger
|
||||
):
|
||||
# fill wallet
|
||||
invoice = await wallet.request_mint(64)
|
||||
await pay_if_regtest(invoice.bolt11)
|
||||
await wallet.mint(64, id=invoice.id, split=[32, 32])
|
||||
mint_quote = await wallet.request_mint(64)
|
||||
await pay_if_regtest(mint_quote.request)
|
||||
await wallet.mint(64, quote_id=mint_quote.quote, split=[32, 32])
|
||||
assert wallet.balance == 64
|
||||
assert len(wallet.proofs) == 2
|
||||
|
||||
@@ -251,26 +251,26 @@ async def test_db_get_connection_lock_different_row(wallet: Wallet, ledger: Ledg
|
||||
if ledger.db.type == db.SQLITE:
|
||||
pytest.skip("SQLite does not support row locking")
|
||||
# this should work since we lock two different rows
|
||||
invoice = await wallet.request_mint(64)
|
||||
invoice2 = await wallet.request_mint(64)
|
||||
mint_quote = await wallet.request_mint(64)
|
||||
mint_quote_2 = await wallet.request_mint(64)
|
||||
|
||||
async def get_connection2():
|
||||
"""This code makes sure that only the error of the second connection is raised (which we check in the assert_err)"""
|
||||
try:
|
||||
async with ledger.db.get_connection(
|
||||
lock_table="mint_quotes",
|
||||
lock_select_statement=f"quote='{invoice.id}'",
|
||||
lock_select_statement=f"quote='{mint_quote.quote}'",
|
||||
lock_timeout=0.1,
|
||||
):
|
||||
try:
|
||||
async with ledger.db.get_connection(
|
||||
lock_table="mint_quotes",
|
||||
lock_select_statement=f"quote='{invoice2.id}'",
|
||||
lock_select_statement=f"quote='{mint_quote_2.quote}'",
|
||||
lock_timeout=0.1,
|
||||
) as conn2:
|
||||
# write something with conn1, this time we should reach this block with postgres
|
||||
quote = await ledger.crud.get_mint_quote(
|
||||
quote_id=invoice2.id, db=ledger.db, conn=conn2
|
||||
quote_id=mint_quote_2.quote, db=ledger.db, conn=conn2
|
||||
)
|
||||
assert quote is not None
|
||||
quote.amount = 100
|
||||
@@ -294,10 +294,10 @@ async def test_db_get_connection_lock_different_row(wallet: Wallet, ledger: Ledg
|
||||
@pytest.mark.asyncio
|
||||
async def test_db_lock_table(wallet: Wallet, ledger: Ledger):
|
||||
# fill wallet
|
||||
invoice = await wallet.request_mint(64)
|
||||
await pay_if_regtest(invoice.bolt11)
|
||||
mint_quote = await wallet.request_mint(64)
|
||||
await pay_if_regtest(mint_quote.request)
|
||||
|
||||
await wallet.mint(64, id=invoice.id)
|
||||
await wallet.mint(64, quote_id=mint_quote.quote)
|
||||
assert wallet.balance == 64
|
||||
|
||||
async with ledger.db.connect(lock_table="proofs_pending", lock_timeout=0.1) as conn:
|
||||
|
||||
Reference in New Issue
Block a user