-
-
Notifications
You must be signed in to change notification settings - Fork 265
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
80 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 62 additions & 38 deletions
100
packages/beacon-state-transition/src/altair/block/processSyncCommittee.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,87 @@ | ||
import {verifyAggregate} from "@chainsafe/bls"; | ||
import {altair} from "@chainsafe/lodestar-types"; | ||
import {assert} from "@chainsafe/lodestar-utils"; | ||
|
||
import { | ||
computeEpochAtSlot, | ||
computeSigningRoot, | ||
getBlockRootAtSlot, | ||
getDomain, | ||
increaseBalance, | ||
ISignatureSet, | ||
SignatureSetType, | ||
verifySignatureSet, | ||
zipIndexesInBitList, | ||
} from "../../util"; | ||
import {CachedBeaconState} from "../../allForks/util"; | ||
import {BitList, isTreeBacked, TreeBacked} from "@chainsafe/ssz"; | ||
|
||
export function processSyncCommittee( | ||
state: CachedBeaconState<altair.BeaconState>, | ||
aggregate: altair.SyncAggregate, | ||
syncAggregate: altair.SyncAggregate, | ||
verifySignatures = true | ||
): void { | ||
const {config, epochCtx} = state; | ||
const {syncParticipantReward, syncProposerReward} = epochCtx; | ||
const previousSlot = Math.max(state.slot, 1) - 1; | ||
const committeeIndices = state.currSyncCommitteeIndexes; | ||
const {syncParticipantReward, syncProposerReward} = state.epochCtx; | ||
// the only time aggregate is not a TreeBacked is when producing a new block | ||
const participantIndices = isTreeBacked(aggregate) | ||
? zipIndexesInBitList( | ||
committeeIndices, | ||
aggregate.syncCommitteeBits as TreeBacked<BitList>, | ||
config.types.altair.SyncCommitteeBits | ||
) | ||
: committeeIndices.filter((index) => !!aggregate.syncCommitteeBits[index]); | ||
const participantPubkeys = participantIndices.map((validatorIndex) => state.validators[validatorIndex].pubkey); | ||
const domain = getDomain( | ||
config, | ||
state, | ||
config.params.DOMAIN_SYNC_COMMITTEE, | ||
computeEpochAtSlot(config, previousSlot) | ||
); | ||
const signingRoot = computeSigningRoot( | ||
config, | ||
config.types.Root, | ||
getBlockRootAtSlot(config, state, previousSlot), | ||
domain | ||
); | ||
const participantIndices = getParticipantIndices(state, syncAggregate); | ||
|
||
// different from the spec but not sure how to get through signature verification for default/empty SyncAggregate in the spec test | ||
if (verifySignatures && participantIndices.length > 0) { | ||
assert.true( | ||
verifyAggregate( | ||
participantPubkeys.map((pubkey) => pubkey.valueOf() as Uint8Array), | ||
signingRoot, | ||
aggregate.syncCommitteeSignature.valueOf() as Uint8Array | ||
), | ||
"Sync committee signature invalid" | ||
); | ||
if (verifySignatures) { | ||
const signatureSet = getSyncCommitteeSignatureSet(state, syncAggregate, 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"); | ||
} | ||
} | ||
|
||
const proposerIndex = epochCtx.getBeaconProposer(state.slot); | ||
const proposerIndex = state.epochCtx.getBeaconProposer(state.slot); | ||
for (const participantIndex of participantIndices) { | ||
increaseBalance(state, participantIndex, syncParticipantReward); | ||
} | ||
increaseBalance(state, proposerIndex, syncProposerReward * BigInt(participantIndices.length)); | ||
} | ||
|
||
export function getSyncCommitteeSignatureSet( | ||
state: CachedBeaconState<altair.BeaconState>, | ||
syncAggregate: altair.SyncAggregate, | ||
/** Optional parameter to prevent computing it twice */ | ||
participantIndices?: number[] | ||
): ISignatureSet | null { | ||
const {config, epochCtx} = state; | ||
|
||
const previousSlot = Math.max(state.slot, 1) - 1; | ||
const rootSigned = getBlockRootAtSlot(config, state, previousSlot); | ||
|
||
// the only time aggregate is not a TreeBacked is when producing a new block | ||
if (!participantIndices) { | ||
participantIndices = getParticipantIndices(state, syncAggregate); | ||
} | ||
|
||
// When there's no participation we consider the signature valid and just ignore it | ||
if (participantIndices.length === 0) { | ||
return null; | ||
} | ||
|
||
const epochSig = computeEpochAtSlot(config, previousSlot); | ||
const domain = getDomain(config, state, config.params.DOMAIN_SYNC_COMMITTEE, epochSig); | ||
|
||
return { | ||
type: SignatureSetType.aggregate, | ||
pubkeys: participantIndices.map((i) => epochCtx.index2pubkey[i]), | ||
signingRoot: computeSigningRoot(config, config.types.Root, rootSigned, domain), | ||
signature: syncAggregate.syncCommitteeSignature.valueOf() as Uint8Array, | ||
}; | ||
} | ||
|
||
/** Common logic for processSyncCommittee() and getSyncCommitteeSignatureSet() */ | ||
function getParticipantIndices( | ||
state: CachedBeaconState<altair.BeaconState>, | ||
syncAggregate: altair.SyncAggregate | ||
): number[] { | ||
const committeeIndices = state.currSyncCommitteeIndexes; | ||
|
||
return isTreeBacked(syncAggregate) | ||
? zipIndexesInBitList( | ||
committeeIndices, | ||
syncAggregate.syncCommitteeBits as TreeBacked<BitList>, | ||
state.config.types.altair.SyncCommitteeBits | ||
) | ||
: committeeIndices.filter((index) => !!syncAggregate.syncCommitteeBits[index]); | ||
} |