Search in sources :

Example 1 with ValidatorLivenessRequest

use of tech.pegasys.teku.api.request.v1.validator.ValidatorLivenessRequest 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()));
    }
}
Also used : Optional(java.util.Optional) ServiceUnavailableException(tech.pegasys.teku.api.exceptions.ServiceUnavailableException) ValidatorLivenessRequest(tech.pegasys.teku.api.request.v1.validator.ValidatorLivenessRequest) OpenApi(io.javalin.plugin.openapi.annotations.OpenApi)

Example 2 with ValidatorLivenessRequest

use of tech.pegasys.teku.api.request.v1.validator.ValidatorLivenessRequest in project teku by ConsenSys.

the class PostValidatorLivenessIntegrationTest method shouldDetectActiveValidator.

@Test
public void shouldDetectActiveValidator() throws IOException {
    final UInt64 epoch = UInt64.ZERO;
    final UInt64 validatorIndex = UInt64.ONE;
    startRestAPIAtGenesis(SpecMilestone.ALTAIR);
    blockReceivedFromProposer(spec.computeStartSlotAtEpoch(epoch).plus(1), validatorIndex);
    setCurrentSlot(12);
    when(syncService.getCurrentSyncState()).thenReturn(SyncState.IN_SYNC);
    final ValidatorLivenessRequest request = new ValidatorLivenessRequest(epoch, List.of(validatorIndex));
    Response response = post(PostValidatorLiveness.ROUTE, jsonProvider.objectToJSON(request));
    assertThat(response.code()).isEqualTo(SC_OK);
    final PostValidatorLivenessResponse result = jsonProvider.jsonToObject(response.body().string(), PostValidatorLivenessResponse.class);
    assertThat(result.data.get(0)).isEqualTo(new ValidatorLivenessAtEpoch(validatorIndex, epoch, true));
}
Also used : PostValidatorLivenessResponse(tech.pegasys.teku.api.response.v1.validator.PostValidatorLivenessResponse) Response(okhttp3.Response) PostValidatorLivenessResponse(tech.pegasys.teku.api.response.v1.validator.PostValidatorLivenessResponse) ValidatorLivenessAtEpoch(tech.pegasys.teku.api.response.v1.validator.ValidatorLivenessAtEpoch) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) ValidatorLivenessRequest(tech.pegasys.teku.api.request.v1.validator.ValidatorLivenessRequest) Test(org.junit.jupiter.api.Test) AbstractDataBackedRestAPIIntegrationTest(tech.pegasys.teku.beaconrestapi.AbstractDataBackedRestAPIIntegrationTest)

Example 3 with ValidatorLivenessRequest

use of tech.pegasys.teku.api.request.v1.validator.ValidatorLivenessRequest in project teku by ConsenSys.

the class TekuNode method getValidatorLivenessAtEpoch.

private Map<UInt64, Boolean> getValidatorLivenessAtEpoch(final UInt64 epoch, List<UInt64> validators) throws IOException {
    final ValidatorLivenessRequest request = new ValidatorLivenessRequest(epoch, validators);
    final String response = httpClient.post(getRestApiUrl(), "/eth/v1/validator/liveness", jsonProvider.objectToJSON(request));
    final PostValidatorLivenessResponse livenessResponse = jsonProvider.jsonToObject(response, PostValidatorLivenessResponse.class);
    final Map<UInt64, Boolean> output = new HashMap<>();
    for (ValidatorLivenessAtEpoch entry : livenessResponse.data) {
        output.put(entry.index, entry.isLive);
    }
    return output;
}
Also used : PostValidatorLivenessResponse(tech.pegasys.teku.api.response.v1.validator.PostValidatorLivenessResponse) HashMap(java.util.HashMap) ValidatorLivenessAtEpoch(tech.pegasys.teku.api.response.v1.validator.ValidatorLivenessAtEpoch) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) ValidatorLivenessRequest(tech.pegasys.teku.api.request.v1.validator.ValidatorLivenessRequest)

Aggregations

ValidatorLivenessRequest (tech.pegasys.teku.api.request.v1.validator.ValidatorLivenessRequest)3 PostValidatorLivenessResponse (tech.pegasys.teku.api.response.v1.validator.PostValidatorLivenessResponse)2 ValidatorLivenessAtEpoch (tech.pegasys.teku.api.response.v1.validator.ValidatorLivenessAtEpoch)2 UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)2 OpenApi (io.javalin.plugin.openapi.annotations.OpenApi)1 HashMap (java.util.HashMap)1 Optional (java.util.Optional)1 Response (okhttp3.Response)1 Test (org.junit.jupiter.api.Test)1 ServiceUnavailableException (tech.pegasys.teku.api.exceptions.ServiceUnavailableException)1 AbstractDataBackedRestAPIIntegrationTest (tech.pegasys.teku.beaconrestapi.AbstractDataBackedRestAPIIntegrationTest)1