Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions packages/beacon-node/src/chain/blocks/verifyBlocksSignatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export async function verifyBlocksSignatures(
): Promise<{verifySignaturesTime: number}> {
const isValidPromises: Promise<boolean>[] = [];
const recvToValLatency = Date.now() / 1000 - (opts.seenTimestampSec ?? Date.now() / 1000);
const currentSyncCommitteeIndexed = preState0.epochCtx.currentSyncCommitteeIndexed;

// Verifies signatures after running state transition, so all SyncCommittee signed roots are known at this point.
// We must ensure block.slot <= state.slot before running getAllBlockSignatureSets().
Expand All @@ -41,9 +42,16 @@ export async function verifyBlocksSignatures(
: //
// Verify signatures per block to track which block is invalid
bls.verifySignatureSets(
getBlockSignatureSets(config, index2pubkey, preState0, block, indexedAttestationsByBlock[i], {
skipProposerSignature: opts.validProposerSignature,
})
getBlockSignatureSets(
config,
index2pubkey,
currentSyncCommitteeIndexed,
block,
indexedAttestationsByBlock[i],
{
skipProposerSignature: opts.validProposerSignature,
}
)
);

// getBlockSignatureSets() takes 45ms in benchmarks for 2022Q2 mainnet blocks (100 sigs). When syncing a 32 blocks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ export async function validateAttesterSlashing(
});
}

const signatureSets = getAttesterSlashingSignatureSets(chain.config, chain.index2pubkey, state, attesterSlashing);
const signatureSets = getAttesterSlashingSignatureSets(
chain.config,
chain.index2pubkey,
state.slot,
attesterSlashing
);
if (!(await chain.bls.verifySignatureSets(signatureSets, {batchable: true, priority: prioritizeBls}))) {
throw new AttesterSlashingError(GossipAction.REJECT, {
code: AttesterSlashingErrorCode.INVALID,
Expand Down
2 changes: 1 addition & 1 deletion packages/beacon-node/src/chain/validation/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export async function validateGossipBlock(

// [REJECT] The proposer signature, signed_beacon_block.signature, is valid with respect to the proposer_index pubkey.
if (!chain.seenBlockInputCache.isVerifiedProposerSignature(blockSlot, blockRoot, signedBlock.signature)) {
const signatureSet = getBlockProposerSignatureSet(chain.config, chain.index2pubkey, blockState, signedBlock);
const signatureSet = getBlockProposerSignatureSet(chain.config, chain.index2pubkey, signedBlock);
// Don't batch so verification is not delayed
if (!(await chain.bls.verifySignatureSets([signatureSet], {verifyOnMainThread: true}))) {
throw new BlockGossipError(GossipAction.REJECT, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ async function validateProposerSlashing(
});
}

const signatureSets = getProposerSlashingSignatureSets(chain.config, chain.index2pubkey, state, proposerSlashing);
const signatureSets = getProposerSlashingSignatureSets(
chain.config,
chain.index2pubkey,
state.slot,
proposerSlashing
);
if (!(await chain.bls.verifySignatureSets(signatureSets, {batchable: true, priority: prioritizeBls}))) {
throw new ProposerSlashingError(GossipAction.REJECT, {
code: ProposerSlashingErrorCode.INVALID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ async function validateVoluntaryExit(
});
}

const signatureSet = getVoluntaryExitSignatureSet(chain.config, chain.index2pubkey, state, voluntaryExit);
const signatureSet = getVoluntaryExitSignatureSet(chain.config, chain.index2pubkey, state.slot, voluntaryExit);
if (!(await chain.bls.verifySignatureSets([signatureSet], {batchable: true, priority: prioritizeBls}))) {
throw new VoluntaryExitError(GossipAction.REJECT, {
code: VoluntaryExitErrorCode.INVALID_SIGNATURE,
Expand Down
3 changes: 1 addition & 2 deletions packages/beacon-node/src/sync/backfill/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ export async function verifyBlockProposerSignature(
if (blocks.length === 1 && blocks[0].message.slot === GENESIS_SLOT) return;
const signatures = blocks.reduce((sigs: ISignatureSet[], block) => {
// genesis block doesn't have valid signature
if (block.message.slot !== GENESIS_SLOT)
sigs.push(getBlockProposerSignatureSet(config, index2pubkey, state, block));
if (block.message.slot !== GENESIS_SLOT) sigs.push(getBlockProposerSignatureSet(config, index2pubkey, block));
return sigs;
}, []);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function isValidIndexedAttestation(
}

if (verifySignature) {
return verifySignatureSet(getIndexedAttestationSignatureSet(config, index2pubkey, state, indexedAttestation));
return verifySignatureSet(getIndexedAttestationSignatureSet(config, index2pubkey, state.slot, indexedAttestation));
}
return true;
}
Expand All @@ -38,7 +38,9 @@ export function isValidIndexedAttestationBigint(
}

if (verifySignature) {
return verifySignatureSet(getIndexedAttestationBigintSignatureSet(config, index2pubkey, state, indexedAttestation));
return verifySignatureSet(
getIndexedAttestationBigintSignatureSet(config, index2pubkey, state.slot, indexedAttestation)
);
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export function processAttestationsAltair(
const sigSet = getAttestationWithIndicesSignatureSet(
state.config,
epochCtx.index2pubkey,
state,
state.slot,
attestation,
attestingIndices
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export function assertValidProposerSlashing(
const signatureSets = getProposerSlashingSignatureSets(
state.config,
state.epochCtx.index2pubkey,
state,
state.slot,
proposerSlashing
);
for (let i = 0; i < signatureSets.length; i++) {
Expand Down
4 changes: 2 additions & 2 deletions packages/state-transition/src/block/processRandao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import {getRandaoMix} from "../util/index.js";
* PERF: Fixed work independent of block contents.
*/
export function processRandao(state: CachedBeaconStateAllForks, block: BeaconBlock, verifySignature = true): void {
const {epochCtx} = state;
const {epochCtx, config} = state;
const epoch = epochCtx.epoch;
const randaoReveal = block.body.randaoReveal;

// verify RANDAO reveal
if (verifySignature && !verifyRandaoSignature(state.config, epochCtx.index2pubkey, state, block)) {
if (verifySignature && !verifyRandaoSignature(config, epochCtx.index2pubkey, block)) {
throw new Error("RANDAO reveal is an invalid signature");
}

Expand Down
11 changes: 7 additions & 4 deletions packages/state-transition/src/block/processSyncCommittee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {BeaconConfig} from "@lodestar/config";
import {DOMAIN_SYNC_COMMITTEE, SYNC_COMMITTEE_SIZE} from "@lodestar/params";
import {altair, ssz} from "@lodestar/types";
import {Index2PubkeyCache} from "../cache/pubkeyCache.js";
import {SyncCommitteeCache} from "../cache/syncCommitteeCache.js";
import {G2_POINT_AT_INFINITY} from "../constants/index.js";
import {CachedBeaconStateAllForks} from "../types.js";
import {
Expand All @@ -28,7 +29,7 @@ export function processSyncAggregate(
const signatureSet = getSyncCommitteeSignatureSet(
state.config,
state.epochCtx.index2pubkey,
state,
state.epochCtx.currentSyncCommitteeIndexed,
block,
participantIndices
);
Expand Down Expand Up @@ -73,7 +74,7 @@ export function processSyncAggregate(
export function getSyncCommitteeSignatureSet(
config: BeaconConfig,
index2pubkey: Index2PubkeyCache,
state: CachedBeaconStateAllForks,
currentSyncCommitteeIndexed: SyncCommitteeCache,
block: altair.BeaconBlock,
/** Optional parameter to prevent computing it twice */
participantIndices?: number[]
Expand Down Expand Up @@ -101,7 +102,7 @@ export function getSyncCommitteeSignatureSet(
const rootSigned = block.parentRoot;

if (!participantIndices) {
const committeeIndices = state.epochCtx.currentSyncCommitteeIndexed.validatorIndices;
const committeeIndices = currentSyncCommitteeIndexed.validatorIndices;
participantIndices = syncAggregate.syncCommitteeBits.intersectValues(committeeIndices);
}

Expand All @@ -115,7 +116,9 @@ export function getSyncCommitteeSignatureSet(
throw Error("Empty sync committee signature is not infinity");
}

const domain = config.getDomain(state.slot, DOMAIN_SYNC_COMMITTEE, previousSlot);
// the getDomain() api requires the state slot as 1st param, however it's the same to block.slot in state-transition
// and the same epoch when we verify blocks in batch in beacon-node. So we can safely use block.slot here.
const domain = config.getDomain(block.slot, DOMAIN_SYNC_COMMITTEE, previousSlot);

return {
type: SignatureSetType.aggregate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export function getVoluntaryExitValidity(

if (
verifySignature &&
!verifyVoluntaryExitSignature(state.config, epochCtx.index2pubkey, state, signedVoluntaryExit)
!verifyVoluntaryExitSignature(state.config, epochCtx.index2pubkey, state.slot, signedVoluntaryExit)
) {
return VoluntaryExitValidity.invalidSignature;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,43 @@
import {BeaconConfig} from "@lodestar/config";
import {DOMAIN_BEACON_ATTESTER} from "@lodestar/params";
import {AttesterSlashing, IndexedAttestationBigint, SignedBeaconBlock, ssz} from "@lodestar/types";
import {AttesterSlashing, IndexedAttestationBigint, SignedBeaconBlock, Slot, ssz} from "@lodestar/types";
import {Index2PubkeyCache} from "../cache/pubkeyCache.js";
import {CachedBeaconStateAllForks} from "../types.js";
import {ISignatureSet, SignatureSetType, computeSigningRoot, computeStartSlotAtEpoch} from "../util/index.js";

/** Get signature sets from all AttesterSlashing objects in a block */
export function getAttesterSlashingsSignatureSets(
config: BeaconConfig,
index2pubkey: Index2PubkeyCache,
state: CachedBeaconStateAllForks,
signedBlock: SignedBeaconBlock
): ISignatureSet[] {
// the getDomain() api requires the state slot as 1st param, however it's the same to block.slot in state-transition
// and the same epoch when we verify blocks in batch in beacon-node. So we can safely use block.slot here.
const blockSlot = signedBlock.message.slot;
return signedBlock.message.body.attesterSlashings.flatMap((attesterSlashing) =>
getAttesterSlashingSignatureSets(config, index2pubkey, state, attesterSlashing)
getAttesterSlashingSignatureSets(config, index2pubkey, blockSlot, attesterSlashing)
);
}

/** Get signature sets from a single AttesterSlashing object */
export function getAttesterSlashingSignatureSets(
config: BeaconConfig,
index2pubkey: Index2PubkeyCache,
state: CachedBeaconStateAllForks,
stateSlot: Slot,
attesterSlashing: AttesterSlashing
): ISignatureSet[] {
return [attesterSlashing.attestation1, attesterSlashing.attestation2].map((attestation) =>
getIndexedAttestationBigintSignatureSet(config, index2pubkey, state, attestation)
getIndexedAttestationBigintSignatureSet(config, index2pubkey, stateSlot, attestation)
);
}

export function getIndexedAttestationBigintSignatureSet(
config: BeaconConfig,
index2pubkey: Index2PubkeyCache,
state: CachedBeaconStateAllForks,
stateSlot: Slot,
indexedAttestation: IndexedAttestationBigint
): ISignatureSet {
const slot = computeStartSlotAtEpoch(Number(indexedAttestation.data.target.epoch as bigint));
const domain = config.getDomain(state.slot, DOMAIN_BEACON_ATTESTER, slot);
const messageSlot = computeStartSlotAtEpoch(Number(indexedAttestation.data.target.epoch as bigint));
const domain = config.getDomain(stateSlot, DOMAIN_BEACON_ATTESTER, messageSlot);

return {
type: SignatureSetType.aggregate,
Expand Down
22 changes: 11 additions & 11 deletions packages/state-transition/src/signatureSets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {ForkSeq} from "@lodestar/params";
import {IndexedAttestation, SignedBeaconBlock, altair, capella} from "@lodestar/types";
import {getSyncCommitteeSignatureSet} from "../block/processSyncCommittee.js";
import {Index2PubkeyCache} from "../cache/pubkeyCache.js";
import {CachedBeaconStateAllForks, CachedBeaconStateAltair} from "../types.js";
import {SyncCommitteeCache} from "../cache/syncCommitteeCache.js";
import {ISignatureSet} from "../util/index.js";
import {getAttesterSlashingsSignatureSets} from "./attesterSlashings.js";
import {getBlsToExecutionChangeSignatureSets} from "./blsToExecutionChange.js";
Expand All @@ -29,7 +29,7 @@ export * from "./voluntaryExits.js";
export function getBlockSignatureSets(
config: BeaconConfig,
index2pubkey: Index2PubkeyCache,
state: CachedBeaconStateAllForks,
currentSyncCommitteeIndexed: SyncCommitteeCache,
signedBlock: SignedBeaconBlock,
indexedAttestations: IndexedAttestation[],
opts?: {
Expand All @@ -38,26 +38,26 @@ export function getBlockSignatureSets(
}
): ISignatureSet[] {
// fork based validations
const fork = state.config.getForkSeq(signedBlock.message.slot);
const fork = config.getForkSeq(signedBlock.message.slot);

const signatureSets = [
getRandaoRevealSignatureSet(config, index2pubkey, state, signedBlock.message),
...getProposerSlashingsSignatureSets(config, index2pubkey, state, signedBlock),
...getAttesterSlashingsSignatureSets(config, index2pubkey, state, signedBlock),
...getAttestationsSignatureSets(config, index2pubkey, state, signedBlock, indexedAttestations),
...getVoluntaryExitsSignatureSets(config, index2pubkey, state, signedBlock),
getRandaoRevealSignatureSet(config, index2pubkey, signedBlock.message),
...getProposerSlashingsSignatureSets(config, index2pubkey, signedBlock),
...getAttesterSlashingsSignatureSets(config, index2pubkey, signedBlock),
...getAttestationsSignatureSets(config, index2pubkey, signedBlock, indexedAttestations),
...getVoluntaryExitsSignatureSets(config, index2pubkey, signedBlock),
];

if (!opts?.skipProposerSignature) {
signatureSets.push(getBlockProposerSignatureSet(config, index2pubkey, state, signedBlock));
signatureSets.push(getBlockProposerSignatureSet(config, index2pubkey, signedBlock));
}

// Only after altair fork, validate tSyncCommitteeSignature
if (fork >= ForkSeq.altair) {
const syncCommitteeSignatureSet = getSyncCommitteeSignatureSet(
config,
index2pubkey,
state as CachedBeaconStateAltair,
currentSyncCommitteeIndexed,
(signedBlock as altair.SignedBeaconBlock).message
);
// There may be no participants in this syncCommitteeSignature, so it must not be validated
Expand All @@ -69,7 +69,7 @@ export function getBlockSignatureSets(
// only after capella fork
if (fork >= ForkSeq.capella) {
const blsToExecutionChangeSignatureSets = getBlsToExecutionChangeSignatureSets(
state.config,
config,
signedBlock as capella.SignedBeaconBlock
);
if (blsToExecutionChangeSignatureSets.length > 0) {
Expand Down
23 changes: 12 additions & 11 deletions packages/state-transition/src/signatureSets/indexedAttestation.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {BeaconConfig} from "@lodestar/config";
import {DOMAIN_BEACON_ATTESTER} from "@lodestar/params";
import {IndexedAttestation, SignedBeaconBlock, phase0, ssz} from "@lodestar/types";
import {IndexedAttestation, SignedBeaconBlock, Slot, phase0, ssz} from "@lodestar/types";
import {Index2PubkeyCache} from "../cache/pubkeyCache.js";
import {CachedBeaconStateAllForks} from "../types.js";
import {
ISignatureSet,
computeSigningRoot,
Expand All @@ -12,39 +11,39 @@ import {

export function getAttestationDataSigningRoot(
config: BeaconConfig,
state: CachedBeaconStateAllForks,
stateSlot: Slot,
data: phase0.AttestationData
): Uint8Array {
const slot = computeStartSlotAtEpoch(data.target.epoch);
const domain = config.getDomain(state.slot, DOMAIN_BEACON_ATTESTER, slot);
const messageSlot = computeStartSlotAtEpoch(data.target.epoch);
const domain = config.getDomain(stateSlot, DOMAIN_BEACON_ATTESTER, messageSlot);

return computeSigningRoot(ssz.phase0.AttestationData, data, domain);
}

export function getAttestationWithIndicesSignatureSet(
config: BeaconConfig,
index2pubkey: Index2PubkeyCache,
state: CachedBeaconStateAllForks,
stateSlot: Slot,
attestation: Pick<phase0.Attestation, "data" | "signature">,
attestingIndices: number[]
): ISignatureSet {
return createAggregateSignatureSetFromComponents(
attestingIndices.map((i) => index2pubkey[i]),
getAttestationDataSigningRoot(config, state, attestation.data),
getAttestationDataSigningRoot(config, stateSlot, attestation.data),
attestation.signature
);
}

export function getIndexedAttestationSignatureSet(
config: BeaconConfig,
index2pubkey: Index2PubkeyCache,
state: CachedBeaconStateAllForks,
stateSlot: Slot,
indexedAttestation: IndexedAttestation
): ISignatureSet {
return getAttestationWithIndicesSignatureSet(
config,
index2pubkey,
state,
stateSlot,
indexedAttestation,
indexedAttestation.attestingIndices
);
Expand All @@ -53,7 +52,6 @@ export function getIndexedAttestationSignatureSet(
export function getAttestationsSignatureSets(
config: BeaconConfig,
index2pubkey: Index2PubkeyCache,
state: CachedBeaconStateAllForks,
signedBlock: SignedBeaconBlock,
indexedAttestations: IndexedAttestation[]
): ISignatureSet[] {
Expand All @@ -62,7 +60,10 @@ export function getAttestationsSignatureSets(
`Indexed attestations length mismatch: got ${indexedAttestations.length}, expected ${signedBlock.message.body.attestations.length}`
);
}
// the getDomain() api requires the state slot as 1st param, however it's the same to block.slot in state-transition
// and the same epoch when we verify blocks in batch in beacon-node. So we can safely use block.slot here.
const blockSlot = signedBlock.message.slot;
return indexedAttestations.map((indexedAttestation) =>
getIndexedAttestationSignatureSet(config, index2pubkey, state, indexedAttestation)
getIndexedAttestationSignatureSet(config, index2pubkey, blockSlot, indexedAttestation)
);
}
Loading
Loading