Search in sources :

Example 51 with Eth2Peer

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

the class BeaconBlocksByRangeIntegrationTest method shouldSendEmptyResponseWhenNoBlocksAreAvailable.

@Test
public void shouldSendEmptyResponseWhenNoBlocksAreAvailable() throws Exception {
    final Eth2Peer peer = createPeer();
    final List<SignedBeaconBlock> response = requestBlocks(peer);
    assertThat(response).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 52 with Eth2Peer

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

the class BeaconBlocksByRangeIntegrationTest method requestBlockBySlotAfterPeerDisconnectedImmediately.

@Test
public void requestBlockBySlotAfterPeerDisconnectedImmediately() throws Exception {
    final Eth2Peer peer = createPeer();
    // Setup chain
    peerStorage.chainUpdater().advanceChain();
    final SignedBlockAndState block2 = peerStorage.chainUpdater().advanceChain();
    peerStorage.chainUpdater().updateBestBlock(block2);
    peer.disconnectImmediately(Optional.empty(), false);
    final SafeFuture<Optional<SignedBeaconBlock>> res = peer.requestBlockBySlot(UInt64.ONE);
    waitFor(() -> assertThat(res).isDone());
    assertThat(res).isCompletedExceptionally();
    assertThatThrownBy(res::get).hasRootCauseInstanceOf(PeerDisconnectedException.class);
}
Also used : Optional(java.util.Optional) Eth2Peer(tech.pegasys.teku.networking.eth2.peers.Eth2Peer) SignedBlockAndState(tech.pegasys.teku.spec.datastructures.blocks.SignedBlockAndState) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 53 with Eth2Peer

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

the class BeaconBlocksByRangeIntegrationTest method requestBlockBySlot_shouldReturnEmptyWhenBlockIsNotKnown.

@Test
void requestBlockBySlot_shouldReturnEmptyWhenBlockIsNotKnown() throws Exception {
    final Eth2Peer peer = createPeer();
    final Optional<SignedBeaconBlock> result = waitFor(peer.requestBlockBySlot(UInt64.valueOf(49382982)));
    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 54 with Eth2Peer

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

the class BeaconBlocksByRootMessageHandler method onIncomingMessage.

@Override
public void onIncomingMessage(final String protocolId, final Eth2Peer peer, final BeaconBlocksByRootRequestMessage message, final ResponseCallback<SignedBeaconBlock> callback) {
    LOG.trace("Peer {} requested BeaconBlocks with roots: {}", peer.getId(), message);
    if (storageClient.getStore() != null) {
        SafeFuture<Void> future = SafeFuture.COMPLETE;
        if (!peer.wantToMakeRequest() || !peer.wantToReceiveObjects(callback, message.size())) {
            peer.disconnectCleanly(DisconnectReason.RATE_LIMITING).reportExceptions();
            return;
        }
        for (SszBytes32 blockRoot : message) {
            future = future.thenCompose(__ -> storageClient.getStore().retrieveSignedBlock(blockRoot.get()).thenCompose(block -> {
                final Optional<RpcException> validationResult = block.flatMap(b -> validateResponse(protocolId, b));
                if (validationResult.isPresent()) {
                    return SafeFuture.failedFuture(validationResult.get());
                }
                return block.map(callback::respond).orElse(SafeFuture.COMPLETE);
            }));
        }
        future.finish(callback::completeSuccessfully, err -> handleError(callback, err));
    } else {
        callback.completeSuccessfully();
    }
}
Also used : PeerRequiredLocalMessageHandler(tech.pegasys.teku.networking.eth2.rpc.core.PeerRequiredLocalMessageHandler) BeaconBlocksByRootRequestMessage(tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.BeaconBlocksByRootRequestMessage) RpcException(tech.pegasys.teku.networking.eth2.rpc.core.RpcException) ClosedChannelException(java.nio.channels.ClosedChannelException) Throwables(com.google.common.base.Throwables) SafeFuture(tech.pegasys.teku.infrastructure.async.SafeFuture) SszBytes32(tech.pegasys.teku.infrastructure.ssz.primitive.SszBytes32) ResponseCallback(tech.pegasys.teku.networking.eth2.rpc.core.ResponseCallback) DisconnectReason(tech.pegasys.teku.networking.p2p.peer.DisconnectReason) StreamClosedException(tech.pegasys.teku.networking.p2p.rpc.StreamClosedException) BeaconChainMethodIds(tech.pegasys.teku.networking.eth2.rpc.beaconchain.BeaconChainMethodIds) RecentChainData(tech.pegasys.teku.storage.client.RecentChainData) Eth2Peer(tech.pegasys.teku.networking.eth2.peers.Eth2Peer) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Spec(tech.pegasys.teku.spec.Spec) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) LogManager(org.apache.logging.log4j.LogManager) InvalidRpcMethodVersion(tech.pegasys.teku.networking.eth2.rpc.core.RpcException.InvalidRpcMethodVersion) SpecMilestone(tech.pegasys.teku.spec.SpecMilestone) SszBytes32(tech.pegasys.teku.infrastructure.ssz.primitive.SszBytes32) RpcException(tech.pegasys.teku.networking.eth2.rpc.core.RpcException)

Example 55 with Eth2Peer

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

the class StatusMessageHandler method onIncomingMessage.

@Override
public void onIncomingMessage(final String protocolId, final Eth2Peer peer, final StatusMessage message, final ResponseCallback<StatusMessage> callback) {
    LOG.trace("Peer {} sent status {}", peer.getId(), message);
    if (!peer.wantToMakeRequest()) {
        return;
    }
    final PeerStatus status = PeerStatus.fromStatusMessage(message);
    peer.updateStatus(status);
    final Optional<StatusMessage> localStatus = statusMessageFactory.createStatusMessage();
    if (localStatus.isPresent()) {
        callback.respondAndCompleteSuccessfully(localStatus.get());
    } else {
        LOG.warn("Node is not ready to receive p2p traffic. Responding to incoming status message with an error.");
        callback.completeWithErrorResponse(NODE_NOT_READY);
    }
}
Also used : PeerStatus(tech.pegasys.teku.networking.eth2.peers.PeerStatus) StatusMessage(tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.StatusMessage)

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