Search in sources :

Example 1 with ValidatorBlockResult

use of tech.pegasys.teku.api.schema.ValidatorBlockResult in project teku by ConsenSys.

the class PostBlock method handle.

@OpenApi(path = ROUTE, method = HttpMethod.POST, summary = "Publish a signed block", tags = { TAG_BEACON, TAG_VALIDATOR_REQUIRED }, requestBody = @OpenApiRequestBody(content = { @OpenApiContent(from = SignedBlock.class) }), description = "Submit a signed beacon block to the beacon node to be imported." + " The beacon node performs the required validation.", responses = { @OpenApiResponse(status = RES_OK, description = "Block has been successfully broadcast, validated and imported."), @OpenApiResponse(status = RES_ACCEPTED, description = "Block has been successfully broadcast, but failed validation and has not been imported."), @OpenApiResponse(status = RES_BAD_REQUEST, description = "Unable to parse request body."), @OpenApiResponse(status = RES_INTERNAL_ERROR, description = "Beacon node experienced an internal error."), @OpenApiResponse(status = RES_SERVICE_UNAVAILABLE, description = "Beacon node is currently syncing.") })
@Override
public void handle(final Context ctx) throws Exception {
    try {
        if (syncDataProvider.isSyncing()) {
            ctx.status(SC_SERVICE_UNAVAILABLE);
            ctx.json(BadRequest.serviceUnavailable(jsonProvider));
            return;
        }
        final SignedBeaconBlock signedBeaconBlock = validatorDataProvider.parseBlock(jsonProvider, ctx.body());
        ctx.future(validatorDataProvider.submitSignedBlock(signedBeaconBlock).thenApplyChecked(validatorBlockResult -> handleResponseContext(ctx, validatorBlockResult)));
    } catch (final JsonProcessingException ex) {
        ctx.status(SC_BAD_REQUEST);
        ctx.json(BadRequest.badRequest(jsonProvider, ex.getMessage()));
    } catch (final Exception ex) {
        LOG.error("Failed to post block due to internal error", ex);
        ctx.status(SC_INTERNAL_SERVER_ERROR);
        ctx.json(BadRequest.internalError(jsonProvider, ex.getMessage()));
    }
}
Also used : SC_INTERNAL_SERVER_ERROR(javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR) RES_SERVICE_UNAVAILABLE(tech.pegasys.teku.infrastructure.http.RestApiConstants.RES_SERVICE_UNAVAILABLE) TAG_BEACON(tech.pegasys.teku.infrastructure.http.RestApiConstants.TAG_BEACON) OpenApiContent(io.javalin.plugin.openapi.annotations.OpenApiContent) SC_SERVICE_UNAVAILABLE(javax.servlet.http.HttpServletResponse.SC_SERVICE_UNAVAILABLE) ValidatorDataProvider(tech.pegasys.teku.api.ValidatorDataProvider) RES_INTERNAL_ERROR(tech.pegasys.teku.infrastructure.http.RestApiConstants.RES_INTERNAL_ERROR) RES_BAD_REQUEST(tech.pegasys.teku.infrastructure.http.RestApiConstants.RES_BAD_REQUEST) Context(io.javalin.http.Context) OpenApiResponse(io.javalin.plugin.openapi.annotations.OpenApiResponse) JsonProvider(tech.pegasys.teku.provider.JsonProvider) RES_ACCEPTED(tech.pegasys.teku.infrastructure.http.RestApiConstants.RES_ACCEPTED) SignedBlock(tech.pegasys.teku.api.schema.interfaces.SignedBlock) HttpMethod(io.javalin.plugin.openapi.annotations.HttpMethod) SyncDataProvider(tech.pegasys.teku.api.SyncDataProvider) BadRequest(tech.pegasys.teku.beaconrestapi.schema.BadRequest) TAG_VALIDATOR_REQUIRED(tech.pegasys.teku.infrastructure.http.RestApiConstants.TAG_VALIDATOR_REQUIRED) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Handler(io.javalin.http.Handler) Logger(org.apache.logging.log4j.Logger) SignedBeaconBlock(tech.pegasys.teku.api.schema.SignedBeaconBlock) SC_OK(javax.servlet.http.HttpServletResponse.SC_OK) RES_OK(tech.pegasys.teku.infrastructure.http.RestApiConstants.RES_OK) SC_BAD_REQUEST(javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST) OpenApi(io.javalin.plugin.openapi.annotations.OpenApi) OpenApiRequestBody(io.javalin.plugin.openapi.annotations.OpenApiRequestBody) ValidatorBlockResult(tech.pegasys.teku.api.schema.ValidatorBlockResult) SC_ACCEPTED(javax.servlet.http.HttpServletResponse.SC_ACCEPTED) LogManager(org.apache.logging.log4j.LogManager) DataProvider(tech.pegasys.teku.api.DataProvider) SignedBeaconBlock(tech.pegasys.teku.api.schema.SignedBeaconBlock) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) OpenApi(io.javalin.plugin.openapi.annotations.OpenApi)

Example 2 with ValidatorBlockResult

use of tech.pegasys.teku.api.schema.ValidatorBlockResult in project teku by ConsenSys.

the class PostBlockTest method shouldReturnAcceptedIfBlockFailsValidation.

@Test
void shouldReturnAcceptedIfBlockFailsValidation() throws Exception {
    final ValidatorBlockResult failResult = new ValidatorBlockResult(202, Optional.of("ERROR"), Optional.empty());
    final SafeFuture<ValidatorBlockResult> validatorBlockResultSafeFuture = SafeFuture.completedFuture(failResult);
    when(syncDataProvider.isSyncing()).thenReturn(false);
    when(context.body()).thenReturn(buildSignedBeaconBlock());
    when(validatorDataProvider.submitSignedBlock(any())).thenReturn(validatorBlockResultSafeFuture);
    handler.handle(context);
    verify(context).status(SC_ACCEPTED);
    verify(context).future(args.capture());
    SafeFuture<String> data = args.getValue();
    assertThat(data.get()).isEqualTo("");
}
Also used : ValidatorBlockResult(tech.pegasys.teku.api.schema.ValidatorBlockResult) Test(org.junit.jupiter.api.Test)

Example 3 with ValidatorBlockResult

use of tech.pegasys.teku.api.schema.ValidatorBlockResult in project teku by ConsenSys.

the class ValidatorDataProviderTest method submitSignedBlock_shouldReturn202ForInvalidBlock.

@TestTemplate
public void submitSignedBlock_shouldReturn202ForInvalidBlock() {
    final SignedBeaconBlock internalSignedBeaconBlock = dataStructureUtil.randomSignedBeaconBlock(1);
    final tech.pegasys.teku.api.schema.SignedBeaconBlock signedBeaconBlock = tech.pegasys.teku.api.schema.SignedBeaconBlock.create(internalSignedBeaconBlock);
    final AtomicInteger failReasonCount = new AtomicInteger();
    Stream.of(FailureReason.values()).filter(failureReason -> !failureReason.equals(FailureReason.INTERNAL_ERROR)).forEach(failureReason -> {
        failReasonCount.getAndIncrement();
        final SafeFuture<SendSignedBlockResult> failImportResult = completedFuture(SendSignedBlockResult.notImported(failureReason.name()));
        when(validatorApiChannel.sendSignedBlock(any())).thenReturn(failImportResult);
        final SafeFuture<ValidatorBlockResult> validatorBlockResultSafeFuture = provider.submitSignedBlock(signedBeaconBlock);
        try {
            assertThat(validatorBlockResultSafeFuture.get().getResponseCode()).isEqualTo(202);
        } catch (final Exception e) {
            fail("Exception while executing test.");
        }
    });
    // Assert that the check has run over each FailureReason except the 500.
    assertThat(failReasonCount.get()).isEqualTo(FailureReason.values().length - 1);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) SignedBeaconBlockAltair(tech.pegasys.teku.api.schema.altair.SignedBeaconBlockAltair) SignedBeaconBlockBellatrix(tech.pegasys.teku.api.schema.bellatrix.SignedBeaconBlockBellatrix) CombinedChainDataClient(tech.pegasys.teku.storage.client.CombinedChainDataClient) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TestSpecContext(tech.pegasys.teku.spec.TestSpecContext) Attestation(tech.pegasys.teku.api.schema.Attestation) ONE(tech.pegasys.teku.infrastructure.unsigned.UInt64.ONE) SafeFutureAssert.assertThatSafeFuture(tech.pegasys.teku.infrastructure.async.SafeFutureAssert.assertThatSafeFuture) AttesterDuties(tech.pegasys.teku.validator.api.AttesterDuties) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SszDataAssert.assertThatSszData(tech.pegasys.teku.infrastructure.ssz.SszDataAssert.assertThatSszData) BadRequestException(tech.pegasys.teku.api.exceptions.BadRequestException) JsonProvider(tech.pegasys.teku.provider.JsonProvider) BLSPubKey(tech.pegasys.teku.api.schema.BLSPubKey) Bytes32(org.apache.tuweni.bytes.Bytes32) BLSSignature(tech.pegasys.teku.api.schema.BLSSignature) SC_BAD_REQUEST(tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_BAD_REQUEST) SendSignedBlockResult(tech.pegasys.teku.validator.api.SendSignedBlockResult) Collections.emptyList(java.util.Collections.emptyList) PostDataFailure(tech.pegasys.teku.api.response.v1.beacon.PostDataFailure) List(java.util.List) Assertions.fail(org.assertj.core.api.Assertions.fail) Stream(java.util.stream.Stream) ValidatorApiChannel(tech.pegasys.teku.validator.api.ValidatorApiChannel) PostDataFailureResponse(tech.pegasys.teku.api.response.v1.beacon.PostDataFailureResponse) DataStructureUtil(tech.pegasys.teku.spec.util.DataStructureUtil) Optional(java.util.Optional) SpecContext(tech.pegasys.teku.spec.TestSpecInvocationContextProvider.SpecContext) ValidatorBlockResult(tech.pegasys.teku.api.schema.ValidatorBlockResult) SubmitDataError(tech.pegasys.teku.validator.api.SubmitDataError) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) FailureReason(tech.pegasys.teku.spec.logic.common.statetransition.results.BlockImportResult.FailureReason) SafeFuture(tech.pegasys.teku.infrastructure.async.SafeFuture) SafeFuture.completedFuture(tech.pegasys.teku.infrastructure.async.SafeFuture.completedFuture) TestTemplate(org.junit.jupiter.api.TestTemplate) ArgumentCaptor(org.mockito.ArgumentCaptor) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) PostAttesterDutiesResponse(tech.pegasys.teku.api.response.v1.validator.PostAttesterDutiesResponse) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) ChainDataUnavailableException(tech.pegasys.teku.storage.client.ChainDataUnavailableException) ZERO(tech.pegasys.teku.infrastructure.unsigned.UInt64.ZERO) Spec(tech.pegasys.teku.spec.Spec) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) SafeFutureAssert(tech.pegasys.teku.infrastructure.async.SafeFutureAssert) BeaconBlock(tech.pegasys.teku.api.schema.BeaconBlock) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Mockito.when(org.mockito.Mockito.when) SignedBeaconBlockPhase0(tech.pegasys.teku.api.schema.phase0.SignedBeaconBlockPhase0) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) IntList(it.unimi.dsi.fastutil.ints.IntList) BLSTestUtil(tech.pegasys.teku.bls.BLSTestUtil) AttesterDuty(tech.pegasys.teku.validator.api.AttesterDuty) SendSignedBlockResult(tech.pegasys.teku.validator.api.SendSignedBlockResult) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ValidatorBlockResult(tech.pegasys.teku.api.schema.ValidatorBlockResult) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) BadRequestException(tech.pegasys.teku.api.exceptions.BadRequestException) ChainDataUnavailableException(tech.pegasys.teku.storage.client.ChainDataUnavailableException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ExecutionException(java.util.concurrent.ExecutionException) TestTemplate(org.junit.jupiter.api.TestTemplate)

Example 4 with ValidatorBlockResult

use of tech.pegasys.teku.api.schema.ValidatorBlockResult in project teku by ConsenSys.

the class ValidatorDataProviderTest method submitSignedBlock_shouldReturn500ForInternalError.

@TestTemplate
public void submitSignedBlock_shouldReturn500ForInternalError() throws ExecutionException, InterruptedException {
    final SignedBeaconBlock internalSignedBeaconBlock = dataStructureUtil.randomSignedBeaconBlock(1);
    final tech.pegasys.teku.api.schema.SignedBeaconBlock signedBeaconBlock = tech.pegasys.teku.api.schema.SignedBeaconBlock.create(internalSignedBeaconBlock);
    final SafeFuture<SendSignedBlockResult> failImportResult = completedFuture(SendSignedBlockResult.rejected(FailureReason.INTERNAL_ERROR.name()));
    when(validatorApiChannel.sendSignedBlock(any())).thenReturn(failImportResult);
    final SafeFuture<ValidatorBlockResult> validatorBlockResultSafeFuture = provider.submitSignedBlock(signedBeaconBlock);
    assertThat(validatorBlockResultSafeFuture.get().getResponseCode()).isEqualTo(500);
}
Also used : SendSignedBlockResult(tech.pegasys.teku.validator.api.SendSignedBlockResult) ValidatorBlockResult(tech.pegasys.teku.api.schema.ValidatorBlockResult) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) TestTemplate(org.junit.jupiter.api.TestTemplate)

Example 5 with ValidatorBlockResult

use of tech.pegasys.teku.api.schema.ValidatorBlockResult in project teku by ConsenSys.

the class ValidatorDataProviderTest method submitSignedBlock_shouldReturn200ForSuccess.

@TestTemplate
public void submitSignedBlock_shouldReturn200ForSuccess() throws ExecutionException, InterruptedException {
    final SignedBeaconBlock internalSignedBeaconBlock = dataStructureUtil.randomSignedBeaconBlock(1);
    final tech.pegasys.teku.api.schema.SignedBeaconBlock signedBeaconBlock = tech.pegasys.teku.api.schema.SignedBeaconBlock.create(internalSignedBeaconBlock);
    final SafeFuture<SendSignedBlockResult> successImportResult = completedFuture(SendSignedBlockResult.success(internalSignedBeaconBlock.getRoot()));
    when(validatorApiChannel.sendSignedBlock(any())).thenReturn(successImportResult);
    final SafeFuture<ValidatorBlockResult> validatorBlockResultSafeFuture = provider.submitSignedBlock(signedBeaconBlock);
    assertThat(validatorBlockResultSafeFuture.get().getResponseCode()).isEqualTo(200);
}
Also used : SendSignedBlockResult(tech.pegasys.teku.validator.api.SendSignedBlockResult) ValidatorBlockResult(tech.pegasys.teku.api.schema.ValidatorBlockResult) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) TestTemplate(org.junit.jupiter.api.TestTemplate)

Aggregations

ValidatorBlockResult (tech.pegasys.teku.api.schema.ValidatorBlockResult)6 TestTemplate (org.junit.jupiter.api.TestTemplate)3 SignedBeaconBlock (tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock)3 SendSignedBlockResult (tech.pegasys.teku.validator.api.SendSignedBlockResult)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 Test (org.junit.jupiter.api.Test)2 JsonProvider (tech.pegasys.teku.provider.JsonProvider)2 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 OpenApiRequestBody (io.javalin.plugin.openapi.annotations.OpenApiRequestBody)1 OpenApiResponse (io.javalin.plugin.openapi.annotations.OpenApiResponse)1 IntList (it.unimi.dsi.fastutil.ints.IntList)1 Collections.emptyList (java.util.Collections.emptyList)1 List (java.util.List)1 Optional (java.util.Optional)1 ExecutionException (java.util.concurrent.ExecutionException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1