Search in sources :

Example 1 with DiscoveryPeer

use of tech.pegasys.teku.networking.p2p.discovery.DiscoveryPeer in project teku by ConsenSys.

the class Eth2PeerSelectionStrategy method selectAndRemoveRandomPeers.

private List<PeerAddress> selectAndRemoveRandomPeers(final P2PNetwork<?> network, final PeerPools peerPools, final int randomlySelectedPeersToAdd, final List<DiscoveryPeer> allCandidatePeers) {
    final List<PeerAddress> selectedPeers = new ArrayList<>();
    shuffler.shuffle(allCandidatePeers);
    while (!allCandidatePeers.isEmpty() && selectedPeers.size() < randomlySelectedPeersToAdd) {
        final DiscoveryPeer candidate = allCandidatePeers.remove(0);
        checkCandidate(candidate, network).ifPresent(peerAddress -> {
            peerPools.addPeerToPool(peerAddress.getId(), RANDOMLY_SELECTED);
            selectedPeers.add(peerAddress);
        });
    }
    return selectedPeers;
}
Also used : DiscoveryPeer(tech.pegasys.teku.networking.p2p.discovery.DiscoveryPeer) PeerAddress(tech.pegasys.teku.networking.p2p.network.PeerAddress) ArrayList(java.util.ArrayList)

Example 2 with DiscoveryPeer

use of tech.pegasys.teku.networking.p2p.discovery.DiscoveryPeer 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)

Example 3 with DiscoveryPeer

use of tech.pegasys.teku.networking.p2p.discovery.DiscoveryPeer in project teku by ConsenSys.

the class Eth2PeerSelectionStrategyTest method setUp.

@BeforeEach
void setUp() {
    when(peerSubnetSubscriptions.createScorer()).thenReturn(peerScorer);
    when(reputationManager.isConnectionInitiationAllowed(any())).thenReturn(true);
    when(network.createPeerAddress(any(DiscoveryPeer.class))).thenAnswer(invocation -> {
        final DiscoveryPeer peer = invocation.getArgument(0);
        return new PeerAddress(new MockNodeId(peer.getPublicKey()));
    });
}
Also used : MockNodeId(tech.pegasys.teku.networking.p2p.mock.MockNodeId) DiscoveryPeer(tech.pegasys.teku.networking.p2p.discovery.DiscoveryPeer) PeerAddress(tech.pegasys.teku.networking.p2p.network.PeerAddress) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 4 with DiscoveryPeer

use of tech.pegasys.teku.networking.p2p.discovery.DiscoveryPeer in project teku by ConsenSys.

the class NodeRecordConverterTest method shouldConvertAttnets.

@Test
public void shouldConvertAttnets() {
    SszBitvector persistentSubnets = ATT_SUBNET_SCHEMA.ofBits(1, 8, 14, 32);
    Bytes encodedPersistentSubnets = persistentSubnets.sszSerialize();
    final Optional<DiscoveryPeer> result = convertNodeRecordWithFields(new EnrField(EnrField.IP_V6, IPV6_LOCALHOST), new EnrField(EnrField.TCP_V6, 1234), new EnrField(ATTESTATION_SUBNET_ENR_FIELD, encodedPersistentSubnets));
    assertThat(result).contains(new DiscoveryPeer(PUB_KEY, new InetSocketAddress("::1", 1234), ENR_FORK_ID, persistentSubnets, SYNCNETS));
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) DiscoveryPeer(tech.pegasys.teku.networking.p2p.discovery.DiscoveryPeer) EnrField(org.ethereum.beacon.discovery.schema.EnrField) InetSocketAddress(java.net.InetSocketAddress) SszBitvector(tech.pegasys.teku.infrastructure.ssz.collections.SszBitvector) Test(org.junit.jupiter.api.Test)

Example 5 with DiscoveryPeer

use of tech.pegasys.teku.networking.p2p.discovery.DiscoveryPeer in project teku by ConsenSys.

the class NodeRecordConverterTest method shouldConvertIpV6Record.

@Test
public void shouldConvertIpV6Record() {
    final Optional<DiscoveryPeer> result = convertNodeRecordWithFields(new EnrField(EnrField.IP_V6, IPV6_LOCALHOST), new EnrField(EnrField.TCP_V6, 1234));
    assertThat(result).contains(new DiscoveryPeer(PUB_KEY, new InetSocketAddress("::1", 1234), ENR_FORK_ID, ATTNETS, SYNCNETS));
}
Also used : DiscoveryPeer(tech.pegasys.teku.networking.p2p.discovery.DiscoveryPeer) EnrField(org.ethereum.beacon.discovery.schema.EnrField) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.jupiter.api.Test)

Aggregations

DiscoveryPeer (tech.pegasys.teku.networking.p2p.discovery.DiscoveryPeer)20 Test (org.junit.jupiter.api.Test)14 InetSocketAddress (java.net.InetSocketAddress)13 EnrField (org.ethereum.beacon.discovery.schema.EnrField)9 Bytes (org.apache.tuweni.bytes.Bytes)8 SszBitvector (tech.pegasys.teku.infrastructure.ssz.collections.SszBitvector)5 Multiaddr (io.libp2p.core.multiformats.Multiaddr)4 NodeRecord (org.ethereum.beacon.discovery.schema.NodeRecord)3 PeerAddress (tech.pegasys.teku.networking.p2p.network.PeerAddress)3 ArrayList (java.util.ArrayList)2 EnrForkId (tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.EnrForkId)2 Duration (java.time.Duration)1 Collection (java.util.Collection)1 Collections.emptyList (java.util.Collections.emptyList)1 List (java.util.List)1 Optional (java.util.Optional)1 Collectors.toList (java.util.stream.Collectors.toList)1 Stream (java.util.stream.Stream)1 LogManager (org.apache.logging.log4j.LogManager)1 Logger (org.apache.logging.log4j.Logger)1