delegate block validation to workers

pull/1/head
ansuz 3 years ago
parent ba1a7b37e1
commit 7c7acbeae6

@ -27,7 +27,7 @@ const BlockStore = require("../storage/block");
author of the block, since we assume that the block will have been author of the block, since we assume that the block will have been
encrypted with xsalsa20-poly1305 which is authenticated. 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)); var cb = Util.once(Util.mkAsync(_cb));
// convert the public key to a Uint8Array and validate it // 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 // call back with (err) if unsuccessful
if (!verified) { return void cb("E_COULD_NOT_VERIFY"); } 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) { Block.validateAncestorProof = function (Env, proof, _cb) {
@ -135,20 +135,26 @@ Block.writeLoginBlock = function (Env, safeKey, msg, _cb) {
previousKey = provenKey; previousKey = provenKey;
})); }));
}).nThen(function (w) { }).nThen(function (w) {
validateLoginBlock(Env, publicKey, signature, block, w(function (e, _validatedBlock) { Env.validateLoginBlock(publicKey, signature, block, w(function (e, _validatedBlock) {
if (e) { if (e) {
w.abort(); w.abort();
return void cb(e); return void cb(e);
} }
if (!(_validatedBlock instanceof Uint8Array)) { if (typeof(_validatedBlock) !== 'string') {
w.abort(); w.abort();
return void cb('E_INVALID_BLOCK'); return void cb('E_INVALID_BLOCK_RETURNED');
} }
validatedBlock = _validatedBlock; validatedBlock = _validatedBlock;
})); }));
}).nThen(function () { }).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', { Env.Log.info('BLOCK_WRITE_BY_OWNER', {
safeKey: safeKey, safeKey: safeKey,
blockId: publicKey, blockId: publicKey,
@ -185,7 +191,7 @@ Block.removeLoginBlock = function (Env, safeKey, msg, _cb) {
return void cb("INCORRECT_KEY"); return void cb("INCORRECT_KEY");
} }
}).nThen(function () { }).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); } if (e) { return void cb(e); }
BlockStore.archive(Env, publicKey, function (err) { BlockStore.archive(Env, publicKey, function (err) {
Env.Log.info('ARCHIVAL_BLOCK_BY_OWNER_RPC', { Env.Log.info('ARCHIVAL_BLOCK_BY_OWNER_RPC', {

@ -697,6 +697,10 @@ COMMANDS.VALIDATE_ANCESTOR_PROOF = function (data, cb) {
Block.validateAncestorProof(Env, data && data.proof, 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) { process.on('message', function (data) {
if (!data || !data.txid || !data.pid) { if (!data || !data.txid || !data.pid) {
return void process.send({ return void process.send({

@ -451,6 +451,15 @@ Workers.initialize = function (Env, config, _cb) {
}, cb); }, cb);
}; };
Env.validateLoginBlock = function (publicKey, signature, block, cb) {
sendCommand({
command: 'VALIDATE_LOGIN_BLOCK',
publicKey: publicKey,
signature: signature,
block: block,
}, cb);
};
cb(void 0); cb(void 0);
}); });
}; };

Loading…
Cancel
Save