Skip to content

Commit

Permalink
refactor shcema definitions retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
mehdi-aouadi committed May 10, 2023
1 parent 2c43635 commit 2a44aaf
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package tech.pegasys.teku.beaconrestapi.handlers.tekuv1.beacon;

import static tech.pegasys.teku.beaconrestapi.BeaconRestApiTypes.SLOT_PARAMETER;
import static tech.pegasys.teku.beaconrestapi.handlers.v1.beacon.MilestoneDependentTypesUtil.getSchemaDefinitionForAllMilestones;
import static tech.pegasys.teku.ethereum.json.types.EthereumTypes.MILESTONE_TYPE;
import static tech.pegasys.teku.ethereum.json.types.EthereumTypes.SIGNATURE_TYPE;
import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_NOT_FOUND;
Expand All @@ -29,7 +30,6 @@
import tech.pegasys.teku.api.ChainDataProvider;
import tech.pegasys.teku.api.DataProvider;
import tech.pegasys.teku.api.migrated.AllBlocksAtSlotData;
import tech.pegasys.teku.beaconrestapi.handlers.v1.beacon.MilestoneDependentTypesUtil;
import tech.pegasys.teku.infrastructure.async.SafeFuture;
import tech.pegasys.teku.infrastructure.json.types.SerializableOneOfTypeDefinition;
import tech.pegasys.teku.infrastructure.json.types.SerializableTypeDefinition;
Expand Down Expand Up @@ -91,7 +91,7 @@ public void handleRequest(RestApiRequest request) throws JsonProcessingException
public static SerializableTypeDefinition<AllBlocksAtSlotData> getResponseType(
final SchemaDefinitionCache schemaDefinitionCache) {
final SerializableOneOfTypeDefinition<BeaconBlock> messageType =
MilestoneDependentTypesUtil.getSchemaDefinitionForAllMilestones(
getSchemaDefinitionForAllMilestones(
schemaDefinitionCache,
"BeaconBlock",
SchemaDefinitions::getBeaconBlockSchema,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package tech.pegasys.teku.beaconrestapi.handlers.v1.beacon;

import static tech.pegasys.teku.beaconrestapi.BeaconRestApiTypes.PARAMETER_BLOCK_ID;
import static tech.pegasys.teku.beaconrestapi.handlers.v1.beacon.MilestoneDependentTypesUtil.getSchemaDefinitionForAllMilestones;
import static tech.pegasys.teku.ethereum.json.types.EthereumTypes.MILESTONE_TYPE;
import static tech.pegasys.teku.ethereum.json.types.EthereumTypes.sszResponseType;
import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_OK;
Expand Down Expand Up @@ -92,7 +93,7 @@ public void handleRequest(RestApiRequest request) throws JsonProcessingException
private static SerializableTypeDefinition<ObjectAndMetaData<SignedBeaconBlock>> getResponseType(
SchemaDefinitionCache schemaDefinitionCache) {
final SerializableTypeDefinition<SignedBeaconBlock> signedBeaconBlockType =
MilestoneDependentTypesUtil.getSchemaDefinitionForAllMilestones(
getSchemaDefinitionForAllMilestones(
schemaDefinitionCache,
"SignedBlindedBeaconBlock",
SchemaDefinitions::getSignedBlindedBeaconBlockSchema,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package tech.pegasys.teku.beaconrestapi.handlers.v1.beacon;

import com.fasterxml.jackson.core.JsonProcessingException;
import java.util.List;
import java.util.Optional;
import java.util.function.BiPredicate;
import java.util.function.Function;
Expand All @@ -38,17 +39,8 @@ SerializableOneOfTypeDefinition<T> getAvailableSchemaDefinitionForAllMilestones(
final String title,
final Function<SchemaDefinitions, Optional<SszSchema<? extends T>>> schemaGetter,
final BiPredicate<T, SpecMilestone> predicate) {
final SerializableOneOfTypeDefinitionBuilder<T> builder =
new SerializableOneOfTypeDefinitionBuilder<T>().title(title);
for (SpecMilestone milestone : SpecMilestone.values()) {
final Optional<SszSchema<? extends T>> schemaDefinition =
schemaGetter.apply(schemaDefinitionCache.getSchemaDefinition(milestone));
schemaDefinition.ifPresent(
sszSchema ->
builder.withType(
value -> predicate.test(value, milestone), sszSchema.getJsonTypeDefinition()));
}
return builder.build();
return getAvailableSchemaDefinitions(
schemaDefinitionCache, title, schemaGetter, predicate, List.of(SpecMilestone.values()));
}

public static <T extends SszData>
Expand All @@ -58,9 +50,24 @@ SerializableOneOfTypeDefinition<T> getAvailableSchemaDefinitionUpToMilestone(
final Function<SchemaDefinitions, Optional<SszSchema<? extends T>>> schemaGetter,
final BiPredicate<T, SpecMilestone> predicate,
final SpecMilestone milestone) {
return getAvailableSchemaDefinitions(
schemaDefinitionCache,
title,
schemaGetter,
predicate,
SpecMilestone.getMilestonesUpTo(milestone));
}

private static <T extends SszData>
SerializableOneOfTypeDefinition<T> getAvailableSchemaDefinitions(
final SchemaDefinitionCache schemaDefinitionCache,
final String title,
final Function<SchemaDefinitions, Optional<SszSchema<? extends T>>> schemaGetter,
final BiPredicate<T, SpecMilestone> predicate,
final List<SpecMilestone> milestones) {
final SerializableOneOfTypeDefinitionBuilder<T> builder =
new SerializableOneOfTypeDefinitionBuilder<T>().title(title);
for (SpecMilestone milestoneValue : SpecMilestone.getMilestonesUpTo(milestone)) {
for (SpecMilestone milestoneValue : milestones) {
final Optional<SszSchema<? extends T>> schemaDefinition =
schemaGetter.apply(schemaDefinitionCache.getSchemaDefinition(milestoneValue));
schemaDefinition.ifPresent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

package tech.pegasys.teku.beaconrestapi.handlers.v1.beacon;

import static tech.pegasys.teku.beaconrestapi.handlers.v1.beacon.MilestoneDependentTypesUtil.getSchemaDefinitionForAllMilestones;
import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_ACCEPTED;
import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_INTERNAL_SERVER_ERROR;
import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_OK;
Expand Down Expand Up @@ -101,7 +102,7 @@ private static EndpointMetadata getEndpointMetaData(
+ " The beacon node performs the required validation.")
.tags(TAG_VALIDATOR, TAG_VALIDATOR_REQUIRED)
.requestBodyType(
MilestoneDependentTypesUtil.getSchemaDefinitionForAllMilestones(
getSchemaDefinitionForAllMilestones(
schemaDefinitionCache,
"SignedBlindedBlock",
SchemaDefinitions::getSignedBlindedBeaconBlockSchema,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

package tech.pegasys.teku.beaconrestapi.handlers.v1.beacon;

import static tech.pegasys.teku.beaconrestapi.handlers.v1.beacon.MilestoneDependentTypesUtil.getSchemaDefinitionForAllMilestones;
import static tech.pegasys.teku.beaconrestapi.handlers.v1.beacon.MilestoneDependentTypesUtil.slotBasedSelector;
import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_ACCEPTED;
import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_INTERNAL_SERVER_ERROR;
Expand Down Expand Up @@ -73,7 +74,7 @@ private static EndpointMetadata createMetadata(
+ " The beacon node performs the required validation.")
.tags(TAG_BEACON, TAG_VALIDATOR_REQUIRED)
.requestBodyType(
MilestoneDependentTypesUtil.getSchemaDefinitionForAllMilestones(
getSchemaDefinitionForAllMilestones(
schemaDefinitionCache,
"SignedBeaconBlock",
SchemaDefinitions::getSignedBeaconBlockSchema,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import static tech.pegasys.teku.beaconrestapi.BeaconRestApiTypes.GRAFFITI_PARAMETER;
import static tech.pegasys.teku.beaconrestapi.BeaconRestApiTypes.RANDAO_PARAMETER;
import static tech.pegasys.teku.beaconrestapi.BeaconRestApiTypes.SLOT_PARAMETER;
import static tech.pegasys.teku.beaconrestapi.handlers.v1.beacon.MilestoneDependentTypesUtil.getSchemaDefinitionForAllMilestones;
import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_OK;
import static tech.pegasys.teku.infrastructure.http.RestApiConstants.SLOT_PATH_DESCRIPTION;
import static tech.pegasys.teku.infrastructure.http.RestApiConstants.TAG_VALIDATOR;
Expand All @@ -27,7 +28,6 @@
import org.apache.tuweni.bytes.Bytes32;
import tech.pegasys.teku.api.DataProvider;
import tech.pegasys.teku.api.ValidatorDataProvider;
import tech.pegasys.teku.beaconrestapi.handlers.v1.beacon.MilestoneDependentTypesUtil;
import tech.pegasys.teku.bls.BLSSignature;
import tech.pegasys.teku.infrastructure.async.SafeFuture;
import tech.pegasys.teku.infrastructure.json.types.SerializableTypeDefinition;
Expand Down Expand Up @@ -90,7 +90,7 @@ public void handleRequest(RestApiRequest request) throws JsonProcessingException
private static SerializableTypeDefinition<BeaconBlock> getResponseType(
final SchemaDefinitionCache schemaDefinitionCache) {
final SerializableTypeDefinition<BeaconBlock> beaconBlockType =
MilestoneDependentTypesUtil.getSchemaDefinitionForAllMilestones(
getSchemaDefinitionForAllMilestones(
schemaDefinitionCache,
"BeaconState",
SchemaDefinitions::getBeaconBlockSchema,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package tech.pegasys.teku.beaconrestapi.handlers.v2.beacon;

import static tech.pegasys.teku.beaconrestapi.BeaconRestApiTypes.PARAMETER_BLOCK_ID;
import static tech.pegasys.teku.beaconrestapi.handlers.v1.beacon.MilestoneDependentTypesUtil.getSchemaDefinitionForAllMilestones;
import static tech.pegasys.teku.ethereum.json.types.EthereumTypes.MILESTONE_TYPE;
import static tech.pegasys.teku.ethereum.json.types.EthereumTypes.sszResponseType;
import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_OK;
Expand All @@ -28,7 +29,6 @@
import tech.pegasys.teku.api.ChainDataProvider;
import tech.pegasys.teku.api.DataProvider;
import tech.pegasys.teku.api.schema.Version;
import tech.pegasys.teku.beaconrestapi.handlers.v1.beacon.MilestoneDependentTypesUtil;
import tech.pegasys.teku.infrastructure.async.SafeFuture;
import tech.pegasys.teku.infrastructure.json.types.SerializableTypeDefinition;
import tech.pegasys.teku.infrastructure.restapi.endpoints.AsyncApiResponse;
Expand Down Expand Up @@ -91,7 +91,7 @@ public void handleRequest(RestApiRequest request) throws JsonProcessingException
private static SerializableTypeDefinition<ObjectAndMetaData<SignedBeaconBlock>> getResponseType(
SchemaDefinitionCache schemaDefinitionCache) {
final SerializableTypeDefinition<SignedBeaconBlock> signedBeaconBlockType =
MilestoneDependentTypesUtil.getSchemaDefinitionForAllMilestones(
getSchemaDefinitionForAllMilestones(
schemaDefinitionCache,
"SignedBeaconBlock",
SchemaDefinitions::getSignedBeaconBlockSchema,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package tech.pegasys.teku.beaconrestapi.handlers.v2.debug;

import static tech.pegasys.teku.beaconrestapi.BeaconRestApiTypes.PARAMETER_STATE_ID;
import static tech.pegasys.teku.beaconrestapi.handlers.v1.beacon.MilestoneDependentTypesUtil.getSchemaDefinitionForAllMilestones;
import static tech.pegasys.teku.ethereum.json.types.EthereumTypes.MILESTONE_TYPE;
import static tech.pegasys.teku.ethereum.json.types.EthereumTypes.sszResponseType;
import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_OK;
Expand All @@ -28,7 +29,6 @@
import tech.pegasys.teku.api.ChainDataProvider;
import tech.pegasys.teku.api.DataProvider;
import tech.pegasys.teku.api.schema.Version;
import tech.pegasys.teku.beaconrestapi.handlers.v1.beacon.MilestoneDependentTypesUtil;
import tech.pegasys.teku.infrastructure.async.SafeFuture;
import tech.pegasys.teku.infrastructure.json.types.SerializableTypeDefinition;
import tech.pegasys.teku.infrastructure.restapi.endpoints.AsyncApiResponse;
Expand Down Expand Up @@ -99,7 +99,7 @@ private static SerializableTypeDefinition<StateAndMetaData> getResponseType(
.withField(FINALIZED, BOOLEAN_TYPE, ObjectAndMetaData::isFinalized)
.withField(
"data",
MilestoneDependentTypesUtil.getSchemaDefinitionForAllMilestones(
getSchemaDefinitionForAllMilestones(
schemaDefinitionCache,
"BeaconState",
SchemaDefinitions::getBeaconStateSchema,
Expand Down

0 comments on commit 2a44aaf

Please sign in to comment.