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

Calculate consensus only from outbound connected peers - Closes #3969 #4134

Merged
merged 15 commits into from
Aug 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions elements/lisk-p2p/src/p2p.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,8 +534,10 @@ export class P2P extends EventEmitter {
return this._peerPool.getAllConnectedPeerInfos();
}

public getUniqueConnectedPeers(): ReadonlyArray<P2PDiscoveredPeerInfo> {
return this._peerPool.getUniqueConnectedPeers();
public getUniqueOutboundConnectedPeers(): ReadonlyArray<
mitsuaki-u marked this conversation as resolved.
Show resolved Hide resolved
P2PDiscoveredPeerInfo
> {
return this._peerPool.getUniqueOutboundConnectedPeers();
}

public getDisconnectedPeers(): ReadonlyArray<P2PDiscoveredPeerInfo> {
Expand Down
12 changes: 8 additions & 4 deletions elements/lisk-p2p/src/peer_pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,12 +534,16 @@ export class PeerPool extends EventEmitter {
return peers;
}

public getUniqueConnectedPeers(): ReadonlyArray<P2PDiscoveredPeerInfo> {
return getUniquePeersbyIp(this.getAllConnectedPeerInfos());
public getUniqueOutboundConnectedPeers(): ReadonlyArray<
P2PDiscoveredPeerInfo
> {
return getUniquePeersbyIp(this.getAllConnectedPeerInfos(OutboundPeer));
}

public getAllConnectedPeerInfos(): ReadonlyArray<P2PDiscoveredPeerInfo> {
return this.getConnectedPeers().map(
public getAllConnectedPeerInfos(
kind?: typeof OutboundPeer | typeof InboundPeer,
): ReadonlyArray<P2PDiscoveredPeerInfo> {
return this.getConnectedPeers(kind).map(
peer => peer.peerInfo as P2PDiscoveredPeerInfo,
);
}
Expand Down
20 changes: 10 additions & 10 deletions elements/lisk-p2p/test/unit/peer_pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,27 +248,27 @@ describe('peerPool', () => {
});
});

describe('#getUniqueConnectedPeers', () => {
describe('#getUniqueOutboundConnectedPeers', () => {
const samplePeers = initializePeerInfoList();

describe('when two peers have same peer infos', () => {
let uniqueConnectedPeers: ReadonlyArray<P2PDiscoveredPeerInfo>;
let uniqueOutboundConnectedPeers: ReadonlyArray<P2PDiscoveredPeerInfo>;
diego-G marked this conversation as resolved.
Show resolved Hide resolved

beforeEach(async () => {
const duplicatesList = [...samplePeers, samplePeers[0], samplePeers[1]];
sandbox
.stub(peerPool, 'getAllConnectedPeerInfos')
.returns(duplicatesList);
uniqueConnectedPeers = peerPool.getUniqueConnectedPeers();
uniqueOutboundConnectedPeers = peerPool.getUniqueOutboundConnectedPeers();
});

it('should remove the duplicate peers with the same ips', async () => {
expect(uniqueConnectedPeers).eql(samplePeers);
expect(uniqueOutboundConnectedPeers).eql(samplePeers);
});
});

describe('when two peers have same IP and different wsPort and height', () => {
let uniqueConnectedPeers: ReadonlyArray<P2PDiscoveredPeerInfo>;
let uniqueOutboundConnectedPeers: ReadonlyArray<P2PDiscoveredPeerInfo>;

beforeEach(async () => {
const peer1 = {
Expand All @@ -287,16 +287,16 @@ describe('peerPool', () => {
sandbox
.stub(peerPool, 'getAllConnectedPeerInfos')
.returns(duplicatesList);
uniqueConnectedPeers = peerPool.getUniqueConnectedPeers();
uniqueOutboundConnectedPeers = peerPool.getUniqueOutboundConnectedPeers();
});

it('should remove the duplicate ip and choose the one with higher height', async () => {
expect(uniqueConnectedPeers).eql(samplePeers);
expect(uniqueOutboundConnectedPeers).eql(samplePeers);
});
});

describe('when two peers have same IP and different wsPort but same height', () => {
let uniqueConnectedPeers: ReadonlyArray<P2PDiscoveredPeerInfo>;
let uniqueOutboundConnectedPeers: ReadonlyArray<P2PDiscoveredPeerInfo>;

beforeEach(async () => {
const peer1 = {
Expand All @@ -315,11 +315,11 @@ describe('peerPool', () => {
sandbox
.stub(peerPool, 'getAllConnectedPeerInfos')
.returns(duplicatesList);
uniqueConnectedPeers = peerPool.getUniqueConnectedPeers();
uniqueOutboundConnectedPeers = peerPool.getUniqueOutboundConnectedPeers();
});

it('should remove the duplicate ip and choose one of the peer in sequence', async () => {
expect(uniqueConnectedPeers).eql(samplePeers);
expect(uniqueOutboundConnectedPeers).eql(samplePeers);
});
});
});
Expand Down
14 changes: 10 additions & 4 deletions framework/src/modules/chain/peers.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,20 @@ class Peers {
const { broadhash } = await this.channel.invoke('app:getApplicationState');

const activeCount = Math.min(
await this.channel.invoke('network:getConnectedPeersCountByFilter', {}),
await this.channel.invoke(
'network:getUniqueOutboundConnectedPeersCount',
{},
),
MAX_PEERS,
);

const matchedCount = Math.min(
await this.channel.invoke('network:getConnectedPeersCountByFilter', {
broadhash,
}),
await this.channel.invoke(
'network:getUniqueOutboundConnectedPeersCount',
{
broadhash,
},
),
MAX_PEERS,
);

Expand Down
3 changes: 2 additions & 1 deletion framework/src/modules/http_api/controllers/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ async function _getForgingStatus(publicKey) {
* @private
*/
async function _getNetworkHeight() {
const peers = await library.channel.invoke('network:getConnectedPeers', {
const peers = await library.channel.invoke('network:getPeers', {
limit: 100,
state: 2,
});
if (!peers || !peers.length) {
return 0;
Expand Down
5 changes: 1 addition & 4 deletions framework/src/modules/http_api/controllers/peers.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,7 @@ PeersController.getPeers = async function getPeers(context, next) {

try {
const peersByFilters = await channel.invoke('network:getPeers', filters);
const peersCount = await channel.invoke(
'network:getPeersCountByFilter',
filters,
);
const peersCount = await channel.invoke('network:getPeersCount', filters);

return next(null, {
data: peersByFilters,
Expand Down
11 changes: 4 additions & 7 deletions framework/src/modules/network/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,12 @@ module.exports = class NetworkModule extends BaseModule {
getPeers: {
handler: action => this.network.actions.getPeers(action),
},
getConnectedPeers: {
handler: action => this.network.actions.getConnectedPeers(action),
getPeersCount: {
handler: action => this.network.actions.getPeersCount(action),
},
getPeersCountByFilter: {
handler: action => this.network.actions.getPeersCountByFilter(action),
},
getConnectedPeersCountByFilter: {
getUniqueOutboundConnectedPeersCount: {
diego-G marked this conversation as resolved.
Show resolved Hide resolved
handler: action =>
this.network.actions.getConnectedPeersCountByFilter(action),
this.network.actions.getUniqueOutboundConnectedPeersCount(action),
},
};
}
Expand Down
60 changes: 0 additions & 60 deletions framework/src/modules/network/lookup_peers_ips.js

This file was deleted.

36 changes: 14 additions & 22 deletions framework/src/modules/network/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,9 @@ const {
EVENT_UNBAN_PEER,
} = require('@liskhq/lisk-p2p');
const randomstring = require('randomstring');
const lookupPeersIPs = require('./lookup_peers_ips');
const { createLoggerComponent } = require('../../components/logger');
const { createStorageComponent } = require('../../components/storage');
const {
getByFilter,
getCountByFilter,
getConsolidatedPeersList,
} = require('./filter_peers');
const { filterByParams, consolidatePeers, lookupPeersIPs } = require('./utils');
const { Peer } = require('./components/storage/entities');

const hasNamespaceReg = /:/;
Expand Down Expand Up @@ -344,34 +339,31 @@ module.exports = class Network {
action.params.peerId,
),
getPeers: action => {
const peerList = getConsolidatedPeersList({
const peers = consolidatePeers({
connectedPeers: this.p2p.getConnectedPeers(),
disconnectedPeers: this.p2p.getDisconnectedPeers(),
});

return getByFilter(peerList, action.params);
return filterByParams(peers, action.params);
},
getConnectedPeers: action => {
const peerList = getConsolidatedPeersList({
connectedPeers: this.p2p.getConnectedPeers(),
});

return getByFilter(peerList, action.params);
},
getPeersCountByFilter: action => {
const peerList = getConsolidatedPeersList({
getPeersCount: action => {
const peers = consolidatePeers({
connectedPeers: this.p2p.getConnectedPeers(),
disconnectedPeers: this.p2p.getDisconnectedPeers(),
});

return getCountByFilter(peerList, action.params);
const { limit, offset, ...filterWithoutLimitOffset } = action.params;

return filterByParams(peers, filterWithoutLimitOffset).length;
},
getConnectedPeersCountByFilter: action => {
const peerList = getConsolidatedPeersList({
connectedPeers: this.p2p.getUniqueConnectedPeers(),
getUniqueOutboundConnectedPeersCount: action => {
const peers = consolidatePeers({
connectedPeers: this.p2p.getUniqueOutboundConnectedPeers(),
});

return getCountByFilter(peerList, action.params);
const { limit, offset, ...filterWithoutLimitOffset } = action.params;

return filterByParams(peers, filterWithoutLimitOffset).length;
},
applyPenalty: action =>
this.p2p.applyPenalty(action.params.peerId, action.params.penalty),
Expand Down
Loading