From 95955b443f0f94020d333f1320e3e79ac5474121 Mon Sep 17 00:00:00 2001 From: Enrico Del Fante Date: Mon, 14 Oct 2024 17:19:58 +0200 Subject: [PATCH] migrate all schemas in AbstractSchemaDefinitions --- .../schemas/AbstractSchemaDefinitions.java | 24 +++++----- .../schemas/registry/BaseSchemaProvider.java | 18 +++---- .../registry/SchemaRegistryBuilder.java | 47 +++++++++++++++---- .../spec/schemas/registry/SchemaTypes.java | 9 ++++ .../registry/BaseSchemaProviderTest.java | 20 ++++---- 5 files changed, 79 insertions(+), 39 deletions(-) diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/AbstractSchemaDefinitions.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/AbstractSchemaDefinitions.java index 91145d621d4..20ed0773748 100644 --- a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/AbstractSchemaDefinitions.java +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/AbstractSchemaDefinitions.java @@ -13,34 +13,34 @@ package tech.pegasys.teku.spec.schemas; +import static tech.pegasys.teku.spec.schemas.registry.SchemaTypes.ATTNETS_ENR_FIELD_SCHEMA; +import static tech.pegasys.teku.spec.schemas.registry.SchemaTypes.BEACON_BLOCKS_BY_ROOT_REQUEST_MESSAGE_SCHEMA; +import static tech.pegasys.teku.spec.schemas.registry.SchemaTypes.HISTORICAL_BATCH_SCHEMA; +import static tech.pegasys.teku.spec.schemas.registry.SchemaTypes.SYNCNETS_ENR_FIELD_SCHEMA; + import tech.pegasys.teku.infrastructure.ssz.collections.SszBitvector; import tech.pegasys.teku.infrastructure.ssz.schema.collections.SszBitvectorSchema; import tech.pegasys.teku.spec.config.SpecConfig; -import tech.pegasys.teku.spec.constants.NetworkConstants; import tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.BeaconBlocksByRootRequestMessage; +import tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.BeaconBlocksByRootRequestMessage.BeaconBlocksByRootRequestMessageSchema; import tech.pegasys.teku.spec.datastructures.state.HistoricalBatch.HistoricalBatchSchema; import tech.pegasys.teku.spec.schemas.registry.SchemaRegistry; -import tech.pegasys.teku.spec.schemas.registry.SchemaTypes; public abstract class AbstractSchemaDefinitions implements SchemaDefinitions { protected SchemaRegistry schemaRegistry; final SszBitvectorSchema attnetsENRFieldSchema; - final SszBitvectorSchema syncnetsENRFieldSchema = - SszBitvectorSchema.create(NetworkConstants.SYNC_COMMITTEE_SUBNET_COUNT); + private final SszBitvectorSchema syncnetsENRFieldSchema; private final HistoricalBatchSchema historicalBatchSchema; - private final BeaconBlocksByRootRequestMessage.BeaconBlocksByRootRequestMessageSchema - beaconBlocksByRootRequestMessageSchema; + private final BeaconBlocksByRootRequestMessageSchema beaconBlocksByRootRequestMessageSchema; public AbstractSchemaDefinitions(final SchemaRegistry schemaRegistry) { this.schemaRegistry = schemaRegistry; - this.historicalBatchSchema = - new HistoricalBatchSchema(schemaRegistry.getSpecConfig().getSlotsPerHistoricalRoot()); - + this.historicalBatchSchema = schemaRegistry.get(HISTORICAL_BATCH_SCHEMA); this.beaconBlocksByRootRequestMessageSchema = - new BeaconBlocksByRootRequestMessage.BeaconBlocksByRootRequestMessageSchema( - schemaRegistry.getSpecConfig()); - this.attnetsENRFieldSchema = schemaRegistry.get(SchemaTypes.ATTNETS_ENR_FIELD_SCHEMA); + schemaRegistry.get(BEACON_BLOCKS_BY_ROOT_REQUEST_MESSAGE_SCHEMA); + this.syncnetsENRFieldSchema = schemaRegistry.get(SYNCNETS_ENR_FIELD_SCHEMA); + this.attnetsENRFieldSchema = schemaRegistry.get(ATTNETS_ENR_FIELD_SCHEMA); } abstract long getMaxValidatorPerAttestation(SpecConfig specConfig); diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/registry/BaseSchemaProvider.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/registry/BaseSchemaProvider.java index 95eeceda456..4411165fa97 100644 --- a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/registry/BaseSchemaProvider.java +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/registry/BaseSchemaProvider.java @@ -102,18 +102,23 @@ public String toString() { } } - static Builder providerBuilder(final SchemaId schemaId) { - return new Builder<>(schemaId); + static Builder constantProviderBuilder(final SchemaId schemaId) { + return new Builder<>(schemaId, true); + } + + static Builder variableProviderBuilder(final SchemaId schemaId) { + return new Builder<>(schemaId, false); } static class Builder { private final SchemaId schemaId; + private final boolean isConstant; final List> schemaProviderCreators = new ArrayList<>(); private SpecMilestone untilMilestone = SpecMilestone.getHighestMilestone(); - private boolean isConstant = false; - private Builder(final SchemaId schemaId) { + private Builder(final SchemaId schemaId, final boolean isConstant) { this.schemaId = schemaId; + this.isConstant = isConstant; } public Builder withCreator( @@ -134,11 +139,6 @@ public Builder until(final SpecMilestone untilMilestone) { return this; } - public Builder constant() { - this.isConstant = true; - return this; - } - public BaseSchemaProvider build() { checkArgument( !schemaProviderCreators.isEmpty(), "There should be at least 1 creator for %s", schemaId); diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/registry/SchemaRegistryBuilder.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/registry/SchemaRegistryBuilder.java index ddf5971ebf1..833990c1339 100644 --- a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/registry/SchemaRegistryBuilder.java +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/registry/SchemaRegistryBuilder.java @@ -13,21 +13,26 @@ package tech.pegasys.teku.spec.schemas.registry; -import static tech.pegasys.teku.spec.schemas.registry.BaseSchemaProvider.providerBuilder; +import static tech.pegasys.teku.spec.schemas.registry.BaseSchemaProvider.constantProviderBuilder; import static tech.pegasys.teku.spec.schemas.registry.SchemaTypes.ATTESTATION_SCHEMA; import static tech.pegasys.teku.spec.schemas.registry.SchemaTypes.ATTNETS_ENR_FIELD_SCHEMA; +import static tech.pegasys.teku.spec.schemas.registry.SchemaTypes.BEACON_BLOCKS_BY_ROOT_REQUEST_MESSAGE_SCHEMA; +import static tech.pegasys.teku.spec.schemas.registry.SchemaTypes.HISTORICAL_BATCH_SCHEMA; +import static tech.pegasys.teku.spec.schemas.registry.SchemaTypes.SYNCNETS_ENR_FIELD_SCHEMA; import com.google.common.annotations.VisibleForTesting; import java.util.HashSet; import java.util.Set; -import tech.pegasys.teku.infrastructure.ssz.collections.SszBitvector; import tech.pegasys.teku.infrastructure.ssz.schema.collections.SszBitvectorSchema; import tech.pegasys.teku.spec.SpecMilestone; import tech.pegasys.teku.spec.config.SpecConfig; +import tech.pegasys.teku.spec.constants.NetworkConstants; +import tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.BeaconBlocksByRootRequestMessage.BeaconBlocksByRootRequestMessageSchema; import tech.pegasys.teku.spec.datastructures.operations.Attestation; import tech.pegasys.teku.spec.datastructures.operations.AttestationSchema; import tech.pegasys.teku.spec.datastructures.operations.versions.electra.AttestationElectraSchema; import tech.pegasys.teku.spec.datastructures.operations.versions.phase0.AttestationPhase0Schema; +import tech.pegasys.teku.spec.datastructures.state.HistoricalBatch.HistoricalBatchSchema; import tech.pegasys.teku.spec.schemas.registry.SchemaTypes.SchemaId; public class SchemaRegistryBuilder { @@ -39,13 +44,14 @@ public static SchemaRegistryBuilder create() { return new SchemaRegistryBuilder() // PHASE0 .addProvider(createAttnetsENRFieldSchemaProvider()) + .addProvider(createSyncnetsENRFieldSchemaProvider()) + .addProvider(createBeaconBlocksByRootRequestMessageSchemaProvider()) + .addProvider(createHistoricalBatchSchemaProvider()) .addProvider(createAttestationSchemaProvider()); } - private static SchemaProvider> - createAttnetsENRFieldSchemaProvider() { - return providerBuilder(ATTNETS_ENR_FIELD_SCHEMA) - .constant() + private static SchemaProvider createAttnetsENRFieldSchemaProvider() { + return constantProviderBuilder(ATTNETS_ENR_FIELD_SCHEMA) .withCreator( SpecMilestone.PHASE0, (registry, specConfig) -> @@ -53,9 +59,34 @@ public static SchemaRegistryBuilder create() { .build(); } + private static SchemaProvider createSyncnetsENRFieldSchemaProvider() { + return constantProviderBuilder(SYNCNETS_ENR_FIELD_SCHEMA) + .withCreator( + SpecMilestone.PHASE0, + (registry, specConfig) -> + SszBitvectorSchema.create(NetworkConstants.SYNC_COMMITTEE_SUBNET_COUNT)) + .build(); + } + + private static SchemaProvider createBeaconBlocksByRootRequestMessageSchemaProvider() { + return constantProviderBuilder(BEACON_BLOCKS_BY_ROOT_REQUEST_MESSAGE_SCHEMA) + .withCreator( + SpecMilestone.PHASE0, + (registry, specConfig) -> new BeaconBlocksByRootRequestMessageSchema(specConfig)) + .build(); + } + + private static SchemaProvider createHistoricalBatchSchemaProvider() { + return constantProviderBuilder(HISTORICAL_BATCH_SCHEMA) + .withCreator( + SpecMilestone.PHASE0, + (registry, specConfig) -> + new HistoricalBatchSchema(specConfig.getSlotsPerHistoricalRoot())) + .build(); + } + private static SchemaProvider> createAttestationSchemaProvider() { - return providerBuilder(ATTESTATION_SCHEMA) - .constant() + return constantProviderBuilder(ATTESTATION_SCHEMA) .withCreator( SpecMilestone.PHASE0, (registry, specConfig) -> diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/registry/SchemaTypes.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/registry/SchemaTypes.java index de164ca7f7a..c1d830df20e 100644 --- a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/registry/SchemaTypes.java +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/registry/SchemaTypes.java @@ -23,8 +23,10 @@ import tech.pegasys.teku.spec.SpecMilestone; import tech.pegasys.teku.spec.datastructures.execution.ExecutionPayloadHeader; import tech.pegasys.teku.spec.datastructures.execution.ExecutionPayloadHeaderSchema; +import tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.BeaconBlocksByRootRequestMessage.BeaconBlocksByRootRequestMessageSchema; import tech.pegasys.teku.spec.datastructures.operations.Attestation; import tech.pegasys.teku.spec.datastructures.operations.AttestationSchema; +import tech.pegasys.teku.spec.datastructures.state.HistoricalBatch.HistoricalBatchSchema; import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState; import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconStateSchema; import tech.pegasys.teku.spec.datastructures.state.beaconstate.MutableBeaconState; @@ -33,6 +35,13 @@ public class SchemaTypes { // PHASE0 public static final SchemaId> ATTNETS_ENR_FIELD_SCHEMA = create("ATTNETS_ENR_FIELD_SCHEMA"); + public static final SchemaId> SYNCNETS_ENR_FIELD_SCHEMA = + create("SYNCNETS_ENR_FIELD_SCHEMA"); + public static final SchemaId HISTORICAL_BATCH_SCHEMA = + create("HISTORICAL_BATCH_SCHEMA"); + public static final SchemaId + BEACON_BLOCKS_BY_ROOT_REQUEST_MESSAGE_SCHEMA = + create("BEACON_BLOCKS_BY_ROOT_REQUEST_MESSAGE_SCHEMA"); public static final SchemaId> ATTESTATION_SCHEMA = create("ATTESTATION_SCHEMA"); diff --git a/ethereum/spec/src/test/java/tech/pegasys/teku/spec/schemas/registry/BaseSchemaProviderTest.java b/ethereum/spec/src/test/java/tech/pegasys/teku/spec/schemas/registry/BaseSchemaProviderTest.java index 54a5196219f..838c118fe3f 100644 --- a/ethereum/spec/src/test/java/tech/pegasys/teku/spec/schemas/registry/BaseSchemaProviderTest.java +++ b/ethereum/spec/src/test/java/tech/pegasys/teku/spec/schemas/registry/BaseSchemaProviderTest.java @@ -23,7 +23,8 @@ import static tech.pegasys.teku.spec.SpecMilestone.CAPELLA; import static tech.pegasys.teku.spec.SpecMilestone.DENEB; import static tech.pegasys.teku.spec.SpecMilestone.PHASE0; -import static tech.pegasys.teku.spec.schemas.registry.BaseSchemaProvider.providerBuilder; +import static tech.pegasys.teku.spec.schemas.registry.BaseSchemaProvider.constantProviderBuilder; +import static tech.pegasys.teku.spec.schemas.registry.BaseSchemaProvider.variableProviderBuilder; import org.junit.jupiter.api.Test; import tech.pegasys.teku.spec.SpecMilestone; @@ -38,7 +39,7 @@ class BaseSchemaProviderTest { @Test void shouldSupportContinuousUntilHighestMilestone() { final SchemaProvider provider = - providerBuilder(STRING_SCHEMA_ID) + variableProviderBuilder(STRING_SCHEMA_ID) .withCreator(ALTAIR, (r, c) -> "TestSchemaAltair") .withCreator(BELLATRIX, (r, c) -> "TestSchemaBellatrix") .build(); @@ -59,8 +60,7 @@ void shouldSupportContinuousUntilHighestMilestone() { @Test void shouldSupportContinuousConstantWithUntil() { final SchemaProvider provider = - providerBuilder(STRING_SCHEMA_ID) - .constant() + constantProviderBuilder(STRING_SCHEMA_ID) .withCreator(PHASE0, (r, c) -> "TestSchemaPhase0") .withCreator(BELLATRIX, (r, c) -> "TestSchemaBellatrix") .until(CAPELLA) @@ -90,7 +90,7 @@ void shouldSupportContinuousConstantWithUntil() { @Test void shouldSupportContinuousDefaultVariable() { final SchemaProvider provider = - providerBuilder(STRING_SCHEMA_ID) + variableProviderBuilder(STRING_SCHEMA_ID) .withCreator(PHASE0, (r, c) -> "TestSchema" + r.getMilestone()) .until(CAPELLA) .build(); @@ -117,7 +117,7 @@ void shouldSupportContinuousDefaultVariable() { @Test void shouldThrowWhenNoCreators() { - assertThatThrownBy(() -> providerBuilder(STRING_SCHEMA_ID).build()) + assertThatThrownBy(() -> variableProviderBuilder(STRING_SCHEMA_ID).build()) .isInstanceOf(IllegalArgumentException.class) .hasMessageStartingWith("There should be at least 1 creator"); } @@ -125,7 +125,7 @@ void shouldThrowWhenNoCreators() { @Test void shouldThrowWhenAskingForAnUnsupportedMilestone() { final SchemaProvider provider = - providerBuilder(STRING_SCHEMA_ID) + variableProviderBuilder(STRING_SCHEMA_ID) .withCreator(ALTAIR, (r, c) -> "TestSchemaAltair") .until(ALTAIR) .build(); @@ -141,7 +141,7 @@ void shouldThrowWhenAskingForAnUnsupportedMilestone() { void shouldThrowWhenNotAscendingMilestones() { assertThatThrownBy( () -> - providerBuilder(STRING_SCHEMA_ID) + variableProviderBuilder(STRING_SCHEMA_ID) .withCreator(PHASE0, (r, c) -> "TestSchema") .withCreator(PHASE0, (r, c) -> "TestSchema")) .isInstanceOf(IllegalArgumentException.class) @@ -149,7 +149,7 @@ void shouldThrowWhenNotAscendingMilestones() { assertThatThrownBy( () -> - providerBuilder(STRING_SCHEMA_ID) + variableProviderBuilder(STRING_SCHEMA_ID) .withCreator(ALTAIR, (r, c) -> "TestSchema") .withCreator(PHASE0, (r, c) -> "TestSchema")) .isInstanceOf(IllegalArgumentException.class) @@ -160,7 +160,7 @@ void shouldThrowWhenNotAscendingMilestones() { void shouldThrowWhenWithUntilIsPriorToMilestone() { assertThatThrownBy( () -> - providerBuilder(STRING_SCHEMA_ID) + variableProviderBuilder(STRING_SCHEMA_ID) .withCreator(PHASE0, (r, c) -> "TestSchema") .withCreator(CAPELLA, (r, c) -> "TestSchema") .until(ALTAIR)