Search in sources :

Example 1 with Peer

use of tech.pegasys.teku.networking.p2p.peer.Peer in project teku by ConsenSys.

the class Eth2P2PNetworkBuilder method buildNetwork.

protected DiscoveryNetwork<?> buildNetwork(final GossipEncoding gossipEncoding, final SubnetSubscriptionService syncCommitteeSubnetService) {
    final ReputationManager reputationManager = new ReputationManager(metricsSystem, timeProvider, Constants.REPUTATION_MANAGER_CAPACITY);
    PreparedGossipMessageFactory defaultMessageFactory = gossipEncoding.createPreparedGossipMessageFactory(recentChainData::getMilestoneByForkDigest);
    final GossipTopicFilter gossipTopicsFilter = new Eth2GossipTopicFilter(recentChainData, gossipEncoding, spec);
    final NetworkConfig networkConfig = config.getNetworkConfig();
    final DiscoveryConfig discoConfig = config.getDiscoveryConfig();
    final P2PNetwork<Peer> p2pNetwork = createLibP2PNetworkBuilder().asyncRunner(asyncRunner).metricsSystem(metricsSystem).config(networkConfig).privateKeyProvider(new LibP2PPrivateKeyLoader(keyValueStore, networkConfig.getPrivateKeySource())).reputationManager(reputationManager).rpcMethods(rpcMethods).peerHandlers(peerHandlers).preparedGossipMessageFactory(defaultMessageFactory).gossipTopicFilter(gossipTopicsFilter).build();
    final AttestationSubnetTopicProvider attestationSubnetTopicProvider = new AttestationSubnetTopicProvider(recentChainData, gossipEncoding);
    final SyncCommitteeSubnetTopicProvider syncCommitteeSubnetTopicProvider = new SyncCommitteeSubnetTopicProvider(recentChainData, gossipEncoding);
    final TargetPeerRange targetPeerRange = new TargetPeerRange(discoConfig.getMinPeers(), discoConfig.getMaxPeers(), discoConfig.getMinRandomlySelectedPeers());
    final SchemaDefinitionsSupplier currentSchemaDefinitions = () -> recentChainData.getCurrentSpec().getSchemaDefinitions();
    return createDiscoveryNetworkBuilder().metricsSystem(metricsSystem).asyncRunner(asyncRunner).kvStore(keyValueStore).p2pNetwork(p2pNetwork).peerSelectionStrategy(new Eth2PeerSelectionStrategy(targetPeerRange, network -> PeerSubnetSubscriptions.create(currentSchemaDefinitions, network, attestationSubnetTopicProvider, syncCommitteeSubnetTopicProvider, syncCommitteeSubnetService, config.getTargetSubnetSubscriberCount()), reputationManager, Collections::shuffle)).discoveryConfig(discoConfig).p2pConfig(networkConfig).spec(config.getSpec()).currentSchemaDefinitionsSupplier(currentSchemaDefinitions).build();
}
Also used : PreparedGossipMessageFactory(tech.pegasys.teku.networking.p2p.gossip.PreparedGossipMessageFactory) Eth2PeerSelectionStrategy(tech.pegasys.teku.networking.eth2.peers.Eth2PeerSelectionStrategy) Peer(tech.pegasys.teku.networking.p2p.peer.Peer) NetworkConfig(tech.pegasys.teku.networking.p2p.network.config.NetworkConfig) AttestationSubnetTopicProvider(tech.pegasys.teku.networking.eth2.gossip.subnets.AttestationSubnetTopicProvider) TargetPeerRange(tech.pegasys.teku.networking.p2p.connection.TargetPeerRange) LibP2PPrivateKeyLoader(tech.pegasys.teku.networking.p2p.libp2p.LibP2PPrivateKeyLoader) Eth2GossipTopicFilter(tech.pegasys.teku.networking.eth2.gossip.topics.Eth2GossipTopicFilter) GossipTopicFilter(tech.pegasys.teku.networking.p2p.libp2p.gossip.GossipTopicFilter) Eth2GossipTopicFilter(tech.pegasys.teku.networking.eth2.gossip.topics.Eth2GossipTopicFilter) SchemaDefinitionsSupplier(tech.pegasys.teku.spec.schemas.SchemaDefinitionsSupplier) SyncCommitteeSubnetTopicProvider(tech.pegasys.teku.networking.eth2.gossip.subnets.SyncCommitteeSubnetTopicProvider) ReputationManager(tech.pegasys.teku.networking.p2p.reputation.ReputationManager) DiscoveryConfig(tech.pegasys.teku.networking.p2p.discovery.DiscoveryConfig) Collections(java.util.Collections)

Example 2 with Peer

use of tech.pegasys.teku.networking.p2p.peer.Peer in project teku by ConsenSys.

the class Eth2PeerManagerTest method onConnect_shouldDisconnectIfPeerReturnsErrorResponseToStatusMessage.

@Test
void onConnect_shouldDisconnectIfPeerReturnsErrorResponseToStatusMessage() {
    final Peer peer = createPeer(1);
    final Eth2Peer eth2Peer = getEth2Peer(peer);
    when(peer.connectionInitiatedLocally()).thenReturn(true);
    when(eth2Peer.sendStatus()).thenReturn(SafeFuture.failedFuture(new RpcException(RpcResponseStatus.SERVER_ERROR_CODE, "It went boom")));
    peerManager.onConnect(peer);
    verify(eth2Peer).disconnectImmediately(Optional.of(DisconnectReason.REMOTE_FAULT), true);
}
Also used : Peer(tech.pegasys.teku.networking.p2p.peer.Peer) RpcException(tech.pegasys.teku.networking.eth2.rpc.core.RpcException) Test(org.junit.jupiter.api.Test)

Example 3 with Peer

use of tech.pegasys.teku.networking.p2p.peer.Peer in project teku by ConsenSys.

the class Eth2PeerManagerTest method createPeer.

private Peer createPeer(final int id) {
    final Peer peer = mock(Peer.class);
    final Eth2Peer eth2Peer = createEth2Peer(peer);
    eth2Peers.put(peer, eth2Peer);
    when(peer.getId()).thenReturn(new MockNodeId(id));
    when(eth2PeerFactory.create(same(peer), any())).thenReturn(eth2Peer);
    return peer;
}
Also used : MockNodeId(tech.pegasys.teku.networking.p2p.mock.MockNodeId) Peer(tech.pegasys.teku.networking.p2p.peer.Peer)

Example 4 with Peer

use of tech.pegasys.teku.networking.p2p.peer.Peer in project teku by ConsenSys.

the class Eth2PeerManagerTest method onConnect_shouldDisconnectIfStatusMessageFailsToSend.

@Test
void onConnect_shouldDisconnectIfStatusMessageFailsToSend() {
    final Peer peer = createPeer(1);
    final Eth2Peer eth2Peer = getEth2Peer(peer);
    when(peer.connectionInitiatedLocally()).thenReturn(true);
    when(eth2Peer.sendStatus()).thenReturn(SafeFuture.failedFuture(new IOException("Failed to send")));
    peerManager.onConnect(peer);
    verify(eth2Peer).disconnectImmediately(Optional.empty(), true);
}
Also used : Peer(tech.pegasys.teku.networking.p2p.peer.Peer) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Example 5 with Peer

use of tech.pegasys.teku.networking.p2p.peer.Peer in project teku by ConsenSys.

the class Eth2PeerManagerTest method onConnect_shouldNotDisconnectIncomingPeerWhenStatusMessageSent.

@Test
void onConnect_shouldNotDisconnectIncomingPeerWhenStatusMessageSent() {
    final Peer peer = createPeer(1);
    final Eth2Peer eth2Peer = getEth2Peer(peer);
    when(eth2Peer.hasStatus()).thenReturn(true);
    when(peer.connectionInitiatedLocally()).thenReturn(false);
    peerManager.onConnect(peer);
    assertThat(asyncRunner.hasDelayedActions()).isTrue();
    // We receive peer status before the timeout
    setInitialPeerStatus(peer);
    asyncRunner.executeQueuedActions();
    // So don't disconnect
    verify(eth2Peer, never()).disconnectCleanly(DisconnectReason.REMOTE_FAULT);
}
Also used : Peer(tech.pegasys.teku.networking.p2p.peer.Peer) Test(org.junit.jupiter.api.Test)

Aggregations

Peer (tech.pegasys.teku.networking.p2p.peer.Peer)22 Test (org.junit.jupiter.api.Test)19 MockNodeId (tech.pegasys.teku.networking.p2p.mock.MockNodeId)10 ArrayList (java.util.ArrayList)6 SafeFuture (tech.pegasys.teku.infrastructure.async.SafeFuture)6 StubPeer (tech.pegasys.teku.network.p2p.peer.StubPeer)6 ConnectionManager (tech.pegasys.teku.networking.p2p.connection.ConnectionManager)6 PeerAddress (tech.pegasys.teku.networking.p2p.network.PeerAddress)3 Multiaddr (io.libp2p.core.multiformats.Multiaddr)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 P2PNetwork (tech.pegasys.teku.networking.p2p.network.P2PNetwork)2 SchemaDefinitionsSupplier (tech.pegasys.teku.spec.schemas.SchemaDefinitionsSupplier)2 Connection (io.libp2p.core.Connection)1 Session (io.libp2p.core.security.SecureChannel.Session)1 IOException (java.io.IOException)1 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 Path (java.nio.file.Path)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1