Search in sources :

Example 1 with ValidatorLivenessAtEpoch

use of tech.pegasys.teku.api.response.v1.validator.ValidatorLivenessAtEpoch 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 2 with ValidatorLivenessAtEpoch

use of tech.pegasys.teku.api.response.v1.validator.ValidatorLivenessAtEpoch 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)

Example 3 with ValidatorLivenessAtEpoch

use of tech.pegasys.teku.api.response.v1.validator.ValidatorLivenessAtEpoch 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));
    });
}
Also used : PostValidatorLivenessResponse(tech.pegasys.teku.api.response.v1.validator.PostValidatorLivenessResponse) ValidatorLivenessAtEpoch(tech.pegasys.teku.api.response.v1.validator.ValidatorLivenessAtEpoch) ArrayList(java.util.ArrayList) BadRequestException(tech.pegasys.teku.api.exceptions.BadRequestException) ServiceUnavailableException(tech.pegasys.teku.api.exceptions.ServiceUnavailableException) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64)

Aggregations

PostValidatorLivenessResponse (tech.pegasys.teku.api.response.v1.validator.PostValidatorLivenessResponse)3 ValidatorLivenessAtEpoch (tech.pegasys.teku.api.response.v1.validator.ValidatorLivenessAtEpoch)3 UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)3 ValidatorLivenessRequest (tech.pegasys.teku.api.request.v1.validator.ValidatorLivenessRequest)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Response (okhttp3.Response)1 Test (org.junit.jupiter.api.Test)1 BadRequestException (tech.pegasys.teku.api.exceptions.BadRequestException)1 ServiceUnavailableException (tech.pegasys.teku.api.exceptions.ServiceUnavailableException)1 AbstractDataBackedRestAPIIntegrationTest (tech.pegasys.teku.beaconrestapi.AbstractDataBackedRestAPIIntegrationTest)1