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