queue signature validation per-channel to ensure correct ordering

pull/1/head
ansuz 5 years ago
parent ddd12bfd7a
commit 02347d134a

@ -38,6 +38,7 @@ module.exports.create = function (config, cb) {
channel_cache: {}, channel_cache: {},
queueStorage: WriteQueue(), queueStorage: WriteQueue(),
queueDeletes: WriteQueue(), queueDeletes: WriteQueue(),
queueValidation: WriteQueue(),
batchIndexReads: BatchRead("HK_GET_INDEX"), batchIndexReads: BatchRead("HK_GET_INDEX"),
batchMetadata: BatchRead('GET_METADATA'), batchMetadata: BatchRead('GET_METADATA'),

@ -859,23 +859,30 @@ HK.onChannelMessage = function (Env, Server, channel, msgStruct) {
// trim the checkpoint indicator off the message if it's present // trim the checkpoint indicator off the message if it's present
let signedMsg = (isCp) ? msgStruct[4].replace(CHECKPOINT_PATTERN, '') : msgStruct[4]; let signedMsg = (isCp) ? msgStruct[4].replace(CHECKPOINT_PATTERN, '') : msgStruct[4];
// convert the message from a base64 string into a Uint8Array
//const txid = Util.uid();
// Listen for messages // Listen for messages
//console.log(+new Date(), "Send verification request"); /* queueing this helps avoid race conditions in which workers
Env.validateMessage(signedMsg, metadata.validateKey, w(function (err) { validate and write messages in a different order than they were received.
// no errors means success For best effect the validate and store should actually be queued atomically,
if (!err) { return; } but this is a step in the right direction.
// validation can fail in multiple ways */
if (err === 'FAILED') { var proceed = w();
// we log this case, but not others for some reason Env.queueValidation(channel.id, function (next) {
Log.info("HK_SIGNED_MESSAGE_REJECTED", 'Channel '+channel.id); Env.validateMessage(signedMsg, metadata.validateKey, function (err) {
} // always go on to the next item in the queue regardless of the outcome
// always abort if there was an error... next();
return void w.abort();
})); // no errors means success
if (!err) { return proceed(); }
// validation can fail in multiple ways
if (err === 'FAILED') {
// we log this case, but not others for some reason
Log.info("HK_SIGNED_MESSAGE_REJECTED", 'Channel '+channel.id);
}
// always abort if there was an error...
return void w.abort();
});
});
}).nThen(function () { }).nThen(function () {
// do checkpoint stuff... // do checkpoint stuff...

Loading…
Cancel
Save