From 3b0081aebb6d3244216a3044a02c8b17097d3c4d Mon Sep 17 00:00:00 2001 From: ZmnSCPxj Date: Mon, 26 Feb 2018 12:37:53 +0000 Subject: [PATCH] invoices: Add delexpiredinvoice command. --- doc/Makefile | 1 + doc/lightning-delexpiredinvoice.7 | 0 doc/lightning-delexpiredinvoice.7.txt | 35 ++++++++++++++++++++++++++ doc/lightning-delinvoice.7 | 6 ++--- doc/lightning-delinvoice.7.txt | 3 ++- lightningd/invoice.c | 36 +++++++++++++++++++++++++++ wallet/invoices.c | 13 ++++++++++ wallet/invoices.h | 10 ++++++++ wallet/test/run-wallet.c | 4 +++ wallet/wallet.c | 4 +++ wallet/wallet.h | 10 ++++++++ 11 files changed, 118 insertions(+), 4 deletions(-) create mode 100644 doc/lightning-delexpiredinvoice.7 create mode 100644 doc/lightning-delexpiredinvoice.7.txt diff --git a/doc/Makefile b/doc/Makefile index 06c2e3a60..c09c340f2 100644 --- a/doc/Makefile +++ b/doc/Makefile @@ -6,6 +6,7 @@ doc-wrongdir: MANPAGES := doc/lightning-cli.1 \ doc/lightning-decodepay.7 \ + doc/lightning-delexpiredinvoice.7 \ doc/lightning-delinvoice.7 \ doc/lightning-getroute.7 \ doc/lightning-invoice.7 \ diff --git a/doc/lightning-delexpiredinvoice.7 b/doc/lightning-delexpiredinvoice.7 new file mode 100644 index 000000000..e69de29bb diff --git a/doc/lightning-delexpiredinvoice.7.txt b/doc/lightning-delexpiredinvoice.7.txt new file mode 100644 index 000000000..c11610b19 --- /dev/null +++ b/doc/lightning-delexpiredinvoice.7.txt @@ -0,0 +1,35 @@ +LIGHTNING-DELEXPIREDINVOICE(7) +============================== +:doctype: manpage + +NAME +---- +lightning-delexpiredinvoice - Protocol for removing expired invoices. + +SYNOPSIS +-------- +*delexpiredinvoice* ['maxexpirytime'] + +DESCRIPTION +----------- +The *delexpiredinvoice* RPC command removes all invoices that have +expired on or before the given 'maxexpirytime'. + +If 'maxexpirytime' is not specified then all expired invoices are +deleted. + +RETURN VALUE +------------ +On success, does nothing. + +AUTHOR +------ +ZmnSCPxj is mainly responsible. + +SEE ALSO +-------- +lightning-delinvoice(7) + +RESOURCES +--------- +Main web site: https://github.com/ElementsProject/lightning diff --git a/doc/lightning-delinvoice.7 b/doc/lightning-delinvoice.7 index 0833647ff..c9baf3ed1 100644 --- a/doc/lightning-delinvoice.7 +++ b/doc/lightning-delinvoice.7 @@ -2,12 +2,12 @@ .\" Title: lightning-delinvoice .\" Author: [see the "AUTHOR" section] .\" Generator: DocBook XSL Stylesheets v1.79.1 -.\" Date: 01/18/2018 +.\" Date: 02/26/2018 .\" Manual: \ \& .\" Source: \ \& .\" Language: English .\" -.TH "LIGHTNING\-DELINVOIC" "7" "01/18/2018" "\ \&" "\ \&" +.TH "LIGHTNING\-DELINVOIC" "7" "02/26/2018" "\ \&" "\ \&" .\" ----------------------------------------------------------------- .\" * Define some portability stuff .\" ----------------------------------------------------------------- @@ -45,7 +45,7 @@ On success, an invoice description will be returned as per lightning\-listinvoic Rusty Russell is mainly responsible\&. .SH "SEE ALSO" .sp -lightning\-listinvoice(7), lightning\-waitinvoice(7), lightning\-invoice(7)\&. +lightning\-listinvoice(7), lightning\-waitinvoice(7), lightning\-invoice(7), lightning\-delexpiredinvoice(7) .SH "RESOURCES" .sp Main web site: https://github\&.com/ElementsProject/lightning diff --git a/doc/lightning-delinvoice.7.txt b/doc/lightning-delinvoice.7.txt index ab93b2cbd..1e72a01b9 100644 --- a/doc/lightning-delinvoice.7.txt +++ b/doc/lightning-delinvoice.7.txt @@ -30,7 +30,8 @@ Rusty Russell is mainly responsible. SEE ALSO -------- -lightning-listinvoice(7), lightning-waitinvoice(7), lightning-invoice(7). +lightning-listinvoice(7), lightning-waitinvoice(7), lightning-invoice(7), +lightning-delexpiredinvoice(7) RESOURCES --------- diff --git a/lightningd/invoice.c b/lightningd/invoice.c index eb2770c18..d399029a0 100644 --- a/lightningd/invoice.c +++ b/lightningd/invoice.c @@ -387,6 +387,42 @@ static const struct json_command delinvoice_command = { }; AUTODATA(json_command, &delinvoice_command); +static void json_delexpiredinvoice(struct command *cmd, const char *buffer, + const jsmntok_t *params) +{ + jsmntok_t *maxexpirytimetok; + u64 maxexpirytime = time_now().ts.tv_sec; + struct json_result *result; + + if (!json_get_params(cmd, buffer, params, + "?maxexpirytime", &maxexpirytimetok, + NULL)) { + return; + } + + if (maxexpirytimetok) { + if (!json_tok_u64(buffer, maxexpirytimetok, &maxexpirytime)) { + command_fail(cmd, "'%.*s' is not a valid number", + maxexpirytimetok->end - maxexpirytimetok->start, + buffer + maxexpirytimetok->start); + return; + } + } + + wallet_invoice_delete_expired(cmd->ld->wallet, maxexpirytime); + + result = new_json_result(cmd); + json_object_start(result, NULL); + json_object_end(result); + command_success(cmd, result); +} +static const struct json_command delexpiredinvoice_command = { + "delexpiredinvoice", + json_delexpiredinvoice, + "Delete all expired invoices that expired as of given {maxexpirytime} (a UNIX epoch time), or all expired invoices if not specified" +}; +AUTODATA(json_command, &delexpiredinvoice_command); + static void json_waitanyinvoice(struct command *cmd, const char *buffer, const jsmntok_t *params) { diff --git a/wallet/invoices.c b/wallet/invoices.c index 118281b44..67bcc7520 100644 --- a/wallet/invoices.c +++ b/wallet/invoices.c @@ -384,6 +384,19 @@ bool invoices_delete(struct invoices *invoices, return true; } +void invoices_delete_expired(struct invoices *invoices, + u64 max_expiry_time) +{ + sqlite3_stmt *stmt; + stmt = db_prepare(invoices->db, + "DELETE FROM invoices" + " WHERE state = ?" + " AND expiry_time <= ?;"); + sqlite3_bind_int(stmt, 1, EXPIRED); + sqlite3_bind_int64(stmt, 2, max_expiry_time); + db_exec_prepared(invoices->db, stmt); +} + bool invoices_iterate(struct invoices *invoices, struct invoice_iterator *it) { diff --git a/wallet/invoices.h b/wallet/invoices.h index 8fbf196d8..db898ccc3 100644 --- a/wallet/invoices.h +++ b/wallet/invoices.h @@ -101,6 +101,16 @@ bool invoices_find_unpaid(struct invoices *invoices, bool invoices_delete(struct invoices *invoices, struct invoice invoice); +/** + * invoices_delete_expired - Delete all expired invoices + * with expiration time less than or equal to the given. + * + * @invoices - the invoice handler. + * @max_expiry_time - the maximum expiry time to delete. + */ +void invoices_delete_expired(struct invoices *invoices, + u64 max_expiry_time); + /** * invoices_iterate - Iterate over all existing invoices * diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index 66110097f..60a201c9b 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -113,6 +113,10 @@ bool invoices_create(struct invoices *invoices UNNEEDED, bool invoices_delete(struct invoices *invoices UNNEEDED, struct invoice invoice UNNEEDED) { fprintf(stderr, "invoices_delete called!\n"); abort(); } +/* Generated stub for invoices_delete_expired */ +void invoices_delete_expired(struct invoices *invoices UNNEEDED, + u64 max_expiry_time UNNEEDED) +{ fprintf(stderr, "invoices_delete_expired called!\n"); abort(); } /* Generated stub for invoices_find_by_label */ bool invoices_find_by_label(struct invoices *invoices UNNEEDED, struct invoice *pinvoice UNNEEDED, diff --git a/wallet/wallet.c b/wallet/wallet.c index ded5c83bf..9045f3afa 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -1395,6 +1395,10 @@ bool wallet_invoice_delete(struct wallet *wallet, { return invoices_delete(wallet->invoices, invoice); } +void wallet_invoice_delete_expired(struct wallet *wallet, u64 e) +{ + invoices_delete_expired(wallet->invoices, e); +} bool wallet_invoice_iterate(struct wallet *wallet, struct invoice_iterator *it) { diff --git a/wallet/wallet.h b/wallet/wallet.h index d7ad9a4c8..23a9450b1 100644 --- a/wallet/wallet.h +++ b/wallet/wallet.h @@ -506,6 +506,16 @@ bool wallet_invoice_find_unpaid(struct wallet *wallet, bool wallet_invoice_delete(struct wallet *wallet, struct invoice invoice); +/** + * wallet_invoice_delete_expired - Delete all expired invoices + * with expiration time less than or equal to the given. + * + * @wallet - the wallet to delete invoices from. + * @max_expiry_time - the maximum expiry time to delete. + */ +void wallet_invoice_delete_expired(struct wallet *wallet, + u64 max_expiry_time); + /** * wallet_invoice_iterate - Iterate over all existing invoices *