Skip to content

Commit

Permalink
Add schema registry in schema definitions (#8615)
Browse files Browse the repository at this point in the history
  • Loading branch information
tbenr authored Sep 27, 2024
1 parent 080b5a5 commit 3064bc9
Show file tree
Hide file tree
Showing 15 changed files with 189 additions and 118 deletions.
4 changes: 3 additions & 1 deletion ethereum/spec/src/main/java/tech/pegasys/teku/spec/Spec.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
import tech.pegasys.teku.spec.logic.common.util.SyncCommitteeUtil;
import tech.pegasys.teku.spec.logic.versions.bellatrix.block.OptimisticExecutionPayloadExecutor;
import tech.pegasys.teku.spec.schemas.SchemaDefinitions;
import tech.pegasys.teku.spec.schemas.registry.SchemaRegistryBuilder;

public class Spec {
private final Map<SpecMilestone, SpecVersion> specVersions;
Expand All @@ -122,9 +123,10 @@ private Spec(
static Spec create(final SpecConfig config, final SpecMilestone highestMilestoneSupported) {
final Map<SpecMilestone, SpecVersion> specVersions = new EnumMap<>(SpecMilestone.class);
final ForkSchedule.Builder forkScheduleBuilder = ForkSchedule.builder();
final SchemaRegistryBuilder schemaRegistryBuilder = SchemaRegistryBuilder.create();

for (SpecMilestone milestone : SpecMilestone.getMilestonesUpTo(highestMilestoneSupported)) {
SpecVersion.create(milestone, config)
SpecVersion.create(milestone, config, schemaRegistryBuilder)
.ifPresent(
milestoneSpec -> {
forkScheduleBuilder.addNextMilestone(milestoneSpec);
Expand Down
79 changes: 60 additions & 19 deletions ethereum/spec/src/main/java/tech/pegasys/teku/spec/SpecVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import tech.pegasys.teku.spec.schemas.SchemaDefinitionsDeneb;
import tech.pegasys.teku.spec.schemas.SchemaDefinitionsElectra;
import tech.pegasys.teku.spec.schemas.SchemaDefinitionsPhase0;
import tech.pegasys.teku.spec.schemas.registry.SchemaRegistry;
import tech.pegasys.teku.spec.schemas.registry.SchemaRegistryBuilder;

public class SpecVersion extends DelegatingSpecLogic {
private final SpecMilestone milestone;
Expand All @@ -55,54 +57,93 @@ private SpecVersion(
}

public static Optional<SpecVersion> create(
final SpecMilestone milestone, final SpecConfig specConfig) {
final SpecMilestone milestone,
final SpecConfig specConfig,
final SchemaRegistryBuilder schemaRegistryBuilder) {

return switch (milestone) {
case PHASE0 -> Optional.of(createPhase0(specConfig));
case ALTAIR -> specConfig.toVersionAltair().map(SpecVersion::createAltair);
case BELLATRIX -> specConfig.toVersionBellatrix().map(SpecVersion::createBellatrix);
case CAPELLA -> specConfig.toVersionCapella().map(SpecVersion::createCapella);
case DENEB -> specConfig.toVersionDeneb().map(SpecVersion::createDeneb);
case ELECTRA -> specConfig.toVersionElectra().map(SpecVersion::createElectra);
case PHASE0 -> Optional.of(createPhase0(specConfig, schemaRegistryBuilder));
case ALTAIR ->
specConfig
.toVersionAltair()
.map(specConfigAltair -> createAltair(specConfigAltair, schemaRegistryBuilder));
case BELLATRIX ->
specConfig
.toVersionBellatrix()
.map(
specConfigBellatrix ->
createBellatrix(specConfigBellatrix, schemaRegistryBuilder));
case CAPELLA ->
specConfig
.toVersionCapella()
.map(specConfigCapella -> createCapella(specConfigCapella, schemaRegistryBuilder));
case DENEB ->
specConfig
.toVersionDeneb()
.map(specConfigDeneb -> createDeneb(specConfigDeneb, schemaRegistryBuilder));
case ELECTRA ->
specConfig
.toVersionElectra()
.map(specConfigElectra -> createElectra(specConfigElectra, schemaRegistryBuilder));
};
}

static SpecVersion createPhase0(final SpecConfig specConfig) {
final SchemaDefinitions schemaDefinitions = new SchemaDefinitionsPhase0(specConfig);
static SpecVersion createPhase0(
final SpecConfig specConfig, final SchemaRegistryBuilder schemaRegistryBuilder) {
final SchemaRegistry schemaRegistry =
schemaRegistryBuilder.build(SpecMilestone.PHASE0, specConfig);
final SchemaDefinitions schemaDefinitions = new SchemaDefinitionsPhase0(schemaRegistry);
final SpecLogic specLogic =
SpecLogicPhase0.create(specConfig, schemaDefinitions, SYSTEM_TIME_PROVIDER);
return new SpecVersion(SpecMilestone.PHASE0, specConfig, schemaDefinitions, specLogic);
}

static SpecVersion createAltair(final SpecConfigAltair specConfig) {
final SchemaDefinitionsAltair schemaDefinitions = new SchemaDefinitionsAltair(specConfig);
static SpecVersion createAltair(
final SpecConfigAltair specConfig, final SchemaRegistryBuilder schemaRegistryBuilder) {
final SchemaRegistry schemaRegistry =
schemaRegistryBuilder.build(SpecMilestone.ALTAIR, specConfig);
final SchemaDefinitionsAltair schemaDefinitions = new SchemaDefinitionsAltair(schemaRegistry);
final SpecLogic specLogic =
SpecLogicAltair.create(specConfig, schemaDefinitions, SYSTEM_TIME_PROVIDER);
return new SpecVersion(SpecMilestone.ALTAIR, specConfig, schemaDefinitions, specLogic);
}

static SpecVersion createBellatrix(final SpecConfigBellatrix specConfig) {
final SchemaDefinitionsBellatrix schemaDefinitions = new SchemaDefinitionsBellatrix(specConfig);
static SpecVersion createBellatrix(
final SpecConfigBellatrix specConfig, final SchemaRegistryBuilder schemaRegistryBuilder) {
final SchemaRegistry schemaRegistry =
schemaRegistryBuilder.build(SpecMilestone.BELLATRIX, specConfig);
final SchemaDefinitionsBellatrix schemaDefinitions =
new SchemaDefinitionsBellatrix(schemaRegistry);
final SpecLogic specLogic =
SpecLogicBellatrix.create(specConfig, schemaDefinitions, SYSTEM_TIME_PROVIDER);
return new SpecVersion(SpecMilestone.BELLATRIX, specConfig, schemaDefinitions, specLogic);
}

static SpecVersion createCapella(final SpecConfigCapella specConfig) {
final SchemaDefinitionsCapella schemaDefinitions = new SchemaDefinitionsCapella(specConfig);
static SpecVersion createCapella(
final SpecConfigCapella specConfig, final SchemaRegistryBuilder schemaRegistryBuilder) {
final SchemaRegistry schemaRegistry =
schemaRegistryBuilder.build(SpecMilestone.CAPELLA, specConfig);
final SchemaDefinitionsCapella schemaDefinitions = new SchemaDefinitionsCapella(schemaRegistry);
final SpecLogicCapella specLogic =
SpecLogicCapella.create(specConfig, schemaDefinitions, SYSTEM_TIME_PROVIDER);
return new SpecVersion(SpecMilestone.CAPELLA, specConfig, schemaDefinitions, specLogic);
}

static SpecVersion createDeneb(final SpecConfigDeneb specConfig) {
final SchemaDefinitionsDeneb schemaDefinitions = new SchemaDefinitionsDeneb(specConfig);
static SpecVersion createDeneb(
final SpecConfigDeneb specConfig, final SchemaRegistryBuilder schemaRegistryBuilder) {
final SchemaRegistry schemaRegistry =
schemaRegistryBuilder.build(SpecMilestone.DENEB, specConfig);
final SchemaDefinitionsDeneb schemaDefinitions = new SchemaDefinitionsDeneb(schemaRegistry);
final SpecLogicDeneb specLogic =
SpecLogicDeneb.create(specConfig, schemaDefinitions, SYSTEM_TIME_PROVIDER);
return new SpecVersion(SpecMilestone.DENEB, specConfig, schemaDefinitions, specLogic);
}

static SpecVersion createElectra(final SpecConfigElectra specConfig) {
final SchemaDefinitionsElectra schemaDefinitions = new SchemaDefinitionsElectra(specConfig);
static SpecVersion createElectra(
final SpecConfigElectra specConfig, final SchemaRegistryBuilder schemaRegistryBuilder) {
final SchemaRegistry schemaRegistry =
schemaRegistryBuilder.build(SpecMilestone.ELECTRA, specConfig);
final SchemaDefinitionsElectra schemaDefinitions = new SchemaDefinitionsElectra(schemaRegistry);
final SpecLogicElectra specLogic =
SpecLogicElectra.create(specConfig, schemaDefinitions, SYSTEM_TIME_PROVIDER);
return new SpecVersion(SpecMilestone.ELECTRA, specConfig, schemaDefinitions, specLogic);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
import tech.pegasys.teku.spec.constants.NetworkConstants;
import tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.BeaconBlocksByRootRequestMessage;
import tech.pegasys.teku.spec.datastructures.state.HistoricalBatch.HistoricalBatchSchema;
import tech.pegasys.teku.spec.schemas.registry.SchemaRegistry;

public abstract class AbstractSchemaDefinitions implements SchemaDefinitions {
protected SchemaRegistry schemaRegistry;

final SszBitvectorSchema<SszBitvector> attnetsENRFieldSchema;
final SszBitvectorSchema<SszBitvector> syncnetsENRFieldSchema =
Expand All @@ -29,16 +31,25 @@ public abstract class AbstractSchemaDefinitions implements SchemaDefinitions {
private final BeaconBlocksByRootRequestMessage.BeaconBlocksByRootRequestMessageSchema
beaconBlocksByRootRequestMessageSchema;

public AbstractSchemaDefinitions(final SpecConfig specConfig) {
this.historicalBatchSchema = new HistoricalBatchSchema(specConfig.getSlotsPerHistoricalRoot());
public AbstractSchemaDefinitions(final SchemaRegistry schemaRegistry) {
this.schemaRegistry = schemaRegistry;
this.historicalBatchSchema =
new HistoricalBatchSchema(schemaRegistry.getSpecConfig().getSlotsPerHistoricalRoot());

this.beaconBlocksByRootRequestMessageSchema =
new BeaconBlocksByRootRequestMessage.BeaconBlocksByRootRequestMessageSchema(specConfig);
this.attnetsENRFieldSchema = SszBitvectorSchema.create(specConfig.getAttestationSubnetCount());
new BeaconBlocksByRootRequestMessage.BeaconBlocksByRootRequestMessageSchema(
schemaRegistry.getSpecConfig());
this.attnetsENRFieldSchema =
SszBitvectorSchema.create(schemaRegistry.getSpecConfig().getAttestationSubnetCount());
}

abstract long getMaxValidatorPerAttestation(SpecConfig specConfig);

@Override
public SchemaRegistry getSchemaRegistry() {
return schemaRegistry;
}

@Override
public SszBitvectorSchema<SszBitvector> getAttnetsENRFieldSchema() {
return attnetsENRFieldSchema;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import tech.pegasys.teku.spec.Spec;
import tech.pegasys.teku.spec.SpecMilestone;
import tech.pegasys.teku.spec.SpecVersion;
import tech.pegasys.teku.spec.schemas.registry.SchemaRegistryBuilder;

public class SchemaDefinitionCache {
private final Spec spec;
Expand All @@ -46,7 +47,8 @@ private SchemaDefinitions createSchemaDefinition(final SpecMilestone milestone)
if (specVersion != null) {
return specVersion.getSchemaDefinitions();
}
return SpecVersion.create(milestone, spec.getGenesisSpecConfig())
return SpecVersion.create(
milestone, spec.getGenesisSpecConfig(), SchemaRegistryBuilder.create())
.orElseThrow(
() ->
new IllegalArgumentException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import tech.pegasys.teku.spec.datastructures.operations.SignedAggregateAndProof.SignedAggregateAndProofSchema;
import tech.pegasys.teku.spec.datastructures.state.HistoricalBatch.HistoricalBatchSchema;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconStateSchema;
import tech.pegasys.teku.spec.schemas.registry.SchemaRegistry;

public interface SchemaDefinitions {

Expand Down Expand Up @@ -89,6 +90,9 @@ public interface SchemaDefinitions {
@NonSchema
BeaconBlockBodyBuilder createBeaconBlockBodyBuilder();

@NonSchema
SchemaRegistry getSchemaRegistry();

@NonSchema
default Optional<SchemaDefinitionsAltair> toVersionAltair() {
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.BeaconStateAltair;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.BeaconStateSchemaAltair;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.MutableBeaconStateAltair;
import tech.pegasys.teku.spec.schemas.registry.SchemaRegistry;

public class SchemaDefinitionsAltair extends AbstractSchemaDefinitions {
private final IndexedAttestationSchema<IndexedAttestation> indexedAttestationSchema;
Expand All @@ -72,8 +73,9 @@ public class SchemaDefinitionsAltair extends AbstractSchemaDefinitions {
private final LightClientUpdateSchema lightClientUpdateSchema;
private final LightClientUpdateResponseSchema lightClientUpdateResponseSchema;

public SchemaDefinitionsAltair(final SpecConfigAltair specConfig) {
super(specConfig);
public SchemaDefinitionsAltair(final SchemaRegistry schemaRegistry) {
super(schemaRegistry);
final SpecConfigAltair specConfig = SpecConfigAltair.required(schemaRegistry.getSpecConfig());
this.indexedAttestationSchema =
new IndexedAttestationPhase0Schema(getMaxValidatorPerAttestation(specConfig))
.castTypeToIndexedAttestationSchema();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.bellatrix.BeaconStateBellatrix;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.bellatrix.BeaconStateSchemaBellatrix;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.bellatrix.MutableBeaconStateBellatrix;
import tech.pegasys.teku.spec.schemas.registry.SchemaRegistry;

public class SchemaDefinitionsBellatrix extends SchemaDefinitionsAltair {
private final BeaconStateSchemaBellatrix beaconStateSchema;
Expand All @@ -52,8 +53,10 @@ public class SchemaDefinitionsBellatrix extends SchemaDefinitionsAltair {
private final BuilderBidSchema<?> builderBidSchema;
private final SignedBuilderBidSchema signedBuilderBidSchema;

public SchemaDefinitionsBellatrix(final SpecConfigBellatrix specConfig) {
super(specConfig);
public SchemaDefinitionsBellatrix(final SchemaRegistry schemaRegistry) {
super(schemaRegistry);
final SpecConfigBellatrix specConfig =
SpecConfigBellatrix.required(schemaRegistry.getSpecConfig());
final long maxValidatorsPerAttestation = getMaxValidatorPerAttestation(specConfig);
this.beaconStateSchema = BeaconStateSchemaBellatrix.create(specConfig);
this.executionPayloadHeaderSchema = beaconStateSchema.getLastExecutionPayloadHeaderSchema();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.capella.BeaconStateSchemaCapella;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.capella.MutableBeaconStateCapella;
import tech.pegasys.teku.spec.datastructures.state.versions.capella.HistoricalSummary;
import tech.pegasys.teku.spec.schemas.registry.SchemaRegistry;

public class SchemaDefinitionsCapella extends SchemaDefinitionsBellatrix {

Expand All @@ -70,8 +71,9 @@ public class SchemaDefinitionsCapella extends SchemaDefinitionsBellatrix {

private final HistoricalSummary.HistoricalSummarySchema historicalSummarySchema;

public SchemaDefinitionsCapella(final SpecConfigCapella specConfig) {
super(specConfig);
public SchemaDefinitionsCapella(final SchemaRegistry schemaRegistry) {
super(schemaRegistry);
final SpecConfigCapella specConfig = SpecConfigCapella.required(schemaRegistry.getSpecConfig());
this.executionPayloadSchemaCapella = new ExecutionPayloadSchemaCapella(specConfig);
this.blsToExecutionChangeSchema = new BlsToExecutionChangeSchema();
this.signedBlsToExecutionChangeSchema = new SignedBlsToExecutionChangeSchema();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.deneb.BeaconStateDeneb;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.deneb.BeaconStateSchemaDeneb;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.deneb.MutableBeaconStateDeneb;
import tech.pegasys.teku.spec.schemas.registry.SchemaRegistry;

public class SchemaDefinitionsDeneb extends SchemaDefinitionsCapella {

Expand Down Expand Up @@ -82,8 +83,9 @@ public class SchemaDefinitionsDeneb extends SchemaDefinitionsCapella {
private final ExecutionPayloadAndBlobsBundleSchema executionPayloadAndBlobsBundleSchema;
private final BlobSidecarsByRootRequestMessageSchema blobSidecarsByRootRequestMessageSchema;

public SchemaDefinitionsDeneb(final SpecConfigDeneb specConfig) {
super(specConfig);
public SchemaDefinitionsDeneb(final SchemaRegistry schemaRegistry) {
super(schemaRegistry);
final SpecConfigDeneb specConfig = SpecConfigDeneb.required(schemaRegistry.getSpecConfig());
this.executionPayloadSchemaDeneb = new ExecutionPayloadSchemaDeneb(specConfig);

this.beaconStateSchema = BeaconStateSchemaDeneb.create(specConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import tech.pegasys.teku.spec.datastructures.state.versions.electra.PendingBalanceDeposit;
import tech.pegasys.teku.spec.datastructures.state.versions.electra.PendingConsolidation;
import tech.pegasys.teku.spec.datastructures.state.versions.electra.PendingPartialWithdrawal;
import tech.pegasys.teku.spec.schemas.registry.SchemaRegistry;

public class SchemaDefinitionsElectra extends SchemaDefinitionsDeneb {

Expand Down Expand Up @@ -101,8 +102,9 @@ public class SchemaDefinitionsElectra extends SchemaDefinitionsDeneb {
pendingPartialWithdrawalSchema;
private final PendingConsolidation.PendingConsolidationSchema pendingConsolidationSchema;

public SchemaDefinitionsElectra(final SpecConfigElectra specConfig) {
super(specConfig);
public SchemaDefinitionsElectra(final SchemaRegistry schemaRegistry) {
super(schemaRegistry);
final SpecConfigElectra specConfig = SpecConfigElectra.required(schemaRegistry.getSpecConfig());

final long maxValidatorsPerAttestation = getMaxValidatorPerAttestation(specConfig);
this.indexedAttestationSchema =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import tech.pegasys.teku.spec.datastructures.operations.versions.phase0.IndexedAttestationPhase0Schema;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconStateSchema;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.phase0.BeaconStateSchemaPhase0;
import tech.pegasys.teku.spec.schemas.registry.SchemaRegistry;

public class SchemaDefinitionsPhase0 extends AbstractSchemaDefinitions {
private final IndexedAttestationSchema<IndexedAttestation> indexedAttestationSchema;
Expand All @@ -52,8 +53,9 @@ public class SchemaDefinitionsPhase0 extends AbstractSchemaDefinitions {
private final BeaconBlockSchema beaconBlockSchema;
private final SignedBeaconBlockSchema signedBeaconBlockSchema;

public SchemaDefinitionsPhase0(final SpecConfig specConfig) {
super(specConfig);
public SchemaDefinitionsPhase0(final SchemaRegistry schemaRegistry) {
super(schemaRegistry);
final SpecConfig specConfig = schemaRegistry.getSpecConfig();
this.indexedAttestationSchema =
new IndexedAttestationPhase0Schema(getMaxValidatorPerAttestation(specConfig))
.castTypeToIndexedAttestationSchema();
Expand Down
Loading

0 comments on commit 3064bc9

Please sign in to comment.