From 11903aed6c8d9a69b586896226fdb8f63a4d0ad8 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Wed, 4 Oct 2017 14:45:00 +0200 Subject: [PATCH] wallet: Wiring in invoice persistence into JSON-RPC and master Signed-off-by: Christian Decker --- lightningd/invoice.c | 29 +++++++++-------------------- lightningd/invoice.h | 7 ++----- lightningd/lightningd.c | 6 +++++- lightningd/test/run-find_my_path.c | 3 +++ 4 files changed, 19 insertions(+), 26 deletions(-) diff --git a/lightningd/invoice.c b/lightningd/invoice.c index d043a82f8..22f1262c6 100644 --- a/lightningd/invoice.c +++ b/lightningd/invoice.c @@ -56,20 +56,12 @@ static struct invoice *find_invoice_by_label(const struct list_head *list, } void invoice_add(struct invoices *invs, - const struct preimage *r, - u64 msatoshi, - const char *label, - enum invoice_status state) + struct invoice *inv) { + tal_steal(invs, inv); struct invoice *invoice = tal(invs, struct invoice); - - invoice->msatoshi = msatoshi; - invoice->r = *r; - invoice->state = state; - invoice->label = tal_strdup(invoice, label); sha256(&invoice->rhash, invoice->r.r, sizeof(invoice->r.r)); - - list_add(&invs->invlist, &invoice->list); + list_add(&invs->invlist, &inv->list); } struct invoices *invoices_init(const tal_t *ctx) @@ -96,12 +88,6 @@ static void tell_waiter(struct command *cmd, const struct invoice *paid) } /* UNIFICATION FIXME */ -void db_resolve_invoice(struct lightningd *ld, - const char *label); -bool db_new_invoice(struct lightningd *ld, - u64 msatoshi, - const char *label, - const struct preimage *r); bool db_remove_invoice(struct lightningd *ld, const char *label); void resolve_invoice(struct lightningd *ld, struct invoice *invoice) @@ -117,7 +103,7 @@ void resolve_invoice(struct lightningd *ld, struct invoice *invoice) list)) != NULL) tell_waiter(w->cmd, invoice); - db_resolve_invoice(ld, invoice->label); + wallet_invoice_save(ld->wallet, invoice); } static void json_invoice(struct command *cmd, @@ -138,6 +124,7 @@ static void json_invoice(struct command *cmd, } invoice = tal(cmd, struct invoice); + invoice->id = 0; invoice->state = UNPAID; if (r) { if (!hex_decode(buffer + r->start, r->end - r->start, @@ -178,11 +165,13 @@ static void json_invoice(struct command *cmd, return; } - if (!db_new_invoice(cmd->ld, invoice->msatoshi, invoice->label, - &invoice->r)) { + if (!wallet_invoice_save(cmd->ld->wallet, invoice)) { + printf("Could not save the invoice to the database: %s", + cmd->ld->wallet->db->err); command_fail(cmd, "database error"); return; } + /* OK, connect it to main state, respond with hash */ tal_steal(invs, invoice); list_add(&invs->invlist, &invoice->list); diff --git a/lightningd/invoice.h b/lightningd/invoice.h index 732095c85..ad998d919 100644 --- a/lightningd/invoice.h +++ b/lightningd/invoice.h @@ -29,11 +29,8 @@ struct invoice { #define INVOICE_MAX_LABEL_LEN 128 /* From database */ -void invoice_add(struct invoices *i, - const struct preimage *r, - u64 msatoshi, - const char *label, - enum invoice_status state); +void invoice_add(struct invoices *invs, + struct invoice *inv); void resolve_invoice(struct lightningd *ld, struct invoice *invoice); diff --git a/lightningd/lightningd.c b/lightningd/lightningd.c index baab26a33..bdae17e7d 100644 --- a/lightningd/lightningd.c +++ b/lightningd/lightningd.c @@ -276,6 +276,11 @@ int main(int argc, char *argv[]) /* FIXME: Load from peers. */ 0); + /* Load invoices from the database */ + if (!wallet_invoices_load(ld->wallet, ld->invoices)) { + err(1, "Could not load invoices from the database"); + } + /* Load peers from database */ wallet_channels_load_active(ld->wallet, &ld->peers); @@ -325,4 +330,3 @@ int main(int argc, char *argv[]) tal_free(log_book); return 0; } - diff --git a/lightningd/test/run-find_my_path.c b/lightningd/test/run-find_my_path.c index 14f4c7a5d..622f4f7ab 100644 --- a/lightningd/test/run-find_my_path.c +++ b/lightningd/test/run-find_my_path.c @@ -86,6 +86,9 @@ bool wallet_htlcs_reconnect(struct wallet *wallet UNNEEDED, struct htlc_in_map *htlcs_in UNNEEDED, struct htlc_out_map *htlcs_out UNNEEDED) { fprintf(stderr, "wallet_htlcs_reconnect called!\n"); abort(); } +/* Generated stub for wallet_invoices_load */ +bool wallet_invoices_load(struct wallet *wallet UNNEEDED, struct invoices *invs UNNEEDED) +{ fprintf(stderr, "wallet_invoices_load called!\n"); abort(); } /* Generated stub for wallet_new */ struct wallet *wallet_new(const tal_t *ctx UNNEEDED, struct log *log UNNEEDED) { fprintf(stderr, "wallet_new called!\n"); abort(); }