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 #570 from LiskHQ/0.8.2
Browse files Browse the repository at this point in the history
0.8.2 Hotfix
  • Loading branch information
karmacoma authored May 5, 2017
2 parents 79dd512 + f9b1405 commit 645c436
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 173 deletions.
2 changes: 1 addition & 1 deletion config.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"port": 8000,
"address": "0.0.0.0",
"version": "0.8.1",
"version": "0.8.2",
"minVersion": ">=0.5.0",
"fileLogLevel": "info",
"logFileName": "logs/lisk.log",
Expand Down
9 changes: 2 additions & 7 deletions helpers/RoundChanges.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,8 @@ var exceptions = require('./exceptions');

// Constructor
function RoundChanges (scope) {
if (scope.backwards) {
this.roundFees = Math.floor(scope.__private.unFeesByRound[scope.round]) || 0;
this.roundRewards = (scope.__private.unRewardsByRound[scope.round] || []);
} else {
this.roundFees = Math.floor(scope.__private.feesByRound[scope.round]) || 0;
this.roundRewards = (scope.__private.rewardsByRound[scope.round] || []);
}
this.roundFees = Math.floor(scope.roundFees) || 0;
this.roundRewards = (scope.roundRewards || []);

// Apply exception for round if required
if (exceptions.rounds[scope.round]) {
Expand Down
71 changes: 54 additions & 17 deletions logic/round.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@ var sql = require('../sql/rounds.js');
function Round (scope, t) {
this.scope = scope;
this.t = t;

// List of required scope properties
var requiredProperties = ['library', 'modules', 'block', 'round', 'backwards'];

// Require extra scope properties when finishing round
if (scope.finishRound) {
requiredProperties = requiredProperties.concat(['roundFees', 'roundRewards', 'roundDelegates', 'roundOutsiders']);
}

// Iterate over requiredProperties, checking for undefined scope properties
requiredProperties.forEach(function (property) {
if (scope[property] === undefined) {
throw 'Missing required scope property: ' + property;
}
});
}

// Public methods
Expand All @@ -23,11 +38,11 @@ Round.prototype.mergeBlockGenerator = function () {
};

Round.prototype.updateMissedBlocks = function () {
if (this.scope.outsiders.length === 0) {
if (this.scope.roundOutsiders.length === 0) {
return this.t;
}

return this.t.none(sql.updateMissedBlocks(this.scope.backwards), [this.scope.outsiders]);
return this.t.none(sql.updateMissedBlocks(this.scope.backwards), [this.scope.roundOutsiders]);
};

Round.prototype.getVotes = function () {
Expand Down Expand Up @@ -73,10 +88,16 @@ Round.prototype.applyRound = function () {
var roundChanges = new RoundChanges(this.scope);
var queries = [];

for (var i = 0; i < this.scope.delegates.length; i++) {
var delegate = this.scope.delegates[i];
// Reverse delegates if going backwards
var delegates = (this.scope.backwards) ? this.scope.roundDelegates.reverse() : this.scope.roundDelegates;

// Apply round changes to each delegate
for (var i = 0; i < this.scope.roundDelegates.length; i++) {
var delegate = this.scope.roundDelegates[i];
var changes = roundChanges.at(i);

this.scope.library.logger.trace('Delegate changes', { delegate: delegate, changes: changes });

queries.push(this.scope.modules.accounts.mergeAccountAndGet({
publicKey: delegate,
balance: (this.scope.backwards ? -changes.balance : changes.balance),
Expand All @@ -86,32 +107,48 @@ Round.prototype.applyRound = function () {
fees: (this.scope.backwards ? -changes.fees : changes.fees),
rewards: (this.scope.backwards ? -changes.rewards : changes.rewards)
}));
}

if (i === this.scope.delegates.length - 1) {
queries.push(this.scope.modules.accounts.mergeAccountAndGet({
publicKey: delegate,
balance: (this.scope.backwards ? -changes.feesRemaining : changes.feesRemaining),
u_balance: (this.scope.backwards ? -changes.feesRemaining : changes.feesRemaining),
blockId: this.scope.block.id,
round: this.scope.round,
fees: (this.scope.backwards ? -changes.feesRemaining : changes.feesRemaining)
}));
}
// Decide which delegate receives fees remainder
var remainderIndex = (this.scope.backwards) ? 0 : delegates.length - 1;
var remainderDelegate = delegates[remainderIndex];

// Get round changes for chosen delegate
var changes = roundChanges.at(remainderIndex);

// Apply fees remaining to chosen delegate
if (changes.feesRemaining > 0) {
var feesRemaining = (this.scope.backwards ? -changes.feesRemaining : changes.feesRemaining);

this.scope.library.logger.trace('Fees remaining', { index: remainderIndex, delegate: remainderDelegate, fees: feesRemaining });

queries.push(this.scope.modules.accounts.mergeAccountAndGet({
publicKey: remainderDelegate,
balance: feesRemaining,
u_balance: feesRemaining,
blockId: this.scope.block.id,
round: this.scope.round,
fees: feesRemaining
}));
}

return this.t.none(queries.join(''));
this.scope.library.logger.trace('Applying round', queries);

if (queries.length > 0) {
return this.t.none(queries.join(''));
} else {
return this.t;
}
};

Round.prototype.land = function () {
this.scope.__private.ticking = true;
return this.updateVotes()
.then(this.updateMissedBlocks.bind(this))
.then(this.flushRound.bind(this))
.then(this.applyRound.bind(this))
.then(this.updateVotes.bind(this))
.then(this.flushRound.bind(this))
.then(function () {
this.scope.__private.ticking = false;
return this.t;
}.bind(this));
};
Expand Down
26 changes: 22 additions & 4 deletions modules/accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,26 @@ Accounts.prototype.getAccounts = function (filter, fields, cb) {

Accounts.prototype.setAccountAndGet = function (data, cb) {
var address = data.address || null;
var err;

if (address === null) {
if (data.publicKey) {
address = self.generateAddressByPublicKey(data.publicKey);
} else {
return setImmediate(cb, 'Missing address or public key');
err = 'Missing address or public key';
}
}

if (!address) {
return setImmediate(cb, 'Invalid public key');
err = 'Invalid public key';
}

if (err) {
if (typeof cb === 'function') {
return setImmediate(cb, err);
} else {
throw err;
}
}

library.logic.account.set(address, data, function (err) {
Expand All @@ -116,17 +125,26 @@ Accounts.prototype.setAccountAndGet = function (data, cb) {

Accounts.prototype.mergeAccountAndGet = function (data, cb) {
var address = data.address || null;
var err;

if (address === null) {
if (data.publicKey) {
address = self.generateAddressByPublicKey(data.publicKey);
} else {
return setImmediate(cb, 'Missing address or public key');
err = 'Missing address or public key';
}
}

if (!address) {
return setImmediate(cb, 'Invalid public key');
err = 'Invalid public key';
}

if (err) {
if (typeof cb === 'function') {
return setImmediate(cb, err);
} else {
throw err;
}
}

return library.logic.account.merge(address, data, cb);
Expand Down
64 changes: 34 additions & 30 deletions modules/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,8 @@ __private.applyBlock = function (block, broadcast, cb, saveBlock) {
undoUnconfirmedList: function (seriesCb) {
modules.transactions.undoUnconfirmedList(function (err, ids) {
if (err) {
// TODO: Send a numbered signal to be caught by forever to trigger a rebuild.
// Fatal error, memory tables will be inconsistent
library.logger.error('Failed to undo unconfirmed list', err);
return process.exit(0);
} else {
unconfirmedTransactionIds = ids;
Expand Down Expand Up @@ -466,7 +467,7 @@ __private.applyBlock = function (block, broadcast, cb, saveBlock) {
err = ['Failed to apply transaction:', transaction.id, '-', err].join(' ');
library.logger.error(err);
library.logger.error('Transaction', transaction);
// TODO: Send a numbered signal to be caught by forever to trigger a rebuild.
// Fatal error, memory tables will be inconsistent
process.exit(0);
}
// DATABASE: write
Expand All @@ -475,7 +476,7 @@ __private.applyBlock = function (block, broadcast, cb, saveBlock) {
err = ['Failed to apply transaction:', transaction.id, '-', err].join(' ');
library.logger.error(err);
library.logger.error('Transaction', transaction);
// TODO: Send a numbered signal to be caught by forever to trigger a rebuild.
// Fatal error, memory tables will be inconsistent
process.exit(0);
}
// Transaction applied, removed from the unconfirmed list.
Expand All @@ -497,7 +498,7 @@ __private.applyBlock = function (block, broadcast, cb, saveBlock) {
if (err) {
library.logger.error('Failed to save block...');
library.logger.error('Block', block);
// TODO: Send a numbered signal to be caught by forever to trigger a rebuild.
// Fatal error, memory tables will be inconsistent
process.exit(0);
}

Expand Down Expand Up @@ -639,10 +640,24 @@ __private.popLastBlock = function (oldLastBlock, cb) {
}
], cb);
}, function (err) {
modules.rounds.backwardTick(oldLastBlock, previousBlock, function () {
if (err) {
// Fatal error, memory tables will be inconsistent
library.logger.error('Failed to undo transactions', err);
return process.exit(0);
}

modules.rounds.backwardTick(oldLastBlock, previousBlock, function (err) {
if (err) {
// Fatal error, memory tables will be inconsistent
library.logger.error('Failed to perform backwards tick', err);
return process.exit(0);
}

__private.deleteBlock(oldLastBlock.id, function (err) {
if (err) {
return setImmediate(cb, err);
// Fatal error, memory tables will be inconsistent
library.logger.error('Failed to delete block', err);
return process.exit(0);
}

return setImmediate(cb, null, previousBlock);
Expand Down Expand Up @@ -678,9 +693,9 @@ __private.receiveBlock = function (block, cb) {
library.logger.info([
'Received new block id:', block.id,
'height:', block.height,
'round:', modules.rounds.calc(modules.blocks.getLastBlock().height),
'round:', modules.rounds.calc(block.height),
'slot:', slots.getSlotNumber(block.timestamp),
'reward:', modules.blocks.getLastBlock().reward
'reward:', block.reward
].join(' '));

self.lastReceipt(new Date());
Expand Down Expand Up @@ -960,21 +975,16 @@ Blocks.prototype.deleteLastBlock = function (cb) {
}

async.series({
backwardSwap: function (seriesCb) {
modules.rounds.directionSwap('backward', null, seriesCb);
},
popLastBlock: function (seriesCb) {
__private.popLastBlock(__private.lastBlock, function (err, newLastBlock) {
if (err) {
library.logger.error('Error deleting last block', __private.lastBlock);
return setImmediate(seriesCb, err);
} else {
__private.lastBlock = newLastBlock;
return setImmediate(seriesCb);
}

__private.lastBlock = newLastBlock;
return setImmediate(seriesCb);
});
},
forwardSwap: function (seriesCb) {
modules.rounds.directionSwap('forward', __private.lastBlock, seriesCb);
}
}, function (err) {
return setImmediate(cb, err, __private.lastBlock);
Expand Down Expand Up @@ -1234,9 +1244,6 @@ Blocks.prototype.deleteBlocksBefore = function (block, cb) {
var blocks = [];

async.series({
backwardSwap: function (seriesCb) {
modules.rounds.directionSwap('backward', null, seriesCb);
},
popBlocks: function (seriesCb) {
async.whilst(
function () {
Expand All @@ -1253,9 +1260,6 @@ Blocks.prototype.deleteBlocksBefore = function (block, cb) {
return setImmediate(seriesCb, err, blocks);
}
);
},
forwardSwap: function (seriesCb) {
modules.rounds.directionSwap('forward', __private.lastBlock, seriesCb);
}
});
};
Expand All @@ -1275,14 +1279,14 @@ Blocks.prototype.sandboxApi = function (call, args, cb) {

// Events
Blocks.prototype.onReceiveBlock = function (block) {
// When client is not loaded, is syncing or round is ticking
// Do not receive new blocks as client is not ready
if (!__private.loaded || modules.loader.syncing() || modules.rounds.ticking()) {
library.logger.debug('Client not ready to receive block', block.id);
return;
}

library.sequence.add(function (cb) {
// When client is not loaded, is syncing or round is ticking
// Do not receive new blocks as client is not ready
if (!__private.loaded || modules.loader.syncing() || modules.rounds.ticking()) {
library.logger.debug('Client not ready to receive block', block.id);
return setImmediate(cb);
}

if (block.previousBlock === __private.lastBlock.id && __private.lastBlock.height + 1 === block.height) {
return __private.receiveBlock(block, cb);
} else if (block.previousBlock !== __private.lastBlock.id && __private.lastBlock.height + 1 === block.height) {
Expand Down
Loading

0 comments on commit 645c436

Please sign in to comment.