From 388230341b3595659d683b3dd464c2641c15912f Mon Sep 17 00:00:00 2001 From: Mehdi AOUADI Date: Wed, 24 Jul 2024 09:48:32 +0200 Subject: [PATCH] add consensus header to GetBlockAttestationsV2 API (#8465) * add consensus header to GetBlockAttestationsV2 API --- .../handlers/v2/beacon/GetBlockAttestationsV2.java | 14 +++++++++++--- .../v2/beacon/GetBlockAttestationsV2Test.java | 6 ++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/data/beaconrestapi/src/main/java/tech/pegasys/teku/beaconrestapi/handlers/v2/beacon/GetBlockAttestationsV2.java b/data/beaconrestapi/src/main/java/tech/pegasys/teku/beaconrestapi/handlers/v2/beacon/GetBlockAttestationsV2.java index 2f58169438e..d309cfef9b0 100644 --- a/data/beaconrestapi/src/main/java/tech/pegasys/teku/beaconrestapi/handlers/v2/beacon/GetBlockAttestationsV2.java +++ b/data/beaconrestapi/src/main/java/tech/pegasys/teku/beaconrestapi/handlers/v2/beacon/GetBlockAttestationsV2.java @@ -18,6 +18,7 @@ import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_OK; import static tech.pegasys.teku.infrastructure.http.RestApiConstants.EXECUTION_OPTIMISTIC; import static tech.pegasys.teku.infrastructure.http.RestApiConstants.FINALIZED; +import static tech.pegasys.teku.infrastructure.http.RestApiConstants.HEADER_CONSENSUS_VERSION; import static tech.pegasys.teku.infrastructure.http.RestApiConstants.TAG_BEACON; import static tech.pegasys.teku.infrastructure.json.types.CoreTypes.BOOLEAN_TYPE; import static tech.pegasys.teku.infrastructure.json.types.SerializableTypeDefinition.listOf; @@ -28,6 +29,7 @@ import java.util.function.Predicate; import tech.pegasys.teku.api.ChainDataProvider; import tech.pegasys.teku.api.DataProvider; +import tech.pegasys.teku.api.schema.Version; import tech.pegasys.teku.infrastructure.async.SafeFuture; import tech.pegasys.teku.infrastructure.json.types.DeserializableTypeDefinition; import tech.pegasys.teku.infrastructure.json.types.SerializableOneOfTypeDefinition; @@ -75,9 +77,15 @@ public void handleRequest(final RestApiRequest request) throws JsonProcessingExc request.respondAsync( future.thenApply( - maybeObjectAndMetaData -> - maybeObjectAndMetaData - .map(AsyncApiResponse::respondOk) + maybeAttestationsAndMetadata -> + maybeAttestationsAndMetadata + .map( + attestationsAndMetadata -> { + request.header( + HEADER_CONSENSUS_VERSION, + Version.fromMilestone(attestationsAndMetadata.getMilestone()).name()); + return AsyncApiResponse.respondOk(attestationsAndMetadata); + }) .orElseGet(AsyncApiResponse::respondNotFound))); } diff --git a/data/beaconrestapi/src/test/java/tech/pegasys/teku/beaconrestapi/handlers/v2/beacon/GetBlockAttestationsV2Test.java b/data/beaconrestapi/src/test/java/tech/pegasys/teku/beaconrestapi/handlers/v2/beacon/GetBlockAttestationsV2Test.java index 21242f5dcf6..b084ae0be2b 100644 --- a/data/beaconrestapi/src/test/java/tech/pegasys/teku/beaconrestapi/handlers/v2/beacon/GetBlockAttestationsV2Test.java +++ b/data/beaconrestapi/src/test/java/tech/pegasys/teku/beaconrestapi/handlers/v2/beacon/GetBlockAttestationsV2Test.java @@ -19,6 +19,7 @@ import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_BAD_REQUEST; import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_INTERNAL_SERVER_ERROR; import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_OK; +import static tech.pegasys.teku.infrastructure.http.RestApiConstants.HEADER_CONSENSUS_VERSION; import static tech.pegasys.teku.infrastructure.json.types.DeserializableTypeDefinition.listOf; import static tech.pegasys.teku.infrastructure.restapi.MetadataTestUtil.getResponseStringFromMetadata; import static tech.pegasys.teku.infrastructure.restapi.MetadataTestUtil.verifyMetadataErrorResponse; @@ -29,6 +30,7 @@ import java.util.Optional; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import tech.pegasys.teku.api.schema.Version; import tech.pegasys.teku.beaconrestapi.AbstractMigratedBeaconHandlerTest; import tech.pegasys.teku.ethereum.json.types.SharedApiTypes; import tech.pegasys.teku.infrastructure.async.SafeFuture; @@ -91,6 +93,8 @@ public void shouldReturnBlockAttestationsInformationForPhase0() throws JsonProce assertThat(request.getResponseCode()).isEqualTo(SC_OK); assertThat(phase0responseData.getData().size()).isGreaterThan(0); assertThat(request.getResponseBody()).isEqualTo(optionalData.get()); + assertThat(request.getResponseHeaders(HEADER_CONSENSUS_VERSION)) + .isEqualTo(Version.fromMilestone(phase0responseData.getMilestone()).name()); } @Test @@ -105,6 +109,8 @@ public void shouldReturnBlockAttestationsInformationForElectra() throws JsonProc assertThat(request.getResponseCode()).isEqualTo(SC_OK); assertThat(electraResponseData.getData().size()).isGreaterThan(0); assertThat(request.getResponseBody()).isEqualTo(optionalData.get()); + assertThat(request.getResponseHeaders(HEADER_CONSENSUS_VERSION)) + .isEqualTo(Version.fromMilestone(electraResponseData.getMilestone()).name()); } @Test