Search in sources :

Example 6 with Eth2Peer

use of tech.pegasys.teku.networking.eth2.peers.Eth2Peer in project teku by ConsenSys.

the class FetchBlockTaskTest method run_failAndRetryWithNoNewPeers.

@Test
public void run_failAndRetryWithNoNewPeers() {
    final SignedBeaconBlock block = dataStructureUtil.randomSignedBeaconBlock(10);
    final Bytes32 blockRoot = block.getMessage().hashTreeRoot();
    FetchBlockTask task = FetchBlockTask.create(eth2P2PNetwork, blockRoot);
    final Eth2Peer peer = registerNewPeer(1);
    when(peer.requestBlockByRoot(blockRoot)).thenReturn(SafeFuture.failedFuture(new RuntimeException("whoops")));
    final SafeFuture<FetchBlockResult> result = task.run();
    assertThat(result).isDone();
    final FetchBlockResult fetchBlockResult = result.getNow(null);
    assertThat(fetchBlockResult.isSuccessful()).isFalse();
    assertThat(fetchBlockResult.getStatus()).isEqualTo(Status.FETCH_FAILED);
    assertThat(task.getNumberOfRetries()).isEqualTo(0);
    // Retry
    final SafeFuture<FetchBlockResult> result2 = task.run();
    assertThat(result).isDone();
    final FetchBlockResult fetchBlockResult2 = result2.getNow(null);
    assertThat(fetchBlockResult2.isSuccessful()).isFalse();
    assertThat(fetchBlockResult2.getStatus()).isEqualTo(Status.NO_AVAILABLE_PEERS);
    assertThat(task.getNumberOfRetries()).isEqualTo(0);
}
Also used : Eth2Peer(tech.pegasys.teku.networking.eth2.peers.Eth2Peer) FetchBlockResult(tech.pegasys.teku.beacon.sync.gossip.FetchBlockTask.FetchBlockResult) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) Bytes32(org.apache.tuweni.bytes.Bytes32) Test(org.junit.jupiter.api.Test)

Example 7 with Eth2Peer

use of tech.pegasys.teku.networking.eth2.peers.Eth2Peer 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 8 with Eth2Peer

use of tech.pegasys.teku.networking.eth2.peers.Eth2Peer in project teku by ConsenSys.

the class FetchBlockTaskTest method run_successful.

@Test
public void run_successful() {
    final SignedBeaconBlock block = dataStructureUtil.randomSignedBeaconBlock(10);
    final Bytes32 blockRoot = block.getMessage().hashTreeRoot();
    FetchBlockTask task = FetchBlockTask.create(eth2P2PNetwork, blockRoot);
    assertThat(task.getBlockRoot()).isEqualTo(blockRoot);
    final Eth2Peer peer = registerNewPeer(1);
    when(peer.requestBlockByRoot(blockRoot)).thenReturn(SafeFuture.completedFuture(Optional.of(block)));
    final SafeFuture<FetchBlockResult> result = task.run();
    assertThat(result).isDone();
    final FetchBlockResult fetchBlockResult = result.getNow(null);
    assertThat(fetchBlockResult.isSuccessful()).isTrue();
    assertThat(fetchBlockResult.getBlock()).isEqualTo(block);
}
Also used : Eth2Peer(tech.pegasys.teku.networking.eth2.peers.Eth2Peer) FetchBlockResult(tech.pegasys.teku.beacon.sync.gossip.FetchBlockTask.FetchBlockResult) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) Bytes32(org.apache.tuweni.bytes.Bytes32) Test(org.junit.jupiter.api.Test)

Example 9 with Eth2Peer

use of tech.pegasys.teku.networking.eth2.peers.Eth2Peer in project teku by ConsenSys.

the class ReadinessTest method shouldReturnOkWhenInSyncAndReadyAndTargetPeerCountReached.

@Test
public void shouldReturnOkWhenInSyncAndReadyAndTargetPeerCountReached() throws Exception {
    final Eth2Peer peer1 = mock(Eth2Peer.class);
    final Readiness handler = new Readiness(syncDataProvider, chainDataProvider, network, jsonProvider);
    when(chainDataProvider.isStoreAvailable()).thenReturn(true);
    when(syncService.getCurrentSyncState()).thenReturn(SyncState.IN_SYNC);
    when(eth2P2PNetwork.streamPeers()).thenReturn(Stream.of(peer1, peer1)).thenReturn(Stream.of(peer1, peer1));
    when(context.queryParamMap()).thenReturn(Map.of(TARGET_PEER_COUNT, List.of("1")));
    handler.handle(context);
    verifyCacheStatus(CACHE_NONE);
    verifyStatusCode(SC_OK);
}
Also used : Eth2Peer(tech.pegasys.teku.networking.eth2.peers.Eth2Peer) AbstractBeaconHandlerTest(tech.pegasys.teku.beaconrestapi.AbstractBeaconHandlerTest) Test(org.junit.jupiter.api.Test)

Example 10 with Eth2Peer

use of tech.pegasys.teku.networking.eth2.peers.Eth2Peer in project teku by ConsenSys.

the class ReadinessTest method shouldReturnUnavailableWhenTargetPeerCountNotReached.

@Test
public void shouldReturnUnavailableWhenTargetPeerCountNotReached() throws Exception {
    final Eth2Peer peer1 = mock(Eth2Peer.class);
    final Readiness handler = new Readiness(syncDataProvider, chainDataProvider, network, jsonProvider);
    when(chainDataProvider.isStoreAvailable()).thenReturn(true);
    when(syncService.getCurrentSyncState()).thenReturn(SyncState.IN_SYNC);
    when(eth2P2PNetwork.streamPeers()).thenReturn(Stream.of(peer1)).thenReturn(Stream.of(peer1));
    when(context.queryParamMap()).thenReturn(Map.of(TARGET_PEER_COUNT, List.of("10")));
    handler.handle(context);
    verifyCacheStatus(CACHE_NONE);
    verifyStatusCode(SC_SERVICE_UNAVAILABLE);
}
Also used : Eth2Peer(tech.pegasys.teku.networking.eth2.peers.Eth2Peer) AbstractBeaconHandlerTest(tech.pegasys.teku.beaconrestapi.AbstractBeaconHandlerTest) Test(org.junit.jupiter.api.Test)

Aggregations

Eth2Peer (tech.pegasys.teku.networking.eth2.peers.Eth2Peer)47 Test (org.junit.jupiter.api.Test)37 SignedBeaconBlock (tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock)29 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)26 Bytes32 (org.apache.tuweni.bytes.Bytes32)14 SignedBlockAndState (tech.pegasys.teku.spec.datastructures.blocks.SignedBlockAndState)10 ArrayList (java.util.ArrayList)7 Optional (java.util.Optional)7 SafeFuture (tech.pegasys.teku.infrastructure.async.SafeFuture)7 RpcException (tech.pegasys.teku.networking.eth2.rpc.core.RpcException)7 MethodSource (org.junit.jupiter.params.provider.MethodSource)6 RecentChainData (tech.pegasys.teku.storage.client.RecentChainData)6 FetchBlockResult (tech.pegasys.teku.beacon.sync.gossip.FetchBlockTask.FetchBlockResult)5 StorageSystem (tech.pegasys.teku.storage.storageSystem.StorageSystem)5 MetadataMessage (tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.metadata.MetadataMessage)4 LogManager (org.apache.logging.log4j.LogManager)3 PeerStatus (tech.pegasys.teku.networking.eth2.peers.PeerStatus)3 Spec (tech.pegasys.teku.spec.Spec)3 Throwables (com.google.common.base.Throwables)2 ClosedChannelException (java.nio.channels.ClosedChannelException)2