Search in sources :

Example 1 with PeerAddress

use of tech.pegasys.teku.networking.p2p.network.PeerAddress 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 PeerAddress

use of tech.pegasys.teku.networking.p2p.network.PeerAddress 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 PeerAddress

use of tech.pegasys.teku.networking.p2p.network.PeerAddress 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 PeerAddress

use of tech.pegasys.teku.networking.p2p.network.PeerAddress in project teku by ConsenSys.

the class ConnectionManagerTest method setUp.

@BeforeEach
public void setUp() {
    when(discoveryService.searchForPeers()).thenReturn(new SafeFuture<>());
    when(peerSelectionStrategy.selectPeersToConnect(eq(network), any(), any())).thenAnswer(invocation -> {
        final Supplier<? extends Collection<DiscoveryPeer>> candidateSupplier = invocation.getArgument(2);
        return candidateSupplier.get().stream().map(peer -> new PeerAddress(new MockNodeId(peer.getPublicKey()))).collect(toList());
    });
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) PeerSelectionStrategy(tech.pegasys.teku.networking.p2p.connection.PeerSelectionStrategy) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TimeoutException(java.util.concurrent.TimeoutException) SafeFuture(tech.pegasys.teku.infrastructure.async.SafeFuture) Bytes(org.apache.tuweni.bytes.Bytes) NoOpMetricsSystem(org.hyperledger.besu.metrics.noop.NoOpMetricsSystem) MockNodeId(tech.pegasys.teku.networking.p2p.mock.MockNodeId) Supplier(java.util.function.Supplier) InetAddress(java.net.InetAddress) StubPeer(tech.pegasys.teku.network.p2p.peer.StubPeer) ArgumentCaptor(org.mockito.ArgumentCaptor) PeerConnectedSubscriber(tech.pegasys.teku.networking.p2p.peer.PeerConnectedSubscriber) P2PNetwork(tech.pegasys.teku.networking.p2p.network.P2PNetwork) StubAsyncRunner(tech.pegasys.teku.infrastructure.async.StubAsyncRunner) Spec(tech.pegasys.teku.spec.Spec) EnrForkId(tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.EnrForkId) Collections.emptyList(java.util.Collections.emptyList) Collection(java.util.Collection) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) InetSocketAddress(java.net.InetSocketAddress) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test) Collectors.toList(java.util.stream.Collectors.toList) Mockito.never(org.mockito.Mockito.never) List(java.util.List) Stream(java.util.stream.Stream) TestSpecFactory(tech.pegasys.teku.spec.TestSpecFactory) Optional(java.util.Optional) ConnectionManager(tech.pegasys.teku.networking.p2p.connection.ConnectionManager) SchemaDefinitionsSupplier(tech.pegasys.teku.spec.schemas.SchemaDefinitionsSupplier) PeerAddress(tech.pegasys.teku.networking.p2p.network.PeerAddress) Peer(tech.pegasys.teku.networking.p2p.peer.Peer) Mockito.mock(org.mockito.Mockito.mock) MockNodeId(tech.pegasys.teku.networking.p2p.mock.MockNodeId) PeerAddress(tech.pegasys.teku.networking.p2p.network.PeerAddress) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 5 with PeerAddress

use of tech.pegasys.teku.networking.p2p.network.PeerAddress in project teku by ConsenSys.

the class ReputationManagerTest method shouldAllowConnectionAfterDisconnectAfterTimePasses.

@Test
void shouldAllowConnectionAfterDisconnectAfterTimePasses() {
    reputationManager.reportDisconnection(peerAddress, Optional.empty(), true);
    timeProvider.advanceTimeBySeconds(MORE_THAN_DISALLOW_PERIOD);
    assertThat(reputationManager.isConnectionInitiationAllowed(new PeerAddress(new MockNodeId(1)))).isTrue();
}
Also used : MockNodeId(tech.pegasys.teku.networking.p2p.mock.MockNodeId) PeerAddress(tech.pegasys.teku.networking.p2p.network.PeerAddress) Test(org.junit.jupiter.api.Test)

Aggregations

PeerAddress (tech.pegasys.teku.networking.p2p.network.PeerAddress)9 MockNodeId (tech.pegasys.teku.networking.p2p.mock.MockNodeId)7 Test (org.junit.jupiter.api.Test)6 DiscoveryPeer (tech.pegasys.teku.networking.p2p.discovery.DiscoveryPeer)3 ArrayList (java.util.ArrayList)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections.emptyList (java.util.Collections.emptyList)1 List (java.util.List)1 Optional (java.util.Optional)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 Supplier (java.util.function.Supplier)1 Collectors.toList (java.util.stream.Collectors.toList)1 Stream (java.util.stream.Stream)1 Bytes (org.apache.tuweni.bytes.Bytes)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1