Skip to content

Commit

Permalink
Add block values meta to all produce block apis
Browse files Browse the repository at this point in the history
  • Loading branch information
nflaig committed Jan 7, 2024
1 parent c470aa7 commit 766a3b8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 23 deletions.
32 changes: 10 additions & 22 deletions packages/api/src/beacon/routes/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@ import {
Slot,
ssz,
ValidatorIndex,
Wei,
Gwei,
ProducedBlockSource,
stringType,
} from "@lodestar/types";
import {AnyPostEndpoint, Endpoint, ResponseCodec, RouteDefinitions, Schema} from "../../utils/index.js";
import {fromGraffitiHex, toForkName, toGraffitiHex} from "../../utils/serdes.js";
import {
ArrayOf,
BlockValuesMeta,
EmptyMeta,
EmptyMetaCodec,
EmptyResponseCodec,
Expand All @@ -31,6 +30,7 @@ import {
ExecutionOptimisticMeta,
VersionCodec,
VersionMeta,
WithBlockValues,
WithMeta,
WithVersion,
} from "../../utils/codecs.js";
Expand All @@ -52,12 +52,10 @@ export type ExtraProduceBlockOps = {
blindedLocal?: boolean;
};

export type ProduceBlockV3Meta = {
version: ForkName;
export type ProduceBlockMeta = VersionMeta & BlockValuesMeta;
export type ProduceBlockV3Meta = ProduceBlockMeta & {
executionPayloadBlinded: boolean;
executionPayloadSource: ProducedBlockSource;
executionPayloadValue: Wei;
consensusBlockValue: Gwei;
};

// See /packages/api/src/routes/index.ts for reasoning and instructions to add new routes
Expand Down Expand Up @@ -312,7 +310,7 @@ export type Endpoints = {
};
},
allForks.BeaconBlockOrContents,
VersionMeta
ProduceBlockMeta
>;

/**
Expand Down Expand Up @@ -356,7 +354,7 @@ export type Endpoints = {
},
{params: {slot: number}; query: {randao_reveal: string; graffiti: string}},
allForks.BlindedBeaconBlock,
VersionMeta
ProduceBlockMeta
>;

/**
Expand Down Expand Up @@ -673,7 +671,7 @@ export const definitions: RouteDefinitions<Endpoints> = {
(fork) =>
(isForkBlobs(fork) ? BlockContentsType : ssz[fork].BeaconBlock) as Type<allForks.BeaconBlockOrContents>
),
meta: VersionCodec,
meta: WithBlockValues(VersionCodec),
},
},
produceBlockV3: {
Expand Down Expand Up @@ -733,13 +731,11 @@ export const definitions: RouteDefinitions<Endpoints> = {
? BlockContentsType
: ssz[version].BeaconBlock) as Type<allForks.FullOrBlindedBeaconBlockOrContents>
),
meta: {
meta: WithBlockValues({
toJson: (meta) => ({
version: meta.version,
execution_payload_blinded: meta.executionPayloadBlinded,
execution_payload_source: meta.executionPayloadSource,
execution_payload_value: meta.executionPayloadValue,
consensus_block_value: meta.consensusBlockValue,
}),
fromJson: (val) => {
const executionPayloadBlinded = (val as {execution_payload_blinded: boolean}).execution_payload_blinded;
Expand All @@ -753,17 +749,12 @@ export const definitions: RouteDefinitions<Endpoints> = {
version: toForkName((val as {version: string}).version),
executionPayloadBlinded,
executionPayloadSource,
// For cross client usage where beacon or validator are of separate clients, executionPayloadValue could be missing
executionPayloadValue: BigInt((val as {execution_payload_value: string}).execution_payload_value ?? "0"),
consensusBlockValue: BigInt((val as {consensus_block_value: string}).consensus_block_value ?? "0"),
};
},
toHeadersObject: (meta) => ({
"Eth-Consensus-Version": meta.version,
"Eth-Execution-Payload-Blinded": String(meta.executionPayloadBlinded),
"Eth-Execution-Payload-Source": String(meta.executionPayloadSource),
"Eth-Execution-Payload-Value": String(meta.executionPayloadValue),
"Eth-Consensus-Block-Value": String(meta.consensusBlockValue),
}),
fromHeaders: (headers) => {
const executionPayloadBlinded = Boolean(headers.get("Eth-Execution-Payload-Blinded")!);
Expand All @@ -777,12 +768,9 @@ export const definitions: RouteDefinitions<Endpoints> = {
version: toForkName(headers.get("Eth-Consensus-Version")!),
executionPayloadBlinded,
executionPayloadSource,
// For cross client usage where beacon or validator are of separate clients, executionPayloadValue could be missing
executionPayloadValue: BigInt(headers.get("Eth-Execution-Payload-Value") ?? "0"),
consensusBlockValue: BigInt(headers.get("Eth-Consensus-Block-Value") ?? "0"),
};
},
},
}),
},
},
produceBlindedBlock: {
Expand All @@ -808,7 +796,7 @@ export const definitions: RouteDefinitions<Endpoints> = {
},
resp: {
data: WithVersion((fork) => ssz.allForksBlinded[isForkExecution(fork) ? fork : ForkName.bellatrix].BeaconBlock),
meta: VersionCodec,
meta: WithBlockValues(VersionCodec),
},
},
produceAttestationData: {
Expand Down
32 changes: 31 additions & 1 deletion packages/api/src/utils/codecs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import {ArrayType, ListBasicType, ListCompositeType, Type, isBasicType, isCompositeType} from "@chainsafe/ssz";
import {ForkName} from "@lodestar/params";
import {Root} from "@lodestar/types";
import {Gwei, Root, Wei} from "@lodestar/types";
import {fromHex, toHex} from "@lodestar/utils";
import {ExecutionOptimistic} from "../beacon/routes/beacon/block.js";
import {
Expand All @@ -29,6 +29,7 @@ export type ExecutionOptimisticMeta = {executionOptimistic: ExecutionOptimistic}
export type VersionMeta = {version: ForkName};
export type ExecutionOptimisticAndVersionMeta = ExecutionOptimisticMeta & VersionMeta;
export type ExecutionOptimisticAndDependentRootMeta = {executionOptimistic: ExecutionOptimistic; dependentRoot: Root};
export type BlockValuesMeta = {executionPayloadValue: Wei; consensusBlockValue: Gwei};

/** Shortcut for routes that have no params, query */
export const EmptyGetRequestCodec: GetRequestCodec<AnyGetEndpoint> = {
Expand Down Expand Up @@ -156,6 +157,35 @@ export const ExecutionOptimisticAndDependentRootCodec: ResponseMetadataCodec<Exe
}),
};

export function WithBlockValues<M extends Record<string, unknown>>(
meta: ResponseMetadataCodec<Omit<M, keyof BlockValuesMeta>>
): ResponseMetadataCodec<M & BlockValuesMeta> {
return {
toJson: (val) => ({
...(meta.toJson(val) as Record<string, unknown>),
execution_payload_value: val.executionPayloadValue.toString(),
consensus_block_value: val.consensusBlockValue.toString(),
}),
fromJson: (val) => ({
...(meta.fromJson(val) as M),
// For cross client usage where beacon or validator are of separate clients, executionPayloadValue could be missing
executionPayloadValue: BigInt((val as {execution_payload_value: string}).execution_payload_value ?? "0"),
consensusBlockValue: BigInt((val as {consensus_block_value: string}).consensus_block_value ?? "0"),
}),
toHeadersObject: (val) => ({
...meta.toHeadersObject(val),
"Eth-Execution-Payload-Value": val.executionPayloadValue.toString(),
"Eth-Consensus-Block-Value": val.consensusBlockValue.toString(),
}),
fromHeaders: (val) => ({
...(meta.fromHeaders(val) as M),
// For cross client usage where beacon or validator are of separate clients, executionPayloadValue could be missing
executionPayloadValue: BigInt(val.get("Eth-Execution-Payload-Value") ?? "0"),
consensusBlockValue: BigInt(val.get("Eth-Consensus-Block-Value") ?? "0"),
}),
};
}

export const EmptyResponseCodec: ResponseCodec<AnyEndpoint> = {
data: EmptyResponseDataCodec,
meta: EmptyMetaCodec,
Expand Down

0 comments on commit 766a3b8

Please sign in to comment.