Search in sources :

Example 6 with MockNodeId

use of tech.pegasys.teku.networking.p2p.mock.MockNodeId in project teku by ConsenSys.

the class SubnetScorerTest method shouldScoreCandidatePeersOnSubnetsWithFewPeersMoreHighly.

@Test
void shouldScoreCandidatePeersOnSubnetsWithFewPeersMoreHighly() {
    final MockNodeId node1 = new MockNodeId(0);
    final MockNodeId node2 = new MockNodeId(1);
    final MockNodeId node3 = new MockNodeId(2);
    final SubnetScorer scorer = SubnetScorer.create(PeerSubnetSubscriptions.builder(() -> schemaDefinitions).attestationSubnetSubscriptions(b -> b.addRelevantSubnet(1).addRelevantSubnet(2).addRelevantSubnet(3).addRelevantSubnet(5).addSubscriber(1, node2).addSubscriber(3, node3)).syncCommitteeSubnetSubscriptions(b -> b.addRelevantSubnet(1).addRelevantSubnet(2).addSubscriber(1, node1).addSubscriber(1, node2).addSubscriber(1, node3)).build());
    assertCandidatePeerScores(scorer, entry(candidateWithSubnets(List.of(1, 3), List.of(1)), 562), entry(candidateWithSubnets(List.of(1), List.of(1)), 312), entry(candidateWithSubnets(List.of(2), List.of(1)), 1062), entry(candidateWithSubnets(List.of(3), List.of(1)), 312), entry(candidateWithSubnets(emptyList(), emptyList()), 0), entry(candidateWithSubnets(List.of(5), emptyList()), 1000), entry(candidateWithSubnets(List.of(4), emptyList()), 0), entry(candidateWithSubnets(emptyList(), List.of(2)), 1000), entry(candidateWithSubnets(emptyList(), List.of(3)), 0));
}
Also used : MockNodeId(tech.pegasys.teku.networking.p2p.mock.MockNodeId) PeerScorer(tech.pegasys.teku.networking.eth2.peers.PeerScorer) Collections.emptyList(java.util.Collections.emptyList) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SchemaDefinitions(tech.pegasys.teku.spec.schemas.SchemaDefinitions) MockNodeId(tech.pegasys.teku.networking.p2p.mock.MockNodeId) Assertions.entry(org.assertj.core.api.Assertions.entry) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) List(java.util.List) SszBitvector(tech.pegasys.teku.infrastructure.ssz.collections.SszBitvector) Stream(java.util.stream.Stream) TestSpecFactory(tech.pegasys.teku.spec.TestSpecFactory) Pair(org.apache.commons.lang3.tuple.Pair) Map(java.util.Map) Spec(tech.pegasys.teku.spec.Spec) NodeId(tech.pegasys.teku.networking.p2p.peer.NodeId) Test(org.junit.jupiter.api.Test)

Example 7 with MockNodeId

use of tech.pegasys.teku.networking.p2p.mock.MockNodeId in project teku by ConsenSys.

the class PeerManagerTest method subscribeConnect_shouldRejectConnectionThatAlreadyExists.

@Test
public void subscribeConnect_shouldRejectConnectionThatAlreadyExists() {
    final List<Peer> connectedPeers = new ArrayList<>();
    peerManager.subscribeConnect(connectedPeers::add);
    final Peer peer = mock(Peer.class);
    when(peer.getId()).thenReturn(new MockNodeId(1));
    peerManager.onConnectedPeer(peer);
    assertThat(connectedPeers).containsExactly(peer);
    Assertions.assertThrows(PeerAlreadyConnectedException.class, () -> peerManager.onConnectedPeer(peer));
    assertThat(connectedPeers).containsExactly(peer);
}
Also used : MockNodeId(tech.pegasys.teku.networking.p2p.mock.MockNodeId) Peer(tech.pegasys.teku.networking.p2p.peer.Peer) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Example 8 with MockNodeId

use of tech.pegasys.teku.networking.p2p.mock.MockNodeId in project teku by ConsenSys.

the class PeerManagerTest method shouldReportFailedConnectionsToReputationManager.

@Test
public void shouldReportFailedConnectionsToReputationManager() {
    final Multiaddr multiaddr = Multiaddr.fromString("/ip4/127.0.0.1/tcp/9000");
    final MultiaddrPeerAddress peerAddress = new MultiaddrPeerAddress(new MockNodeId(1), multiaddr);
    when(network.connect(multiaddr)).thenReturn(CompletableFuture.failedFuture(new RuntimeException("Nope")));
    final SafeFuture<Peer> result = peerManager.connect(peerAddress, network);
    assertThat(result).isCompletedExceptionally();
    verify(reputationManager).reportInitiatedConnectionFailed(peerAddress);
    verify(reputationManager, never()).reportInitiatedConnectionSuccessful(peerAddress);
}
Also used : MockNodeId(tech.pegasys.teku.networking.p2p.mock.MockNodeId) Peer(tech.pegasys.teku.networking.p2p.peer.Peer) Multiaddr(io.libp2p.core.multiformats.Multiaddr) Test(org.junit.jupiter.api.Test)

Example 9 with MockNodeId

use of tech.pegasys.teku.networking.p2p.mock.MockNodeId in project teku by ConsenSys.

the class PeerPoolsTest method shouldReturnToDefaultPoolWhenPeerForgotten.

@Test
void shouldReturnToDefaultPoolWhenPeerForgotten() {
    final MockNodeId nodeId = new MockNodeId(1);
    pools.addPeerToPool(nodeId, PeerPool.RANDOMLY_SELECTED);
    pools.forgetPeer(nodeId);
    assertThat(pools.getPool(nodeId)).isEqualTo(PeerPool.SCORE_BASED);
}
Also used : MockNodeId(tech.pegasys.teku.networking.p2p.mock.MockNodeId) Test(org.junit.jupiter.api.Test)

Example 10 with MockNodeId

use of tech.pegasys.teku.networking.p2p.mock.MockNodeId in project teku by ConsenSys.

the class ConnectionManagerTest method shouldConnectPeersThatPassPeerFilter.

@Test
public void shouldConnectPeersThatPassPeerFilter() {
    final ConnectionManager manager = createManager();
    final StubPeer peer1 = new StubPeer(new MockNodeId(1));
    final StubPeer peer2 = new StubPeer(new MockNodeId(2));
    when(network.connect(PEER1)).thenReturn(SafeFuture.completedFuture(peer1));
    when(network.connect(PEER2)).thenReturn(SafeFuture.completedFuture(peer2));
    when(discoveryService.streamKnownPeers()).thenReturn(Stream.of(DISCOVERY_PEER1, DISCOVERY_PEER2));
    manager.start().join();
    verify(network).connect(PEER1);
    verify(network).connect(PEER2);
}
Also used : MockNodeId(tech.pegasys.teku.networking.p2p.mock.MockNodeId) ConnectionManager(tech.pegasys.teku.networking.p2p.connection.ConnectionManager) StubPeer(tech.pegasys.teku.network.p2p.peer.StubPeer) Test(org.junit.jupiter.api.Test)

Aggregations

MockNodeId (tech.pegasys.teku.networking.p2p.mock.MockNodeId)36 Test (org.junit.jupiter.api.Test)32 StubPeer (tech.pegasys.teku.network.p2p.peer.StubPeer)15 ConnectionManager (tech.pegasys.teku.networking.p2p.connection.ConnectionManager)10 Peer (tech.pegasys.teku.networking.p2p.peer.Peer)10 PeerAddress (tech.pegasys.teku.networking.p2p.network.PeerAddress)8 SafeFuture (tech.pegasys.teku.infrastructure.async.SafeFuture)4 ArrayList (java.util.ArrayList)3 Collections.emptyList (java.util.Collections.emptyList)3 List (java.util.List)3 Stream (java.util.stream.Stream)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3 Spec (tech.pegasys.teku.spec.Spec)3 TestSpecFactory (tech.pegasys.teku.spec.TestSpecFactory)3 Multiaddr (io.libp2p.core.multiformats.Multiaddr)2 Map (java.util.Map)2 Function (java.util.function.Function)2 Collectors (java.util.stream.Collectors)2 Pair (org.apache.commons.lang3.tuple.Pair)2