mirror of
https://github.com/aljazceru/lightning.git
synced 2026-01-26 17:24:20 +01:00
wallet/invoice: remove indirection.
We can expose the dbid, rather than pretending we have some "struct invoice" which is actually just the dbid. And don't have a pile of "wallet_" wrappers for redirection. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
#include <db/common.h>
|
||||
#include <db/exec.h>
|
||||
#include <db/utils.h>
|
||||
#include <lightningd/invoice.h>
|
||||
#include <wallet/invoices.h>
|
||||
#include <wallet/wallet.h>
|
||||
|
||||
@@ -14,12 +15,12 @@ struct invoice_waiter {
|
||||
/* Is this waiting for any invoice to resolve? */
|
||||
bool any;
|
||||
/* If !any, the specific invoice this is waiting on */
|
||||
u64 id;
|
||||
u64 inv_dbid;
|
||||
|
||||
struct list_node list;
|
||||
|
||||
/* The callback to use */
|
||||
void (*cb)(const struct invoice *, void*);
|
||||
void (*cb)(const u64 *inv_dbid, void*);
|
||||
void *cbarg;
|
||||
};
|
||||
|
||||
@@ -37,41 +38,41 @@ struct invoices {
|
||||
};
|
||||
|
||||
static void trigger_invoice_waiter(struct invoice_waiter *w,
|
||||
const struct invoice *invoice)
|
||||
const u64 *inv_dbid)
|
||||
{
|
||||
w->triggered = true;
|
||||
w->cb(invoice, w->cbarg);
|
||||
w->cb(inv_dbid, w->cbarg);
|
||||
}
|
||||
|
||||
static void trigger_invoice_waiter_resolve(struct invoices *invoices,
|
||||
u64 id,
|
||||
const struct invoice *invoice)
|
||||
u64 inv_dbid)
|
||||
{
|
||||
struct invoice_waiter *w;
|
||||
struct invoice_waiter *n;
|
||||
|
||||
list_for_each_safe(&invoices->waiters, w, n, list) {
|
||||
if (!w->any && w->id != id)
|
||||
if (!w->any && w->inv_dbid != inv_dbid)
|
||||
continue;
|
||||
list_del_from(&invoices->waiters, &w->list);
|
||||
tal_steal(tmpctx, w);
|
||||
trigger_invoice_waiter(w, invoice);
|
||||
trigger_invoice_waiter(w, &inv_dbid);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
trigger_invoice_waiter_expire_or_delete(struct invoices *invoices,
|
||||
u64 id,
|
||||
const struct invoice *invoice)
|
||||
u64 inv_dbid,
|
||||
bool deleted)
|
||||
{
|
||||
struct invoice_waiter *w;
|
||||
struct invoice_waiter *n;
|
||||
|
||||
list_for_each_safe(&invoices->waiters, w, n, list) {
|
||||
if (w->any || w->id != id)
|
||||
if (w->any || w->inv_dbid != inv_dbid)
|
||||
continue;
|
||||
list_del_from(&invoices->waiters, &w->list);
|
||||
tal_steal(tmpctx, w);
|
||||
trigger_invoice_waiter(w, invoice);
|
||||
trigger_invoice_waiter(w, deleted ? NULL : &inv_dbid);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -150,7 +151,7 @@ struct invoices *invoices_new(const tal_t *ctx,
|
||||
|
||||
struct invoice_id_node {
|
||||
struct list_node list;
|
||||
u64 id;
|
||||
u64 inv_dbid;
|
||||
};
|
||||
|
||||
static void trigger_expiration(struct invoices *invoices)
|
||||
@@ -159,7 +160,6 @@ static void trigger_expiration(struct invoices *invoices)
|
||||
struct invoice_id_node *idn;
|
||||
u64 now = time_now().ts.tv_sec;
|
||||
struct db_stmt *stmt;
|
||||
struct invoice i;
|
||||
|
||||
/* Free current expiration timer */
|
||||
invoices->expiration_timer = tal_free(invoices->expiration_timer);
|
||||
@@ -177,7 +177,7 @@ static void trigger_expiration(struct invoices *invoices)
|
||||
while (db_step(stmt)) {
|
||||
idn = tal(tmpctx, struct invoice_id_node);
|
||||
list_add_tail(&idlist, &idn->list);
|
||||
idn->id = db_col_u64(stmt, "id");
|
||||
idn->inv_dbid = db_col_u64(stmt, "id");
|
||||
}
|
||||
tal_free(stmt);
|
||||
|
||||
@@ -187,8 +187,7 @@ static void trigger_expiration(struct invoices *invoices)
|
||||
/* Trigger expirations */
|
||||
list_for_each(&idlist, idn, list) {
|
||||
/* Trigger expiration */
|
||||
i.id = idn->id;
|
||||
trigger_invoice_waiter_expire_or_delete(invoices, idn->id, &i);
|
||||
trigger_invoice_waiter_expire_or_delete(invoices, idn->inv_dbid, false);
|
||||
}
|
||||
|
||||
install_expiration_timer(invoices);
|
||||
@@ -248,7 +247,7 @@ done:
|
||||
}
|
||||
|
||||
bool invoices_create(struct invoices *invoices,
|
||||
struct invoice *pinvoice,
|
||||
u64 *inv_dbid,
|
||||
const struct amount_msat *msat TAKES,
|
||||
const struct json_escape *label TAKES,
|
||||
u64 expiry,
|
||||
@@ -260,11 +259,10 @@ bool invoices_create(struct invoices *invoices,
|
||||
const struct sha256 *local_offer_id)
|
||||
{
|
||||
struct db_stmt *stmt;
|
||||
struct invoice dummy;
|
||||
u64 expiry_time;
|
||||
u64 now = time_now().ts.tv_sec;
|
||||
|
||||
if (invoices_find_by_label(invoices, &dummy, label)) {
|
||||
if (invoices_find_by_label(invoices, inv_dbid, label)) {
|
||||
if (taken(msat))
|
||||
tal_free(msat);
|
||||
if (taken(label))
|
||||
@@ -310,7 +308,7 @@ bool invoices_create(struct invoices *invoices,
|
||||
|
||||
db_exec_prepared_v2(stmt);
|
||||
|
||||
pinvoice->id = db_last_insert_id_v2(take(stmt));
|
||||
*inv_dbid = db_last_insert_id_v2(take(stmt));
|
||||
|
||||
/* Install expiration trigger. */
|
||||
if (!invoices->expiration_timer ||
|
||||
@@ -327,9 +325,8 @@ bool invoices_create(struct invoices *invoices,
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool invoices_find_by_label(struct invoices *invoices,
|
||||
struct invoice *pinvoice,
|
||||
u64 *inv_dbid,
|
||||
const struct json_escape *label)
|
||||
{
|
||||
struct db_stmt *stmt;
|
||||
@@ -344,13 +341,13 @@ bool invoices_find_by_label(struct invoices *invoices,
|
||||
return false;
|
||||
}
|
||||
|
||||
pinvoice->id = db_col_u64(stmt, "id");
|
||||
*inv_dbid = db_col_u64(stmt, "id");
|
||||
tal_free(stmt);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool invoices_find_by_rhash(struct invoices *invoices,
|
||||
struct invoice *pinvoice,
|
||||
u64 *inv_dbid,
|
||||
const struct sha256 *rhash)
|
||||
{
|
||||
struct db_stmt *stmt;
|
||||
@@ -365,14 +362,14 @@ bool invoices_find_by_rhash(struct invoices *invoices,
|
||||
tal_free(stmt);
|
||||
return false;
|
||||
} else {
|
||||
pinvoice->id = db_col_u64(stmt, "id");
|
||||
*inv_dbid = db_col_u64(stmt, "id");
|
||||
tal_free(stmt);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
bool invoices_find_unpaid(struct invoices *invoices,
|
||||
struct invoice *pinvoice,
|
||||
u64 *inv_dbid,
|
||||
const struct sha256 *rhash)
|
||||
{
|
||||
struct db_stmt *stmt;
|
||||
@@ -388,20 +385,20 @@ bool invoices_find_unpaid(struct invoices *invoices,
|
||||
tal_free(stmt);
|
||||
return false;
|
||||
} else {
|
||||
pinvoice->id = db_col_u64(stmt, "id");
|
||||
*inv_dbid = db_col_u64(stmt, "id");
|
||||
tal_free(stmt);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
bool invoices_delete(struct invoices *invoices, struct invoice invoice)
|
||||
bool invoices_delete(struct invoices *invoices, u64 inv_dbid)
|
||||
{
|
||||
struct db_stmt *stmt;
|
||||
int changes;
|
||||
/* Delete from database. */
|
||||
stmt = db_prepare_v2(invoices->wallet->db,
|
||||
SQL("DELETE FROM invoices WHERE id=?;"));
|
||||
db_bind_u64(stmt, 0, invoice.id);
|
||||
db_bind_u64(stmt, 0, inv_dbid);
|
||||
db_exec_prepared_v2(stmt);
|
||||
|
||||
changes = db_count_changes(stmt);
|
||||
@@ -411,11 +408,11 @@ bool invoices_delete(struct invoices *invoices, struct invoice invoice)
|
||||
return false;
|
||||
}
|
||||
/* Tell all the waiters about the fact that it was deleted. */
|
||||
trigger_invoice_waiter_expire_or_delete(invoices, invoice.id, NULL);
|
||||
trigger_invoice_waiter_expire_or_delete(invoices, inv_dbid, true);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool invoices_delete_description(struct invoices *invoices, struct invoice invoice)
|
||||
bool invoices_delete_description(struct invoices *invoices, u64 inv_dbid)
|
||||
{
|
||||
struct db_stmt *stmt;
|
||||
int changes;
|
||||
@@ -423,7 +420,7 @@ bool invoices_delete_description(struct invoices *invoices, struct invoice invoi
|
||||
stmt = db_prepare_v2(invoices->wallet->db, SQL("UPDATE invoices"
|
||||
" SET description = NULL"
|
||||
" WHERE ID = ?;"));
|
||||
db_bind_u64(stmt, 0, invoice.id);
|
||||
db_bind_u64(stmt, 0, inv_dbid);
|
||||
db_exec_prepared_v2(stmt);
|
||||
|
||||
changes = db_count_changes(stmt);
|
||||
@@ -502,7 +499,8 @@ static s64 get_next_pay_index(struct db *db)
|
||||
return next_pay_index;
|
||||
}
|
||||
|
||||
static enum invoice_status invoice_get_status(struct invoices *invoices, struct invoice invoice)
|
||||
static enum invoice_status invoice_get_status(struct invoices *invoices,
|
||||
u64 inv_dbid)
|
||||
{
|
||||
struct db_stmt *stmt;
|
||||
enum invoice_status state;
|
||||
@@ -510,7 +508,7 @@ static enum invoice_status invoice_get_status(struct invoices *invoices, struct
|
||||
|
||||
stmt = db_prepare_v2(
|
||||
invoices->wallet->db, SQL("SELECT state FROM invoices WHERE id = ?;"));
|
||||
db_bind_u64(stmt, 0, invoice.id);
|
||||
db_bind_u64(stmt, 0, inv_dbid);
|
||||
db_query_prepared(stmt);
|
||||
|
||||
res = db_step(stmt);
|
||||
@@ -521,14 +519,14 @@ static enum invoice_status invoice_get_status(struct invoices *invoices, struct
|
||||
}
|
||||
|
||||
/* If there's an associated offer, mark it used. */
|
||||
static void maybe_mark_offer_used(struct db *db, struct invoice invoice)
|
||||
static void maybe_mark_offer_used(struct db *db, u64 inv_dbid)
|
||||
{
|
||||
struct db_stmt *stmt;
|
||||
struct sha256 local_offer_id;
|
||||
|
||||
stmt = db_prepare_v2(
|
||||
db, SQL("SELECT local_offer_id FROM invoices WHERE id = ?;"));
|
||||
db_bind_u64(stmt, 0, invoice.id);
|
||||
db_bind_u64(stmt, 0, inv_dbid);
|
||||
db_query_prepared(stmt);
|
||||
|
||||
db_step(stmt);
|
||||
@@ -543,13 +541,13 @@ static void maybe_mark_offer_used(struct db *db, struct invoice invoice)
|
||||
}
|
||||
|
||||
bool invoices_resolve(struct invoices *invoices,
|
||||
struct invoice invoice,
|
||||
u64 inv_dbid,
|
||||
struct amount_msat received)
|
||||
{
|
||||
struct db_stmt *stmt;
|
||||
s64 pay_index;
|
||||
u64 paid_timestamp;
|
||||
enum invoice_status state = invoice_get_status(invoices, invoice);
|
||||
enum invoice_status state = invoice_get_status(invoices, inv_dbid);
|
||||
|
||||
if (state != UNPAID)
|
||||
return false;
|
||||
@@ -569,13 +567,13 @@ bool invoices_resolve(struct invoices *invoices,
|
||||
db_bind_u64(stmt, 1, pay_index);
|
||||
db_bind_amount_msat(stmt, 2, &received);
|
||||
db_bind_u64(stmt, 3, paid_timestamp);
|
||||
db_bind_u64(stmt, 4, invoice.id);
|
||||
db_bind_u64(stmt, 4, inv_dbid);
|
||||
db_exec_prepared_v2(take(stmt));
|
||||
|
||||
maybe_mark_offer_used(invoices->wallet->db, invoice);
|
||||
maybe_mark_offer_used(invoices->wallet->db, inv_dbid);
|
||||
|
||||
/* Tell all the waiters about the paid invoice. */
|
||||
trigger_invoice_waiter_resolve(invoices, invoice.id, &invoice);
|
||||
trigger_invoice_waiter_resolve(invoices, inv_dbid);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -592,14 +590,14 @@ static void destroy_invoice_waiter(struct invoice_waiter *w)
|
||||
static void add_invoice_waiter(const tal_t *ctx,
|
||||
struct list_head *waiters,
|
||||
bool any,
|
||||
u64 id,
|
||||
void (*cb)(const struct invoice *, void*),
|
||||
u64 inv_dbid,
|
||||
void (*cb)(const u64 *, void*),
|
||||
void* cbarg)
|
||||
{
|
||||
struct invoice_waiter *w = tal(ctx, struct invoice_waiter);
|
||||
w->triggered = false;
|
||||
w->any = any;
|
||||
w->id = id;
|
||||
w->inv_dbid = inv_dbid;
|
||||
list_add_tail(waiters, &w->list);
|
||||
w->cb = cb;
|
||||
w->cbarg = cbarg;
|
||||
@@ -610,11 +608,10 @@ static void add_invoice_waiter(const tal_t *ctx,
|
||||
void invoices_waitany(const tal_t *ctx,
|
||||
struct invoices *invoices,
|
||||
u64 lastpay_index,
|
||||
void (*cb)(const struct invoice *, void*),
|
||||
void (*cb)(const u64 *, void*),
|
||||
void *cbarg)
|
||||
{
|
||||
struct db_stmt *stmt;
|
||||
struct invoice invoice;
|
||||
|
||||
/* Look for an already-paid invoice. */
|
||||
stmt = db_prepare_v2(invoices->wallet->db,
|
||||
@@ -627,9 +624,9 @@ void invoices_waitany(const tal_t *ctx,
|
||||
db_query_prepared(stmt);
|
||||
|
||||
if (db_step(stmt)) {
|
||||
invoice.id = db_col_u64(stmt, "id");
|
||||
u64 inv_dbid = db_col_u64(stmt, "id");
|
||||
|
||||
cb(&invoice, cbarg);
|
||||
cb(&inv_dbid, cbarg);
|
||||
} else {
|
||||
/* None found. */
|
||||
add_invoice_waiter(ctx, &invoices->waiters,
|
||||
@@ -641,27 +638,27 @@ void invoices_waitany(const tal_t *ctx,
|
||||
|
||||
void invoices_waitone(const tal_t *ctx,
|
||||
struct invoices *invoices,
|
||||
struct invoice invoice,
|
||||
void (*cb)(const struct invoice *, void*),
|
||||
u64 inv_dbid,
|
||||
void (*cb)(const u64 *, void*),
|
||||
void *cbarg)
|
||||
{
|
||||
enum invoice_status state;
|
||||
|
||||
state = invoice_get_status(invoices, invoice);
|
||||
state = invoice_get_status(invoices, inv_dbid);
|
||||
|
||||
if (state == PAID || state == EXPIRED) {
|
||||
cb(&invoice, cbarg);
|
||||
cb(&inv_dbid, cbarg);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Not yet paid. */
|
||||
add_invoice_waiter(ctx, &invoices->waiters,
|
||||
false, invoice.id, cb, cbarg);
|
||||
false, inv_dbid, cb, cbarg);
|
||||
}
|
||||
|
||||
struct invoice_details *invoices_get_details(const tal_t *ctx,
|
||||
struct invoices *invoices,
|
||||
struct invoice invoice)
|
||||
u64 inv_dbid)
|
||||
{
|
||||
struct db_stmt *stmt;
|
||||
bool res;
|
||||
@@ -683,7 +680,7 @@ struct invoice_details *invoices_get_details(const tal_t *ctx,
|
||||
", local_offer_id"
|
||||
" FROM invoices"
|
||||
" WHERE id = ?;"));
|
||||
db_bind_u64(stmt, 0, invoice.id);
|
||||
db_bind_u64(stmt, 0, inv_dbid);
|
||||
db_query_prepared(stmt);
|
||||
res = db_step(stmt);
|
||||
assert(res);
|
||||
|
||||
@@ -30,7 +30,7 @@ struct invoices *invoices_new(const tal_t *ctx,
|
||||
* invoices_create - Create a new invoice.
|
||||
*
|
||||
* @invoices - the invoice handler.
|
||||
* @pinvoice - pointer to location to load new invoice in.
|
||||
* @inv_dbid - pointer to location to put the invoice dbid in
|
||||
* @msat - the amount the invoice should have, or
|
||||
* NULL for any-amount invoices.
|
||||
* @label - the unique label for this invoice. Must be
|
||||
@@ -43,7 +43,7 @@ struct invoices *invoices_new(const tal_t *ctx,
|
||||
* FIXME: Fallback addresses
|
||||
*/
|
||||
bool invoices_create(struct invoices *invoices,
|
||||
struct invoice *pinvoice,
|
||||
u64 *inv_dbid,
|
||||
const struct amount_msat *msat TAKES,
|
||||
const struct json_escape *label TAKES,
|
||||
u64 expiry,
|
||||
@@ -58,14 +58,14 @@ bool invoices_create(struct invoices *invoices,
|
||||
* invoices_find_by_label - Search for an invoice by label
|
||||
*
|
||||
* @param invoices - the invoice handler.
|
||||
* @param pinvoice - pointer to location to load found invoice in.
|
||||
* @param inv_dbid - pointer to location to put the found dbid in
|
||||
* @param label - the label to search for.
|
||||
*
|
||||
* Returns false if no invoice with that label exists.
|
||||
* Returns true if found.
|
||||
*/
|
||||
bool invoices_find_by_label(struct invoices *invoices,
|
||||
struct invoice *pinvoice,
|
||||
u64 *inv_dbid,
|
||||
const struct json_escape *label);
|
||||
|
||||
/**
|
||||
@@ -73,14 +73,14 @@ bool invoices_find_by_label(struct invoices *invoices,
|
||||
* payment_hash
|
||||
*
|
||||
* @invoices - the invoice handler.
|
||||
* @pinvoice - pointer to location to load found invoice in.
|
||||
* @inv_dbid - pointer to location to put the found dbid in
|
||||
* @rhash - the payment_hash to search for.
|
||||
*
|
||||
* Returns false if no invoice with that rhash exists.
|
||||
* Returns true if found.
|
||||
*/
|
||||
bool invoices_find_by_rhash(struct invoices *invoices,
|
||||
struct invoice *pinvoice,
|
||||
u64 *inv_dbid,
|
||||
const struct sha256 *rhash);
|
||||
|
||||
/**
|
||||
@@ -88,37 +88,36 @@ bool invoices_find_by_rhash(struct invoices *invoices,
|
||||
* payment_hash
|
||||
*
|
||||
* @invoices - the invoice handler.
|
||||
* @pinvoice - pointer to location to load found invoice in.
|
||||
* @inv_dbid - pointer to location to load found invoice dbid in.
|
||||
* @rhash - the payment_hash to search for.
|
||||
*
|
||||
* Returns false if no unpaid invoice with that rhash exists.
|
||||
* Returns true if found.
|
||||
*/
|
||||
bool invoices_find_unpaid(struct invoices *invoices,
|
||||
struct invoice *pinvoice,
|
||||
u64 *inv_dbid,
|
||||
const struct sha256 *rhash);
|
||||
|
||||
/**
|
||||
* invoices_delete - Delete an invoice
|
||||
*
|
||||
* @invoices - the invoice handler.
|
||||
* @invoice - the invoice to delete.
|
||||
* @inv_dbid - the invoice to delete.
|
||||
*
|
||||
* Return false on failure.
|
||||
*/
|
||||
bool invoices_delete(struct invoices *invoices,
|
||||
struct invoice invoice);
|
||||
bool invoices_delete(struct invoices *invoices, u64 inv_dbid);
|
||||
|
||||
/**
|
||||
* invoices_delete_description - Remove description from an invoice
|
||||
*
|
||||
* @invoices - the invoice handler.
|
||||
* @invoice - the invoice to remove description from.
|
||||
* @inv_dbid - the invoice to remove description from.
|
||||
*
|
||||
* Return false on failure.
|
||||
*/
|
||||
bool invoices_delete_description(struct invoices *invoices,
|
||||
struct invoice invoice);
|
||||
u64 inv_dbid);
|
||||
|
||||
/**
|
||||
* invoices_delete_expired - Delete all expired invoices
|
||||
@@ -166,13 +165,13 @@ const struct invoice_details *invoices_iterator_deref(
|
||||
* invoices_resolve - Mark an invoice as paid
|
||||
*
|
||||
* @invoices - the invoice handler.
|
||||
* @invoice - the invoice to mark as paid.
|
||||
* @inv_dbid - the invoice to mark as paid.
|
||||
* @received - the actual amount received.
|
||||
*
|
||||
* If the invoice is not UNPAID, returns false.
|
||||
*/
|
||||
bool invoices_resolve(struct invoices *invoices,
|
||||
struct invoice invoice,
|
||||
u64 inv_dbid,
|
||||
struct amount_msat received);
|
||||
|
||||
/**
|
||||
@@ -187,12 +186,14 @@ bool invoices_resolve(struct invoices *invoices,
|
||||
* paid with pay_index greater than lastpay_index, this
|
||||
* is called immediately, otherwise it is called during
|
||||
* an invoices_resolve call.
|
||||
* If the invoice was deleted, the callback is given a NULL
|
||||
* first argument.
|
||||
* @cbarg - the callback data.
|
||||
*/
|
||||
void invoices_waitany(const tal_t *ctx,
|
||||
struct invoices *invoices,
|
||||
u64 lastpay_index,
|
||||
void (*cb)(const struct invoice *, void*),
|
||||
void (*cb)(const u64 *, void*),
|
||||
void *cbarg);
|
||||
|
||||
/**
|
||||
@@ -202,19 +203,19 @@ void invoices_waitany(const tal_t *ctx,
|
||||
* @ctx - the owner of the callback. If the owner is freed,
|
||||
* the callback is cancelled.
|
||||
* @invoices - the invoice handler,
|
||||
* @invoice - the invoice to wait on.
|
||||
* @inv_dbid - the invoice to wait on.
|
||||
* @cb - the callback to invoice. If invoice is already paid
|
||||
* or expired, this is called immediately, otherwise it is
|
||||
* called during an invoices_resolve or invoices_delete call.
|
||||
* If the invoice was deleted, the callback is given a NULL
|
||||
* invoice.
|
||||
* first argument (inv_dbid).
|
||||
* @cbarg - the callback data.
|
||||
*
|
||||
*/
|
||||
void invoices_waitone(const tal_t *ctx,
|
||||
struct invoices *invoices,
|
||||
struct invoice invoice,
|
||||
void (*cb)(const struct invoice *, void*),
|
||||
u64 inv_dbid,
|
||||
void (*cb)(const u64 *, void*),
|
||||
void *cbarg);
|
||||
|
||||
/**
|
||||
@@ -222,11 +223,11 @@ void invoices_waitone(const tal_t *ctx,
|
||||
*
|
||||
* @ctx - the owner of the label and msatoshi fields returned.
|
||||
* @invoices - the invoice handler,
|
||||
* @invoice - the invoice to get details on.
|
||||
* @inv_dbid - the invoice to get details on.
|
||||
* @return pointer to the invoice details allocated off of `ctx`.
|
||||
*/
|
||||
struct invoice_details *invoices_get_details(const tal_t *ctx,
|
||||
struct invoices *invoices,
|
||||
struct invoice invoice);
|
||||
u64 inv_dbid);
|
||||
|
||||
#endif /* LIGHTNING_WALLET_INVOICES_H */
|
||||
|
||||
@@ -238,84 +238,11 @@ void htlc_set_add(struct lightningd *ld UNNEEDED,
|
||||
struct amount_msat total_msat UNNEEDED,
|
||||
const struct secret *payment_secret UNNEEDED)
|
||||
{ fprintf(stderr, "htlc_set_add called!\n"); abort(); }
|
||||
/* Generated stub for invoices_create */
|
||||
bool invoices_create(struct invoices *invoices UNNEEDED,
|
||||
struct invoice *pinvoice UNNEEDED,
|
||||
const struct amount_msat *msat TAKES UNNEEDED,
|
||||
const struct json_escape *label TAKES UNNEEDED,
|
||||
u64 expiry UNNEEDED,
|
||||
const char *b11enc UNNEEDED,
|
||||
const char *description UNNEEDED,
|
||||
const u8 *features UNNEEDED,
|
||||
const struct preimage *r UNNEEDED,
|
||||
const struct sha256 *rhash UNNEEDED,
|
||||
const struct sha256 *local_offer_id UNNEEDED)
|
||||
{ fprintf(stderr, "invoices_create called!\n"); abort(); }
|
||||
/* Generated stub for invoices_delete */
|
||||
bool invoices_delete(struct invoices *invoices UNNEEDED,
|
||||
struct invoice invoice UNNEEDED)
|
||||
{ fprintf(stderr, "invoices_delete called!\n"); abort(); }
|
||||
/* Generated stub for invoices_delete_description */
|
||||
bool invoices_delete_description(struct invoices *invoices UNNEEDED,
|
||||
struct invoice invoice UNNEEDED)
|
||||
{ fprintf(stderr, "invoices_delete_description called!\n"); abort(); }
|
||||
/* Generated stub for invoices_delete_expired */
|
||||
void invoices_delete_expired(struct invoices *invoices UNNEEDED,
|
||||
u64 max_expiry_time UNNEEDED)
|
||||
{ fprintf(stderr, "invoices_delete_expired called!\n"); abort(); }
|
||||
/* Generated stub for invoices_find_by_label */
|
||||
bool invoices_find_by_label(struct invoices *invoices UNNEEDED,
|
||||
struct invoice *pinvoice UNNEEDED,
|
||||
const struct json_escape *label UNNEEDED)
|
||||
{ fprintf(stderr, "invoices_find_by_label called!\n"); abort(); }
|
||||
/* Generated stub for invoices_find_by_rhash */
|
||||
bool invoices_find_by_rhash(struct invoices *invoices UNNEEDED,
|
||||
struct invoice *pinvoice UNNEEDED,
|
||||
const struct sha256 *rhash UNNEEDED)
|
||||
{ fprintf(stderr, "invoices_find_by_rhash called!\n"); abort(); }
|
||||
/* Generated stub for invoices_find_unpaid */
|
||||
bool invoices_find_unpaid(struct invoices *invoices UNNEEDED,
|
||||
struct invoice *pinvoice UNNEEDED,
|
||||
const struct sha256 *rhash UNNEEDED)
|
||||
{ fprintf(stderr, "invoices_find_unpaid called!\n"); abort(); }
|
||||
/* Generated stub for invoices_get_details */
|
||||
struct invoice_details *invoices_get_details(const tal_t *ctx UNNEEDED,
|
||||
struct invoices *invoices UNNEEDED,
|
||||
struct invoice invoice UNNEEDED)
|
||||
{ fprintf(stderr, "invoices_get_details called!\n"); abort(); }
|
||||
/* Generated stub for invoices_iterate */
|
||||
bool invoices_iterate(struct invoices *invoices UNNEEDED,
|
||||
struct invoice_iterator *it UNNEEDED)
|
||||
{ fprintf(stderr, "invoices_iterate called!\n"); abort(); }
|
||||
/* Generated stub for invoices_iterator_deref */
|
||||
const struct invoice_details *invoices_iterator_deref(
|
||||
const tal_t *ctx UNNEEDED, struct invoices *invoices UNNEEDED,
|
||||
const struct invoice_iterator *it UNNEEDED)
|
||||
{ fprintf(stderr, "invoices_iterator_deref called!\n"); abort(); }
|
||||
/* Generated stub for invoices_new */
|
||||
struct invoices *invoices_new(const tal_t *ctx UNNEEDED,
|
||||
struct wallet *wallet UNNEEDED,
|
||||
struct timers *timers UNNEEDED)
|
||||
{ fprintf(stderr, "invoices_new called!\n"); abort(); }
|
||||
/* Generated stub for invoices_resolve */
|
||||
bool invoices_resolve(struct invoices *invoices UNNEEDED,
|
||||
struct invoice invoice UNNEEDED,
|
||||
struct amount_msat received UNNEEDED)
|
||||
{ fprintf(stderr, "invoices_resolve called!\n"); abort(); }
|
||||
/* Generated stub for invoices_waitany */
|
||||
void invoices_waitany(const tal_t *ctx UNNEEDED,
|
||||
struct invoices *invoices UNNEEDED,
|
||||
u64 lastpay_index UNNEEDED,
|
||||
void (*cb)(const struct invoice * UNNEEDED, void*) UNNEEDED,
|
||||
void *cbarg UNNEEDED)
|
||||
{ fprintf(stderr, "invoices_waitany called!\n"); abort(); }
|
||||
/* Generated stub for invoices_waitone */
|
||||
void invoices_waitone(const tal_t *ctx UNNEEDED,
|
||||
struct invoices *invoices UNNEEDED,
|
||||
struct invoice invoice UNNEEDED,
|
||||
void (*cb)(const struct invoice * UNNEEDED, void*) UNNEEDED,
|
||||
void *cbarg UNNEEDED)
|
||||
{ fprintf(stderr, "invoices_waitone called!\n"); abort(); }
|
||||
/* Generated stub for is_hsm_secret_encrypted */
|
||||
int is_hsm_secret_encrypted(const char *path UNNEEDED)
|
||||
{ fprintf(stderr, "is_hsm_secret_encrypted called!\n"); abort(); }
|
||||
|
||||
@@ -3044,93 +3044,6 @@ bool wallet_htlcs_load_out_for_channel(struct wallet *wallet,
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool wallet_invoice_create(struct wallet *wallet,
|
||||
struct invoice *pinvoice,
|
||||
const struct amount_msat *msat TAKES,
|
||||
const struct json_escape *label TAKES,
|
||||
u64 expiry,
|
||||
const char *b11enc,
|
||||
const char *description,
|
||||
const u8 *features,
|
||||
const struct preimage *r,
|
||||
const struct sha256 *rhash,
|
||||
const struct sha256 *local_offer_id)
|
||||
{
|
||||
return invoices_create(wallet->invoices, pinvoice, msat, label, expiry, b11enc, description, features, r, rhash, local_offer_id);
|
||||
}
|
||||
bool wallet_invoice_find_by_label(struct wallet *wallet,
|
||||
struct invoice *pinvoice,
|
||||
const struct json_escape *label)
|
||||
{
|
||||
return invoices_find_by_label(wallet->invoices, pinvoice, label);
|
||||
}
|
||||
bool wallet_invoice_find_by_rhash(struct wallet *wallet,
|
||||
struct invoice *pinvoice,
|
||||
const struct sha256 *rhash)
|
||||
{
|
||||
return invoices_find_by_rhash(wallet->invoices, pinvoice, rhash);
|
||||
}
|
||||
bool wallet_invoice_find_unpaid(struct wallet *wallet,
|
||||
struct invoice *pinvoice,
|
||||
const struct sha256 *rhash)
|
||||
{
|
||||
return invoices_find_unpaid(wallet->invoices, pinvoice, rhash);
|
||||
}
|
||||
bool wallet_invoice_delete(struct wallet *wallet,
|
||||
struct invoice invoice)
|
||||
{
|
||||
return invoices_delete(wallet->invoices, invoice);
|
||||
}
|
||||
bool wallet_invoice_delete_description(struct wallet *wallet,
|
||||
struct invoice invoice)
|
||||
{
|
||||
return invoices_delete_description(wallet->invoices, invoice);
|
||||
}
|
||||
void wallet_invoice_delete_expired(struct wallet *wallet, u64 e)
|
||||
{
|
||||
invoices_delete_expired(wallet->invoices, e);
|
||||
}
|
||||
bool wallet_invoice_iterate(struct wallet *wallet,
|
||||
struct invoice_iterator *it)
|
||||
{
|
||||
return invoices_iterate(wallet->invoices, it);
|
||||
}
|
||||
const struct invoice_details *
|
||||
wallet_invoice_iterator_deref(const tal_t *ctx, struct wallet *wallet,
|
||||
const struct invoice_iterator *it)
|
||||
{
|
||||
return invoices_iterator_deref(ctx, wallet->invoices, it);
|
||||
}
|
||||
bool wallet_invoice_resolve(struct wallet *wallet,
|
||||
struct invoice invoice,
|
||||
struct amount_msat msatoshi_received)
|
||||
{
|
||||
return invoices_resolve(wallet->invoices, invoice, msatoshi_received);
|
||||
}
|
||||
void wallet_invoice_waitany(const tal_t *ctx,
|
||||
struct wallet *wallet,
|
||||
u64 lastpay_index,
|
||||
void (*cb)(const struct invoice *, void*),
|
||||
void *cbarg)
|
||||
{
|
||||
invoices_waitany(ctx, wallet->invoices, lastpay_index, cb, cbarg);
|
||||
}
|
||||
void wallet_invoice_waitone(const tal_t *ctx,
|
||||
struct wallet *wallet,
|
||||
struct invoice invoice,
|
||||
void (*cb)(const struct invoice *, void*),
|
||||
void *cbarg)
|
||||
{
|
||||
invoices_waitone(ctx, wallet->invoices, invoice, cb, cbarg);
|
||||
}
|
||||
|
||||
struct invoice_details *wallet_invoice_details(const tal_t *ctx,
|
||||
struct wallet *wallet,
|
||||
struct invoice invoice)
|
||||
{
|
||||
return invoices_get_details(ctx, wallet->invoices, invoice);
|
||||
}
|
||||
|
||||
struct htlc_stub *wallet_htlc_stubs(const tal_t *ctx, struct wallet *wallet,
|
||||
struct channel *chan, u64 commit_num)
|
||||
{
|
||||
|
||||
249
wallet/wallet.h
249
wallet/wallet.h
@@ -15,6 +15,7 @@ struct amount_msat;
|
||||
struct invoices;
|
||||
struct channel;
|
||||
struct channel_inflight;
|
||||
struct json_escape;
|
||||
struct lightningd;
|
||||
struct node_id;
|
||||
struct oneshot;
|
||||
@@ -62,6 +63,13 @@ static inline enum output_status output_status_in_db(enum output_status s)
|
||||
fatal("%s: %u is invalid", __func__, s);
|
||||
}
|
||||
|
||||
/* An object that handles iteration over the set of invoices */
|
||||
struct invoice_iterator {
|
||||
/* The contents of this object is subject to change
|
||||
* and should not be depended upon */
|
||||
void *p;
|
||||
};
|
||||
|
||||
/* Enumeration of all known output types. These include all types that
|
||||
* could ever end up on-chain and we may need to react upon. Notice
|
||||
* that `to_local`, `htlc_offer`, and `htlc_recv` may need immediate
|
||||
@@ -830,247 +838,6 @@ static inline enum invoice_status invoice_status_in_db(enum invoice_status s)
|
||||
fatal("%s: %u is invalid", __func__, s);
|
||||
}
|
||||
|
||||
/* The information about an invoice */
|
||||
struct invoice_details {
|
||||
/* Current invoice state */
|
||||
enum invoice_status state;
|
||||
/* Preimage for this invoice */
|
||||
struct preimage r;
|
||||
/* Hash of preimage r */
|
||||
struct sha256 rhash;
|
||||
/* Label assigned by user */
|
||||
const struct json_escape *label;
|
||||
/* NULL if they specified "any" */
|
||||
struct amount_msat *msat;
|
||||
/* Absolute UNIX epoch time this will expire */
|
||||
u64 expiry_time;
|
||||
/* Set if state == PAID; order to be returned by waitanyinvoice */
|
||||
u64 pay_index;
|
||||
/* Set if state == PAID; amount received */
|
||||
struct amount_msat received;
|
||||
/* Set if state == PAID; time paid */
|
||||
u64 paid_timestamp;
|
||||
/* BOLT11 or BOLT12 encoding for this invoice */
|
||||
const char *invstring;
|
||||
|
||||
/* The description of the payment. */
|
||||
char *description;
|
||||
/* The features, if any (tal_arr) */
|
||||
u8 *features;
|
||||
/* The offer this refers to, if any. */
|
||||
struct sha256 *local_offer_id;
|
||||
};
|
||||
|
||||
/* An object that handles iteration over the set of invoices */
|
||||
struct invoice_iterator {
|
||||
/* The contents of this object is subject to change
|
||||
* and should not be depended upon */
|
||||
void *p;
|
||||
};
|
||||
|
||||
struct invoice {
|
||||
/* Internal, rest of lightningd should not use */
|
||||
/* Database ID */
|
||||
u64 id;
|
||||
};
|
||||
|
||||
#define INVOICE_MAX_LABEL_LEN 128
|
||||
|
||||
/**
|
||||
* wallet_invoice_create - Create a new invoice.
|
||||
*
|
||||
* @wallet - the wallet to create the invoice in.
|
||||
* @pinvoice - pointer to location to load new invoice in.
|
||||
* @msat - the amount the invoice should have, or
|
||||
* NULL for any-amount invoices.
|
||||
* @label - the unique label for this invoice. Must be
|
||||
* non-NULL.
|
||||
* @expiry - the number of seconds before the invoice
|
||||
* expires
|
||||
*
|
||||
* Returns false if label already exists or expiry is 0.
|
||||
* Returns true if created invoice.
|
||||
* FIXME: Fallback addresses
|
||||
*/
|
||||
bool wallet_invoice_create(struct wallet *wallet,
|
||||
struct invoice *pinvoice,
|
||||
const struct amount_msat *msat TAKES,
|
||||
const struct json_escape *label TAKES,
|
||||
u64 expiry,
|
||||
const char *b11enc,
|
||||
const char *description,
|
||||
const u8 *features,
|
||||
const struct preimage *r,
|
||||
const struct sha256 *rhash,
|
||||
const struct sha256 *local_offer_id);
|
||||
|
||||
/**
|
||||
* wallet_invoice_find_by_label - Search for an invoice by label
|
||||
*
|
||||
* @wallet - the wallet to search.
|
||||
* @pinvoice - pointer to location to load found invoice in.
|
||||
* @label - the label to search for.
|
||||
*
|
||||
* Returns false if no invoice with that label exists.
|
||||
* Returns true if found.
|
||||
*/
|
||||
bool wallet_invoice_find_by_label(struct wallet *wallet,
|
||||
struct invoice *pinvoice,
|
||||
const struct json_escape *label);
|
||||
|
||||
/**
|
||||
* wallet_invoice_find_by_rhash - Search for an invoice by payment_hash
|
||||
*
|
||||
* @wallet - the wallet to search.
|
||||
* @pinvoice - pointer to location to load found invoice in.
|
||||
* @rhash - the payment_hash to search for.
|
||||
*
|
||||
* Returns false if no invoice with that rhash exists.
|
||||
* Returns true if found.
|
||||
*/
|
||||
bool wallet_invoice_find_by_rhash(struct wallet *wallet,
|
||||
struct invoice *pinvoice,
|
||||
const struct sha256 *rhash);
|
||||
|
||||
/**
|
||||
* wallet_invoice_find_unpaid - Search for an unpaid, unexpired invoice by
|
||||
* payment_hash
|
||||
*
|
||||
* @wallet - the wallet to search.
|
||||
* @pinvoice - pointer to location to load found invoice in.
|
||||
* @rhash - the payment_hash to search for.
|
||||
*
|
||||
* Returns false if no unpaid invoice with that rhash exists.
|
||||
* Returns true if found.
|
||||
*/
|
||||
bool wallet_invoice_find_unpaid(struct wallet *wallet,
|
||||
struct invoice *pinvoice,
|
||||
const struct sha256 *rhash);
|
||||
|
||||
/**
|
||||
* wallet_invoice_delete - Delete an invoice
|
||||
*
|
||||
* @wallet - the wallet to delete the invoice from.
|
||||
* @invoice - the invoice to delete.
|
||||
*
|
||||
* Return false on failure.
|
||||
*/
|
||||
bool wallet_invoice_delete(struct wallet *wallet,
|
||||
struct invoice invoice);
|
||||
|
||||
bool wallet_invoice_delete_description(struct wallet *wallet,
|
||||
struct invoice invoice);
|
||||
|
||||
/**
|
||||
* wallet_invoice_delete_expired - Delete all expired invoices
|
||||
* with expiration time less than or equal to the given.
|
||||
*
|
||||
* @wallet - the wallet to delete invoices from.
|
||||
* @max_expiry_time - the maximum expiry time to delete.
|
||||
*/
|
||||
void wallet_invoice_delete_expired(struct wallet *wallet,
|
||||
u64 max_expiry_time);
|
||||
|
||||
|
||||
/**
|
||||
* wallet_invoice_iterate - Iterate over all existing invoices
|
||||
*
|
||||
* @wallet - the wallet whose invoices are to be iterated over.
|
||||
* @iterator - the iterator object to use.
|
||||
*
|
||||
* Return false at end-of-sequence, true if still iterating.
|
||||
* Usage:
|
||||
*
|
||||
* struct invoice_iterator it;
|
||||
* memset(&it, 0, sizeof(it))
|
||||
* while (wallet_invoice_iterate(wallet, &it)) {
|
||||
* ...
|
||||
* }
|
||||
*/
|
||||
bool wallet_invoice_iterate(struct wallet *wallet,
|
||||
struct invoice_iterator *it);
|
||||
|
||||
/**
|
||||
* wallet_invoice_iterator_deref - Read the details of the
|
||||
* invoice currently pointed to by the given iterator.
|
||||
*
|
||||
* @ctx - the owner of the label and msatoshi fields returned.
|
||||
* @wallet - the wallet whose invoices are to be iterated over.
|
||||
* @iterator - the iterator object to use.
|
||||
* @return pointer to the invoice details allocated off of `ctx`.
|
||||
*/
|
||||
const struct invoice_details *wallet_invoice_iterator_deref(const tal_t *ctx,
|
||||
struct wallet *wallet,
|
||||
const struct invoice_iterator *it);
|
||||
|
||||
/**
|
||||
* wallet_invoice_resolve - Mark an invoice as paid
|
||||
*
|
||||
* @wallet - the wallet containing the invoice.
|
||||
* @invoice - the invoice to mark as paid.
|
||||
* @received - the actual amount received.
|
||||
*
|
||||
* If the invoice is not UNPAID, returns false.
|
||||
*/
|
||||
bool wallet_invoice_resolve(struct wallet *wallet,
|
||||
struct invoice invoice,
|
||||
struct amount_msat received);
|
||||
|
||||
/**
|
||||
* wallet_invoice_waitany - Wait for any invoice to be paid.
|
||||
*
|
||||
* @ctx - the owner of the callback. If the owner is freed,
|
||||
* the callback is cancelled.
|
||||
* @wallet - the wallet to query.
|
||||
* @lastpay_index - wait for invoices after the specified
|
||||
* pay_index. Use 0 to wait for the first invoice.
|
||||
* @cb - the callback to invoke. If an invoice is already
|
||||
* paid with pay_index greater than lastpay_index, this
|
||||
* is called immediately, otherwise it is called during
|
||||
* an invoices_resolve call. Will never be given a NULL
|
||||
* pointer-to-invoice.
|
||||
* @cbarg - the callback data.
|
||||
*/
|
||||
void wallet_invoice_waitany(const tal_t *ctx,
|
||||
struct wallet *wallet,
|
||||
u64 lastpay_index,
|
||||
void (*cb)(const struct invoice *, void*),
|
||||
void *cbarg);
|
||||
|
||||
/**
|
||||
* wallet_invoice_waitone - Wait for a specific invoice to be paid,
|
||||
* deleted, or expired.
|
||||
*
|
||||
* @ctx - the owner of the callback. If the owner is freed,
|
||||
* the callback is cancelled.
|
||||
* @wallet - the wallet to query.
|
||||
* @invoice - the invoice to wait on.
|
||||
* @cb - the callback to invoice. If invoice is already paid
|
||||
* or expired, this is called immediately, otherwise it is
|
||||
* called during an invoices_resolve or invoices_delete call.
|
||||
* If the invoice was deleted, the callback is given a NULL
|
||||
* invoice.
|
||||
* @cbarg - the callback data.
|
||||
*
|
||||
*/
|
||||
void wallet_invoice_waitone(const tal_t *ctx,
|
||||
struct wallet *wallet,
|
||||
struct invoice invoice,
|
||||
void (*cb)(const struct invoice *, void*),
|
||||
void *cbarg);
|
||||
|
||||
/**
|
||||
* wallet_invoice_details - Get the invoice_details of an invoice.
|
||||
*
|
||||
* @ctx - the owner of the label and msatoshi fields returned.
|
||||
* @wallet - the wallet to query.
|
||||
* @invoice - the invoice to get details on.
|
||||
* @return pointer to the invoice details allocated off of `ctx`.
|
||||
*/
|
||||
struct invoice_details *wallet_invoice_details(const tal_t *ctx,
|
||||
struct wallet *wallet,
|
||||
struct invoice invoice);
|
||||
|
||||
/**
|
||||
* wallet_htlc_stubs - Retrieve HTLC stubs for the given channel
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user