Skip to content

Commit 5c82724

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

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
@@ -10898,6 +10898,50 @@
1089810898
}
1089910899
]
1090010900
},
10901+
"delnetworkevent.json": {
10902+
"$schema": "../rpc-schema-draft.json",
10903+
"type": "object",
10904+
"rpc": "delnetworkevent",
10905+
"title": "Command for removing a listnetworkevents entry",
10906+
"description": [
10907+
"The **delnetworkevent** RPC command removes a single event from **listnetworkevents**, using the uniquely-identifying *created_index*.",
10908+
"",
10909+
"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."
10910+
],
10911+
"request": {
10912+
"required": [
10913+
"created_index"
10914+
],
10915+
"additionalProperties": false,
10916+
"properties": {
10917+
"created_index": {
10918+
"type": "u64",
10919+
"description": [
10920+
"The unique created_index of the entry.."
10921+
]
10922+
}
10923+
}
10924+
},
10925+
"response": {
10926+
"required": [],
10927+
"additionalProperties": false,
10928+
"properties": {}
10929+
},
10930+
"errors": [
10931+
"The following errors may be reported:",
10932+
"",
10933+
"- 1402: The listnetworkevents specified does not exist."
10934+
],
10935+
"author": [
10936+
"Rusty Russell <<[email protected]>> is mainly responsible."
10937+
],
10938+
"see_also": [
10939+
"lightning-autoclean(7)"
10940+
],
10941+
"resources": [
10942+
"Main web site: <https://github.com/ElementsProject/lightning>"
10943+
]
10944+
},
1090110945
"delpay.json": {
1090210946
"$schema": "../rpc-schema-draft.json",
1090310947
"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
@@ -4996,3 +4996,32 @@ def test_networkevents(node_factory, executor):
49964996
assert nevents == [{'created_index': 7,
49974997
'peer_id': l2.info['id'],
49984998
'type': 'connect'}]
4999+
5000+
fut = executor.submit(l1.rpc.wait, 'networkevents', 'deleted', 1)
5001+
time.sleep(1)
5002+
l1.rpc.delnetworkevent(7)
5003+
assert l1.rpc.listnetworkevents(start=7) == {'networkevents': []}
5004+
5005+
res = fut.result(TIMEOUT)
5006+
assert res == {'subsystem': 'networkevents',
5007+
'deleted': 1,
5008+
'networkevents': {'created_index': 7}}
5009+
5010+
with pytest.raises(RpcError, match="Could not find that networkevent") as err:
5011+
l1.rpc.delnetworkevent(7)
5012+
DELNETWORKEVENT_NOT_FOUND = 1402
5013+
assert err.value.error['code'] == DELNETWORKEVENT_NOT_FOUND
5014+
5015+
l1.rpc.delnetworkevent(3)
5016+
with l1.rpc.reply_filter({'networkevents': [{"created_index": True, "type": True}]}):
5017+
assert l1.rpc.listnetworkevents() == {'networkevents':
5018+
[{'created_index': 1,
5019+
'type': 'connect'},
5020+
{'created_index': 2,
5021+
'type': 'ping'},
5022+
{'created_index': 4,
5023+
'type': 'connect_fail'},
5024+
{'created_index': 5,
5025+
'type': 'connect_fail'},
5026+
{'created_index': 6,
5027+
'type': 'connect_fail'}]}

wallet/wallet.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7518,6 +7518,13 @@ static u64 network_event_index_created(struct lightningd *ld,
75187518
WAIT_INDEX_CREATED);
75197519
}
75207520

7521+
static void network_event_index_deleted(struct lightningd *ld,
7522+
u64 created_index)
7523+
{
7524+
network_event_index_inc(ld, &created_index, NULL, NULL,
7525+
WAIT_INDEX_DELETED);
7526+
}
7527+
75217528
/* Put the next network event into the db */
75227529
void wallet_save_network_event(struct lightningd *ld,
75237530
const struct node_id *peer_id,
@@ -7550,6 +7557,30 @@ void wallet_save_network_event(struct lightningd *ld,
75507557
db_exec_prepared_v2(take(stmt));
75517558
}
75527559

7560+
bool wallet_network_event_delete(struct wallet *w, u64 created_index)
7561+
{
7562+
struct db_stmt *stmt;
7563+
bool changed;
7564+
7565+
stmt = db_prepare_v2(w->db,
7566+
SQL("DELETE FROM network_events"
7567+
" WHERE id = ?"));
7568+
db_bind_u64(stmt, created_index);
7569+
db_exec_prepared_v2(stmt);
7570+
7571+
changed = db_count_changes(stmt) != 0;
7572+
tal_free(stmt);
7573+
7574+
if (changed) {
7575+
/* FIXME: We don't set other details here, since that
7576+
* would need an extra lookup */
7577+
network_event_index_deleted(w->ld, created_index);
7578+
}
7579+
7580+
return changed;
7581+
7582+
}
7583+
75537584
struct missing {
75547585
size_t num_found;
75557586
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
@@ -1279,3 +1279,28 @@ static const struct json_command listnetworkevents_cmd = {
12791279
json_listnetworkevents
12801280
};
12811281
AUTODATA(json_command, &listnetworkevents_cmd);
1282+
1283+
static struct command_result *json_delnetworkevent(struct command *cmd,
1284+
const char *buffer,
1285+
const jsmntok_t *obj UNNEEDED,
1286+
const jsmntok_t *params)
1287+
{
1288+
u64 *created_index;
1289+
1290+
if (!param(cmd, buffer, params,
1291+
p_req("created_index", param_u64, &created_index),
1292+
NULL))
1293+
return command_param_failed();
1294+
1295+
if (!wallet_network_event_delete(cmd->ld->wallet, *created_index))
1296+
return command_fail(cmd, DELNETWORKEVENT_NOT_FOUND,
1297+
"Could not find that networkevent");
1298+
1299+
return command_success(cmd, json_stream_success(cmd));
1300+
}
1301+
1302+
static const struct json_command delnetworkevent_command = {
1303+
"delnetworkevent",
1304+
json_delnetworkevent,
1305+
};
1306+
AUTODATA(json_command, &delnetworkevent_command);

0 commit comments

Comments
 (0)