Skip to content

Commit fed15fb

Browse files
committed
lightningd: delnetworkevent support
Changelog-Added: JSON-RPC: `delnetworkevent` to delete from listnetworkevents. Signed-off-by: Rusty Russell <[email protected]>
1 parent 23e632f commit fed15fb

File tree

7 files changed

+180
-1
lines changed

7 files changed

+180
-1
lines changed

common/jsonrpc_errors.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,9 @@ enum jsonrpc_errcode {
129129
/* Errors from signmessage command */
130130
SIGNMESSAGE_PUBKEY_NOT_FOUND = 1301,
131131

132-
/* Errors from delforward command */
132+
/* Errors from del commands */
133133
DELFORWARD_NOT_FOUND = 1401,
134+
DELNETWORKEVENT_NOT_FOUND = 1402,
134135

135136
/* Errors from runes */
136137
RUNE_NOT_AUTHORIZED = 1501,

contrib/msggen/msggen/schema.json

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10344,6 +10344,50 @@
1034410344
}
1034510345
]
1034610346
},
10347+
"delnetworkevent.json": {
10348+
"$schema": "../rpc-schema-draft.json",
10349+
"type": "object",
10350+
"rpc": "delnetworkevent",
10351+
"title": "Command for removing a listnetworkevents entry",
10352+
"description": [
10353+
"The **delnetworkevent** RPC command removes a single event from **listnetworkevents**, using the uniquely-identifying *created_index*.",
10354+
"",
10355+
"This command is mainly used by the *autoclean* plugin (see lightningd-config(7)), as these database entries are only kept for your own analysis, removing them has no effect on the running of your node."
10356+
],
10357+
"request": {
10358+
"required": [
10359+
"created_index"
10360+
],
10361+
"additionalProperties": false,
10362+
"properties": {
10363+
"created_index": {
10364+
"type": "u64",
10365+
"description": [
10366+
"The unique created_index of the entry.."
10367+
]
10368+
}
10369+
}
10370+
},
10371+
"response": {
10372+
"required": [],
10373+
"additionalProperties": false,
10374+
"properties": {}
10375+
},
10376+
"errors": [
10377+
"The following errors may be reported:",
10378+
"",
10379+
"- 1402: The listnetworkevents specified does not exist."
10380+
],
10381+
"author": [
10382+
"Rusty Russell <<[email protected]>> is mainly responsible."
10383+
],
10384+
"see_also": [
10385+
"lightning-autoclean(7)"
10386+
],
10387+
"resources": [
10388+
"Main web site: <https://github.com/ElementsProject/lightning>"
10389+
]
10390+
},
1034710391
"delpay.json": {
1034810392
"$schema": "../rpc-schema-draft.json",
1034910393
"type": "object",

doc/schemas/delnetworkevent.json

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"$schema": "../rpc-schema-draft.json",
3+
"type": "object",
4+
"rpc": "delnetworkevent",
5+
"title": "Command for removing a listnetworkevents entry",
6+
"description": [
7+
"The **delnetworkevent** RPC command removes a single event from **listnetworkevents**, using the uniquely-identifying *created_index*.",
8+
"",
9+
"This command is mainly used by the *autoclean* plugin (see lightningd-config(7)), as these database entries are only kept for your own analysis, removing them has no effect on the running of your node."
10+
],
11+
"request": {
12+
"required": [
13+
"created_index"
14+
],
15+
"additionalProperties": false,
16+
"properties": {
17+
"created_index": {
18+
"type": "u64",
19+
"description": [
20+
"The unique created_index of the entry.."
21+
]
22+
}
23+
}
24+
},
25+
"response": {
26+
"required": [],
27+
"additionalProperties": false,
28+
"properties": {}
29+
},
30+
"errors": [
31+
"The following errors may be reported:",
32+
"",
33+
"- 1402: The listnetworkevents specified does not exist."
34+
],
35+
"author": [
36+
"Rusty Russell <<[email protected]>> is mainly responsible."
37+
],
38+
"see_also": [
39+
"lightning-autoclean(7)"
40+
],
41+
"resources": [
42+
"Main web site: <https://github.com/ElementsProject/lightning>"
43+
]
44+
}

tests/test_connection.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5015,3 +5015,34 @@ def test_networkevents(node_factory, executor):
50155015

50165016
failevents = [n for n in l2.rpc.listnetworkevents()['networkevents'] if n['type'] == 'connect_fail']
50175017
assert failevents[-1]['connect_attempted'] is False
5018+
5019+
fut = executor.submit(l1.rpc.wait, 'networkevents', 'deleted', 1)
5020+
time.sleep(1)
5021+
l1.rpc.delnetworkevent(8)
5022+
assert l1.rpc.listnetworkevents(start=8) == {'networkevents': []}
5023+
5024+
res = fut.result(TIMEOUT)
5025+
assert res == {'subsystem': 'networkevents',
5026+
'deleted': 1,
5027+
'networkevents': {'created_index': 8}}
5028+
5029+
with pytest.raises(RpcError, match="Could not find that networkevent") as err:
5030+
l1.rpc.delnetworkevent(8)
5031+
DELNETWORKEVENT_NOT_FOUND = 1402
5032+
assert err.value.error['code'] == DELNETWORKEVENT_NOT_FOUND
5033+
5034+
l1.rpc.delnetworkevent(3)
5035+
with l1.rpc.reply_filter({'networkevents': [{"created_index": True, "type": True}]}):
5036+
assert l1.rpc.listnetworkevents() == {'networkevents':
5037+
[{'created_index': 1,
5038+
'type': 'connect'},
5039+
{'created_index': 2,
5040+
'type': 'ping'},
5041+
{'created_index': 4,
5042+
'type': 'connect_fail'},
5043+
{'created_index': 5,
5044+
'type': 'connect_fail'},
5045+
{'created_index': 6,
5046+
'type': 'connect_fail'},
5047+
{'created_index': 7,
5048+
'type': 'connect'}]}

wallet/wallet.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7538,6 +7538,13 @@ static u64 network_event_index_created(struct lightningd *ld,
75387538
WAIT_INDEX_CREATED);
75397539
}
75407540

7541+
static void network_event_index_deleted(struct lightningd *ld,
7542+
u64 created_index)
7543+
{
7544+
network_event_index_inc(ld, &created_index, NULL, NULL,
7545+
WAIT_INDEX_DELETED);
7546+
}
7547+
75417548
/* Put the next network event into the db */
75427549
void wallet_save_network_event(struct lightningd *ld,
75437550
const struct node_id *peer_id,
@@ -7573,6 +7580,30 @@ void wallet_save_network_event(struct lightningd *ld,
75737580
db_exec_prepared_v2(take(stmt));
75747581
}
75757582

7583+
bool wallet_network_event_delete(struct wallet *w, u64 created_index)
7584+
{
7585+
struct db_stmt *stmt;
7586+
bool changed;
7587+
7588+
stmt = db_prepare_v2(w->db,
7589+
SQL("DELETE FROM network_events"
7590+
" WHERE id = ?"));
7591+
db_bind_u64(stmt, created_index);
7592+
db_exec_prepared_v2(stmt);
7593+
7594+
changed = db_count_changes(stmt) != 0;
7595+
tal_free(stmt);
7596+
7597+
if (changed) {
7598+
/* FIXME: We don't set other details here, since that
7599+
* would need an extra lookup */
7600+
network_event_index_deleted(w->ld, created_index);
7601+
}
7602+
7603+
return changed;
7604+
7605+
}
7606+
75767607
struct missing {
75777608
size_t num_found;
75787609
struct missing_addr *addrs;

wallet/wallet.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1920,6 +1920,9 @@ struct db_stmt *wallet_network_events_first(struct wallet *w,
19201920
struct db_stmt *wallet_network_events_next(struct wallet *w,
19211921
struct db_stmt *stmt);
19221922

1923+
/* Delete one entry. Returns false if it doesn't exist. */
1924+
bool wallet_network_event_delete(struct wallet *w, u64 created_index);
1925+
19231926
/**
19241927
* Extract a network event from the db.
19251928
* @ctx: the tal ctx to allocate off

wallet/walletrpc.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,3 +1269,28 @@ static const struct json_command listnetworkevents_cmd = {
12691269
json_listnetworkevents
12701270
};
12711271
AUTODATA(json_command, &listnetworkevents_cmd);
1272+
1273+
static struct command_result *json_delnetworkevent(struct command *cmd,
1274+
const char *buffer,
1275+
const jsmntok_t *obj UNNEEDED,
1276+
const jsmntok_t *params)
1277+
{
1278+
u64 *created_index;
1279+
1280+
if (!param(cmd, buffer, params,
1281+
p_req("created_index", param_u64, &created_index),
1282+
NULL))
1283+
return command_param_failed();
1284+
1285+
if (!wallet_network_event_delete(cmd->ld->wallet, *created_index))
1286+
return command_fail(cmd, DELNETWORKEVENT_NOT_FOUND,
1287+
"Could not find that networkevent");
1288+
1289+
return command_success(cmd, json_stream_success(cmd));
1290+
}
1291+
1292+
static const struct json_command delnetworkevent_command = {
1293+
"delnetworkevent",
1294+
json_delnetworkevent,
1295+
};
1296+
AUTODATA(json_command, &delnetworkevent_command);

0 commit comments

Comments
 (0)