From 14feef175772508c8038684ebe4ace5f352590d1 Mon Sep 17 00:00:00 2001 From: ansuz Date: Wed, 25 Mar 2020 18:37:42 -0400 Subject: [PATCH] calculate pin list checksums in a worker --- lib/commands/pin-rpc.js | 16 +--------------- lib/hk-util.js | 9 ++++++++- lib/workers/check-signature.js | 21 +++++++++++++++++++-- 3 files changed, 28 insertions(+), 18 deletions(-) diff --git a/lib/commands/pin-rpc.js b/lib/commands/pin-rpc.js index d77732ce9..27a8ae28d 100644 --- a/lib/commands/pin-rpc.js +++ b/lib/commands/pin-rpc.js @@ -142,23 +142,9 @@ var getFreeSpace = Pinning.getFreeSpace = function (Env, safeKey, cb) { }); }; -var hashChannelList = function (A) { - var uniques = []; - - A.forEach(function (a) { - if (uniques.indexOf(a) === -1) { uniques.push(a); } - }); - uniques.sort(); - - var hash = Nacl.util.encodeBase64(Nacl.hash(Nacl - .util.decodeUTF8(JSON.stringify(uniques)))); - - return hash; -}; - var getHash = Pinning.getHash = function (Env, safeKey, cb) { getChannelList(Env, safeKey, function (channels) { - cb(void 0, hashChannelList(channels)); + Env.hashChannelList(channels, cb); }); }; diff --git a/lib/hk-util.js b/lib/hk-util.js index 57e0afe36..799ec547f 100644 --- a/lib/hk-util.js +++ b/lib/hk-util.js @@ -951,7 +951,7 @@ HK.initializeValidationWorkers = function (Env) { worker.on('message', function (res) { if (!res || !res.txid) { return; } //console.log(+new Date(), "Received verification response"); - response.handle(res.txid, [res.error]); + response.handle(res.txid, [res.error, res.value]); }); // Spawn a new process in one ends worker.on('exit', function () { @@ -1001,6 +1001,13 @@ HK.initializeValidationWorkers = function (Env) { key: publicKey, }, cb); }; + + Env.hashChannelList = function (channels, cb) { + send({ + command: 'HASH_CHANNEL_LIST', + channels: channels, + }, cb); + }; }; /* onChannelMessage diff --git a/lib/workers/check-signature.js b/lib/workers/check-signature.js index 846f41d3b..5ed58ac7c 100644 --- a/lib/workers/check-signature.js +++ b/lib/workers/check-signature.js @@ -73,17 +73,34 @@ COMMANDS.DETACHED = function (data, cb) { cb(); }; +COMMANDS.HASH_CHANNEL_LIST = function (data, cb) { + var channels = data.channels; + if (!Array.isArray(channels)) { return void cb('INVALID_CHANNEL_LIST'); } + var uniques = []; + + channels.forEach(function (a) { + if (uniques.indexOf(a) === -1) { uniques.push(a); } + }); + uniques.sort(); + + var hash = Nacl.util.encodeBase64(Nacl.hash(Nacl + .util.decodeUTF8(JSON.stringify(uniques)))); + + cb(void 0, hash); +}; + process.on('message', function (data) { - if (!data || !data.key || !data.msg || !data.txid) { + if (!data || !data.txid) { return void process.send({ error:'E_INVAL' }); } - const cb = function (err) { + const cb = function (err, value) { process.send({ txid: data.txid, error: err, + value: value, }); };