@Test void selectPeersToConnect_shouldConnectToHighestScoringPeers() { final Eth2PeerSelectionStrategy strategy = createStrategy(2, 2, 0); final DiscoveryPeer discoveryPeer1 = createDiscoveryPeer(PEER1, 1); final DiscoveryPeer discoveryPeer2 = createDiscoveryPeer(PEER2, 2); final DiscoveryPeer discoveryPeer3 = createDiscoveryPeer(PEER3, 3); final DiscoveryPeer discoveryPeer4 = createDiscoveryPeer(PEER4, 4); peerScorer.setScore(discoveryPeer1.getPersistentSubnets(), 100); peerScorer.setScore(discoveryPeer2.getPersistentSubnets(), 200); peerScorer.setScore(discoveryPeer3.getPersistentSubnets(), 150); peerScorer.setScore(discoveryPeer4.getPersistentSubnets(), 500); assertThat( strategy.selectPeersToConnect( network, peerPools, () -> List.of(discoveryPeer1, discoveryPeer2, discoveryPeer3, discoveryPeer4))) .containsExactlyInAnyOrder(PEER2, PEER4); }
@Override public List<PeerAddress> selectPeersToConnect( final P2PNetwork<?> network, final PeerPools peerPools, final Supplier<List<DiscoveryPeer>> candidates) { final PeerSubnetSubscriptions peerSubnetSubscriptions = peerSubnetSubscriptionsFactory.create(network); final int peersRequiredForPeerCount = targetPeerCountRange.getPeersToAdd(network.getPeerCount()); final int randomlySelectedPeerCount = getCurrentRandomlySelectedPeerCount(network, peerPools); final int randomlySelectedPeersToAdd = targetPeerCountRange.getRandomlySelectedPeersToAdd(randomlySelectedPeerCount); final int peersRequiredForSubnets = peerSubnetSubscriptions.getSubscribersRequired(); final int scoreBasedPeersToAdd = Math.max(peersRequiredForPeerCount - randomlySelectedPeerCount, peersRequiredForSubnets); final int maxPeersToAdd = scoreBasedPeersToAdd + randomlySelectedPeersToAdd; LOG.trace("Connecting to up to {} known peers", maxPeersToAdd); if (maxPeersToAdd == 0) { return emptyList(); } final List<DiscoveryPeer> allCandidatePeers = new ArrayList<>(candidates.get()); final List<PeerAddress> selectedPeers = new ArrayList<>(); if (randomlySelectedPeersToAdd > 0) { selectedPeers.addAll( selectAndRemoveRandomPeers( network, peerPools, randomlySelectedPeersToAdd, allCandidatePeers)); } if (scoreBasedPeersToAdd > 0) { selectedPeers.addAll( selectPeersByScore( network, peerSubnetSubscriptions, scoreBasedPeersToAdd, allCandidatePeers)); } return unmodifiableList(selectedPeers); }
Eth2PeerSelectionStrategy implements PeerSelectionStrategy { @Override public List<PeerAddress> selectPeersToConnect( final P2PNetwork<?> network, final PeerPools peerPools, final Supplier<List<DiscoveryPeer>> candidates) { final PeerSubnetSubscriptions peerSubnetSubscriptions = peerSubnetSubscriptionsFactory.create(network); final int peersRequiredForPeerCount = targetPeerCountRange.getPeersToAdd(network.getPeerCount()); final int randomlySelectedPeerCount = getCurrentRandomlySelectedPeerCount(network, peerPools); final int randomlySelectedPeersToAdd = targetPeerCountRange.getRandomlySelectedPeersToAdd(randomlySelectedPeerCount); final int peersRequiredForSubnets = peerSubnetSubscriptions.getSubscribersRequired(); final int scoreBasedPeersToAdd = Math.max(peersRequiredForPeerCount - randomlySelectedPeerCount, peersRequiredForSubnets); final int maxPeersToAdd = scoreBasedPeersToAdd + randomlySelectedPeersToAdd; LOG.trace("Connecting to up to {} known peers", maxPeersToAdd); if (maxPeersToAdd == 0) { return emptyList(); } final List<DiscoveryPeer> allCandidatePeers = new ArrayList<>(candidates.get()); final List<PeerAddress> selectedPeers = new ArrayList<>(); if (randomlySelectedPeersToAdd > 0) { selectedPeers.addAll( selectAndRemoveRandomPeers( network, peerPools, randomlySelectedPeersToAdd, allCandidatePeers)); } if (scoreBasedPeersToAdd > 0) { selectedPeers.addAll( selectPeersByScore( network, peerSubnetSubscriptions, scoreBasedPeersToAdd, allCandidatePeers)); } return unmodifiableList(selectedPeers); } }
Eth2PeerSelectionStrategy implements PeerSelectionStrategy { @Override public List<PeerAddress> selectPeersToConnect( final P2PNetwork<?> network, final PeerPools peerPools, final Supplier<List<DiscoveryPeer>> candidates) { final PeerSubnetSubscriptions peerSubnetSubscriptions = peerSubnetSubscriptionsFactory.create(network); final int peersRequiredForPeerCount = targetPeerCountRange.getPeersToAdd(network.getPeerCount()); final int randomlySelectedPeerCount = getCurrentRandomlySelectedPeerCount(network, peerPools); final int randomlySelectedPeersToAdd = targetPeerCountRange.getRandomlySelectedPeersToAdd(randomlySelectedPeerCount); final int peersRequiredForSubnets = peerSubnetSubscriptions.getSubscribersRequired(); final int scoreBasedPeersToAdd = Math.max(peersRequiredForPeerCount - randomlySelectedPeerCount, peersRequiredForSubnets); final int maxPeersToAdd = scoreBasedPeersToAdd + randomlySelectedPeersToAdd; LOG.trace("Connecting to up to {} known peers", maxPeersToAdd); if (maxPeersToAdd == 0) { return emptyList(); } final List<DiscoveryPeer> allCandidatePeers = new ArrayList<>(candidates.get()); final List<PeerAddress> selectedPeers = new ArrayList<>(); if (randomlySelectedPeersToAdd > 0) { selectedPeers.addAll( selectAndRemoveRandomPeers( network, peerPools, randomlySelectedPeersToAdd, allCandidatePeers)); } if (scoreBasedPeersToAdd > 0) { selectedPeers.addAll( selectPeersByScore( network, peerSubnetSubscriptions, scoreBasedPeersToAdd, allCandidatePeers)); } return unmodifiableList(selectedPeers); } Eth2PeerSelectionStrategy(
      final TargetPeerRange targetPeerCountRange,
      final PeerSubnetSubscriptions.Factory peerSubnetSubscriptionsFactory,
      final ReputationManager reputationManager,
      final Shuffler shuffler); }
Eth2PeerSelectionStrategy implements PeerSelectionStrategy { @Override public List<PeerAddress> selectPeersToConnect( final P2PNetwork<?> network, final PeerPools peerPools, final Supplier<List<DiscoveryPeer>> candidates) { final PeerSubnetSubscriptions peerSubnetSubscriptions = peerSubnetSubscriptionsFactory.create(network); final int peersRequiredForPeerCount = targetPeerCountRange.getPeersToAdd(network.getPeerCount()); final int randomlySelectedPeerCount = getCurrentRandomlySelectedPeerCount(network, peerPools); final int randomlySelectedPeersToAdd = targetPeerCountRange.getRandomlySelectedPeersToAdd(randomlySelectedPeerCount); final int peersRequiredForSubnets = peerSubnetSubscriptions.getSubscribersRequired(); final int scoreBasedPeersToAdd = Math.max(peersRequiredForPeerCount - randomlySelectedPeerCount, peersRequiredForSubnets); final int maxPeersToAdd = scoreBasedPeersToAdd + randomlySelectedPeersToAdd; LOG.trace("Connecting to up to {} known peers", maxPeersToAdd); if (maxPeersToAdd == 0) { return emptyList(); } final List<DiscoveryPeer> allCandidatePeers = new ArrayList<>(candidates.get()); final List<PeerAddress> selectedPeers = new ArrayList<>(); if (randomlySelectedPeersToAdd > 0) { selectedPeers.addAll( selectAndRemoveRandomPeers( network, peerPools, randomlySelectedPeersToAdd, allCandidatePeers)); } if (scoreBasedPeersToAdd > 0) { selectedPeers.addAll( selectPeersByScore( network, peerSubnetSubscriptions, scoreBasedPeersToAdd, allCandidatePeers)); } return unmodifiableList(selectedPeers); } 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<PeerAddress> selectPeersToConnect( final P2PNetwork<?> network, final PeerPools peerPools, final Supplier<List<DiscoveryPeer>> candidates) { final PeerSubnetSubscriptions peerSubnetSubscriptions = peerSubnetSubscriptionsFactory.create(network); final int peersRequiredForPeerCount = targetPeerCountRange.getPeersToAdd(network.getPeerCount()); final int randomlySelectedPeerCount = getCurrentRandomlySelectedPeerCount(network, peerPools); final int randomlySelectedPeersToAdd = targetPeerCountRange.getRandomlySelectedPeersToAdd(randomlySelectedPeerCount); final int peersRequiredForSubnets = peerSubnetSubscriptions.getSubscribersRequired(); final int scoreBasedPeersToAdd = Math.max(peersRequiredForPeerCount - randomlySelectedPeerCount, peersRequiredForSubnets); final int maxPeersToAdd = scoreBasedPeersToAdd + randomlySelectedPeersToAdd; LOG.trace("Connecting to up to {} known peers", maxPeersToAdd); if (maxPeersToAdd == 0) { return emptyList(); } final List<DiscoveryPeer> allCandidatePeers = new ArrayList<>(candidates.get()); final List<PeerAddress> selectedPeers = new ArrayList<>(); if (randomlySelectedPeersToAdd > 0) { selectedPeers.addAll( selectAndRemoveRandomPeers( network, peerPools, randomlySelectedPeersToAdd, allCandidatePeers)); } if (scoreBasedPeersToAdd > 0) { selectedPeers.addAll( selectPeersByScore( network, peerSubnetSubscriptions, scoreBasedPeersToAdd, allCandidatePeers)); } return unmodifiableList(selectedPeers); } 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);  }