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

[fix] #827 Block control on startup

parent 086ec175
No related branches found
No related tags found
No related merge requests found
...@@ -243,10 +243,10 @@ function BlockchainContext() { ...@@ -243,10 +243,10 @@ function BlockchainContext() {
}); });
this.revertCurrentBlock = () => co(function *() { this.revertCurrentBlock = () => co(function *() {
const current = yield that.current(); const head_1 = yield dal.bindexDAL.head(1);
logger.debug('Reverting block #%s...', current.number); logger.debug('Reverting block #%s...', head_1.number);
const res = yield that.revertBlock(current); const res = yield that.revertBlock(head_1.number, head_1.hash);
logger.debug('Reverted block #%s', current.number); logger.debug('Reverted block #%s', head_1.number);
// Invalidates the head, since it has changed. // Invalidates the head, since it has changed.
yield refreshHead(); yield refreshHead();
return res; return res;
...@@ -264,9 +264,9 @@ function BlockchainContext() { ...@@ -264,9 +264,9 @@ function BlockchainContext() {
logger.debug('Applied block #%s', block.number); logger.debug('Applied block #%s', block.number);
}); });
this.revertBlock = (block) => co(function *() { this.revertBlock = (number, hash) => co(function *() {
const blockstamp = [block.number, block.hash].join('-'); const blockstamp = [number, hash].join('-');
// Revert links // Revert links
const writtenOn = yield dal.cindexDAL.getWrittenOn(blockstamp); const writtenOn = yield dal.cindexDAL.getWrittenOn(blockstamp);
...@@ -283,23 +283,27 @@ function BlockchainContext() { ...@@ -283,23 +283,27 @@ function BlockchainContext() {
} }
// Revert nodes // Revert nodes
yield undoMembersUpdate(block); yield undoMembersUpdate(blockstamp);
yield dal.bindexDAL.removeBlock(block.number); yield dal.bindexDAL.removeBlock(number);
yield dal.mindexDAL.removeBlock(blockstamp); yield dal.mindexDAL.removeBlock(blockstamp);
yield dal.iindexDAL.removeBlock(blockstamp); yield dal.iindexDAL.removeBlock(blockstamp);
yield dal.cindexDAL.removeBlock(blockstamp); yield dal.cindexDAL.removeBlock(blockstamp);
yield dal.sindexDAL.removeBlock(blockstamp); yield dal.sindexDAL.removeBlock(blockstamp);
// Then: normal updates // Then: normal updates
const previousBlock = yield dal.getBlockByNumberAndHashOrNull(block.number - 1, block.previousHash || ''); const previousBlock = yield dal.getBlock(number - 1);
// Set the block as SIDE block (equivalent to removal from main branch) // Set the block as SIDE block (equivalent to removal from main branch)
yield dal.blockDAL.setSideBlock(block, previousBlock); yield dal.blockDAL.setSideBlock(number, previousBlock);
// Remove any source created for this block (both Dividend and Transaction). // Remove any source created for this block (both Dividend and Transaction).
yield dal.removeAllSourcesOfBlock(blockstamp); yield dal.removeAllSourcesOfBlock(blockstamp);
const block = yield dal.getBlockByBlockstampOrNull(blockstamp);
if (block) {
// For some reason the block might not exist (issue #827)
yield undoDeleteTransactions(block); yield undoDeleteTransactions(block);
}
}); });
const checkIssuer = (block) => co(function*() { const checkIssuer = (block) => co(function*() {
...@@ -442,24 +446,35 @@ function BlockchainContext() { ...@@ -442,24 +446,35 @@ function BlockchainContext() {
}); });
}); });
function undoMembersUpdate (block) { function undoMembersUpdate (blockstamp) {
return co(function *() { return co(function *() {
const joiners = yield dal.iindexDAL.getWrittenOn(blockstamp);
for (const entry of joiners) {
// Undo 'join' which can be either newcomers or comebackers // Undo 'join' which can be either newcomers or comebackers
for (const msRaw of block.joiners) { // => equivalent to i_index.member = true AND i_index.op = 'UPDATE'
let ms = Membership.statics.fromInline(msRaw, 'IN', conf.currency); if (entry.member === true && entry.op === constants.IDX_UPDATE) {
const idty = yield dal.getWrittenIdtyByPubkey(ms.issuer); const idty = yield dal.getWrittenIdtyByPubkey(entry.pub);
dal.wotb.setEnabled(false, idty.wotb_id); dal.wotb.setEnabled(false, idty.wotb_id);
} }
}
const newcomers = yield dal.iindexDAL.getWrittenOn(blockstamp);
for (const entry of newcomers) {
// Undo newcomers // Undo newcomers
for (let i = 0; i < block.identities.length; i++) { // => equivalent to i_index.op = 'CREATE'
if (entry.op === constants.IDX_CREATE) {
// Does not matter which one it really was, we pop the last X identities // Does not matter which one it really was, we pop the last X identities
dal.wotb.removeNode(); dal.wotb.removeNode();
} }
}
const excluded = yield dal.iindexDAL.getWrittenOn(blockstamp);
for (const entry of excluded) {
// Undo excluded (make them become members again in wotb) // Undo excluded (make them become members again in wotb)
for (const pubkey of block.excluded) { // => equivalent to m_index.member = false
const idty = yield dal.getWrittenIdtyByPubkey(pubkey); if (entry.member === false && entry.op === constants.IDX_UPDATE) {
const idty = yield dal.getWrittenIdtyByPubkey(entry.pub);
dal.wotb.setEnabled(true, idty.wotb_id); dal.wotb.setEnabled(true, idty.wotb_id);
} }
}
}); });
} }
......
...@@ -14,6 +14,8 @@ function AbstractIndex() { ...@@ -14,6 +14,8 @@ function AbstractIndex() {
const that = this; const that = this;
this.getWrittenOn = (blockstamp) => that.query('SELECT * FROM ' + that.table + ' WHERE written_on = ?', [blockstamp]);
this.trimRecords = (belowNumber) => co(function*() { this.trimRecords = (belowNumber) => co(function*() {
const belowRecords = yield that.query('SELECT COUNT(*) as nbRecords, pub FROM ' + that.table + ' ' + const belowRecords = yield that.query('SELECT COUNT(*) as nbRecords, pub FROM ' + that.table + ' ' +
'WHERE CAST(written_on as int) < ? ' + 'WHERE CAST(written_on as int) < ? ' +
......
...@@ -147,8 +147,8 @@ function BlockDAL(driver) { ...@@ -147,8 +147,8 @@ function BlockDAL(driver) {
}); });
} }
this.setSideBlock = (block, previousBlock) => co(function *() { this.setSideBlock = (number, previousBlock) => co(function *() {
yield that.query('UPDATE block SET fork = ? WHERE number = ? and hash = ?', [true, block.number, block.hash]); yield that.query('UPDATE block SET fork = ? WHERE number = ?', [true, number]);
current = previousBlock; current = previousBlock;
}); });
......
...@@ -187,7 +187,21 @@ function Server (home, memoryOnly, overrideConf) { ...@@ -187,7 +187,21 @@ function Server (home, memoryOnly, overrideConf) {
this.submitP = (obj, isInnerWrite) => Q.nbind(this.submit, this)(obj, isInnerWrite); this.submitP = (obj, isInnerWrite) => Q.nbind(this.submit, this)(obj, isInnerWrite);
this.initDAL = () => this.dal.init(); this.initDAL = () => co(function*() {
yield that.dal.init();
// Maintenance
let head_1 = yield that.dal.bindexDAL.head(1);
if (head_1) {
// Case 1: b_index < block
yield that.dal.blockDAL.exec('DELETE FROM block WHERE NOT fork AND number > ' + head_1.number);
// Case 2: b_index > block
const current = yield that.dal.blockDAL.getCurrent();
const nbBlocksToRevert = (head_1.number - current.number);
for (let i = 0; i < nbBlocksToRevert; i++) {
yield that.revert();
}
}
});
this.recomputeSelfPeer = () => that.PeeringService.generateSelfPeer(that.conf, 0); this.recomputeSelfPeer = () => that.PeeringService.generateSelfPeer(that.conf, 0);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment