@@ -7438,6 +7438,155 @@ struct db_stmt *wallet_channel_moves_next(struct wallet *wallet, struct db_stmt
74387438 return stmt ;
74397439}
74407440
7441+ struct db_stmt * wallet_network_events_first (struct wallet * w ,
7442+ const struct node_id * specific_id ,
7443+ u64 liststart ,
7444+ u32 * listlimit )
7445+ {
7446+ struct db_stmt * stmt ;
7447+
7448+ if (specific_id ) {
7449+ stmt = db_prepare_v2 (w -> db ,
7450+ SQL ("SELECT"
7451+ " id"
7452+ ", peer_id"
7453+ ", type"
7454+ ", reason"
7455+ ", timestamp"
7456+ ", duration_nsec"
7457+ ", connect_attempted"
7458+ " FROM network_events"
7459+ " WHERE peer_id = ? AND id >= ?"
7460+ " ORDER BY id"
7461+ " LIMIT ?;" ));
7462+ db_bind_node_id (stmt , specific_id );
7463+ } else {
7464+ stmt = db_prepare_v2 (w -> db ,
7465+ SQL ("SELECT"
7466+ " id"
7467+ ", peer_id"
7468+ ", type"
7469+ ", reason"
7470+ ", timestamp"
7471+ ", duration_nsec"
7472+ ", connect_attempted"
7473+ " FROM network_events"
7474+ " WHERE id >= ?"
7475+ " ORDER BY id"
7476+ " LIMIT ?;" ));
7477+ }
7478+ db_bind_u64 (stmt , liststart );
7479+ if (listlimit )
7480+ db_bind_int (stmt , * listlimit );
7481+ else
7482+ db_bind_int (stmt , INT_MAX );
7483+ db_query_prepared (stmt );
7484+ return wallet_channel_moves_next (w , stmt );
7485+ }
7486+
7487+ const char * network_event_name (enum network_event n )
7488+ {
7489+ switch (n ) {
7490+ case NETWORK_EVENT_CONNECT :
7491+ return "connect" ;
7492+ case NETWORK_EVENT_CONNECTFAIL :
7493+ return "connect_fail" ;
7494+ case NETWORK_EVENT_PING :
7495+ return "ping" ;
7496+ case NETWORK_EVENT_DISCONNECT :
7497+ return "disconnect" ;
7498+ }
7499+ fatal ("%s: %u is invalid" , __func__ , n );
7500+ }
7501+
7502+ struct db_stmt * wallet_network_events_next (struct wallet * w ,
7503+ struct db_stmt * stmt )
7504+ {
7505+ if (!db_step (stmt ))
7506+ return tal_free (stmt );
7507+
7508+ return stmt ;
7509+ }
7510+
7511+ void wallet_network_events_extract (const tal_t * ctx ,
7512+ struct db_stmt * stmt ,
7513+ u64 * id ,
7514+ struct node_id * peer_id ,
7515+ u64 * timestamp ,
7516+ enum network_event * etype ,
7517+ const char * * reason ,
7518+ u64 * duration_nsec ,
7519+ bool * connect_attempted )
7520+ {
7521+ * id = db_col_u64 (stmt , "id" );
7522+ db_col_node_id (stmt , "peer_id" , peer_id );
7523+ * etype = network_event_in_db (db_col_int (stmt , "type" ));
7524+ * timestamp = db_col_u64 (stmt , "timestamp" );
7525+ * reason = db_col_strdup_optional (ctx , stmt , "reason" );
7526+ * duration_nsec = db_col_u64 (stmt , "duration_nsec" );
7527+ * connect_attempted = db_col_u64 (stmt , "connect_attempted" );
7528+ }
7529+
7530+ static u64 network_event_index_inc (struct lightningd * ld ,
7531+ /* NULL means it's being created */
7532+ const u64 * created_index ,
7533+ const enum network_event * etype ,
7534+ const struct node_id * peer_id ,
7535+ enum wait_index idx )
7536+ {
7537+ return wait_index_increment (ld , ld -> wallet -> db ,
7538+ WAIT_SUBSYSTEM_NETWORKEVENTS , idx ,
7539+ /* "" is a magic value meaning 'current val' */
7540+ "=created_index" , created_index ? tal_fmt (tmpctx , "%" PRIu64 , * created_index ) : "" ,
7541+ "type" , etype ? network_event_name (* etype ) : NULL ,
7542+ "peer_id" , peer_id ? fmt_node_id (tmpctx , peer_id ) : NULL ,
7543+ NULL );
7544+ }
7545+
7546+ static u64 network_event_index_created (struct lightningd * ld ,
7547+ enum network_event etype ,
7548+ const struct node_id * peer_id )
7549+ {
7550+ return network_event_index_inc (ld , NULL ,
7551+ & etype , peer_id ,
7552+ WAIT_INDEX_CREATED );
7553+ }
7554+
7555+ /* Put the next network event into the db */
7556+ void wallet_save_network_event (struct lightningd * ld ,
7557+ const struct node_id * peer_id ,
7558+ enum network_event etype ,
7559+ const char * reason ,
7560+ u64 duration_nsec ,
7561+ bool connect_attempted )
7562+ {
7563+ u64 id ;
7564+ struct db_stmt * stmt ;
7565+
7566+ stmt = db_prepare_v2 (ld -> wallet -> db ,
7567+ SQL ("INSERT INTO network_events ("
7568+ " id,"
7569+ " peer_id,"
7570+ " type, "
7571+ " timestamp,"
7572+ " reason,"
7573+ " duration_nsec,"
7574+ " connect_attempted) VALUES "
7575+ "(?, ?, ?, ?, ?, ?, ?);" ));
7576+ id = network_event_index_created (ld , etype , peer_id );
7577+ db_bind_u64 (stmt , id );
7578+ db_bind_node_id (stmt , peer_id );
7579+ db_bind_int (stmt , network_event_in_db (etype ));
7580+ db_bind_u64 (stmt , time_now ().ts .tv_sec );
7581+ if (reason )
7582+ db_bind_text (stmt , reason );
7583+ else
7584+ db_bind_null (stmt );
7585+ db_bind_u64 (stmt , duration_nsec );
7586+ db_bind_int (stmt , connect_attempted );
7587+ db_exec_prepared_v2 (take (stmt ));
7588+ }
7589+
74417590struct missing {
74427591 size_t num_found ;
74437592 struct missing_addr * addrs ;
0 commit comments