diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/Spec.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/Spec.java index d05880e09be..02ec2c93b96 100644 --- a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/Spec.java +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/Spec.java @@ -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 specVersions; @@ -122,9 +123,10 @@ private Spec( static Spec create(final SpecConfig config, final SpecMilestone highestMilestoneSupported) { final Map 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); diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/SpecVersion.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/SpecVersion.java index 029d0b247e8..5170facb71e 100644 --- a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/SpecVersion.java +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/SpecVersion.java @@ -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; @@ -55,54 +57,93 @@ private SpecVersion( } public static Optional 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); 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 d2f9551a4d7..086f6587cc1 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 @@ -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 attnetsENRFieldSchema; final SszBitvectorSchema syncnetsENRFieldSchema = @@ -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 getAttnetsENRFieldSchema() { return attnetsENRFieldSchema; diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/SchemaDefinitionCache.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/SchemaDefinitionCache.java index fe4f9b5f27e..faff07cddf0 100644 --- a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/SchemaDefinitionCache.java +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/SchemaDefinitionCache.java @@ -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; @@ -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( diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/SchemaDefinitions.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/SchemaDefinitions.java index f9af34bf087..75ad884784d 100644 --- a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/SchemaDefinitions.java +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/SchemaDefinitions.java @@ -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 { @@ -89,6 +90,9 @@ public interface SchemaDefinitions { @NonSchema BeaconBlockBodyBuilder createBeaconBlockBodyBuilder(); + @NonSchema + SchemaRegistry getSchemaRegistry(); + @NonSchema default Optional toVersionAltair() { return Optional.empty(); diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/SchemaDefinitionsAltair.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/SchemaDefinitionsAltair.java index 735fc4d1e2b..99f977c999f 100644 --- a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/SchemaDefinitionsAltair.java +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/SchemaDefinitionsAltair.java @@ -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 indexedAttestationSchema; @@ -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(); diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/SchemaDefinitionsBellatrix.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/SchemaDefinitionsBellatrix.java index 29534d8b1f4..ce0e6b8b567 100644 --- a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/SchemaDefinitionsBellatrix.java +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/SchemaDefinitionsBellatrix.java @@ -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; @@ -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(); diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/SchemaDefinitionsCapella.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/SchemaDefinitionsCapella.java index ce3e9e02041..61aa76e92af 100644 --- a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/SchemaDefinitionsCapella.java +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/SchemaDefinitionsCapella.java @@ -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 { @@ -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(); diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/SchemaDefinitionsDeneb.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/SchemaDefinitionsDeneb.java index 05effc1ed3d..af9dbb4e5b2 100644 --- a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/SchemaDefinitionsDeneb.java +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/SchemaDefinitionsDeneb.java @@ -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 { @@ -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); diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/SchemaDefinitionsElectra.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/SchemaDefinitionsElectra.java index 0cd9243c73c..fd29e92cd34 100644 --- a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/SchemaDefinitionsElectra.java +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/SchemaDefinitionsElectra.java @@ -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 { @@ -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 = diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/SchemaDefinitionsPhase0.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/SchemaDefinitionsPhase0.java index 83cb6ceb615..08387f63345 100644 --- a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/SchemaDefinitionsPhase0.java +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/SchemaDefinitionsPhase0.java @@ -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 indexedAttestationSchema; @@ -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(); diff --git a/ethereum/spec/src/test/java/tech/pegasys/teku/spec/ForkScheduleTest.java b/ethereum/spec/src/test/java/tech/pegasys/teku/spec/ForkScheduleTest.java index 76203ec6c0e..299b52f1b2b 100644 --- a/ethereum/spec/src/test/java/tech/pegasys/teku/spec/ForkScheduleTest.java +++ b/ethereum/spec/src/test/java/tech/pegasys/teku/spec/ForkScheduleTest.java @@ -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.registry.SchemaRegistryBuilder; public class ForkScheduleTest { private static final SpecConfig MINIMAL_CONFIG = @@ -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(); @@ -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(); @@ -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(); @@ -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(); @@ -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)) @@ -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); @@ -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(); @@ -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(); @@ -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(); } diff --git a/ethereum/spec/src/test/java/tech/pegasys/teku/spec/SpecVersionTest.java b/ethereum/spec/src/test/java/tech/pegasys/teku/spec/SpecVersionTest.java index 1794982f102..0ecc34c084a 100644 --- a/ethereum/spec/src/test/java/tech/pegasys/teku/spec/SpecVersionTest.java +++ b/ethereum/spec/src/test/java/tech/pegasys/teku/spec/SpecVersionTest.java @@ -16,7 +16,9 @@ import static org.assertj.core.api.Assertions.assertThat; import java.util.Optional; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; +import tech.pegasys.teku.spec.config.SpecConfig; import tech.pegasys.teku.spec.config.SpecConfigAltair; import tech.pegasys.teku.spec.config.SpecConfigBellatrix; import tech.pegasys.teku.spec.config.SpecConfigCapella; @@ -24,79 +26,68 @@ import tech.pegasys.teku.spec.config.SpecConfigElectra; import tech.pegasys.teku.spec.config.SpecConfigLoader; import tech.pegasys.teku.spec.networks.Eth2Network; +import tech.pegasys.teku.spec.schemas.registry.SchemaRegistryBuilder; class SpecVersionTest { - private final SpecConfigAltair minimalConfig = - SpecConfigAltair.required(SpecConfigLoader.loadConfig(Eth2Network.MINIMAL.configName())); + private final SchemaRegistryBuilder schemaRegistryBuilder = SchemaRegistryBuilder.create(); + private final SpecConfig minimalConfig = + SpecConfigLoader.loadConfig(Eth2Network.MINIMAL.configName()); - @Test - void shouldCreatePhase0Spec() { - final SpecVersion expectedVersion = SpecVersion.createPhase0(minimalConfig); - final Optional actualVersion = - SpecVersion.create(SpecMilestone.PHASE0, minimalConfig); - assertThat(actualVersion).isPresent(); - assertThat(actualVersion.get().getMilestone()).isEqualTo(SpecMilestone.PHASE0); - assertThat(actualVersion.get().getSchemaDefinitions()) - .hasSameClassAs(expectedVersion.getSchemaDefinitions()); - } - - @Test - void shouldCreateAltairSpec() { - final SpecConfigAltair altairSpecConfig = SpecConfigAltair.required(minimalConfig); - final SpecVersion expectedVersion = SpecVersion.createAltair(altairSpecConfig); - final Optional actualVersion = - SpecVersion.create(SpecMilestone.ALTAIR, minimalConfig); - assertThat(actualVersion).isPresent(); - assertThat(actualVersion.get().getMilestone()).isEqualTo(SpecMilestone.ALTAIR); - assertThat(actualVersion.get().getSchemaDefinitions()) - .hasSameClassAs(expectedVersion.getSchemaDefinitions()); - } - - @Test - void shouldCreateBellatrixSpec() { - final SpecConfigBellatrix bellatrixSpecConfig = SpecConfigBellatrix.required(minimalConfig); - final SpecVersion expectedVersion = SpecVersion.createBellatrix(bellatrixSpecConfig); - final Optional actualVersion = - SpecVersion.create(SpecMilestone.BELLATRIX, minimalConfig); - assertThat(actualVersion).isPresent(); - assertThat(actualVersion.get().getMilestone()).isEqualTo(SpecMilestone.BELLATRIX); - assertThat(actualVersion.get().getSchemaDefinitions()) - .hasSameClassAs(expectedVersion.getSchemaDefinitions()); - } + @ParameterizedTest + @EnumSource(SpecMilestone.class) + void shouldCreateSpec(final SpecMilestone milestone) { + // make intelliJ happy + SpecVersion expectedVersion = null; + Optional actualVersion = Optional.empty(); - @Test - void shouldCreateCapellaSpec() { - final SpecConfigCapella capellaSpecConfig = SpecConfigCapella.required(minimalConfig); - final SpecVersion expectedVersion = SpecVersion.createCapella(capellaSpecConfig); - final Optional actualVersion = - SpecVersion.create(SpecMilestone.CAPELLA, minimalConfig); - assertThat(actualVersion).isPresent(); - assertThat(actualVersion.get().getMilestone()).isEqualTo(SpecMilestone.CAPELLA); - assertThat(actualVersion.get().getSchemaDefinitions()) - .hasSameClassAs(expectedVersion.getSchemaDefinitions()); - } + switch (milestone) { + case PHASE0 -> { + expectedVersion = SpecVersion.createPhase0(minimalConfig, schemaRegistryBuilder); + actualVersion = + SpecVersion.create(SpecMilestone.PHASE0, minimalConfig, schemaRegistryBuilder); + } - @Test - void shouldCreateDenebSpec() { - final SpecConfigDeneb denebSpecConfig = SpecConfigDeneb.required(minimalConfig); - final SpecVersion expectedVersion = SpecVersion.createDeneb(denebSpecConfig); - final Optional actualVersion = - SpecVersion.create(SpecMilestone.DENEB, minimalConfig); - assertThat(actualVersion).isPresent(); - assertThat(actualVersion.get().getMilestone()).isEqualTo(SpecMilestone.DENEB); - assertThat(actualVersion.get().getSchemaDefinitions()) - .hasSameClassAs(expectedVersion.getSchemaDefinitions()); - } + case ALTAIR -> { + expectedVersion = + SpecVersion.createAltair( + SpecConfigAltair.required(minimalConfig), schemaRegistryBuilder); + actualVersion = + SpecVersion.create(SpecMilestone.ALTAIR, minimalConfig, schemaRegistryBuilder); + } + case BELLATRIX -> { + expectedVersion = + SpecVersion.createBellatrix( + SpecConfigBellatrix.required(minimalConfig), schemaRegistryBuilder); + actualVersion = + SpecVersion.create(SpecMilestone.BELLATRIX, minimalConfig, schemaRegistryBuilder); + } + case CAPELLA -> { + expectedVersion = + SpecVersion.createCapella( + SpecConfigCapella.required(minimalConfig), schemaRegistryBuilder); + actualVersion = + SpecVersion.create(SpecMilestone.CAPELLA, minimalConfig, schemaRegistryBuilder); + } + case DENEB -> { + expectedVersion = + SpecVersion.createDeneb(SpecConfigDeneb.required(minimalConfig), schemaRegistryBuilder); + actualVersion = + SpecVersion.create(SpecMilestone.DENEB, minimalConfig, schemaRegistryBuilder); + } + case ELECTRA -> { + expectedVersion = + SpecVersion.createElectra( + SpecConfigElectra.required(minimalConfig), schemaRegistryBuilder); + actualVersion = + SpecVersion.create(SpecMilestone.ELECTRA, minimalConfig, schemaRegistryBuilder); + } + } - @Test - void shouldCreateElectraSpec() { - final SpecConfigElectra electraSpecConfig = SpecConfigElectra.required(minimalConfig); - final SpecVersion expectedVersion = SpecVersion.createElectra(electraSpecConfig); - final Optional actualVersion = - SpecVersion.create(SpecMilestone.ELECTRA, minimalConfig); assertThat(actualVersion).isPresent(); - assertThat(actualVersion.get().getMilestone()).isEqualTo(SpecMilestone.ELECTRA); + assertThat(actualVersion.get().getMilestone()).isEqualTo(milestone); assertThat(actualVersion.get().getSchemaDefinitions()) .hasSameClassAs(expectedVersion.getSchemaDefinitions()); + assertThat(actualVersion.get().getSchemaDefinitions().getSchemaRegistry().getMilestone()) + .isSameAs(milestone); } } diff --git a/ethereum/spec/src/test/java/tech/pegasys/teku/spec/logic/versions/electra/helpers/BeaconStateAccessorsElectraTest.java b/ethereum/spec/src/test/java/tech/pegasys/teku/spec/logic/versions/electra/helpers/BeaconStateAccessorsElectraTest.java index 3a6eb21c073..2a00a816b9d 100644 --- a/ethereum/spec/src/test/java/tech/pegasys/teku/spec/logic/versions/electra/helpers/BeaconStateAccessorsElectraTest.java +++ b/ethereum/spec/src/test/java/tech/pegasys/teku/spec/logic/versions/electra/helpers/BeaconStateAccessorsElectraTest.java @@ -35,7 +35,7 @@ class BeaconStateAccessorsElectraTest { spy(SpecConfigElectra.required(spec.atSlot(UInt64.ZERO).getConfig())); private final PredicatesElectra predicatesElectra = new PredicatesElectra(specConfig); private final SchemaDefinitionsElectra schemaDefinitionsElectra = - new SchemaDefinitionsElectra(SpecConfigElectra.required(specConfig)); + spec.getGenesisSchemaDefinitions().toVersionElectra().orElseThrow(); private final MiscHelpersElectra miscHelpersElectra = new MiscHelpersElectra( SpecConfigElectra.required(specConfig), predicatesElectra, schemaDefinitionsElectra); diff --git a/teku/src/main/java/tech/pegasys/teku/cli/subcommand/debug/PrettyPrintCommand.java b/teku/src/main/java/tech/pegasys/teku/cli/subcommand/debug/PrettyPrintCommand.java index 08ff0cb1565..5e0d2467fd3 100644 --- a/teku/src/main/java/tech/pegasys/teku/cli/subcommand/debug/PrettyPrintCommand.java +++ b/teku/src/main/java/tech/pegasys/teku/cli/subcommand/debug/PrettyPrintCommand.java @@ -38,6 +38,7 @@ import tech.pegasys.teku.spec.SpecMilestone; import tech.pegasys.teku.spec.SpecVersion; import tech.pegasys.teku.spec.config.SpecConfigLoader; +import tech.pegasys.teku.spec.schemas.registry.SchemaRegistryBuilder; @Command( name = "pretty-print", @@ -95,7 +96,9 @@ public class PrettyPrintCommand implements Callable { @Override public Integer call() throws IOException { final SpecVersion spec = - SpecVersion.create(milestone, SpecConfigLoader.loadConfig(network)).orElseThrow(); + SpecVersion.create( + milestone, SpecConfigLoader.loadConfig(network), SchemaRegistryBuilder.create()) + .orElseThrow(); final Bytes inputData; try (final InputStream in = openStream()) { inputData = Bytes.wrap(IOUtils.toByteArray(in));