mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-01-19 05:44:12 +01:00
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:
parent
a66200832a
commit
cce432b77f
@ -411,12 +411,8 @@ static void json_waitanyinvoice(struct command *cmd,
|
|||||||
u64 pay_index;
|
u64 pay_index;
|
||||||
struct invoice_waiter *w;
|
struct invoice_waiter *w;
|
||||||
struct invoices *invs = cmd->ld->invoices;
|
struct invoices *invs = cmd->ld->invoices;
|
||||||
bool res;
|
|
||||||
struct wallet *wallet = cmd->ld->wallet;
|
struct wallet *wallet = cmd->ld->wallet;
|
||||||
char* outlabel;
|
struct invoice *inv;
|
||||||
struct sha256 outrhash;
|
|
||||||
u64 outmsatoshi;
|
|
||||||
u64 outpay_index;
|
|
||||||
struct json_result *response;
|
struct json_result *response;
|
||||||
|
|
||||||
if (!json_get_params(buffer, params,
|
if (!json_get_params(buffer, params,
|
||||||
@ -438,25 +434,24 @@ static void json_waitanyinvoice(struct command *cmd,
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Find next paid invoice. */
|
/* Find next paid invoice. */
|
||||||
res = wallet_invoice_nextpaid(cmd, wallet, pay_index,
|
inv = wallet_invoice_nextpaid(cmd, wallet, pay_index);
|
||||||
&outlabel, &outrhash,
|
|
||||||
&outmsatoshi, &outpay_index);
|
|
||||||
|
|
||||||
/* If we found one, return it. */
|
/* If we found one, return it. */
|
||||||
if (res) {
|
if (inv) {
|
||||||
response = new_json_result(cmd);
|
response = new_json_result(cmd);
|
||||||
|
|
||||||
json_object_start(response, NULL);
|
json_object_start(response, NULL);
|
||||||
json_add_string(response, "label", outlabel);
|
json_add_string(response, "label", inv->label);
|
||||||
json_add_hex(response, "rhash", &outrhash, sizeof(outrhash));
|
json_add_hex(response, "rhash", &inv->rhash, sizeof(inv->rhash));
|
||||||
json_add_u64(response, "msatoshi", outmsatoshi);
|
if (inv->msatoshi)
|
||||||
|
json_add_u64(response, "msatoshi", *inv->msatoshi);
|
||||||
json_add_bool(response, "complete", true);
|
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);
|
json_object_end(response);
|
||||||
|
|
||||||
command_success(cmd, 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. */
|
* also frees cmd. */
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -1174,20 +1174,18 @@ static void wallet_stmt2invoice(sqlite3_stmt *stmt, struct invoice *inv)
|
|||||||
list_head_init(&inv->waitone_waiters);
|
list_head_init(&inv->waitone_waiters);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wallet_invoice_nextpaid(const tal_t *cxt,
|
struct invoice *wallet_invoice_nextpaid(const tal_t *ctx,
|
||||||
const struct wallet *wallet,
|
const struct wallet *wallet,
|
||||||
u64 pay_index,
|
u64 pay_index)
|
||||||
char **outlabel,
|
|
||||||
struct sha256 *outrhash,
|
|
||||||
u64 *outmsatoshi,
|
|
||||||
u64 *outpay_index)
|
|
||||||
{
|
{
|
||||||
sqlite3_stmt *stmt;
|
sqlite3_stmt *stmt;
|
||||||
int res;
|
int res;
|
||||||
|
struct invoice *inv = tal(ctx, struct invoice);
|
||||||
|
|
||||||
/* Generate query. */
|
/* Generate query. */
|
||||||
stmt = db_prepare(wallet->db,
|
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"
|
" FROM invoices"
|
||||||
" WHERE pay_index NOT NULL"
|
" WHERE pay_index NOT NULL"
|
||||||
" AND pay_index > ?"
|
" AND pay_index > ?"
|
||||||
@ -1198,20 +1196,12 @@ bool wallet_invoice_nextpaid(const tal_t *cxt,
|
|||||||
if (res != SQLITE_ROW) {
|
if (res != SQLITE_ROW) {
|
||||||
/* No paid invoice found. */
|
/* No paid invoice found. */
|
||||||
sqlite3_finalize(stmt);
|
sqlite3_finalize(stmt);
|
||||||
return false;
|
return tal_free(inv);
|
||||||
} else {
|
} else {
|
||||||
/* Paid invoice found, return data. */
|
wallet_stmt2invoice(stmt, inv);
|
||||||
*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);
|
|
||||||
|
|
||||||
sqlite3_finalize(stmt);
|
sqlite3_finalize(stmt);
|
||||||
return true;
|
return inv;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -346,9 +346,8 @@ bool wallet_htlcs_reconnect(struct wallet *wallet,
|
|||||||
/**
|
/**
|
||||||
* wallet_invoice_nextpaid -- Find a paid invoice.
|
* wallet_invoice_nextpaid -- Find a paid invoice.
|
||||||
*
|
*
|
||||||
* Get the details (label, rhash, msatoshi, pay_index) of the first paid
|
* Get the first paid invoice greater than the given pay_index. Return NULL
|
||||||
* invoice greater than the given pay_index. Return false if no paid
|
* if no paid invoice found. The first ever paid invoice will
|
||||||
* invoice found, return true if found. The first ever paid invoice will
|
|
||||||
* have a pay_index of 1 or greater, so giving a pay_index of 0 will get
|
* 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.
|
* the first ever paid invoice if there is one.
|
||||||
*
|
*
|
||||||
@ -356,19 +355,10 @@ bool wallet_htlcs_reconnect(struct wallet *wallet,
|
|||||||
* @wallet: Wallet to query
|
* @wallet: Wallet to query
|
||||||
* @pay_index: The paid invoice returned will have pay_index greater
|
* @pay_index: The paid invoice returned will have pay_index greater
|
||||||
* than this argument.
|
* 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,
|
struct invoice *wallet_invoice_nextpaid(const tal_t *ctx,
|
||||||
const struct wallet *wallet,
|
const struct wallet *wallet,
|
||||||
u64 pay_index,
|
u64 pay_index);
|
||||||
char **outlabel,
|
|
||||||
struct sha256 *outrhash,
|
|
||||||
u64 *outmsatoshi,
|
|
||||||
u64 *outpay_index);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* wallet_invoice_save -- Save/update an invoice to the wallet
|
* wallet_invoice_save -- Save/update an invoice to the wallet
|
||||||
|
Loading…
Reference in New Issue
Block a user