Skip to content

Commit

Permalink
move schemas to ApiSchemas
Browse files Browse the repository at this point in the history
  • Loading branch information
tbenr committed May 23, 2022
1 parent 0fc24b2 commit 5735ff6
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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());

Expand Down Expand Up @@ -109,19 +102,4 @@ public void handleRequest(final RestApiRequest request) throws JsonProcessingExc
.registerValidators(request.getRequestBody())
.thenApply(AsyncApiResponse::respondOk));
}

private static DeserializableTypeDefinition<SszList<SignedValidatorRegistration>> getRequestType(
final Spec spec) {

signedValidatorRegistrationsSchema =
new SignedValidatorRegistrationsSchema(
new SignedValidatorRegistrationSchema(new ValidatorRegistrationSchema()),
spec.getGenesisSpecConfig().getValidatorRegistryLimit());
return signedValidatorRegistrationsSchema.getJsonTypeDefinition();
}

private static SszList<SignedValidatorRegistration> deserializeSignedValidatorRegistrations(
final Bytes serializedSignedValidatorRegistration) {
return signedValidatorRegistrationsSchema.sszDeserialize(serializedSignedValidatorRegistration);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -88,10 +87,6 @@ class RestExecutionBuilderClientTest {

private final MockWebServer mockWebServer = new MockWebServer();
private final OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
private final DeserializableTypeDefinition<SignedValidatorRegistration>
signedValidatorRegistrationType =
new SignedValidatorRegistrationSchema(new ValidatorRegistrationSchema())
.getJsonTypeDefinition();

private SchemaDefinitionsBellatrix schemaDefinitionsBellatrix;

Expand Down Expand Up @@ -329,7 +324,9 @@ private <T> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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);
Expand All @@ -76,7 +72,7 @@ public SafeFuture<Response<Void>> registerValidator(
final UInt64 slot, final SignedValidatorRegistration signedValidatorRegistration) {

final DeserializableTypeDefinition<SignedValidatorRegistration> requestType =
signedValidatorRegistrationSchema.getJsonTypeDefinition();
SIGNED_VALIDATOR_REGISTRATION_SCHEMA.getJsonTypeDefinition();
return restClient
.postAsync(
BuilderApiMethod.REGISTER_VALIDATOR.getPath(), signedValidatorRegistration, requestType)
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -140,11 +140,6 @@ public final class DataStructureUtil {
private int seed;
private Supplier<BLSPublicKey> 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);
}
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 5735ff6

Please sign in to comment.