UNPKG

@lodestar/beacon-node

Version:

A Typescript implementation of the beacon chain

40 lines 1.96 kB
import { LodestarError } from "@lodestar/utils"; import { ReqRespMethod, responseSszTypeByMethod } from "../types.js"; import { sszDeserializeResponse } from "./collect.js"; /** * Asserts a response from BeaconBlocksByRange respects the request and is sequential * Note: MUST allow missing block for skipped slots. */ export async function collectSequentialBlocksInRange(blockStream, { count, startSlot }) { const blocks = []; for await (const chunk of blockStream) { const blockType = responseSszTypeByMethod[ReqRespMethod.BeaconBlocksByRange](chunk.fork, chunk.protocolVersion); const block = sszDeserializeResponse(blockType, chunk.data); const blockSlot = block.message.slot; // Note: step is deprecated and assumed to be 1 if (blockSlot >= startSlot + count) { throw new BlocksByRangeError({ code: BlocksByRangeErrorCode.OVER_MAX_SLOT }); } if (blockSlot < startSlot) { throw new BlocksByRangeError({ code: BlocksByRangeErrorCode.UNDER_START_SLOT }); } const prevBlock = blocks.at(-1); if (prevBlock && prevBlock.data.message.slot >= blockSlot) { throw new BlocksByRangeError({ code: BlocksByRangeErrorCode.BAD_SEQUENCE }); } blocks.push({ data: block, bytes: chunk.data }); if (blocks.length >= count) { break; // Done, collected all blocks } } return blocks; } export var BlocksByRangeErrorCode; (function (BlocksByRangeErrorCode) { BlocksByRangeErrorCode["UNDER_START_SLOT"] = "BLOCKS_BY_RANGE_ERROR_UNDER_START_SLOT"; BlocksByRangeErrorCode["OVER_MAX_SLOT"] = "BLOCKS_BY_RANGE_ERROR_OVER_MAX_SLOT"; BlocksByRangeErrorCode["BAD_SEQUENCE"] = "BLOCKS_BY_RANGE_ERROR_BAD_SEQUENCE"; })(BlocksByRangeErrorCode || (BlocksByRangeErrorCode = {})); export class BlocksByRangeError extends LodestarError { } //# sourceMappingURL=collectSequentialBlocksInRange.js.map