diff --git a/packages/api/src/beacon/routes/beacon/block.ts b/packages/api/src/beacon/routes/beacon/block.ts index 73680ac0afc2..99fbf7d45645 100644 --- a/packages/api/src/beacon/routes/beacon/block.ts +++ b/packages/api/src/beacon/routes/beacon/block.ts @@ -11,8 +11,10 @@ import { BeaconBlockBody, SignedBeaconBlockOrContents, SignedBlindedBeaconBlock, + SignedBlockContents, + sszTypesFor, } from "@lodestar/types"; -import {ForkName, ForkPreExecution, ForkSeq, isForkExecution} from "@lodestar/params"; +import {ForkName, ForkPreExecution, isForkBlobs, isForkExecution} from "@lodestar/params"; import {Endpoint, RequestCodec, RouteDefinitions, Schema} from "../../../utils/index.js"; import {EmptyMeta, EmptyResponseCodec, EmptyResponseData, WithVersion} from "../../../utils/codecs.js"; import { @@ -37,19 +39,10 @@ export const BlockHeadersResponseType = new ListCompositeType(BlockHeaderRespons export const RootResponseType = new ContainerType({ root: ssz.Root, }); -export const SignedBlockContentsType = new ContainerType( - { - signedBlock: ssz.deneb.SignedBeaconBlock, - kzgProofs: ssz.deneb.KZGProofs, - blobs: ssz.deneb.Blobs, - }, - {jsonCase: "eth2"} -); export type BlockHeaderResponse = ValueOf; export type BlockHeadersResponse = ValueOf; export type RootResponse = ValueOf; -export type SignedBlockContents = ValueOf; export type BlockId = RootHex | Slot | "head" | "genesis" | "finalized" | "justified"; @@ -297,11 +290,12 @@ export function getDefinitions(config: ChainForkConfig): RouteDefinitions { const slot = isSignedBlockContents(signedBlockOrContents) ? signedBlockOrContents.signedBlock.message.slot : signedBlockOrContents.message.slot; + const fork = config.getForkName(slot); + return { - body: - config.getForkSeq(slot) < ForkSeq.deneb - ? config.getForkTypes(slot).SignedBeaconBlock.serialize(signedBlockOrContents as SignedBeaconBlock) - : SignedBlockContentsType.serialize(signedBlockOrContents as SignedBlockContents), + body: isForkBlobs(fork) + ? sszTypesFor(fork).SignedBlockContents.serialize(signedBlockOrContents as SignedBlockContents) + : sszTypesFor(fork).SignedBeaconBlock.serialize(signedBlockOrContents as SignedBeaconBlock), headers: { [MetaHeader.Version]: config.getForkName(slot), }, @@ -345,12 +338,10 @@ export function getDefinitions(config: ChainForkConfig): RouteDefinitions { const forkName = toForkName(fromHeaders(headers, MetaHeader.Version)); - const forkSeq = config.forks[forkName].seq; return { - signedBlockOrContents: - forkSeq < ForkSeq.deneb - ? ssz[forkName].SignedBeaconBlock.deserialize(body) - : SignedBlockContentsType.deserialize(body), + signedBlockOrContents: isForkBlobs(forkName) + ? sszTypesFor(forkName).SignedBlockContents.deserialize(body) + : ssz[forkName].SignedBeaconBlock.deserialize(body), }; }, schema: { @@ -371,25 +362,23 @@ export function getDefinitions(config: ChainForkConfig): RouteDefinitions { const forkName = toForkName(fromHeaders(headers, MetaHeader.Version)); - const forkSeq = config.forks[forkName].seq; return { - signedBlockOrContents: - forkSeq < ForkSeq.deneb - ? ssz[forkName].SignedBeaconBlock.fromJson(body) - : SignedBlockContentsType.fromJson(body), + signedBlockOrContents: isForkBlobs(forkName) + ? sszTypesFor(forkName).SignedBlockContents.fromJson(body) + : ssz[forkName].SignedBeaconBlock.fromJson(body), broadcastValidation: query.broadcast_validation as BroadcastValidation, }; }, @@ -397,25 +386,24 @@ export function getDefinitions(config: ChainForkConfig): RouteDefinitions { const forkName = toForkName(fromHeaders(headers, MetaHeader.Version)); - const forkSeq = config.forks[forkName].seq; return { - signedBlockOrContents: - forkSeq < ForkSeq.deneb - ? ssz[forkName].SignedBeaconBlock.deserialize(body) - : SignedBlockContentsType.deserialize(body), + signedBlockOrContents: isForkBlobs(forkName) + ? sszTypesFor(forkName).SignedBlockContents.deserialize(body) + : ssz[forkName].SignedBeaconBlock.deserialize(body), broadcastValidation: query.broadcast_validation as BroadcastValidation, }; }, diff --git a/packages/types/src/deneb/sszTypes.ts b/packages/types/src/deneb/sszTypes.ts index c93c3f145beb..0e3bf78b4b27 100644 --- a/packages/types/src/deneb/sszTypes.ts +++ b/packages/types/src/deneb/sszTypes.ts @@ -312,3 +312,21 @@ export const SSEPayloadAttributes = new ContainerType( }, {typeName: "SSEPayloadAttributes", jsonCase: "eth2"} ); + +export const BlockContents = new ContainerType( + { + block: BeaconBlock, + kzgProofs: KZGProofs, + blobs: Blobs, + }, + {typeName: "BlockContents", jsonCase: "eth2"} +); + +export const SignedBlockContents = new ContainerType( + { + signedBlock: SignedBeaconBlock, + kzgProofs: KZGProofs, + blobs: Blobs, + }, + {typeName: "BlockContents", jsonCase: "eth2"} +); diff --git a/packages/types/src/deneb/types.ts b/packages/types/src/deneb/types.ts index 9a901c9a1a81..7ee6648aeaf2 100644 --- a/packages/types/src/deneb/types.ts +++ b/packages/types/src/deneb/types.ts @@ -1,6 +1,4 @@ import {ValueOf} from "@chainsafe/ssz"; -import {ForkName} from "@lodestar/params"; -import type {BlockContents} from "../types.js"; import * as ssz from "./sszTypes.js"; export type KZGProof = ValueOf; @@ -49,4 +47,7 @@ export type LightClientOptimisticUpdate = ValueOf; export type ProducedBlobSidecars = Omit; -export type Contents = Omit, "block">; + +export type BlockContents = ValueOf; +export type SignedBlockContents = ValueOf; +export type Contents = Omit; diff --git a/packages/types/src/types.ts b/packages/types/src/types.ts index 46641d55667e..3602299ae5e1 100644 --- a/packages/types/src/types.ts +++ b/packages/types/src/types.ts @@ -124,12 +124,8 @@ type TypesByFork = { BuilderBid: deneb.BuilderBid; SignedBuilderBid: deneb.SignedBuilderBid; SSEPayloadAttributes: deneb.SSEPayloadAttributes; - BlockContents: {block: BeaconBlock; kzgProofs: deneb.KZGProofs; blobs: deneb.Blobs}; - SignedBlockContents: { - signedBlock: SignedBeaconBlock; - kzgProofs: deneb.KZGProofs; - blobs: deneb.Blobs; - }; + BlockContents: deneb.BlockContents; + SignedBlockContents: deneb.SignedBlockContents; ExecutionPayloadAndBlobsBundle: deneb.ExecutionPayloadAndBlobsBundle; BlobsBundle: deneb.BlobsBundle; Contents: deneb.Contents;