@lodestar/beacon-node
Version:
A Typescript implementation of the beacon chain
43 lines • 2.09 kB
JavaScript
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 }, serializedCache) {
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.message.slot >= blockSlot) {
throw new BlocksByRangeError({ code: BlocksByRangeErrorCode.BAD_SEQUENCE });
}
blocks.push(block);
// optionally cache the serialized response if the cache is available
serializedCache?.set(block, chunk.data);
if (blocks.length >= count) {
break; // Done, collected all blocks
}
}
return blocks;
}
export { BlocksByRangeErrorCode };
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