autoclean: clean up listforwards as well.

And take the opportunity to rename l0 and l1 in the tests to the
more natural l1 l2 (since we add l3).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2022-09-19 10:23:00 +09:30
committed by Christian Decker
parent 3079afb024
commit a15f1be5f8
2 changed files with 155 additions and 55 deletions

View File

@@ -8,6 +8,8 @@
#include <plugins/libplugin.h>
enum subsystem {
SUCCEEDEDFORWARDS,
FAILEDFORWARDS,
SUCCEEDEDPAYS,
FAILEDPAYS,
PAIDINVOICES,
@@ -15,6 +17,8 @@ enum subsystem {
#define NUM_SUBSYSTEM (EXPIREDINVOICES + 1)
};
static const char *subsystem_str[] = {
"succeededforwards",
"failedforwards",
"succeededpays",
"failedpays",
"paidinvoices",
@@ -227,6 +231,73 @@ static struct command_result *listsendpays_done(struct command *cmd,
return set_next_timer(plugin);
}
static struct command_result *listforwards_done(struct command *cmd,
const char *buf,
const jsmntok_t *result,
char *unused)
{
const jsmntok_t *t, *fwds = json_get_member(buf, result, "forwards");
size_t i;
u64 now = time_now().ts.tv_sec;
json_for_each_arr(i, t, fwds) {
const jsmntok_t *status = json_get_member(buf, t, "status");
jsmntok_t time;
enum subsystem subsys;
u64 restime;
if (json_tok_streq(buf, status, "settled")) {
subsys = SUCCEEDEDFORWARDS;
} else if (json_tok_streq(buf, status, "failed")
|| json_tok_streq(buf, status, "local_failed")) {
subsys = FAILEDFORWARDS;
} else
continue;
/* Continue if we don't care. */
if (subsystem_age[subsys] == 0)
continue;
time = *json_get_member(buf, t, "resolved_time");
/* This is a float, so truncate at '.' */
for (int off = time.start; off < time.end; off++) {
if (buf[off] == '.')
time.end = off;
}
if (!json_to_u64(buf, &time, &restime)) {
plugin_err(plugin, "Bad time '%.*s'",
json_tok_full_len(&time),
json_tok_full(buf, &time));
}
if (restime <= now - subsystem_age[subsys]) {
struct out_req *req;
const jsmntok_t *inchan, *inid;
inchan = json_get_member(buf, t, "in_channel");
inid = json_get_member(buf, t, "in_htlc_id");
req = jsonrpc_request_start(plugin, NULL, "delforward",
del_done, del_failed,
int2ptr(subsys));
json_add_tok(req->js, "in_channel", inchan, buf);
json_add_tok(req->js, "in_htlc_id", inid, buf);
json_add_tok(req->js, "status", status, buf);
send_outreq(plugin, req);
plugin_log(plugin, LOG_DBG, "Cleaning up fwd %.*s/%.*s",
json_tok_full_len(inchan),
json_tok_full(buf, inchan),
json_tok_full_len(inid),
json_tok_full(buf, inid));
cleanup_reqs_remaining++;
}
}
if (cleanup_reqs_remaining)
return command_still_pending(cmd);
return set_next_timer(plugin);
}
static void do_clean(void *unused)
{
struct out_req *req = NULL;
@@ -248,6 +319,14 @@ static void do_clean(void *unused)
send_outreq(plugin, req);
}
if (subsystem_age[SUCCEEDEDFORWARDS] != 0
|| subsystem_age[FAILEDFORWARDS] != 0) {
req = jsonrpc_request_start(plugin, NULL, "listforwards",
listforwards_done, cmd_failed,
(char *)"listforwards");
send_outreq(plugin, req);
}
if (!req)
set_next_timer(plugin);
}