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 #1020 from LiskHQ/1008-refactor-peers-list-arguments
Browse files Browse the repository at this point in the history
Remove argument mutation from Peers.prototype.list - Subtask #1008
  • Loading branch information
karmacoma authored Nov 26, 2017
2 parents 9707be0 + d815ce6 commit 2fbc5f8
Showing 1 changed file with 27 additions and 22 deletions.
49 changes: 27 additions & 22 deletions modules/peers.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,61 +444,66 @@ Peers.prototype.acceptable = function (peers) {

/**
* Gets peers list and calculated consensus.
* @param {Object} options - Constains limit, broadhash.
* @param {Object} options
* @param {number} options.limit[=constants.maxPeers] - Maximum number of peers to get.
* @param {string} options.broadhash[=null] - Broadhash to match peers by.
* @param {string} options.normalized[=undefined] - Return peers in normalized (json) form.
* @param {Array} options.allowedStates[=[2]] - Allowed peer states.
* @param {number} options.attempt[=undefined] - If 0: Return peers with equal options.broadhash
* If 1: Return peers with different options.broadhash
* If not specified: return peers regardless of options.broadhash
* @param {function} cb - Callback function.
* @returns {setImmediateCallback} error | peers, consensus
*/
Peers.prototype.list = function (options, cb) {
options.limit = options.limit || constants.maxPeers;
options.broadhash = options.broadhash || modules.system.getBroadhash();
options.allowedStates = options.allowedStates || [Peer.STATE.CONNECTED];
options.attempts = ['matched broadhash', 'unmatched broadhash'];
options.attempt = 0;
options.matched = 0;

function randomList (options, peers, cb) {
var limit = options.limit || constants.maxPeers;
var broadhash = options.broadhash || modules.system.getBroadhash();
var allowedStates = options.allowedStates || [Peer.STATE.CONNECTED];
var attempts = (options.attempt === 0 || options.attempt === 1) ? [options.attempt] : [1, 0];
var attemptsDescriptions = ['matched broadhash', 'unmatched broadhash'];

function randomList (peers, cb) {
// Get full peers list (random)
__private.getByFilter({normalized: options.normalized}, function (err, peersList) {
var accepted, found, matched, picked;

found = peersList.length;
var attempt = attempts.pop();
// Apply filters
peersList = peersList.filter(function (peer) {
if (options.broadhash) {
if (broadhash) {
// Skip banned and disconnected peers by default
return options.allowedStates.indexOf(peer.state) !== -1 && (
return allowedStates.indexOf(peer.state) !== -1 && (
// Matched broadhash when attempt 0
options.attempt === 0 ? (peer.broadhash === options.broadhash) :
attempt === 0 ? (peer.broadhash === broadhash) :
// Unmatched broadhash when attempt 1
options.attempt === 1 ? (peer.broadhash !== options.broadhash) : false
attempt === 1 ? (peer.broadhash !== broadhash) : false
);
} else {
// Skip banned and disconnected peers by default
return options.allowedStates.indexOf(peer.state) !== -1;
return allowedStates.indexOf(peer.state) !== -1;
}
});
matched = peersList.length;
// Apply limit
peersList = peersList.slice(0, options.limit);
peersList = peersList.slice(0, limit);
picked = peersList.length;
accepted = peers.concat(peersList);
library.logger.debug('Listing peers', {attempt: options.attempts[options.attempt], found: found, matched: matched, picked: picked, accepted: accepted.length});
library.logger.debug('Listing peers', {attempt: attemptsDescriptions[options.attempt], found: found, matched: matched, picked: picked, accepted: accepted.length});
return setImmediate(cb, null, accepted);
});
}

async.waterfall([
function (waterCb) {
// Matched broadhash
return randomList (options, [], waterCb);
return randomList([], waterCb);
},
function (peers, waterCb) {
options.matched = peers.length;
options.limit -= peers.length;
++options.attempt;
if (options.limit > 0) {
limit -= peers.length;
if (attempts.length && limit > 0) {
// Unmatched broadhash
return randomList(options, peers, waterCb);
return randomList(peers, waterCb);
} else {
return setImmediate(waterCb, null, peers);
}
Expand Down

0 comments on commit 2fbc5f8

Please sign in to comment.