From 0fee9f82154c5b651694565fe1c70b2f2a6d3f28 Mon Sep 17 00:00:00 2001 From: evgeny Date: Mon, 26 Oct 2026 22:59:26 +0100 Subject: [PATCH] Explain edit rejection causes and unchanged submissions in diagnostics --- src/chat/chat_msg.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/chat/chat_msg.c b/src/chat/chat_msg.c index f27c72e10..786efe07b 100644 --- a/src/chat/chat_msg.c +++ b/src/chat/chat_msg.c @@ -144,7 +144,8 @@ int chat_core_edit_message(struct UTUN_INSTANCE* inst, const char* channel, cons snprintf(sql, sizeof(sql), "SELECT timestamp,node_id,nonce,ver,edited_at," "json_extract(CAST(data AS TEXT),'$.ct'),json_extract(CAST(data AS TEXT),'$.d')," "json_set(CAST(data AS TEXT),'$.d',?2) FROM \"%s\" WHERE message_id=?1", table); - sqlite3_stmt* st = NULL; struct db_message_revision revision = {0}; uint64_t ts = 0; int result = -1; + sqlite3_stmt* st = NULL; struct db_message_revision revision = {0}; uint64_t ts = 0, observed_ver = 0; int result = -1; + const char* stage = "lookup"; int rc = sqlite3_prepare_v2(cc->db, sql, -1, &st, NULL); if (rc == SQLITE_OK) rc = sqlite3_bind_blob(st, 1, message_id, 32, SQLITE_STATIC); if (rc == SQLITE_OK) rc = sqlite3_bind_text(st, 2, text, (int)len, SQLITE_STATIC); @@ -152,14 +153,19 @@ int chat_core_edit_message(struct UTUN_INSTANCE* inst, const char* channel, cons if (rc != SQLITE_ROW) goto done; ts = (uint64_t)sqlite3_column_int64(st, 0); uint64_t author = (uint64_t)sqlite3_column_int64(st, 1), ver = (uint64_t)sqlite3_column_int64(st, 3); + observed_ver = ver; const char* ct = (const char*)sqlite3_column_text(st, 5); - if (author != cc->my_node_id || !ct || (strcmp(ct, "text") && strcmp(ct, "text/plain"))) goto done; - if (ver != expected_ver) { result = -2; goto done; } + if (author != cc->my_node_id) { stage = "foreign author"; goto done; } + if (!ct || (strcmp(ct, "text") && strcmp(ct, "text/plain"))) { stage = "unsupported content type"; goto done; } + if (ver != expected_ver) { stage = "stale editor version"; result = -2; goto done; } + revision.ver = ver; revision.edited_at = (uint64_t)sqlite3_column_int64(st, 4); const char* old = (const char*)sqlite3_column_text(st, 6); - if (old && (size_t)sqlite3_column_bytes(st, 6) == len && !memcmp(old, text, len)) { result = 0; goto done; } + if (old && (size_t)sqlite3_column_bytes(st, 6) == len && !memcmp(old, text, len)) { stage = "unchanged"; result = 0; goto done; } const void* nonce = sqlite3_column_blob(st, 2); int bytes = sqlite3_column_bytes(st, 7); const char* body = (const char*)sqlite3_column_text(st, 7); - if (!nonce || sqlite3_column_bytes(st, 2) != 16 || !body || bytes <= 0 || bytes > DB_SYNC_MESSAGE_MAX || ver == INT64_MAX) goto done; + if (!nonce || sqlite3_column_bytes(st, 2) != 16) { stage = "invalid stored nonce"; goto done; } + if (!body || bytes <= 0 || bytes > DB_SYNC_MESSAGE_MAX) { stage = "encoded message exceeds limit"; goto done; } + if (ver == INT64_MAX) { stage = "version exhausted"; goto done; } memcpy(revision.nonce, nonce, 16); revision.ver = ver + 1; uint64_t edited = (uint64_t)sqlite3_column_int64(st, 4); revision.edited_at = db_sync_next_timestamp(si); @@ -168,15 +174,16 @@ int chat_core_edit_message(struct UTUN_INSTANCE* inst, const char* channel, cons memcpy(json, body, bytes); json[bytes] = 0; rc = sqlite3_finalize(st); st = NULL; uint8_t signature[64]; + stage = "sign/store"; if (rc == SQLITE_OK && db_sync_sign_message(db_sync_instance_group_id(si), ts, author, &revision, json, bytes, inst->my_ed25519_privkey, signature) == 0 && db_sync_insert_signed(si, json, bytes, signature, 64, ts, NULL, &revision) == 0) result = 0; done: sqlite3_finalize(st); - if (result) DEBUG_WARN(DEBUG_CATEGORY_CHAT_SYNC, "edit: rejected ch=%s expected=%llu result=%d sql=%d: %s", - channel, (unsigned long long)expected_ver, result, rc, sqlite3_errmsg(cc->db)); - else DEBUG_INFO(DEBUG_CATEGORY_CHAT_SYNC, "edit: saved ch=%s id=%02x%02x%02x%02x ver=%llu edited=%llu", - channel, message_id[0], message_id[1], message_id[2], message_id[3], + if (result) DEBUG_WARN(DEBUG_CATEGORY_CHAT_SYNC, "edit: rejected ch=%s stage=%s expected=%llu stored=%llu result=%d sql=%d: %s", + channel, stage, (unsigned long long)expected_ver, (unsigned long long)observed_ver, result, rc, sqlite3_errmsg(cc->db)); + else DEBUG_INFO(DEBUG_CATEGORY_CHAT_SYNC, "edit: %s ch=%s id=%02x%02x%02x%02x ver=%llu edited=%llu", + !strcmp(stage, "unchanged") ? "unchanged" : "saved", channel, message_id[0], message_id[1], message_id[2], message_id[3], (unsigned long long)(revision.ver ? revision.ver : expected_ver), (unsigned long long)revision.edited_at); return result; }