debug: Add an /ebt endpoint to examine the state of EBT clocks. Prevent invalid identities from getting in there, now that I see them.
All checks were successful
Build Tilde Friends / Build-All (push) Successful in 28m58s

This commit is contained in:
2025-03-09 11:28:47 -04:00
parent 973cd53266
commit 67b84830cd
3 changed files with 109 additions and 18 deletions

View File

@ -7,6 +7,7 @@
#include "uv.h"
#include <stdlib.h>
#include <string.h>
typedef struct _ebt_entry_t
@ -57,6 +58,12 @@ static int _ebt_entry_compare(const void* a, const void* b)
static ebt_entry_t* _ebt_get_entry(tf_ssb_ebt_t* ebt, const char* id)
{
uint8_t bin[k_id_bin_len];
if (!tf_ssb_id_str_to_bin(bin, id))
{
return NULL;
}
int index = tf_util_insert_index(id, ebt->entries, ebt->entries_count, sizeof(ebt_entry_t), _ebt_entry_compare);
if (index < ebt->entries_count && strcmp(id, ebt->entries[index].id) == 0)
{
@ -104,21 +111,24 @@ void tf_ssb_ebt_receive_clock(tf_ssb_ebt_t* ebt, JSContext* context, JSValue clo
JS_ToInt64(context, &sequence, in_clock);
ebt_entry_t* entry = _ebt_get_entry(ebt, author);
if (sequence < 0)
if (entry)
{
entry->in = -1;
entry->in_replicate = false;
entry->in_receive = false;
}
else
{
entry->in = sequence >> 1;
entry->in_replicate = true;
entry->in_receive = (sequence & 1) == 0;
}
if (!entry->in_receive)
{
tf_ssb_connection_remove_new_message_request(ebt->connection, author);
if (sequence < 0)
{
entry->in = -1;
entry->in_replicate = false;
entry->in_receive = false;
}
else
{
entry->in = sequence >> 1;
entry->in_replicate = true;
entry->in_receive = (sequence & 1) == 0;
}
if (!entry->in_receive)
{
tf_ssb_connection_remove_new_message_request(ebt->connection, author);
}
}
JS_FreeCString(context, author);
JS_FreeValue(context, key);
@ -154,7 +164,8 @@ static void _ebt_add_to_clock(ebt_get_clock_t* work, const char* id, int64_t val
{
int count = work->clock ? work->clock->count : 0;
ebt_entry_t* entry = _ebt_get_entry(work->ebt, id);
if ((replicate && !entry->out_replicate) || (receive && !entry->out_receive) || ((replicate || receive || entry->out_replicate || entry->out_receive) && entry->out != value))
if (entry &&
((replicate && !entry->out_replicate) || (receive && !entry->out_receive) || ((replicate || receive || entry->out_replicate || entry->out_receive) && entry->out != value)))
{
entry->out = value;
entry->out_replicate = entry->out_replicate || replicate;
@ -303,10 +314,13 @@ void tf_ssb_ebt_set_messages_sent(tf_ssb_ebt_t* ebt, const char* id, int64_t seq
{
uv_mutex_lock(&ebt->mutex);
ebt_entry_t* entry = _ebt_get_entry(ebt, id);
entry->in = tf_max(entry->in, sequence);
if (entry->in == entry->out && (tf_ssb_connection_get_flags(ebt->connection) & k_tf_ssb_connect_flag_one_shot) == 0)
if (entry)
{
tf_ssb_connection_add_new_message_request(ebt->connection, id, tf_ssb_connection_get_ebt_request_number(ebt->connection), false);
entry->in = tf_max(entry->in, sequence);
if (entry->in == entry->out && (tf_ssb_connection_get_flags(ebt->connection) & k_tf_ssb_connect_flag_one_shot) == 0)
{
tf_ssb_connection_add_new_message_request(ebt->connection, id, tf_ssb_connection_get_ebt_request_number(ebt->connection), false);
}
}
uv_mutex_unlock(&ebt->mutex);
}
@ -320,3 +334,25 @@ void tf_ssb_ebt_set_send_clock_pending(tf_ssb_ebt_t* ebt, int pending)
{
ebt->send_clock_pending = pending;
}
void tf_ssb_ebt_debug_clock(tf_ssb_ebt_t* ebt, JSContext* context, JSValue debug)
{
uv_mutex_lock(&ebt->mutex);
for (int i = 0; i < ebt->entries_count; i++)
{
ebt_entry_t* entry = &ebt->entries[i];
JSValue clock = JS_NewObject(context);
JSValue out = JS_NewObject(context);
JSValue in = JS_NewObject(context);
JS_SetPropertyStr(context, out, "value", JS_NewInt64(context, entry->out));
JS_SetPropertyStr(context, out, "replicate", JS_NewBool(context, entry->out_replicate));
JS_SetPropertyStr(context, out, "receive", JS_NewBool(context, entry->out_receive));
JS_SetPropertyStr(context, clock, "out", out);
JS_SetPropertyStr(context, in, "value", JS_NewInt64(context, entry->in));
JS_SetPropertyStr(context, in, "replicate", JS_NewBool(context, entry->in_replicate));
JS_SetPropertyStr(context, in, "receive", JS_NewBool(context, entry->in_receive));
JS_SetPropertyStr(context, clock, "in", in);
JS_SetPropertyStr(context, debug, entry->id, clock);
}
uv_mutex_unlock(&ebt->mutex);
}