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));
}
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;
}
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));
});
}
Aggregations