Commit 800a211a authored by Cédric Moreau's avatar Cédric Moreau

Added PoW delay parameter before each PoW computation (optional)

parent 106ba3bf
...@@ -303,6 +303,7 @@ var tasks = { ...@@ -303,6 +303,7 @@ var tasks = {
next(); next();
}); });
}, },
async.apply(simpleInteger, "Time to wait before starting the computation of a new block", "powDelay", conf),
async.apply(simpleInteger, "Acceptable time offset when receiving a new keyblock", "tsInterval", conf), async.apply(simpleInteger, "Acceptable time offset when receiving a new keyblock", "tsInterval", conf),
], done); ], done);
} }
......
...@@ -23,6 +23,7 @@ var ConfigurationSchema = new Schema({ ...@@ -23,6 +23,7 @@ var ConfigurationSchema = new Schema({
sigQty: {"type": Number, "default": 5}, sigQty: {"type": Number, "default": 5},
powZeroMin: {"type": Number, "default": 4}, powZeroMin: {"type": Number, "default": 4},
powPeriod: {"type": Number, "default": 1}, powPeriod: {"type": Number, "default": 1},
powDelay: {"type": Number, "default": 0}, // Delay before starting computation of a new block
participate: {"type": Boolean, "default": true}, // Participate to writing the keychain participate: {"type": Boolean, "default": true}, // Participate to writing the keychain
tsInterval: {"type": Number, "default": 30}, tsInterval: {"type": Number, "default": 30},
}); });
......
...@@ -27,6 +27,12 @@ var computeNextCallback = null; ...@@ -27,6 +27,12 @@ var computeNextCallback = null;
// Flag telling if computation has started // Flag telling if computation has started
var computationActivated = false; var computationActivated = false;
// Timeout var for delaying computation of next block
var computationTimeout = null;
// Flag for saying if timeout was already waited
var computationTimeoutDone = false;
function KeyService (conn, conf, PublicKeyService, PeeringService) { function KeyService (conn, conf, PublicKeyService, PeeringService) {
var KeychainService = this; var KeychainService = this;
...@@ -1488,6 +1494,10 @@ function KeyService (conn, conf, PublicKeyService, PeeringService) { ...@@ -1488,6 +1494,10 @@ function KeyService (conn, conf, PublicKeyService, PeeringService) {
return; return;
} }
computationActivated = true; computationActivated = true;
if (computationTimeout) {
clearTimeout(computationTimeout);
computationTimeout = null;
}
var sigFunc, block, difficulty; var sigFunc, block, difficulty;
async.waterfall([ async.waterfall([
function (next) { function (next) {
...@@ -1516,7 +1526,16 @@ function KeyService (conn, conf, PublicKeyService, PeeringService) { ...@@ -1516,7 +1526,16 @@ function KeyService (conn, conf, PublicKeyService, PeeringService) {
function (res, next){ function (res, next){
if (!res) { if (!res) {
next(null, null, 'Waiting for a root block before computing new blocks'); next(null, null, 'Waiting for a root block before computing new blocks');
} else if (conf.powDelay && !computationTimeoutDone) {
computationTimeoutDone = true;
computationTimeout = setTimeout(function () {
if (computeNextCallback)
computeNextCallback();
}, conf.powDelay*1000);
next(null, null, 'Waiting ' + conf.powDelay + 's before starting computing next block...');
return;
} else { } else {
computationTimeoutDone = false;
KeychainService.prove(res.block, res.signature, res.trial, function (err, proofBlock) { KeychainService.prove(res.block, res.signature, res.trial, function (err, proofBlock) {
next(null, proofBlock, err); next(null, proofBlock, err);
}); });
...@@ -1525,9 +1544,13 @@ function KeyService (conn, conf, PublicKeyService, PeeringService) { ...@@ -1525,9 +1544,13 @@ function KeyService (conn, conf, PublicKeyService, PeeringService) {
], function (err, proofBlock, powCanceled) { ], function (err, proofBlock, powCanceled) {
if (powCanceled) { if (powCanceled) {
logger.warn(powCanceled); logger.warn(powCanceled);
computeNextCallback = async.apply(done, null, null); computeNextCallback = function () {
computeNextCallback = null;
done(null, null);
};
computationActivated = false computationActivated = false
} else { } else {
// Proof-of-work found
computationActivated = false computationActivated = false
done(err, proofBlock); done(err, proofBlock);
} }
......
...@@ -47,6 +47,7 @@ program ...@@ -47,6 +47,7 @@ program
.option('--sigQty <number>', 'Minimum number of required certifications to be a member/stay as a member') .option('--sigQty <number>', 'Minimum number of required certifications to be a member/stay as a member')
.option('--powZeroMin <number>', 'Minimum number of leading zeros for a proof-of-work') .option('--powZeroMin <number>', 'Minimum number of leading zeros for a proof-of-work')
.option('--powPeriod <number>', 'Number of blocks to wait to decrease proof-of-work difficulty by one') .option('--powPeriod <number>', 'Number of blocks to wait to decrease proof-of-work difficulty by one')
.option('--powDelay <number>', 'Number of seconds to wait before starting the computation of next block')
.option('--particpate <Y|N>', 'Participate to writing the keychain') .option('--particpate <Y|N>', 'Participate to writing the keychain')
.option('--tsInterval <number>', 'Number of seconds as acceptable offset when receiving a keyblock') .option('--tsInterval <number>', 'Number of seconds as acceptable offset when receiving a keyblock')
.option('--openpgpjs', 'Prefer using embedded Openpgpjs implementation for signing requests') .option('--openpgpjs', 'Prefer using embedded Openpgpjs implementation for signing requests')
...@@ -461,6 +462,7 @@ function overrideConf(conf) { ...@@ -461,6 +462,7 @@ function overrideConf(conf) {
sigQty: program.sigQty, sigQty: program.sigQty,
powZeroMin: program.powZeroMin, powZeroMin: program.powZeroMin,
powPeriod: program.powPeriod, powPeriod: program.powPeriod,
powDelay: program.powDelay,
particpate: program.particpate, particpate: program.particpate,
tsInterval: program.tsInterval tsInterval: program.tsInterval
} }
...@@ -487,6 +489,7 @@ function overrideConf(conf) { ...@@ -487,6 +489,7 @@ function overrideConf(conf) {
if (cli.ucp.sigQty) conf.sigQty = cli.ucp.sigQty; if (cli.ucp.sigQty) conf.sigQty = cli.ucp.sigQty;
if (cli.ucp.powZeroMin) conf.powZeroMin = cli.ucp.powZeroMin; if (cli.ucp.powZeroMin) conf.powZeroMin = cli.ucp.powZeroMin;
if (cli.ucp.powPeriod) conf.powPeriod = cli.ucp.powPeriod; if (cli.ucp.powPeriod) conf.powPeriod = cli.ucp.powPeriod;
if (cli.ucp.powDelay) conf.powDelay = cli.ucp.powDelay;
if (cli.ucp.participate) conf.participate = cli.ucp.participate == 'Y'; if (cli.ucp.participate) conf.participate = cli.ucp.participate == 'Y';
if (cli.ucp.tsInterval) conf.tsInterval = cli.ucp.tsInterval; if (cli.ucp.tsInterval) conf.tsInterval = cli.ucp.tsInterval;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment