From be258eea4d212ba70a3f1658509be9d2f0c192be Mon Sep 17 00:00:00 2001 From: Evgeny Date: Thu, 23 Jul 2026 00:24:16 +0300 Subject: [PATCH] fix: remove stale peers_* CREATE TABLE from chat_core, drop ORDER BY node_RTT MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit chat_core_ensure_channel_ready() had a duplicate CREATE TABLE peers_* with OLD schema (no node_RTT, node_type, connected). Since it was called from 4 places WITHOUT topo_node_sqlite_channel_put() first, the old schema was created first, and IF NOT EXISTS in the new DDL was a no-op. Removed the entire old block. Only topo_node_sqlite_channel_put() now creates peers_* tables with the full schema. Also removed ORDER BY p.node_RTT from get_public_peers/get_local_peers — ordering not needed for sequential connect attempts. --- src/routing_layer/topo_node_sqlite.c | 6 ++---- tools/chatgui/transport/chat_core.c | 26 -------------------------- 2 files changed, 2 insertions(+), 30 deletions(-) diff --git a/src/routing_layer/topo_node_sqlite.c b/src/routing_layer/topo_node_sqlite.c index c9cc74ee..0e007b34 100644 --- a/src/routing_layer/topo_node_sqlite.c +++ b/src/routing_layer/topo_node_sqlite.c @@ -770,8 +770,7 @@ int topo_node_sqlite_get_public_peers(sqlite3* db, const char* channel_id, 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))" - " ORDER BY p.node_RTT", peers_tbl); + " WHERE a.node_id=p.node_id AND a.family=4 AND a.addr_type IN (1,2)))", 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); @@ -796,8 +795,7 @@ int topo_node_sqlite_get_local_peers(sqlite3* db, const char* channel_id, 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); + " WHERE a.node_id=p.node_id AND a.family=4 AND a.addr_type IN (0,3)))", 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); diff --git a/tools/chatgui/transport/chat_core.c b/tools/chatgui/transport/chat_core.c index 2ef16763..e8a5860c 100644 --- a/tools/chatgui/transport/chat_core.c +++ b/tools/chatgui/transport/chat_core.c @@ -634,32 +634,6 @@ void chat_core_ensure_channel_ready(const char* ch_id) { char tbl_msg[80]; msg_table_name(ch_id, tbl_msg, sizeof(tbl_msg)); - char tbl_peers[80]; peers_table_name(ch_id, tbl_peers, sizeof(tbl_peers)); - char sql[512]; - snprintf(sql, sizeof(sql), - "CREATE TABLE IF NOT EXISTS \"%s\" (" - " node_id INTEGER PRIMARY KEY," /* уникальный ID узла (64-bit) */ - " x25519_pubkey BLOB NOT NULL," /* X25519 публичный ключ (32 байта) */ - " ed25519_pubkey BLOB," /* Ed25519 публичный ключ (32 байта) — верификация join_sig/update_sig */ - " join_sig BLOB," /* Ed25519 подпись присоединения (PEERS_JOIN_SIG_SIZE=64). - signed: ch_x25519(32)||ch_ed25519(32)||node_id(8 LE)||x25519(32)||join_ts(8 LE) */ - " join_ts INTEGER DEFAULT 0," /* NTP timestamp подписи join_sig — защита от replay */ - " update_sig BLOB," /* Ed25519 подпись обновления userinfo (PEERS_UPDATE_SIG_SIZE=64). - signed: join_sig(64)||update_ts(8 LE)||userinfo(NUL-terminated) */ - " update_ts INTEGER DEFAULT 0," /* NTP timestamp подписи update_sig */ - " userinfo TEXT NOT NULL DEFAULT ''," /* JSON с информацией о пользователе: {"name":""}. - извлекается через json_flat_get(userinfo, "name", ...) */ - " creator_sig BLOB," /* Ed25519 подпись создателя (PEERS_JOIN_SIG_SIZE=64). - в CHANNEL_INFO_RESP ставится join_sig инвайтера, иначе NULL (заглушка) */ - " comment TEXT," /* произвольная заметка (пока не используется в логике) */ - " joined_at INTEGER DEFAULT (unixepoch())" /* NTP timestamp добавления записи в таблицу */ - ")", tbl_peers); - db_exec(sql); - - // Migration: rename old 'name' column to 'userinfo' (match server schema) - snprintf(sql, sizeof(sql), "ALTER TABLE \"%s\" RENAME COLUMN name TO userinfo", tbl_peers); - sqlite3_exec(g_cc.db, sql, NULL, NULL, NULL); - uint64_t ch_hash = 0; { const uint8_t* chd = (const uint8_t*)ch_id; size_t chl = strlen(ch_id); uint8_t sh[32]; SHA256(chd, chl, sh); memcpy(&ch_hash, sh, 8); } struct DB_SYNC_INSTANCE* si = db_sync_instance_add(g_cc.inst, tbl_msg, ch_hash);