use of tech.pegasys.teku.spec.logic.common.statetransition.results.BlockImportResult.FailureReason in project teku by ConsenSys.
the class PeerSync method handleFailedRequestToPeer.
private PeerSyncResult handleFailedRequestToPeer(Eth2Peer peer, final PeerStatus peerStatus, Throwable err) {
Throwable rootException = Throwables.getRootCause(err);
if (rootException instanceof FailedBlockImportException) {
final FailedBlockImportException importException = (FailedBlockImportException) rootException;
final FailureReason reason = importException.getResult().getFailureReason();
final SignedBeaconBlock block = importException.getBlock();
if (reason.equals(FailureReason.UNKNOWN_PARENT) && !hasPeerFinalizedBlock(block, peerStatus)) {
// We received a block that doesn't connect to our chain.
// This can happen if our peer is sending us blocks from the non-final portion of their
// chain. They may be sending us blocks from a stale fork that we have already pruned out of
// our Store.
LOG.debug("Failed to import non-final block from peer (err: {}) {}: {}", reason, block, peer);
return PeerSyncResult.IMPORT_FAILED;
} else if (BAD_BLOCK_FAILURE_REASONS.contains(reason)) {
LOG.warn("Failed to import block from peer (err: {}) {}: {}", reason, block, peer);
LOG.debug("Disconnecting from peer ({}) who sent invalid block ({}): {}", peer, reason.name(), block);
disconnectFromPeer(peer);
return PeerSyncResult.BAD_BLOCK;
} else {
LOG.warn("Failed to import block from peer (err: {}) {}: {}", reason, block, peer);
return PeerSyncResult.IMPORT_FAILED;
}
}
if (rootException instanceof CancellationException) {
return PeerSyncResult.CANCELLED;
}
if (rootException instanceof BlocksByRangeResponseInvalidResponseException || rootException instanceof RpcException) {
disconnectFromPeer(peer);
return PeerSyncResult.INVALID_RESPONSE;
}
if (err instanceof RuntimeException) {
throw (RuntimeException) err;
} else {
throw new RuntimeException("Unhandled error while syncing", err);
}
}
use of tech.pegasys.teku.spec.logic.common.statetransition.results.BlockImportResult.FailureReason in project teku by ConsenSys.
the class ValidatorDataProviderTest method submitSignedBlock_shouldReturn202ForInvalidBlock.
@TestTemplate
public void submitSignedBlock_shouldReturn202ForInvalidBlock() {
final SignedBeaconBlock internalSignedBeaconBlock = dataStructureUtil.randomSignedBeaconBlock(1);
final tech.pegasys.teku.api.schema.SignedBeaconBlock signedBeaconBlock = tech.pegasys.teku.api.schema.SignedBeaconBlock.create(internalSignedBeaconBlock);
final AtomicInteger failReasonCount = new AtomicInteger();
Stream.of(FailureReason.values()).filter(failureReason -> !failureReason.equals(FailureReason.INTERNAL_ERROR)).forEach(failureReason -> {
failReasonCount.getAndIncrement();
final SafeFuture<SendSignedBlockResult> failImportResult = completedFuture(SendSignedBlockResult.notImported(failureReason.name()));
when(validatorApiChannel.sendSignedBlock(any())).thenReturn(failImportResult);
final SafeFuture<ValidatorBlockResult> validatorBlockResultSafeFuture = provider.submitSignedBlock(signedBeaconBlock);
try {
assertThat(validatorBlockResultSafeFuture.get().getResponseCode()).isEqualTo(202);
} catch (final Exception e) {
fail("Exception while executing test.");
}
});
// Assert that the check has run over each FailureReason except the 500.
assertThat(failReasonCount.get()).isEqualTo(FailureReason.values().length - 1);
}
Aggregations