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 <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2018-01-13 21:42:49 +10:30
parent a66200832a
commit cce432b77f
3 changed files with 23 additions and 48 deletions

View File

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

View File

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

View File

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