Search in sources :

Example 1 with PeerSubnetSubscriptions

use of tech.pegasys.teku.networking.eth2.gossip.subnets.PeerSubnetSubscriptions in project teku by ConsenSys.

the class Eth2PeerSelectionStrategy method selectPeersToConnect.

@Override
public List<PeerAddress> selectPeersToConnect(final P2PNetwork<?> network, final PeerPools peerPools, final Supplier<? extends Collection<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));
    }
    // Unmodifiable to make errorprone happy
    return unmodifiableList(selectedPeers);
}
Also used : DiscoveryPeer(tech.pegasys.teku.networking.p2p.discovery.DiscoveryPeer) PeerAddress(tech.pegasys.teku.networking.p2p.network.PeerAddress) PeerSubnetSubscriptions(tech.pegasys.teku.networking.eth2.gossip.subnets.PeerSubnetSubscriptions) ArrayList(java.util.ArrayList)

Aggregations

ArrayList (java.util.ArrayList)1 PeerSubnetSubscriptions (tech.pegasys.teku.networking.eth2.gossip.subnets.PeerSubnetSubscriptions)1 DiscoveryPeer (tech.pegasys.teku.networking.p2p.discovery.DiscoveryPeer)1 PeerAddress (tech.pegasys.teku.networking.p2p.network.PeerAddress)1