Skip to content

Commit

Permalink
fix rollback issue
Browse files Browse the repository at this point in the history
  • Loading branch information
sqfasd committed Sep 12, 2017
1 parent b621910 commit 566a010
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 22 deletions.
60 changes: 43 additions & 17 deletions src/core/round.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,21 +84,30 @@ Round.prototype.flush = function (round, cb) {
}

Round.prototype.directionSwap = function (direction, lastBlock, cb) {
if (direction == 'backward') {
private.feesByRound = {};
private.rewardsByRound = {};
private.delegatesByRound = {};
self.flush(self.calc(lastBlock.height), cb);
} else {
private.unFeesByRound = {};
private.unRewardsByRound = {};
private.unDelegatesByRound = {};
self.flush(self.calc(lastBlock.height), cb);
}
cb()
// if (direction == 'backward') {
// private.feesByRound = {};
// private.rewardsByRound = {};
// private.delegatesByRound = {};
// self.flush(self.calc(lastBlock.height), cb);
// } else {
// private.unFeesByRound = {};
// private.unRewardsByRound = {};
// private.unDelegatesByRound = {};
// self.flush(self.calc(lastBlock.height), cb);
// }
}

Round.prototype.backwardTick = function (block, previousBlock, cb) {
function done(err) {
if (err) {
library.logger.error("Round backward tick failed: " + err);
} else {
library.logger.debug("Round backward tick completed", {
block: block,
previousBlock: previousBlock
});
}
cb && cb(err);
}

Expand All @@ -116,14 +125,23 @@ Round.prototype.backwardTick = function (block, previousBlock, cb) {

var prevRound = self.calc(previousBlock.height);

private.unFeesByRound[round] = (private.unFeesByRound[round] || 0);
private.unFeesByRound[round] += block.totalFee;
// private.unFeesByRound[round] = (private.unFeesByRound[round] || 0);
// private.unFeesByRound[round] += block.totalFee;

private.unRewardsByRound[round] = (private.unRewardsByRound[round] || []);
private.unRewardsByRound[round].push(block.reward);
// private.unRewardsByRound[round] = (private.unRewardsByRound[round] || []);
// private.unRewardsByRound[round].push(block.reward);

private.unDelegatesByRound[round] = private.unDelegatesByRound[round] || [];
private.unDelegatesByRound[round].push(block.generatorPublicKey);
// private.unDelegatesByRound[round] = private.unDelegatesByRound[round] || [];
// private.unDelegatesByRound[round].push(block.generatorPublicKey);

private.feesByRound[round] = (private.feesByRound[round] || 0);
private.feesByRound[round] -= block.totalFee;

private.rewardsByRound[round] = (private.rewardsByRound[round] || []);
private.rewardsByRound[round].pop()

private.delegatesByRound[round] = private.delegatesByRound[round] || [];
private.delegatesByRound[round].pop()

if (prevRound === round && previousBlock.height !== 1) {
return done();
Expand All @@ -132,6 +150,14 @@ Round.prototype.backwardTick = function (block, previousBlock, cb) {
if (private.unDelegatesByRound[round].length !== slots.delegates && previousBlock.height !== 1) {
return done();
}
library.logger.warn('Unexpected roll back cross round', {
round: round,
prevRound: prevRound,
block: block,
previousBlock: previousBlock
});
process.exit(1);
// FIXME process the cross round rollback
var outsiders = [];
async.series([
function (cb) {
Expand Down
15 changes: 10 additions & 5 deletions src/utils/dblite-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,16 @@ module.exports.connect = function (connectString, cb) {
"CREATE UNIQUE INDEX IF NOT EXISTS peers_unique ON peers(ip, port)",
"CREATE UNIQUE INDEX IF NOT EXISTS peers_dapp_unique ON peers_dapp(peerId, dappId)",
"CREATE INDEX IF NOT EXISTS blocks_generator_public_key ON blocks(generatorPublicKey)",
"CREATE INDEX IF NOT EXISTS blocks_reward ON blocks(reward)",
"CREATE INDEX IF NOT EXISTS blocks_totalFee ON blocks(totalFee)",
"CREATE INDEX IF NOT EXISTS blocks_totalAmount ON blocks(totalAmount)",
"CREATE INDEX IF NOT EXISTS blocks_numberOfTransactions ON blocks(numberOfTransactions)",
"CREATE INDEX IF NOT EXISTS blocks_timestamp ON blocks(timestamp)",
// "CREATE INDEX IF NOT EXISTS blocks_reward ON blocks(reward)",
// "CREATE INDEX IF NOT EXISTS blocks_totalFee ON blocks(totalFee)",
// "CREATE INDEX IF NOT EXISTS blocks_totalAmount ON blocks(totalAmount)",
// "CREATE INDEX IF NOT EXISTS blocks_numberOfTransactions ON blocks(numberOfTransactions)",
// "CREATE INDEX IF NOT EXISTS blocks_timestamp ON blocks(timestamp)",
"drop index if exists blocks_reward",
"drop index if exists blocks_totalFee",
"drop index if exists blocks_totalAmount",
"drop index if exists blocks_numberOfTransactions",
"drop index if exists blocks_timestamp",
"CREATE INDEX IF NOT EXISTS trs_block_id ON trs(blockId)",
"CREATE INDEX IF NOT EXISTS trs_sender_id ON trs(senderId)",
"CREATE INDEX IF NOT EXISTS trs_recipient_id ON trs(recipientId)",
Expand Down

0 comments on commit 566a010

Please sign in to comment.