Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate all schemas in AbstractSchemaDefinitions #8726

Merged
merged 2 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<SszBitvector> attnetsENRFieldSchema;
final SszBitvectorSchema<SszBitvector> syncnetsENRFieldSchema =
SszBitvectorSchema.create(NetworkConstants.SYNC_COMMITTEE_SUBNET_COUNT);
private final SszBitvectorSchema<SszBitvector> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,23 @@ public String toString() {
}
}

static <T> Builder<T> providerBuilder(final SchemaId<T> schemaId) {
return new Builder<>(schemaId);
static <T> Builder<T> constantProviderBuilder(final SchemaId<T> schemaId) {
return new Builder<>(schemaId, true);
}

static <T> Builder<T> variableProviderBuilder(final SchemaId<T> schemaId) {
return new Builder<>(schemaId, false);
}

static class Builder<T> {
private final SchemaId<T> schemaId;
private final boolean isConstant;
final List<SchemaProviderCreator<T>> schemaProviderCreators = new ArrayList<>();
private SpecMilestone untilMilestone = SpecMilestone.getHighestMilestone();
private boolean isConstant = false;

private Builder(final SchemaId<T> schemaId) {
private Builder(final SchemaId<T> schemaId, final boolean isConstant) {
this.schemaId = schemaId;
this.isConstant = isConstant;
}

public Builder<T> withCreator(
Expand All @@ -134,11 +139,6 @@ public Builder<T> until(final SpecMilestone untilMilestone) {
return this;
}

public Builder<T> constant() {
this.isConstant = true;
return this;
}

public BaseSchemaProvider<T> build() {
checkArgument(
!schemaProviderCreators.isEmpty(), "There should be at least 1 creator for %s", schemaId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -39,23 +44,49 @@ public static SchemaRegistryBuilder create() {
return new SchemaRegistryBuilder()
// PHASE0
.addProvider(createAttnetsENRFieldSchemaProvider())
.addProvider(createSyncnetsENRFieldSchemaProvider())
.addProvider(createBeaconBlocksByRootRequestMessageSchemaProvider())
.addProvider(createHistoricalBatchSchemaProvider())
.addProvider(createAttestationSchemaProvider());
}

private static SchemaProvider<SszBitvectorSchema<SszBitvector>>
createAttnetsENRFieldSchemaProvider() {
return providerBuilder(ATTNETS_ENR_FIELD_SCHEMA)
.constant()
private static SchemaProvider<?> createAttnetsENRFieldSchemaProvider() {
return constantProviderBuilder(ATTNETS_ENR_FIELD_SCHEMA)
.withCreator(
SpecMilestone.PHASE0,
(registry, specConfig) ->
SszBitvectorSchema.create(specConfig.getAttestationSubnetCount()))
.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<AttestationSchema<Attestation>> createAttestationSchemaProvider() {
return providerBuilder(ATTESTATION_SCHEMA)
.constant()
return constantProviderBuilder(ATTESTATION_SCHEMA)
.withCreator(
SpecMilestone.PHASE0,
(registry, specConfig) ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,6 +35,13 @@ public class SchemaTypes {
// PHASE0
public static final SchemaId<SszBitvectorSchema<SszBitvector>> ATTNETS_ENR_FIELD_SCHEMA =
create("ATTNETS_ENR_FIELD_SCHEMA");
public static final SchemaId<SszBitvectorSchema<SszBitvector>> SYNCNETS_ENR_FIELD_SCHEMA =
create("SYNCNETS_ENR_FIELD_SCHEMA");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are we going to reference by strings elsewhere? Wonder if we should use const's or an enum or something so that we're not typing the string a bunch if we are

Copy link
Contributor Author

@tbenr tbenr Oct 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ths string are not referenced elsewhere, they are just used internally (they are exposed in error logs to be able to identify where the error is in the schema definitions)
It is not possible to move to an enum (at least i haven't found any good alternative) since we need to capture the type here.

I attempted this trick to move the string as an enum but it doesn't not worth it IMO.
image

Btw strings are enforced to be equal to the const via unit test.

public static final SchemaId<HistoricalBatchSchema> HISTORICAL_BATCH_SCHEMA =
create("HISTORICAL_BATCH_SCHEMA");
public static final SchemaId<BeaconBlocksByRootRequestMessageSchema>
BEACON_BLOCKS_BY_ROOT_REQUEST_MESSAGE_SCHEMA =
create("BEACON_BLOCKS_BY_ROOT_REQUEST_MESSAGE_SCHEMA");

public static final SchemaId<AttestationSchema<Attestation>> ATTESTATION_SCHEMA =
create("ATTESTATION_SCHEMA");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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)
Expand Down Expand Up @@ -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();
Expand All @@ -117,15 +117,15 @@ 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");
}

@Test
void shouldThrowWhenAskingForAnUnsupportedMilestone() {
final SchemaProvider<?> provider =
providerBuilder(STRING_SCHEMA_ID)
variableProviderBuilder(STRING_SCHEMA_ID)
.withCreator(ALTAIR, (r, c) -> "TestSchemaAltair")
.until(ALTAIR)
.build();
Expand All @@ -141,15 +141,15 @@ 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)
.hasMessageStartingWith("Creator's milestones must added in strict ascending order");

assertThatThrownBy(
() ->
providerBuilder(STRING_SCHEMA_ID)
variableProviderBuilder(STRING_SCHEMA_ID)
.withCreator(ALTAIR, (r, c) -> "TestSchema")
.withCreator(PHASE0, (r, c) -> "TestSchema"))
.isInstanceOf(IllegalArgumentException.class)
Expand All @@ -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)
Expand Down