Skip to content

Commit

Permalink
Verify committee signatures in batch
Browse files Browse the repository at this point in the history
  • Loading branch information
twoeths committed Jun 3, 2021
1 parent 5554cd1 commit e83cd27
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function getAllBlockSignatureSetsExceptProposer(
if (computeEpochAtSlot(state.config, signedBlock.message.slot) >= state.config.params.ALTAIR_FORK_EPOCH) {
const syncCommitteeSignatureSet = getSyncCommitteeSignatureSet(
state as CachedBeaconState<altair.BeaconState>,
(signedBlock as altair.SignedBeaconBlock).message.body.syncAggregate
(signedBlock as altair.SignedBeaconBlock).message
);
// There may be no participants in this syncCommitteeSignature, so it must not be validated
if (syncCommitteeSignatureSet) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ export function processSyncCommittee(

// different from the spec but not sure how to get through signature verification for default/empty SyncAggregate in the spec test
if (verifySignatures) {
const signatureSet = getSyncCommitteeSignatureSet(state, syncAggregate, participantIndices);
// This is to conform to the spec - we want the signature to be verified
const block = {slot: state.slot, body: {syncAggregate}} as altair.BeaconBlock;
const signatureSet = getSyncCommitteeSignatureSet(state, block, participantIndices);
// When there's no participation we consider the signature valid and just ignore i
if (signatureSet !== null && !verifySignatureSet(signatureSet)) {
throw Error("Sync committee signature invalid");
Expand All @@ -39,13 +41,13 @@ export function processSyncCommittee(

export function getSyncCommitteeSignatureSet(
state: CachedBeaconState<altair.BeaconState>,
syncAggregate: altair.SyncAggregate,
block: altair.BeaconBlock,
/** Optional parameter to prevent computing it twice */
participantIndices?: number[]
): ISignatureSet | null {
const {config, epochCtx} = state;

const previousSlot = Math.max(state.slot, 1) - 1;
const {syncAggregate} = block.body;
const previousSlot = Math.max(block.slot, 1) - 1;
const rootSigned = getBlockRootAtSlot(config, state, previousSlot);

if (!participantIndices) {
Expand Down
31 changes: 14 additions & 17 deletions packages/lodestar/src/chain/blocks/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,20 @@ export async function processChainSegment(
}

try {
// It's important to process up to block slot to get through signature verification at fork transition
let preState = await regen.getBlockSlotState(firstBlock.message.parentRoot, firstBlock.message.slot);

// Verify the signature of the blocks, returning early if the signature is invalid.
let preState = await regen.getPreState(firstBlock.message);
for (const block of blocksInEpoch) {
preState = await runStateTransition({emitter, forkChoice, metrics}, checkpointStateCache, preState, {
reprocess: job.reprocess,
prefinalized: job.prefinalized,
signedBlock: block,
validProposerSignature: true,
validSignatures: true,
});
importedBlocks++;
// this avoids keeping our node busy processing blocks
await sleep(0);
}
// Verify the signature afterall bc all SyncCommittee signed roots are only known at this point
if (!job.validSignatures && !opts?.disableBlsBatchVerify) {
const signatureSets: ISignatureSet[] = [];
for (const block of blocksInEpoch) {
Expand All @@ -141,19 +151,6 @@ export async function processChainSegment(
});
}
}

for (const block of blocksInEpoch) {
preState = await runStateTransition({emitter, forkChoice, metrics}, checkpointStateCache, preState, {
reprocess: job.reprocess,
prefinalized: job.prefinalized,
signedBlock: block,
validProposerSignature: true,
validSignatures: true,
});
importedBlocks++;
// this avoids keeping our node busy processing blocks
await sleep(0);
}
} catch (e) {
if (e instanceof RegenError) {
throw new ChainSegmentError({
Expand Down

0 comments on commit e83cd27

Please sign in to comment.