All files / src/utils select.ts

96.55% Statements 56/58
87.5% Branches 35/40
100% Functions 7/7
96.55% Lines 56/58

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160                              43x   43x               43x     524x   524x 2041x   2041x   2041x 2041x 940x   940x       940x       1101x       524x     43x     12x 12x   12x 3x     9x       9x     43x     1102x 1102x   1102x   1102x 120x         1102x 120x                   1102x 1x 1x   1101x 1101x     1102x 1102x 1102x   1102x     43x     2466x       1942x     524x       327x       197x   197x       197x   197x 197x   197x 827x 797x   395x       432x   94x     338x       197x    
/*
 * Copyright © 2019 Lisk Foundation
 *
 * See the LICENSE file at the top-level directory of this distribution
 * for licensing information.
 *
 * Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation,
 * no part of this software, including this file, may be copied, modified,
 * propagated, or distributed except according to the terms contained in the
 * LICENSE file.
 *
 * Removal or modification of this copyright notice is prohibited.
 *
 */
// tslint:disable-next-line no-require-imports
import shuffle = require('lodash.shuffle');
 
import { ConnectionKind } from '../constants';
import {
	P2PPeerInfo,
	P2PPeerSelectionForConnectionInput,
	P2PPeerSelectionForRequestInput,
	P2PPeerSelectionForSendInput,
} from '../p2p_types';
 
const _removeCommonIPsFromLists = (
	peerList: ReadonlyArray<P2PPeerInfo>,
): ReadonlyArray<P2PPeerInfo> => {
	const peerMap = new Map<string, P2PPeerInfo>();
 
	for (const peer of peerList) {
		const { sharedState } = peer;
		const peerHeight =
			sharedState && sharedState.height ? (sharedState.height as number) : 0;
 
		const tempPeer = peerMap.get(peer.ipAddress);
		if (tempPeer) {
			const { sharedState: tempSharedState } = tempPeer;
			const tempPeerHeight =
				tempSharedState && tempSharedState.height
					? (tempSharedState.height as number)
					: 0;
 
			Iif (peerHeight > tempPeerHeight) {
				peerMap.set(peer.ipAddress, peer);
			}
		} else {
			peerMap.set(peer.ipAddress, peer);
		}
	}
 
	return [...peerMap.values()];
};
 
export const selectPeersForRequest = (
	input: P2PPeerSelectionForRequestInput,
): ReadonlyArray<P2PPeerInfo> => {
	const { peers } = input;
	const peerLimit = input.peerLimit;
 
	if (peers.length === 0) {
		return [];
	}
 
	Iif (peerLimit === undefined) {
		return shuffle(peers);
	}
 
	return shuffle(peers).slice(0, peerLimit);
};
 
export const selectPeersForSend = (
	input: P2PPeerSelectionForSendInput,
): ReadonlyArray<P2PPeerInfo> => {
	const shuffledPeers = shuffle(input.peers);
	const peerLimit = input.peerLimit as number;
	// tslint:disable: no-magic-numbers
	const halfPeerLimit = Math.round(peerLimit / 2);
 
	const outboundPeers = shuffledPeers.filter((peerInfo: P2PPeerInfo) =>
		peerInfo.internalState
			? peerInfo.internalState.connectionKind === ConnectionKind.OUTBOUND
			: false,
	);
 
	const inboundPeers = shuffledPeers.filter((peerInfo: P2PPeerInfo) =>
		peerInfo.internalState
			? peerInfo.internalState.connectionKind === ConnectionKind.INBOUND
			: false,
	);
 
	// tslint:disable: no-let
	let shortestPeersList;
	// tslint:disable: no-let
	let longestPeersList;
 
	if (outboundPeers.length < inboundPeers.length) {
		shortestPeersList = outboundPeers;
		longestPeersList = inboundPeers;
	} else {
		shortestPeersList = inboundPeers;
		longestPeersList = outboundPeers;
	}
 
	const selectedFirstKindPeers = shortestPeersList.slice(0, halfPeerLimit);
	const remainingPeerLimit = peerLimit - selectedFirstKindPeers.length;
	const selectedSecondKindPeers = longestPeersList.slice(0, remainingPeerLimit);
 
	return selectedFirstKindPeers.concat(selectedSecondKindPeers);
};
 
export const selectPeersForConnection = (
	input: P2PPeerSelectionForConnectionInput,
): ReadonlyArray<P2PPeerInfo> => {
	if (
		(input.peerLimit && input.peerLimit < 0) ||
		(input.triedPeers.length === 0 && input.newPeers.length === 0)
	) {
		return [];
	}
 
	if (
		input.peerLimit === undefined ||
		input.peerLimit >= input.triedPeers.length + input.newPeers.length
	) {
		return _removeCommonIPsFromLists([...input.newPeers, ...input.triedPeers]);
	}
 
	// LIP004 https://github.com/LiskHQ/lips/blob/master/proposals/lip-0004.md#peer-discovery-and-selection
	const minimumProbability = 0.5;
	const x =
		input.triedPeers.length < 100
			? minimumProbability
			: input.triedPeers.length /
			  (input.triedPeers.length + input.newPeers.length);
	const r = Math.max(x, minimumProbability);
 
	const shuffledTriedPeers = shuffle(input.triedPeers);
	const shuffledNewPeers = shuffle(input.newPeers);
 
	const peerList = [...Array(input.peerLimit)].map(() => {
		if (shuffledTriedPeers.length !== 0) {
			if (Math.random() < r) {
				// With probability r
				return shuffledTriedPeers.pop() as P2PPeerInfo;
			}
		}
 
		if (shuffledNewPeers.length !== 0) {
			// With probability 1-r
			return shuffledNewPeers.pop() as P2PPeerInfo;
		}
 
		return shuffledTriedPeers.pop() as P2PPeerInfo;
	});
 
	// TODO: Remove the usage of height for choosing among peers having same ipAddress, instead use productivity and reputation
	return _removeCommonIPsFromLists(peerList);
};