Search in sources :

Example 1 with Attestation

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

the class EventSubscriptionManager method onNewAttestation.

protected void onNewAttestation(final ValidateableAttestation attestation) {
    final Attestation attestationEvent = new Attestation(attestation.getAttestation());
    notifySubscribersOfEvent(EventType.attestation, attestationEvent);
}
Also used : Attestation(tech.pegasys.teku.api.schema.Attestation) ValidateableAttestation(tech.pegasys.teku.spec.datastructures.attestation.ValidateableAttestation)

Example 2 with Attestation

use of tech.pegasys.teku.api.schema.Attestation 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)

Example 3 with Attestation

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

the class ValidatorDataProviderTest method submitAttestation_shouldSubmitAnInternalAttestationStructure.

@TestTemplate
void submitAttestation_shouldSubmitAnInternalAttestationStructure() {
    tech.pegasys.teku.spec.datastructures.operations.Attestation internalAttestation = dataStructureUtil.randomAttestation();
    Attestation attestation = new Attestation(internalAttestation);
    final List<SubmitDataError> errors = List.of(new SubmitDataError(ZERO, "Nope"));
    final SafeFuture<List<SubmitDataError>> result = SafeFuture.completedFuture(errors);
    when(validatorApiChannel.sendSignedAttestations(any())).thenReturn(result);
    assertThatSafeFuture(provider.submitAttestations(List.of(attestation))).isCompletedWithOptionalContaining(new PostDataFailureResponse(SC_BAD_REQUEST, ValidatorDataProvider.PARTIAL_PUBLISH_FAILURE_MESSAGE, List.of(new PostDataFailure(ZERO, "Nope"))));
    verify(validatorApiChannel).sendSignedAttestations(args.capture());
    assertThat(args.getValue()).hasSize(1);
    assertThatSszData(args.getValue().get(0)).isEqualByAllMeansTo(internalAttestation);
}
Also used : PostDataFailureResponse(tech.pegasys.teku.api.response.v1.beacon.PostDataFailureResponse) PostDataFailure(tech.pegasys.teku.api.response.v1.beacon.PostDataFailure) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) IntList(it.unimi.dsi.fastutil.ints.IntList) Attestation(tech.pegasys.teku.api.schema.Attestation) SubmitDataError(tech.pegasys.teku.validator.api.SubmitDataError) TestTemplate(org.junit.jupiter.api.TestTemplate)

Example 4 with Attestation

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

the class OkHttpValidatorRestApiClientTest method createAggregate_WhenSuccess_ReturnsAttestation.

@Test
public void createAggregate_WhenSuccess_ReturnsAttestation() {
    final Bytes32 attestationHashTreeRoot = Bytes32.random();
    final Attestation expectedAttestation = schemaObjects.attestation();
    mockWebServer.enqueue(new MockResponse().setResponseCode(SC_OK).setBody(asJson(new GetAggregatedAttestationResponse(expectedAttestation))));
    final Optional<Attestation> attestation = apiClient.createAggregate(UInt64.ONE, attestationHashTreeRoot);
    assertThat(attestation).isPresent();
    assertThat(attestation.get()).usingRecursiveComparison().isEqualTo(expectedAttestation);
}
Also used : GetAggregatedAttestationResponse(tech.pegasys.teku.api.response.v1.validator.GetAggregatedAttestationResponse) MockResponse(okhttp3.mockwebserver.MockResponse) Bytes32(org.apache.tuweni.bytes.Bytes32) Attestation(tech.pegasys.teku.api.schema.Attestation) Test(org.junit.jupiter.api.Test)

Example 5 with Attestation

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

the class OkHttpValidatorRestApiClientTest method sendSignedAttestation_MakesExpectedRequest.

@Test
public void sendSignedAttestation_MakesExpectedRequest() throws Exception {
    final Attestation attestation = schemaObjects.attestation();
    mockWebServer.enqueue(new MockResponse().setResponseCode(SC_OK));
    apiClient.sendSignedAttestations(List.of(attestation));
    RecordedRequest request = mockWebServer.takeRequest();
    assertThat(request.getMethod()).isEqualTo("POST");
    assertThat(request.getPath()).contains(ValidatorApiMethod.SEND_SIGNED_ATTESTATION.getPath(emptyMap()));
    assertThat(request.getBody().readString(StandardCharsets.UTF_8)).isEqualTo(asJson(List.of(attestation)));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Attestation(tech.pegasys.teku.api.schema.Attestation) Test(org.junit.jupiter.api.Test)

Aggregations

Attestation (tech.pegasys.teku.api.schema.Attestation)11 Test (org.junit.jupiter.api.Test)7 MockResponse (okhttp3.mockwebserver.MockResponse)4 List (java.util.List)2 Bytes32 (org.apache.tuweni.bytes.Bytes32)2 PostDataFailure (tech.pegasys.teku.api.response.v1.beacon.PostDataFailure)2 PostDataFailureResponse (tech.pegasys.teku.api.response.v1.beacon.PostDataFailureResponse)2 GetAggregatedAttestationResponse (tech.pegasys.teku.api.response.v1.validator.GetAggregatedAttestationResponse)2 SignedBlockAndState (tech.pegasys.teku.spec.datastructures.blocks.SignedBlockAndState)2 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 IntList (it.unimi.dsi.fastutil.ints.IntList)1 Collections.emptyList (java.util.Collections.emptyList)1 Map (java.util.Map)1