check authenticated rpc signatures in separate threads

This commit is contained in:
ansuz
2020-03-24 14:31:40 -04:00
parent 0e09c73a60
commit 40251948d4
6 changed files with 131 additions and 107 deletions

View File

@@ -849,7 +849,7 @@ HK.initializeIndexWorkers = function (Env, config, _cb) {
if (idx !== -1) {
workers.splice(idx, 1);
}
var w = fork('lib/compute-index');
var w = fork('lib/workers/compute-index');
initWorker(w, function (err) {
if (err) {
throw new Error(err);
@@ -878,7 +878,7 @@ HK.initializeIndexWorkers = function (Env, config, _cb) {
nThen(function (w) {
OS.cpus().forEach(function () {
initWorker(fork('lib/compute-index'), w(function (err) {
initWorker(fork('lib/workers/compute-index'), w(function (err) {
if (!err) { return; }
w.abort();
return void cb(err);
@@ -898,7 +898,7 @@ HK.initializeValidationWorkers = function (Env) {
// Create our workers
const workers = [];
for (let i = 0; i < numCPUs; i++) {
workers.push(fork('lib/check-signature.js'));
workers.push(fork('lib/workers/check-signature.js'));
}
const response = Util.response();
@@ -911,13 +911,12 @@ HK.initializeValidationWorkers = function (Env) {
});
// Spawn a new process in one ends
worker.on('exit', function () {
// XXX make sure it's dead?
var idx = workers.indexOf(worker);
if (idx !== -1) {
workers.splice(idx, 1);
}
// Spawn a new one
var w = fork('lib/check-signature.js');
var w = fork('lib/workers/check-signature.js');
workers.push(w);
initWorker(w);
});
@@ -925,27 +924,39 @@ HK.initializeValidationWorkers = function (Env) {
workers.forEach(initWorker);
var nextWorker = 0;
Env.validateMessage = function (signedMsg, key, _cb) {
const send = function (msg, _cb) {
// let's be paranoid about asynchrony and only calling back once..
var cb = Util.once(Util.mkAsync(_cb));
var txid = Util.uid();
// expect a response within 15s
response.expect(txid, cb, 15000);
nextWorker = (nextWorker + 1) % workers.length;
if (workers.length === 0 || typeof(workers[nextWorker].send) !== 'function') {
console.error(workers);
throw new Error("INVALID_WORKERS");
}
var cb = Util.once(Util.mkAsync(_cb));
var txid = msg.txid = Util.uid();
// expect a response within 15s
response.expect(txid, cb, 15000);
// Send the request
workers[nextWorker].send({
txid: txid,
workers[nextWorker].send(msg);
};
Env.validateMessage = function (signedMsg, key, cb) {
send({
msg: signedMsg,
key: key,
});
command: 'INLINE',
}, cb);
};
Env.checkSignature = function (signedMsg, signature, publicKey, cb) {
send({
command: 'DETACHED',
sig: signature,
msg: signedMsg,
key: publicKey,
}, cb);
};
};