-
-
Notifications
You must be signed in to change notification settings - Fork 306
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move proof endpoints to new namespace (#4771)
* Move proofs endpoints to new namespace * Rename proofs -> proof * Fix renaming issues Co-authored-by: Cayman <[email protected]>
- Loading branch information
1 parent
331a8e6
commit f74462e
Showing
23 changed files
with
212 additions
and
130 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
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,27 +1,13 @@ | ||
import {IChainForkConfig} from "@lodestar/config"; | ||
import {deserializeProof} from "@chainsafe/persistent-merkle-tree"; | ||
import {Api, ReqTypes, routesData, getReqSerializers, getReturnTypes} from "../routes/lightclient.js"; | ||
import {IHttpClient, getFetchOptsSerializers, generateGenericJsonClient} from "../../utils/client/index.js"; | ||
import {IHttpClient, generateGenericJsonClient} from "../../utils/client/index.js"; | ||
|
||
/** | ||
* REST HTTP client for lightclient routes | ||
*/ | ||
export function getClient(_config: IChainForkConfig, httpClient: IHttpClient): Api { | ||
const reqSerializers = getReqSerializers(); | ||
const returnTypes = getReturnTypes(); | ||
|
||
// Some routes return JSON, use a client auto-generator | ||
const client = generateGenericJsonClient<Api, ReqTypes>(routesData, reqSerializers, returnTypes, httpClient); | ||
// For `getStateProof()` generate request serializer | ||
const fetchOptsSerializers = getFetchOptsSerializers<Api, ReqTypes>(routesData, reqSerializers); | ||
|
||
return { | ||
...client, | ||
|
||
async getStateProof(stateId, paths) { | ||
const buffer = await httpClient.arrayBuffer(fetchOptsSerializers.getStateProof(stateId, paths)); | ||
const proof = deserializeProof(new Uint8Array(buffer)); | ||
return {data: proof}; | ||
}, | ||
}; | ||
// All routes return JSON, use a client auto-generator | ||
return generateGenericJsonClient<Api, ReqTypes>(routesData, reqSerializers, returnTypes, httpClient); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import {IChainForkConfig} from "@lodestar/config"; | ||
import {deserializeProof} from "@chainsafe/persistent-merkle-tree"; | ||
import {Api, ReqTypes, routesData, getReqSerializers} from "../routes/proof.js"; | ||
import {IHttpClient, getFetchOptsSerializers} from "../../utils/client/index.js"; | ||
|
||
/** | ||
* REST HTTP client for lightclient routes | ||
*/ | ||
export function getClient(_config: IChainForkConfig, httpClient: IHttpClient): Api { | ||
const reqSerializers = getReqSerializers(); | ||
|
||
// For `getStateProof()` generate request serializer | ||
const fetchOptsSerializers = getFetchOptsSerializers<Api, ReqTypes>(routesData, reqSerializers); | ||
|
||
return { | ||
async getStateProof(stateId, paths) { | ||
const buffer = await httpClient.arrayBuffer(fetchOptsSerializers.getStateProof(stateId, paths)); | ||
const proof = deserializeProof(new Uint8Array(buffer)); | ||
return {data: proof}; | ||
}, | ||
}; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import {JsonPath} from "@chainsafe/ssz"; | ||
import {Proof} from "@chainsafe/persistent-merkle-tree"; | ||
import {ReturnTypes, RoutesData, Schema, sameType, ReqSerializers} from "../../utils/index.js"; | ||
import {queryParseProofPathsArr, querySerializeProofPathsArr} from "../../utils/serdes.js"; | ||
|
||
// See /packages/api/src/routes/index.ts for reasoning and instructions to add new routes | ||
|
||
export type Api = { | ||
/** | ||
* Returns a multiproof of `jsonPaths` at the requested `stateId`. | ||
* The requested `stateId` may not be available. Regular nodes only keep recent states in memory. | ||
*/ | ||
getStateProof(stateId: string, jsonPaths: JsonPath[]): Promise<{data: Proof}>; | ||
}; | ||
|
||
/** | ||
* Define javascript values for each route | ||
*/ | ||
export const routesData: RoutesData<Api> = { | ||
getStateProof: {url: "/eth/v0/beacon/proof/state/{state_id}", method: "GET"}, | ||
}; | ||
|
||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
export type ReqTypes = { | ||
getStateProof: {params: {state_id: string}; query: {paths: string[]}}; | ||
}; | ||
|
||
export function getReqSerializers(): ReqSerializers<Api, ReqTypes> { | ||
return { | ||
getStateProof: { | ||
writeReq: (state_id, paths) => ({params: {state_id}, query: {paths: querySerializeProofPathsArr(paths)}}), | ||
parseReq: ({params, query}) => [params.state_id, queryParseProofPathsArr(query.paths)], | ||
schema: {params: {state_id: Schema.StringRequired}, body: Schema.AnyArray}, | ||
}, | ||
}; | ||
} | ||
|
||
export function getReturnTypes(): ReturnTypes<Api> { | ||
return { | ||
// Just sent the proof JSON as-is | ||
getStateProof: sameType(), | ||
}; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,8 @@ | ||
import {IChainForkConfig} from "@lodestar/config"; | ||
import {serializeProof} from "@chainsafe/persistent-merkle-tree"; | ||
import {Api, ReqTypes, routesData, getReturnTypes, getReqSerializers} from "../routes/lightclient.js"; | ||
import {ServerRoutes, getGenericJsonServer} from "../../utils/server/index.js"; | ||
|
||
export function getRoutes(config: IChainForkConfig, api: Api): ServerRoutes<Api, ReqTypes> { | ||
const reqSerializers = getReqSerializers(); | ||
const serverRoutes = getGenericJsonServer<Api, ReqTypes>( | ||
{routesData, getReturnTypes, getReqSerializers}, | ||
config, | ||
api | ||
); | ||
|
||
return { | ||
...serverRoutes, | ||
|
||
// Non-JSON routes. Return binary | ||
getStateProof: { | ||
...serverRoutes.getStateProof, | ||
handler: async (req) => { | ||
const args = reqSerializers.getStateProof.parseReq(req); | ||
const {data: proof} = await api.getStateProof(...args); | ||
// Fastify 3.x.x will automatically add header `Content-Type: application/octet-stream` if Buffer | ||
return Buffer.from(serializeProof(proof)); | ||
}, | ||
}, | ||
}; | ||
// All routes return JSON, use a server auto-generator | ||
return getGenericJsonServer<Api, ReqTypes>({routesData, getReturnTypes, getReqSerializers}, config, api); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import {IChainForkConfig} from "@lodestar/config"; | ||
import {serializeProof} from "@chainsafe/persistent-merkle-tree"; | ||
import {Api, ReqTypes, routesData, getReturnTypes, getReqSerializers} from "../routes/proof.js"; | ||
import {ServerRoutes, getGenericJsonServer} from "../../utils/server/index.js"; | ||
|
||
export function getRoutes(config: IChainForkConfig, api: Api): ServerRoutes<Api, ReqTypes> { | ||
const reqSerializers = getReqSerializers(); | ||
const serverRoutes = getGenericJsonServer<Api, ReqTypes>( | ||
{routesData, getReturnTypes, getReqSerializers}, | ||
config, | ||
api | ||
); | ||
|
||
return { | ||
// Non-JSON routes. Return binary | ||
getStateProof: { | ||
...serverRoutes.getStateProof, | ||
handler: async (req) => { | ||
const args = reqSerializers.getStateProof.parseReq(req); | ||
const {data: proof} = await api.getStateProof(...args); | ||
// Fastify 3.x.x will automatically add header `Content-Type: application/octet-stream` if Buffer | ||
return Buffer.from(serializeProof(proof)); | ||
}, | ||
}, | ||
}; | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/api/test/unit/beacon/genericServerTest/proofs.test.ts
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import {config} from "@lodestar/config/default"; | ||
import {Api, ReqTypes} from "../../../../src/beacon/routes/proof.js"; | ||
import {getClient} from "../../../../src/beacon/client/proof.js"; | ||
import {getRoutes} from "../../../../src/beacon/server/proof.js"; | ||
import {runGenericServerTest} from "../../../utils/genericServerTest.js"; | ||
import {testData} from "../testData/proofs.js"; | ||
|
||
describe("beacon / proofs", () => { | ||
runGenericServerTest<Api, ReqTypes>(config, getClient, getRoutes, testData); | ||
}); |
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
Oops, something went wrong.