From acd4a189ae5d2e6a3a7b3ae573561db192559a9c Mon Sep 17 00:00:00 2001 From: Evgeny Date: Thu, 23 Jul 2026 18:13:14 +0300 Subject: [PATCH] db_sync: add author_sig VERIFY FAIL diagnostics + SYNC_DONE retry limit - db_verify_author_sig: show ed_pubkey and sig first bytes on failure - SI_PEER: add sync_retry_count field - db_handle_sync_done: limit re-initiate to 3 retries before giving up - db_sync_initiate_sync / db_sync_on_conn_up: reset retry_count --- src/db_sync.c | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/db_sync.c b/src/db_sync.c index beae8525..edf64632 100644 --- a/src/db_sync.c +++ b/src/db_sync.c @@ -38,6 +38,7 @@ struct SI_PEER { uint64_t node_id; uint32_t synced_pos; uint8_t sync_state; + uint8_t sync_retry_count; uint64_t sync_start_tb; }; @@ -400,7 +401,8 @@ static int db_verify_author_sig(struct DB_SYNC_INSTANCE* si, uint8_t pubkey[32]; if (db_get_ed25519_pubkey(si->db_sync, author_node_id, pubkey) != 0) { DEBUG_WARN(DEBUG_CATEGORY_DB_SYNC, - "cannot get Ed25519 pubkey for author=%016llx", (unsigned long long)author_node_id); + "cannot get Ed25519 pubkey for author=%016llx — discarding", + (unsigned long long)author_node_id); return -1; } uint8_t msg[8192]; size_t off = 0; @@ -408,8 +410,8 @@ static int db_verify_author_sig(struct DB_SYNC_INSTANCE* si, if (jlen > 0) { memcpy(msg + off, json, jlen); off += jlen; } if (sc_ed25519_verify(pubkey, msg, off, sig) != SC_OK) { DEBUG_WARN(DEBUG_CATEGORY_DB_SYNC, - "author_sig VERIFY FAIL node=%016llx — discarding as forgery", - (unsigned long long)author_node_id); + "author_sig VERIFY FAIL author=%016llx ed_pubkey=%016llx... msg_len=%zu sig=%016llx... — discarding as forgery", + (unsigned long long)author_node_id, *(uint64_t*)pubkey, off, *(uint64_t*)sig); return -1; } return 0; @@ -981,14 +983,24 @@ static void db_handle_sync_done(struct DB_SYNC_INSTANCE* si, uint64_t src, const (mc == pc && mch8 == pch8) ? "matched" : "MISMATCH"); if (mc != pc || mch8 != pch8) { + struct SI_PEER* sp = si_peer_find(si, src); + if (sp) sp->sync_retry_count++; + if (sp && sp->sync_retry_count > 3) { + DEBUG_WARN(DEBUG_CATEGORY_DB_SYNC, + "SYNC_DONE mismatch retry limit exceeded (%u/3) with %016llx — giving up", + sp->sync_retry_count, (unsigned long long)src); + sp->sync_state = 2; sp->synced_pos = (mc > 0) ? mc - 1 : 0; + return; + } DEBUG_INFO(DEBUG_CATEGORY_DB_SYNC, - "SYNC_DONE mismatch with %016llx my_count=%u peer_count=%u my_ch8=%016llx peer_ch8=%016llx — re-initiating", - (unsigned long long)src, mc, pc, (unsigned long long)mch8, (unsigned long long)pch8); + "SYNC_DONE mismatch with %016llx my_count=%u peer_count=%u my_ch8=%016llx peer_ch8=%016llx — re-initiating (retry %u/3)", + (unsigned long long)src, mc, pc, (unsigned long long)mch8, (unsigned long long)pch8, + sp ? sp->sync_retry_count : 0); db_sync_initiate_sync(si, src); return; } struct SI_PEER* sp = si_peer_find(si, src); - if (sp) { sp->synced_pos = (mc > 0) ? mc - 1 : 0; sp->sync_state = 2; } + if (sp) { sp->synced_pos = (mc > 0) ? mc - 1 : 0; sp->sync_state = 2; sp->sync_retry_count = 0; } DEBUG_INFO(DEBUG_CATEGORY_DB_SYNC, "sync confirmed with %016llx count=%u ch8=%016llx", (unsigned long long)src, mc, (unsigned long long)mch8); } @@ -1160,7 +1172,7 @@ static void db_sync_on_conn_up(struct ETCP_CONN* conn, void* arg) if (!p) continue; if (p->sync_state != 0) { skipped_state++; continue; } if (!conn->initialized || !conn->links_up) { skipped_not_ready++; continue; } - p->sync_state = 1; + p->sync_state = 1; p->sync_retry_count = 0; db_sync_initiate_sync(si, pid); synced++; } @@ -1206,7 +1218,7 @@ static void db_sync_initiate_sync(struct DB_SYNC_INSTANCE* si, uint64_t pid) uint32_t mc = db_count(si); DEBUG_TRACE(DEBUG_CATEGORY_DB_SYNC, "pid=%016llx my=%u tbl=%s", (unsigned long long)pid, mc, SI_TBL(si)); struct SI_PEER* p = si_peer_find(si, pid); - if (p) p->sync_start_tb = get_time_tb(); + if (p) { p->sync_start_tb = get_time_tb(); p->sync_retry_count = 0; } uint8_t msg[5]; msg[0] = DB_MSG_INIT_SYNC;