From a6642c13d9ed0009c0e3a6312a82602da7efc9f9 Mon Sep 17 00:00:00 2001 From: Enrico Del Fante Date: Tue, 24 Sep 2024 12:07:07 +0200 Subject: [PATCH] make SchemaRegistryBuilder cross-milestone when possible --- .../src/main/java/tech/pegasys/teku/spec/Spec.java | 4 +++- .../java/tech/pegasys/teku/spec/SpecVersion.java | 6 ++++-- .../teku/spec/schemas/SchemaDefinitionCache.java | 4 +++- .../java/tech/pegasys/teku/spec/SpecVersionTest.java | 12 ++++++------ .../cli/subcommand/debug/PrettyPrintCommand.java | 5 ++++- 5 files changed, 20 insertions(+), 11 deletions(-) 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 88013cf3632..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 @@ -57,8 +57,10 @@ private SpecVersion( } public static Optional create( - final SpecMilestone milestone, final SpecConfig specConfig) { - final SchemaRegistryBuilder schemaRegistryBuilder = SchemaRegistryBuilder.create(); + final SpecMilestone milestone, + final SpecConfig specConfig, + final SchemaRegistryBuilder schemaRegistryBuilder) { + return switch (milestone) { case PHASE0 -> Optional.of(createPhase0(specConfig, schemaRegistryBuilder)); case ALTAIR -> 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/test/java/tech/pegasys/teku/spec/SpecVersionTest.java b/ethereum/spec/src/test/java/tech/pegasys/teku/spec/SpecVersionTest.java index a45383a238d..9c7f91bb6eb 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 @@ -36,7 +36,7 @@ void shouldCreatePhase0Spec() { final SpecVersion expectedVersion = SpecVersion.createPhase0(minimalConfig, schemaRegistryBuilder); final Optional actualVersion = - SpecVersion.create(SpecMilestone.PHASE0, minimalConfig); + SpecVersion.create(SpecMilestone.PHASE0, minimalConfig, schemaRegistryBuilder); assertThat(actualVersion).isPresent(); assertThat(actualVersion.get().getMilestone()).isEqualTo(SpecMilestone.PHASE0); assertThat(actualVersion.get().getSchemaDefinitions()) @@ -49,7 +49,7 @@ void shouldCreateAltairSpec() { final SpecVersion expectedVersion = SpecVersion.createAltair(altairSpecConfig, schemaRegistryBuilder); final Optional actualVersion = - SpecVersion.create(SpecMilestone.ALTAIR, minimalConfig); + SpecVersion.create(SpecMilestone.ALTAIR, minimalConfig, schemaRegistryBuilder); assertThat(actualVersion).isPresent(); assertThat(actualVersion.get().getMilestone()).isEqualTo(SpecMilestone.ALTAIR); assertThat(actualVersion.get().getSchemaDefinitions()) @@ -62,7 +62,7 @@ void shouldCreateBellatrixSpec() { final SpecVersion expectedVersion = SpecVersion.createBellatrix(bellatrixSpecConfig, schemaRegistryBuilder); final Optional actualVersion = - SpecVersion.create(SpecMilestone.BELLATRIX, minimalConfig); + SpecVersion.create(SpecMilestone.BELLATRIX, minimalConfig, schemaRegistryBuilder); assertThat(actualVersion).isPresent(); assertThat(actualVersion.get().getMilestone()).isEqualTo(SpecMilestone.BELLATRIX); assertThat(actualVersion.get().getSchemaDefinitions()) @@ -75,7 +75,7 @@ void shouldCreateCapellaSpec() { final SpecVersion expectedVersion = SpecVersion.createCapella(capellaSpecConfig, schemaRegistryBuilder); final Optional actualVersion = - SpecVersion.create(SpecMilestone.CAPELLA, minimalConfig); + SpecVersion.create(SpecMilestone.CAPELLA, minimalConfig, schemaRegistryBuilder); assertThat(actualVersion).isPresent(); assertThat(actualVersion.get().getMilestone()).isEqualTo(SpecMilestone.CAPELLA); assertThat(actualVersion.get().getSchemaDefinitions()) @@ -88,7 +88,7 @@ void shouldCreateDenebSpec() { final SpecVersion expectedVersion = SpecVersion.createDeneb(denebSpecConfig, schemaRegistryBuilder); final Optional actualVersion = - SpecVersion.create(SpecMilestone.DENEB, minimalConfig); + SpecVersion.create(SpecMilestone.DENEB, minimalConfig, schemaRegistryBuilder); assertThat(actualVersion).isPresent(); assertThat(actualVersion.get().getMilestone()).isEqualTo(SpecMilestone.DENEB); assertThat(actualVersion.get().getSchemaDefinitions()) @@ -101,7 +101,7 @@ void shouldCreateElectraSpec() { final SpecVersion expectedVersion = SpecVersion.createElectra(electraSpecConfig, schemaRegistryBuilder); final Optional actualVersion = - SpecVersion.create(SpecMilestone.ELECTRA, minimalConfig); + SpecVersion.create(SpecMilestone.ELECTRA, minimalConfig, schemaRegistryBuilder); assertThat(actualVersion).isPresent(); assertThat(actualVersion.get().getMilestone()).isEqualTo(SpecMilestone.ELECTRA); assertThat(actualVersion.get().getSchemaDefinitions()) 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));