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();
}
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);
}
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;
}
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);
}
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);
}
Aggregations