From cce432b77fd85fa00bc47d5d1bd6b6cde72643eb Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Sat, 13 Jan 2018 21:42:49 +1030 Subject: [PATCH] wallet_invoice_nextpaid: return a struct invoice. This reuses the same code internally, and also now means that we deal correctly with "any" msatoshi invoices: the old code would a return 'msatoshi' of 0 in that case. Signed-off-by: Rusty Russell --- lightningd/invoice.c | 23 +++++++++-------------- wallet/wallet.c | 28 +++++++++------------------- wallet/wallet.h | 22 ++++++---------------- 3 files changed, 24 insertions(+), 49 deletions(-) diff --git a/lightningd/invoice.c b/lightningd/invoice.c index ccd4c1a3f..c8b98b464 100644 --- a/lightningd/invoice.c +++ b/lightningd/invoice.c @@ -411,12 +411,8 @@ static void json_waitanyinvoice(struct command *cmd, u64 pay_index; struct invoice_waiter *w; struct invoices *invs = cmd->ld->invoices; - bool res; struct wallet *wallet = cmd->ld->wallet; - char* outlabel; - struct sha256 outrhash; - u64 outmsatoshi; - u64 outpay_index; + struct invoice *inv; struct json_result *response; if (!json_get_params(buffer, params, @@ -438,25 +434,24 @@ static void json_waitanyinvoice(struct command *cmd, } /* Find next paid invoice. */ - res = wallet_invoice_nextpaid(cmd, wallet, pay_index, - &outlabel, &outrhash, - &outmsatoshi, &outpay_index); + inv = wallet_invoice_nextpaid(cmd, wallet, pay_index); /* If we found one, return it. */ - if (res) { + if (inv) { 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_string(response, "label", inv->label); + json_add_hex(response, "rhash", &inv->rhash, sizeof(inv->rhash)); + if (inv->msatoshi) + json_add_u64(response, "msatoshi", *inv->msatoshi); json_add_bool(response, "complete", true); - json_add_u64(response, "pay_index", outpay_index); + json_add_u64(response, "pay_index", inv->pay_index); json_object_end(response); command_success(cmd, response); - /* outlabel is freed when cmd is freed, and command_success + /* inv is freed when cmd is freed, and command_success * also frees cmd. */ return; } diff --git a/wallet/wallet.c b/wallet/wallet.c index 94e1ca887..0e34ef5ce 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -1174,20 +1174,18 @@ static void wallet_stmt2invoice(sqlite3_stmt *stmt, struct invoice *inv) list_head_init(&inv->waitone_waiters); } -bool wallet_invoice_nextpaid(const tal_t *cxt, - const struct wallet *wallet, - u64 pay_index, - char **outlabel, - struct sha256 *outrhash, - u64 *outmsatoshi, - u64 *outpay_index) +struct invoice *wallet_invoice_nextpaid(const tal_t *ctx, + const struct wallet *wallet, + u64 pay_index) { sqlite3_stmt *stmt; int res; + struct invoice *inv = tal(ctx, struct invoice); /* Generate query. */ stmt = db_prepare(wallet->db, - "SELECT label, payment_hash, msatoshi, pay_index" + "SELECT id, state, payment_key, payment_hash," + " label, msatoshi, expiry_time, pay_index " " FROM invoices" " WHERE pay_index NOT NULL" " AND pay_index > ?" @@ -1198,20 +1196,12 @@ bool wallet_invoice_nextpaid(const tal_t *cxt, if (res != SQLITE_ROW) { /* No paid invoice found. */ sqlite3_finalize(stmt); - return false; + return tal_free(inv); } else { - /* Paid invoice found, return data. */ - *outlabel = tal_strndup(cxt, sqlite3_column_blob(stmt, 0), sqlite3_column_bytes(stmt, 0)); - - assert(sqlite3_column_bytes(stmt, 1) == sizeof(struct sha256)); - memcpy(outrhash, sqlite3_column_blob(stmt, 1), sqlite3_column_bytes(stmt, 1)); - - *outmsatoshi = sqlite3_column_int64(stmt, 2); - - *outpay_index = sqlite3_column_int64(stmt, 3); + wallet_stmt2invoice(stmt, inv); sqlite3_finalize(stmt); - return true; + return inv; } } diff --git a/wallet/wallet.h b/wallet/wallet.h index bd902838b..f3cb8bacf 100644 --- a/wallet/wallet.h +++ b/wallet/wallet.h @@ -346,9 +346,8 @@ bool wallet_htlcs_reconnect(struct wallet *wallet, /** * wallet_invoice_nextpaid -- Find a paid invoice. * - * Get the details (label, rhash, msatoshi, pay_index) of the first paid - * invoice greater than the given pay_index. Return false if no paid - * invoice found, return true if found. The first ever paid invoice will + * Get the first paid invoice greater than the given pay_index. Return NULL + * if no paid invoice found. The first ever paid invoice will * have a pay_index of 1 or greater, so giving a pay_index of 0 will get * the first ever paid invoice if there is one. * @@ -356,19 +355,10 @@ bool wallet_htlcs_reconnect(struct wallet *wallet, * @wallet: Wallet to query * @pay_index: The paid invoice returned will have pay_index greater * than this argument. - * @outlabel: Pointer to label of found paid invoice. Caller - * must free if this function returns true. - * @outrhash: Pointer to struct rhash to be filled. - * @outmsatoshi: Pointer to number of millisatoshis value to pay. - * @outpay_index: Pointer to pay_index of found paid invoice. - */ -bool wallet_invoice_nextpaid(const tal_t *cxt, - const struct wallet *wallet, - u64 pay_index, - char **outlabel, - struct sha256 *outrhash, - u64 *outmsatoshi, - u64 *outpay_index); + */ +struct invoice *wallet_invoice_nextpaid(const tal_t *ctx, + const struct wallet *wallet, + u64 pay_index); /** * wallet_invoice_save -- Save/update an invoice to the wallet