From 6b61454bb26df6700c9d79a0d8df59d8ace9378f Mon Sep 17 00:00:00 2001 From: Maciej Baj Date: Tue, 21 Nov 2017 17:13:16 +0100 Subject: [PATCH 1/3] Refactor peers.getConsensus function --- modules/peers.js | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/modules/peers.js b/modules/peers.js index 79e147c0478..362697e19ba 100644 --- a/modules/peers.js +++ b/modules/peers.js @@ -305,29 +305,28 @@ __private.dbSave = function (cb) { }); }; -Peers.prototype.getConsensus = function (matched, active) { - +/** +* Calculates consensus for as a ratio active to matched peers. +* @param {Array} active - active peers (with connected state) +* @param {Array} matched - peers with same as system broadhash +* @returns {number|undefined} - return consensus or undefined if config.forging.force = true +*/ +Peers.prototype.getConsensus = function (active, matched) { if (library.config.forging.force) { return undefined; } - - active = active || __private.getByFilter({state: Peer.STATE.CONNECTED, normalized: false}); - matched = matched || __private.getMatched({broadhash: modules.system.getBroadhash()}, active); - - active = active.slice(0, constants.maxPeers); - matched = matched.slice(0, constants.maxPeers); - - var consensus = Math.round(matched.length / active.length * 100 * 1e2) / 100; - - if (isNaN(consensus)) { - return 0; - } - - return consensus; + active = active || library.logic.peers.list(true); + var broadhash = modules.system.getBroadhash(); + matched = matched || active.filter(function (peer) { + return peer.broadhash === broadhash; + }); + var activeCount = Math.min(active.length, constants.maxPeers); + var matchedCount = Math.min(matched.length, activeCount); + var consensus = +(matchedCount / activeCount * 100).toPrecision(2); + return isNaN(consensus) ? 0 : consensus; }; // Public methods - /** * Updates peer in peers list. * @param {peer} peer From 816e2471743703eac9cba08cd51180964e1ec852 Mon Sep 17 00:00:00 2001 From: Maciej Baj Date: Thu, 23 Nov 2017 16:52:24 +0100 Subject: [PATCH 2/3] Adapt peers.getConsensus calls after arguments order changed --- logic/broadcaster.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/logic/broadcaster.js b/logic/broadcaster.js index 4ff1fdbfa33..03df2e68bac 100644 --- a/logic/broadcaster.js +++ b/logic/broadcaster.js @@ -111,7 +111,7 @@ Broadcaster.prototype.getPeers = function (params, cb) { } if (self.consensus !== undefined && originalLimit === constants.maxPeers) { - self.consensus = modules.peers.getConsensus(null, peers); + self.consensus = modules.peers.getConsensus(peers); library.logger.info(['Broadhash consensus now', self.consensus, '%'].join(' ')); } From e8c589bf09f4b72a19aac0bc4ebef248c81a1154 Mon Sep 17 00:00:00 2001 From: Oliver Beddows Date: Sun, 26 Nov 2017 23:36:26 +0100 Subject: [PATCH 3/3] Standardise docs --- modules/peers.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/peers.js b/modules/peers.js index 362697e19ba..db03c202f2a 100644 --- a/modules/peers.js +++ b/modules/peers.js @@ -307,9 +307,9 @@ __private.dbSave = function (cb) { /** * Calculates consensus for as a ratio active to matched peers. -* @param {Array} active - active peers (with connected state) -* @param {Array} matched - peers with same as system broadhash -* @returns {number|undefined} - return consensus or undefined if config.forging.force = true +* @param {Array} active - Active peers (with connected state). +* @param {Array} matched - Peers with same as system broadhash. +* @returns {number|undefined} - Consensus or undefined if config.forging.force = true. */ Peers.prototype.getConsensus = function (active, matched) { if (library.config.forging.force) {