Skip to content
Snippets Groups Projects
Commit c0457341 authored by Cédric Moreau's avatar Cédric Moreau
Browse files

Implemented PoW difficulty rule

parent 2a30e11b
No related branches found
No related tags found
No related merge requests found
...@@ -236,6 +236,16 @@ KeyBlockSchema.statics.nextNumber = function (done) { ...@@ -236,6 +236,16 @@ KeyBlockSchema.statics.nextNumber = function (done) {
}); });
}; };
KeyBlockSchema.statics.lastOfIssuer = function (issuer, done) {
this
.find({ issuer: issuer })
.sort({ 'number': -1 })
.limit(1)
.exec(function (err, blocks) {
done(err, (blocks && blocks.length == 1) ? blocks[0] : null);
});
};
KeyBlockSchema.statics.current = function (done) { KeyBlockSchema.statics.current = function (done) {
this.find({}).sort({ number: -1 }).limit(1).exec(function (err, blocks) { this.find({}).sort({ number: -1 }).limit(1).exec(function (err, blocks) {
......
...@@ -27,7 +27,8 @@ function KeyService (conn, conf, PublicKeyService) { ...@@ -27,7 +27,8 @@ function KeyService (conn, conf, PublicKeyService) {
var Link = conn.model('Link'); var Link = conn.model('Link');
var Key = conn.model('Key'); var Key = conn.model('Key');
var MINIMUM_ZERO_START = 0; var MINIMUM_ZERO_START = 1;
var PROOF_OF_WORK_PERIOD = 1 // Value 1 allows for continuous single-member writing with minimum difficulty
var LINK_QUANTITY_MIN = 1; var LINK_QUANTITY_MIN = 1;
var MAX_STEPS = 1; var MAX_STEPS = 1;
var MAX_LINK_VALIDITY = 3600*24*30; // 30 days var MAX_LINK_VALIDITY = 3600*24*30; // 30 days
...@@ -478,8 +479,35 @@ function KeyService (conn, conf, PublicKeyService) { ...@@ -478,8 +479,35 @@ function KeyService (conn, conf, PublicKeyService) {
var powRegexp = new RegExp('^0{' + MINIMUM_ZERO_START + '}'); var powRegexp = new RegExp('^0{' + MINIMUM_ZERO_START + '}');
if (!block.hash.match(powRegexp)) if (!block.hash.match(powRegexp))
done('Not a proof-of-work'); done('Not a proof-of-work');
else else {
done(); // Compute exactly how much zeros are required for this block's issuer
var lastBlockPenality = 0;
var nbWaitedPeriods = 0;
async.waterfall([
function (next){
KeyBlock.lastOfIssuer(block.issuer, next);
},
function (last, next){
if (last) {
var leadingZeros = last.hash.match(/^0+/)[0];
lastBlockPenality = leadingZeros.length - MINIMUM_ZERO_START + 1;
nbWaitedPeriods = Math.floor((block.number - last.number) / PROOF_OF_WORK_PERIOD);
next();
} else {
next();
}
},
function (next){
var nbZeros = Math.max(MINIMUM_ZERO_START, MINIMUM_ZERO_START + lastBlockPenality - nbWaitedPeriods);
var powRegexp = new RegExp('^0{' + nbZeros + ',}');
if (!block.hash.match(powRegexp))
next('Wrong proof-of-work level: given ' + block.hash.match(/^0+/)[0].length + ' zeros, required was ' + nbZeros + ' zeros');
else {
next();
}
},
], done);
}
} }
function checkKicked (block, newLinks, done) { function checkKicked (block, newLinks, done) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment