@Test void selectPeersToDisconnect_shouldDisconnectExcessRandomlySelectedPeersWhenOutscored() { final Eth2PeerSelectionStrategy strategy = createStrategy(2, 2, 1); final StubPeer peer1 = new StubPeer(new MockNodeId(1)); final StubPeer peer2 = new StubPeer(new MockNodeId(2)); final StubPeer peer3 = new StubPeer(new MockNodeId(3)); when(network.getPeerCount()).thenReturn(3); when(network.streamPeers()).thenReturn(Stream.of(peer1, peer2, peer3)); peerPools.addPeerToPool(peer1.getId(), PeerPool.RANDOMLY_SELECTED); peerPools.addPeerToPool(peer2.getId(), PeerPool.RANDOMLY_SELECTED); peerScorer.setScore(peer1.getId(), 100); peerScorer.setScore(peer2.getId(), 50); peerScorer.setScore(peer3.getId(), 250); withShuffleOrder(peer2, peer1, peer3); assertThat(strategy.selectPeersToDisconnect(network, peerPools)) .containsExactlyInAnyOrder(peer2); }
@Override public List<Peer> selectPeersToDisconnect( final P2PNetwork<?> network, final PeerPools peerPools) { final Map<PeerPool, List<Peer>> peersBySource = network .streamPeers() .collect(Collectors.groupingBy(peer -> peerPools.getPool(peer.getId()))); final List<Peer> randomlySelectedPeers = peersBySource.getOrDefault(RANDOMLY_SELECTED, new ArrayList<>()); final int randomlySelectedPeerCount = randomlySelectedPeers.size(); final int currentPeerCount = network.getPeerCount(); final int peersToDrop = targetPeerCountRange.getPeersToDrop(currentPeerCount); if (peersToDrop == 0) { return emptyList(); } final PeerScorer peerScorer = peerSubnetSubscriptionsFactory.create(network).createScorer(); final int randomlySelectedPeersToDrop = targetPeerCountRange.getRandomlySelectedPeersToDrop( randomlySelectedPeerCount, currentPeerCount); shuffler.shuffle(randomlySelectedPeers); final List<Peer> randomlySelectedPeersBeingDropped = randomlySelectedPeers.subList( 0, Math.min(randomlySelectedPeersToDrop, randomlySelectedPeers.size())); randomlySelectedPeersBeingDropped.forEach( peer -> peerPools.addPeerToPool(peer.getId(), SCORE_BASED)); return Stream.concat( randomlySelectedPeersBeingDropped.stream(), peersBySource.getOrDefault(SCORE_BASED, emptyList()).stream()) .sorted(Comparator.comparing(peerScorer::scoreExistingPeer)) .limit(peersToDrop) .collect(toList()); }
Eth2PeerSelectionStrategy implements PeerSelectionStrategy { @Override public List<Peer> selectPeersToDisconnect( final P2PNetwork<?> network, final PeerPools peerPools) { final Map<PeerPool, List<Peer>> peersBySource = network .streamPeers() .collect(Collectors.groupingBy(peer -> peerPools.getPool(peer.getId()))); final List<Peer> randomlySelectedPeers = peersBySource.getOrDefault(RANDOMLY_SELECTED, new ArrayList<>()); final int randomlySelectedPeerCount = randomlySelectedPeers.size(); final int currentPeerCount = network.getPeerCount(); final int peersToDrop = targetPeerCountRange.getPeersToDrop(currentPeerCount); if (peersToDrop == 0) { return emptyList(); } final PeerScorer peerScorer = peerSubnetSubscriptionsFactory.create(network).createScorer(); final int randomlySelectedPeersToDrop = targetPeerCountRange.getRandomlySelectedPeersToDrop( randomlySelectedPeerCount, currentPeerCount); shuffler.shuffle(randomlySelectedPeers); final List<Peer> randomlySelectedPeersBeingDropped = randomlySelectedPeers.subList( 0, Math.min(randomlySelectedPeersToDrop, randomlySelectedPeers.size())); randomlySelectedPeersBeingDropped.forEach( peer -> peerPools.addPeerToPool(peer.getId(), SCORE_BASED)); return Stream.concat( randomlySelectedPeersBeingDropped.stream(), peersBySource.getOrDefault(SCORE_BASED, emptyList()).stream()) .sorted(Comparator.comparing(peerScorer::scoreExistingPeer)) .limit(peersToDrop) .collect(toList()); } }
Eth2PeerSelectionStrategy implements PeerSelectionStrategy { @Override public List<Peer> selectPeersToDisconnect( final P2PNetwork<?> network, final PeerPools peerPools) { final Map<PeerPool, List<Peer>> peersBySource = network .streamPeers() .collect(Collectors.groupingBy(peer -> peerPools.getPool(peer.getId()))); final List<Peer> randomlySelectedPeers = peersBySource.getOrDefault(RANDOMLY_SELECTED, new ArrayList<>()); final int randomlySelectedPeerCount = randomlySelectedPeers.size(); final int currentPeerCount = network.getPeerCount(); final int peersToDrop = targetPeerCountRange.getPeersToDrop(currentPeerCount); if (peersToDrop == 0) { return emptyList(); } final PeerScorer peerScorer = peerSubnetSubscriptionsFactory.create(network).createScorer(); final int randomlySelectedPeersToDrop = targetPeerCountRange.getRandomlySelectedPeersToDrop( randomlySelectedPeerCount, currentPeerCount); shuffler.shuffle(randomlySelectedPeers); final List<Peer> randomlySelectedPeersBeingDropped = randomlySelectedPeers.subList( 0, Math.min(randomlySelectedPeersToDrop, randomlySelectedPeers.size())); randomlySelectedPeersBeingDropped.forEach( peer -> peerPools.addPeerToPool(peer.getId(), SCORE_BASED)); return Stream.concat( randomlySelectedPeersBeingDropped.stream(), peersBySource.getOrDefault(SCORE_BASED, emptyList()).stream()) .sorted(Comparator.comparing(peerScorer::scoreExistingPeer)) .limit(peersToDrop) .collect(toList()); } Eth2PeerSelectionStrategy(
      final TargetPeerRange targetPeerCountRange,
      final PeerSubnetSubscriptions.Factory peerSubnetSubscriptionsFactory,
      final ReputationManager reputationManager,
      final Shuffler shuffler); }
Eth2PeerSelectionStrategy implements PeerSelectionStrategy { @Override public List<Peer> selectPeersToDisconnect( final P2PNetwork<?> network, final PeerPools peerPools) { final Map<PeerPool, List<Peer>> peersBySource = network .streamPeers() .collect(Collectors.groupingBy(peer -> peerPools.getPool(peer.getId()))); final List<Peer> randomlySelectedPeers = peersBySource.getOrDefault(RANDOMLY_SELECTED, new ArrayList<>()); final int randomlySelectedPeerCount = randomlySelectedPeers.size(); final int currentPeerCount = network.getPeerCount(); final int peersToDrop = targetPeerCountRange.getPeersToDrop(currentPeerCount); if (peersToDrop == 0) { return emptyList(); } final PeerScorer peerScorer = peerSubnetSubscriptionsFactory.create(network).createScorer(); final int randomlySelectedPeersToDrop = targetPeerCountRange.getRandomlySelectedPeersToDrop( randomlySelectedPeerCount, currentPeerCount); shuffler.shuffle(randomlySelectedPeers); final List<Peer> randomlySelectedPeersBeingDropped = randomlySelectedPeers.subList( 0, Math.min(randomlySelectedPeersToDrop, randomlySelectedPeers.size())); randomlySelectedPeersBeingDropped.forEach( peer -> peerPools.addPeerToPool(peer.getId(), SCORE_BASED)); return Stream.concat( randomlySelectedPeersBeingDropped.stream(), peersBySource.getOrDefault(SCORE_BASED, emptyList()).stream()) .sorted(Comparator.comparing(peerScorer::scoreExistingPeer)) .limit(peersToDrop) .collect(toList()); } Eth2PeerSelectionStrategy(
      final TargetPeerRange targetPeerCountRange,
      final PeerSubnetSubscriptions.Factory peerSubnetSubscriptionsFactory,
      final ReputationManager reputationManager,
      final Shuffler shuffler); @Override List<PeerAddress> selectPeersToConnect(
      final P2PNetwork<?> network,
      final PeerPools peerPools,
      final Supplier<List<DiscoveryPeer>> candidates); @Override List<Peer> selectPeersToDisconnect(
      final P2PNetwork<?> network, final PeerPools peerPools); }
Eth2PeerSelectionStrategy implements PeerSelectionStrategy { @Override public List<Peer> selectPeersToDisconnect( final P2PNetwork<?> network, final PeerPools peerPools) { final Map<PeerPool, List<Peer>> peersBySource = network .streamPeers() .collect(Collectors.groupingBy(peer -> peerPools.getPool(peer.getId()))); final List<Peer> randomlySelectedPeers = peersBySource.getOrDefault(RANDOMLY_SELECTED, new ArrayList<>()); final int randomlySelectedPeerCount = randomlySelectedPeers.size(); final int currentPeerCount = network.getPeerCount(); final int peersToDrop = targetPeerCountRange.getPeersToDrop(currentPeerCount); if (peersToDrop == 0) { return emptyList(); } final PeerScorer peerScorer = peerSubnetSubscriptionsFactory.create(network).createScorer(); final int randomlySelectedPeersToDrop = targetPeerCountRange.getRandomlySelectedPeersToDrop( randomlySelectedPeerCount, currentPeerCount); shuffler.shuffle(randomlySelectedPeers); final List<Peer> randomlySelectedPeersBeingDropped = randomlySelectedPeers.subList( 0, Math.min(randomlySelectedPeersToDrop, randomlySelectedPeers.size())); randomlySelectedPeersBeingDropped.forEach( peer -> peerPools.addPeerToPool(peer.getId(), SCORE_BASED)); return Stream.concat( randomlySelectedPeersBeingDropped.stream(), peersBySource.getOrDefault(SCORE_BASED, emptyList()).stream()) .sorted(Comparator.comparing(peerScorer::scoreExistingPeer)) .limit(peersToDrop) .collect(toList()); } Eth2PeerSelectionStrategy(
      final TargetPeerRange targetPeerCountRange,
      final PeerSubnetSubscriptions.Factory peerSubnetSubscriptionsFactory,
      final ReputationManager reputationManager,
      final Shuffler shuffler); @Override List<PeerAddress> selectPeersToConnect(
      final P2PNetwork<?> network,
      final PeerPools peerPools,
      final Supplier<List<DiscoveryPeer>> candidates); @Override List<Peer> selectPeersToDisconnect(
      final P2PNetwork<?> network, final PeerPools peerPools);  }