|
| 1 | +/* |
| 2 | + * Copyright Consensys Software Inc., 2024 |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 5 | + * the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 10 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | + * specific language governing permissions and limitations under the License. |
| 12 | + */ |
| 13 | + |
| 14 | +package tech.pegasys.teku.validator.client.restapi.apis; |
| 15 | + |
| 16 | +import static tech.pegasys.teku.ethereum.json.types.SharedApiTypes.PUBKEY_API_TYPE; |
| 17 | +import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_OK; |
| 18 | +import static tech.pegasys.teku.infrastructure.json.types.CoreTypes.STRING_TYPE; |
| 19 | +import static tech.pegasys.teku.validator.client.restapi.ValidatorRestApi.TAG_GRAFFITI; |
| 20 | +import static tech.pegasys.teku.validator.client.restapi.ValidatorTypes.PARAM_PUBKEY_TYPE; |
| 21 | + |
| 22 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 23 | +import java.util.Objects; |
| 24 | +import java.util.Optional; |
| 25 | +import java.util.function.Function; |
| 26 | +import org.apache.commons.lang3.NotImplementedException; |
| 27 | +import tech.pegasys.teku.bls.BLSPublicKey; |
| 28 | +import tech.pegasys.teku.infrastructure.json.types.SerializableTypeDefinition; |
| 29 | +import tech.pegasys.teku.infrastructure.restapi.endpoints.EndpointMetadata; |
| 30 | +import tech.pegasys.teku.infrastructure.restapi.endpoints.RestApiEndpoint; |
| 31 | +import tech.pegasys.teku.infrastructure.restapi.endpoints.RestApiRequest; |
| 32 | + |
| 33 | +public class GetGraffiti extends RestApiEndpoint { |
| 34 | + public static final String ROUTE = "/eth/v1/validator/{pubkey}/graffiti"; |
| 35 | + |
| 36 | + private static final SerializableTypeDefinition<GraffitiResponse> GRAFFITI_TYPE = |
| 37 | + SerializableTypeDefinition.object(GraffitiResponse.class) |
| 38 | + .withOptionalField("pubkey", PUBKEY_API_TYPE, GraffitiResponse::getPublicKey) |
| 39 | + .withField("graffiti", STRING_TYPE, GraffitiResponse::getGraffiti) |
| 40 | + .build(); |
| 41 | + |
| 42 | + private static final SerializableTypeDefinition<GraffitiResponse> RESPONSE_TYPE = |
| 43 | + SerializableTypeDefinition.object(GraffitiResponse.class) |
| 44 | + .name("GraffitiResponse") |
| 45 | + .withField("data", GRAFFITI_TYPE, Function.identity()) |
| 46 | + .build(); |
| 47 | + |
| 48 | + public GetGraffiti() { |
| 49 | + super( |
| 50 | + EndpointMetadata.get(ROUTE) |
| 51 | + .operationId("getGraffiti") |
| 52 | + .summary("Get Graffiti") |
| 53 | + .description( |
| 54 | + "Get the graffiti for an individual validator. If no graffiti is set explicitly, returns the process-wide default.") |
| 55 | + .tags(TAG_GRAFFITI) |
| 56 | + .withBearerAuthSecurity() |
| 57 | + .pathParam(PARAM_PUBKEY_TYPE) |
| 58 | + .response(SC_OK, "Success response", RESPONSE_TYPE) |
| 59 | + .withAuthenticationResponses() |
| 60 | + .withNotFoundResponse() |
| 61 | + .withNotImplementedResponse() |
| 62 | + .build()); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public void handleRequest(RestApiRequest request) throws JsonProcessingException { |
| 67 | + throw new NotImplementedException("Not implemented"); |
| 68 | + } |
| 69 | + |
| 70 | + static class GraffitiResponse { |
| 71 | + private final Optional<BLSPublicKey> publicKey; |
| 72 | + private final String graffiti; |
| 73 | + |
| 74 | + GraffitiResponse(final BLSPublicKey publicKey, final String graffiti) { |
| 75 | + this.publicKey = Optional.of(publicKey); |
| 76 | + this.graffiti = graffiti; |
| 77 | + } |
| 78 | + |
| 79 | + Optional<BLSPublicKey> getPublicKey() { |
| 80 | + return publicKey; |
| 81 | + } |
| 82 | + |
| 83 | + String getGraffiti() { |
| 84 | + return graffiti; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public boolean equals(Object o) { |
| 89 | + if (this == o) { |
| 90 | + return true; |
| 91 | + } |
| 92 | + if (o == null || getClass() != o.getClass()) { |
| 93 | + return false; |
| 94 | + } |
| 95 | + GraffitiResponse that = (GraffitiResponse) o; |
| 96 | + return Objects.equals(publicKey, that.publicKey) && Objects.equals(graffiti, that.graffiti); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public int hashCode() { |
| 101 | + return Objects.hash(publicKey, graffiti); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments