Skip to content

Commit

Permalink
reuse attestation type definition
Browse files Browse the repository at this point in the history
  • Loading branch information
mehdi-aouadi committed Jul 24, 2024
1 parent cdb0324 commit eb4f50a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@
import tech.pegasys.teku.spec.SpecMilestone;
import tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlockHeader;
import tech.pegasys.teku.spec.datastructures.metadata.BlockAndMetaData;
import tech.pegasys.teku.spec.datastructures.operations.Attestation;
import tech.pegasys.teku.spec.datastructures.validator.BroadcastValidationLevel;
import tech.pegasys.teku.spec.schemas.SchemaDefinitionCache;

public class BeaconRestApiTypes {
private static final StringValueTypeDefinition<StatusParameter> STATUS_VALUE =
Expand Down Expand Up @@ -247,6 +249,26 @@ public class BeaconRestApiTypes {
public static final ParameterMetadata<SpecMilestone> ETH_CONSENSUS_VERSION_TYPE =
new ParameterMetadata<>(HEADER_CONSENSUS_VERSION, MILESTONE_TYPE);

@SuppressWarnings("unchecked")
public static DeserializableTypeDefinition<Attestation> electraAttestationTypeDef(
final SchemaDefinitionCache schemaDefinitionCache) {
return (DeserializableTypeDefinition<Attestation>)
schemaDefinitionCache
.getSchemaDefinition(SpecMilestone.ELECTRA)
.getAttestationSchema()
.getJsonTypeDefinition();
}

@SuppressWarnings("unchecked")
public static DeserializableTypeDefinition<Attestation> phase0AttestationTypeDef(
final SchemaDefinitionCache schemaDefinitionCache) {
return (DeserializableTypeDefinition<Attestation>)
schemaDefinitionCache
.getSchemaDefinition(SpecMilestone.PHASE0)
.getAttestationSchema()
.getJsonTypeDefinition();
}

@SuppressWarnings("JavaCase")
public enum BroadcastValidationParameter {
gossip,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,15 @@
import tech.pegasys.teku.api.ChainDataProvider;
import tech.pegasys.teku.api.DataProvider;
import tech.pegasys.teku.api.schema.Version;
import tech.pegasys.teku.beaconrestapi.BeaconRestApiTypes;
import tech.pegasys.teku.infrastructure.async.SafeFuture;
import tech.pegasys.teku.infrastructure.json.types.DeserializableTypeDefinition;
import tech.pegasys.teku.infrastructure.json.types.SerializableOneOfTypeDefinition;
import tech.pegasys.teku.infrastructure.json.types.SerializableOneOfTypeDefinitionBuilder;
import tech.pegasys.teku.infrastructure.json.types.SerializableTypeDefinition;
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.SpecMilestone;
import tech.pegasys.teku.spec.datastructures.metadata.ObjectAndMetaData;
import tech.pegasys.teku.spec.datastructures.operations.Attestation;
import tech.pegasys.teku.spec.schemas.SchemaDefinitionCache;
Expand Down Expand Up @@ -92,24 +91,15 @@ public void handleRequest(final RestApiRequest request) throws JsonProcessingExc
@SuppressWarnings("unchecked")
private static SerializableTypeDefinition<ObjectAndMetaData<List<Attestation>>> getResponseType(
final SchemaDefinitionCache schemaDefinitionCache) {
final DeserializableTypeDefinition<Attestation> electraAttestationTypeDef =
(DeserializableTypeDefinition<Attestation>)
schemaDefinitionCache
.getSchemaDefinition(SpecMilestone.ELECTRA)
.getAttestationSchema()
.getJsonTypeDefinition();

final DeserializableTypeDefinition<Attestation> phase0AttestationTypeDef =
(DeserializableTypeDefinition<Attestation>)
schemaDefinitionCache
.getSchemaDefinition(SpecMilestone.PHASE0)
.getAttestationSchema()
.getJsonTypeDefinition();

final SerializableOneOfTypeDefinition<List<Attestation>> oneOfTypeDefinition =
new SerializableOneOfTypeDefinitionBuilder<List<Attestation>>()
.withType(electraAttestationsPredicate(), listOf(electraAttestationTypeDef))
.withType(phase0AttestationsPredicate(), listOf(phase0AttestationTypeDef))
.withType(
electraAttestationsPredicate(),
listOf(BeaconRestApiTypes.electraAttestationTypeDef(schemaDefinitionCache)))
.withType(
phase0AttestationsPredicate(),
listOf(BeaconRestApiTypes.phase0AttestationTypeDef(schemaDefinitionCache)))
.build();

return SerializableTypeDefinition.<ObjectAndMetaData<List<Attestation>>>object()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
import tech.pegasys.teku.api.DataProvider;
import tech.pegasys.teku.api.ValidatorDataProvider;
import tech.pegasys.teku.api.schema.Version;
import tech.pegasys.teku.beaconrestapi.BeaconRestApiTypes;
import tech.pegasys.teku.infrastructure.async.SafeFuture;
import tech.pegasys.teku.infrastructure.http.HttpStatusCodes;
import tech.pegasys.teku.infrastructure.json.types.DeserializableTypeDefinition;
import tech.pegasys.teku.infrastructure.json.types.SerializableOneOfTypeDefinition;
import tech.pegasys.teku.infrastructure.json.types.SerializableOneOfTypeDefinitionBuilder;
import tech.pegasys.teku.infrastructure.json.types.SerializableTypeDefinition;
Expand All @@ -39,7 +39,6 @@
import tech.pegasys.teku.infrastructure.restapi.endpoints.RestApiEndpoint;
import tech.pegasys.teku.infrastructure.restapi.endpoints.RestApiRequest;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.spec.SpecMilestone;
import tech.pegasys.teku.spec.datastructures.metadata.ObjectAndMetaData;
import tech.pegasys.teku.spec.datastructures.operations.Attestation;
import tech.pegasys.teku.spec.schemas.SchemaDefinitionCache;
Expand Down Expand Up @@ -105,24 +104,14 @@ public void handleRequest(final RestApiRequest request) throws JsonProcessingExc
private static SerializableTypeDefinition<ObjectAndMetaData<Attestation>> getResponseType(
final SchemaDefinitionCache schemaDefinitionCache) {

final DeserializableTypeDefinition<Attestation> electraAttestationTypeDef =
(DeserializableTypeDefinition<Attestation>)
schemaDefinitionCache
.getSchemaDefinition(SpecMilestone.ELECTRA)
.getAttestationSchema()
.getJsonTypeDefinition();

final DeserializableTypeDefinition<Attestation> phase0AttestationTypeDef =
(DeserializableTypeDefinition<Attestation>)
schemaDefinitionCache
.getSchemaDefinition(SpecMilestone.PHASE0)
.getAttestationSchema()
.getJsonTypeDefinition();

final SerializableOneOfTypeDefinition<Attestation> oneOfTypeDefinition =
new SerializableOneOfTypeDefinitionBuilder<Attestation>()
.withType(electraAttestationPredicate(), electraAttestationTypeDef)
.withType(phase0AttestationPredicate(), phase0AttestationTypeDef)
.withType(
electraAttestationPredicate(),
BeaconRestApiTypes.electraAttestationTypeDef(schemaDefinitionCache))
.withType(
phase0AttestationPredicate(),
BeaconRestApiTypes.phase0AttestationTypeDef(schemaDefinitionCache))
.build();

return SerializableTypeDefinition.<ObjectAndMetaData<Attestation>>object()
Expand All @@ -139,6 +128,6 @@ private static Predicate<Attestation> phase0AttestationPredicate() {

private static Predicate<Attestation> electraAttestationPredicate() {
// Only once we are in Electra attestations will have committee bits
return attestation -> attestation.requiresCommitteeBits();
return Attestation::requiresCommitteeBits;
}
}

0 comments on commit eb4f50a

Please sign in to comment.