Browse Source

autoclean: make this a plugin.

No change in behavior.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
htlc_accepted_hook
Rusty Russell 6 years ago
committed by Christian Decker
parent
commit
4370ffa978
  1. 2
      Makefile
  2. 27
      lightningd/invoice.c
  3. 7
      lightningd/lightningd.c
  4. 4
      lightningd/lightningd.h
  5. 8
      lightningd/options.c
  6. 17
      plugins/Makefile
  7. 101
      plugins/autoclean.c
  8. 41
      wallet/invoices.c
  9. 4
      wallet/wallet.c

2
Makefile

@ -485,7 +485,7 @@ PKGLIBEXEC_PROGRAMS = \
lightningd/lightning_hsmd \ lightningd/lightning_hsmd \
lightningd/lightning_onchaind \ lightningd/lightning_onchaind \
lightningd/lightning_openingd lightningd/lightning_openingd
PLUGINS=plugins/pay PLUGINS=plugins/pay plugins/autoclean
install-program: installdirs $(BIN_PROGRAMS) $(PKGLIBEXEC_PROGRAMS) $(PLUGINS) install-program: installdirs $(BIN_PROGRAMS) $(PKGLIBEXEC_PROGRAMS) $(PLUGINS)
@$(NORMAL_INSTALL) @$(NORMAL_INSTALL)

27
lightningd/invoice.c

@ -921,33 +921,6 @@ static const struct json_command delexpiredinvoice_command = {
}; };
AUTODATA(json_command, &delexpiredinvoice_command); AUTODATA(json_command, &delexpiredinvoice_command);
static struct command_result *json_autocleaninvoice(struct command *cmd,
const char *buffer,
const jsmntok_t *obj UNNEEDED,
const jsmntok_t *params)
{
u64 *cycle;
u64 *exby;
if (!param(cmd, buffer, params,
p_opt_def("cycle_seconds", param_u64, &cycle, 3600),
p_opt_def("expired_by", param_u64, &exby, 86400),
NULL))
return command_param_failed();
wallet_invoice_autoclean(cmd->ld->wallet, *cycle, *exby);
return command_success(cmd, null_response(cmd));
}
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 struct command_result *json_waitanyinvoice(struct command *cmd, static struct command_result *json_waitanyinvoice(struct command *cmd,
const char *buffer, const char *buffer,
const jsmntok_t *obj UNNEEDED, const jsmntok_t *obj UNNEEDED,

7
lightningd/lightningd.c

@ -196,8 +196,6 @@ static struct lightningd *new_lightningd(const tal_t *ctx)
ld->daemon = false; ld->daemon = false;
ld->config_filename = NULL; ld->config_filename = NULL;
ld->pidfile = NULL; ld->pidfile = NULL;
ld->ini_autocleaninvoice_cycle = 0;
ld->ini_autocleaninvoice_expiredby = 86400;
ld->proxyaddr = NULL; ld->proxyaddr = NULL;
ld->use_proxy_always = false; ld->use_proxy_always = false;
ld->pure_tor_setup = false; ld->pure_tor_setup = false;
@ -722,11 +720,6 @@ int main(int argc, char *argv[])
/*~ Initialize the transaction filter with our pubkeys. */ /*~ Initialize the transaction filter with our pubkeys. */
init_txfilter(ld->wallet, ld->owned_txfilter); init_txfilter(ld->wallet, ld->owned_txfilter);
/*~ Set up invoice autoclean. */
wallet_invoice_autoclean(ld->wallet,
ld->ini_autocleaninvoice_cycle,
ld->ini_autocleaninvoice_expiredby);
/*~ Pull peers, channels and HTLCs from db. */ /*~ Pull peers, channels and HTLCs from db. */
load_channels_from_wallet(ld); load_channels_from_wallet(ld);

4
lightningd/lightningd.h

@ -178,10 +178,6 @@ struct lightningd {
/* PID file */ /* PID file */
char *pidfile; char *pidfile;
/* Initial autocleaninvoice settings. */
u64 ini_autocleaninvoice_cycle;
u64 ini_autocleaninvoice_expiredby;
/* Number of blocks we wait for a channel to get funded /* Number of blocks we wait for a channel to get funded
* if we are the fundee. */ * if we are the fundee. */
u32 max_funding_unconfirmed; u32 max_funding_unconfirmed;

8
lightningd/options.c

@ -407,14 +407,6 @@ static void config_register_opts(struct lightningd *ld)
opt_set_bool_arg, opt_show_bool, opt_set_bool_arg, opt_show_bool,
&deprecated_apis, &deprecated_apis,
"Enable deprecated options, JSONRPC commands, fields, etc."); "Enable deprecated options, JSONRPC commands, fields, etc.");
opt_register_arg("--autocleaninvoice-cycle",
opt_set_u64, opt_show_u64,
&ld->ini_autocleaninvoice_cycle,
"Perform cleanup of expired invoices every given seconds, or do not autoclean if 0");
opt_register_arg("--autocleaninvoice-expired-by",
opt_set_u64, opt_show_u64,
&ld->ini_autocleaninvoice_expiredby,
"If expired invoice autoclean enabled, invoices that have expired for at least this given seconds are cleaned");
opt_register_arg("--proxy", opt_add_proxy_addr, NULL, opt_register_arg("--proxy", opt_add_proxy_addr, NULL,
ld,"Set a socks v5 proxy IP address and port"); ld,"Set a socks v5 proxy IP address and port");
opt_register_arg("--tor-service-password", opt_set_talstr, NULL, opt_register_arg("--tor-service-password", opt_set_talstr, NULL,

17
plugins/Makefile

@ -1,6 +1,9 @@
PLUGIN_PAY_SRC := plugins/pay.c PLUGIN_PAY_SRC := plugins/pay.c
PLUGIN_PAY_OBJS := $(PLUGIN_PAY_SRC:.c=.o) PLUGIN_PAY_OBJS := $(PLUGIN_PAY_SRC:.c=.o)
PLUGIN_AUTOCLEAN_SRC := plugins/autoclean.c
PLUGIN_AUTOCLEAN_OBJS := $(PLUGIN_AUTOCLEAN_SRC:.c=.o)
PLUGIN_LIB_SRC := plugins/libplugin.c PLUGIN_LIB_SRC := plugins/libplugin.c
PLUGIN_LIB_HEADER := plugins/libplugin.h PLUGIN_LIB_HEADER := plugins/libplugin.h
PLUGIN_LIB_OBJS := $(PLUGIN_LIB_SRC:.c=.o) PLUGIN_LIB_OBJS := $(PLUGIN_LIB_SRC:.c=.o)
@ -37,17 +40,19 @@ PLUGIN_COMMON_OBJS := \
plugins/pay: $(PLUGIN_PAY_OBJS) $(PLUGIN_LIB_OBJS) $(PLUGIN_COMMON_OBJS) $(JSMN_OBJS) $(CCAN_OBJS) plugins/pay: $(PLUGIN_PAY_OBJS) $(PLUGIN_LIB_OBJS) $(PLUGIN_COMMON_OBJS) $(JSMN_OBJS) $(CCAN_OBJS)
$(PLUGIN_PAY_OBJS) $(PLUGIN_LIB_OBJS): $(PLUGIN_LIB_HEADER) plugins/autoclean: $(PLUGIN_AUTOCLEAN_OBJS) $(PLUGIN_LIB_OBJS) $(PLUGIN_COMMON_OBJS) $(JSMN_OBJS) $(CCAN_OBJS)
$(PLUGIN_PAY_OBJS) $(PLUGIN_AUTOCLEAN_OBJS) $(PLUGIN_LIB_OBJS): $(PLUGIN_LIB_HEADER)
# Make sure these depend on everything. # Make sure these depend on everything.
ALL_PROGRAMS += plugins/pay ALL_PROGRAMS += plugins/pay plugins/autoclean
ALL_OBJS += $(PLUGIN_PAY_OBJS) $(PLUGIN_LIB_OBJS) ALL_OBJS += $(PLUGIN_PAY_OBJS) $(PLUGIN_LIB_OBJS)
check-source: $(PLUGIN_PAY_SRC:%=check-src-include-order/%) check-source: $(PLUGIN_PAY_SRC:%=check-src-include-order/%) $(PLUGIN_AUTOCLEAN_SRC:%=check-src-include-order/%)
check-source-bolt: $(PLUGIN_PAY_SRC:%=bolt-check/%) check-source-bolt: $(PLUGIN_PAY_SRC:%=bolt-check/%) $(PLUGIN_AUTOCLEAN_SRC:%=bolt-check/%)
check-whitespace: $(PLUGIN_PAY_SRC:%=check-whitespace/%) check-whitespace: $(PLUGIN_PAY_SRC:%=check-whitespace/%) $(PLUGIN_AUTOCLEAN_SRC:%=check-whitespace/%)
clean: plugin-clean clean: plugin-clean
plugin-clean: plugin-clean:
$(RM) $(PLUGIN_PAY_OBJS) $(PLUGIN_LIB_OBJS) $(RM) $(PLUGIN_PAY_OBJS) $(PLUGIN_AUTOCLEAN_OBJS) $(PLUGIN_LIB_OBJS)

101
plugins/autoclean.c

@ -0,0 +1,101 @@
#include <ccan/array_size/array_size.h>
#include <ccan/cast/cast.h>
#include <ccan/intmap/intmap.h>
#include <ccan/tal/str/str.h>
#include <ccan/time/time.h>
#include <common/amount.h>
#include <common/bolt11.h>
#include <common/pseudorand.h>
#include <common/type_to_string.h>
#include <gossipd/gossip_constants.h>
#include <plugins/libplugin.h>
#include <stdio.h>
static u64 cycle_seconds = 0, expired_by = 86400;
static struct plugin_timer *cleantimer;
static struct plugin_conn *rpc;
static struct command_result *do_clean(void);
static struct command_result *ignore(struct command *timer,
const char *buf,
const jsmntok_t *result,
void *arg)
{
cleantimer = plugin_timer(rpc, time_from_sec(cycle_seconds), do_clean);
return timer_complete();
}
static struct command_result *do_clean(void)
{
u64 age = time_now().ts.tv_sec - expired_by;
/* FIXME: delexpiredinvoice should be in our plugin too! */
return send_outreq(NULL, "delexpiredinvoice", ignore, ignore, NULL,
"'maxexpirytime': %"PRIu64, age);
}
static struct command_result *json_autocleaninvoice(struct command *cmd,
const char *buffer,
const jsmntok_t *params)
{
u64 *cycle;
u64 *exby;
if (!param(cmd, buffer, params,
p_opt_def("cycle_seconds", param_u64, &cycle, 3600),
p_opt_def("expired_by", param_u64, &exby, 86400),
NULL))
return NULL;
cycle_seconds = *cycle;
expired_by = *exby;
if (cycle_seconds == 0) {
tal_free(cleantimer);
return command_success(cmd, "'Autoclean timer disabled'");
}
tal_free(cleantimer);
cleantimer = plugin_timer(rpc, time_from_sec(cycle_seconds), do_clean);
return command_success(cmd, tal_fmt(cmd, "'Autocleaning %"PRIu64
"-second old invoices every %"PRIu64
" seconds'",
expired_by, cycle_seconds));
}
static void init(struct plugin_conn *prpc)
{
rpc = prpc;
if (cycle_seconds) {
plugin_log(LOG_INFORM, "autocleaning every %"PRIu64" seconds", cycle_seconds);
cleantimer = plugin_timer(rpc, time_from_sec(cycle_seconds),
do_clean);
} else
plugin_log(LOG_DBG, "autocleaning not active");
}
static const struct plugin_command commands[] = { {
"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). ",
json_autocleaninvoice
}
};
int main(int argc, char *argv[])
{
setup_locale();
plugin_main(argv, init, commands, ARRAY_SIZE(commands),
plugin_option("autocleaninvoice-cycle",
"Perform cleanup of expired invoices every"
" given seconds, or do not autoclean if 0",
u64_option, &cycle_seconds),
plugin_option("autocleaninvoice-expired-by",
"If expired invoice autoclean enabled,"
" invoices that have expired for at least"
" this given seconds are cleaned",
u64_option, &expired_by),
NULL);
}

41
wallet/invoices.c

@ -45,10 +45,6 @@ struct invoices {
u64 min_expiry_time; u64 min_expiry_time;
/* Expiration timer */ /* Expiration timer */
struct oneshot *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, static void trigger_invoice_waiter(struct invoice_waiter *w,
@ -160,7 +156,6 @@ struct invoices *invoices_new(const tal_t *ctx,
list_head_init(&invs->waiters); list_head_init(&invs->waiters);
invs->expiration_timer = NULL; invs->expiration_timer = NULL;
invs->autoclean_timer = NULL;
update_db_expirations(invs, time_now().ts.tv_sec); update_db_expirations(invs, time_now().ts.tv_sec);
install_expiration_timer(invs); install_expiration_timer(invs);
@ -421,42 +416,6 @@ void invoices_delete_expired(struct invoices *invoices,
db_exec_prepared(invoices->db, stmt); 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, bool invoices_iterate(struct invoices *invoices,
struct invoice_iterator *it) struct invoice_iterator *it)
{ {

4
wallet/wallet.c

@ -1570,10 +1570,6 @@ void wallet_invoice_delete_expired(struct wallet *wallet, u64 e)
{ {
invoices_delete_expired(wallet->invoices, 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, bool wallet_invoice_iterate(struct wallet *wallet,
struct invoice_iterator *it) struct invoice_iterator *it)
{ {

Loading…
Cancel
Save