forked from Consensys/teku
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add POST state validators endpoint (Consensys#7706)
- Loading branch information
1 parent
7fc2037
commit d695ed4
Showing
10 changed files
with
533 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
...sources/tech/pegasys/teku/beaconrestapi/beacon/schema/PostStateValidatorsRequestBody.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"title" : "PostStateValidatorsRequestBody", | ||
"type" : "object", | ||
"required" : [ ], | ||
"properties" : { | ||
"ids" : { | ||
"type" : "array", | ||
"items" : { | ||
"type" : "string" | ||
} | ||
}, | ||
"statuses" : { | ||
"type" : "array", | ||
"items" : { | ||
"type" : "string" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
...src/main/java/tech/pegasys/teku/beaconrestapi/handlers/v1/beacon/PostStateValidators.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/* | ||
* Copyright Consensys Software Inc., 2022 | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package tech.pegasys.teku.beaconrestapi.handlers.v1.beacon; | ||
|
||
import static tech.pegasys.teku.beaconrestapi.BeaconRestApiTypes.PARAMETER_STATE_ID; | ||
import static tech.pegasys.teku.beaconrestapi.handlers.v1.beacon.StatusParameter.getApplicableValidatorStatuses; | ||
import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_OK; | ||
import static tech.pegasys.teku.infrastructure.http.RestApiConstants.TAG_BEACON; | ||
import static tech.pegasys.teku.infrastructure.json.types.CoreTypes.STRING_TYPE; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import tech.pegasys.teku.api.ChainDataProvider; | ||
import tech.pegasys.teku.api.DataProvider; | ||
import tech.pegasys.teku.api.migrated.StateValidatorData; | ||
import tech.pegasys.teku.api.response.v1.beacon.ValidatorStatus; | ||
import tech.pegasys.teku.infrastructure.async.SafeFuture; | ||
import tech.pegasys.teku.infrastructure.json.types.DeserializableTypeDefinition; | ||
import tech.pegasys.teku.infrastructure.restapi.endpoints.AsyncApiResponse; | ||
import tech.pegasys.teku.infrastructure.restapi.endpoints.EndpointMetadata; | ||
import tech.pegasys.teku.infrastructure.restapi.endpoints.RestApiEndpoint; | ||
import tech.pegasys.teku.infrastructure.restapi.endpoints.RestApiRequest; | ||
import tech.pegasys.teku.spec.datastructures.metadata.ObjectAndMetaData; | ||
|
||
public class PostStateValidators extends RestApiEndpoint { | ||
private static final DeserializableTypeDefinition<RequestBody> REQUEST_TYPE = | ||
DeserializableTypeDefinition.object(RequestBody.class) | ||
.name("PostStateValidatorsRequestBody") | ||
.initializer(RequestBody::new) | ||
.withOptionalField( | ||
"ids", | ||
DeserializableTypeDefinition.listOf(STRING_TYPE), | ||
RequestBody::getMaybeIds, | ||
RequestBody::setIds) | ||
.withOptionalField( | ||
"statuses", | ||
DeserializableTypeDefinition.listOf(STRING_TYPE), | ||
RequestBody::getMaybeStringStatuses, | ||
RequestBody::setStatuses) | ||
.build(); | ||
|
||
private final ChainDataProvider chainDataProvider; | ||
|
||
public PostStateValidators(final DataProvider dataProvider) { | ||
this(dataProvider.getChainDataProvider()); | ||
} | ||
|
||
PostStateValidators(final ChainDataProvider provider) { | ||
super( | ||
EndpointMetadata.post(GetStateValidators.ROUTE) | ||
.operationId("postStateValidators") | ||
.summary("Get validators from state") | ||
.description( | ||
"Returns filterable list of validators with their balance, status and index.") | ||
.pathParam(PARAMETER_STATE_ID) | ||
.requestBodyType(REQUEST_TYPE) | ||
.tags(TAG_BEACON) | ||
.response(SC_OK, "Request successful", GetStateValidators.RESPONSE_TYPE) | ||
.withNotFoundResponse() | ||
.build()); | ||
this.chainDataProvider = provider; | ||
} | ||
|
||
@Override | ||
public void handleRequest(RestApiRequest request) throws JsonProcessingException { | ||
final Optional<RequestBody> requestBody = request.getOptionalRequestBody(); | ||
final List<String> validators = requestBody.map(RequestBody::getIds).orElse(List.of()); | ||
final List<StatusParameter> statusParameters = | ||
requestBody.map(RequestBody::getStatuses).orElse(List.of()); | ||
|
||
final Set<ValidatorStatus> statusFilter = getApplicableValidatorStatuses(statusParameters); | ||
|
||
SafeFuture<Optional<ObjectAndMetaData<List<StateValidatorData>>>> future = | ||
chainDataProvider.getStateValidators( | ||
request.getPathParameter(PARAMETER_STATE_ID), validators, statusFilter); | ||
|
||
request.respondAsync( | ||
future.thenApply( | ||
maybeData -> | ||
maybeData | ||
.map(AsyncApiResponse::respondOk) | ||
.orElseGet(AsyncApiResponse::respondNotFound))); | ||
} | ||
|
||
static class RequestBody { | ||
private List<String> ids = List.of(); | ||
private List<StatusParameter> statuses = List.of(); | ||
|
||
RequestBody() {} | ||
|
||
public RequestBody(final List<String> ids, final List<StatusParameter> statuses) { | ||
this.ids = ids; | ||
this.statuses = statuses; | ||
} | ||
|
||
public List<String> getIds() { | ||
return ids; | ||
} | ||
|
||
public Optional<List<String>> getMaybeIds() { | ||
return ids.isEmpty() ? Optional.empty() : Optional.of(ids); | ||
} | ||
|
||
public void setIds(final Optional<List<String>> ids) { | ||
ids.ifPresent(i -> this.ids = i); | ||
} | ||
|
||
public List<StatusParameter> getStatuses() { | ||
return statuses; | ||
} | ||
|
||
public Optional<List<String>> getMaybeStringStatuses() { | ||
return statuses.isEmpty() | ||
? Optional.empty() | ||
: Optional.of(statuses.stream().map(Enum::name).toList()); | ||
} | ||
|
||
public void setStatuses(final Optional<List<String>> statuses) { | ||
statuses.ifPresent(s -> this.statuses = s.stream().map(StatusParameter::parse).toList()); | ||
} | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
...api/src/main/java/tech/pegasys/teku/beaconrestapi/handlers/v1/beacon/StatusParameter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright Consensys Software Inc., 2023 | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package tech.pegasys.teku.beaconrestapi.handlers.v1.beacon; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import tech.pegasys.teku.api.response.v1.beacon.ValidatorStatus; | ||
|
||
public enum StatusParameter { | ||
PENDING_INITIALIZED("pending_initialized"), | ||
PENDING_QUEUED("pending_queued"), | ||
ACTIVE_ONGOING("active_ongoing"), | ||
ACTIVE_EXITING("active_exiting"), | ||
ACTIVE_SLASHED("active_slashed"), | ||
EXITED_UNSLASHED("exited_unslashed"), | ||
EXITED_SLASHED("exited_slashed"), | ||
WITHDRAWAL_POSSIBLE("withdrawal_possible"), | ||
WITHDRAWAL_DONE("withdrawal_done"), | ||
ACTIVE("active"), | ||
PENDING("pending"), | ||
EXITED("exited"), | ||
WITHDRAWAL("withdrawal"); | ||
|
||
private final String value; | ||
|
||
StatusParameter(final String value) { | ||
this.value = value; | ||
} | ||
|
||
public String getValue() { | ||
return value; | ||
} | ||
|
||
public static StatusParameter parse(final String value) { | ||
return StatusParameter.valueOf(value.toUpperCase(Locale.ROOT)); | ||
} | ||
|
||
public static Set<ValidatorStatus> getApplicableValidatorStatuses( | ||
final List<StatusParameter> statusParameters) { | ||
return statusParameters.stream() | ||
.flatMap( | ||
statusParameter -> | ||
Arrays.stream(ValidatorStatus.values()) | ||
.filter( | ||
validatorStatus -> | ||
validatorStatus.name().contains(statusParameter.getValue()))) | ||
.collect(Collectors.toSet()); | ||
} | ||
} |
Oops, something went wrong.