@lodestar/beacon-node
Version:
A Typescript implementation of the beacon chain
36 lines • 1.74 kB
JavaScript
import { GENESIS_SLOT } from "@lodestar/params";
import { getBlockProposerSignatureSet } from "@lodestar/state-transition";
import { ssz } from "@lodestar/types";
import { BackfillSyncError, BackfillSyncErrorCode } from "./errors.js";
export function verifyBlockSequence(config, blocks, anchorRoot) {
let nextRoot = anchorRoot;
let nextAnchor = null;
const verifiedBlocks = [];
for (const block of blocks.reverse()) {
const blockRoot = config.getForkTypes(block.data.message.slot).BeaconBlock.hashTreeRoot(block.data.message);
if (!ssz.Root.equals(blockRoot, nextRoot)) {
if (ssz.Root.equals(nextRoot, anchorRoot)) {
throw new BackfillSyncError({ code: BackfillSyncErrorCode.NOT_ANCHORED });
}
return { nextAnchor, verifiedBlocks, error: BackfillSyncErrorCode.NOT_LINEAR };
}
verifiedBlocks.push(block);
nextAnchor = { block: block.data, slot: block.data.message.slot, root: nextRoot };
nextRoot = block.data.message.parentRoot;
}
return { nextAnchor, verifiedBlocks };
}
export async function verifyBlockProposerSignature(bls, state, blocks) {
if (blocks.length === 1 && blocks[0].data.message.slot === GENESIS_SLOT)
return;
const signatures = blocks.reduce((sigs, block) => {
// genesis block doesn't have valid signature
if (block.data.message.slot !== GENESIS_SLOT)
sigs.push(getBlockProposerSignatureSet(state, block.data));
return sigs;
}, []);
if (!(await bls.verifySignatureSets(signatures, { batchable: true }))) {
throw new BackfillSyncError({ code: BackfillSyncErrorCode.INVALID_SIGNATURE });
}
}
//# sourceMappingURL=verify.js.map