use of tech.pegasys.teku.api.exceptions.ServiceUnavailableException in project teku by ConsenSys.
the class PostValidatorLiveness method handle.
@OpenApi(path = ROUTE, method = HttpMethod.GET, summary = "Get Validator Liveness", tags = { TAG_EXPERIMENTAL }, requestBody = @OpenApiRequestBody(content = { @OpenApiContent(from = ValidatorLivenessRequest.class) }), description = "Requests the beacon node to indicate if a validator has been" + " observed to be live in a given epoch. The beacon node might detect liveness by" + " observing messages from the validator on the network, in the beacon chain," + " from its API or from any other source. It is important to note that the" + " values returned by the beacon node are not canonical; they are best-effort" + " and based upon a subjective view of the network.", responses = { @OpenApiResponse(status = RES_OK, content = @OpenApiContent(from = PostValidatorLivenessResponse.class)), @OpenApiResponse(status = RES_BAD_REQUEST, description = "Invalid parameter supplied"), @OpenApiResponse(status = RES_INTERNAL_ERROR), @OpenApiResponse(status = RES_SERVICE_UNAVAILABLE, description = SERVICE_UNAVAILABLE) })
@Override
public void handle(Context ctx) throws Exception {
if (!chainDataProvider.isStoreAvailable() || syncDataProvider.isSyncing()) {
throw new ServiceUnavailableException();
}
try {
final ValidatorLivenessRequest request = parseRequestBody(ctx.body(), ValidatorLivenessRequest.class);
SafeFuture<Optional<PostValidatorLivenessResponse>> future = nodeDataProvider.getValidatorLiveness(request, chainDataProvider.getCurrentEpoch());
handleOptionalResult(ctx, future, this::handleResult, this::handleError, SC_SERVICE_UNAVAILABLE);
} catch (IllegalArgumentException ex) {
LOG.trace("Illegal argument in PostValidatorLiveness", ex);
ctx.status(SC_BAD_REQUEST);
ctx.json(BadRequest.badRequest(jsonProvider, ex.getMessage()));
}
}
use of tech.pegasys.teku.api.exceptions.ServiceUnavailableException in project teku by ConsenSys.
the class NodeDataProvider method getValidatorLiveness.
public SafeFuture<Optional<PostValidatorLivenessResponse>> getValidatorLiveness(final ValidatorLivenessRequest request, final Optional<UInt64> maybeCurrentEpoch) {
if (!isLivenessTrackingEnabled) {
return SafeFuture.failedFuture(new BadRequestException("Validator liveness tracking is not enabled on this beacon node, cannot service request"));
}
// if no validator indices were requested, that's a bad request.
if (request.indices.isEmpty()) {
return SafeFuture.failedFuture(new BadRequestException("No validator indices posted in validator liveness request"));
}
if (maybeCurrentEpoch.isEmpty()) {
return SafeFuture.failedFuture(new ServiceUnavailableException());
}
final UInt64 currentEpoch = maybeCurrentEpoch.get();
if (currentEpoch.isLessThan(request.epoch)) {
return SafeFuture.failedFuture(new BadRequestException(String.format("Current node epoch %s, cannot check liveness for a future epoch %s", currentEpoch, request.epoch)));
} else if (currentEpoch.minusMinZero(TRACKED_EPOCHS).isGreaterThan(request.epoch)) {
return SafeFuture.failedFuture(new BadRequestException(String.format("Current node epoch %s, cannot check liveness for an epoch (%s) more than %d in the past", currentEpoch, request.epoch, TRACKED_EPOCHS)));
}
return activeValidatorChannel.validatorsLiveAtEpoch(request.indices, request.epoch).thenApply(validatorLivenessMap -> {
final List<ValidatorLivenessAtEpoch> livenessAtEpochs = new ArrayList<>();
validatorLivenessMap.forEach((validatorIndex, liveness) -> livenessAtEpochs.add(new ValidatorLivenessAtEpoch(validatorIndex, request.epoch, liveness)));
return Optional.of(new PostValidatorLivenessResponse(livenessAtEpochs));
});
}
Aggregations