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