use of tech.pegasys.teku.networking.p2p.peer.PeerDisconnectedException in project teku by ConsenSys.
the class RpcResponseCallback method completeWithUnexpectedError.
@Override
public void completeWithUnexpectedError(final Throwable error) {
if (error instanceof PeerDisconnectedException) {
LOG.trace("Not sending RPC response as peer has already disconnected");
// But close the stream just to be completely sure we don't leak any resources.
rpcStream.closeAbruptly().reportExceptions();
} else {
completeWithErrorResponse(new ServerErrorException());
}
}
use of tech.pegasys.teku.networking.p2p.peer.PeerDisconnectedException in project teku by ConsenSys.
the class SyncSourceBatchTest method requestMoreBlocks_shouldResetAndSelectNewPeerAfterDisconnection.
@Test
void requestMoreBlocks_shouldResetAndSelectNewPeerAfterDisconnection() {
final Runnable callback = mock(Runnable.class);
final Batch batch = createBatch(70, 50);
batch.requestMoreBlocks(callback);
// First request returns some data, so the batch isn't in initial state
final StubSyncSource firstSyncSource = getSyncSource(batch);
firstSyncSource.receiveBlocks(dataStructureUtil.randomSignedBeaconBlock(72));
verify(callback).run();
batch.markFirstBlockConfirmed();
batch.markAsContested();
// Second request should go to the same source
batch.requestMoreBlocks(callback);
firstSyncSource.assertRequestedBlocks(73, 47);
assertThatBatch(batch).isNotEmpty();
// But this requests fails
firstSyncSource.failRequest(new PeerDisconnectedException());
// The request is complete, so should call the callback
verify(callback, times(2)).run();
// And the batch should be back in initial state
assertThatBatch(batch).isEmpty();
assertThatBatch(batch).isNotContested();
assertThatBatch(batch).hasUnconfirmedFirstBlock();
// Third request selects a new peer to request data from
batch.requestMoreBlocks(callback);
assertThat(syncSources.get(batch)).hasSize(2);
final StubSyncSource secondSyncSource = getSyncSource(batch);
assertThat(secondSyncSource).isNotSameAs(firstSyncSource);
secondSyncSource.assertRequestedBlocks(70, 50);
}
use of tech.pegasys.teku.networking.p2p.peer.PeerDisconnectedException in project teku by ConsenSys.
the class RpcHandler method sendRequest.
public SafeFuture<RpcStreamController<TOutgoingHandler>> sendRequest(Connection connection, TRequest request, TRespHandler responseHandler) {
final Bytes initialPayload;
try {
initialPayload = rpcMethod.encodeRequest(request);
} catch (Exception e) {
return SafeFuture.failedFuture(e);
}
Interruptor closeInterruptor = SafeFuture.createInterruptor(connection.closeFuture(), PeerDisconnectedException::new);
Interruptor timeoutInterruptor = SafeFuture.createInterruptor(asyncRunner.getDelayedFuture(TIMEOUT), () -> new StreamTimeoutException("Timed out waiting to initialize stream for protocol(s): " + String.join(",", rpcMethod.getIds())));
return SafeFuture.notInterrupted(closeInterruptor).thenApply(__ -> connection.muxerSession().createStream(this)).thenWaitFor(StreamPromise::getStream).orInterrupt(closeInterruptor, timeoutInterruptor).thenCompose(streamPromise -> {
final SafeFuture<String> protocolIdFuture = SafeFuture.of(streamPromise.getStream().thenCompose(Stream::getProtocol));
// waiting for controller, writing initial payload or interrupt
return SafeFuture.of(streamPromise.getController()).<String, RpcStreamController<TOutgoingHandler>>thenCombine(protocolIdFuture, (controller, protocolId) -> {
final TOutgoingHandler handler = rpcMethod.createOutgoingRequestHandler(protocolId, request, responseHandler);
controller.setOutgoingRequestHandler(handler);
return controller;
}).orInterrupt(closeInterruptor, timeoutInterruptor).thenWaitFor(controller -> controller.getRpcStream().writeBytes(initialPayload)).orInterrupt(closeInterruptor, timeoutInterruptor).whenException(err -> closeStreamAbruptly(streamPromise.getStream().join()));
}).catchAndRethrow(err -> {
if (ExceptionUtil.getCause(err, ConnectionClosedException.class).isPresent()) {
throw new PeerDisconnectedException(err);
}
});
}
use of tech.pegasys.teku.networking.p2p.peer.PeerDisconnectedException in project teku by ConsenSys.
the class PeerRequiredLocalMessageHandler method onIncomingMessage.
@Override
public void onIncomingMessage(final String protocolId, final Optional<Eth2Peer> maybePeer, final I message, final ResponseCallback<O> callback) {
maybePeer.ifPresentOrElse(peer -> onIncomingMessage(protocolId, peer, message, callback), () -> {
LOG.trace("Ignoring message of type {} because peer has disconnected", message.getClass());
callback.completeWithUnexpectedError(new PeerDisconnectedException());
});
}
Aggregations