feat: auto-connect Phase 3 (local/netif addresses) + restart fix

Phase 1: connected peers (connected=1)
Phase 2: direct/EIM (addr_type=1,2), exclude self
Phase 3: local/strict NAT (addr_type=0,3), exclude self
Timeout: 5000→2000ms per attempt
Restart: DOWN triggers reconnect when phase=DONE & active=0

Debug logs: init, Phase1 per-peer, UP phase info, restart branches
This commit is contained in:
Evgeny committed 2026-07-22 23:55:44 +03:00
1 parent f05727ab13
commit fb8e35eb11
4 files changed
+101 -14

No files matched your search

+31 -4
View File
@@ -762,18 +762,45 @@ 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** out_ids, int* out_count, uint64_t self_node_id) {
if (!db || !channel_id || !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[280];
char sql[300];
snprintf(sql, sizeof(sql),
"SELECT p.node_id FROM \"%s\" p"
" WHERE EXISTS (SELECT 1 FROM node_addresses a"
" WHERE a.node_id=p.node_id AND a.family=4 AND a.addr_type!=0)"
" 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))"
" ORDER BY p.node_RTT", peers_tbl);
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);
int cnt = 0;
while (sqlite3_step(st) == SQLITE_ROW) cnt++;
if (cnt == 0) { sqlite3_finalize(st); return 0; }
uint64_t* ids = u_malloc((size_t)cnt * sizeof(uint64_t));
if (!ids) { sqlite3_finalize(st); return -1; }
sqlite3_reset(st); int i = 0;
while (sqlite3_step(st) == SQLITE_ROW) ids[i++] = (uint64_t)sqlite3_column_int64(st, 0);
sqlite3_finalize(st);
*out_ids = ids; *out_count = cnt;
return 0;
}
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;
*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))"
" ORDER BY p.node_RTT", peers_tbl);
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);
int cnt = 0;
while (sqlite3_step(st) == SQLITE_ROW) cnt++;
if (cnt == 0) { sqlite3_finalize(st); return 0; }