Browse Source

invoice: Use wallet_invoice_nextpaid in waitanyinvoice implementation.

Fixes: #442
ppa-0.6.1
ZmnSCPxj 7 years ago
committed by Rusty Russell
parent
commit
1de339eff6
  1. 47
      lightningd/invoice.c

47
lightningd/invoice.c

@ -346,11 +346,16 @@ AUTODATA(json_command, &delinvoice_command);
static void json_waitanyinvoice(struct command *cmd, static void json_waitanyinvoice(struct command *cmd,
const char *buffer, const jsmntok_t *params) const char *buffer, const jsmntok_t *params)
{ {
struct invoice *i;
jsmntok_t *labeltok; jsmntok_t *labeltok;
const char *label = NULL; const char *label = NULL;
struct invoice_waiter *w; struct invoice_waiter *w;
struct invoices *invs = cmd->ld->invoices; struct invoices *invs = cmd->ld->invoices;
int res;
struct wallet *wallet = cmd->ld->wallet;
char* outlabel;
struct sha256 outrhash;
u64 outmsatoshi;
struct json_result *response;
if (!json_get_params(buffer, params, if (!json_get_params(buffer, params,
"?label", &labeltok, "?label", &labeltok,
@ -360,30 +365,38 @@ static void json_waitanyinvoice(struct command *cmd,
} }
if (!labeltok) { if (!labeltok) {
i = list_top(&invs->invlist, struct invoice, list); label = NULL;
/* Advance until we find a PAID one */
while (i && i->state == UNPAID) {
i = list_next(&invs->invlist, i, list);
}
} else { } else {
label = tal_strndup(cmd, buffer + labeltok->start, label = tal_strndup(cmd, buffer + labeltok->start,
labeltok->end - labeltok->start); labeltok->end - labeltok->start);
i = find_invoice_by_label(invs, label); }
if (!i) {
/* Find next paid invoice. */
res = wallet_invoice_nextpaid(cmd, wallet, label,
&outlabel, &outrhash, &outmsatoshi);
label = tal_free(label);
/* If we failed to find label, error it. */
if (res == -1) {
command_fail(cmd, "Label not found"); command_fail(cmd, "Label not found");
return; return;
} }
/* Skip this particular invoice */
i = list_next(&invs->invlist, i, list);
while (i && i->state == UNPAID) {
i = list_next(&invs->invlist, i, list);
}
}
/* If we found one, return it. */ /* If we found one, return it. */
if (i) { if (res == 1) {
tell_waiter(cmd, i); response = new_json_result(cmd);
json_object_start(response, NULL);
json_add_string(response, "label", outlabel);
json_add_hex(response, "rhash", &outrhash, sizeof(outrhash));
json_add_u64(response, "msatoshi", outmsatoshi);
json_add_bool(response, "complete", true);
json_object_end(response);
command_success(cmd, response);
/* outlabel is freed when cmd is freed, and command_success
* also frees cmd. */
return; return;
} }

Loading…
Cancel
Save