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 #465 from 4miners/462_improve-sync-speed
Browse files Browse the repository at this point in the history
Improve sync speed - Closes #462
  • Loading branch information
karmacoma committed Mar 8, 2017
2 parents 8eaab90 + 10a0aa9 commit 3d18dfb
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions modules/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -521,12 +521,15 @@ __private.sync = function (cb) {
// Given a list of peers (with associated blockchain height), we find a list of good peers (likely to sync with), then perform a histogram cut, removing peers far from the most common observed height. This is not as easy as it sounds, since the histogram has likely been made accross several blocks, therefore need to aggregate).
__private.findGoodPeers = function (heights) {
var lastBlockHeight = modules.blocks.getLastBlock().height;
library.logger.trace('Good peers - received', {count: heights.length});

heights = heights.filter(function (item) {
// Removing unreachable peers or heights below last block height
return item != null || item.height >= lastBlockHeight;
return item != null && item.height >= lastBlockHeight;
});

library.logger.trace('Good peers - filtered', {count: heights.length});

// No peers found
if (heights.length === 0) {
return {height: 0, peers: []};
Expand Down Expand Up @@ -557,8 +560,11 @@ __private.findGoodPeers = function (heights) {
// Performing histogram cut of peers too far from histogram maximum
var peers = heights.filter(function (item) {
return item && Math.abs(height - item.height) < aggregation + 1;
}).map(function (item) {
return library.logic.peers.create(item);
});

library.logger.trace('Good peers - accepted', {count: peers.length});
library.logger.debug('Good peers', peers);

return {height: height, peers: peers};
Expand All @@ -576,13 +582,19 @@ Loader.prototype.getNetwork = function (cb) {
return setImmediate(cb, null, __private.network);
}

__private.network = __private.findGoodPeers(library.logic.peers.list());
modules.peers.list({}, function (err, peers) {
if (err) {
return setImmediate(cb, err);
}

if (!__private.network.peers.length) {
return setImmediate(cb, 'Failed to find enough good peers');
} else {
return setImmediate(cb, null, __private.network);
}
__private.network = __private.findGoodPeers(peers);

if (!__private.network.peers.length) {
return setImmediate(cb, 'Failed to find enough good peers');
} else {
return setImmediate(cb, null, __private.network);
}
});
};

Loader.prototype.syncing = function () {
Expand Down

0 comments on commit 3d18dfb

Please sign in to comment.