Skip to content

Commit da33aba

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

File tree

7 files changed

+178
-1
lines changed

7 files changed

+178
-1
lines changed

common/jsonrpc_errors.h

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

133-
/* Errors from delforward command */
133+
/* Errors from del commands */
134134
DELFORWARD_NOT_FOUND = 1401,
135+
DELNETWORKEVENT_NOT_FOUND = 1402,
135136

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

contrib/msggen/msggen/schema.json

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10281,6 +10281,50 @@
1028110281
}
1028210282
]
1028310283
},
10284+
"delnetworkevent.json": {
10285+
"$schema": "../rpc-schema-draft.json",
10286+
"type": "object",
10287+
"rpc": "delnetworkevent",
10288+
"title": "Command for removing a listnetworkevents entry",
10289+
"description": [
10290+
"The **delnetworkevent** RPC command removes a single event from **listnetworkevents**, using the uniquely-identifying *created_index*.",
10291+
"",
10292+
"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."
10293+
],
10294+
"request": {
10295+
"required": [
10296+
"created_index"
10297+
],
10298+
"additionalProperties": false,
10299+
"properties": {
10300+
"created_index": {
10301+
"type": "u64",
10302+
"description": [
10303+
"The unique created_index of the entry.."
10304+
]
10305+
}
10306+
}
10307+
},
10308+
"response": {
10309+
"required": [],
10310+
"additionalProperties": false,
10311+
"properties": {}
10312+
},
10313+
"errors": [
10314+
"The following errors may be reported:",
10315+
"",
10316+
"- 1402: The listnetworkevents specified does not exist."
10317+
],
10318+
"author": [
10319+
"Rusty Russell <<[email protected]>> is mainly responsible."
10320+
],
10321+
"see_also": [
10322+
"lightning-autoclean(7)"
10323+
],
10324+
"resources": [
10325+
"Main web site: <https://github.com/ElementsProject/lightning>"
10326+
]
10327+
},
1028410328
"delpay.json": {
1028510329
"$schema": "../rpc-schema-draft.json",
1028610330
"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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4997,3 +4997,32 @@ def test_networkevents(node_factory, executor):
49974997
assert nevents == [{'created_index': 7,
49984998
'peer_id': l2.info['id'],
49994999
'type': 'connect'}]
5000+
5001+
fut = executor.submit(l1.rpc.wait, 'networkevents', 'deleted', 1)
5002+
time.sleep(1)
5003+
l1.rpc.delnetworkevent(7)
5004+
assert l1.rpc.listnetworkevents(start=7) == {'networkevents': []}
5005+
5006+
res = fut.result(TIMEOUT)
5007+
assert res == {'subsystem': 'networkevents',
5008+
'deleted': 1,
5009+
'networkevents': {'created_index': 7}}
5010+
5011+
with pytest.raises(RpcError, match="Could not find that networkevent") as err:
5012+
l1.rpc.delnetworkevent(7)
5013+
DELNETWORKEVENT_NOT_FOUND = 1402
5014+
assert err.value.error['code'] == DELNETWORKEVENT_NOT_FOUND
5015+
5016+
l1.rpc.delnetworkevent(3)
5017+
with l1.rpc.reply_filter({'networkevents': [{"created_index": True, "type": True}]}):
5018+
assert l1.rpc.listnetworkevents() == {'networkevents':
5019+
[{'created_index': 1,
5020+
'type': 'connect'},
5021+
{'created_index': 2,
5022+
'type': 'ping'},
5023+
{'created_index': 4,
5024+
'type': 'connect_fail'},
5025+
{'created_index': 5,
5026+
'type': 'connect_fail'},
5027+
{'created_index': 6,
5028+
'type': 'connect_fail'}]}

wallet/wallet.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7470,6 +7470,13 @@ static u64 network_event_index_created(struct lightningd *ld,
74707470
WAIT_INDEX_CREATED);
74717471
}
74727472

7473+
static void network_event_index_deleted(struct lightningd *ld,
7474+
u64 created_index)
7475+
{
7476+
network_event_index_inc(ld, &created_index, NULL, NULL,
7477+
WAIT_INDEX_DELETED);
7478+
}
7479+
74737480
/* Put the next network event into the db */
74747481
void wallet_save_network_event(struct lightningd *ld,
74757482
const struct node_id *peer_id,
@@ -7502,6 +7509,30 @@ void wallet_save_network_event(struct lightningd *ld,
75027509
db_exec_prepared_v2(take(stmt));
75037510
}
75047511

7512+
bool wallet_network_event_delete(struct wallet *w, u64 created_index)
7513+
{
7514+
struct db_stmt *stmt;
7515+
bool changed;
7516+
7517+
stmt = db_prepare_v2(w->db,
7518+
SQL("DELETE FROM network_events"
7519+
" WHERE id = ?"));
7520+
db_bind_u64(stmt, created_index);
7521+
db_exec_prepared_v2(stmt);
7522+
7523+
changed = db_count_changes(stmt) != 0;
7524+
tal_free(stmt);
7525+
7526+
if (changed) {
7527+
/* FIXME: We don't set other details here, since that
7528+
* would need an extra lookup */
7529+
network_event_index_deleted(w->ld, created_index);
7530+
}
7531+
7532+
return changed;
7533+
7534+
}
7535+
75057536
struct missing {
75067537
size_t num_found;
75077538
struct missing_addr *addrs;

wallet/wallet.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1938,6 +1938,9 @@ void wallet_save_network_event(struct lightningd *ld,
19381938
const char *reason,
19391939
u64 duration_nsec);
19401940

1941+
/* Delete one entry. Returns false if it doesn't exist. */
1942+
bool wallet_network_event_delete(struct wallet *w, u64 created_index);
1943+
19411944
/**
19421945
* wallet_begin_old_close_rescan: rescan for missing mutual close p2wpkh outputs.
19431946
*

wallet/walletrpc.c

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

0 commit comments

Comments
 (0)