-
-
Notifications
You must be signed in to change notification settings - Fork 289
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: download blocks as ssz (#5923)
* feat: getBlock api to support application/octet-stream header * fix: use specific handler for getBlock getBlockV2 * fix: build error * Revert "fix: build error" This reverts commit fbeb88a. * fix: infer returned type for getBlock apis * feat: add getBlock() client api supporting ssz * chore: remove comments
- Loading branch information
Showing
9 changed files
with
136 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,46 @@ | ||
import {ChainForkConfig} from "@lodestar/config"; | ||
import {Api, ReqTypes, routesData, getReqSerializers, getReturnTypes} from "../routes/beacon/index.js"; | ||
import {IHttpClient, generateGenericJsonClient} from "../../utils/client/index.js"; | ||
import {Api, ReqTypes, routesData, getReqSerializers, getReturnTypes, BlockId} from "../routes/beacon/index.js"; | ||
import {IHttpClient, generateGenericJsonClient, getFetchOptsSerializers} from "../../utils/client/index.js"; | ||
import {ResponseFormat} from "../../interfaces.js"; | ||
import {BlockResponse, BlockV2Response} from "../routes/beacon/block.js"; | ||
|
||
/** | ||
* REST HTTP client for beacon routes | ||
*/ | ||
export function getClient(config: ChainForkConfig, httpClient: IHttpClient): Api { | ||
const reqSerializers = getReqSerializers(config); | ||
const returnTypes = getReturnTypes(); | ||
// All routes return JSON, use a client auto-generator | ||
return generateGenericJsonClient<Api, ReqTypes>(routesData, reqSerializers, returnTypes, httpClient); | ||
// Some routes return JSON, use a client auto-generator | ||
const client = generateGenericJsonClient<Api, ReqTypes>(routesData, reqSerializers, returnTypes, httpClient); | ||
const fetchOptsSerializer = getFetchOptsSerializers<Api, ReqTypes>(routesData, reqSerializers); | ||
|
||
return { | ||
...client, | ||
async getBlock<T extends ResponseFormat = "json">(blockId: BlockId, format?: T) { | ||
if (format === "ssz") { | ||
const res = await httpClient.arrayBuffer({ | ||
...fetchOptsSerializer.getBlock(blockId, format), | ||
}); | ||
return { | ||
ok: true, | ||
response: new Uint8Array(res.body), | ||
status: res.status, | ||
} as BlockResponse<T>; | ||
} | ||
return client.getBlock(blockId, format); | ||
}, | ||
async getBlockV2<T extends ResponseFormat = "json">(blockId: BlockId, format?: T) { | ||
if (format === "ssz") { | ||
const res = await httpClient.arrayBuffer({ | ||
...fetchOptsSerializer.getBlockV2(blockId, format), | ||
}); | ||
return { | ||
ok: true, | ||
response: new Uint8Array(res.body), | ||
status: res.status, | ||
} as BlockV2Response<T>; | ||
} | ||
return client.getBlockV2(blockId, format); | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters