diff --git a/src/routing_layer/topo_node_sqlite.c b/src/routing_layer/topo_node_sqlite.c index 0e007b34..7a3b12eb 100644 --- a/src/routing_layer/topo_node_sqlite.c +++ b/src/routing_layer/topo_node_sqlite.c @@ -763,14 +763,13 @@ int topo_node_sqlite_get_connected_peers(sqlite3* db, const char* channel_id, int topo_node_sqlite_get_public_peers(sqlite3* db, const char* channel_id, uint64_t** out_ids, int* out_count, uint64_t self_node_id) { - if (!db || !channel_id || !out_ids || !out_count) return -1; + if (!db || !out_ids || !out_count) return -1; *out_ids = NULL; *out_count = 0; - char peers_tbl[80]; peers_table_name(channel_id, peers_tbl, sizeof(peers_tbl)); - char sql[300]; - snprintf(sql, sizeof(sql), - "SELECT p.node_id FROM \"%s\" p" - " WHERE p.node_id!=? AND EXISTS (SELECT 1 FROM node_addresses a" - " WHERE a.node_id=p.node_id AND a.family=4 AND a.addr_type IN (1,2)))", peers_tbl); + (void)channel_id; + const char* sql = + "SELECT DISTINCT na.node_id FROM node_addresses na" + " WHERE na.node_id!=?1 AND na.family=4 AND na.addr_type IN (1,2)" + " AND EXISTS (SELECT 1 FROM nodes n WHERE n.node_id=na.node_id)"; sqlite3_stmt* st = NULL; if (sqlite3_prepare_v2(db, sql, -1, &st, NULL) != SQLITE_OK) return -1; sqlite3_bind_int64(st, 1, (sqlite3_int64)self_node_id); @@ -788,14 +787,13 @@ int topo_node_sqlite_get_public_peers(sqlite3* db, const char* channel_id, int topo_node_sqlite_get_local_peers(sqlite3* db, const char* channel_id, uint64_t** out_ids, int* out_count, uint64_t self_node_id) { - if (!db || !channel_id || !out_ids || !out_count) return -1; + if (!db || !out_ids || !out_count) return -1; *out_ids = NULL; *out_count = 0; - char peers_tbl[80]; peers_table_name(channel_id, peers_tbl, sizeof(peers_tbl)); - char sql[300]; - snprintf(sql, sizeof(sql), - "SELECT p.node_id FROM \"%s\" p" - " WHERE p.node_id!=? AND EXISTS (SELECT 1 FROM node_addresses a" - " WHERE a.node_id=p.node_id AND a.family=4 AND a.addr_type IN (0,3)))", peers_tbl); + (void)channel_id; + const char* sql = + "SELECT DISTINCT na.node_id FROM node_addresses na" + " WHERE na.node_id!=?1 AND na.family=4 AND na.addr_type IN (0,3)" + " AND EXISTS (SELECT 1 FROM nodes n WHERE n.node_id=na.node_id)"; sqlite3_stmt* st = NULL; if (sqlite3_prepare_v2(db, sql, -1, &st, NULL) != SQLITE_OK) return -1; sqlite3_bind_int64(st, 1, (sqlite3_int64)self_node_id);