Browse Source
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
9 changed files with 113 additions and 98 deletions
@ -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); |
||||
|
} |
Loading…
Reference in new issue