UNPKG

@lodestar/api

Version:

A Typescript REST client for the Ethereum Consensus API

188 lines • 7.86 kB
import { ContainerType, OptionalType } from "@chainsafe/ssz"; import { ArrayOf, StringType, ssz, stringType } from "@lodestar/types"; import { EmptyMetaCodec, EmptyRequestCodec, EmptyResponseCodec, JsonOnlyResponseCodec, } from "../../utils/codecs.js"; import { HttpStatusCode } from "../../utils/httpStatusCode.js"; import { Schema } from "../../utils/index.js"; import { WireFormat } from "../../utils/wireFormat.js"; // TODO: Workaround for tsgo import-elision bug: ensure this is treated as a runtime value. // https://github.com/microsoft/typescript-go/issues/2212 void HttpStatusCode; export const NetworkIdentityType = new ContainerType({ /** Cryptographic hash of a peer’s public key. [Read more](https://docs.libp2p.io/concepts/peer-id/) */ peerId: stringType, /** Ethereum node record. [Read more](https://eips.ethereum.org/EIPS/eip-778) */ enr: stringType, p2pAddresses: ArrayOf(stringType), discoveryAddresses: ArrayOf(stringType), // TODO Fulu: replace with `ssz.fulu.Metadata` once `custody_group_count` is more widely supported /** Based on Ethereum Consensus [Metadata object](https://github.com/ethereum/consensus-specs/blob/v1.1.10/specs/phase0/p2p-interface.md#metadata) */ metadata: ssz.altair.Metadata, }, { jsonCase: "eth2" }); export const PeerCountType = new ContainerType({ disconnected: ssz.UintNum64, connecting: ssz.UintNum64, connected: ssz.UintNum64, disconnecting: ssz.UintNum64, }, { jsonCase: "eth2" }); export const SyncingStatusType = new ContainerType({ /** Head slot node is trying to reach */ headSlot: ssz.Slot, /** How many slots node needs to process to reach head. 0 if synced. */ syncDistance: ssz.Slot, /** Set to true if the node is syncing, false if the node is synced. */ isSyncing: ssz.Boolean, /** Set to true if the node is optimistically tracking head. */ isOptimistic: ssz.Boolean, /** Set to true if the connected el client is offline */ elOffline: ssz.Boolean, }, { jsonCase: "eth2" }); export const NodePeerType = new ContainerType({ peerId: stringType, enr: new OptionalType(stringType), lastSeenP2pAddress: stringType, state: new StringType(), // the spec does not specify direction for a disconnected peer, lodestar uses null in that case direction: new OptionalType(new StringType()), }, { jsonCase: "eth2" }); export const NodePeersType = ArrayOf(NodePeerType); export { NodeHealth }; var NodeHealth; (function (NodeHealth) { NodeHealth["READY"] = HttpStatusCode.OK; if (typeof NodeHealth.READY !== "string") NodeHealth[NodeHealth.READY] = "READY"; NodeHealth["SYNCING"] = HttpStatusCode.PARTIAL_CONTENT; if (typeof NodeHealth.SYNCING !== "string") NodeHealth[NodeHealth.SYNCING] = "SYNCING"; NodeHealth["NOT_INITIALIZED_OR_ISSUES"] = HttpStatusCode.SERVICE_UNAVAILABLE; if (typeof NodeHealth.NOT_INITIALIZED_OR_ISSUES !== "string") NodeHealth[NodeHealth.NOT_INITIALIZED_OR_ISSUES] = "NOT_INITIALIZED_OR_ISSUES"; })(NodeHealth || (NodeHealth = {})); export { ClientCode }; /** * Client code as defined in https://github.com/ethereum/execution-apis/blob/dc4dbca37ef8697d782f431af19120beaf5517f5/src/engine/identification.md#clientcode * ClientCode.XX is dedicated to other clients which do not have their own code */ var ClientCode; (function (ClientCode) { ClientCode["BU"] = "BU"; ClientCode["EJ"] = "EJ"; ClientCode["EG"] = "EG"; ClientCode["GE"] = "GE"; ClientCode["GR"] = "GR"; ClientCode["LH"] = "LH"; ClientCode["LS"] = "LS"; ClientCode["NM"] = "NM"; ClientCode["NB"] = "NB"; ClientCode["TE"] = "TE"; ClientCode["TK"] = "TK"; ClientCode["PM"] = "PM"; ClientCode["RH"] = "RH"; ClientCode["XX"] = "XX"; })(ClientCode || (ClientCode = {})); export function getDefinitions(_config) { return { getNetworkIdentity: { url: "/eth/v1/node/identity", method: "GET", req: EmptyRequestCodec, resp: { onlySupport: WireFormat.json, // TODO Fulu: clean this up data: { ...JsonOnlyResponseCodec.data, toJson: (data) => { const json = NetworkIdentityType.toJson(data); const { custodyGroupCount } = data.metadata; json.metadata.custody_group_count = custodyGroupCount !== undefined ? String(custodyGroupCount) : undefined; return json; }, fromJson: (json) => { const data = NetworkIdentityType.fromJson(json); const { metadata: { custody_group_count }, } = json; data.metadata.custodyGroupCount = custody_group_count !== undefined ? parseInt(custody_group_count) : undefined; return data; }, }, meta: EmptyMetaCodec, }, }, getPeers: { url: "/eth/v1/node/peers", method: "GET", req: { writeReq: ({ state, direction }) => ({ query: { state, direction } }), parseReq: ({ query }) => ({ state: query.state, direction: query.direction }), schema: { query: { state: Schema.StringArray, direction: Schema.StringArray } }, }, resp: { data: NodePeersType, meta: { toJson: (d) => d, fromJson: (d) => ({ count: d.count }), toHeadersObject: () => ({}), fromHeaders: () => ({}), }, transform: { toResponse: (data, meta) => ({ data, meta }), fromResponse: (resp) => resp, }, onlySupport: WireFormat.json, }, }, getPeer: { url: "/eth/v1/node/peers/{peer_id}", method: "GET", req: { writeReq: ({ peerId }) => ({ params: { peer_id: peerId } }), parseReq: ({ params }) => ({ peerId: params.peer_id }), schema: { params: { peer_id: Schema.StringRequired } }, }, resp: { data: NodePeerType, meta: EmptyMetaCodec, onlySupport: WireFormat.json, }, }, getPeerCount: { url: "/eth/v1/node/peer_count", method: "GET", req: EmptyRequestCodec, resp: { data: PeerCountType, meta: EmptyMetaCodec, }, }, getNodeVersion: { url: "/eth/v1/node/version", method: "GET", req: EmptyRequestCodec, resp: JsonOnlyResponseCodec, }, getNodeVersionV2: { url: "/eth/v2/node/version", method: "GET", req: EmptyRequestCodec, resp: JsonOnlyResponseCodec, }, getSyncingStatus: { url: "/eth/v1/node/syncing", method: "GET", req: EmptyRequestCodec, resp: { data: SyncingStatusType, meta: EmptyMetaCodec, }, }, getHealth: { url: "/eth/v1/node/health", method: "GET", req: { writeReq: ({ syncingStatus }) => ({ query: { syncing_status: syncingStatus } }), parseReq: ({ query }) => ({ syncingStatus: query.syncing_status }), schema: { query: { syncing_status: Schema.Uint } }, }, resp: EmptyResponseCodec, }, }; } //# sourceMappingURL=node.js.map