Skip to content

Commit

Permalink
provide schema registry in schema definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
tbenr committed Sep 18, 2024
1 parent 394d70c commit 39e9961
Show file tree
Hide file tree
Showing 12 changed files with 125 additions and 54 deletions.
75 changes: 57 additions & 18 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.SchemaRegistry;
import tech.pegasys.teku.spec.schemas.SchemaRegistryBuilder;

public class SpecVersion extends DelegatingSpecLogic {
private final SpecMilestone milestone;
Expand All @@ -56,53 +58,90 @@ private SpecVersion(

public static Optional<SpecVersion> create(
final SpecMilestone milestone, final SpecConfig specConfig) {
final SchemaRegistryBuilder schemaRegistryBuilder = SchemaRegistryBuilder.create();
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 @@ -21,6 +21,7 @@
import tech.pegasys.teku.spec.datastructures.state.HistoricalBatch.HistoricalBatchSchema;

public abstract class AbstractSchemaDefinitions implements SchemaDefinitions {
protected SchemaRegistry schemaRegistry;

final SszBitvectorSchema<SszBitvector> attnetsENRFieldSchema;
final SszBitvectorSchema<SszBitvector> syncnetsENRFieldSchema =
Expand All @@ -29,16 +30,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 @@ -89,6 +89,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 @@ -72,8 +72,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 @@ -52,8 +52,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 @@ -70,8 +70,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 @@ -82,8 +82,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 @@ -107,8 +107,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 @@ -52,8 +52,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
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import tech.pegasys.teku.spec.datastructures.state.Fork;
import tech.pegasys.teku.spec.datastructures.util.ForkAndSpecMilestone;
import tech.pegasys.teku.spec.networks.Eth2Network;
import tech.pegasys.teku.spec.schemas.SchemaRegistryBuilder;

public class ForkScheduleTest {
private static final SpecConfig MINIMAL_CONFIG =
Expand Down Expand Up @@ -57,10 +58,12 @@ public class ForkScheduleTest {
TRANSITION_CONFIG.toVersionAltair().orElseThrow().getAltairForkVersion();
static final Bytes4 UNKNOWN_FORK_VERSION = Bytes4.fromHexStringLenient("0xFFFFFFFF");

static final SchemaRegistryBuilder SCHEMA_REGISTRY_BUILDER = SchemaRegistryBuilder.create();

@Test
public void build_validScheduleWithAltairTransition() {
final SpecVersion phase0 = SpecVersion.createPhase0(TRANSITION_CONFIG);
final SpecVersion altair = SpecVersion.createAltair(TRANSITION_CONFIG);
final SpecVersion phase0 = SpecVersion.createPhase0(TRANSITION_CONFIG, SCHEMA_REGISTRY_BUILDER);
final SpecVersion altair = SpecVersion.createAltair(TRANSITION_CONFIG, SCHEMA_REGISTRY_BUILDER);

final ForkSchedule forkSchedule =
ForkSchedule.builder().addNextMilestone(phase0).addNextMilestone(altair).build();
Expand All @@ -70,8 +73,8 @@ public void build_validScheduleWithAltairTransition() {

@Test
public void build_validScheduleWithAltairAtGenesis_phase0AndAltairSupplied() {
final SpecVersion phase0 = SpecVersion.createPhase0(ALTAIR_CONFIG);
final SpecVersion altair = SpecVersion.createAltair(ALTAIR_CONFIG);
final SpecVersion phase0 = SpecVersion.createPhase0(ALTAIR_CONFIG, SCHEMA_REGISTRY_BUILDER);
final SpecVersion altair = SpecVersion.createAltair(ALTAIR_CONFIG, SCHEMA_REGISTRY_BUILDER);

final ForkSchedule forkSchedule =
ForkSchedule.builder().addNextMilestone(phase0).addNextMilestone(altair).build();
Expand All @@ -82,7 +85,7 @@ public void build_validScheduleWithAltairAtGenesis_phase0AndAltairSupplied() {

@Test
public void build_validScheduleWithAltairAtGenesis_onlyAltairSupplied() {
final SpecVersion altair = SpecVersion.createAltair(ALTAIR_CONFIG);
final SpecVersion altair = SpecVersion.createAltair(ALTAIR_CONFIG, SCHEMA_REGISTRY_BUILDER);

final ForkSchedule forkSchedule = ForkSchedule.builder().addNextMilestone(altair).build();

Expand All @@ -92,7 +95,7 @@ public void build_validScheduleWithAltairAtGenesis_onlyAltairSupplied() {

@Test
public void build_validPhase0Schedule() {
final SpecVersion phase0 = SpecVersion.createPhase0(PHASE0_CONFIG);
final SpecVersion phase0 = SpecVersion.createPhase0(PHASE0_CONFIG, SCHEMA_REGISTRY_BUILDER);

final ForkSchedule forkSchedule = ForkSchedule.builder().addNextMilestone(phase0).build();

Expand All @@ -102,7 +105,7 @@ public void build_validPhase0Schedule() {

@Test
public void builder_milestonesSuppliedOutOfOrder_altairProcessedAtNonZeroSlot() {
final SpecVersion altair = SpecVersion.createAltair(TRANSITION_CONFIG);
final SpecVersion altair = SpecVersion.createAltair(TRANSITION_CONFIG, SCHEMA_REGISTRY_BUILDER);
final ForkSchedule.Builder builder = ForkSchedule.builder();

assertThatThrownBy(() -> builder.addNextMilestone(altair))
Expand All @@ -112,8 +115,8 @@ public void builder_milestonesSuppliedOutOfOrder_altairProcessedAtNonZeroSlot()

@Test
public void builder_milestonesSuppliedOutOfOrder_processAltairBeforePhase0() {
final SpecVersion altair = SpecVersion.createAltair(ALTAIR_CONFIG);
final SpecVersion phase0 = SpecVersion.createPhase0(ALTAIR_CONFIG);
final SpecVersion altair = SpecVersion.createAltair(ALTAIR_CONFIG, SCHEMA_REGISTRY_BUILDER);
final SpecVersion phase0 = SpecVersion.createPhase0(ALTAIR_CONFIG, SCHEMA_REGISTRY_BUILDER);
final ForkSchedule.Builder builder = ForkSchedule.builder();

builder.addNextMilestone(altair);
Expand All @@ -132,7 +135,7 @@ public void getSupportedMilestones_withTransition() {

@Test
public void getSupportedMilestones_onlyAltairConfigured() {
final SpecVersion altair = SpecVersion.createAltair(ALTAIR_CONFIG);
final SpecVersion altair = SpecVersion.createAltair(ALTAIR_CONFIG, SCHEMA_REGISTRY_BUILDER);

final ForkSchedule forkSchedule = ForkSchedule.builder().addNextMilestone(altair).build();

Expand All @@ -142,7 +145,7 @@ public void getSupportedMilestones_onlyAltairConfigured() {

@Test
public void getSupportedMilestones_onlyPhase0Configured() {
final SpecVersion phase0 = SpecVersion.createPhase0(PHASE0_CONFIG);
final SpecVersion phase0 = SpecVersion.createPhase0(PHASE0_CONFIG, SCHEMA_REGISTRY_BUILDER);

final ForkSchedule forkSchedule = ForkSchedule.builder().addNextMilestone(phase0).build();

Expand Down Expand Up @@ -398,10 +401,11 @@ public void getGenesisFork_withTransition() {

private ForkSchedule buildForkSchedule(final SpecConfig specConfig) {
final ForkSchedule.Builder builder = ForkSchedule.builder();
builder.addNextMilestone(SpecVersion.createPhase0(specConfig));
builder.addNextMilestone(SpecVersion.createPhase0(specConfig, SCHEMA_REGISTRY_BUILDER));
specConfig
.toVersionAltair()
.ifPresent(a -> builder.addNextMilestone(SpecVersion.createAltair(a)));
.ifPresent(
a -> builder.addNextMilestone(SpecVersion.createAltair(a, SCHEMA_REGISTRY_BUILDER)));

return builder.build();
}
Expand Down
Loading

0 comments on commit 39e9961

Please sign in to comment.