Skip to content

Commit 7d24364

Browse files
rustyrussellLagrang3
authored andcommitted
lightningd: don't do previous invoice checking in createinvoicerequest.
This is an undocumented interface, so we can just change it. Rename "recurrence_label" to the more general "label", now we don't require it to find previous payments. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent 6b851c9 commit 7d24364

4 files changed

Lines changed: 5 additions & 122 deletions

File tree

‎lightningd/offer.c‎

Lines changed: 1 addition & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -313,101 +313,6 @@ static const struct json_command enableoffer_command = {
313313
};
314314
AUTODATA(json_command, &enableoffer_command);
315315

316-
317-
/* We do some sanity checks now, since we're looking up prev payment anyway,
318-
* but our main purpose is to fill in prev_basetime tweak. */
319-
static struct command_result *prev_payment(struct command *cmd,
320-
const struct json_escape *label,
321-
const struct tlv_invoice_request *invreq,
322-
u64 **prev_basetime)
323-
{
324-
struct sha256 invreq_oid;
325-
u64 last_recurrence = UINT64_MAX;
326-
bool prev_unpaid = false;
327-
328-
invreq_offer_id(invreq, &invreq_oid);
329-
330-
for (struct db_stmt *stmt = payments_by_label(cmd->ld->wallet, label);
331-
stmt;
332-
stmt = payments_next(cmd->ld->wallet, stmt)) {
333-
const struct wallet_payment *payment;
334-
const struct tlv_invoice *inv;
335-
const char *fail;
336-
struct sha256 inv_oid;
337-
338-
payment = payment_get_details(tmpctx, stmt);
339-
if (!payment->invstring)
340-
continue;
341-
342-
inv = invoice_decode(tmpctx, payment->invstring,
343-
strlen(payment->invstring),
344-
NULL, chainparams, &fail);
345-
if (!inv)
346-
continue;
347-
348-
/* They can reuse labels across different offers. */
349-
invoice_offer_id(inv, &inv_oid);
350-
if (!sha256_eq(&inv_oid, &invreq_oid))
351-
continue;
352-
353-
/* Be paranoid, in case someone inserts their own
354-
* clashing label! */
355-
if (!inv->invreq_recurrence_counter)
356-
continue;
357-
358-
/* BOLT-recurrence #12:
359-
* - if `offer_recurrence_base` is present:
360-
* - MUST include `invreq_recurrence_start`
361-
* - MUST set `period_offset` to the period the sender wants for the
362-
* initial request
363-
* - MUST set `period_offset` to the same value on all following requests.
364-
*/
365-
if (inv->invreq_recurrence_start
366-
&& invreq->invreq_recurrence_start
367-
&& *inv->invreq_recurrence_start != *invreq->invreq_recurrence_start) {
368-
tal_free(stmt);
369-
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
370-
"recurrence_start was"
371-
" previously %u",
372-
*inv->invreq_recurrence_start);
373-
}
374-
375-
/* They should all have the same basetime */
376-
if (!*prev_basetime)
377-
*prev_basetime = tal_dup(cmd, u64, inv->invoice_recurrence_basetime);
378-
379-
/* Track highest one for better diagnostics */
380-
if (last_recurrence == UINT64_MAX
381-
|| last_recurrence < *inv->invreq_recurrence_counter) {
382-
last_recurrence = *inv->invreq_recurrence_counter;
383-
}
384-
385-
if (*inv->invreq_recurrence_counter == *invreq->invreq_recurrence_counter-1) {
386-
/* Got it! */
387-
if (payment->status == PAYMENT_COMPLETE) {
388-
tal_free(stmt);
389-
return NULL;
390-
} else
391-
prev_unpaid = true;
392-
}
393-
}
394-
395-
/* We found one, but it didn't succeed */
396-
if (prev_unpaid)
397-
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
398-
"previous invoice payment did not succeed");
399-
400-
/* We found one, but it was not the previus one */
401-
if (last_recurrence != UINT64_MAX)
402-
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
403-
"previous invoice has not been paid (last was %"PRIu64")",
404-
last_recurrence);
405-
406-
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
407-
"No previous payment attempted for this"
408-
" label and offer");
409-
}
410-
411316
/* FIXME(vincenzopalazzo): move this to comm/bolt12.h */
412317
static struct command_result *param_b12_invreq(struct command *cmd,
413318
const char *name,
@@ -483,7 +388,7 @@ static struct command_result *json_createinvoicerequest(struct command *cmd,
483388
if (!param_check(cmd, buffer, params,
484389
p_req("bolt12", param_b12_invreq, &invreq),
485390
p_req("savetodb", param_bool, &save),
486-
p_opt("recurrence_label", param_label, &label),
391+
p_opt("label", param_label, &label),
487392
p_opt_def("single_use", param_bool, &single_use, true),
488393
NULL))
489394
return command_param_failed();
@@ -493,21 +398,6 @@ static struct command_result *json_createinvoicerequest(struct command *cmd,
493398
else
494399
status = OFFER_MULTIPLE_USE_UNUSED;
495400

496-
/* If it's a recurring payment, we look for previous to copy basetime */
497-
if (invreq->invreq_recurrence_counter) {
498-
if (!label)
499-
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
500-
"Need payment label for recurring payments");
501-
502-
if (*invreq->invreq_recurrence_counter != 0) {
503-
struct command_result *err
504-
= prev_payment(cmd, label, invreq,
505-
&prev_basetime);
506-
if (err)
507-
return err;
508-
}
509-
}
510-
511401
/* If the payer_id is not our node id, we sanity check that it
512402
* correctly maps from invreq_metadata */
513403
if (!pubkey_eq(invreq->invreq_payer_id, &cmd->ld->our_pubkey)) {

‎plugins/fetchinvoice.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,7 +1096,7 @@ struct command_result *json_fetchinvoice(struct command *cmd,
10961096
json_add_string(req->js, "bolt12", invrequest_encode(tmpctx, invreq));
10971097
json_add_bool(req->js, "savetodb", false);
10981098
if (rec_label)
1099-
json_add_string(req->js, "recurrence_label", rec_label);
1099+
json_add_string(req->js, "label", rec_label);
11001100
return send_outreq(req);
11011101
}
11021102

@@ -1250,7 +1250,7 @@ struct command_result *json_cancelrecurringinvoice(struct command *cmd,
12501250
/* We don't want this is the database: that's only for ones we publish */
12511251
json_add_string(req->js, "bolt12", invrequest_encode(tmpctx, invreq));
12521252
json_add_bool(req->js, "savetodb", false);
1253-
json_add_string(req->js, "recurrence_label", rec_label);
1253+
json_add_string(req->js, "label", rec_label);
12541254
return send_outreq(req);
12551255
}
12561256

‎plugins/offers_offer.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ static struct command_result *call_createinvoicerequest(struct command *cmd,
607607
json_add_bool(req->js, "savetodb", true);
608608
json_add_bool(req->js, "single_use", single_use);
609609
if (label)
610-
json_add_string(req->js, "recurrence_label", label);
610+
json_add_string(req->js, "label", label);
611611
return send_outreq(req);
612612
}
613613

‎tests/test_pay.py‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4795,7 +4795,7 @@ def test_fetchinvoice_recurrence(node_factory, bitcoind):
47954795
assert period2['paywindow_end'] == period2['endtime']
47964796

47974797
# Can't request 2 before paying 1.
4798-
with pytest.raises(RpcError, match='previous invoice has not been paid'):
4798+
with pytest.raises(RpcError, match='Remote node sent failure message.*Previous invoice #1 status "unpaid"'):
47994799
l1.rpc.call('fetchinvoice', {'offer': offer3['bolt12'],
48004800
'recurrence_counter': 2,
48014801
'recurrence_label': 'test recurrence'})
@@ -7229,9 +7229,6 @@ def test_cancel_recurrence(node_factory):
72297229
with pytest.raises(RpcError, match="recurrence_counter: Must be non-zero"):
72307230
l1.rpc.cancelrecurringinvoice(offer['bolt12'], 0, 'test_cancel_recurrence')
72317231

7232-
with pytest.raises(RpcError, match="No previous payment attempted for this label and offer"):
7233-
l1.rpc.cancelrecurringinvoice(offer['bolt12'], 1, 'test_cancel_recurrence')
7234-
72357232
# Fetch and pay first one
72367233
ret = l1.rpc.fetchinvoice(offer=offer['bolt12'],
72377234
recurrence_counter=0,
@@ -7241,10 +7238,6 @@ def test_cancel_recurrence(node_factory):
72417238
decoded = l1.rpc.decode(m.group(1))
72427239
assert 'invreq_recurrence_cancel' not in decoded
72437240

7244-
# Cancel counter must be correct!
7245-
with pytest.raises(RpcError, match=r"previous invoice has not been paid \(last was 0\)"):
7246-
l1.rpc.cancelrecurringinvoice(offer['bolt12'], 2, 'test_cancel_recurrence')
7247-
72487241
# Cancel second one.
72497242
l1.rpc.cancelrecurringinvoice(offer=offer['bolt12'],
72507243
recurrence_counter=1,

0 commit comments

Comments
 (0)