Move ssb.appendMessageWithIdentity's DB work off the main thread.

This commit is contained in:
Cory McWilliams 2024-06-16 07:51:06 -04:00
parent 2bc71a18a6
commit 991022adfc
4 changed files with 70 additions and 35 deletions

View File

@ -1784,18 +1784,28 @@ static bool _tf_ssb_connection_box_stream_recv(tf_ssb_connection_t* connection)
return true; return true;
} }
JSValue tf_ssb_sign_message(tf_ssb_t* ssb, const char* author, const uint8_t* private_key, JSValue message) JSValue tf_ssb_sign_message(tf_ssb_t* ssb, const char* author, const uint8_t* private_key, JSValue message, const char* previous_id, int64_t previous_sequence)
{ {
char previous_id[crypto_hash_sha256_BYTES * 2]; char actual_previous_id[crypto_hash_sha256_BYTES * 2];
int64_t previous_sequence = 0; int64_t actual_previous_sequence = 0;
bool have_previous = tf_ssb_db_get_latest_message_by_author(ssb, author, &previous_sequence, previous_id, sizeof(previous_id)); bool have_previous = false;
if (previous_id)
{
have_previous = *previous_id && previous_sequence > 0;
snprintf(actual_previous_id, sizeof(actual_previous_id), "%s", previous_id);
actual_previous_sequence = previous_sequence;
}
else
{
have_previous = tf_ssb_db_get_latest_message_by_author(ssb, author, &actual_previous_sequence, actual_previous_id, sizeof(actual_previous_id));
}
JSContext* context = ssb->context; JSContext* context = ssb->context;
JSValue root = JS_NewObject(context); JSValue root = JS_NewObject(context);
JS_SetPropertyStr(context, root, "previous", have_previous ? JS_NewString(context, previous_id) : JS_NULL); JS_SetPropertyStr(context, root, "previous", have_previous ? JS_NewString(context, actual_previous_id) : JS_NULL);
JS_SetPropertyStr(context, root, "author", JS_NewString(context, author)); JS_SetPropertyStr(context, root, "author", JS_NewString(context, author));
JS_SetPropertyStr(context, root, "sequence", JS_NewInt64(context, previous_sequence + 1)); JS_SetPropertyStr(context, root, "sequence", JS_NewInt64(context, actual_previous_sequence + 1));
int64_t now = (int64_t)time(NULL); int64_t now = (int64_t)time(NULL);
JS_SetPropertyStr(context, root, "timestamp", JS_NewInt64(context, now * 1000LL)); JS_SetPropertyStr(context, root, "timestamp", JS_NewInt64(context, now * 1000LL));

View File

@ -278,9 +278,11 @@ void tf_ssb_run(tf_ssb_t* ssb);
** @param author The author's public key. ** @param author The author's public key.
** @param private_key The author's private key. ** @param private_key The author's private key.
** @param message The message to sign. ** @param message The message to sign.
** @param previous_id The ID of the previous message in the feed. Optional.
** @param previous_sequence The sequence number of the previous message in the feed. Optional.
** @return The signed message. ** @return The signed message.
*/ */
JSValue tf_ssb_sign_message(tf_ssb_t* ssb, const char* author, const uint8_t* private_key, JSValue message); JSValue tf_ssb_sign_message(tf_ssb_t* ssb, const char* author, const uint8_t* private_key, JSValue message, const char* previous_id, int64_t previous_sequence);
/** /**
** Get the server's identity. ** Get the server's identity.

View File

@ -663,8 +663,15 @@ static JSValue _tf_ssb_getIdentityInfo(JSContext* context, JSValueConst this_val
typedef struct _append_message_t typedef struct _append_message_t
{ {
char id[k_id_base64_len];
uint8_t private_key[crypto_sign_SECRETKEYBYTES];
bool got_private_key;
char previous_id[512];
int64_t previous_sequence;
JSContext* context; JSContext* context;
JSValue promise[2]; JSValue promise[2];
JSValue message;
char user[];
} append_message_t; } append_message_t;
static void _tf_ssb_appendMessage_finish(append_message_t* async, bool success, JSValue result) static void _tf_ssb_appendMessage_finish(append_message_t* async, bool success, JSValue result)
@ -672,6 +679,7 @@ static void _tf_ssb_appendMessage_finish(append_message_t* async, bool success,
JSValue error = JS_Call(async->context, success ? async->promise[0] : async->promise[1], JS_UNDEFINED, 1, &result); JSValue error = JS_Call(async->context, success ? async->promise[0] : async->promise[1], JS_UNDEFINED, 1, &result);
tf_util_report_error(async->context, error); tf_util_report_error(async->context, error);
JS_FreeValue(async->context, error); JS_FreeValue(async->context, error);
JS_FreeValue(async->context, async->message);
JS_FreeValue(async->context, async->promise[0]); JS_FreeValue(async->context, async->promise[0]);
JS_FreeValue(async->context, async->promise[1]); JS_FreeValue(async->context, async->promise[1]);
tf_free(async); tf_free(async);
@ -688,35 +696,50 @@ static void _tf_ssb_appendMessageWithIdentity_callback(const char* id, bool veri
_tf_ssb_appendMessage_finish(async, verified, result); _tf_ssb_appendMessage_finish(async, verified, result);
} }
static void _tf_ssb_append_message_with_identity_get_key_work(tf_ssb_t* ssb, void* user_data)
{
append_message_t* work = user_data;
work->got_private_key = tf_ssb_db_identity_get_private_key(ssb, work->user, work->id, work->private_key, sizeof(work->private_key));
tf_ssb_db_get_latest_message_by_author(ssb, work->id, &work->previous_sequence, work->previous_id, sizeof(work->previous_id));
}
static void _tf_ssb_append_message_with_identity_get_key_after_work(tf_ssb_t* ssb, int status, void* user_data)
{
append_message_t* work = user_data;
if (work->got_private_key)
{
JSValue signed_message = tf_ssb_sign_message(ssb, work->id, work->private_key, work->message, work->previous_id, work->previous_sequence);
tf_ssb_verify_strip_and_store_message(ssb, signed_message, _tf_ssb_appendMessageWithIdentity_callback, work);
JS_FreeValue(work->context, signed_message);
}
else
{
_tf_ssb_appendMessage_finish(work, false, JS_ThrowInternalError(work->context, "Unable to get private key for user %s with identity %s.", work->user, work->id));
}
}
static JSValue _tf_ssb_appendMessageWithIdentity(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) static JSValue _tf_ssb_appendMessageWithIdentity(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
{ {
append_message_t* async = tf_malloc(sizeof(append_message_t));
*async = (append_message_t) { .context = context };
JSValue result = JS_NewPromiseCapability(context, async->promise);
tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId); tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId);
if (ssb) if (!ssb)
{ {
const char* user = JS_ToCString(context, argv[0]); return JS_ThrowInternalError(context, "No SSB instance.");
}
size_t user_length = 0;
const char* user = JS_ToCStringLen(context, &user_length, argv[0]);
const char* id = JS_ToCString(context, argv[1]); const char* id = JS_ToCString(context, argv[1]);
uint8_t private_key[crypto_sign_SECRETKEYBYTES];
if (tf_ssb_db_identity_get_private_key(ssb, user, id, private_key, sizeof(private_key))) append_message_t* work = tf_malloc(sizeof(append_message_t) + user_length + 1);
{ *work = (append_message_t) { .context = context, .message = JS_DupValue(context, argv[2]) };
JSValue signed_message = tf_ssb_sign_message(ssb, id, private_key, argv[2]); memcpy(work->user, user, user_length + 1);
tf_ssb_verify_strip_and_store_message(ssb, signed_message, _tf_ssb_appendMessageWithIdentity_callback, async); snprintf(work->id, sizeof(work->id), "%s", id);
JS_FreeValue(context, signed_message);
}
else
{
_tf_ssb_appendMessage_finish(async, false, JS_ThrowInternalError(context, "Unable to get private key for user %s with identity %s.", user, id));
}
JS_FreeCString(context, id); JS_FreeCString(context, id);
JS_FreeCString(context, user); JS_FreeCString(context, user);
}
else JSValue result = JS_NewPromiseCapability(context, work->promise);
{ tf_ssb_run_work(ssb, _tf_ssb_append_message_with_identity_get_key_work, _tf_ssb_append_message_with_identity_get_key_after_work, work);
_tf_ssb_appendMessage_finish(async, false, JS_ThrowInternalError(context, "No SSB instance."));
}
return result; return result;
} }

View File

@ -192,7 +192,7 @@ void tf_ssb_test_ssb(const tf_test_options_t* options)
JS_SetPropertyStr(context0, obj, "type", JS_NewString(context0, "post")); JS_SetPropertyStr(context0, obj, "type", JS_NewString(context0, "post"));
JS_SetPropertyStr(context0, obj, "text", JS_NewString(context0, "Hello, world!")); JS_SetPropertyStr(context0, obj, "text", JS_NewString(context0, "Hello, world!"));
bool stored = false; bool stored = false;
JSValue signed_message = tf_ssb_sign_message(ssb0, id0, priv0, obj); JSValue signed_message = tf_ssb_sign_message(ssb0, id0, priv0, obj, NULL, 0);
tf_ssb_verify_strip_and_store_message(ssb0, signed_message, _message_stored, &stored); tf_ssb_verify_strip_and_store_message(ssb0, signed_message, _message_stored, &stored);
JS_FreeValue(context0, signed_message); JS_FreeValue(context0, signed_message);
_wait_stored(ssb0, &stored); _wait_stored(ssb0, &stored);
@ -202,7 +202,7 @@ void tf_ssb_test_ssb(const tf_test_options_t* options)
JS_SetPropertyStr(context0, obj, "type", JS_NewString(context0, "post")); JS_SetPropertyStr(context0, obj, "type", JS_NewString(context0, "post"));
JS_SetPropertyStr(context0, obj, "text", JS_NewString(context0, "First post.")); JS_SetPropertyStr(context0, obj, "text", JS_NewString(context0, "First post."));
stored = false; stored = false;
signed_message = tf_ssb_sign_message(ssb0, id0, priv0, obj); signed_message = tf_ssb_sign_message(ssb0, id0, priv0, obj, NULL, 0);
tf_ssb_verify_strip_and_store_message(ssb0, signed_message, _message_stored, &stored); tf_ssb_verify_strip_and_store_message(ssb0, signed_message, _message_stored, &stored);
JS_FreeValue(context0, signed_message); JS_FreeValue(context0, signed_message);
_wait_stored(ssb0, &stored); _wait_stored(ssb0, &stored);
@ -217,7 +217,7 @@ void tf_ssb_test_ssb(const tf_test_options_t* options)
JS_SetPropertyUint32(context0, mentions, 0, mention); JS_SetPropertyUint32(context0, mentions, 0, mention);
JS_SetPropertyStr(context0, obj, "mentions", mentions); JS_SetPropertyStr(context0, obj, "mentions", mentions);
stored = false; stored = false;
signed_message = tf_ssb_sign_message(ssb0, id0, priv0, obj); signed_message = tf_ssb_sign_message(ssb0, id0, priv0, obj, NULL, 0);
tf_ssb_verify_strip_and_store_message(ssb0, signed_message, _message_stored, &stored); tf_ssb_verify_strip_and_store_message(ssb0, signed_message, _message_stored, &stored);
JS_FreeValue(context0, signed_message); JS_FreeValue(context0, signed_message);
_wait_stored(ssb0, &stored); _wait_stored(ssb0, &stored);
@ -276,7 +276,7 @@ void tf_ssb_test_ssb(const tf_test_options_t* options)
JS_SetPropertyStr(context0, obj, "type", JS_NewString(context0, "post")); JS_SetPropertyStr(context0, obj, "type", JS_NewString(context0, "post"));
JS_SetPropertyStr(context0, obj, "text", JS_NewString(context0, "Message to self.")); JS_SetPropertyStr(context0, obj, "text", JS_NewString(context0, "Message to self."));
stored = false; stored = false;
signed_message = tf_ssb_sign_message(ssb0, id0, priv0, obj); signed_message = tf_ssb_sign_message(ssb0, id0, priv0, obj, NULL, 0);
tf_ssb_verify_strip_and_store_message(ssb0, signed_message, _message_stored, &stored); tf_ssb_verify_strip_and_store_message(ssb0, signed_message, _message_stored, &stored);
JS_FreeValue(context0, signed_message); JS_FreeValue(context0, signed_message);
_wait_stored(ssb0, &stored); _wait_stored(ssb0, &stored);
@ -549,7 +549,7 @@ void tf_ssb_test_following(const tf_test_options_t* options)
JS_SetPropertyStr(context, message, "contact", JS_NewString(context, contact)); \ JS_SetPropertyStr(context, message, "contact", JS_NewString(context, contact)); \
JS_SetPropertyStr(context, message, "following", follow ? JS_TRUE : JS_FALSE); \ JS_SetPropertyStr(context, message, "following", follow ? JS_TRUE : JS_FALSE); \
JS_SetPropertyStr(context, message, "blocking", block ? JS_TRUE : JS_FALSE); \ JS_SetPropertyStr(context, message, "blocking", block ? JS_TRUE : JS_FALSE); \
signed_message = tf_ssb_sign_message(ssb0, id, priv, message); \ signed_message = tf_ssb_sign_message(ssb0, id, priv, message, NULL, 0); \
stored = false; \ stored = false; \
tf_ssb_verify_strip_and_store_message(ssb0, signed_message, _message_stored, &stored); \ tf_ssb_verify_strip_and_store_message(ssb0, signed_message, _message_stored, &stored); \
_wait_stored(ssb0, &stored); \ _wait_stored(ssb0, &stored); \
@ -608,7 +608,7 @@ void tf_ssb_test_bench(const tf_test_options_t* options)
for (int i = 0; i < k_messages; i++) for (int i = 0; i < k_messages; i++)
{ {
bool stored = false; bool stored = false;
JSValue signed_message = tf_ssb_sign_message(ssb0, id0, priv0, obj); JSValue signed_message = tf_ssb_sign_message(ssb0, id0, priv0, obj, NULL, 0);
tf_ssb_verify_strip_and_store_message(ssb0, signed_message, _message_stored, &stored); tf_ssb_verify_strip_and_store_message(ssb0, signed_message, _message_stored, &stored);
JS_FreeValue(tf_ssb_get_context(ssb0), signed_message); JS_FreeValue(tf_ssb_get_context(ssb0), signed_message);
_wait_stored(ssb0, &stored); _wait_stored(ssb0, &stored);