wallet: hook up created_index for invoices.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2023-07-22 17:16:17 +09:30
parent 6326f500ba
commit 7b69e7e1fe
2 changed files with 46 additions and 5 deletions

View File

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

View File

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