Skip to content

Commit

Permalink
feat: make produce block/signed block contents api multifork
Browse files Browse the repository at this point in the history
  • Loading branch information
g11tech committed Jul 1, 2024
1 parent 0330d03 commit 68cefff
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 74 deletions.
84 changes: 36 additions & 48 deletions packages/api/src/beacon/routes/beacon/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<typeof BlockHeaderResponseType>;
export type BlockHeadersResponse = ValueOf<typeof BlockHeadersResponseType>;
export type RootResponse = ValueOf<typeof RootResponseType>;
export type SignedBlockContents = ValueOf<typeof SignedBlockContentsType>;

export type BlockId = RootHex | Slot | "head" | "genesis" | "finalized" | "justified";

Expand Down Expand Up @@ -297,11 +290,12 @@ export function getDefinitions(config: ChainForkConfig): RouteDefinitions<Endpoi
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.toJson(signedBlockOrContents as SignedBeaconBlock)
: SignedBlockContentsType.toJson(signedBlockOrContents as SignedBlockContents),
body: isForkBlobs(fork)
? ssz.allForksBlobs[fork].SignedBlockContents.toJson(signedBlockOrContents as SignedBlockContents)
: sszTypesFor(fork).SignedBeaconBlock.toJson(signedBlockOrContents as SignedBeaconBlock),
headers: {
[MetaHeader.Version]: config.getForkName(slot),
},
Expand All @@ -321,36 +315,33 @@ export function getDefinitions(config: ChainForkConfig): RouteDefinitions<Endpoi
: (body as SignedBeaconBlock).message.slot
);
}
const forkSeq = config.forks[forkName].seq;
return {
signedBlockOrContents:
forkSeq < ForkSeq.deneb
? ssz[forkName].SignedBeaconBlock.fromJson(body)
: SignedBlockContentsType.fromJson(body),
signedBlockOrContents: isForkBlobs(forkName)
? ssz.allForksBlobs[forkName].SignedBlockContents.fromJson(body)
: ssz[forkName].SignedBeaconBlock.fromJson(body),
};
},
writeReqSsz: ({signedBlockOrContents}) => {
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)
? ssz.allForksBlobs[fork].SignedBlockContents.serialize(signedBlockOrContents as SignedBlockContents)
: sszTypesFor(fork).SignedBeaconBlock.serialize(signedBlockOrContents as SignedBeaconBlock),
headers: {
[MetaHeader.Version]: config.getForkName(slot),
},
};
},
parseReqSsz: ({body, headers}) => {
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)
? ssz.allForksBlobs[forkName].SignedBlockContents.deserialize(body)
: ssz[forkName].SignedBeaconBlock.deserialize(body),
};
},
schema: {
Expand All @@ -371,51 +362,48 @@ export function getDefinitions(config: ChainForkConfig): RouteDefinitions<Endpoi
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.toJson(signedBlockOrContents as SignedBeaconBlock)
: SignedBlockContentsType.toJson(signedBlockOrContents as SignedBlockContents),
body: isForkBlobs(fork)
? ssz.allForksBlobs[fork].SignedBlockContents.toJson(signedBlockOrContents as SignedBlockContents)
: sszTypesFor(fork).SignedBeaconBlock.toJson(signedBlockOrContents as SignedBeaconBlock),
headers: {
[MetaHeader.Version]: config.getForkName(slot),
[MetaHeader.Version]: fork,
},
query: {broadcast_validation: broadcastValidation},
};
},
parseReqJson: ({body, headers, query}) => {
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)
? ssz.allForksBlobs[forkName].SignedBlockContents.fromJson(body)
: ssz[forkName].SignedBeaconBlock.fromJson(body),
broadcastValidation: query.broadcast_validation as BroadcastValidation,
};
},
writeReqSsz: ({signedBlockOrContents, broadcastValidation}) => {
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)
? ssz.allForksBlobs[fork].SignedBlockContents.serialize(signedBlockOrContents as SignedBlockContents)
: sszTypesFor(fork).SignedBeaconBlock.serialize(signedBlockOrContents as SignedBeaconBlock),
headers: {
[MetaHeader.Version]: config.getForkName(slot),
[MetaHeader.Version]: fork,
},
query: {broadcast_validation: broadcastValidation},
};
},
parseReqSsz: ({body, headers, query}) => {
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)
? ssz.allForksBlobs[forkName].SignedBlockContents.deserialize(body)
: ssz[forkName].SignedBeaconBlock.deserialize(body),
broadcastValidation: query.broadcast_validation as BroadcastValidation,
};
},
Expand Down
16 changes: 5 additions & 11 deletions packages/api/src/beacon/routes/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,6 @@ export type ProduceBlockV3Meta = ValueOf<typeof ProduceBlockV3MetaType> & {
executionPayloadSource: ProducedBlockSource;
};

export const BlockContentsType = new ContainerType(
{
block: ssz.deneb.BeaconBlock,
kzgProofs: ssz.deneb.KZGProofs,
blobs: ssz.deneb.Blobs,
},
{jsonCase: "eth2"}
);

export const AttesterDutyType = new ContainerType(
{
/** The validator's public key, uniquely identifying them */
Expand Down Expand Up @@ -630,7 +621,10 @@ export function getDefinitions(config: ChainForkConfig): RouteDefinitions<Endpoi
},
resp: {
data: WithVersion(
(fork) => (isForkBlobs(fork) ? BlockContentsType : ssz[fork].BeaconBlock) as Type<BeaconBlockOrContents>
(fork) =>
(isForkBlobs(fork)
? ssz.allForksBlobs[fork].BlockContents
: ssz[fork].BeaconBlock) as Type<BeaconBlockOrContents>
),
meta: VersionCodec,
},
Expand Down Expand Up @@ -693,7 +687,7 @@ export function getDefinitions(config: ChainForkConfig): RouteDefinitions<Endpoi
(executionPayloadBlinded
? getExecutionForkTypes(version).BlindedBeaconBlock
: isForkBlobs(version)
? BlockContentsType
? ssz.allForksBlobs[version].BlockContents
: ssz[version].BeaconBlock) as Type<BeaconBlockOrContents | BlindedBeaconBlock>
),
meta: {
Expand Down
18 changes: 18 additions & 0 deletions packages/types/src/deneb/sszTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
);
7 changes: 4 additions & 3 deletions packages/types/src/deneb/types.ts
Original file line number Diff line number Diff line change
@@ -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<typeof ssz.KZGProof>;
Expand Down Expand Up @@ -49,4 +47,7 @@ export type LightClientOptimisticUpdate = ValueOf<typeof ssz.LightClientOptimist
export type LightClientStore = ValueOf<typeof ssz.LightClientStore>;

export type ProducedBlobSidecars = Omit<BlobSidecars, "signedBlockHeader" | "kzgCommitmentInclusionProof">;
export type Contents = Omit<BlockContents<ForkName.deneb>, "block">;

export type BlockContents = ValueOf<typeof ssz.BlockContents>;
export type SignedBlockContents = ValueOf<typeof ssz.SignedBlockContents>;
export type Contents = Omit<BlockContents, "block">;
18 changes: 18 additions & 0 deletions packages/types/src/electra/sszTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,3 +417,21 @@ export const SSEPayloadAttributes = new ContainerType(
},
{typeName: "SSEPayloadAttributes", jsonCase: "eth2"}
);

export const BlockContents = new ContainerType(
{
block: BeaconBlock,
kzgProofs: denebSsz.KZGProofs,
blobs: denebSsz.Blobs,
},
{typeName: "BlockContents", jsonCase: "eth2"}
);

export const SignedBlockContents = new ContainerType(
{
signedBlock: SignedBeaconBlock,
kzgProofs: denebSsz.KZGProofs,
blobs: denebSsz.Blobs,
},
{typeName: "BlockContents", jsonCase: "eth2"}
);
3 changes: 3 additions & 0 deletions packages/types/src/electra/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,6 @@ export type LightClientStore = ValueOf<typeof ssz.LightClientStore>;
export type PendingBalanceDeposit = ValueOf<typeof ssz.PendingBalanceDeposit>;
export type PendingPartialWithdrawal = ValueOf<typeof ssz.PendingPartialWithdrawal>;
export type PendingConsolidation = ValueOf<typeof ssz.PendingConsolidation>;

export type BlockContents = ValueOf<typeof ssz.BlockContents>;
export type SignedBlockContents = ValueOf<typeof ssz.SignedBlockContents>;
4 changes: 4 additions & 0 deletions packages/types/src/sszTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ const typesByFork = {
IndexedAttestationBigint: phase0.IndexedAttestationBigint,
AttesterSlashing: phase0.AttesterSlashing,
SignedAggregateAndProof: phase0.SignedAggregateAndProof,
BlockContents: deneb.BlockContents,
SignedBlockContents: deneb.SignedBlockContents,
},
[ForkName.electra]: {
BeaconBlock: electra.BeaconBlock,
Expand Down Expand Up @@ -150,6 +152,8 @@ const typesByFork = {
IndexedAttestationBigint: electra.IndexedAttestationBigint,
AttesterSlashing: electra.AttesterSlashing,
SignedAggregateAndProof: electra.SignedAggregateAndProof,
BlockContents: electra.BlockContents,
SignedBlockContents: electra.SignedBlockContents,
},
};

Expand Down
16 changes: 4 additions & 12 deletions packages/types/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,8 @@ type TypesByFork = {
BuilderBid: deneb.BuilderBid;
SignedBuilderBid: deneb.SignedBuilderBid;
SSEPayloadAttributes: deneb.SSEPayloadAttributes;
BlockContents: {block: BeaconBlock<ForkName.deneb>; kzgProofs: deneb.KZGProofs; blobs: deneb.Blobs};
SignedBlockContents: {
signedBlock: SignedBeaconBlock<ForkName.deneb>;
kzgProofs: deneb.KZGProofs;
blobs: deneb.Blobs;
};
BlockContents: deneb.BlockContents;
SignedBlockContents: deneb.SignedBlockContents;
ExecutionPayloadAndBlobsBundle: deneb.ExecutionPayloadAndBlobsBundle;
BlobsBundle: deneb.BlobsBundle;
Contents: deneb.Contents;
Expand Down Expand Up @@ -190,12 +186,8 @@ type TypesByFork = {
BuilderBid: electra.BuilderBid;
SignedBuilderBid: electra.SignedBuilderBid;
SSEPayloadAttributes: electra.SSEPayloadAttributes;
BlockContents: {block: BeaconBlock<ForkName.electra>; kzgProofs: deneb.KZGProofs; blobs: deneb.Blobs};
SignedBlockContents: {
signedBlock: SignedBeaconBlock<ForkName.electra>;
kzgProofs: deneb.KZGProofs;
blobs: deneb.Blobs;
};
BlockContents: electra.BlockContents;
SignedBlockContents: electra.SignedBlockContents;
ExecutionPayloadAndBlobsBundle: deneb.ExecutionPayloadAndBlobsBundle;
BlobsBundle: deneb.BlobsBundle;
Contents: deneb.Contents;
Expand Down

0 comments on commit 68cefff

Please sign in to comment.