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

Fix: certifications could be replayed too quickly

parent 1cc22eec
No related branches found
No related tags found
No related merge requests found
......@@ -182,7 +182,7 @@ function BlockchainContext(conf, dal) {
},
function (next){
// Save links
updateLinks(block, next);
updateLinks(block, next, dal.getBlockOrNull.bind(dal));
},
function (next){
// Compute obsolete links
......@@ -452,18 +452,26 @@ function BlockchainContext(conf, dal) {
}, done);
}
function updateLinks (block, done) {
function updateLinks (block, done, getBlockOrNull) {
async.forEach(block.certifications, function(inlineCert, callback){
var cert = Certification.statics.fromInline(inlineCert);
dal.saveLink(
return co(function *() {
let tagBlock = block;
if (block.number > 0) {
tagBlock = yield getBlockOrNull(cert.block_number);
}
return dal.saveLink(
new Link({
source: cert.from,
target: cert.to,
timestamp: block.medianTime,
timestamp: tagBlock.medianTime,
block_number: block.number,
block_hash: block.hash,
obsolete: false
})).then(_.partial(callback, null)).catch(callback);
}));
})
.then(_.partial(callback, null))
.catch(callback);
}, done);
}
......
......@@ -52,10 +52,10 @@ module.exports = function(dal) {
this.getPreviousLinkFor = function (from, to, done) {
async.waterfall([
function (next){
dal.getObsoletesFromTo(from, to).then(_.partial(next, null)).catch(next);
dal.getPreviousLinks(from, to).then(_.partial(next, null)).catch(next);
},
function (links, next){
next(null, links.length > 0 ? links[0] : null);
function (previous, next){
next(null, previous);
}
], done);
};
......
......@@ -357,15 +357,11 @@ function FileDAL(profile, home, localDir, myFS, parentFileDAL, dalName, loki) {
return that.linksDAL.getValidLinksTo(to);
};
this.getObsoletesFromTo = function(from, to) {
return that.linksDAL.getObsoleteLinksFromTo()
.then(function(links){
return _.chain(links).
where({ target: to, source: from }).
sortBy(function(lnk){ return -lnk.timestamp; }).
value();
this.getPreviousLinks = (from, to) => co(function *() {
let links = yield that.linksDAL.getLinksWithPath(from, to);
links = _.sortBy(links, 'timestamp');
return links[links.length - 1];
});
};
this.getValidFromTo = function(from, to) {
return that.getValidLinksFrom(from)
......
......@@ -41,14 +41,13 @@ function LinksDAL(loki) {
obsolete: false
});
this.getObsoleteLinksFromTo = (from, to) => this.lokiFind({
this.getLinksWithPath = (from, to) =>
this.lokiFind({
$and: [{
source: from
},{
to: to
target: to
}]
}, {
obsolete: true
});
this.obsoletesLinks = (minTimestamp) => co(function *() {
......
......@@ -878,7 +878,7 @@ function GlobalValidator (conf, dao) {
},
function (previous, next){
var duration = previous && (block.medianTime - parseInt(previous.timestamp));
if (previous && (duration < conf.sigDelay)) {
if (previous && (duration < conf.sigDelay + conf.sigValidity)) {
next('Too early for this certification');
} else {
next();
......
......@@ -1320,7 +1320,14 @@ function BlockchainService (conf, mainDAL, pair) {
},
function (next){
// Save links
mainContext.updateLinks(block, next);
mainContext.updateLinks(block, next, (number) => {
let firstLocalNumber = blocks[0].number;
if (number >= firstLocalNumber) {
let offset = number - firstLocalNumber;
return Q(blocks[offset]);
}
return mainDAL.getBlockOrNull(number);
});
},
function (next){
// Update consumed sources & create new ones
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment