Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Merge pull request #4 from LiskHQ/0.9.4-fork-detection-fix
Browse files Browse the repository at this point in the history
0.9.4 fork detection fix
  • Loading branch information
karmacoma authored Aug 20, 2017
2 parents f53d447 + af9f778 commit 6cea99a
Show file tree
Hide file tree
Showing 2 changed files with 391 additions and 187 deletions.
220 changes: 152 additions & 68 deletions modules/blocks/process.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

var _ = require('lodash');
var async = require('async');
var constants = require('../../helpers/constants.js');
var schema = require('../../schema/blocks.js');
Expand Down Expand Up @@ -264,7 +265,7 @@ Process.prototype.loadBlocksFromPeer = function (peer, cb) {

// Process single block
function processBlock (block, seriesCb) {
// Start block processing - broadcast: false, saveBlock: true, checked: false
// Start block processing - broadcast: false, saveBlock: true
modules.blocks.verify.processBlock(block, false, function (err) {
if (!err) {
// Update last valid block
Expand All @@ -276,7 +277,7 @@ Process.prototype.loadBlocksFromPeer = function (peer, cb) {
library.logger.debug('Block processing failed', {id: id, err: err.toString(), module: 'blocks', block: block});
}
return seriesCb(err);
}, true, false);
}, true);
}

async.waterfall([
Expand Down Expand Up @@ -343,8 +344,8 @@ Process.prototype.generateBlock = function (keypair, timestamp, cb) {
return setImmediate(cb, e);
}

// Start block processing - broadcast: true, saveBlock: true, checked: false
modules.blocks.verify.processBlock(block, true, cb, true, false);
// Start block processing - broadcast: true, saveBlock: true
modules.blocks.verify.processBlock(block, true, cb, true);
});
};

Expand All @@ -358,7 +359,7 @@ Process.prototype.generateBlock = function (keypair, timestamp, cb) {
* @public
* @method onReceiveBlock
* @listens module:transport~event:receiveBlock
* @param {block} block New block
* @param {block} block New block
*/
Process.prototype.onReceiveBlock = function (block) {
var lastBlock;
Expand All @@ -372,75 +373,40 @@ Process.prototype.onReceiveBlock = function (block) {
return;
}

// Get the last block
lastBlock = modules.blocks.lastBlock.get();

if (block.id === lastBlock.id) {
library.logger.debug('Block already processed', block.id);
return setImmediate(cb);
}

modules.blocks.verify.checkBlock(block, function (err) {
if (err) {
library.logger.error('Failed to check block', err);
return setImmediate(cb);
}

// Initial check if new block looks fine
if (block.previousBlock === lastBlock.id && lastBlock.height + 1 === block.height) {
// Process received block
return __private.receiveBlock(block, cb);
} else if (block.previousBlock !== lastBlock.id && lastBlock.height + 1 === block.height) {
// Fork: Consecutive height but different previous block id
modules.delegates.fork(block, 1);

// We should keep the oldest one or if both have same age - keep one with lower id
if (block.timestamp > lastBlock.timestamp || (block.timestamp === lastBlock.timestamp && block.id > lastBlock.id)) {
library.logger.info('Last block stands');
return setImmediate(cb);
} else {
// In other cases - we have wrong parent and should rewind
library.logger.info('Last block and parent loses');
// Delete last 2 blocks
async.series([
modules.blocks.chain.deleteLastBlock,
modules.blocks.chain.deleteLastBlock
], cb);
}
} else if (block.previousBlock === lastBlock.previousBlock && block.height === lastBlock.height && block.id !== lastBlock.id) {
// Fork: Same height and previous block id, but different block id
modules.delegates.fork(block, 5);

// Check if delegate forged on more than one node
if (block.generatorPublicKey === lastBlock.generatorPublicKey) {
library.logger.warn('Delegate forging on multiple nodes', block.generatorPublicKey);
}

// Two competiting blocks on same height, we should keep the oldest one or if both have same age - keep one with lower id
if (block.timestamp > lastBlock.timestamp || (block.timestamp === lastBlock.timestamp && block.id > lastBlock.id)) {
library.logger.info('Last block stands');
return setImmediate(cb);
} else {
library.logger.info('Last block loses');
async.series([
function (seriesCb) {
// Delete last block
modules.blocks.chain.deleteLastBlock(seriesCb);
},
function (seriesCb) {
// Process received block
return __private.receiveBlock(block, seriesCb);
}
], cb);
}
// Detect sane block
if (block.previousBlock === lastBlock.id && lastBlock.height + 1 === block.height) {
// Process received block
return __private.receiveBlock(block, cb);
} else if (block.previousBlock !== lastBlock.id && lastBlock.height + 1 === block.height) {
// Process received fork cause 1
return __private.receiveForkOne(block, lastBlock, cb);
} else if (block.previousBlock === lastBlock.previousBlock && block.height === lastBlock.height && block.id !== lastBlock.id) {
// Process received fork cause 5
return __private.receiveForkFive(block, lastBlock, cb);
} else {
if (block.id === lastBlock.id) {
library.logger.debug('Block already processed', block.id);
} else {
return setImmediate(cb);
library.logger.warn([
'Discarded block that does not match with current chain:', block.id,
'height:', block.height,
'round:', modules.rounds.calc(block.height),
'slot:', slots.getSlotNumber(block.timestamp),
'generator:', block.generatorPublicKey
].join(' '));
}
});

// Discard received block
return setImmediate(cb);
}
});
};

/**
* Receive block - logs info about received block, updates last receipt, fires processing
* Receive block - logs info about received block, updates last receipt, processes block
*
* @private
* @async
Expand All @@ -459,8 +425,126 @@ __private.receiveBlock = function (block, cb) {

// Update last receipt
modules.blocks.lastReceipt.update();
// Start block processing - broadcast: true, saveBlock: true, checked: true
modules.blocks.verify.processBlock(block, true, cb, true, true);
// Start block processing - broadcast: true, saveBlock: true
modules.blocks.verify.processBlock(block, true, cb, true);
};

/**
* Receive block detected as fork cause 1: Consecutive height but different previous block id
*
* @private
* @async
* @method receiveBlock
* @param {Object} block Received block
* @param {Function} cb Callback function
*/
__private.receiveForkOne = function (block, lastBlock, cb) {
var tmp_block = _.clone(block);

// Fork: Consecutive height but different previous block id
modules.delegates.fork(block, 1);

// Keep the oldest block, or if both have same age, keep block with lower id
if (block.timestamp > lastBlock.timestamp || (block.timestamp === lastBlock.timestamp && block.id > lastBlock.id)) {
library.logger.info('Last block stands');
return setImmediate(cb); // Discard received block
} else {
library.logger.info('Last block and parent loses');
async.series([
function (seriesCb) {
try {
tmp_block = library.logic.block.objectNormalize(tmp_block);
} catch (err) {
return setImmediate(seriesCb, err);
}
return setImmediate(seriesCb);
},
// Check received block before any deletion
function (seriesCb) {
var check = modules.blocks.verify.verifyReceipt(tmp_block);

if (!check.verified) {
library.logger.error(['Block', tmp_block.id, 'verification failed'].join(' '), check.errors.join(', '));
// Return first error from checks
return setImmediate(seriesCb, check.errors[0]);
} else {
return setImmediate(seriesCb);
}
},
// Delete last 2 blocks
modules.blocks.chain.deleteLastBlock,
modules.blocks.chain.deleteLastBlock
], function (err) {
if (err) {
library.logger.error('Fork recovery failed', err);
}
return setImmediate(cb, err);
});
}
};

/**
* Receive block detected as fork cause 5: Same height and previous block id, but different block id
*
* @private
* @async
* @method receiveBlock
* @param {Object} block Received block
* @param {Function} cb Callback function
*/
__private.receiveForkFive = function (block, lastBlock, cb) {
var tmp_block = _.clone(block);

// Fork: Same height and previous block id, but different block id
modules.delegates.fork(block, 5);

// Check if delegate forged on more than one node
if (block.generatorPublicKey === lastBlock.generatorPublicKey) {
library.logger.warn('Delegate forging on multiple nodes', block.generatorPublicKey);
}

// Keep the oldest block, or if both have same age, keep block with lower id
if (block.timestamp > lastBlock.timestamp || (block.timestamp === lastBlock.timestamp && block.id > lastBlock.id)) {
library.logger.info('Last block stands');
return setImmediate(cb); // Discard received block
} else {
library.logger.info('Last block loses');
async.series([
function (seriesCb) {
try {
tmp_block = library.logic.block.objectNormalize(tmp_block);
} catch (err) {
return setImmediate(seriesCb, err);
}
return setImmediate(seriesCb);
},
// Check received block before any deletion
function (seriesCb) {
var check = modules.blocks.verify.verifyReceipt(tmp_block);

if (!check.verified) {
library.logger.error(['Block', tmp_block.id, 'verification failed'].join(' '), check.errors.join(', '));
// Return first error from checks
return setImmediate(seriesCb, check.errors[0]);
} else {
return setImmediate(seriesCb);
}
},
// Delete last block
function (seriesCb) {
modules.blocks.chain.deleteLastBlock(seriesCb);
},
// Process received block
function (seriesCb) {
return __private.receiveBlock(block, seriesCb);
}
], function (err) {
if (err) {
library.logger.error('Fork recovery failed', err);
}
return setImmediate(cb, err);
});
}
};

/**
Expand Down
Loading

0 comments on commit 6cea99a

Please sign in to comment.