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

Clean up loadBlocksFromNetwork function from loader module - Closes #2292 & #2384 #2555

Merged

Conversation

diego-G
Copy link

@diego-G diego-G commented Nov 16, 2018

What was the problem?

The flow of the function was not easy to understand obscuring errors.

How did I fix it?

How to test it?

Review checklist

When a timeout is triggered from loadBlocksFromPeer, loaded value changes to true incorrectly stopping the synch process without a proper reason. Besides the number of errors to stop the process is too little due to most of them are timeouts.
This metthod is currently used privately in Loader module. Instead of
getting all the peers, we get a random one.
If the function is called with height 1 which means the genesis block,
it has to call the callback without error returning block undefined.
It adds Try count to follow the failed attemps and decreases the limit
to 5 again.
@@ -261,6 +261,11 @@ __private.receiveForkFive = function(block, lastBlock, cb) {
Process.prototype.getCommonBlock = function(peer, height, cb) {
let comparisonFailed = false;

// Allowing blocks to be loaded from genesis
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 this actually?

Copy link
Author

Choose a reason for hiding this comment

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

I move it back to the former method.

@@ -846,86 +842,92 @@ __private.snapshotFinished = err => {
* @todo Add description for the params
*/
__private.loadBlocksFromNetwork = function(cb) {
let errorCount = 0;
// Number of failed attempts to load from peers.
let tries = 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we reset this when an attempt is successful? Also, the variable name can be more descriptive.

modules/loader.js Outdated Show resolved Hide resolved
);
errorCount += 1;
return next();
if (!commonBlock && lastBlock.height != 1) {
Copy link
Contributor

Choose a reason for hiding this comment

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

When lastBlock.height is 1 we will not log anything, it this behavior correct?

},
],
err => {
if (err) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This will never happen because we always call waterfallCb without error.

modules/loader.js Outdated Show resolved Hide resolved
@diego-G diego-G force-pushed the 2384-improve_performance_synchronisation_process branch from 7b75776 to 9defa79 Compare November 16, 2018 14:51
@diego-G diego-G requested a review from 4miners November 16, 2018 14:52
modules/loader.js Outdated Show resolved Hide resolved
modules/loader.js Outdated Show resolved Hide resolved
modules/loader.js Outdated Show resolved Hide resolved
modules/loader.js Outdated Show resolved Hide resolved
modules/loader.js Outdated Show resolved Hide resolved
Copy link
Contributor

@nazarhussain nazarhussain left a comment

Choose a reason for hiding this comment

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

  1. Use function names in async steps, to have clear understanding.
  2. Use error object names to avoid error shadowing in the code
  3. Add proper explanation comments for better understanding

The actual issue #2292 describe the problem as:

The function adds a lot of obscurity in the understanding of the functionality, it jumps control flow in multiple ways and is missing useful comments.

I feel the function loadBlocksFromNetwork still have same problems mentioned as above. I suggest to ask author of the issue @SargeKhan to review it and see if his concerns are covered.

nazarhussain
nazarhussain previously approved these changes Nov 19, 2018
@diego-G
Copy link
Author

diego-G commented Nov 19, 2018

@nazarhussain I don't think we need more people reviewing the PR. I've added another commit to address the point 1. What I feel it is that we need a proper refactoring across the entire code to tackle these kind of concerns plus having any kind of prettifier/formatter. This will speed up our review process.

@4miners
Copy link
Contributor

4miners commented Nov 19, 2018

The issue #2384 is covered by this PR only partially. I think we shouldn't close it yet.

@@ -1054,13 +1041,13 @@ Loader.prototype.findGoodPeers = function(peers) {

// Public methods
/**
* Gets a list of "good" peers from network.
Copy link
Contributor

Choose a reason for hiding this comment

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

Please extend the comment and explain what does it mean to be a good peer.

Copy link
Author

Choose a reason for hiding this comment

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

A good peer is what findGoodPeers() decides. I haven't modified that function. It performs a complex calculation that cannot be explained easily:

  • It filters unreachable peers and those ones below the node's height itself.
  • It orders peers by descending height
  • It creates a histogram of peer's heights
  • It removes outliers

I'd open another PR if we need to review that process.

@diego-G
Copy link
Author

diego-G commented Nov 20, 2018

@4miners #2384 contains a bug in the first two bullets and an improvement in the third bullet that should be studied in a deeper way. If we think it is worthy, we should open an independent issue for it.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Clean up loadBlocksFromNetwork function in modules.loader
4 participants