lightnind: add connectd's reported events to the db.
What changed, and why it matters
This commit adds routine logging of network events—successful connections, failed connections, disconnections, and ping latencies—to the node's internal database. It is a bookkeeping/telemetry change, not a fix for a security flaw, and it does not change how the node handles peers or funds.
No security action required; review as normal feature/telemetry code.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch wires connectd-reported events into wallet_save_network_event() so they are persisted: CONNECTFAIL in connect_failed(), PING in handle_ping_latency(), CONNECT in handle_peer_connected(), and DISCONNECT in peer_disconnected(). A duplicate-suppression guard (errcode != CONNECT_DISCONNECTED_DURING) prevents recording the same failure twice. A stub is added to a unit test. There is no change to protocol handling, authentication, cryptography, or access control.
Changed components
lightningd/connect_control.clightningd/peer_control.clightningd/test/run-invoice-select-inchan.cInspect captured patch +32 / −0
diff --git a/lightningd/connect_control.c b/lightningd/connect_control.c
index 850dc9d7..c2a28522 100644
--- a/lightningd/connect_control.c
+++ b/lightningd/connect_control.c
@@ -262,6 +262,14 @@ static void connect_failed(struct lightningd *ld,
{
struct connect *c;
+ /* Don't record twice. */
+ if (errcode != CONNECT_DISCONNECTED_DURING)
+ wallet_save_network_event(ld, id,
+ NETWORK_EVENT_CONNECTFAIL,
+ errmsg,
+ connect_nsec,
+ connect_attempted);
+
/* We can have multiple connect commands: fail them all */
while ((c = find_connect(ld, id)) != NULL) {
/* They delete themselves from list */
@@ -490,6 +498,12 @@ static void handle_ping_latency(struct lightningd *ld, const u8 *msg)
log_peer_trace(ld->log, &id, "Ping latency: %"PRIu64"nsec",
nsec);
+
+ wallet_save_network_event(ld, &id,
+ NETWORK_EVENT_PING,
+ NULL,
+ nsec,
+ false);
}
static unsigned connectd_msg(struct subd *connectd, const u8 *msg, const int *fds)
diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c
index dd0396d6..f5b064ca 100644
--- a/lightningd/peer_control.c
+++ b/lightningd/peer_control.c
@@ -1744,6 +1744,12 @@ void handle_peer_connected(struct lightningd *ld, const u8 *msg)
connect_nsec = 0;
}
+ wallet_save_network_event(ld, &id,
+ NETWORK_EVENT_CONNECT,
+ hook_payload->incoming ? NULL : connect_reason,
+ connect_nsec,
+ false);
+
/* If we connected, and it's a normal address */
if (!hook_payload->incoming
&& hook_payload->addr.itype == ADDR_INTERNAL_WIREADDR
@@ -2091,6 +2097,10 @@ static void peer_disconnected(struct lightningd *ld,
struct disconnect_command *i, *next;
struct peer *p;
+ wallet_save_network_event(ld, id,
+ NETWORK_EVENT_DISCONNECT,
+ NULL, connected_time_nsec, false);
+
/* If we still have peer, it's disconnected now */
p = peer_by_id(ld, id);
if (p) {
diff --git a/lightningd/test/run-invoice-select-inchan.c b/lightningd/test/run-invoice-select-inchan.c
index 064c8bcb..b2721ea8 100644
--- a/lightningd/test/run-invoice-select-inchan.c
+++ b/lightningd/test/run-invoice-select-inchan.c
@@ -687,6 +687,14 @@ char *wallet_offer_find(const tal_t *ctx UNNEEDED,
enum offer_status *status)
{ fprintf(stderr, "wallet_offer_find called!\n"); abort(); }
+/* Generated stub for wallet_save_network_event */
+void wallet_save_network_event(struct lightningd *ld UNNEEDED,
+ const struct node_id *peer_id UNNEEDED,
+ enum network_event etype UNNEEDED,
+ const char *reason UNNEEDED,
+ u64 duration_nsec UNNEEDED,
+ bool connect_attempted UNNEEDED)
+{ fprintf(stderr, "wallet_save_network_event called!\n"); abort(); }
/* Generated stub for wallet_total_forward_fees */
struct amount_msat wallet_total_forward_fees(struct wallet *w UNNEEDED)
{ fprintf(stderr, "wallet_total_forward_fees called!\n"); abort(); }
Why this scored 13/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.