From 7b69e7e1feccae2858d326d468b6d0274d9d5620 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Sat, 22 Jul 2023 17:16:17 +0930 Subject: [PATCH] wallet: hook up created_index for invoices. Signed-off-by: Rusty Russell --- tests/test_invoices.py | 41 ++++++++++++++++++++++++++++++++++++++++- wallet/invoices.c | 10 ++++++---- 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/tests/test_invoices.py b/tests/test_invoices.py index 9fbd7f2af..fd2481fa4 100644 --- a/tests/test_invoices.py +++ b/tests/test_invoices.py @@ -1,7 +1,7 @@ from fixtures import * # noqa: F401,F403 from fixtures import TEST_NETWORK from pyln.client import RpcError, Millisatoshi -from utils import only_one, wait_for, wait_channel_quiescent, mine_funding_to_announce +from utils import only_one, wait_for, wait_channel_quiescent, mine_funding_to_announce, TIMEOUT import os @@ -707,6 +707,45 @@ def test_listinvoices_filter(node_factory): assert len(r['invoices']) == 0 +def test_wait_invoices(node_factory, executor): + l1, l2 = node_factory.line_graph(2) + + # Asking for 0 gives us current index. + waitres = l2.rpc.call('wait', {'subsystem': 'invoices', 'indexname': 'created', 'nextvalue': 0}) + assert waitres == {'subsystem': 'invoices', + 'created': 0} + + # Now ask for 1. + waitfut = executor.submit(l2.rpc.call, 'wait', {'subsystem': 'invoices', 'indexname': 'created', 'nextvalue': 1}) + time.sleep(1) + + inv = l2.rpc.invoice(42, 'invlabel', 'invdesc') + waitres = waitfut.result(TIMEOUT) + assert waitres == {'subsystem': 'invoices', + 'created': 1, + 'details': {'label': 'invlabel', + 'bolt11': inv['bolt11'], + 'status': 'unpaid'}} + + # Second returns instantly, without any details. + waitres = l2.rpc.call('wait', {'subsystem': 'invoices', 'indexname': 'created', 'nextvalue': 1}) + assert waitres == {'subsystem': 'invoices', + 'created': 1} + + # Deleting correctly produces 2, not another 1! + l2.rpc.delinvoice('invlabel', 'unpaid') + + waitfut = executor.submit(l2.rpc.call, 'wait', {'subsystem': 'invoices', 'indexname': 'created', 'nextvalue': 2}) + time.sleep(1) + inv = l2.rpc.invoice(42, 'invlabel', 'invdesc2') + waitres = waitfut.result(TIMEOUT) + assert waitres == {'subsystem': 'invoices', + 'created': 2, + 'details': {'label': 'invlabel', + 'bolt11': inv['bolt11'], + 'status': 'unpaid'}} + + def test_invoice_deschash(node_factory, chainparams): l1, l2 = node_factory.line_graph(2) diff --git a/wallet/invoices.c b/wallet/invoices.c index e46973c62..c7514e549 100644 --- a/wallet/invoices.c +++ b/wallet/invoices.c @@ -273,19 +273,22 @@ bool invoices_create(struct invoices *invoices, /* Compute expiration. */ expiry_time = now + expiry; + *inv_dbid = invoice_index_created(invoices->wallet->ld, UNPAID, label, b11enc); + /* Save to database. */ stmt = db_prepare_v2( invoices->wallet->db, SQL("INSERT INTO invoices" - " ( payment_hash, payment_key, state" + " ( id, payment_hash, payment_key, state" " , msatoshi, label, expiry_time" " , pay_index, msatoshi_received" " , paid_timestamp, bolt11, description, features, local_offer_id)" - " VALUES ( ?, ?, ?" + " VALUES ( ?, ?, ?, ?" " , ?, ?, ?" " , NULL, NULL" " , NULL, ?, ?, ?, ?);")); + db_bind_u64(stmt, *inv_dbid); db_bind_sha256(stmt, rhash); db_bind_preimage(stmt, r); db_bind_int(stmt, UNPAID); @@ -307,8 +310,7 @@ bool invoices_create(struct invoices *invoices, db_bind_null(stmt); db_exec_prepared_v2(stmt); - - *inv_dbid = db_last_insert_id_v2(take(stmt)); + tal_free(stmt); /* Install expiration trigger. */ if (!invoices->expiration_timer ||