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 = {
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),
], done);
}
......
......@@ -23,6 +23,7 @@ var ConfigurationSchema = new Schema({
sigQty: {"type": Number, "default": 5},
powZeroMin: {"type": Number, "default": 4},
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
tsInterval: {"type": Number, "default": 30},
});
......
......@@ -27,6 +27,12 @@ var computeNextCallback = null;
// Flag telling if computation has started
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) {
var KeychainService = this;
......@@ -1488,6 +1494,10 @@ function KeyService (conn, conf, PublicKeyService, PeeringService) {
return;
}
computationActivated = true;
if (computationTimeout) {
clearTimeout(computationTimeout);
computationTimeout = null;
}
var sigFunc, block, difficulty;
async.waterfall([
function (next) {
......@@ -1516,7 +1526,16 @@ function KeyService (conn, conf, PublicKeyService, PeeringService) {
function (res, next){
if (!res) {
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 {
computationTimeoutDone = false;
KeychainService.prove(res.block, res.signature, res.trial, function (err, proofBlock) {
next(null, proofBlock, err);
});
......@@ -1525,9 +1544,13 @@ function KeyService (conn, conf, PublicKeyService, PeeringService) {
], function (err, proofBlock, powCanceled) {
if (powCanceled) {
logger.warn(powCanceled);
computeNextCallback = async.apply(done, null, null);
computeNextCallback = function () {
computeNextCallback = null;
done(null, null);
};
computationActivated = false
} else {
// Proof-of-work found
computationActivated = false
done(err, proofBlock);
}
......
......@@ -47,6 +47,7 @@ program
.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('--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('--tsInterval <number>', 'Number of seconds as acceptable offset when receiving a keyblock')
.option('--openpgpjs', 'Prefer using embedded Openpgpjs implementation for signing requests')
......@@ -461,6 +462,7 @@ function overrideConf(conf) {
sigQty: program.sigQty,
powZeroMin: program.powZeroMin,
powPeriod: program.powPeriod,
powDelay: program.powDelay,
particpate: program.particpate,
tsInterval: program.tsInterval
}
......@@ -487,6 +489,7 @@ function overrideConf(conf) {
if (cli.ucp.sigQty) conf.sigQty = cli.ucp.sigQty;
if (cli.ucp.powZeroMin) conf.powZeroMin = cli.ucp.powZeroMin;
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.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