From 769f53a2ebc59b06d5a8fa7ad4b823f4e855062f Mon Sep 17 00:00:00 2001 From: Enrico Del Fante Date: Thu, 21 Nov 2024 16:31:45 +0100 Subject: [PATCH] Migrate block content schemas (#8843) * migrate-block-content-schemas * deleted too much * lookup cache first --- .../versions/deneb/BlockContentsSchema.java | 25 +++++------- .../deneb/SignedBlockContentsSchema.java | 25 +++++------- .../spec/schemas/SchemaDefinitionsDeneb.java | 14 +++---- .../schemas/SchemaDefinitionsElectra.java | 40 ------------------- .../spec/schemas/registry/SchemaRegistry.java | 9 +++-- .../registry/SchemaRegistryBuilder.java | 25 ++++++++++++ 6 files changed, 55 insertions(+), 83 deletions(-) diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/blocks/versions/deneb/BlockContentsSchema.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/blocks/versions/deneb/BlockContentsSchema.java index f0d0bee0657..5a507439e86 100644 --- a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/blocks/versions/deneb/BlockContentsSchema.java +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/blocks/versions/deneb/BlockContentsSchema.java @@ -13,6 +13,9 @@ package tech.pegasys.teku.spec.datastructures.blocks.versions.deneb; +import static tech.pegasys.teku.spec.schemas.registry.SchemaTypes.BEACON_BLOCK_SCHEMA; +import static tech.pegasys.teku.spec.schemas.registry.SchemaTypes.BLOB_SCHEMA; + import java.util.List; import tech.pegasys.teku.infrastructure.ssz.SszList; import tech.pegasys.teku.infrastructure.ssz.containers.ContainerSchema3; @@ -22,12 +25,11 @@ import tech.pegasys.teku.kzg.KZGProof; import tech.pegasys.teku.spec.config.SpecConfigDeneb; import tech.pegasys.teku.spec.datastructures.blobs.versions.deneb.Blob; -import tech.pegasys.teku.spec.datastructures.blobs.versions.deneb.BlobSchema; import tech.pegasys.teku.spec.datastructures.blocks.BeaconBlock; -import tech.pegasys.teku.spec.datastructures.blocks.BeaconBlockSchema; import tech.pegasys.teku.spec.datastructures.blocks.BlockContainerSchema; import tech.pegasys.teku.spec.datastructures.type.SszKZGProof; import tech.pegasys.teku.spec.datastructures.type.SszKZGProofSchema; +import tech.pegasys.teku.spec.schemas.registry.SchemaRegistry; public class BlockContentsSchema extends ContainerSchema3, SszList> @@ -36,27 +38,20 @@ public class BlockContentsSchema static final SszFieldName FIELD_KZG_PROOFS = () -> "kzg_proofs"; static final SszFieldName FIELD_BLOBS = () -> "blobs"; - BlockContentsSchema( + public BlockContentsSchema( final String containerName, final SpecConfigDeneb specConfig, - final BeaconBlockSchema beaconBlockSchema, - final BlobSchema blobSchema) { + final SchemaRegistry schemaRegistry) { super( containerName, - namedSchema("block", beaconBlockSchema), + namedSchema("block", schemaRegistry.get(BEACON_BLOCK_SCHEMA)), namedSchema( FIELD_KZG_PROOFS, SszListSchema.create(SszKZGProofSchema.INSTANCE, specConfig.getMaxBlobsPerBlock())), namedSchema( - FIELD_BLOBS, SszListSchema.create(blobSchema, specConfig.getMaxBlobsPerBlock()))); - } - - public static BlockContentsSchema create( - final SpecConfigDeneb specConfig, - final BeaconBlockSchema beaconBlockSchema, - final BlobSchema blobSchema, - final String containerName) { - return new BlockContentsSchema(containerName, specConfig, beaconBlockSchema, blobSchema); + FIELD_BLOBS, + SszListSchema.create( + schemaRegistry.get(BLOB_SCHEMA), specConfig.getMaxBlobsPerBlock()))); } public BlockContents create( diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/blocks/versions/deneb/SignedBlockContentsSchema.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/blocks/versions/deneb/SignedBlockContentsSchema.java index 34f68ca6540..88dde53f8a2 100644 --- a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/blocks/versions/deneb/SignedBlockContentsSchema.java +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/blocks/versions/deneb/SignedBlockContentsSchema.java @@ -13,6 +13,9 @@ package tech.pegasys.teku.spec.datastructures.blocks.versions.deneb; +import static tech.pegasys.teku.spec.schemas.registry.SchemaTypes.BLOB_SCHEMA; +import static tech.pegasys.teku.spec.schemas.registry.SchemaTypes.SIGNED_BEACON_BLOCK_SCHEMA; + import java.util.List; import tech.pegasys.teku.infrastructure.ssz.SszList; import tech.pegasys.teku.infrastructure.ssz.containers.ContainerSchema3; @@ -22,12 +25,12 @@ import tech.pegasys.teku.kzg.KZGProof; import tech.pegasys.teku.spec.config.SpecConfigDeneb; import tech.pegasys.teku.spec.datastructures.blobs.versions.deneb.Blob; -import tech.pegasys.teku.spec.datastructures.blobs.versions.deneb.BlobSchema; import tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock; import tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlockSchema; import tech.pegasys.teku.spec.datastructures.blocks.SignedBlockContainerSchema; import tech.pegasys.teku.spec.datastructures.type.SszKZGProof; import tech.pegasys.teku.spec.datastructures.type.SszKZGProofSchema; +import tech.pegasys.teku.spec.schemas.registry.SchemaRegistry; public class SignedBlockContentsSchema extends ContainerSchema3< @@ -37,28 +40,20 @@ public class SignedBlockContentsSchema static final SszFieldName FIELD_KZG_PROOFS = () -> "kzg_proofs"; static final SszFieldName FIELD_BLOBS = () -> "blobs"; - SignedBlockContentsSchema( + public SignedBlockContentsSchema( final String containerName, final SpecConfigDeneb specConfig, - final SignedBeaconBlockSchema signedBeaconBlockSchema, - final BlobSchema blobSchema) { + final SchemaRegistry schemaRegistry) { super( containerName, - namedSchema("signed_block", signedBeaconBlockSchema), + namedSchema("signed_block", schemaRegistry.get(SIGNED_BEACON_BLOCK_SCHEMA)), namedSchema( FIELD_KZG_PROOFS, SszListSchema.create(SszKZGProofSchema.INSTANCE, specConfig.getMaxBlobsPerBlock())), namedSchema( - FIELD_BLOBS, SszListSchema.create(blobSchema, specConfig.getMaxBlobsPerBlock()))); - } - - public static SignedBlockContentsSchema create( - final SpecConfigDeneb specConfig, - final SignedBeaconBlockSchema signedBeaconBlockSchema, - final BlobSchema blobSchema, - final String containerName) { - return new SignedBlockContentsSchema( - containerName, specConfig, signedBeaconBlockSchema, blobSchema); + FIELD_BLOBS, + SszListSchema.create( + schemaRegistry.get(BLOB_SCHEMA), specConfig.getMaxBlobsPerBlock()))); } public SignedBlockContents create( 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 9099819dd25..4d52b89fc09 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 @@ -20,11 +20,12 @@ import static tech.pegasys.teku.spec.schemas.registry.SchemaTypes.BLOB_SCHEMA; import static tech.pegasys.teku.spec.schemas.registry.SchemaTypes.BLOB_SIDECARS_BY_ROOT_REQUEST_MESSAGE_SCHEMA; import static tech.pegasys.teku.spec.schemas.registry.SchemaTypes.BLOB_SIDECAR_SCHEMA; +import static tech.pegasys.teku.spec.schemas.registry.SchemaTypes.BLOCK_CONTENTS_SCHEMA; +import static tech.pegasys.teku.spec.schemas.registry.SchemaTypes.SIGNED_BLOCK_CONTENTS_SCHEMA; import java.util.Optional; import tech.pegasys.teku.infrastructure.ssz.SszList; import tech.pegasys.teku.infrastructure.ssz.schema.SszListSchema; -import tech.pegasys.teku.spec.config.SpecConfigDeneb; import tech.pegasys.teku.spec.datastructures.blobs.versions.deneb.Blob; import tech.pegasys.teku.spec.datastructures.blobs.versions.deneb.BlobKzgCommitmentsSchema; import tech.pegasys.teku.spec.datastructures.blobs.versions.deneb.BlobSchema; @@ -57,18 +58,13 @@ public class SchemaDefinitionsDeneb extends SchemaDefinitionsCapella { public SchemaDefinitionsDeneb(final SchemaRegistry schemaRegistry) { super(schemaRegistry); - final SpecConfigDeneb specConfig = SpecConfigDeneb.required(schemaRegistry.getSpecConfig()); - this.blobKzgCommitmentsSchema = schemaRegistry.get(BLOB_KZG_COMMITMENTS_SCHEMA); + this.blobKzgCommitmentsSchema = schemaRegistry.get(BLOB_KZG_COMMITMENTS_SCHEMA); this.blobSchema = schemaRegistry.get(BLOB_SCHEMA); this.blobsInBlockSchema = schemaRegistry.get(BLOBS_IN_BLOCK_SCHEMA); this.blobSidecarSchema = schemaRegistry.get(BLOB_SIDECAR_SCHEMA); - this.blockContentsSchema = - BlockContentsSchema.create( - specConfig, getBeaconBlockSchema(), blobSchema, "BlockContentsDeneb"); - this.signedBlockContentsSchema = - SignedBlockContentsSchema.create( - specConfig, getSignedBeaconBlockSchema(), blobSchema, "SignedBlockContentsDeneb"); + this.blockContentsSchema = schemaRegistry.get(BLOCK_CONTENTS_SCHEMA); + this.signedBlockContentsSchema = schemaRegistry.get(SIGNED_BLOCK_CONTENTS_SCHEMA); this.blobsBundleSchema = schemaRegistry.get(BLOBS_BUNDLE_SCHEMA); this.executionPayloadAndBlobsBundleSchema = new ExecutionPayloadAndBlobsBundleSchema(getExecutionPayloadSchema(), blobsBundleSchema); 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 cbf50701f83..e92debca3ab 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 @@ -23,15 +23,8 @@ import java.util.Optional; import tech.pegasys.teku.infrastructure.ssz.schema.SszListSchema; import tech.pegasys.teku.spec.config.SpecConfig; -import tech.pegasys.teku.spec.config.SpecConfigElectra; -import tech.pegasys.teku.spec.datastructures.blocks.BlockContainer; -import tech.pegasys.teku.spec.datastructures.blocks.BlockContainerSchema; -import tech.pegasys.teku.spec.datastructures.blocks.SignedBlockContainer; -import tech.pegasys.teku.spec.datastructures.blocks.SignedBlockContainerSchema; import tech.pegasys.teku.spec.datastructures.blocks.blockbody.BeaconBlockBodyBuilder; import tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.electra.BeaconBlockBodyBuilderElectra; -import tech.pegasys.teku.spec.datastructures.blocks.versions.deneb.BlockContentsSchema; -import tech.pegasys.teku.spec.datastructures.blocks.versions.deneb.SignedBlockContentsSchema; import tech.pegasys.teku.spec.datastructures.builder.BuilderPayloadSchema; import tech.pegasys.teku.spec.datastructures.builder.ExecutionPayloadAndBlobsBundleSchema; import tech.pegasys.teku.spec.datastructures.execution.versions.electra.ConsolidationRequest; @@ -47,8 +40,6 @@ import tech.pegasys.teku.spec.schemas.registry.SchemaRegistry; public class SchemaDefinitionsElectra extends SchemaDefinitionsDeneb { - private final BlockContentsSchema blockContentsSchema; - private final SignedBlockContentsSchema signedBlockContentsSchema; private final ExecutionPayloadAndBlobsBundleSchema executionPayloadAndBlobsBundleSchema; private final ExecutionRequestsSchema executionRequestsSchema; @@ -67,22 +58,11 @@ public class SchemaDefinitionsElectra extends SchemaDefinitionsDeneb { public SchemaDefinitionsElectra(final SchemaRegistry schemaRegistry) { super(schemaRegistry); - final SpecConfigElectra specConfig = SpecConfigElectra.required(schemaRegistry.getSpecConfig()); - this.executionRequestsSchema = schemaRegistry.get(EXECUTION_REQUESTS_SCHEMA); this.pendingDepositsSchema = schemaRegistry.get(PENDING_DEPOSITS_SCHEMA); this.pendingPartialWithdrawalsSchema = schemaRegistry.get(PENDING_PARTIAL_WITHDRAWALS_SCHEMA); this.pendingConsolidationsSchema = schemaRegistry.get(PENDING_CONSOLIDATIONS_SCHEMA); - this.blockContentsSchema = - BlockContentsSchema.create( - specConfig, getBeaconBlockSchema(), getBlobSchema(), "BlockContentsElectra"); - this.signedBlockContentsSchema = - SignedBlockContentsSchema.create( - specConfig, - getSignedBeaconBlockSchema(), - getBlobSchema(), - "SignedBlockContentsElectra"); this.executionPayloadAndBlobsBundleSchema = new ExecutionPayloadAndBlobsBundleSchema( getExecutionPayloadSchema(), schemaRegistry.get(BLOBS_BUNDLE_SCHEMA)); @@ -105,16 +85,6 @@ public static SchemaDefinitionsElectra required(final SchemaDefinitions schemaDe return (SchemaDefinitionsElectra) schemaDefinitions; } - @Override - public BlockContainerSchema getBlockContainerSchema() { - return getBlockContentsSchema().castTypeToBlockContainer(); - } - - @Override - public SignedBlockContainerSchema getSignedBlockContainerSchema() { - return getSignedBlockContentsSchema().castTypeToSignedBlockContainer(); - } - @Override public BuilderPayloadSchema getBuilderPayloadSchema() { return getExecutionPayloadAndBlobsBundleSchema(); @@ -127,16 +97,6 @@ public BeaconBlockBodyBuilder createBeaconBlockBodyBuilder() { getBlindedBeaconBlockBodySchema().toBlindedVersionElectra().orElseThrow()); } - @Override - public BlockContentsSchema getBlockContentsSchema() { - return blockContentsSchema; - } - - @Override - public SignedBlockContentsSchema getSignedBlockContentsSchema() { - return signedBlockContentsSchema; - } - @Override public ExecutionPayloadAndBlobsBundleSchema getExecutionPayloadAndBlobsBundleSchema() { return executionPayloadAndBlobsBundleSchema; diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/registry/SchemaRegistry.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/registry/SchemaRegistry.java index 79e7f7532be..34415dce95c 100644 --- a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/registry/SchemaRegistry.java +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/registry/SchemaRegistry.java @@ -67,6 +67,11 @@ boolean isProviderRegistered(final SchemaProvider provider) { @SuppressWarnings("unchecked") public T get(final SchemaId schemaId) { + final T schema = cache.get(milestone, schemaId); + if (schema != null) { + return schema; + } + final SchemaProvider provider = (SchemaProvider) providers.get(schemaId); if (provider == null) { throw new IllegalArgumentException( @@ -75,10 +80,6 @@ public T get(final SchemaId schemaId) { + " or it does not support milestone " + milestone); } - final T schema = cache.get(milestone, schemaId); - if (schema != null) { - return schema; - } // The schema was not found. // we reach this point only during priming when we actually ask providers to generate schemas diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/registry/SchemaRegistryBuilder.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/registry/SchemaRegistryBuilder.java index e2f1c03ffb6..99847a5b1b8 100644 --- a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/registry/SchemaRegistryBuilder.java +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/registry/SchemaRegistryBuilder.java @@ -37,6 +37,7 @@ import static tech.pegasys.teku.spec.schemas.registry.SchemaTypes.BLOB_SCHEMA; import static tech.pegasys.teku.spec.schemas.registry.SchemaTypes.BLOB_SIDECARS_BY_ROOT_REQUEST_MESSAGE_SCHEMA; import static tech.pegasys.teku.spec.schemas.registry.SchemaTypes.BLOB_SIDECAR_SCHEMA; +import static tech.pegasys.teku.spec.schemas.registry.SchemaTypes.BLOCK_CONTENTS_SCHEMA; import static tech.pegasys.teku.spec.schemas.registry.SchemaTypes.BLS_TO_EXECUTION_CHANGE_SCHEMA; import static tech.pegasys.teku.spec.schemas.registry.SchemaTypes.BUILDER_BID_SCHEMA; import static tech.pegasys.teku.spec.schemas.registry.SchemaTypes.EXECUTION_PAYLOAD_HEADER_SCHEMA; @@ -51,6 +52,7 @@ import static tech.pegasys.teku.spec.schemas.registry.SchemaTypes.SIGNED_AGGREGATE_AND_PROOF_SCHEMA; import static tech.pegasys.teku.spec.schemas.registry.SchemaTypes.SIGNED_BEACON_BLOCK_SCHEMA; import static tech.pegasys.teku.spec.schemas.registry.SchemaTypes.SIGNED_BLINDED_BEACON_BLOCK_SCHEMA; +import static tech.pegasys.teku.spec.schemas.registry.SchemaTypes.SIGNED_BLOCK_CONTENTS_SCHEMA; import static tech.pegasys.teku.spec.schemas.registry.SchemaTypes.SIGNED_BLS_TO_EXECUTION_CHANGE_SCHEMA; import static tech.pegasys.teku.spec.schemas.registry.SchemaTypes.SIGNED_BUILDER_BID_SCHEMA; import static tech.pegasys.teku.spec.schemas.registry.SchemaTypes.SYNCNETS_ENR_FIELD_SCHEMA; @@ -84,6 +86,8 @@ import tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.electra.BeaconBlockBodySchemaElectraImpl; import tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.electra.BlindedBeaconBlockBodySchemaElectraImpl; import tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.phase0.BeaconBlockBodySchemaPhase0; +import tech.pegasys.teku.spec.datastructures.blocks.versions.deneb.BlockContentsSchema; +import tech.pegasys.teku.spec.datastructures.blocks.versions.deneb.SignedBlockContentsSchema; import tech.pegasys.teku.spec.datastructures.builder.BlobsBundleSchema; import tech.pegasys.teku.spec.datastructures.builder.SignedBuilderBidSchema; import tech.pegasys.teku.spec.datastructures.builder.versions.bellatrix.BuilderBidSchemaBellatrix; @@ -165,6 +169,8 @@ public static SchemaRegistryBuilder create() { .addProvider(createBlobSidecarSchemaProvider()) .addProvider(createBlobSidecarsByRootRequestMessageSchemaProvider()) .addProvider(createBlobsBundleSchemaProvider()) + .addProvider(createBlockContentsSchema()) + .addProvider(createSignedBlockContentsSchema()) // ELECTRA .addProvider(createPendingConsolidationsSchemaProvider()) @@ -173,6 +179,25 @@ public static SchemaRegistryBuilder create() { .addProvider(createExecutionRequestsSchemaProvider()); } + private static SchemaProvider createBlockContentsSchema() { + return providerBuilder(BLOCK_CONTENTS_SCHEMA) + .withCreator( + DENEB, + (registry, specConfig, schemaName) -> + new BlockContentsSchema(schemaName, SpecConfigDeneb.required(specConfig), registry)) + .build(); + } + + private static SchemaProvider createSignedBlockContentsSchema() { + return providerBuilder(SIGNED_BLOCK_CONTENTS_SCHEMA) + .withCreator( + DENEB, + (registry, specConfig, schemaName) -> + new SignedBlockContentsSchema( + schemaName, SpecConfigDeneb.required(specConfig), registry)) + .build(); + } + private static SchemaProvider createSignedBuilderBidSchemaProvider() { return providerBuilder(SIGNED_BUILDER_BID_SCHEMA) .withCreator(