Browse Source

invoices: Change iteration interface to be more abstract.

ppa-0.6.1
ZmnSCPxj 7 years ago
committed by Christian Decker
parent
commit
f05c86618c
  1. 8
      lightningd/invoice.c
  2. 18
      wallet/invoices.c
  3. 31
      wallet/invoices.h
  4. 10
      wallet/test/run-wallet.c
  5. 13
      wallet/wallet.c
  6. 37
      wallet/wallet.h

8
lightningd/invoice.c

@ -245,15 +245,15 @@ static void json_add_invoices(struct json_result *response,
const char *buffer, const jsmntok_t *label, const char *buffer, const jsmntok_t *label,
bool modern) bool modern)
{ {
const struct invoice *i; struct invoice_iterator it;
struct invoice_details details; struct invoice_details details;
char *lbl = NULL; char *lbl = NULL;
if (label) if (label)
lbl = tal_strndup(response, &buffer[label->start], label->end - label->start); lbl = tal_strndup(response, &buffer[label->start], label->end - label->start);
i = NULL; memset(&it, 0, sizeof(it));
while ((i = wallet_invoice_iterate(wallet, i)) != NULL) { while (wallet_invoice_iterate(wallet, &it)) {
wallet_invoice_details(response, wallet, i, &details); wallet_invoice_iterator_deref(response, wallet, &it, &details);
if (lbl && !streq(details.label, lbl)) if (lbl && !streq(details.label, lbl))
continue; continue;
json_add_invoice(response, &details, modern); json_add_invoice(response, &details, modern);

18
wallet/invoices.c

@ -446,13 +446,21 @@ bool invoices_delete(struct invoices *invoices,
return true; return true;
} }
const struct invoice *invoices_iterate(struct invoices *invoices, bool invoices_iterate(struct invoices *invoices,
const struct invoice *invoice) struct invoice_iterator *it)
{ {
if (invoice) if (it->curr)
return list_next(&invoices->invlist, invoice, list); it->curr = list_next(&invoices->invlist, it->curr, list);
else else
return list_top(&invoices->invlist, struct invoice, list); it->curr = list_top(&invoices->invlist, struct invoice, list);
return it->curr != NULL;
}
void invoices_iterator_deref(const tal_t *ctx,
struct invoices *invoices,
const struct invoice_iterator *it,
struct invoice_details *details)
{
invoices_get_details(ctx, invoices, it->curr, details);
} }
static s64 get_next_pay_index(struct db *db) static s64 get_next_pay_index(struct db *db)

31
wallet/invoices.h

@ -8,6 +8,7 @@
struct db; struct db;
struct invoice; struct invoice;
struct invoice_details; struct invoice_details;
struct invoice_iterator;
struct invoices; struct invoices;
struct log; struct log;
struct sha256; struct sha256;
@ -91,18 +92,34 @@ bool invoices_delete(struct invoices *invoices,
* invoices_iterate - Iterate over all existing invoices * invoices_iterate - Iterate over all existing invoices
* *
* @invoices - the invoice handler. * @invoices - the invoice handler.
* @invoice - the previous invoice you iterated over. * @iterator - the iterator object to use.
* *
* Return NULL at end-of-sequence. Usage: * Return false at end-of-sequence, true if still iterating.
* Usage:
* *
* const struct invoice *i; * struct invoice_iterator it;
* i = NULL; * memset(&it, 0, sizeof(it))
* while ((i = invoices_iterate(invoices, i))) { * while (invoices_iterate(wallet, &it)) {
* ... * ...
* } * }
*/ */
const struct invoice *invoices_iterate(struct invoices *invoices, bool invoices_iterate(struct invoices *invoices,
const struct invoice *invoice); 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.
* @details - pointer to details object to load.
*
*/
void invoices_iterator_deref(const tal_t *ctx,
struct invoices *invoices,
const struct invoice_iterator *it,
struct invoice_details *details);
/** /**
* invoices_resolve - Mark an invoice as paid * invoices_resolve - Mark an invoice as paid

10
wallet/test/run-wallet.c

@ -119,9 +119,15 @@ void invoices_get_details(const tal_t *ctx UNNEEDED,
struct invoice_details *details UNNEEDED) struct invoice_details *details UNNEEDED)
{ fprintf(stderr, "invoices_get_details called!\n"); abort(); } { fprintf(stderr, "invoices_get_details called!\n"); abort(); }
/* Generated stub for invoices_iterate */ /* Generated stub for invoices_iterate */
const struct invoice *invoices_iterate(struct invoices *invoices UNNEEDED, bool invoices_iterate(struct invoices *invoices UNNEEDED,
const struct invoice *invoice UNNEEDED) struct invoice_iterator *it UNNEEDED)
{ fprintf(stderr, "invoices_iterate called!\n"); abort(); } { fprintf(stderr, "invoices_iterate called!\n"); abort(); }
/* Generated stub for invoices_iterator_deref */
void invoices_iterator_deref(const tal_t *ctx UNNEEDED,
struct invoices *invoices UNNEEDED,
const struct invoice_iterator *it UNNEEDED,
struct invoice_details *details UNNEEDED)
{ fprintf(stderr, "invoices_iterator_deref called!\n"); abort(); }
/* Generated stub for invoices_load */ /* Generated stub for invoices_load */
bool invoices_load(struct invoices *invoices UNNEEDED) bool invoices_load(struct invoices *invoices UNNEEDED)
{ fprintf(stderr, "invoices_load called!\n"); abort(); } { fprintf(stderr, "invoices_load called!\n"); abort(); }

13
wallet/wallet.c

@ -1287,10 +1287,17 @@ bool wallet_invoice_delete(struct wallet *wallet,
{ {
return invoices_delete(wallet->invoices, invoice); return invoices_delete(wallet->invoices, invoice);
} }
const struct invoice *wallet_invoice_iterate(struct wallet *wallet, bool wallet_invoice_iterate(struct wallet *wallet,
const struct invoice *invoice) struct invoice_iterator *it)
{
return invoices_iterate(wallet->invoices, it);
}
void wallet_invoice_iterator_deref(const tal_t *ctx,
struct wallet *wallet,
const struct invoice_iterator *it,
struct invoice_details *details)
{ {
return invoices_iterate(wallet->invoices, invoice); return invoices_iterator_deref(ctx, wallet->invoices, it, details);
} }
void wallet_invoice_resolve(struct wallet *wallet, void wallet_invoice_resolve(struct wallet *wallet,
const struct invoice *invoice, const struct invoice *invoice,

37
wallet/wallet.h

@ -390,6 +390,13 @@ struct invoice_details {
u64 paid_timestamp; u64 paid_timestamp;
}; };
/* 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 */
const struct invoice *curr;
};
struct invoice { struct invoice {
/* Internal, rest of lightningd should not use */ /* Internal, rest of lightningd should not use */
/* List off ld->wallet->invoices. Must be first or else /* List off ld->wallet->invoices. Must be first or else
@ -476,18 +483,34 @@ bool wallet_invoice_delete(struct wallet *wallet,
* wallet_invoice_iterate - Iterate over all existing invoices * wallet_invoice_iterate - Iterate over all existing invoices
* *
* @wallet - the wallet whose invoices are to be iterated over. * @wallet - the wallet whose invoices are to be iterated over.
* @invoice - the previous invoice you iterated over. * @iterator - the iterator object to use.
* *
* Return NULL at end-of-sequence. Usage: * Return false at end-of-sequence, true if still iterating.
* Usage:
* *
* const struct invoice *i; * struct invoice_iterator it;
* i = NULL; * memset(&it, 0, sizeof(it))
* while ((i = wallet_invoice_iterate(wallet, i))) { * while (wallet_invoice_iterate(wallet, &it)) {
* ... * ...
* } * }
*/ */
const struct invoice *wallet_invoice_iterate(struct wallet *wallet, bool wallet_invoice_iterate(struct wallet *wallet,
const struct invoice *invoice); 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.
* @details - pointer to details object to load.
*
*/
void wallet_invoice_iterator_deref(const tal_t *ctx,
struct wallet *wallet,
const struct invoice_iterator *it,
struct invoice_details *details);
/** /**
* wallet_invoice_resolve - Mark an invoice as paid * wallet_invoice_resolve - Mark an invoice as paid

Loading…
Cancel
Save