From 7c7acbeae68580254d8a953393f751d2a1689a8c Mon Sep 17 00:00:00 2001 From: ansuz Date: Mon, 28 Jun 2021 15:07:48 +0530 Subject: [PATCH] delegate block validation to workers --- lib/commands/block.js | 20 +++++++++++++------- lib/workers/db-worker.js | 4 ++++ lib/workers/index.js | 9 +++++++++ 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/lib/commands/block.js b/lib/commands/block.js index 947740908..ea6a19dc2 100644 --- a/lib/commands/block.js +++ b/lib/commands/block.js @@ -27,7 +27,7 @@ const BlockStore = require("../storage/block"); author of the block, since we assume that the block will have been encrypted with xsalsa20-poly1305 which is authenticated. */ -var validateLoginBlock = function (Env, publicKey, signature, block, _cb) { // FIXME BLOCKS +Block.validateLoginBlock = function (Env, publicKey, signature, block, _cb) { var cb = Util.once(Util.mkAsync(_cb)); // convert the public key to a Uint8Array and validate it @@ -67,7 +67,7 @@ var validateLoginBlock = function (Env, publicKey, signature, block, _cb) { // F // call back with (err) if unsuccessful if (!verified) { return void cb("E_COULD_NOT_VERIFY"); } - return void cb(null, u8_block); + return void cb(null, block); }; Block.validateAncestorProof = function (Env, proof, _cb) { @@ -135,20 +135,26 @@ Block.writeLoginBlock = function (Env, safeKey, msg, _cb) { previousKey = provenKey; })); }).nThen(function (w) { - validateLoginBlock(Env, publicKey, signature, block, w(function (e, _validatedBlock) { + Env.validateLoginBlock(publicKey, signature, block, w(function (e, _validatedBlock) { if (e) { w.abort(); return void cb(e); } - if (!(_validatedBlock instanceof Uint8Array)) { + if (typeof(_validatedBlock) !== 'string') { w.abort(); - return void cb('E_INVALID_BLOCK'); + return void cb('E_INVALID_BLOCK_RETURNED'); } validatedBlock = _validatedBlock; })); }).nThen(function () { - BlockStore.write(Env, publicKey, Buffer.from(validatedBlock), function (err) { + var buffer; + try { + buffer = Buffer.from(Nacl.util.decodeBase64(validatedBlock)); + } catch (err) { + return void cb('E_BLOCK_DESERIALIZATION'); + } + BlockStore.write(Env, publicKey, buffer, function (err) { Env.Log.info('BLOCK_WRITE_BY_OWNER', { safeKey: safeKey, blockId: publicKey, @@ -185,7 +191,7 @@ Block.removeLoginBlock = function (Env, safeKey, msg, _cb) { return void cb("INCORRECT_KEY"); } }).nThen(function () { - validateLoginBlock(Env, publicKey, signature, DELETE_BLOCK, function (e /*::, validatedBlock */) { + Env.validateLoginBlock(publicKey, signature, DELETE_BLOCK, function (e) { if (e) { return void cb(e); } BlockStore.archive(Env, publicKey, function (err) { Env.Log.info('ARCHIVAL_BLOCK_BY_OWNER_RPC', { diff --git a/lib/workers/db-worker.js b/lib/workers/db-worker.js index 137b756b4..2371240ec 100644 --- a/lib/workers/db-worker.js +++ b/lib/workers/db-worker.js @@ -697,6 +697,10 @@ COMMANDS.VALIDATE_ANCESTOR_PROOF = function (data, cb) { Block.validateAncestorProof(Env, data && data.proof, cb); }; +COMMANDS.VALIDATE_LOGIN_BLOCK = function (data, cb) { + Block.validateLoginBlock(Env, data.publicKey, data.signature, data.block, cb); +}; + process.on('message', function (data) { if (!data || !data.txid || !data.pid) { return void process.send({ diff --git a/lib/workers/index.js b/lib/workers/index.js index 85c66eeb5..c2bfb5740 100644 --- a/lib/workers/index.js +++ b/lib/workers/index.js @@ -451,6 +451,15 @@ Workers.initialize = function (Env, config, _cb) { }, cb); }; + Env.validateLoginBlock = function (publicKey, signature, block, cb) { + sendCommand({ + command: 'VALIDATE_LOGIN_BLOCK', + publicKey: publicKey, + signature: signature, + block: block, + }, cb); + }; + cb(void 0); }); };