Skip to content

Commit

Permalink
invoices: Implement autocleaninvoice.
Browse files Browse the repository at this point in the history
  • Loading branch information
ZmnSCPxj authored and cdecker committed Mar 20, 2018
1 parent de853b4 commit 159e736
Show file tree
Hide file tree
Showing 13 changed files with 227 additions and 6 deletions.
1 change: 1 addition & 0 deletions doc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ doc-wrongdir:
$(MAKE) -C .. doc-all

MANPAGES := doc/lightning-cli.1 \
doc/lightning-autocleaninvoice.7 \
doc/lightning-decodepay.7 \
doc/lightning-delexpiredinvoice.7 \
doc/lightning-delinvoice.7 \
Expand Down
Empty file.
42 changes: 42 additions & 0 deletions doc/lightning-autocleaninvoice.7.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
LIGHTNING-AUTOCLEANINVOICE(7)
=============================
:doctype: manpage

NAME
----
lightning-autocleaninvoice - Set up auto-delete of expired invoice

SYNOPSIS
--------
*autocleaninvoice* ['cycle_seconds'] ['expired_by']

DESCRIPTION
-----------
The *autocleaninvoice* RPC command sets up automatic cleaning of
expired invoices.

Autoclean will be done every 'cycle_seconds' seconds.
Setting 'cycle_seconds' to 0 disables autoclean.
If not specified, this defaults to 3600 (one hour).

Every autoclean cycle, expired invoices, which have already
been expired for at least 'expired_by' seconds, will be deleted.
If 'expired_by' is not specified, this defaults to 86400 (one day).

On startup of the daemon, no autoclean is set up.

RETURN VALUE
------------
On success, an empty object is returned.

AUTHOR
------
ZmnSCPxj <[email protected]> is mainly responsible.

SEE ALSO
--------
lightning-delexpiredinvoice(7), lightning-delinvoice(7)

RESOURCES
---------
Main web site: https://github.com/ElementsProject/lightning
51 changes: 51 additions & 0 deletions doc/lightning-delexpiredinvoice.7
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'\" t
.\" Title: lightning-delexpiredinvoice
.\" Author: [see the "AUTHOR" section]
.\" Generator: DocBook XSL Stylesheets v1.79.1 <http://docbook.sf.net/>
.\" Date: 03/17/2018
.\" Manual: \ \&
.\" Source: \ \&
.\" Language: English
.\"
.TH "LIGHTNING\-DELEXPIRE" "7" "03/17/2018" "\ \&" "\ \&"
.\" -----------------------------------------------------------------
.\" * Define some portability stuff
.\" -----------------------------------------------------------------
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.\" http://bugs.debian.org/507673
.\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.\" -----------------------------------------------------------------
.\" * set default formatting
.\" -----------------------------------------------------------------
.\" disable hyphenation
.nh
.\" disable justification (adjust text to left margin only)
.ad l
.\" -----------------------------------------------------------------
.\" * MAIN CONTENT STARTS HERE *
.\" -----------------------------------------------------------------
.SH "NAME"
lightning-delexpiredinvoice \- Protocol for removing expired invoices\&.
.SH "SYNOPSIS"
.sp
\fBdelexpiredinvoice\fR [\fImaxexpirytime\fR]
.SH "DESCRIPTION"
.sp
The \fBdelexpiredinvoice\fR RPC command removes all invoices that have expired on or before the given \fImaxexpirytime\fR\&.
.sp
If \fImaxexpirytime\fR is not specified then all expired invoices are deleted\&.
.SH "RETURN VALUE"
.sp
On success, an empty object is returned\&.
.SH "AUTHOR"
.sp
ZmnSCPxj <ZmnSCPxj@protonmail\&.com> is mainly responsible\&.
.SH "SEE ALSO"
.sp
lightning\-delinvoice(7), lightning\-autocleaninvoice(7)
.SH "RESOURCES"
.sp
Main web site: https://github\&.com/ElementsProject/lightning
4 changes: 2 additions & 2 deletions doc/lightning-delexpiredinvoice.7.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ deleted.

RETURN VALUE
------------
On success, does nothing.
On success, an empty object is returned.

AUTHOR
------
ZmnSCPxj <[email protected]> is mainly responsible.

SEE ALSO
--------
lightning-delinvoice(7)
lightning-delinvoice(7), lightning-autocleaninvoice(7)

RESOURCES
---------
Expand Down
6 changes: 3 additions & 3 deletions doc/lightning-delinvoice.7
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
.\" Title: lightning-delinvoice
.\" Author: [see the "AUTHOR" section]
.\" Generator: DocBook XSL Stylesheets v1.79.1 <http://docbook.sf.net/>
.\" Date: 02/26/2018
.\" Date: 03/17/2018
.\" Manual: \ \&
.\" Source: \ \&
.\" Language: English
.\"
.TH "LIGHTNING\-DELINVOIC" "7" "02/26/2018" "\ \&" "\ \&"
.TH "LIGHTNING\-DELINVOIC" "7" "03/17/2018" "\ \&" "\ \&"
.\" -----------------------------------------------------------------
.\" * Define some portability stuff
.\" -----------------------------------------------------------------
Expand Down Expand Up @@ -45,7 +45,7 @@ On success, an invoice description will be returned as per lightning\-listinvoic
Rusty Russell <rusty@rustcorp\&.com\&.au> is mainly responsible\&.
.SH "SEE ALSO"
.sp
lightning\-listinvoice(7), lightning\-waitinvoice(7), lightning\-invoice(7), lightning\-delexpiredinvoice(7)
lightning\-listinvoice(7), lightning\-waitinvoice(7), lightning\-invoice(7), lightning\-delexpiredinvoice(7), lightning\-autocleaninvoice(7)
.SH "RESOURCES"
.sp
Main web site: https://github\&.com/ElementsProject/lightning
2 changes: 1 addition & 1 deletion doc/lightning-delinvoice.7.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Rusty Russell <[email protected]> is mainly responsible.
SEE ALSO
--------
lightning-listinvoice(7), lightning-waitinvoice(7), lightning-invoice(7),
lightning-delexpiredinvoice(7)
lightning-delexpiredinvoice(7), lightning-autocleaninvoice(7)

RESOURCES
---------
Expand Down
50 changes: 50 additions & 0 deletions lightningd/invoice.c
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,56 @@ static const struct json_command delexpiredinvoice_command = {
};
AUTODATA(json_command, &delexpiredinvoice_command);

static void json_autocleaninvoice(struct command *cmd,
const char *buffer,
const jsmntok_t *params)
{
jsmntok_t *cycletok;
jsmntok_t *exbytok;
u64 cycle = 3600;
u64 exby = 86400;
struct json_result *result;

if (!json_get_params(cmd, buffer, params,
"?cycle_seconds", &cycletok,
"?expired_by", &exbytok,
NULL)) {
return;
}

if (cycletok) {
if (!json_tok_u64(buffer, cycletok, &cycle)) {
command_fail(cmd, "'%.*s' is not a valid number",
cycletok->end - cycletok->start,
buffer + cycletok->start);
return;
}
}
if (exbytok) {
if (!json_tok_u64(buffer, exbytok, &exby)) {
command_fail(cmd, "'%.*s' is not a valid number",
exbytok->end - exbytok->start,
buffer + exbytok->start);
return;
}
}

wallet_invoice_autoclean(cmd->ld->wallet, cycle, exby);

result = new_json_result(cmd);
json_object_start(result, NULL);
json_object_end(result);
command_success(cmd, result);
}
static const struct json_command autocleaninvoice_command = {
"autocleaninvoice",
json_autocleaninvoice,
"Set up autoclean of expired invoices. "
"Perform cleanup every {cycle_seconds} (default 3600), or disable autoclean if 0. "
"Clean up expired invoices that have expired for {expired_by} seconds (default 86400). "
};
AUTODATA(json_command, &autocleaninvoice_command);

static void json_waitanyinvoice(struct command *cmd,
const char *buffer, const jsmntok_t *params)
{
Expand Down
41 changes: 41 additions & 0 deletions wallet/invoices.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ struct invoices {
u64 min_expiry_time;
/* Expiration timer */
struct oneshot *expiration_timer;
/* Autoclean timer */
struct oneshot *autoclean_timer;
u64 autoclean_cycle_seconds;
u64 autoclean_expired_by;
};

static void trigger_invoice_waiter(struct invoice_waiter *w,
Expand Down Expand Up @@ -128,6 +132,7 @@ struct invoices *invoices_new(const tal_t *ctx,
list_head_init(&invs->waiters);

invs->expiration_timer = NULL;
invs->autoclean_timer = NULL;

return invs;
}
Expand Down Expand Up @@ -397,6 +402,42 @@ void invoices_delete_expired(struct invoices *invoices,
db_exec_prepared(invoices->db, stmt);
}

static void refresh_autoclean(struct invoices *invoices);
static void trigger_autoclean(struct invoices *invoices)
{
u64 now = time_now().ts.tv_sec;

invoices_delete_expired(invoices,
now - invoices->autoclean_expired_by);

refresh_autoclean(invoices);
}
static void refresh_autoclean(struct invoices *invoices)
{
struct timerel rel = time_from_sec(invoices->autoclean_cycle_seconds);
invoices->autoclean_timer = tal_free(invoices->autoclean_timer);
invoices->autoclean_timer = new_reltimer(invoices->timers,
invoices,
rel,
&trigger_autoclean,
invoices);
}

void invoices_autoclean_set(struct invoices *invoices,
u64 cycle_seconds,
u64 expired_by)
{
/* If not perform autoclean, just clear */
if (cycle_seconds == 0) {
invoices->autoclean_timer = tal_free(invoices->autoclean_timer);
return;
}

invoices->autoclean_cycle_seconds = cycle_seconds;
invoices->autoclean_expired_by = expired_by;
refresh_autoclean(invoices);
}

bool invoices_iterate(struct invoices *invoices,
struct invoice_iterator *it)
{
Expand Down
16 changes: 16 additions & 0 deletions wallet/invoices.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,22 @@ bool invoices_delete(struct invoices *invoices,
void invoices_delete_expired(struct invoices *invoices,
u64 max_expiry_time);

/**
* invoices_autoclean_set - Set up automatic deletion of
* expired invoices.
*
* @invoices - the invoice handler.
* @cycle_seconds - The number of seconds to repeat the
* automatic deletion. If 0, do not perform automatic
* deletion.
* @expiry_by - Each cycle, delete invoices that
* have been expired for at least `expiry_by`
* seconds.
*/
void invoices_autoclean_set(struct invoices *invoices,
u64 cycle_seconds,
u64 expired_by);

/**
* invoices_iterate - Iterate over all existing invoices
*
Expand Down
5 changes: 5 additions & 0 deletions wallet/test/run-wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ u8 *get_offered_global_features(const tal_t *ctx UNNEEDED)
/* Generated stub for get_offered_local_features */
u8 *get_offered_local_features(const tal_t *ctx UNNEEDED)
{ fprintf(stderr, "get_offered_local_features called!\n"); abort(); }
/* Generated stub for invoices_autoclean_set */
void invoices_autoclean_set(struct invoices *invoices UNNEEDED,
u64 cycle_seconds UNNEEDED,
u64 expired_by UNNEEDED)
{ fprintf(stderr, "invoices_autoclean_set called!\n"); abort(); }
/* Generated stub for invoices_create */
bool invoices_create(struct invoices *invoices UNNEEDED,
struct invoice *pinvoice UNNEEDED,
Expand Down
4 changes: 4 additions & 0 deletions wallet/wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -1399,6 +1399,10 @@ void wallet_invoice_delete_expired(struct wallet *wallet, u64 e)
{
invoices_delete_expired(wallet->invoices, e);
}
void wallet_invoice_autoclean(struct wallet *wallet, u64 c, u64 e)
{
invoices_autoclean_set(wallet->invoices, c, e);
}
bool wallet_invoice_iterate(struct wallet *wallet,
struct invoice_iterator *it)
{
Expand Down
11 changes: 11 additions & 0 deletions wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,17 @@ bool wallet_invoice_delete(struct wallet *wallet,
void wallet_invoice_delete_expired(struct wallet *wallet,
u64 max_expiry_time);

/**
* wallet_invoice_autoclean - Set up a repeating autoclean of
* expired invoices.
* Cleans (deletes) expired invoices every @cycle_seconds.
* Clean only those invoices that have been expired for at
* least @expired_by seconds or more.
*/
void wallet_invoice_autoclean(struct wallet * wallet,
u64 cycle_seconds,
u64 expired_by);

/**
* wallet_invoice_iterate - Iterate over all existing invoices
*
Expand Down

0 comments on commit 159e736

Please sign in to comment.