|
@ -1125,6 +1125,65 @@ bool wallet_htlcs_reconnect(struct wallet *wallet, |
|
|
return true; |
|
|
return true; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
int wallet_invoice_nextpaid(const tal_t *cxt, |
|
|
|
|
|
const struct wallet *wallet, |
|
|
|
|
|
const char *labelz, |
|
|
|
|
|
char **outlabel, |
|
|
|
|
|
struct sha256 *outrhash, |
|
|
|
|
|
u64 *outmsatoshi) |
|
|
|
|
|
{ |
|
|
|
|
|
sqlite3_stmt *stmt; |
|
|
|
|
|
int res; |
|
|
|
|
|
u64 pay_index; |
|
|
|
|
|
|
|
|
|
|
|
/* Generate query. */ |
|
|
|
|
|
if (labelz) { |
|
|
|
|
|
/* Find label. */ |
|
|
|
|
|
stmt = db_prepare(wallet->db, |
|
|
|
|
|
"SELECT pay_index FROM invoices WHERE label=?;"); |
|
|
|
|
|
sqlite3_bind_text(stmt, 1, labelz, strlen(labelz), SQLITE_TRANSIENT); |
|
|
|
|
|
res = sqlite3_step(stmt); |
|
|
|
|
|
if (res != SQLITE_ROW) { |
|
|
|
|
|
sqlite3_finalize(stmt); |
|
|
|
|
|
return -1; |
|
|
|
|
|
} |
|
|
|
|
|
pay_index = sqlite3_column_int64(stmt, 0); |
|
|
|
|
|
sqlite3_finalize(stmt); |
|
|
|
|
|
|
|
|
|
|
|
stmt = db_prepare(wallet->db, |
|
|
|
|
|
"SELECT label, payment_hash, msatoshi FROM invoices" |
|
|
|
|
|
" WHERE pay_index NOT NULL" |
|
|
|
|
|
" AND pay_index > ?" |
|
|
|
|
|
" ORDER BY pay_index ASC LIMIT 1;"); |
|
|
|
|
|
sqlite3_bind_int64(stmt, 1, pay_index); |
|
|
|
|
|
} else { |
|
|
|
|
|
stmt = db_prepare(wallet->db, |
|
|
|
|
|
"SELECT label, payment_hash, msatoshi FROM invoices" |
|
|
|
|
|
" WHERE pay_index NOT NULL" |
|
|
|
|
|
" ORDER BY pay_index ASC LIMIT 1;"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
res = sqlite3_step(stmt); |
|
|
|
|
|
if (res != SQLITE_ROW) { |
|
|
|
|
|
/* No paid invoice found. */ |
|
|
|
|
|
sqlite3_finalize(stmt); |
|
|
|
|
|
return 0; |
|
|
|
|
|
} 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); |
|
|
|
|
|
|
|
|
|
|
|
sqlite3_finalize(stmt); |
|
|
|
|
|
return 1; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return -1; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
/* Acquire the next pay_index. */ |
|
|
/* Acquire the next pay_index. */ |
|
|
static s64 wallet_invoice_next_pay_index(struct db *db) |
|
|
static s64 wallet_invoice_next_pay_index(struct db *db) |
|
|
{ |
|
|
{ |
|
|