Search in sources :

Example 1 with ChainDataUnavailableException

use of tech.pegasys.teku.storage.client.ChainDataUnavailableException 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 ChainDataUnavailableException

use of tech.pegasys.teku.storage.client.ChainDataUnavailableException in project teku by ConsenSys.

the class GetNewBlock method handle.

@OpenApi(path = OAPI_ROUTE, deprecated = true, method = HttpMethod.GET, summary = "Produce unsigned block", tags = { TAG_VALIDATOR, TAG_VALIDATOR_REQUIRED }, description = "Requests a beacon node to produce a valid block, which can then be signed by a validator.\n\n" + "__NOTE__: deprecated, switch to using `/eth/v2/validator/blocks/{slot}` for multiple milestone support.", pathParams = { @OpenApiParam(name = SLOT, description = "The slot for which the block should be proposed.") }, queryParams = { @OpenApiParam(name = RANDAO_REVEAL, description = "`BLSSignature Hex` BLS12-381 signature for the current epoch.", required = true), @OpenApiParam(name = GRAFFITI, description = "`Bytes32 Hex` Graffiti.") }, responses = { @OpenApiResponse(status = RES_OK, content = @OpenApiContent(from = GetNewBlockResponse.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(final Context ctx) throws Exception {
    try {
        final Map<String, List<String>> queryParamMap = ctx.queryParamMap();
        final Map<String, String> pathParamMap = ctx.pathParamMap();
        final UInt64 slot = UInt64.valueOf(pathParamMap.get(SLOT));
        final BLSSignature randao = getParameterValueAsBLSSignature(queryParamMap, RANDAO_REVEAL);
        final Optional<Bytes32> graffiti = getOptionalParameterValueAsBytes32(queryParamMap, GRAFFITI);
        ctx.future(provider.getUnsignedBeaconBlockAtSlot(slot, randao, graffiti).thenApplyChecked(maybeBlock -> {
            if (maybeBlock.isEmpty()) {
                throw new ChainDataUnavailableException();
            }
            return produceResultString(maybeBlock.get());
        }).exceptionallyCompose(error -> handleError(ctx, error)));
    } catch (final IllegalArgumentException e) {
        ctx.status(SC_BAD_REQUEST);
        ctx.json(jsonProvider.objectToJSON(new BadRequest(e.getMessage())));
    }
}
Also used : AbstractHandler(tech.pegasys.teku.beaconrestapi.handlers.AbstractHandler) SafeFuture(tech.pegasys.teku.infrastructure.async.SafeFuture) RES_SERVICE_UNAVAILABLE(tech.pegasys.teku.infrastructure.http.RestApiConstants.RES_SERVICE_UNAVAILABLE) OpenApiContent(io.javalin.plugin.openapi.annotations.OpenApiContent) RANDAO_REVEAL(tech.pegasys.teku.infrastructure.http.RestApiConstants.RANDAO_REVEAL) SERVICE_UNAVAILABLE(tech.pegasys.teku.infrastructure.http.RestApiConstants.SERVICE_UNAVAILABLE) SLOT(tech.pegasys.teku.infrastructure.http.RestApiConstants.SLOT) SingleQueryParameterUtils.getParameterValueAsBytes32(tech.pegasys.teku.beaconrestapi.SingleQueryParameterUtils.getParameterValueAsBytes32) ValidatorDataProvider(tech.pegasys.teku.api.ValidatorDataProvider) GRAFFITI(tech.pegasys.teku.infrastructure.http.RestApiConstants.GRAFFITI) RES_INTERNAL_ERROR(tech.pegasys.teku.infrastructure.http.RestApiConstants.RES_INTERNAL_ERROR) TAG_VALIDATOR(tech.pegasys.teku.infrastructure.http.RestApiConstants.TAG_VALIDATOR) RES_BAD_REQUEST(tech.pegasys.teku.infrastructure.http.RestApiConstants.RES_BAD_REQUEST) Context(io.javalin.http.Context) OpenApiResponse(io.javalin.plugin.openapi.annotations.OpenApiResponse) Map(java.util.Map) JsonProvider(tech.pegasys.teku.provider.JsonProvider) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) ChainDataUnavailableException(tech.pegasys.teku.storage.client.ChainDataUnavailableException) Bytes32(org.apache.tuweni.bytes.Bytes32) BLSSignature(tech.pegasys.teku.api.schema.BLSSignature) HttpMethod(io.javalin.plugin.openapi.annotations.HttpMethod) BeaconBlock(tech.pegasys.teku.api.schema.BeaconBlock) BadRequest(tech.pegasys.teku.beaconrestapi.schema.BadRequest) TAG_VALIDATOR_REQUIRED(tech.pegasys.teku.infrastructure.http.RestApiConstants.TAG_VALIDATOR_REQUIRED) Throwables(com.google.common.base.Throwables) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) GetNewBlockResponse(tech.pegasys.teku.api.response.v1.validator.GetNewBlockResponse) Handler(io.javalin.http.Handler) List(java.util.List) OpenApiParam(io.javalin.plugin.openapi.annotations.OpenApiParam) SingleQueryParameterUtils.getParameterValueAsBLSSignature(tech.pegasys.teku.beaconrestapi.SingleQueryParameterUtils.getParameterValueAsBLSSignature) RES_OK(tech.pegasys.teku.infrastructure.http.RestApiConstants.RES_OK) Optional(java.util.Optional) SC_BAD_REQUEST(javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST) OpenApi(io.javalin.plugin.openapi.annotations.OpenApi) DataProvider(tech.pegasys.teku.api.DataProvider) BadRequest(tech.pegasys.teku.beaconrestapi.schema.BadRequest) ChainDataUnavailableException(tech.pegasys.teku.storage.client.ChainDataUnavailableException) List(java.util.List) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) SingleQueryParameterUtils.getParameterValueAsBytes32(tech.pegasys.teku.beaconrestapi.SingleQueryParameterUtils.getParameterValueAsBytes32) Bytes32(org.apache.tuweni.bytes.Bytes32) BLSSignature(tech.pegasys.teku.api.schema.BLSSignature) SingleQueryParameterUtils.getParameterValueAsBLSSignature(tech.pegasys.teku.beaconrestapi.SingleQueryParameterUtils.getParameterValueAsBLSSignature) OpenApi(io.javalin.plugin.openapi.annotations.OpenApi)

Aggregations

UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)2 ChainDataUnavailableException (tech.pegasys.teku.storage.client.ChainDataUnavailableException)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 Throwables (com.google.common.base.Throwables)1 Context (io.javalin.http.Context)1 Handler (io.javalin.http.Handler)1 HttpMethod (io.javalin.plugin.openapi.annotations.HttpMethod)1 OpenApi (io.javalin.plugin.openapi.annotations.OpenApi)1 OpenApiContent (io.javalin.plugin.openapi.annotations.OpenApiContent)1 OpenApiParam (io.javalin.plugin.openapi.annotations.OpenApiParam)1 OpenApiResponse (io.javalin.plugin.openapi.annotations.OpenApiResponse)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 SC_BAD_REQUEST (javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST)1 Bytes32 (org.apache.tuweni.bytes.Bytes32)1 Bytes48 (org.apache.tuweni.bytes.Bytes48)1 DataProvider (tech.pegasys.teku.api.DataProvider)1 ValidatorDataProvider (tech.pegasys.teku.api.ValidatorDataProvider)1 BadRequestException (tech.pegasys.teku.api.exceptions.BadRequestException)1