When checking database integrity, delete messages after a gap.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4005 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
Cory McWilliams 2022-10-14 17:39:08 +00:00
parent 1ab79adb27
commit 954e0227d4

View File

@ -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);