Skip to content

Commit 7ec16bc

Browse files
authored
Update syncing status requirements in getHealth (Consensys#8143)
1 parent 508459f commit 7ec16bc

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

Diff for: data/beaconrestapi/src/integration-test/resources/tech/pegasys/teku/beaconrestapi/beacon/paths/_eth_v1_node_health.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"content" : { }
2727
},
2828
"400" : {
29-
"description" : "The request could not be processed, check the response for more information.",
29+
"description" : "Invalid syncing status code",
3030
"content" : {
3131
"application/json" : {
3232
"schema" : {

Diff for: data/beaconrestapi/src/main/java/tech/pegasys/teku/beaconrestapi/handlers/v1/node/GetHealth.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@
2222

2323
import com.fasterxml.jackson.core.JsonProcessingException;
2424
import io.javalin.http.Header;
25+
import java.util.Optional;
2526
import org.apache.logging.log4j.LogManager;
2627
import org.apache.logging.log4j.Logger;
2728
import tech.pegasys.teku.api.ChainDataProvider;
2829
import tech.pegasys.teku.api.DataProvider;
2930
import tech.pegasys.teku.api.SyncDataProvider;
31+
import tech.pegasys.teku.api.exceptions.BadRequestException;
3032
import tech.pegasys.teku.infrastructure.restapi.endpoints.EndpointMetadata;
3133
import tech.pegasys.teku.infrastructure.restapi.endpoints.RestApiEndpoint;
3234
import tech.pegasys.teku.infrastructure.restapi.endpoints.RestApiRequest;
@@ -53,6 +55,7 @@ public GetHealth(final DataProvider provider) {
5355
.response(SC_OK, "Node is ready")
5456
.response(SC_PARTIAL_CONTENT, "Node is syncing but can serve incomplete data")
5557
.response(SC_SERVICE_UNAVAILABLE, "Node not initialized or having issues")
58+
.withBadRequestResponse(Optional.of("Invalid syncing status code"))
5659
.build());
5760
this.syncProvider = syncProvider;
5861
this.chainDataProvider = chainDataProvider;
@@ -72,7 +75,13 @@ public void handleRequest(RestApiRequest request) throws JsonProcessingException
7275

7376
private int getResponseCodeFromQueryParams(final RestApiRequest request) {
7477
try {
75-
return request.getOptionalQueryParameter(SYNCING_PARAMETER).orElse(SC_PARTIAL_CONTENT);
78+
final int responseCode =
79+
request.getOptionalQueryParameter(SYNCING_PARAMETER).orElse(SC_PARTIAL_CONTENT);
80+
if (responseCode < 100 || responseCode > 599) {
81+
throw new BadRequestException("Invalid syncing status code");
82+
} else {
83+
return responseCode;
84+
}
7685
} catch (IllegalArgumentException ex) {
7786
LOG.trace("Illegal parameter in GetHealth", ex);
7887
}

Diff for: data/beaconrestapi/src/test/java/tech/pegasys/teku/beaconrestapi/handlers/v1/node/GetHealthTest.java

+16
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import static jakarta.servlet.http.HttpServletResponse.SC_CONTINUE;
1717
import static org.assertj.core.api.Assertions.assertThat;
18+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
1819
import static org.mockito.Mockito.when;
1920
import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_BAD_REQUEST;
2021
import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_INTERNAL_SERVER_ERROR;
@@ -28,6 +29,9 @@
2829
import com.fasterxml.jackson.core.JsonProcessingException;
2930
import org.junit.jupiter.api.BeforeEach;
3031
import org.junit.jupiter.api.Test;
32+
import org.junit.jupiter.params.ParameterizedTest;
33+
import org.junit.jupiter.params.provider.ValueSource;
34+
import tech.pegasys.teku.api.exceptions.BadRequestException;
3135
import tech.pegasys.teku.beacon.sync.events.SyncState;
3236
import tech.pegasys.teku.beaconrestapi.AbstractMigratedBeaconHandlerTest;
3337

@@ -111,6 +115,18 @@ public void shouldReturnUnavailableWhenStoreNotAvailable() throws Exception {
111115
assertThat(request.getResponseCode()).isEqualTo(SC_SERVICE_UNAVAILABLE);
112116
}
113117

118+
@ParameterizedTest(name = "syncingStatus: {0}")
119+
@ValueSource(ints = {98, 600})
120+
public void shouldThrowBadRequestWhenInvalidSyncingStatusOutsideBounds(final int syncingStatus) {
121+
when(chainDataProvider.isStoreAvailable()).thenReturn(true);
122+
when(syncService.getCurrentSyncState()).thenReturn(SyncState.SYNCING);
123+
request.setOptionalQueryParameter(SYNCING_STATUS, String.valueOf(syncingStatus));
124+
125+
assertThatThrownBy(() -> handler.handleRequest(request))
126+
.isInstanceOf(BadRequestException.class)
127+
.hasMessageContaining("Invalid syncing status code");
128+
}
129+
114130
@Test
115131
void metadata_shouldHandle400() throws JsonProcessingException {
116132
verifyMetadataErrorResponse(handler, SC_BAD_REQUEST);

0 commit comments

Comments
 (0)