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

Improve block propagation - Closes #541 #623

Merged
merged 40 commits into from
Aug 8, 2017
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
8f9c3cf
Split broadcast, adjust block properties
LucasIsasmendi Jun 14, 2017
f134cae
Merge branch '459-block-verification' into 541-block-propagation
LucasIsasmendi Jun 14, 2017
9a0131d
Fix addBlockProperties
LucasIsasmendi Jun 21, 2017
53462e6
Log with join()
LucasIsasmendi Jun 21, 2017
4c949b5
Add bus.message function
LucasIsasmendi Jun 21, 2017
f836fad
Add clearMessages to bus
LucasIsasmendi Jun 21, 2017
c9f5e0f
Test processBlock()
LucasIsasmendi Jun 21, 2017
df5c320
Eslint adjusts
LucasIsasmendi Jun 21, 2017
2d3e959
Check undefined
LucasIsasmendi Jun 21, 2017
e069ea4
Delete block properties
LucasIsasmendi Jun 21, 2017
084c0b5
Add bson serialization and deserialiation
LucasIsasmendi Jun 21, 2017
b1e0f52
Add test bson deserialiation
LucasIsasmendi Jun 21, 2017
eee4927
Add bson
LucasIsasmendi Jun 21, 2017
fe4c74e
Merge branch '459-block-verification' into 541-block-propagation
LucasIsasmendi Jun 22, 2017
5fc2ee2
Merge conflicts
LucasIsasmendi Jul 12, 2017
e30c968
Adjust modules
LucasIsasmendi Jul 12, 2017
1cddd52
Merge conflicts
LucasIsasmendi Jul 13, 2017
23cba63
Merge conflicts
LucasIsasmendi Jul 13, 2017
d168c0c
Merge conflicts
LucasIsasmendi Jul 15, 2017
8d7c4a8
Merge branch '459-block-verification' into 541-block-propagation
LucasIsasmendi Jul 15, 2017
b17114e
Add,delete default block properties
LucasIsasmendi Jul 16, 2017
ca3130c
Fix delete numberOfTransactions
LucasIsasmendi Jul 17, 2017
f4b23c5
Adjust test
LucasIsasmendi Jul 17, 2017
059834b
Merge branch '459-block-verification' into 541-block-propagation
LucasIsasmendi Jul 17, 2017
9015e51
Fix bson version
LucasIsasmendi Jul 19, 2017
ac70b4e
Merge branch
LucasIsasmendi Jul 27, 2017
7fc68ae
Adjust test descriptions
LucasIsasmendi Jul 27, 2017
4adfa0d
Merge branch '459-block-verification' into 541-block-propagation
LucasIsasmendi Jul 27, 2017
e16422b
Adjust test description
LucasIsasmendi Jul 27, 2017
6c60167
Merge branch '459-block-verification' into 541-block-propagation
LucasIsasmendi Jul 28, 2017
1b96bd7
Merge conflicts
LucasIsasmendi Jul 28, 2017
e925954
Skip transactions and reward when empty
LucasIsasmendi Aug 3, 2017
ad6124d
Fix eslint
LucasIsasmendi Aug 3, 2017
78567cb
Delete unnecessary try/catch block
LucasIsasmendi Aug 3, 2017
c2885a7
Adjust jsdoc comment
LucasIsasmendi Aug 3, 2017
360949f
Merge branch '1.0.0' into 541-block-propagation
4miners Aug 8, 2017
4dee044
Raise newBlock event after block is saved to database
4miners Aug 8, 2017
268c9c1
Remove newBlock events
4miners Aug 8, 2017
2277051
Fix formatting
4miners Aug 8, 2017
fbdda75
Remove redundant return
4miners Aug 8, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions logic/broadcaster.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ var constants = require('../helpers/constants.js');
var jobsQueue = require('../helpers/jobsQueue.js');
var extend = require('extend');
var _ = require('lodash');
var BSON = require('bson');

var bson = new BSON();

// Private fields
var modules, library, self, __private = {};
Expand Down Expand Up @@ -151,6 +154,9 @@ Broadcaster.prototype.broadcast = function (params, options, cb) {
}
},
function getFromPeer (peers, waterCb) {
if (options.data.block) {
options.data.block = bson.deserialize(options.data.block);
}
library.logger.debug('Begin broadcast', options);

if (params.limit === self.config.peerLimit) {
Expand Down
21 changes: 16 additions & 5 deletions modules/blocks/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,13 +327,12 @@ __private.applyTransaction = function (block, transaction, sender, cb) {
* @method applyBlock
* @emits SIGTERM
* @param {Object} block Full normalized block
* @param {boolean} broadcast Indicator that block needs to be broadcasted
* @param {Function} cb Callback function
* @param {boolean} saveBlock Indicator that block needs to be saved to database
* @param {Function} cb Callback function
* @return {Function} cb Callback function from params (through setImmediate)
* @return {Object} cb.err Error if occurred
*/
Chain.prototype.applyBlock = function (block, broadcast, cb, saveBlock) {
Chain.prototype.applyBlock = function (block, saveBlock, cb) {
// Prevent shutdown during database writes.
modules.blocks.isActive.set(true);

Expand Down Expand Up @@ -458,13 +457,11 @@ Chain.prototype.applyBlock = function (block, broadcast, cb, saveBlock) {
}

library.logger.debug('Block applied correctly with ' + block.transactions.length + ' transactions');
library.bus.message('newBlock', block, broadcast);

// DATABASE write. Update delegates accounts
modules.rounds.tick(block, seriesCb);
});
} else {
library.bus.message('newBlock', block, broadcast);

// DATABASE write. Update delegates accounts
modules.rounds.tick(block, seriesCb);
Expand Down Expand Up @@ -497,6 +494,20 @@ Chain.prototype.applyBlock = function (block, broadcast, cb, saveBlock) {
});
};

/**
* Broadcast reduced block to increase network performance.
* @param {Object} reducedBlock reduced block
* @param {boolean} broadcast Indicator that block needs to be broadcasted
* @throws {string} Error description
*/
Chain.prototype.broadcastReducedBlock = function (reducedBlock, broadcast) {
try {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we need try-catch block here? library.bus.message not throws.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, at the beginning bus.message was not in the test scope and try/catch warned about that. There is no reason to keep the warning because bus will be loaded from start.

library.bus.message('newBlock', reducedBlock, broadcast);
} catch (e) {
throw 'reduced newBlock broadcast message error: ' + e;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aren't we loosing stack here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also bus.message function throw an error even?

}
library.logger.debug(['reducedBlock', reducedBlock.id, 'broadcasted correctly'].join(' '));
};

/**
* Deletes last block, undo transactions, recalculate round
Expand Down
4 changes: 2 additions & 2 deletions modules/blocks/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,10 @@ Process.prototype.loadBlocksOffset = function (limit, offset, verify, cb) {
if (block.id === library.genesisblock.block.id) {
modules.blocks.chain.applyGenesisBlock(block, cb);
} else {
// Apply block - broadcast: false, saveBlock: false
// Apply block - saveBlock: false
// FIXME: Looks like we are missing some validations here, because applyBlock is different than processBlock used elesewhere
// - that need to be checked and adjusted to be consistent
modules.blocks.chain.applyBlock(block, false, cb, false);
modules.blocks.chain.applyBlock(block, false, cb);
}
// Update last block
modules.blocks.lastBlock.set(block);
Expand Down
73 changes: 70 additions & 3 deletions modules/blocks/verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ var crypto = require('crypto');
var slots = require('../../helpers/slots.js');
var sql = require('../../sql/blocks.js');
var exceptions = require('../../helpers/exceptions.js');
var BSON = require('bson');

var bson = new BSON();

var modules, library, self, __private = {};

Expand Down Expand Up @@ -87,9 +90,47 @@ __private.checkTransaction = function (block, transaction, cb) {
});
};

/**
* Adds default properties to block.
* @private
* @param {Object} block Block object reduced
* @return {Object} Block object completed
*/
__private.addBlockProperties = function (block) {
if (block.version === undefined) {
block.version = 0;
}
if (block.numberOfTransactions === undefined) {
if (block.transactions === undefined) {
block.numberOfTransactions = 0;
} else {
block.numberOfTransactions = block.transactions.length;
}
}

return block;
};

/**
* Deletes default properties from block.
* @private
* @param {Object} block Block object completed
* @return {Object} Block object reduced
*/
__private.deleteBlockProperties = function (block) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

totalAmount, totalFee, payloadLength can also be skipped if 0

var reducedBlock = JSON.parse(JSON.stringify(block));
if (reducedBlock.version === 0) {
delete reducedBlock.version;
}
// verifyBlock ensures numberOfTransactions is transactions.length
if (reducedBlock.numberOfTransactions) {
delete reducedBlock.numberOfTransactions;
}
return reducedBlock;
};

/**
* Verify block and return all possible errors related to block
*
* @public
* @method verifyBlock
* @param {Object} block Full block
Expand Down Expand Up @@ -158,7 +199,7 @@ Verify.prototype.verifyBlock = function (block) {
totalAmount += transaction.amount;
totalFee += transaction.fee;
}

if (payloadHash.digest().toString('hex') !== block.payloadHash) {
return 'Invalid payload hash';
}
Expand Down Expand Up @@ -227,6 +268,18 @@ Verify.prototype.processBlock = function (block, broadcast, cb, saveBlock) {
}

async.series({
addBlockProperties: function (seriesCb) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That should be performed inside objectNormalize

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Block.prototype.create calls to objectNormalize, there are no missed properties, therefore no need to perform addBlockProperties logic. Same for loadBlocksOffset, with data from DB.
receiveBlock will need it, so we can add it to public logic.block and call it from transport and verify to avoid one function with two logics.

if (!broadcast) {
try {
// set default properties
block = __private.addBlockProperties(block);
} catch (err) {
return setImmediate(seriesCb, err);
}
}

return setImmediate(seriesCb);
},
normalizeBlock: function (seriesCb) {
try {
block = library.logic.block.objectNormalize(block);
Expand All @@ -236,6 +289,20 @@ Verify.prototype.processBlock = function (block, broadcast, cb, saveBlock) {

return setImmediate(seriesCb);
},
deleteBlockProperties: function (seriesCb) {
if (broadcast) {
try {
// delete default properties
var blockReduced = __private.deleteBlockProperties(block);
var serializedBlockReduced = bson.serialize(blockReduced);
modules.blocks.chain.broadcastReducedBlock(serializedBlockReduced, broadcast);
} catch (err) {
return setImmediate(seriesCb, err);
}
}

return setImmediate(seriesCb);
},
verifyBlock: function (seriesCb) {
// Sanity check of the block, if values are coherent.
// No access to database
Expand Down Expand Up @@ -289,7 +356,7 @@ Verify.prototype.processBlock = function (block, broadcast, cb, saveBlock) {
// * Block and transactions have valid values (signatures, block slots, etc...)
// * The check against database state passed (for instance sender has enough LSK, votes are under 101, etc...)
// We thus update the database with the transactions values, save the block and tick it.
modules.blocks.chain.applyBlock(block, broadcast, cb, saveBlock);
modules.blocks.chain.applyBlock(block, saveBlock, cb);
}
});
};
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"async": "=2.1.4",
"bignumber.js": "=4.0.0",
"body-parser": "=1.16.0",
"bson": "^1.0.4",
"bytebuffer": "=5.0.1",
"change-case": "=3.0.1",
"colors": "=1.1.2",
Expand Down
16 changes: 14 additions & 2 deletions test/common/initModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,25 @@ var modulesLoader = new function () {
genesisblock: { block: genesisblock },
logger: this.logger,
network: {
app: express()
app: express(),
io: {
sockets: express()
}
},
public: '../../public',
schema: new z_schema(),
ed: ed,
bus: {
message: function () {}
argsMessages: [],
message: function () {
Array.prototype.push.apply(this.argsMessages, arguments);
},
getMessages: function () {
return this.argsMessages;
},
clearMessages: function () {
this.argsMessages = [];
}
},
nonce: randomString.generate(16)
};
Expand Down
Loading