Search in sources :

Example 1 with MockNodeId

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

the class FetchBlockTaskTest method registerNewPeer.

private Eth2Peer registerNewPeer(final int id) {
    final Eth2Peer peer = mock(Eth2Peer.class);
    when(peer.getOutstandingRequests()).thenReturn(0);
    when(peer.getId()).thenReturn(new MockNodeId(id));
    peers.add(peer);
    return peer;
}
Also used : MockNodeId(tech.pegasys.teku.networking.p2p.mock.MockNodeId) Eth2Peer(tech.pegasys.teku.networking.eth2.peers.Eth2Peer)

Example 2 with MockNodeId

use of tech.pegasys.teku.networking.p2p.mock.MockNodeId 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 3 with MockNodeId

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

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

the class Eth2PeerSelectionStrategyTest method selectPeersToDisconnect_shouldDisconnectExcessRandomlySelectedPeersWhenOutscored.

@Test
void selectPeersToDisconnect_shouldDisconnectExcessRandomlySelectedPeersWhenOutscored() {
    final Eth2PeerSelectionStrategy strategy = createStrategy(2, 2, 1);
    final StubPeer peer1 = new StubPeer(new MockNodeId(1));
    final StubPeer peer2 = new StubPeer(new MockNodeId(2));
    final StubPeer peer3 = new StubPeer(new MockNodeId(3));
    when(network.getPeerCount()).thenReturn(3);
    when(network.streamPeers()).thenReturn(Stream.of(peer1, peer2, peer3));
    peerPools.addPeerToPool(peer1.getId(), PeerPool.RANDOMLY_SELECTED);
    peerPools.addPeerToPool(peer2.getId(), PeerPool.RANDOMLY_SELECTED);
    peerScorer.setScore(peer1.getId(), 100);
    peerScorer.setScore(peer2.getId(), 50);
    peerScorer.setScore(peer3.getId(), 250);
    withShuffleOrder(peer2, peer1, peer3);
    // Peer2 was dropped from the random pool and had the worst score so got dropped
    assertThat(strategy.selectPeersToDisconnect(network, peerPools)).containsExactlyInAnyOrder(peer2);
}
Also used : MockNodeId(tech.pegasys.teku.networking.p2p.mock.MockNodeId) StubPeer(tech.pegasys.teku.network.p2p.peer.StubPeer) Test(org.junit.jupiter.api.Test)

Example 5 with MockNodeId

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

the class Eth2PeerSelectionStrategyTest method selectPeersToDisconnect_shouldMoveExcessRandomlySelectedPeersBackToScoreBasedPool.

@Test
void selectPeersToDisconnect_shouldMoveExcessRandomlySelectedPeersBackToScoreBasedPool() {
    final Eth2PeerSelectionStrategy strategy = createStrategy(2, 2, 1);
    final StubPeer peer1 = new StubPeer(new MockNodeId(1));
    final StubPeer peer2 = new StubPeer(new MockNodeId(2));
    final StubPeer peer3 = new StubPeer(new MockNodeId(3));
    when(network.getPeerCount()).thenReturn(3);
    when(network.streamPeers()).thenReturn(Stream.of(peer1, peer2, peer3));
    peerPools.addPeerToPool(peer1.getId(), PeerPool.RANDOMLY_SELECTED);
    peerPools.addPeerToPool(peer2.getId(), PeerPool.RANDOMLY_SELECTED);
    peerScorer.setScore(peer1.getId(), 100);
    peerScorer.setScore(peer2.getId(), 200);
    peerScorer.setScore(peer3.getId(), 50);
    withShuffleOrder(peer2, peer1, peer3);
    // Peer2 was dropped from the random pool but had a better score than peer3 so was kept
    assertThat(strategy.selectPeersToDisconnect(network, peerPools)).containsExactlyInAnyOrder(peer3);
    assertThat(peerPools.getPool(peer2.getId())).isEqualTo(PeerPool.SCORE_BASED);
}
Also used : MockNodeId(tech.pegasys.teku.networking.p2p.mock.MockNodeId) 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