From ca4d03aa2a2c494f772300f919675f7546a3d478 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 4 Jan 2017 13:18:47 +1030 Subject: [PATCH] lightningd_state: move invoices into separate structure. It's all maintained by invoice.c, so make it private to that. Signed-off-by: Rusty Russell --- daemon/db.c | 2 +- daemon/invoice.c | 85 ++++++++++++++++++++++++++++----------------- daemon/invoice.h | 11 +++--- daemon/lightningd.c | 6 ++-- daemon/lightningd.h | 7 ++-- daemon/peer.c | 2 +- 6 files changed, 66 insertions(+), 47 deletions(-) diff --git a/daemon/db.c b/daemon/db.c index 1a3b7b615..87fc5c987 100644 --- a/daemon/db.c +++ b/daemon/db.c @@ -1131,7 +1131,7 @@ static void db_load_invoice(struct lightningd_state *dstate) msatoshi = sqlite3_column_int64(stmt, 1); label = (const char *)sqlite3_column_text(stmt, 2); paid_num = sqlite3_column_int64(stmt, 3); - invoice_add(dstate, &r, msatoshi, label, paid_num); + invoice_add(dstate->invoices, &r, msatoshi, label, paid_num); } tal_free(ctx); } diff --git a/daemon/invoice.c b/daemon/invoice.c index 2c3c4eddb..590bccf21 100644 --- a/daemon/invoice.c +++ b/daemon/invoice.c @@ -13,6 +13,14 @@ struct invoice_waiter { struct command *cmd; }; +struct invoices { + /* Payments for r values we know about. */ + struct list_head paid, unpaid; + u64 invoices_completed; + /* Waiting for new invoices to be paid. */ + struct list_head invoice_waiters; +}; + static struct invoice *find_inv(const struct list_head *list, const struct sha256 *rhash) { @@ -25,16 +33,15 @@ static struct invoice *find_inv(const struct list_head *list, return NULL; } -struct invoice *find_unpaid(struct lightningd_state *dstate, - const struct sha256 *rhash) +struct invoice *find_unpaid(struct invoices *invs, const struct sha256 *rhash) { - return find_inv(&dstate->unpaid, rhash); + return find_inv(&invs->unpaid, rhash); } -static struct invoice *find_paid(struct lightningd_state *dstate, +static struct invoice *find_paid(struct invoices *invs, const struct sha256 *rhash) { - return find_inv(&dstate->paid, rhash); + return find_inv(&invs->paid, rhash); } static struct invoice *find_invoice_by_label(const struct list_head *list, @@ -49,13 +56,13 @@ static struct invoice *find_invoice_by_label(const struct list_head *list, return NULL; } -void invoice_add(struct lightningd_state *dstate, +void invoice_add(struct invoices *invs, const struct rval *r, u64 msatoshi, const char *label, u64 paid_num) { - struct invoice *invoice = tal(dstate, struct invoice); + struct invoice *invoice = tal(invs, struct invoice); invoice->msatoshi = msatoshi; invoice->r = *r; @@ -64,11 +71,23 @@ void invoice_add(struct lightningd_state *dstate, sha256(&invoice->rhash, invoice->r.r, sizeof(invoice->r.r)); if (paid_num) { - list_add(&dstate->paid, &invoice->list); - if (paid_num > dstate->invoices_completed) - dstate->invoices_completed = paid_num; + list_add(&invs->paid, &invoice->list); + if (paid_num > invs->invoices_completed) + invs->invoices_completed = paid_num; } else - list_add(&dstate->unpaid, &invoice->list); + list_add(&invs->unpaid, &invoice->list); +} + +struct invoices *invoices_init(struct lightningd_state *dstate) +{ + struct invoices *invs = tal(dstate, struct invoices); + + list_head_init(&invs->unpaid); + list_head_init(&invs->paid); + invs->invoices_completed = 0; + list_head_init(&invs->invoice_waiters); + + return invs; } static void tell_waiter(struct command *cmd, const struct invoice *paid) @@ -83,17 +102,17 @@ static void tell_waiter(struct command *cmd, const struct invoice *paid) command_success(cmd, response); } -void resolve_invoice(struct lightningd_state *dstate, - struct invoice *invoice) +void resolve_invoice(struct lightningd_state *dstate, struct invoice *invoice) { struct invoice_waiter *w; + struct invoices *invs = dstate->invoices; - invoice->paid_num = ++dstate->invoices_completed; - list_del_from(&dstate->unpaid, &invoice->list); - list_add_tail(&dstate->paid, &invoice->list); + invoice->paid_num = ++invs->invoices_completed; + list_del_from(&invs->unpaid, &invoice->list); + list_add_tail(&invs->paid, &invoice->list); /* Tell all the waiters about the new paid invoice */ - while ((w = list_pop(&dstate->invoice_waiters, + while ((w = list_pop(&invs->invoice_waiters, struct invoice_waiter, list)) != NULL) tell_waiter(w->cmd, invoice); @@ -107,6 +126,7 @@ static void json_invoice(struct command *cmd, struct invoice *invoice; jsmntok_t *msatoshi, *r, *label; struct json_result *response = new_json_result(cmd); + struct invoices *invs = cmd->dstate->invoices; if (!json_get_params(buffer, params, "amount", &msatoshi, @@ -129,8 +149,8 @@ static void json_invoice(struct command *cmd, randombytes_buf(invoice->r.r, sizeof(invoice->r.r)); sha256(&invoice->rhash, invoice->r.r, sizeof(invoice->r.r)); - if (find_unpaid(cmd->dstate, &invoice->rhash) - || find_paid(cmd->dstate, &invoice->rhash)) { + if (find_unpaid(invs, &invoice->rhash) + || find_paid(invs, &invoice->rhash)) { command_fail(cmd, "Duplicate r value '%s'", tal_hexstr(cmd, &invoice->rhash, sizeof(invoice->rhash))); @@ -147,8 +167,8 @@ static void json_invoice(struct command *cmd, invoice->label = tal_strndup(invoice, buffer + label->start, label->end - label->start); - if (find_invoice_by_label(&cmd->dstate->paid, invoice->label) - || find_invoice_by_label(&cmd->dstate->unpaid, invoice->label)) { + if (find_invoice_by_label(&invs->paid, invoice->label) + || find_invoice_by_label(&invs->unpaid, invoice->label)) { command_fail(cmd, "Duplicate label '%s'", invoice->label); return; } @@ -165,8 +185,8 @@ static void json_invoice(struct command *cmd, return; } /* OK, connect it to main state, respond with hash */ - tal_steal(cmd->dstate, invoice); - list_add(&cmd->dstate->unpaid, &invoice->list); + tal_steal(invs, invoice); + list_add(&invs->unpaid, &invoice->list); json_object_start(response, NULL); json_add_hex(response, "rhash", @@ -209,6 +229,7 @@ static void json_listinvoice(struct command *cmd, { jsmntok_t *label = NULL; struct json_result *response = new_json_result(cmd); + struct invoices *invs = cmd->dstate->invoices; if (!json_get_params(buffer, params, "?label", &label, @@ -219,8 +240,8 @@ static void json_listinvoice(struct command *cmd, json_array_start(response, NULL); - json_add_invoices(response, &cmd->dstate->paid, buffer, label); - json_add_invoices(response, &cmd->dstate->unpaid, buffer, label); + json_add_invoices(response, &invs->paid, buffer, label); + json_add_invoices(response, &invs->unpaid, buffer, label); json_array_end(response); command_success(cmd, response); } @@ -239,6 +260,7 @@ static void json_delinvoice(struct command *cmd, jsmntok_t *labeltok; struct json_result *response = new_json_result(cmd); const char *label; + struct invoices *invs = cmd->dstate->invoices; if (!json_get_params(buffer, params, "label", &labeltok, @@ -249,7 +271,7 @@ static void json_delinvoice(struct command *cmd, label = tal_strndup(cmd, buffer + labeltok->start, labeltok->end - labeltok->start); - i = find_invoice_by_label(&cmd->dstate->unpaid, label); + i = find_invoice_by_label(&invs->unpaid, label); if (!i) { command_fail(cmd, "Unknown invoice"); return; @@ -258,7 +280,7 @@ static void json_delinvoice(struct command *cmd, command_fail(cmd, "Database error"); return; } - list_del_from(&cmd->dstate->unpaid, &i->list); + list_del_from(&invs->unpaid, &i->list); json_object_start(response, NULL); json_add_string(response, "label", i->label); @@ -283,6 +305,7 @@ static void json_waitinvoice(struct command *cmd, jsmntok_t *labeltok; const char *label = NULL; struct invoice_waiter *w; + struct invoices *invs = cmd->dstate->invoices; if (!json_get_params(buffer, params, "?label", &labeltok, @@ -292,16 +315,16 @@ static void json_waitinvoice(struct command *cmd, } if (!labeltok) - i = list_top(&cmd->dstate->paid, struct invoice, list); + i = list_top(&invs->paid, struct invoice, list); else { label = tal_strndup(cmd, buffer + labeltok->start, labeltok->end - labeltok->start); - i = find_invoice_by_label(&cmd->dstate->paid, label); + i = find_invoice_by_label(&invs->paid, label); if (!i) { command_fail(cmd, "Label not found"); return; } - i = list_next(&cmd->dstate->paid, i, list); + i = list_next(&invs->paid, i, list); } /* If we found one, return it. */ @@ -314,7 +337,7 @@ static void json_waitinvoice(struct command *cmd, /* FIXME: Better to use io_wait directly? */ w = tal(cmd, struct invoice_waiter); w->cmd = cmd; - list_add_tail(&cmd->dstate->invoice_waiters, &w->list); + list_add_tail(&invs->invoice_waiters, &w->list); } const struct json_command waitinvoice_command = { diff --git a/daemon/invoice.h b/daemon/invoice.h index 0b574bc2c..8356c4c05 100644 --- a/daemon/invoice.h +++ b/daemon/invoice.h @@ -1,8 +1,9 @@ #ifndef LIGHTNING_DAEMON_INVOICE_H #define LIGHTNING_DAEMON_INVOICE_H #include "config.h" -#include "peer.h" +#include "protobuf_convert.h" +struct invoices; struct lightningd_state; struct invoice { @@ -17,16 +18,16 @@ struct invoice { #define INVOICE_MAX_LABEL_LEN 128 /* From database */ -void invoice_add(struct lightningd_state *dstate, +void invoice_add(struct invoices *i, const struct rval *r, u64 msatoshi, const char *label, u64 complete); -void resolve_invoice(struct lightningd_state *dstate, - struct invoice *invoice); +void resolve_invoice(struct lightningd_state *dstate, struct invoice *invoice); -struct invoice *find_unpaid(struct lightningd_state *dstate, +struct invoice *find_unpaid(struct invoices *i, const struct sha256 *rhash); +struct invoices *invoices_init(struct lightningd_state *dstate); #endif /* LIGHTNING_DAEMON_INVOICE_H */ diff --git a/daemon/lightningd.c b/daemon/lightningd.c index abde82c70..d02d6e8e1 100644 --- a/daemon/lightningd.c +++ b/daemon/lightningd.c @@ -2,6 +2,7 @@ #include "chaintopology.h" #include "configdir.h" #include "db.h" +#include "invoice.h" #include "irc_announce.h" #include "jsonrpc.h" #include "lightningd.h" @@ -359,10 +360,6 @@ static struct lightningd_state *lightningd_state(void) txowatch_hash_init(&dstate->txowatches); list_head_init(&dstate->bitcoin_req); list_head_init(&dstate->wallet); - list_head_init(&dstate->unpaid); - list_head_init(&dstate->paid); - dstate->invoices_completed = 0; - list_head_init(&dstate->invoice_waiters); list_head_init(&dstate->addresses); dstate->dev_never_routefail = false; dstate->dev_no_broadcast = false; @@ -372,6 +369,7 @@ static struct lightningd_state *lightningd_state(void) dstate->external_ip = NULL; dstate->announce = NULL; list_head_init(&dstate->broadcast_queue); + dstate->invoices = invoices_init(dstate); return dstate; } diff --git a/daemon/lightningd.h b/daemon/lightningd.h index 5c327b7ac..6eec6ea26 100644 --- a/daemon/lightningd.h +++ b/daemon/lightningd.h @@ -124,11 +124,8 @@ struct lightningd_state { /* Wallet addresses we maintain. */ struct list_head wallet; - /* Payments for r values we know about. */ - struct list_head paid, unpaid; - u64 invoices_completed; - /* Waiting for new invoices to be paid. */ - struct list_head invoice_waiters; + /* Maintained by invoices.c */ + struct invoices *invoices; /* All known nodes. */ struct node_map *nodes; diff --git a/daemon/peer.c b/daemon/peer.c index 808aee8dd..d149edad6 100644 --- a/daemon/peer.c +++ b/daemon/peer.c @@ -901,7 +901,7 @@ static void their_htlc_added(struct peer *peer, struct htlc *htlc, case ONION_END: if (only_dest) goto free_packet; - invoice = find_unpaid(peer->dstate, &htlc->rhash); + invoice = find_unpaid(peer->dstate->invoices, &htlc->rhash); if (!invoice) { log_unusual(peer->log, "No invoice for HTLC %"PRIu64, htlc->id);