diff --git a/src/ssb.db.c b/src/ssb.db.c index 0edbf654..c4af262b 100644 --- a/src/ssb.db.c +++ b/src/ssb.db.c @@ -731,6 +731,8 @@ bool tf_ssb_db_check(sqlite3* db, const char* check_author) sqlite3_bind_text(statement, 1, check_author, -1, NULL); } char previous_id[k_id_base64_len]; + int64_t previous_sequence = -1; + char previous_author[k_id_base64_len] = { 0 }; while (sqlite3_step(statement) == SQLITE_ROW) { const char* id = (const char*)sqlite3_column_text(statement, 0); @@ -748,28 +750,47 @@ bool tf_ssb_db_check(sqlite3* db, const char* check_author) bool actual_sequence_before_author = false; JSValue j = JS_JSONStringify(context, message, JS_NULL, JS_NewInt32(context, 2)); const char* jv = JS_ToCString(context, j); - if (tf_ssb_verify_and_strip_signature(context, message, actual_id, sizeof(actual_id), out_signature, sizeof(out_signature), &actual_sequence_before_author)) + + bool delete_following = false; + if (strcmp(author, previous_author)) { - if (previous && strcmp(previous, previous_id)) - { - printf("%s:%d previous was %s should be %s\n", id, (int)sequence, previous_id, previous); - } - if (strcmp(id, actual_id)) - { - if (_tf_ssb_update_message_id(db, id, actual_id)) - { - printf("updated %s to %s\n", id, actual_id); - } - else - { - printf("failed to update %s to %s\n", id, actual_id); - } - } + printf("%s\n", author); + } + + if (strcmp(author, previous_author) == 0 && sequence != previous_sequence + 1) + { + printf("Detected gap in messages for %s at sequence = %" PRId64 " => %" PRId64 ".\n", author, previous_sequence, sequence); + delete_following = true; } else { - printf("%s sequence=%" PRId64 " unable to verify signature for %s sequence_before_author=%d message=[%.*s]\n", author, sequence, id, sequence_before_author, (int)strlen(jv), jv); + if (tf_ssb_verify_and_strip_signature(context, message, actual_id, sizeof(actual_id), out_signature, sizeof(out_signature), &actual_sequence_before_author)) + { + if (previous && strcmp(previous, previous_id)) + { + printf("%s:%d previous was %s should be %s\n", id, (int)sequence, previous_id, previous); + } + if (strcmp(id, actual_id)) + { + if (_tf_ssb_update_message_id(db, id, actual_id)) + { + printf("updated %s to %s\n", id, actual_id); + } + else + { + printf("failed to update %s to %s\n", id, actual_id); + } + } + } + else + { + printf("%s sequence=%" PRId64 " unable to verify signature for %s sequence_before_author=%d message=[%.*s]\n", author, sequence, id, sequence_before_author, (int)strlen(jv), jv); + delete_following = true; + } + } + if (delete_following) + { printf("Deleting author = %s sequence >= %" PRId64 ".\n", author, sequence); sqlite3_stmt* delete_statement = NULL; if (sqlite3_prepare(db, "DELETE FROM messages WHERE author = ? AND sequence >= ?", -1, &delete_statement, NULL) == SQLITE_OK) @@ -785,6 +806,10 @@ bool tf_ssb_db_check(sqlite3* db, const char* check_author) sqlite3_finalize(delete_statement); } } + + snprintf(previous_author, sizeof(previous_author), "%s", author); + previous_sequence = sequence; + JS_FreeCString(context, jv); JS_FreeValue(context, j); snprintf(previous_id, sizeof(previous_id), "%s", id);