Skip to content

Commit

Permalink
modify the api
Browse files Browse the repository at this point in the history
  • Loading branch information
g11tech committed Nov 6, 2023
1 parent 9197e6a commit 89e4bd1
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 87 deletions.
40 changes: 10 additions & 30 deletions packages/api/src/beacon/routes/beacon/block.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
import {ContainerType} from "@chainsafe/ssz";
import {ForkName} from "@lodestar/params";
import {ChainForkConfig} from "@lodestar/config";
import {
phase0,
allForks,
Slot,
Root,
ssz,
RootHex,
deneb,
isSignedBlockContents,
isSignedBlindedBlockContents,
} from "@lodestar/types";
import {phase0, allForks, Slot, Root, ssz, RootHex, deneb, isSignedBlockContents} from "@lodestar/types";

import {
RoutesData,
Expand All @@ -30,10 +20,7 @@ import {
import {HttpStatusCode} from "../../../utils/client/httpStatusCode.js";
import {parseAcceptHeader, writeAcceptHeader} from "../../../utils/acceptHeader.js";
import {ApiClientResponse, ResponseFormat} from "../../../interfaces.js";
import {
allForksSignedBlockContentsReqSerializer,
allForksSignedBlindedBlockContentsReqSerializer,
} from "../../../utils/routes.js";
import {allForksSignedBlockContentsReqSerializer} from "../../../utils/routes.js";

// See /packages/api/src/routes/index.ts for reasoning and instructions to add new routes

Expand Down Expand Up @@ -208,7 +195,7 @@ export type Api = {
* Publish a signed blinded block by submitting it to the mev relay and patching in the block
* transactions beacon node gets in response.
*/
publishBlindedBlock(blindedBlockOrContents: allForks.SignedBlindedBeaconBlockOrContents): Promise<
publishBlindedBlock(blindedBlock: allForks.SignedBlindedBeaconBlock): Promise<
ApiClientResponse<
{
[HttpStatusCode.OK]: void;
Expand All @@ -219,7 +206,7 @@ export type Api = {
>;

publishBlindedBlockV2(
blindedBlockOrContents: allForks.SignedBlindedBeaconBlockOrContents,
blindedBlockOrContents: allForks.SignedBlindedBeaconBlock,
opts: {broadcastValidation?: BroadcastValidation}
): Promise<
ApiClientResponse<
Expand Down Expand Up @@ -316,16 +303,9 @@ export function getReqSerializers(config: ChainForkConfig): ReqSerializers<Api,
): allForks.AllForksBlindedSSZTypes["SignedBeaconBlock"] =>
config.getBlindedForkTypes(data.message.slot).SignedBeaconBlock;

const AllForksSignedBlindedBlockOrContents: TypeJson<allForks.SignedBlindedBeaconBlockOrContents> = {
toJson: (data) =>
isSignedBlindedBlockContents(data)
? allForksSignedBlindedBlockContentsReqSerializer(getSignedBlindedBeaconBlockType).toJson(data)
: getSignedBlindedBeaconBlockType(data).toJson(data),

fromJson: (data) =>
(data as {signed_blinded_block: unknown}).signed_blinded_block !== undefined
? allForksSignedBlindedBlockContentsReqSerializer(getSignedBlindedBeaconBlockType).fromJson(data)
: getSignedBlindedBeaconBlockType(data as allForks.SignedBlindedBeaconBlock).fromJson(data),
const AllForksSignedBlindedBlock: TypeJson<allForks.SignedBlindedBeaconBlock> = {
toJson: (data) => getSignedBlindedBeaconBlockType(data).toJson(data),
fromJson: (data) => getSignedBlindedBeaconBlockType(data as allForks.SignedBlindedBeaconBlock).fromJson(data),
};

return {
Expand Down Expand Up @@ -354,14 +334,14 @@ export function getReqSerializers(config: ChainForkConfig): ReqSerializers<Api,
query: {broadcast_validation: Schema.String},
},
},
publishBlindedBlock: reqOnlyBody(AllForksSignedBlindedBlockOrContents, Schema.Object),
publishBlindedBlock: reqOnlyBody(AllForksSignedBlindedBlock, Schema.Object),
publishBlindedBlockV2: {
writeReq: (item, {broadcastValidation}) => ({
body: AllForksSignedBlindedBlockOrContents.toJson(item),
body: AllForksSignedBlindedBlock.toJson(item),
query: {broadcast_validation: broadcastValidation},
}),
parseReq: ({body, query}) => [
AllForksSignedBlindedBlockOrContents.fromJson(body),
AllForksSignedBlindedBlock.fromJson(body),
{broadcastValidation: query.broadcast_validation as BroadcastValidation},
],
schema: {
Expand Down
32 changes: 15 additions & 17 deletions packages/api/src/beacon/routes/validator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {ContainerType, fromHexString, toHexString, Type} from "@chainsafe/ssz";
import {ForkName, ForkBlobs, isForkBlobs, isForkExecution, ForkPreBlobs} from "@lodestar/params";
import {ForkName, ForkBlobs, isForkBlobs, isForkExecution, ForkPreBlobs, ForkExecution} from "@lodestar/params";
import {
allForks,
altair,
Expand Down Expand Up @@ -36,7 +36,7 @@ import {
TypeJson,
} from "../../utils/index.js";
import {fromU64Str, fromGraffitiHex, toU64Str, U64Str, toGraffitiHex} from "../../utils/serdes.js";
import {allForksBlockContentsResSerializer, allForksBlindedBlockContentsResSerializer} from "../../utils/routes.js";
import {allForksBlockContentsResSerializer} from "../../utils/routes.js";
import {ExecutionOptimistic} from "./beacon/block.js";

export enum BuilderSelection {
Expand All @@ -58,14 +58,14 @@ export type ProduceBlockOrContentsRes = {executionPayloadValue: Wei} & (
| {data: allForks.BeaconBlock; version: ForkPreBlobs}
| {data: allForks.BlockContents; version: ForkBlobs}
);
export type ProduceBlindedBlockOrContentsRes = {executionPayloadValue: Wei} & (
| {data: allForks.BlindedBeaconBlock; version: ForkPreBlobs}
| {data: allForks.BlindedBlockContents; version: ForkBlobs}
);
export type ProduceBlindedBlock = {executionPayloadValue: Wei} & {
data: allForks.BlindedBeaconBlock;
version: ForkExecution;
};

export type ProduceFullOrBlindedBlockOrContentsRes =
| (ProduceBlockOrContentsRes & {executionPayloadBlinded: false})
| (ProduceBlindedBlockOrContentsRes & {executionPayloadBlinded: true});
| (ProduceBlindedBlock & {executionPayloadBlinded: true});

// See /packages/api/src/routes/index.ts for reasoning and instructions to add new routes

Expand Down Expand Up @@ -286,7 +286,7 @@ export type Api = {
): Promise<
ApiClientResponse<
{
[HttpStatusCode.OK]: ProduceBlindedBlockOrContentsRes;
[HttpStatusCode.OK]: ProduceBlindedBlock;
},
HttpStatusCode.BAD_REQUEST | HttpStatusCode.SERVICE_UNAVAILABLE
>
Expand Down Expand Up @@ -720,13 +720,11 @@ export function getReturnTypes(): ReturnTypes<Api> {
isForkBlobs(fork) ? allForksBlockContentsResSerializer(fork) : ssz[fork].BeaconBlock
)
) as TypeJson<ProduceBlockOrContentsRes>;
const produceBlindedBlockOrContents = WithExecutionPayloadValue(
WithVersion<allForks.BlindedBeaconBlockOrContents>((fork: ForkName) =>
isForkBlobs(fork)
? allForksBlindedBlockContentsResSerializer(fork)
: ssz.allForksBlinded[isForkExecution(fork) ? fork : ForkName.bellatrix].BeaconBlock
const produceBlindedBlock = WithExecutionPayloadValue(
WithVersion<allForks.BlindedBeaconBlock>(
(fork: ForkName) => ssz.allForksBlinded[isForkExecution(fork) ? fork : ForkName.bellatrix].BeaconBlock
)
) as TypeJson<ProduceBlindedBlockOrContentsRes>;
) as TypeJson<ProduceBlindedBlock>;

return {
getAttesterDuties: WithDependentRootExecutionOptimistic(ArrayOf(AttesterDuty)),
Expand All @@ -740,7 +738,7 @@ export function getReturnTypes(): ReturnTypes<Api> {
if (data.executionPayloadBlinded) {
return {
execution_payload_blinded: true,
...(produceBlindedBlockOrContents.toJson(data) as Record<string, unknown>),
...(produceBlindedBlock.toJson(data) as Record<string, unknown>),
};
} else {
return {
Expand All @@ -751,13 +749,13 @@ export function getReturnTypes(): ReturnTypes<Api> {
},
fromJson: (data) => {
if ((data as {execution_payload_blinded: true}).execution_payload_blinded) {
return {executionPayloadBlinded: true, ...produceBlindedBlockOrContents.fromJson(data)};
return {executionPayloadBlinded: true, ...produceBlindedBlock.fromJson(data)};
} else {
return {executionPayloadBlinded: false, ...produceBlockOrContents.fromJson(data)};
}
},
},
produceBlindedBlock: produceBlindedBlockOrContents,
produceBlindedBlock,

produceAttestationData: ContainerData(ssz.phase0.AttestationData),
produceSyncCommitteeContribution: ContainerData(ssz.altair.SyncCommitteeContribution),
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/builder/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export type Api = {
HttpStatusCode.NOT_FOUND | HttpStatusCode.BAD_REQUEST
>
>;
submitBlindedBlock(signedBlock: allForks.SignedBlindedBeaconBlockOrContents): Promise<
submitBlindedBlock(signedBlock: allForks.SignedBlindedBeaconBlock): Promise<
ApiClientResponse<
{
[HttpStatusCode.OK]: {
Expand Down
49 changes: 10 additions & 39 deletions packages/api/src/utils/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ export function allForksSignedBlockContentsReqSerializer(
return {
toJson: (data) => ({
signed_block: blockSerializer(data.signedBlock).toJson(data.signedBlock),
signed_blob_sidecars: ssz.deneb.SignedBlobSidecars.toJson(data.signedBlobSidecars),
kzg_proofs: ssz.deneb.KZGProofs.toJson(data.kzgProofs),
blobs: ssz.deneb.Blobs.toJson(data.blobs),
}),

fromJson: (data: {signed_block: unknown; signed_blob_sidecars: unknown}) => ({
fromJson: (data: {signed_block: unknown; kzg_proofs: unknown; blobs: unknown}) => ({
signedBlock: blockSerializer(data.signed_block as allForks.SignedBeaconBlock).fromJson(data.signed_block),
signedBlobSidecars: ssz.deneb.SignedBlobSidecars.fromJson(data.signed_blob_sidecars),
kzgProofs: ssz.deneb.KZGProofs.fromJson(data.kzg_proofs),
blobs: ssz.deneb.Blobs.fromJson(data.blobs),
}),
};
}
Expand All @@ -25,44 +27,13 @@ export function allForksBlockContentsResSerializer(fork: ForkBlobs): TypeJson<al
return {
toJson: (data) => ({
block: (ssz.allForks[fork].BeaconBlock as allForks.AllForksSSZTypes["BeaconBlock"]).toJson(data.block),
blob_sidecars: ssz.deneb.BlobSidecars.toJson(data.blobSidecars),
kzg_proofs: ssz.deneb.KZGProofs.toJson(data.kzgProofs),
blobs: ssz.deneb.Blobs.toJson(data.blobs),
}),
fromJson: (data: {block: unknown; blob_sidecars: unknown}) => ({
fromJson: (data: {block: unknown; blob_sidecars: unknown; kzg_proofs: unknown; blobs: unknown}) => ({
block: ssz.allForks[fork].BeaconBlock.fromJson(data.block),
blobSidecars: ssz.deneb.BlobSidecars.fromJson(data.blob_sidecars),
}),
};
}

export function allForksSignedBlindedBlockContentsReqSerializer(
blockSerializer: (data: allForks.SignedBlindedBeaconBlock) => TypeJson<allForks.SignedBlindedBeaconBlock>
): TypeJson<allForks.SignedBlindedBlockContents> {
return {
toJson: (data) => ({
signed_blinded_block: blockSerializer(data.signedBlindedBlock).toJson(data.signedBlindedBlock),
signed_blinded_blob_sidecars: ssz.deneb.SignedBlindedBlobSidecars.toJson(data.signedBlindedBlobSidecars),
}),

fromJson: (data: {signed_blinded_block: unknown; signed_blinded_blob_sidecars: unknown}) => ({
signedBlindedBlock: blockSerializer(data.signed_blinded_block as allForks.SignedBlindedBeaconBlock).fromJson(
data.signed_blinded_block
),
signedBlindedBlobSidecars: ssz.deneb.SignedBlindedBlobSidecars.fromJson(data.signed_blinded_blob_sidecars),
}),
};
}

export function allForksBlindedBlockContentsResSerializer(fork: ForkBlobs): TypeJson<allForks.BlindedBlockContents> {
return {
toJson: (data) => ({
blinded_block: (ssz.allForksBlinded[fork].BeaconBlock as allForks.AllForksBlindedSSZTypes["BeaconBlock"]).toJson(
data.blindedBlock
),
blinded_blob_sidecars: ssz.deneb.BlindedBlobSidecars.toJson(data.blindedBlobSidecars),
}),
fromJson: (data: {blinded_block: unknown; blinded_blob_sidecars: unknown}) => ({
blindedBlock: ssz.allForksBlinded[fork].BeaconBlock.fromJson(data.blinded_block),
blindedBlobSidecars: ssz.deneb.BlindedBlobSidecars.fromJson(data.blinded_blob_sidecars),
kzgProofs: ssz.deneb.KZGProofs.fromJson(data.kzg_proofs),
blobs: ssz.deneb.Blobs.fromJson(data.blobs),
}),
};
}

0 comments on commit 89e4bd1

Please sign in to comment.