UNPKG

@lodestar/beacon-node

Version:

A Typescript implementation of the beacon chain

52 lines 1.91 kB
import { blockToHeader } from "@lodestar/state-transition"; import { GENESIS_SLOT } from "../../../../constants/index.js"; import { rootHexRegex } from "../../../../eth1/provider/utils.js"; import { ApiError, ValidationError } from "../../errors.js"; export function toBeaconHeaderResponse(config, block, canonical = false) { return { root: config.getForkTypes(block.message.slot).BeaconBlock.hashTreeRoot(block.message), canonical, header: { message: blockToHeader(config, block.message), signature: block.signature, }, }; } export function resolveBlockId(forkChoice, blockId) { blockId = String(blockId).toLowerCase(); if (blockId === "head") { return forkChoice.getHead().blockRoot; } if (blockId === "genesis") { return GENESIS_SLOT; } if (blockId === "finalized") { return forkChoice.getFinalizedBlock().blockRoot; } if (blockId === "justified") { return forkChoice.getJustifiedBlock().blockRoot; } if (blockId.startsWith("0x")) { if (!rootHexRegex.test(blockId)) { throw new ValidationError(`Invalid block id '${blockId}'`, "blockId"); } return blockId; } // block id must be slot const blockSlot = parseInt(blockId, 10); if (Number.isNaN(blockSlot) && Number.isNaN(blockSlot - 0)) { throw new ValidationError(`Invalid block id '${blockId}'`, "blockId"); } return blockSlot; } export async function getBlockResponse(chain, blockId) { const rootOrSlot = resolveBlockId(chain.forkChoice, blockId); const res = typeof rootOrSlot === "string" ? await chain.getBlockByRoot(rootOrSlot) : await chain.getCanonicalBlockAtSlot(rootOrSlot); if (!res) { throw new ApiError(404, `Block not found for id '${blockId}'`); } return res; } //# sourceMappingURL=utils.js.map