Search in sources :

Example 1 with BadRequestException

use of tech.pegasys.teku.api.exceptions.BadRequestException in project teku by ConsenSys.

the class ChainDataProvider method validatorParameterToIndex.

private Optional<Integer> validatorParameterToIndex(final tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState state, final String validatorParameter) {
    if (!isStoreAvailable()) {
        throw new ChainDataUnavailableException();
    }
    if (validatorParameter.toLowerCase().startsWith("0x")) {
        final Bytes48 keyBytes = getBytes48FromParameter(validatorParameter);
        try {
            return spec.getValidatorIndex(state, BLSPublicKey.fromBytesCompressed(keyBytes));
        } catch (IllegalArgumentException ex) {
            return Optional.empty();
        }
    }
    try {
        final UInt64 numericValidator = UInt64.valueOf(validatorParameter);
        if (numericValidator.isGreaterThan(UInt64.valueOf(Integer.MAX_VALUE))) {
            throw new BadRequestException(String.format("Validator Index is too high to use: %s", validatorParameter));
        }
        final int validatorIndex = numericValidator.intValue();
        final int validatorCount = state.getValidators().size();
        if (validatorIndex > validatorCount) {
            return Optional.empty();
        }
        return Optional.of(validatorIndex);
    } catch (NumberFormatException ex) {
        throw new BadRequestException(String.format("Invalid validator: %s", validatorParameter));
    }
}
Also used : ChainDataUnavailableException(tech.pegasys.teku.storage.client.ChainDataUnavailableException) Bytes48(org.apache.tuweni.bytes.Bytes48) BadRequestException(tech.pegasys.teku.api.exceptions.BadRequestException) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64)

Example 2 with BadRequestException

use of tech.pegasys.teku.api.exceptions.BadRequestException 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)

Example 3 with BadRequestException

use of tech.pegasys.teku.api.exceptions.BadRequestException in project teku by ConsenSys.

the class ChainDataProvider method getCommitteesFromState.

List<EpochCommitteeResponse> getCommitteesFromState(final tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState state, final Optional<UInt64> epoch, final Optional<UInt64> committeeIndex, final Optional<UInt64> slot) {
    final Predicate<CommitteeAssignment> slotFilter = slot.isEmpty() ? __ -> true : (assignment) -> assignment.getSlot().equals(slot.get());
    final Predicate<CommitteeAssignment> committeeFilter = committeeIndex.isEmpty() ? __ -> true : (assignment) -> assignment.getCommitteeIndex().compareTo(committeeIndex.get()) == 0;
    final UInt64 stateEpoch = spec.computeEpochAtSlot(state.getSlot());
    if (epoch.isPresent() && epoch.get().isGreaterThan(stateEpoch.plus(ONE))) {
        throw new BadRequestException("Epoch " + epoch.get() + " is too far ahead of state epoch " + stateEpoch);
    }
    if (slot.isPresent()) {
        final UInt64 computeEpochAtSlot = spec.computeEpochAtSlot(slot.get());
        if (!computeEpochAtSlot.equals(epoch.orElse(stateEpoch))) {
            throw new BadRequestException("Slot " + slot.get() + " is not in epoch " + epoch.orElse(stateEpoch));
        }
    }
    return combinedChainDataClient.getCommitteesFromState(state, epoch.orElse(stateEpoch)).stream().filter(slotFilter).filter(committeeFilter).map(EpochCommitteeResponse::new).collect(toList());
}
Also used : CommitteeAssignment(tech.pegasys.teku.spec.datastructures.state.CommitteeAssignment) BadRequestException(tech.pegasys.teku.api.exceptions.BadRequestException) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64)

Example 4 with BadRequestException

use of tech.pegasys.teku.api.exceptions.BadRequestException in project teku by ConsenSys.

the class PostKeys method readSlashingProtectionDataIfPresent.

private Optional<SlashingProtectionImporter> readSlashingProtectionDataIfPresent(final Optional<String> slashingData) {
    if (slashingData.isPresent()) {
        final InputStream slashingProtectionData = IOUtils.toInputStream(slashingData.get(), StandardCharsets.UTF_8);
        final SlashingProtectionImporter importer = new SlashingProtectionImporter(slashingProtectionPath);
        try {
            final Optional<String> initialiseError = importer.initialise(slashingProtectionData);
            if (initialiseError.isPresent()) {
                throw new BadRequestException(initialiseError.get());
            }
            return Optional.of(importer);
        } catch (IOException ex) {
            throw new BadRequestException(ex.getMessage());
        }
    }
    return Optional.empty();
}
Also used : InputStream(java.io.InputStream) SlashingProtectionImporter(tech.pegasys.teku.data.SlashingProtectionImporter) BadRequestException(tech.pegasys.teku.api.exceptions.BadRequestException) IOException(java.io.IOException)

Aggregations

BadRequestException (tech.pegasys.teku.api.exceptions.BadRequestException)4 UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)3 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 Bytes48 (org.apache.tuweni.bytes.Bytes48)1 ServiceUnavailableException (tech.pegasys.teku.api.exceptions.ServiceUnavailableException)1 PostValidatorLivenessResponse (tech.pegasys.teku.api.response.v1.validator.PostValidatorLivenessResponse)1 ValidatorLivenessAtEpoch (tech.pegasys.teku.api.response.v1.validator.ValidatorLivenessAtEpoch)1 SlashingProtectionImporter (tech.pegasys.teku.data.SlashingProtectionImporter)1 CommitteeAssignment (tech.pegasys.teku.spec.datastructures.state.CommitteeAssignment)1 ChainDataUnavailableException (tech.pegasys.teku.storage.client.ChainDataUnavailableException)1