-
-
Notifications
You must be signed in to change notification settings - Fork 325
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update API response object #4994
Changes from 23 commits
04a7227
e73d173
9f4137d
a3865f4
9a7e52b
5388ca9
e99f6ba
923137f
cf88c52
1d44e81
1862dfa
b6bbc76
ebdb14c
74d6f5c
13be9fa
8bb4942
c206b38
ad0cd24
edbfaa4
b37b071
8309bc6
443cdde
1f2054b
dee59bb
916dba6
8559b71
f1c419b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
import {IChainForkConfig} from "@lodestar/config"; | ||
import {Api, ReqTypes, routesData, getReqSerializers, getReturnTypes, StateFormat} from "../routes/debug.js"; | ||
import {IHttpClient, getFetchOptsSerializers, generateGenericJsonClient} from "../../utils/client/index.js"; | ||
import {ApiClientResponse} from "../../interfaces.js"; | ||
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"; | ||
|
||
// 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; | ||
|
@@ -12,38 +16,55 @@ export function getClient(_config: IChainForkConfig, httpClient: IHttpClient): A | |
const reqSerializers = getReqSerializers(); | ||
const returnTypes = getReturnTypes(); | ||
// Some routes return JSON, use a client auto-generator | ||
const client = generateGenericJsonClient<Api, ReqTypes>(routesData, reqSerializers, returnTypes, httpClient); | ||
const client = generateGenericJsonClient<Api, ReqTypes>( | ||
routesData, | ||
reqSerializers, | ||
returnTypes as ReturnTypes<Api>, | ||
httpClient | ||
); | ||
// For `getState()` generate request serializer | ||
const fetchOptsSerializers = getFetchOptsSerializers<Api, ReqTypes>(routesData, reqSerializers); | ||
|
||
return { | ||
...client, | ||
|
||
// TODO: Debug the type issue | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you also think about how we would be able to incorporate this? ethereum/beacon-APIs#250 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We already have support for SZZ encoding for few endpoints, similarly we can support for others when required. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This beacon-apis PR is about systematically supporting SSZ for all endpoints, not just supporting one-by-one. Edit: I'm not suggesting to tackle this in this PR, just more an FYI of possible improvement that will be desired. |
||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-expect-error | ||
async getState(stateId: string, format?: StateFormat) { | ||
if (format === "ssz") { | ||
const buffer = await httpClient.arrayBuffer({ | ||
const res = await httpClient.arrayBuffer({ | ||
...fetchOptsSerializers.getState(stateId, format), | ||
timeoutMs: GET_STATE_TIMEOUT_MS, | ||
}); | ||
// Casting to any otherwise Typescript doesn't like the multi-type return | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-explicit-any | ||
return new Uint8Array(buffer) as any; | ||
} else { | ||
return client.getState(stateId, format); | ||
return { | ||
ok: true, | ||
response: new Uint8Array(res.body), | ||
status: res.status, | ||
} as ApiClientResponse<{[HttpStatusCode.OK]: Uint8Array}>; | ||
} | ||
return client.getState(stateId, format); | ||
}, | ||
async getStateV2(stateId: string, format?: StateFormat) { | ||
|
||
// TODO: Debug the type issue | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-expect-error | ||
async getStateV2(stateId: StateId, format?: StateFormat) { | ||
if (format === "ssz") { | ||
const buffer = await httpClient.arrayBuffer({ | ||
const res = await httpClient.arrayBuffer({ | ||
...fetchOptsSerializers.getStateV2(stateId, format), | ||
timeoutMs: GET_STATE_TIMEOUT_MS, | ||
}); | ||
// Casting to any otherwise Typescript doesn't like the multi-type return | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-explicit-any | ||
return new Uint8Array(buffer) as any; | ||
} else { | ||
return client.getStateV2(stateId, format); | ||
return {ok: true, response: new Uint8Array(res.body), status: res.status} as ApiClientResponse<{ | ||
[HttpStatusCode.OK]: Uint8Array; | ||
}>; | ||
} | ||
|
||
return client.getStateV2(stateId, format); | ||
}, | ||
}; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe also reference the similarity to the
Response
type.