Search in sources :

Example 26 with Eth2Peer

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

the class BeaconBlocksByRootIntegrationTest method requestBlockByRoot_shouldReturnEmptyWhenBlockIsNotKnown.

@Test
void requestBlockByRoot_shouldReturnEmptyWhenBlockIsNotKnown() throws Exception {
    final Eth2Peer peer = createPeer();
    final Optional<SignedBeaconBlock> result = waitFor(peer.requestBlockByRoot(Bytes32.fromHexStringLenient("0x123456789")));
    assertThat(result).isEmpty();
}
Also used : Eth2Peer(tech.pegasys.teku.networking.eth2.peers.Eth2Peer) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 27 with Eth2Peer

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

the class BeaconBlocksByRangeMessageHandler method onIncomingMessage.

@Override
public void onIncomingMessage(final String protocolId, final Eth2Peer peer, final BeaconBlocksByRangeRequestMessage message, final ResponseCallback<SignedBeaconBlock> callback) {
    LOG.trace("Peer {} requested {} BeaconBlocks starting at slot {} with step {}", peer.getId(), message.getCount(), message.getStartSlot(), message.getStep());
    if (message.getStep().compareTo(ONE) < 0) {
        callback.completeWithErrorResponse(new RpcException(INVALID_REQUEST_CODE, "Step must be greater than zero"));
        return;
    }
    if (message.getCount().compareTo(UInt64.valueOf(MAX_REQUEST_BLOCKS)) > 0) {
        callback.completeWithErrorResponse(new RpcException(INVALID_REQUEST_CODE, "Only a maximum of " + MAX_REQUEST_BLOCKS + " blocks can be requested per request"));
        return;
    }
    if (!peer.wantToMakeRequest() || !peer.wantToReceiveObjects(callback, maxRequestSize.min(message.getCount()).longValue())) {
        return;
    }
    sendMatchingBlocks(message, callback).finish(callback::completeSuccessfully, error -> {
        final Throwable rootCause = Throwables.getRootCause(error);
        if (rootCause instanceof RpcException) {
            // Keep full context
            LOG.trace("Rejecting beacon blocks by range request", error);
            callback.completeWithErrorResponse((RpcException) rootCause);
        } else {
            if (rootCause instanceof StreamClosedException || rootCause instanceof ClosedChannelException) {
                LOG.trace("Stream closed while sending requested blocks", error);
            } else {
                LOG.error("Failed to process blocks by range request", error);
            }
            callback.completeWithUnexpectedError(error);
        }
    });
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) RpcException(tech.pegasys.teku.networking.eth2.rpc.core.RpcException) StreamClosedException(tech.pegasys.teku.networking.p2p.rpc.StreamClosedException)

Example 28 with Eth2Peer

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

the class FetchBlockTaskTest method run_withMultiplesPeersAvailable.

@Test
public void run_withMultiplesPeersAvailable() {
    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")));
    when(peer.getOutstandingRequests()).thenReturn(1);
    // Add another peer
    final Eth2Peer peer2 = registerNewPeer(2);
    when(peer2.requestBlockByRoot(blockRoot)).thenReturn(SafeFuture.completedFuture(Optional.of(block)));
    when(peer2.getOutstandingRequests()).thenReturn(0);
    // We should choose the peer that is less busy, which successfully returns the 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 29 with Eth2Peer

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

the class FetchBlockTaskTest method run_failAndRetryWithNewPeer.

@Test
public void run_failAndRetryWithNewPeer() {
    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);
    // Add another peer
    final Eth2Peer peer2 = registerNewPeer(2);
    when(peer2.requestBlockByRoot(blockRoot)).thenReturn(SafeFuture.completedFuture(Optional.of(block)));
    // Retry
    final SafeFuture<FetchBlockResult> result2 = task.run();
    assertThat(result).isDone();
    final FetchBlockResult fetchBlockResult2 = result2.getNow(null);
    assertThat(fetchBlockResult2.isSuccessful()).isTrue();
    assertThat(fetchBlockResult2.getBlock()).isEqualTo(block);
    assertThat(task.getNumberOfRetries()).isEqualTo(1);
}
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 30 with Eth2Peer

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

the class FetchBlockTaskTest method cancel.

@Test
public void cancel() {
    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.completedFuture(Optional.of(block)));
    task.cancel();
    final SafeFuture<FetchBlockResult> result = task.run();
    assertThat(result).isDone();
    final FetchBlockResult fetchBlockResult = result.getNow(null);
    assertThat(fetchBlockResult.isSuccessful()).isFalse();
    assertThat(fetchBlockResult.getStatus()).isEqualTo(Status.CANCELLED);
}
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)

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