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