From 5735ff6db540ecd0458c9fd575f46be70cf49308 Mon Sep 17 00:00:00 2001 From: Enrico Del Fante Date: Mon, 23 May 2022 14:54:40 +0200 Subject: [PATCH] move schemas to ApiSchemas --- .../v1/validator/PostRegisterValidator.java | 28 ++-------------- .../rest/RestExecutionBuilderClientTest.java | 11 +++---- .../rest/RestExecutionBuilderClient.java | 8 ++--- .../pegasys/teku/spec/schemas/ApiSchemas.java | 32 +++++++++++++++++++ .../teku/spec/util/DataStructureUtil.java | 13 +++----- 5 files changed, 45 insertions(+), 47 deletions(-) create mode 100644 ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/ApiSchemas.java diff --git a/data/beaconrestapi/src/main/java/tech/pegasys/teku/beaconrestapi/handlers/v1/validator/PostRegisterValidator.java b/data/beaconrestapi/src/main/java/tech/pegasys/teku/beaconrestapi/handlers/v1/validator/PostRegisterValidator.java index 1f030ecedff..e75529632ae 100644 --- a/data/beaconrestapi/src/main/java/tech/pegasys/teku/beaconrestapi/handlers/v1/validator/PostRegisterValidator.java +++ b/data/beaconrestapi/src/main/java/tech/pegasys/teku/beaconrestapi/handlers/v1/validator/PostRegisterValidator.java @@ -19,6 +19,7 @@ import static tech.pegasys.teku.infrastructure.http.RestApiConstants.RES_OK; import static tech.pegasys.teku.infrastructure.http.RestApiConstants.TAG_VALIDATOR; import static tech.pegasys.teku.infrastructure.http.RestApiConstants.TAG_VALIDATOR_REQUIRED; +import static tech.pegasys.teku.spec.schemas.ApiSchemas.SIGNED_VALIDATOR_REGISTRATIONS_SCHEMA; import com.fasterxml.jackson.core.JsonProcessingException; import io.javalin.http.Context; @@ -27,26 +28,18 @@ import io.javalin.plugin.openapi.annotations.OpenApiContent; import io.javalin.plugin.openapi.annotations.OpenApiRequestBody; import io.javalin.plugin.openapi.annotations.OpenApiResponse; -import org.apache.tuweni.bytes.Bytes; import org.jetbrains.annotations.NotNull; import tech.pegasys.teku.api.DataProvider; import tech.pegasys.teku.api.ValidatorDataProvider; import tech.pegasys.teku.api.request.v1.validator.PostRegisterValidatorRequest; import tech.pegasys.teku.beaconrestapi.MigratingEndpointAdapter; -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.RestApiRequest; -import tech.pegasys.teku.infrastructure.ssz.SszList; import tech.pegasys.teku.spec.Spec; -import tech.pegasys.teku.spec.datastructures.execution.SignedValidatorRegistration; -import tech.pegasys.teku.spec.datastructures.execution.SignedValidatorRegistrationSchema; -import tech.pegasys.teku.spec.datastructures.execution.SignedValidatorRegistrationsSchema; -import tech.pegasys.teku.spec.datastructures.execution.ValidatorRegistrationSchema; public class PostRegisterValidator extends MigratingEndpointAdapter { public static final String ROUTE = "/eth/v1/validator/register_validator"; - private static SignedValidatorRegistrationsSchema signedValidatorRegistrationsSchema; private final ValidatorDataProvider validatorDataProvider; @@ -69,8 +62,8 @@ public PostRegisterValidator(final ValidatorDataProvider validatorDataProvider, + " blocks that do not adhere to their latest fee recipient and gas limit preferences.") .tags(TAG_VALIDATOR, TAG_VALIDATOR_REQUIRED) .requestBodyType( - getRequestType(spec), - PostRegisterValidator::deserializeSignedValidatorRegistrations) + SIGNED_VALIDATOR_REGISTRATIONS_SCHEMA.getJsonTypeDefinition(), + SIGNED_VALIDATOR_REGISTRATIONS_SCHEMA::sszDeserialize) .response(SC_OK, "Registration information has been received.") .build()); @@ -109,19 +102,4 @@ public void handleRequest(final RestApiRequest request) throws JsonProcessingExc .registerValidators(request.getRequestBody()) .thenApply(AsyncApiResponse::respondOk)); } - - private static DeserializableTypeDefinition> getRequestType( - final Spec spec) { - - signedValidatorRegistrationsSchema = - new SignedValidatorRegistrationsSchema( - new SignedValidatorRegistrationSchema(new ValidatorRegistrationSchema()), - spec.getGenesisSpecConfig().getValidatorRegistryLimit()); - return signedValidatorRegistrationsSchema.getJsonTypeDefinition(); - } - - private static SszList deserializeSignedValidatorRegistrations( - final Bytes serializedSignedValidatorRegistration) { - return signedValidatorRegistrationsSchema.sszDeserialize(serializedSignedValidatorRegistration); - } } diff --git a/ethereum/executionclient/src/integration-test/java/tech/pegasys/teku/ethereum/executionclient/rest/RestExecutionBuilderClientTest.java b/ethereum/executionclient/src/integration-test/java/tech/pegasys/teku/ethereum/executionclient/rest/RestExecutionBuilderClientTest.java index ecef10e71f8..7de2e25a943 100644 --- a/ethereum/executionclient/src/integration-test/java/tech/pegasys/teku/ethereum/executionclient/rest/RestExecutionBuilderClientTest.java +++ b/ethereum/executionclient/src/integration-test/java/tech/pegasys/teku/ethereum/executionclient/rest/RestExecutionBuilderClientTest.java @@ -14,6 +14,7 @@ package tech.pegasys.teku.ethereum.executionclient.rest; import static org.assertj.core.api.Assertions.assertThat; +import static tech.pegasys.teku.spec.schemas.ApiSchemas.SIGNED_VALIDATOR_REGISTRATION_SCHEMA; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; @@ -47,8 +48,6 @@ import tech.pegasys.teku.spec.datastructures.execution.ExecutionPayload; import tech.pegasys.teku.spec.datastructures.execution.SignedBuilderBid; import tech.pegasys.teku.spec.datastructures.execution.SignedValidatorRegistration; -import tech.pegasys.teku.spec.datastructures.execution.SignedValidatorRegistrationSchema; -import tech.pegasys.teku.spec.datastructures.execution.ValidatorRegistrationSchema; import tech.pegasys.teku.spec.networks.Eth2Network; import tech.pegasys.teku.spec.schemas.SchemaDefinitionsBellatrix; @@ -88,10 +87,6 @@ class RestExecutionBuilderClientTest { private final MockWebServer mockWebServer = new MockWebServer(); private final OkHttpClient okHttpClient = new OkHttpClient.Builder().build(); - private final DeserializableTypeDefinition - signedValidatorRegistrationType = - new SignedValidatorRegistrationSchema(new ValidatorRegistrationSchema()) - .getJsonTypeDefinition(); private SchemaDefinitionsBellatrix schemaDefinitionsBellatrix; @@ -329,7 +324,9 @@ private void verifyRequest( private SignedValidatorRegistration createSignedValidatorRegistration() { try { - return JsonUtil.parse(SIGNED_VALIDATOR_REGISTRATION_REQUEST, signedValidatorRegistrationType); + return JsonUtil.parse( + SIGNED_VALIDATOR_REGISTRATION_REQUEST, + SIGNED_VALIDATOR_REGISTRATION_SCHEMA.getJsonTypeDefinition()); } catch (JsonProcessingException ex) { throw new UncheckedIOException(ex); } diff --git a/ethereum/executionclient/src/main/java/tech/pegasys/teku/ethereum/executionclient/rest/RestExecutionBuilderClient.java b/ethereum/executionclient/src/main/java/tech/pegasys/teku/ethereum/executionclient/rest/RestExecutionBuilderClient.java index 38a3c13dcfb..307d27cc553 100644 --- a/ethereum/executionclient/src/main/java/tech/pegasys/teku/ethereum/executionclient/rest/RestExecutionBuilderClient.java +++ b/ethereum/executionclient/src/main/java/tech/pegasys/teku/ethereum/executionclient/rest/RestExecutionBuilderClient.java @@ -17,6 +17,7 @@ import static tech.pegasys.teku.spec.config.Constants.EL_BUILDER_GET_PAYLOAD_TIMEOUT; import static tech.pegasys.teku.spec.config.Constants.EL_BUILDER_REGISTER_VALIDATOR_TIMEOUT; import static tech.pegasys.teku.spec.config.Constants.EL_BUILDER_STATUS_TIMEOUT; +import static tech.pegasys.teku.spec.schemas.ApiSchemas.SIGNED_VALIDATOR_REGISTRATION_SCHEMA; import java.util.HashMap; import java.util.Map; @@ -38,8 +39,6 @@ import tech.pegasys.teku.spec.datastructures.execution.SignedBuilderBid; import tech.pegasys.teku.spec.datastructures.execution.SignedBuilderBidSchema; import tech.pegasys.teku.spec.datastructures.execution.SignedValidatorRegistration; -import tech.pegasys.teku.spec.datastructures.execution.SignedValidatorRegistrationSchema; -import tech.pegasys.teku.spec.datastructures.execution.ValidatorRegistrationSchema; import tech.pegasys.teku.spec.schemas.SchemaDefinitionCache; import tech.pegasys.teku.spec.schemas.SchemaDefinitionsBellatrix; @@ -56,9 +55,6 @@ public class RestExecutionBuilderClient implements ExecutionBuilderClient { private final RestClient restClient; private final SchemaDefinitionCache schemaDefinitionCache; - private final SignedValidatorRegistrationSchema signedValidatorRegistrationSchema = - new SignedValidatorRegistrationSchema(new ValidatorRegistrationSchema()); - public RestExecutionBuilderClient(final RestClient restClient, final Spec spec) { this.restClient = restClient; this.schemaDefinitionCache = new SchemaDefinitionCache(spec); @@ -76,7 +72,7 @@ public SafeFuture> registerValidator( final UInt64 slot, final SignedValidatorRegistration signedValidatorRegistration) { final DeserializableTypeDefinition requestType = - signedValidatorRegistrationSchema.getJsonTypeDefinition(); + SIGNED_VALIDATOR_REGISTRATION_SCHEMA.getJsonTypeDefinition(); return restClient .postAsync( BuilderApiMethod.REGISTER_VALIDATOR.getPath(), signedValidatorRegistration, requestType) diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/ApiSchemas.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/ApiSchemas.java new file mode 100644 index 00000000000..5d97283cdef --- /dev/null +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/ApiSchemas.java @@ -0,0 +1,32 @@ +/* + * Copyright 2022 ConsenSys AG. + * + * 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.spec.schemas; + +import tech.pegasys.teku.spec.datastructures.execution.SignedValidatorRegistrationSchema; +import tech.pegasys.teku.spec.datastructures.execution.SignedValidatorRegistrationsSchema; +import tech.pegasys.teku.spec.datastructures.execution.ValidatorRegistrationSchema; + +public class ApiSchemas { + + public static final ValidatorRegistrationSchema VALIDATOR_REGISTRATION_SCHEMA = + new ValidatorRegistrationSchema(); + + public static final SignedValidatorRegistrationSchema SIGNED_VALIDATOR_REGISTRATION_SCHEMA = + new SignedValidatorRegistrationSchema(VALIDATOR_REGISTRATION_SCHEMA); + + public static final long MAX_VALIDATOR_REGISTRATIONS_SIZE = 1099511627776L; + public static final SignedValidatorRegistrationsSchema SIGNED_VALIDATOR_REGISTRATIONS_SCHEMA = + new SignedValidatorRegistrationsSchema( + SIGNED_VALIDATOR_REGISTRATION_SCHEMA, MAX_VALIDATOR_REGISTRATIONS_SIZE); +} diff --git a/ethereum/spec/src/testFixtures/java/tech/pegasys/teku/spec/util/DataStructureUtil.java b/ethereum/spec/src/testFixtures/java/tech/pegasys/teku/spec/util/DataStructureUtil.java index 75bc180f9e1..89ee18dda00 100644 --- a/ethereum/spec/src/testFixtures/java/tech/pegasys/teku/spec/util/DataStructureUtil.java +++ b/ethereum/spec/src/testFixtures/java/tech/pegasys/teku/spec/util/DataStructureUtil.java @@ -17,6 +17,8 @@ import static java.util.stream.Collectors.toList; import static tech.pegasys.teku.spec.config.SpecConfig.FAR_FUTURE_EPOCH; import static tech.pegasys.teku.spec.constants.NetworkConstants.SYNC_COMMITTEE_SUBNET_COUNT; +import static tech.pegasys.teku.spec.schemas.ApiSchemas.SIGNED_VALIDATOR_REGISTRATION_SCHEMA; +import static tech.pegasys.teku.spec.schemas.ApiSchemas.VALIDATOR_REGISTRATION_SCHEMA; import it.unimi.dsi.fastutil.ints.IntList; import java.util.ArrayList; @@ -87,9 +89,7 @@ import tech.pegasys.teku.spec.datastructures.execution.ExecutionPayloadHeader; import tech.pegasys.teku.spec.datastructures.execution.SignedBuilderBid; import tech.pegasys.teku.spec.datastructures.execution.SignedValidatorRegistration; -import tech.pegasys.teku.spec.datastructures.execution.SignedValidatorRegistrationSchema; import tech.pegasys.teku.spec.datastructures.execution.ValidatorRegistration; -import tech.pegasys.teku.spec.datastructures.execution.ValidatorRegistrationSchema; import tech.pegasys.teku.spec.datastructures.forkchoice.VoteTracker; import tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.EnrForkId; import tech.pegasys.teku.spec.datastructures.operations.AggregateAndProof; @@ -140,11 +140,6 @@ public final class DataStructureUtil { private int seed; private Supplier pubKeyGenerator = () -> BLSTestUtil.randomPublicKey(nextSeed()); - private final ValidatorRegistrationSchema validatorRegistrationSchema = - new ValidatorRegistrationSchema(); - private final SignedValidatorRegistrationSchema signedValidatorRegistrationSchema = - new SignedValidatorRegistrationSchema(validatorRegistrationSchema); - public DataStructureUtil(final Spec spec) { this(92892824, spec); } @@ -1154,10 +1149,10 @@ public SignedValidatorRegistration randomValidatorRegistration() { public SignedValidatorRegistration randomValidatorRegistration(final BLSPublicKey publicKey) { final ValidatorRegistration validatorRegistration = - validatorRegistrationSchema.create( + VALIDATOR_REGISTRATION_SCHEMA.create( randomBytes20(), randomUInt64(), randomUInt64(), publicKey); - return signedValidatorRegistrationSchema.create(validatorRegistration, randomSignature()); + return SIGNED_VALIDATOR_REGISTRATION_SCHEMA.create(validatorRegistration, randomSignature()); } public ForkChoiceState randomForkChoiceState(final boolean optimisticHead) {