diff --git a/lightningd/invoice.h b/lightningd/invoice.h index 012bb832a..d2fb8529a 100644 --- a/lightningd/invoice.h +++ b/lightningd/invoice.h @@ -6,6 +6,7 @@ struct amount_msat; struct htlc_set; +struct json_escape; struct lightningd; struct sha256; diff --git a/wallet/db.c b/wallet/db.c index 843d27bd9..5c9fc9499 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -963,6 +963,7 @@ static struct migration dbmigrations[] = { {SQL("CREATE TABLE runes_blacklist (start_index BIGINT, end_index BIGINT);"), NULL}, {SQL("ALTER TABLE channels ADD ignore_fee_limits INTEGER DEFAULT 0;"), NULL}, {NULL, migrate_initialize_wait_indexes}, + {SQL("ALTER TABLE invoices ADD updated_index BIGINT DEFAULT 0"), NULL}, }; /** diff --git a/wallet/invoices.c b/wallet/invoices.c index 4badea7d1..e46973c62 100644 --- a/wallet/invoices.c +++ b/wallet/invoices.c @@ -6,8 +6,8 @@ #include #include #include +#include #include -#include struct invoice_waiter { /* Is this waiter already triggered? */ @@ -665,3 +665,59 @@ struct invoice_details *invoices_get_details(const tal_t *ctx, tal_free(stmt); return details; } + +static u64 invoice_index_inc(struct lightningd *ld, + const enum invoice_status *state, + const struct json_escape *label, + const char *invstring, + const char *description, + enum wait_index idx) +{ + const char *invstrname; + + if (invstring && strstarts(invstring, "lni")) + invstrname = "bolt12"; + else + invstrname = "bolt11"; + + + return wait_index_increment(ld, WAIT_SUBSYSTEM_INVOICE, idx, + "status", state ? invoice_status_str(*state) : NULL, + /* We don't want to add more JSON escapes here! */ + "=label", label ? tal_fmt(tmpctx, "\"%s\"", label->s) : NULL, + invstrname, invstring, + "description", description, + NULL); +} + +void invoice_index_deleted(struct lightningd *ld, + enum invoice_status state, + const struct json_escape *label, + const char *invstring) +{ + assert(label); + assert(invstring); + invoice_index_inc(ld, &state, label, invstring, NULL, WAIT_INDEX_DELETED); +} + +/* Fortuntely, dbids start at 1, not 0! */ +u64 invoice_index_created(struct lightningd *ld, + enum invoice_status state, + const struct json_escape *label, + const char *invstring) +{ + assert(label); + assert(invstring); + + return invoice_index_inc(ld, &state, label, invstring, NULL, + WAIT_INDEX_CREATED); +} + +/* FIXME: We allow missing label here! :( */ +u64 invoice_index_update_status(struct lightningd *ld, + const struct json_escape *label, + enum invoice_status state) +{ + return invoice_index_inc(ld, &state, label, NULL, NULL, + WAIT_INDEX_UPDATED); +} diff --git a/wallet/invoices.h b/wallet/invoices.h index 2e39ebbca..1425c8e1c 100644 --- a/wallet/invoices.h +++ b/wallet/invoices.h @@ -3,6 +3,7 @@ #include "config.h" #include #include +#include struct amount_msat; struct db; @@ -221,4 +222,19 @@ struct invoice_details *invoices_get_details(const tal_t *ctx, struct invoices *invoices, u64 inv_dbid); +/* Returns the id to use for the new invoice, and increments it. */ +u64 invoice_index_created(struct lightningd *ld, + enum invoice_status state, + const struct json_escape *label, + const char *invstring); + +/* Returns the current updated_index, and increments it. */ +u64 invoice_index_update_status(struct lightningd *ld, + const struct json_escape *label, + enum invoice_status state); + +void invoice_index_deleted(struct lightningd *ld, + enum invoice_status state, + const struct json_escape *label, + const char *invstring); #endif /* LIGHTNING_WALLET_INVOICES_H */