Search in sources :

Example 1 with BadRequest

use of tech.pegasys.teku.beaconrestapi.schema.BadRequest in project teku by ConsenSys.

the class GetStateCommitteesTest method shouldGetBadRequestIfEpochTooFarInFuture.

@Test
public void shouldGetBadRequestIfEpochTooFarInFuture() throws IOException {
    final Response response = get("head", Map.of("epoch", "1024000"));
    assertThat(response.code()).isEqualTo(SC_BAD_REQUEST);
    final BadRequest body = jsonProvider.jsonToObject(response.body().string(), BadRequest.class);
    assertThat(body.getCode()).isEqualTo(SC_BAD_REQUEST);
    assertThat(body.getMessage()).startsWith("Epoch 1024000 is too far ahead ");
}
Also used : EpochCommitteeResponse(tech.pegasys.teku.api.response.v1.beacon.EpochCommitteeResponse) GetStateCommitteesResponse(tech.pegasys.teku.api.response.v1.beacon.GetStateCommitteesResponse) Response(okhttp3.Response) BadRequest(tech.pegasys.teku.beaconrestapi.schema.BadRequest) Test(org.junit.jupiter.api.Test) AbstractDataBackedRestAPIIntegrationTest(tech.pegasys.teku.beaconrestapi.AbstractDataBackedRestAPIIntegrationTest)

Example 2 with BadRequest

use of tech.pegasys.teku.beaconrestapi.schema.BadRequest in project teku by ConsenSys.

the class GetBlockHeaders method handle.

@OpenApi(path = ROUTE, method = HttpMethod.GET, summary = "Get block headers", tags = { TAG_BEACON }, description = "Retrieves block headers matching given query. By default it will fetch current head slot blocks.", queryParams = { @OpenApiParam(name = SLOT), @OpenApiParam(name = PARENT_ROOT, description = "Not currently supported.") }, responses = { @OpenApiResponse(status = RES_OK, content = @OpenApiContent(from = GetBlockHeadersResponse.class)), @OpenApiResponse(status = RES_BAD_REQUEST), @OpenApiResponse(status = RES_INTERNAL_ERROR) })
@Override
public void handle(@NotNull final Context ctx) throws Exception {
    final Map<String, List<String>> queryParameters = ctx.queryParamMap();
    final Optional<Bytes32> parentRoot = SingleQueryParameterUtils.getParameterValueAsBytes32IfPresent(queryParameters, PARENT_ROOT);
    final Optional<UInt64> slot = SingleQueryParameterUtils.getParameterValueAsUInt64IfPresent(queryParameters, SLOT);
    try {
        ctx.future(chainDataProvider.getBlockHeaders(parentRoot, slot).thenApplyChecked(jsonProvider::objectToJSON).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) TAG_BEACON(tech.pegasys.teku.infrastructure.http.RestApiConstants.TAG_BEACON) OpenApiContent(io.javalin.plugin.openapi.annotations.OpenApiContent) SLOT(tech.pegasys.teku.infrastructure.http.RestApiConstants.SLOT) RES_INTERNAL_ERROR(tech.pegasys.teku.infrastructure.http.RestApiConstants.RES_INTERNAL_ERROR) RES_BAD_REQUEST(tech.pegasys.teku.infrastructure.http.RestApiConstants.RES_BAD_REQUEST) PARENT_ROOT(tech.pegasys.teku.infrastructure.http.RestApiConstants.PARENT_ROOT) 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) Bytes32(org.apache.tuweni.bytes.Bytes32) ChainDataProvider(tech.pegasys.teku.api.ChainDataProvider) HttpMethod(io.javalin.plugin.openapi.annotations.HttpMethod) BadRequest(tech.pegasys.teku.beaconrestapi.schema.BadRequest) Throwables(com.google.common.base.Throwables) SingleQueryParameterUtils(tech.pegasys.teku.beaconrestapi.SingleQueryParameterUtils) Handler(io.javalin.http.Handler) List(java.util.List) OpenApiParam(io.javalin.plugin.openapi.annotations.OpenApiParam) RES_OK(tech.pegasys.teku.infrastructure.http.RestApiConstants.RES_OK) Optional(java.util.Optional) SC_BAD_REQUEST(javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST) GetBlockHeadersResponse(tech.pegasys.teku.api.response.v1.beacon.GetBlockHeadersResponse) OpenApi(io.javalin.plugin.openapi.annotations.OpenApi) NotNull(org.jetbrains.annotations.NotNull) DataProvider(tech.pegasys.teku.api.DataProvider) BadRequest(tech.pegasys.teku.beaconrestapi.schema.BadRequest) List(java.util.List) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) Bytes32(org.apache.tuweni.bytes.Bytes32) OpenApi(io.javalin.plugin.openapi.annotations.OpenApi)

Example 3 with BadRequest

use of tech.pegasys.teku.beaconrestapi.schema.BadRequest in project teku by ConsenSys.

the class PutLogLevel method handle.

@OpenApi(path = ROUTE, method = HttpMethod.PUT, summary = "Changes the log level without restarting.", tags = { TAG_TEKU }, requestBody = @OpenApiRequestBody(content = { @OpenApiContent(from = LogLevel.class) }, description = "```\n{\n  \"level\": (String; acceptable values: ALL, TRACE, DEBUG, INFO, ERROR, FATAL, OFF ),\n" + "  \"log_filter\": [(String; Optional)]\n}\n```"), description = "Changes the log level without restarting. You can change the log level for all logs, or the log level for specific packages or classes.", responses = { @OpenApiResponse(status = RES_NO_CONTENT, description = "The LogLevel was accepted and applied"), @OpenApiResponse(status = RES_BAD_REQUEST, description = INVALID_BODY_SUPPLIED), @OpenApiResponse(status = RES_INTERNAL_ERROR) })
@Override
public void handle(final Context ctx) throws Exception {
    try {
        final LogLevel params = parseRequestBody(ctx.body(), LogLevel.class);
        final String[] logFilters = params.getLogFilter().orElseGet(() -> new String[] { "" });
        for (final String logFilter : logFilters) {
            LoggingConfigurator.setAllLevels(logFilter, params.getLevel());
        }
        ctx.status(SC_NO_CONTENT);
    } catch (final IllegalArgumentException e) {
        ctx.json(jsonProvider.objectToJSON(new BadRequest(e.getMessage())));
        ctx.status(SC_BAD_REQUEST);
    }
}
Also used : BadRequest(tech.pegasys.teku.beaconrestapi.schema.BadRequest) LogLevel(tech.pegasys.teku.api.schema.LogLevel) OpenApi(io.javalin.plugin.openapi.annotations.OpenApi)

Example 4 with BadRequest

use of tech.pegasys.teku.beaconrestapi.schema.BadRequest in project teku by ConsenSys.

the class GetAttestationDataTest method badRequestParamsTest.

private void badRequestParamsTest(final Map<String, List<String>> params, String message) throws Exception {
    when(context.queryParamMap()).thenReturn(params);
    handler.handle(context);
    verify(context).status(SC_BAD_REQUEST);
    if (StringUtils.isNotEmpty(message)) {
        BadRequest badRequest = new BadRequest(message);
        verify(context).json(jsonProvider.objectToJSON(badRequest));
    }
}
Also used : BadRequest(tech.pegasys.teku.beaconrestapi.schema.BadRequest)

Example 5 with BadRequest

use of tech.pegasys.teku.beaconrestapi.schema.BadRequest in project teku by ConsenSys.

the class GetAggregateAttestation method handle.

@OpenApi(path = ROUTE, method = HttpMethod.GET, summary = "Get aggregated attestations", description = "Aggregates all attestations matching given attestation data root and slot.", tags = { TAG_VALIDATOR, TAG_VALIDATOR_REQUIRED }, queryParams = { @OpenApiParam(name = ATTESTATION_DATA_ROOT, description = "`String` HashTreeRoot of AttestationData that validator wants aggregated.", required = true), @OpenApiParam(name = SLOT, description = "`uint64` Non-finalized slot for which to create the aggregation.", required = true) }, responses = { @OpenApiResponse(status = RES_OK, content = @OpenApiContent(from = GetAggregatedAttestationResponse.class), description = "Returns aggregated `Attestation` object with same `AttestationData` root."), @OpenApiResponse(status = RES_BAD_REQUEST, description = "Invalid parameter supplied"), @OpenApiResponse(status = RES_NOT_FOUND, description = "No matching attestations were found"), @OpenApiResponse(status = RES_FORBIDDEN, description = "Beacon node was not assigned to aggregate on that subnet"), @OpenApiResponse(status = RES_INTERNAL_ERROR, description = "Beacon node internal error.") })
@Override
public void handle(Context ctx) throws Exception {
    try {
        final Map<String, List<String>> parameters = ctx.queryParamMap();
        if (parameters.size() < 2) {
            throw new IllegalArgumentException(String.format("Please specify both %s and %s", ATTESTATION_DATA_ROOT, SLOT));
        }
        Bytes32 beacon_block_root = getParameterValueAsBytes32(parameters, ATTESTATION_DATA_ROOT);
        final UInt64 slot = getParameterValueAsUInt64(parameters, SLOT);
        ctx.future(provider.createAggregate(slot, beacon_block_root).thenApplyChecked(optionalAttestation -> serializeResult(ctx, optionalAttestation)).exceptionallyCompose(error -> handleError(ctx, error)));
    } catch (final IllegalArgumentException e) {
        ctx.json(jsonProvider.objectToJSON(new BadRequest(e.getMessage())));
        ctx.status(SC_BAD_REQUEST);
    }
}
Also used : ATTESTATION_DATA_ROOT(tech.pegasys.teku.infrastructure.http.RestApiConstants.ATTESTATION_DATA_ROOT) AbstractHandler(tech.pegasys.teku.beaconrestapi.handlers.AbstractHandler) SC_INTERNAL_SERVER_ERROR(javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR) Attestation(tech.pegasys.teku.api.schema.Attestation) SafeFuture(tech.pegasys.teku.infrastructure.async.SafeFuture) OpenApiContent(io.javalin.plugin.openapi.annotations.OpenApiContent) ErrorResponse(tech.pegasys.teku.beaconrestapi.schema.ErrorResponse) SLOT(tech.pegasys.teku.infrastructure.http.RestApiConstants.SLOT) SingleQueryParameterUtils.getParameterValueAsBytes32(tech.pegasys.teku.beaconrestapi.SingleQueryParameterUtils.getParameterValueAsBytes32) ValidatorDataProvider(tech.pegasys.teku.api.ValidatorDataProvider) 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) RES_NOT_FOUND(tech.pegasys.teku.infrastructure.http.RestApiConstants.RES_NOT_FOUND) Bytes32(org.apache.tuweni.bytes.Bytes32) HttpMethod(io.javalin.plugin.openapi.annotations.HttpMethod) RES_FORBIDDEN(tech.pegasys.teku.infrastructure.http.RestApiConstants.RES_FORBIDDEN) 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) Handler(io.javalin.http.Handler) SC_NOT_FOUND(javax.servlet.http.HttpServletResponse.SC_NOT_FOUND) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) OpenApiParam(io.javalin.plugin.openapi.annotations.OpenApiParam) SingleQueryParameterUtils.getParameterValueAsUInt64(tech.pegasys.teku.beaconrestapi.SingleQueryParameterUtils.getParameterValueAsUInt64) SC_OK(javax.servlet.http.HttpServletResponse.SC_OK) RES_OK(tech.pegasys.teku.infrastructure.http.RestApiConstants.RES_OK) Optional(java.util.Optional) SC_BAD_REQUEST(javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST) GetAggregatedAttestationResponse(tech.pegasys.teku.api.response.v1.validator.GetAggregatedAttestationResponse) OpenApi(io.javalin.plugin.openapi.annotations.OpenApi) DataProvider(tech.pegasys.teku.api.DataProvider) BadRequest(tech.pegasys.teku.beaconrestapi.schema.BadRequest) List(java.util.List) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) SingleQueryParameterUtils.getParameterValueAsUInt64(tech.pegasys.teku.beaconrestapi.SingleQueryParameterUtils.getParameterValueAsUInt64) SingleQueryParameterUtils.getParameterValueAsBytes32(tech.pegasys.teku.beaconrestapi.SingleQueryParameterUtils.getParameterValueAsBytes32) Bytes32(org.apache.tuweni.bytes.Bytes32) OpenApi(io.javalin.plugin.openapi.annotations.OpenApi)

Aggregations

BadRequest (tech.pegasys.teku.beaconrestapi.schema.BadRequest)13 OpenApi (io.javalin.plugin.openapi.annotations.OpenApi)7 Optional (java.util.Optional)6 UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)6 List (java.util.List)5 Bytes32 (org.apache.tuweni.bytes.Bytes32)4 Test (org.junit.jupiter.api.Test)4 Throwables (com.google.common.base.Throwables)3 Context (io.javalin.http.Context)3 Handler (io.javalin.http.Handler)3 HttpMethod (io.javalin.plugin.openapi.annotations.HttpMethod)3 OpenApiContent (io.javalin.plugin.openapi.annotations.OpenApiContent)3 OpenApiParam (io.javalin.plugin.openapi.annotations.OpenApiParam)3 OpenApiResponse (io.javalin.plugin.openapi.annotations.OpenApiResponse)3 Map (java.util.Map)3 SC_BAD_REQUEST (javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST)3 DataProvider (tech.pegasys.teku.api.DataProvider)3 SingleQueryParameterUtils.getParameterValueAsUInt64 (tech.pegasys.teku.beaconrestapi.SingleQueryParameterUtils.getParameterValueAsUInt64)3 AbstractHandler (tech.pegasys.teku.beaconrestapi.handlers.AbstractHandler)3 SafeFuture (tech.pegasys.teku.infrastructure.async.SafeFuture)3