Skip to content

Commit

Permalink
Fix debug API to support SSZ response for each endpoint (ChainSafe#5128)
Browse files Browse the repository at this point in the history
  • Loading branch information
maschad committed Mar 7, 2023
1 parent 960b8c5 commit 5a671b1
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 11 deletions.
6 changes: 3 additions & 3 deletions packages/api/src/beacon/client/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {HttpStatusCode} from "../../utils/client/httpStatusCode.js";
import {generateGenericJsonClient, getFetchOptsSerializers, IHttpClient} from "../../utils/client/index.js";
import {ReturnTypes} from "../../utils/types.js";
import {StateId} from "../routes/beacon/state.js";
import {Api, getReqSerializers, getReturnTypes, ReqTypes, routesData, StateFormat} from "../routes/debug.js";
import {Api, getReqSerializers, getReturnTypes, ReqTypes, routesData, EncodingFormat} from "../routes/debug.js";

// As Jul 2022, it takes up to 3 mins to download states so make this 5 mins for reservation
const GET_STATE_TIMEOUT_MS = 5 * 60 * 1000;
Expand All @@ -31,7 +31,7 @@ export function getClient(_config: ChainForkConfig, httpClient: IHttpClient): Ap
// TODO: Debug the type issue
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
async getState(stateId: string, format?: StateFormat) {
async getState(stateId: string, format?: EncodingFormat) {
if (format === "ssz") {
const res = await httpClient.arrayBuffer({
...fetchOptsSerializers.getState(stateId, format),
Expand All @@ -49,7 +49,7 @@ export function getClient(_config: ChainForkConfig, httpClient: IHttpClient): Ap
// TODO: Debug the type issue
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
async getStateV2(stateId: StateId, format?: StateFormat) {
async getStateV2(stateId: StateId, format?: EncodingFormat) {
if (format === "ssz") {
const res = await httpClient.arrayBuffer({
...fetchOptsSerializers.getStateV2(stateId, format),
Expand Down
45 changes: 39 additions & 6 deletions packages/api/src/beacon/routes/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {ExecutionOptimistic, StateId} from "./beacon/state.js";

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

export type StateFormat = "json" | "ssz";
export type EncodingFormat = "json" | "ssz";
export const mimeTypeSSZ = "application/octet-stream";

const stringType = new StringType();
Expand Down Expand Up @@ -56,22 +56,55 @@ type ProtoNodeApiType = ValueOf<typeof protoNodeSszType>;
export type Api = {
/**
* Retrieves all possible chain heads (leaves of fork choice tree).
* Depending on `Accept` header it can be returned either as json or as bytes serialized by SSZ
*/
getDebugChainHeads(): Promise<ApiClientResponse<{[HttpStatusCode.OK]: {data: {slot: Slot; root: RootHex}[]}}>>;
getDebugChainHeads(
format?: "json"
): Promise<ApiClientResponse<{[HttpStatusCode.OK]: {data: {slot: Slot; root: RootHex}[]}}>>;
getDebugChainHeads(format: "ssz"): Promise<ApiClientResponse<{[HttpStatusCode.OK]: Uint8Array}>>;
getDebugChainHeads(
format?: EncodingFormat
): Promise<
| ApiClientResponse<{[HttpStatusCode.OK]: Uint8Array | {data: {slot: Slot; root: RootHex}[]}}>
| ApiClientResponse<{[HttpStatusCode.OK]: {data: {slot: Slot; root: RootHex}[]}}>
| ApiClientResponse<{[HttpStatusCode.OK]: Uint8Array}>
>;

/**
* Retrieves all possible chain heads (leaves of fork choice tree).
* Depending on `Accept` header it can be returned either as json or as bytes serialized by SSZ
*/
getDebugChainHeadsV2(): Promise<
getDebugChainHeadsV2(
format?: "json"
): Promise<
ApiClientResponse<{
[HttpStatusCode.OK]: {data: {slot: Slot; root: RootHex; executionOptimistic: ExecutionOptimistic}[]};
}>
>;
getDebugChainHeadsV2(
format: "ssz"
): Promise<
ApiClientResponse<{
[HttpStatusCode.OK]: Uint8Array;
}>
>;
getDebugChainHeadsV2(
format?: EncodingFormat
): Promise<
ApiClientResponse<{
[HttpStatusCode.OK]: Uint8Array | {data: {slot: Slot; root: RootHex; executionOptimistic: ExecutionOptimistic}[]};
}>
>;

/**
* Dump all ProtoArray's nodes to debug
* Depending on `Accept` header it can be returned either as json or as bytes serialized by SSZ
*/
getProtoArrayNodes(): Promise<ApiClientResponse<{[HttpStatusCode.OK]: {data: ProtoNodeApiType[]}}>>;
getProtoArrayNodes(format?: "json"): Promise<ApiClientResponse<{[HttpStatusCode.OK]: {data: ProtoNodeApiType[]}}>>;
getProtoArrayNodes(format: "ssz"): Promise<ApiClientResponse<{[HttpStatusCode.OK]: Uint8Array}>>;
getProtoArrayNodes(
format?: EncodingFormat
): Promise<ApiClientResponse<{[HttpStatusCode.OK]: Uint8Array | {data: ProtoNodeApiType[]}}>>;

/**
* Get full BeaconState object
Expand All @@ -90,7 +123,7 @@ export type Api = {
getState(stateId: StateId, format: "ssz"): Promise<ApiClientResponse<{[HttpStatusCode.OK]: Uint8Array}>>;
getState(
stateId: StateId,
format?: StateFormat
format?: EncodingFormat
): Promise<
ApiClientResponse<{
[HttpStatusCode.OK]: Uint8Array | {data: allForks.BeaconState; executionOptimistic: ExecutionOptimistic};
Expand All @@ -116,7 +149,7 @@ export type Api = {
getStateV2(stateId: StateId, format: "ssz"): Promise<ApiClientResponse<{[HttpStatusCode.OK]: Uint8Array}>>;
getStateV2(
stateId: StateId,
format?: StateFormat
format?: EncodingFormat
): Promise<
ApiClientResponse<{
[HttpStatusCode.OK]:
Expand Down
4 changes: 2 additions & 2 deletions packages/api/src/utils/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {FetchOpts, HttpError, IHttpClient} from "./httpClient.js";

/**
* Format FetchFn opts from Fn arguments given a route definition and request serializer.
* For routes that return only JSOn use @see getGenericJsonClient
* For routes that return only JSON see {@link generateGenericJsonClient}
*/
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export function getFetchOptsSerializer<Fn extends (...args: any) => any, ReqType extends ReqGeneric>(
Expand All @@ -34,7 +34,7 @@ export function getFetchOptsSerializer<Fn extends (...args: any) => any, ReqType
}

/**
* Generate `getFetchOptsSerializer()` functions for all routes in `Api`
* Generate {@link getFetchOptsSerializer()} functions for all routes in `Api`
*/
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export function getFetchOptsSerializers<
Expand Down

0 comments on commit 5a671b1

Please sign in to comment.