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

Fixing #374 Now recording the fact a membership is expired

parent fd405dd3
Branches
Tags
No related merge requests found
...@@ -138,6 +138,8 @@ function BlockchainContext() { ...@@ -138,6 +138,8 @@ function BlockchainContext() {
yield that.computeExpiredIdentities(block); yield that.computeExpiredIdentities(block);
// Compute obsolete certifications // Compute obsolete certifications
yield that.computeExpiredCertifications(block); yield that.computeExpiredCertifications(block);
// Compute obsolete memberships
yield that.computeExpiredMemberships (block);
// Update consumed sources & create new ones // Update consumed sources & create new ones
yield that.updateSources(block); yield that.updateSources(block);
// Delete eventually present transactions // Delete eventually present transactions
...@@ -428,6 +430,13 @@ function BlockchainContext() { ...@@ -428,6 +430,13 @@ function BlockchainContext() {
} }
}); });
this.computeExpiredMemberships = (block) => co(function *() {
let lastForExpiry = yield dal.getMembershipExpiringBlock(block, conf.certWindow);
if (lastForExpiry) {
yield dal.flagExpiredMemberships(lastForExpiry.number, block.number);
}
});
this.updateSources = (block) => co(function*() { this.updateSources = (block) => co(function*() {
if (block.dividend) { if (block.dividend) {
const idties = yield dal.getMembers(); const idties = yield dal.getMembers();
......
...@@ -720,6 +720,72 @@ function FileDAL(params) { ...@@ -720,6 +720,72 @@ function FileDAL(params) {
} }
}); });
// TODO: refactor all this duplicated code
this.getMembershipExpiringBlock = (current, msWindow) => co(function *() {
let currentExcluding;
if (current.number > 0) {
try {
currentExcluding = yield that.indicatorsDAL.getCurrentMembershipExpiringBlock();
} catch (e) {
currentExcluding = null;
}
}
if (!currentExcluding) {
const root = yield that.getRootBlock();
const delaySinceStart = current.medianTime - root.medianTime;
if (delaySinceStart > msWindow) {
return that.indicatorsDAL.writeCurrentExpiringForMembership(root).then(() => root);
}
} else {
// Check current position
const currentNextBlock = yield that.getBlock(currentExcluding.number + 1);
if (isExcluding(current, currentExcluding, currentNextBlock, msWindow)) {
return currentExcluding;
} else {
// Have to look for new one
const start = currentExcluding.number;
let newExcluding;
let top = current.number;
let bottom = start;
// Binary tree search
do {
let middle = top - bottom;
if (middle % 2 != 0) {
middle = middle + 1;
}
middle /= 2;
middle += bottom;
if (middle == top) {
middle--;
bottom--; // Helps not being stuck looking at 'top'
}
const middleBlock = yield that.getBlock(middle);
const middleNextB = yield that.getBlock(middle + 1);
const delaySinceMiddle = current.medianTime - middleBlock.medianTime;
const delaySinceNextB = current.medianTime - middleNextB.medianTime;
const isValidPeriod = delaySinceMiddle <= msWindow;
const isValidPeriodB = delaySinceNextB <= msWindow;
const isExcludin = !isValidPeriod && isValidPeriodB;
//console.log('CRT: Search between %s and %s: %s => %s,%s', bottom, top, middle, isValidPeriod ? 'DOWN' : 'UP', isValidPeriodB ? 'DOWN' : 'UP');
if (isExcludin) {
// Found
yield that.indicatorsDAL.writeCurrentExpiringForMembership(middleBlock);
newExcluding = middleBlock;
}
else if (isValidPeriod) {
// Look down in the blockchain
top = middle;
}
else {
// Look up in the blockchain
bottom = middle;
}
} while (!newExcluding);
return newExcluding;
}
}
});
const isExcluding = (current, excluding, nextBlock, certValidtyTime) => { const isExcluding = (current, excluding, nextBlock, certValidtyTime) => {
const delaySinceMiddle = current.medianTime - excluding.medianTime; const delaySinceMiddle = current.medianTime - excluding.medianTime;
const delaySinceNextB = current.medianTime - nextBlock.medianTime; const delaySinceNextB = current.medianTime - nextBlock.medianTime;
...@@ -730,6 +796,7 @@ function FileDAL(params) { ...@@ -730,6 +796,7 @@ function FileDAL(params) {
this.flagExpiredIdentities = (maxNumber, onNumber) => this.idtyDAL.flagExpiredIdentities(maxNumber, onNumber); this.flagExpiredIdentities = (maxNumber, onNumber) => this.idtyDAL.flagExpiredIdentities(maxNumber, onNumber);
this.flagExpiredCertifications = (maxNumber, onNumber) => this.certDAL.flagExpiredCertifications(maxNumber, onNumber); this.flagExpiredCertifications = (maxNumber, onNumber) => this.certDAL.flagExpiredCertifications(maxNumber, onNumber);
this.flagExpiredMemberships = (maxNumber, onNumber) => this.msDAL.flagExpiredMemberships(maxNumber, onNumber);
this.kickWithOutdatedMemberships = (maxNumber) => this.idtyDAL.kickMembersForMembershipBelow(maxNumber); this.kickWithOutdatedMemberships = (maxNumber) => this.idtyDAL.kickMembersForMembershipBelow(maxNumber);
this.revokeWithOutdatedMemberships = (maxNumber) => this.idtyDAL.revokeMembersForMembershipBelow(maxNumber); this.revokeWithOutdatedMemberships = (maxNumber) => this.idtyDAL.revokeMembersForMembershipBelow(maxNumber);
...@@ -852,6 +919,8 @@ function FileDAL(params) { ...@@ -852,6 +919,8 @@ function FileDAL(params) {
this.unflagExpiredCertificationsOf = (number) => that.certDAL.unflagExpiredCertificationsOf(number); this.unflagExpiredCertificationsOf = (number) => that.certDAL.unflagExpiredCertificationsOf(number);
this.unflagExpiredMembershipsOf = (number) => that.msDAL.unflagExpiredMembershipsOf(number);
this.saveSource = (src) => that.sourcesDAL.addSource(src.type, src.number, src.identifier, src.noffset, this.saveSource = (src) => that.sourcesDAL.addSource(src.type, src.number, src.identifier, src.noffset,
src.amount, src.base, src.block_hash, src.time, src.conditions); src.amount, src.base, src.block_hash, src.time, src.conditions);
......
...@@ -33,6 +33,8 @@ function IndicatorsDAL(rootPath, qioFS, parentCore, localDAL, AbstractStorage) { ...@@ -33,6 +33,8 @@ function IndicatorsDAL(rootPath, qioFS, parentCore, localDAL, AbstractStorage) {
this.writeCurrentExpiringForIdty = (excluding) => that.coreFS.writeJSON('indicators/expiringIDTY.json', excluding); this.writeCurrentExpiringForIdty = (excluding) => that.coreFS.writeJSON('indicators/expiringIDTY.json', excluding);
this.writeCurrentExpiringForMembership = (excluding) => that.coreFS.writeJSON('indicators/expiringMS.json', excluding);
this.getCurrentMembershipExcludingBlock = () => that.coreFS.readJSON('indicators/excludingMS.json'); this.getCurrentMembershipExcludingBlock = () => that.coreFS.readJSON('indicators/excludingMS.json');
this.getCurrentMembershipRevocatingBlock = () => that.coreFS.readJSON('indicators/revocatingMS.json'); this.getCurrentMembershipRevocatingBlock = () => that.coreFS.readJSON('indicators/revocatingMS.json');
...@@ -42,4 +44,6 @@ function IndicatorsDAL(rootPath, qioFS, parentCore, localDAL, AbstractStorage) { ...@@ -42,4 +44,6 @@ function IndicatorsDAL(rootPath, qioFS, parentCore, localDAL, AbstractStorage) {
this.getCurrentCertificationExcludingBlock = () => that.coreFS.readJSON('indicators/excludingCRT.json'); this.getCurrentCertificationExcludingBlock = () => that.coreFS.readJSON('indicators/excludingCRT.json');
this.getCurrentIdentityExpiringBlock = () => that.coreFS.readJSON('indicators/expiringIDTY.json'); this.getCurrentIdentityExpiringBlock = () => that.coreFS.readJSON('indicators/expiringIDTY.json');
this.getCurrentMembershipExpiringBlock = () => that.coreFS.readJSON('indicators/expiringMS.json');
} }
...@@ -31,7 +31,8 @@ function MembershipDAL(db) { ...@@ -31,7 +31,8 @@ function MembershipDAL(db) {
'idtyHash', 'idtyHash',
'written', 'written',
'written_number', 'written_number',
'signature' 'signature',
'expired'
]; ];
this.arrays = []; this.arrays = [];
this.booleans = ['written']; this.booleans = ['written'];
...@@ -155,4 +156,17 @@ function MembershipDAL(db) { ...@@ -155,4 +156,17 @@ function MembershipDAL(db) {
return that.exec(queries.join('\n')); return that.exec(queries.join('\n'));
} }
}); });
this.flagExpiredMemberships = (maxNumber, onNumber) => co(function *() {
yield that.exec('UPDATE ' + that.table + ' ' +
'SET expired = ' + onNumber + ' ' +
'WHERE expired IS NULL ' +
'AND blockNumber <= ' + maxNumber);
});
this.unflagExpiredMembershipsOf = (onNumber) => co(function *() {
yield that.exec('UPDATE ' + that.table + ' ' +
'SET expired = NULL ' +
'WHERE expired = ' + onNumber);
});
} }
...@@ -102,7 +102,8 @@ function MetaDAL(db) { ...@@ -102,7 +102,8 @@ function MetaDAL(db) {
}), }),
6: 'BEGIN; ALTER TABLE idty ADD COLUMN expired INTEGER NULL; COMMIT;', 6: 'BEGIN; ALTER TABLE idty ADD COLUMN expired INTEGER NULL; COMMIT;',
7: 'BEGIN; ALTER TABLE cert ADD COLUMN expired INTEGER NULL; COMMIT;' 7: 'BEGIN; ALTER TABLE cert ADD COLUMN expired INTEGER NULL; COMMIT;',
8: 'BEGIN; ALTER TABLE membership ADD COLUMN expired INTEGER NULL; COMMIT;'
}; };
this.init = () => co(function *() { this.init = () => co(function *() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment