From 34b624d1c1d53bcba9ac75decdaa11823fdf5ca5 Mon Sep 17 00:00:00 2001 From: Nico Flaig Date: Sun, 7 Jan 2024 15:46:54 +0100 Subject: [PATCH] Fix json casing in codecs --- packages/api/src/utils/codecs.ts | 54 +++++++++++++++++++++++--------- packages/api/src/utils/serdes.ts | 11 +++++++ 2 files changed, 50 insertions(+), 15 deletions(-) diff --git a/packages/api/src/utils/codecs.ts b/packages/api/src/utils/codecs.ts index 78be0a66a94e..04a963932993 100644 --- a/packages/api/src/utils/codecs.ts +++ b/packages/api/src/utils/codecs.ts @@ -5,8 +5,18 @@ import {ForkName} from "@lodestar/params"; import {Root} from "@lodestar/types"; import {fromHex, toHex} from "@lodestar/utils"; import {ExecutionOptimistic} from "../beacon/routes/beacon/block.js"; -import {AnyEndpoint, AnyGetEndpoint, AnyPostEndpoint, GetRequestCodec, PostRequestCodec, ResponseCodec, ResponseDataCodec, ResponseMetadataCodec} from "./types.js"; -import { WireFormat } from "./headers.js"; +import { + AnyEndpoint, + AnyGetEndpoint, + AnyPostEndpoint, + GetRequestCodec, + PostRequestCodec, + ResponseCodec, + ResponseDataCodec, + ResponseMetadataCodec, +} from "./types.js"; +import {WireFormat} from "./headers.js"; +import {toForkName} from "./serdes.js"; // Utility types / codecs @@ -80,8 +90,12 @@ export function WithVersion( } export const ExecutionOptimisticCodec: ResponseMetadataCodec = { - toJson: (val) => val, - fromJson: (val) => val as ExecutionOptimisticAndVersionMeta, + toJson: ({executionOptimistic}) => ({ + execution_optimistic: executionOptimistic, + }), + fromJson: (val) => ({ + executionOptimistic: (val as {execution_optimistic: boolean}).execution_optimistic, + }), toHeadersObject: (val) => ({ "Eth-Execution-Optimistic": String(val.executionOptimistic), }), @@ -92,36 +106,46 @@ export const ExecutionOptimisticCodec: ResponseMetadataCodec = { toJson: (val) => val, - fromJson: (val) => val as VersionMeta, + fromJson: (val) => ({ + version: toForkName((val as {version: string}).version), + }), toHeadersObject: (val) => ({ "Eth-Consensus-Version": val.version, }), fromHeaders: (val) => ({ - version: val.get("Eth-Consensus-Version")!.toLowerCase() as ForkName, + version: toForkName(val.get("Eth-Consensus-Version")!), }), }; export const ExecutionOptimisticAndVersionCodec: ResponseMetadataCodec = { - toJson: (val) => val, - fromJson: (val) => val as ExecutionOptimisticAndVersionMeta, + toJson: ({executionOptimistic, version}) => ({ + execution_optimistic: executionOptimistic, + version, + }), + fromJson: (val) => ({ + executionOptimistic: (val as {execution_optimistic: boolean}).execution_optimistic, + version: toForkName((val as {version: string}).version), + }), toHeadersObject: (val) => ({ "Eth-Execution-Optimistic": String(val.executionOptimistic), "Eth-Consensus-Version": val.version, }), fromHeaders: (val) => ({ executionOptimistic: Boolean(val.get("Eth-Execution-Optimistic")), - version: val.get("Eth-Consensus-Version")!.toLowerCase() as ForkName, + version: toForkName(val.get("Eth-Consensus-Version")!), }), }; export const ExecutionOptimisticAndDependentRootCodec: ResponseMetadataCodec = { - toJson: ({executionOptimistic, dependentRoot}) => ({executionOptimistic, dependentRoot: toHex(dependentRoot)}), - fromJson: (val) => - ({ - executionOptimistic: (val as any).executionOptimistic as boolean, - dependentRoot: fromHex((val as any).dependentRoot), - }) as ExecutionOptimisticAndDependentRootMeta, + toJson: ({executionOptimistic, dependentRoot}) => ({ + execution_optimistic: executionOptimistic, + dependent_root: toHex(dependentRoot), + }), + fromJson: (val) => ({ + executionOptimistic: (val as {execution_optimistic: boolean}).execution_optimistic, + dependentRoot: fromHex((val as {dependent_root: string}).dependent_root), + }), toHeadersObject: (val) => ({ "Eth-Execution-Optimistic": String(val.executionOptimistic), "Eth-Consensus-Dependent-Root": toHex(val.dependentRoot), diff --git a/packages/api/src/utils/serdes.ts b/packages/api/src/utils/serdes.ts index 44e52a93382e..37664ec8461f 100644 --- a/packages/api/src/utils/serdes.ts +++ b/packages/api/src/utils/serdes.ts @@ -1,4 +1,5 @@ import {fromHexString, JsonPath, toHexString} from "@chainsafe/ssz"; +import {ForkName} from "@lodestar/params"; /** * Serialize proof path to JSON. @@ -93,3 +94,13 @@ export function fromGraffitiHex(hex: string): string { return hex; } } + +export function toForkName(version: string): ForkName { + // Teku returns fork as UPPERCASE + version = version.toLowerCase(); + + // Un-safe external data, validate version is known ForkName value + if (!(version in ForkName)) throw Error(`Invalid version ${version}`); + + return version as ForkName; +}