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 <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2017-01-04 13:18:47 +10:30
parent 59ecb3dfea
commit ca4d03aa2a
6 changed files with 66 additions and 47 deletions

View File

@ -1131,7 +1131,7 @@ static void db_load_invoice(struct lightningd_state *dstate)
msatoshi = sqlite3_column_int64(stmt, 1); msatoshi = sqlite3_column_int64(stmt, 1);
label = (const char *)sqlite3_column_text(stmt, 2); label = (const char *)sqlite3_column_text(stmt, 2);
paid_num = sqlite3_column_int64(stmt, 3); 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); tal_free(ctx);
} }

View File

@ -13,6 +13,14 @@ struct invoice_waiter {
struct command *cmd; 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, static struct invoice *find_inv(const struct list_head *list,
const struct sha256 *rhash) const struct sha256 *rhash)
{ {
@ -25,16 +33,15 @@ static struct invoice *find_inv(const struct list_head *list,
return NULL; return NULL;
} }
struct invoice *find_unpaid(struct lightningd_state *dstate, struct invoice *find_unpaid(struct invoices *invs, const struct sha256 *rhash)
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) 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, 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; return NULL;
} }
void invoice_add(struct lightningd_state *dstate, void invoice_add(struct invoices *invs,
const struct rval *r, const struct rval *r,
u64 msatoshi, u64 msatoshi,
const char *label, const char *label,
u64 paid_num) u64 paid_num)
{ {
struct invoice *invoice = tal(dstate, struct invoice); struct invoice *invoice = tal(invs, struct invoice);
invoice->msatoshi = msatoshi; invoice->msatoshi = msatoshi;
invoice->r = *r; invoice->r = *r;
@ -64,11 +71,23 @@ void invoice_add(struct lightningd_state *dstate,
sha256(&invoice->rhash, invoice->r.r, sizeof(invoice->r.r)); sha256(&invoice->rhash, invoice->r.r, sizeof(invoice->r.r));
if (paid_num) { if (paid_num) {
list_add(&dstate->paid, &invoice->list); list_add(&invs->paid, &invoice->list);
if (paid_num > dstate->invoices_completed) if (paid_num > invs->invoices_completed)
dstate->invoices_completed = paid_num; invs->invoices_completed = paid_num;
} else } 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) 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); command_success(cmd, response);
} }
void resolve_invoice(struct lightningd_state *dstate, void resolve_invoice(struct lightningd_state *dstate, struct invoice *invoice)
struct invoice *invoice)
{ {
struct invoice_waiter *w; struct invoice_waiter *w;
struct invoices *invs = dstate->invoices;
invoice->paid_num = ++dstate->invoices_completed; invoice->paid_num = ++invs->invoices_completed;
list_del_from(&dstate->unpaid, &invoice->list); list_del_from(&invs->unpaid, &invoice->list);
list_add_tail(&dstate->paid, &invoice->list); list_add_tail(&invs->paid, &invoice->list);
/* Tell all the waiters about the new paid invoice */ /* 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, struct invoice_waiter,
list)) != NULL) list)) != NULL)
tell_waiter(w->cmd, invoice); tell_waiter(w->cmd, invoice);
@ -107,6 +126,7 @@ static void json_invoice(struct command *cmd,
struct invoice *invoice; struct invoice *invoice;
jsmntok_t *msatoshi, *r, *label; jsmntok_t *msatoshi, *r, *label;
struct json_result *response = new_json_result(cmd); struct json_result *response = new_json_result(cmd);
struct invoices *invs = cmd->dstate->invoices;
if (!json_get_params(buffer, params, if (!json_get_params(buffer, params,
"amount", &msatoshi, "amount", &msatoshi,
@ -129,8 +149,8 @@ static void json_invoice(struct command *cmd,
randombytes_buf(invoice->r.r, sizeof(invoice->r.r)); randombytes_buf(invoice->r.r, sizeof(invoice->r.r));
sha256(&invoice->rhash, invoice->r.r, sizeof(invoice->r.r)); sha256(&invoice->rhash, invoice->r.r, sizeof(invoice->r.r));
if (find_unpaid(cmd->dstate, &invoice->rhash) if (find_unpaid(invs, &invoice->rhash)
|| find_paid(cmd->dstate, &invoice->rhash)) { || find_paid(invs, &invoice->rhash)) {
command_fail(cmd, "Duplicate r value '%s'", command_fail(cmd, "Duplicate r value '%s'",
tal_hexstr(cmd, &invoice->rhash, tal_hexstr(cmd, &invoice->rhash,
sizeof(invoice->rhash))); sizeof(invoice->rhash)));
@ -147,8 +167,8 @@ static void json_invoice(struct command *cmd,
invoice->label = tal_strndup(invoice, buffer + label->start, invoice->label = tal_strndup(invoice, buffer + label->start,
label->end - label->start); label->end - label->start);
if (find_invoice_by_label(&cmd->dstate->paid, invoice->label) if (find_invoice_by_label(&invs->paid, invoice->label)
|| find_invoice_by_label(&cmd->dstate->unpaid, invoice->label)) { || find_invoice_by_label(&invs->unpaid, invoice->label)) {
command_fail(cmd, "Duplicate label '%s'", invoice->label); command_fail(cmd, "Duplicate label '%s'", invoice->label);
return; return;
} }
@ -165,8 +185,8 @@ static void json_invoice(struct command *cmd,
return; return;
} }
/* OK, connect it to main state, respond with hash */ /* OK, connect it to main state, respond with hash */
tal_steal(cmd->dstate, invoice); tal_steal(invs, invoice);
list_add(&cmd->dstate->unpaid, &invoice->list); list_add(&invs->unpaid, &invoice->list);
json_object_start(response, NULL); json_object_start(response, NULL);
json_add_hex(response, "rhash", json_add_hex(response, "rhash",
@ -209,6 +229,7 @@ static void json_listinvoice(struct command *cmd,
{ {
jsmntok_t *label = NULL; jsmntok_t *label = NULL;
struct json_result *response = new_json_result(cmd); struct json_result *response = new_json_result(cmd);
struct invoices *invs = cmd->dstate->invoices;
if (!json_get_params(buffer, params, if (!json_get_params(buffer, params,
"?label", &label, "?label", &label,
@ -219,8 +240,8 @@ static void json_listinvoice(struct command *cmd,
json_array_start(response, NULL); json_array_start(response, NULL);
json_add_invoices(response, &cmd->dstate->paid, buffer, label); json_add_invoices(response, &invs->paid, buffer, label);
json_add_invoices(response, &cmd->dstate->unpaid, buffer, label); json_add_invoices(response, &invs->unpaid, buffer, label);
json_array_end(response); json_array_end(response);
command_success(cmd, response); command_success(cmd, response);
} }
@ -239,6 +260,7 @@ static void json_delinvoice(struct command *cmd,
jsmntok_t *labeltok; jsmntok_t *labeltok;
struct json_result *response = new_json_result(cmd); struct json_result *response = new_json_result(cmd);
const char *label; const char *label;
struct invoices *invs = cmd->dstate->invoices;
if (!json_get_params(buffer, params, if (!json_get_params(buffer, params,
"label", &labeltok, "label", &labeltok,
@ -249,7 +271,7 @@ static void json_delinvoice(struct command *cmd,
label = tal_strndup(cmd, buffer + labeltok->start, label = tal_strndup(cmd, buffer + labeltok->start,
labeltok->end - 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) { if (!i) {
command_fail(cmd, "Unknown invoice"); command_fail(cmd, "Unknown invoice");
return; return;
@ -258,7 +280,7 @@ static void json_delinvoice(struct command *cmd,
command_fail(cmd, "Database error"); command_fail(cmd, "Database error");
return; return;
} }
list_del_from(&cmd->dstate->unpaid, &i->list); list_del_from(&invs->unpaid, &i->list);
json_object_start(response, NULL); json_object_start(response, NULL);
json_add_string(response, "label", i->label); json_add_string(response, "label", i->label);
@ -283,6 +305,7 @@ static void json_waitinvoice(struct command *cmd,
jsmntok_t *labeltok; jsmntok_t *labeltok;
const char *label = NULL; const char *label = NULL;
struct invoice_waiter *w; struct invoice_waiter *w;
struct invoices *invs = cmd->dstate->invoices;
if (!json_get_params(buffer, params, if (!json_get_params(buffer, params,
"?label", &labeltok, "?label", &labeltok,
@ -292,16 +315,16 @@ static void json_waitinvoice(struct command *cmd,
} }
if (!labeltok) if (!labeltok)
i = list_top(&cmd->dstate->paid, struct invoice, list); i = list_top(&invs->paid, struct invoice, list);
else { else {
label = tal_strndup(cmd, buffer + labeltok->start, label = tal_strndup(cmd, buffer + labeltok->start,
labeltok->end - 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) { if (!i) {
command_fail(cmd, "Label not found"); command_fail(cmd, "Label not found");
return; return;
} }
i = list_next(&cmd->dstate->paid, i, list); i = list_next(&invs->paid, i, list);
} }
/* If we found one, return it. */ /* If we found one, return it. */
@ -314,7 +337,7 @@ static void json_waitinvoice(struct command *cmd,
/* FIXME: Better to use io_wait directly? */ /* FIXME: Better to use io_wait directly? */
w = tal(cmd, struct invoice_waiter); w = tal(cmd, struct invoice_waiter);
w->cmd = cmd; 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 = { const struct json_command waitinvoice_command = {

View File

@ -1,8 +1,9 @@
#ifndef LIGHTNING_DAEMON_INVOICE_H #ifndef LIGHTNING_DAEMON_INVOICE_H
#define LIGHTNING_DAEMON_INVOICE_H #define LIGHTNING_DAEMON_INVOICE_H
#include "config.h" #include "config.h"
#include "peer.h" #include "protobuf_convert.h"
struct invoices;
struct lightningd_state; struct lightningd_state;
struct invoice { struct invoice {
@ -17,16 +18,16 @@ struct invoice {
#define INVOICE_MAX_LABEL_LEN 128 #define INVOICE_MAX_LABEL_LEN 128
/* From database */ /* From database */
void invoice_add(struct lightningd_state *dstate, void invoice_add(struct invoices *i,
const struct rval *r, const struct rval *r,
u64 msatoshi, u64 msatoshi,
const char *label, const char *label,
u64 complete); u64 complete);
void resolve_invoice(struct lightningd_state *dstate, void resolve_invoice(struct lightningd_state *dstate, struct invoice *invoice);
struct invoice *invoice);
struct invoice *find_unpaid(struct lightningd_state *dstate, struct invoice *find_unpaid(struct invoices *i,
const struct sha256 *rhash); const struct sha256 *rhash);
struct invoices *invoices_init(struct lightningd_state *dstate);
#endif /* LIGHTNING_DAEMON_INVOICE_H */ #endif /* LIGHTNING_DAEMON_INVOICE_H */

View File

@ -2,6 +2,7 @@
#include "chaintopology.h" #include "chaintopology.h"
#include "configdir.h" #include "configdir.h"
#include "db.h" #include "db.h"
#include "invoice.h"
#include "irc_announce.h" #include "irc_announce.h"
#include "jsonrpc.h" #include "jsonrpc.h"
#include "lightningd.h" #include "lightningd.h"
@ -359,10 +360,6 @@ static struct lightningd_state *lightningd_state(void)
txowatch_hash_init(&dstate->txowatches); txowatch_hash_init(&dstate->txowatches);
list_head_init(&dstate->bitcoin_req); list_head_init(&dstate->bitcoin_req);
list_head_init(&dstate->wallet); 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); list_head_init(&dstate->addresses);
dstate->dev_never_routefail = false; dstate->dev_never_routefail = false;
dstate->dev_no_broadcast = false; dstate->dev_no_broadcast = false;
@ -372,6 +369,7 @@ static struct lightningd_state *lightningd_state(void)
dstate->external_ip = NULL; dstate->external_ip = NULL;
dstate->announce = NULL; dstate->announce = NULL;
list_head_init(&dstate->broadcast_queue); list_head_init(&dstate->broadcast_queue);
dstate->invoices = invoices_init(dstate);
return dstate; return dstate;
} }

View File

@ -124,11 +124,8 @@ struct lightningd_state {
/* Wallet addresses we maintain. */ /* Wallet addresses we maintain. */
struct list_head wallet; struct list_head wallet;
/* Payments for r values we know about. */ /* Maintained by invoices.c */
struct list_head paid, unpaid; struct invoices *invoices;
u64 invoices_completed;
/* Waiting for new invoices to be paid. */
struct list_head invoice_waiters;
/* All known nodes. */ /* All known nodes. */
struct node_map *nodes; struct node_map *nodes;

View File

@ -901,7 +901,7 @@ static void their_htlc_added(struct peer *peer, struct htlc *htlc,
case ONION_END: case ONION_END:
if (only_dest) if (only_dest)
goto free_packet; goto free_packet;
invoice = find_unpaid(peer->dstate, &htlc->rhash); invoice = find_unpaid(peer->dstate->invoices, &htlc->rhash);
if (!invoice) { if (!invoice) {
log_unusual(peer->log, "No invoice for HTLC %"PRIu64, log_unusual(peer->log, "No invoice for HTLC %"PRIu64,
htlc->id); htlc->id);