-
-
Notifications
You must be signed in to change notification settings - Fork 311
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: beacon node process electra attestations EIP-7549 #6738
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import {toHexString} from "@chainsafe/ssz"; | ||
import {Slot, phase0, ssz} from "@lodestar/types"; | ||
import {Slot, allForks, electra, phase0, ssz} from "@lodestar/types"; | ||
|
||
import {MIN_ATTESTATION_INCLUSION_DELAY, SLOTS_PER_EPOCH, ForkSeq} from "@lodestar/params"; | ||
import {computeEpochAtSlot} from "../util/index.js"; | ||
|
@@ -51,27 +51,22 @@ export function processAttestationPhase0( | |
state.previousEpochAttestations.push(pendingAttestation); | ||
} | ||
|
||
if (!isValidIndexedAttestation(state, epochCtx.getIndexedAttestation(attestation), verifySignature)) { | ||
if (!isValidIndexedAttestation(state, epochCtx.getIndexedAttestation(ForkSeq.phase0, attestation), verifySignature)) { | ||
throw new Error("Attestation is not valid"); | ||
} | ||
} | ||
|
||
export function validateAttestation( | ||
fork: ForkSeq, | ||
state: CachedBeaconStateAllForks, | ||
attestation: phase0.Attestation | ||
attestation: allForks.Attestation | ||
): void { | ||
const {epochCtx} = state; | ||
const slot = state.slot; | ||
const data = attestation.data; | ||
const computedEpoch = computeEpochAtSlot(data.slot); | ||
const committeeCount = epochCtx.getCommitteeCountPerSlot(computedEpoch); | ||
if (!(data.index < committeeCount)) { | ||
throw new Error( | ||
"Attestation committee index not within current committee count: " + | ||
`committeeIndex=${data.index} committeeCount=${committeeCount}` | ||
); | ||
} | ||
|
||
if (!(data.target.epoch === epochCtx.previousShuffling.epoch || data.target.epoch === epochCtx.epoch)) { | ||
throw new Error( | ||
"Attestation target epoch not in previous or current epoch: " + | ||
|
@@ -93,12 +88,47 @@ export function validateAttestation( | |
); | ||
} | ||
|
||
const committee = epochCtx.getBeaconCommittee(data.slot, data.index); | ||
if (attestation.aggregationBits.bitLen !== committee.length) { | ||
throw new Error( | ||
"Attestation aggregation bits length does not match committee length: " + | ||
`aggregationBitsLength=${attestation.aggregationBits.bitLen} committeeLength=${committee.length}` | ||
); | ||
if (fork >= ForkSeq.electra) { | ||
twoeths marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (data.index !== 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use |
||
throw new Error(`AttestationData.index must be zero: index=${data.index}`); | ||
} | ||
const attestationElectra = attestation as electra.Attestation; | ||
const committeeBitsLength = attestationElectra.committeeBits.bitLen; | ||
|
||
if (committeeBitsLength > committeeCount) { | ||
throw new Error( | ||
`Attestation committee bits length are longer than number of committees: committeeBitsLength=${committeeBitsLength} numCommittees=${committeeCount}` | ||
); | ||
} | ||
|
||
// TODO Electra: this should be obsolete soon when the spec switches to committeeIndices | ||
const committeeIndices = attestationElectra.committeeBits.getTrueBitIndexes(); | ||
|
||
// Get total number of attestation participant of every committee specified | ||
const participantCount = committeeIndices | ||
.map((committeeIndex) => epochCtx.getBeaconCommittee(data.slot, committeeIndex).length) | ||
.reduce((acc, committeeSize) => acc + committeeSize, 0); | ||
|
||
if (attestationElectra.aggregationBits.bitLen !== participantCount) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use |
||
throw new Error( | ||
`Attestation aggregation bits length does not match total number of committee participant aggregationBitsLength=${attestation.aggregationBits.bitLen} participantCount=${participantCount}` | ||
); | ||
} | ||
} else { | ||
if (!(data.index < committeeCount)) { | ||
throw new Error( | ||
"Attestation committee index not within current committee count: " + | ||
`committeeIndex=${data.index} committeeCount=${committeeCount}` | ||
); | ||
} | ||
|
||
const committee = epochCtx.getBeaconCommittee(data.slot, data.index); | ||
if (attestation.aggregationBits.bitLen !== committee.length) { | ||
throw new Error( | ||
"Attestation aggregation bits length does not match committee length: " + | ||
`aggregationBitsLength=${attestation.aggregationBits.bitLen} committeeLength=${committee.length}` | ||
); | ||
} | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,17 @@ import {CoordType, PublicKey} from "@chainsafe/bls/types"; | |
import bls from "@chainsafe/bls"; | ||
import * as immutable from "immutable"; | ||
import {fromHexString} from "@chainsafe/ssz"; | ||
import {BLSSignature, CommitteeIndex, Epoch, Slot, ValidatorIndex, phase0, SyncPeriod} from "@lodestar/types"; | ||
import { | ||
BLSSignature, | ||
CommitteeIndex, | ||
Epoch, | ||
Slot, | ||
ValidatorIndex, | ||
phase0, | ||
SyncPeriod, | ||
allForks, | ||
electra, | ||
} from "@lodestar/types"; | ||
import {createBeaconConfig, BeaconConfig, ChainConfig} from "@lodestar/config"; | ||
import { | ||
ATTESTATION_SUBNET_COUNT, | ||
|
@@ -643,15 +653,48 @@ export class EpochCache { | |
* Return the beacon committee at slot for index. | ||
*/ | ||
getBeaconCommittee(slot: Slot, index: CommitteeIndex): Uint32Array { | ||
return this.getBeaconCommittees(slot, [index]); | ||
} | ||
|
||
/** | ||
* Return a single Uint32Array representing concatted committees of indices | ||
*/ | ||
getBeaconCommittees(slot: Slot, indices: CommitteeIndex[]): Uint32Array { | ||
|
||
if (indices.length === 0) { | ||
throw new Error("Attempt to get committees without providing CommitteeIndex"); | ||
} | ||
|
||
const slotCommittees = this.getShufflingAtSlot(slot).committees[slot % SLOTS_PER_EPOCH]; | ||
if (index >= slotCommittees.length) { | ||
throw new EpochCacheError({ | ||
code: EpochCacheErrorCode.COMMITTEE_INDEX_OUT_OF_RANGE, | ||
index, | ||
maxIndex: slotCommittees.length, | ||
}); | ||
const committees = []; | ||
|
||
for (const index of indices) { | ||
if (index >= slotCommittees.length) { | ||
throw new EpochCacheError({ | ||
code: EpochCacheErrorCode.COMMITTEE_INDEX_OUT_OF_RANGE, | ||
index, | ||
maxIndex: slotCommittees.length, | ||
}); | ||
} | ||
committees.push(slotCommittees[index]); | ||
} | ||
|
||
// Early return if only one index | ||
if (committees.length === 1) { | ||
return committees[0]; | ||
} | ||
|
||
// Create a new Uint32Array to flatten `committees` | ||
const totalLength = committees.reduce((acc, curr) => acc + curr.length, 0); | ||
const result = new Uint32Array(totalLength); | ||
|
||
let offset = 0; | ||
for (const committee of committees) { | ||
result.set(committee, offset); | ||
offset += committee.length; | ||
} | ||
return slotCommittees[index]; | ||
|
||
return result; | ||
} | ||
|
||
getCommitteeCountPerSlot(epoch: Epoch): number { | ||
|
@@ -737,10 +780,9 @@ export class EpochCache { | |
/** | ||
* Return the indexed attestation corresponding to ``attestation``. | ||
*/ | ||
getIndexedAttestation(attestation: phase0.Attestation): phase0.IndexedAttestation { | ||
const {aggregationBits, data} = attestation; | ||
const committeeIndices = this.getBeaconCommittee(data.slot, data.index); | ||
const attestingIndices = aggregationBits.intersectValues(committeeIndices); | ||
getIndexedAttestation(fork: ForkSeq, attestation: allForks.Attestation): allForks.IndexedAttestation { | ||
const {data} = attestation; | ||
const attestingIndices = this.getAttestingIndices(fork, attestation); | ||
|
||
// sort in-place | ||
attestingIndices.sort((a, b) => a - b); | ||
|
@@ -751,6 +793,32 @@ export class EpochCache { | |
}; | ||
} | ||
|
||
/** | ||
* Return indices of validators who attestested in `attestation` | ||
*/ | ||
getAttestingIndices(fork: ForkSeq, attestation: allForks.Attestation): number[] { | ||
if (fork < ForkSeq.electra) { | ||
const {aggregationBits, data} = attestation; | ||
const validatorIndices = this.getBeaconCommittee(data.slot, data.index); | ||
|
||
return aggregationBits.intersectValues(validatorIndices); | ||
} else { | ||
const {aggregationBits, committeeBits, data} = attestation as electra.Attestation; | ||
|
||
// There is a naming conflict on the term `committeeIndices` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add |
||
// In Lodestar it usually means a list of validator indices of participants in a committee | ||
// In the spec it means a list of committee indices according to committeeBits | ||
// This `committeeIndices` refers to the latter | ||
const committeeIndices = committeeBits.getTrueBitIndexes(); | ||
|
||
const validatorIndices = this.getBeaconCommittees(data.slot, committeeIndices); | ||
|
||
const attestingIndices = new Set(aggregationBits.intersectValues(validatorIndices)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. return the array from |
||
|
||
return Array.from(attestingIndices); | ||
} | ||
} | ||
|
||
getCommitteeAssignments( | ||
epoch: Epoch, | ||
requestedValidatorIndices: ValidatorIndex[] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,9 @@ export function getAttestationsSignatureSets( | |
signedBlock: allForks.SignedBeaconBlock | ||
): ISignatureSet[] { | ||
return signedBlock.message.body.attestations.map((attestation) => | ||
getIndexedAttestationSignatureSet(state, state.epochCtx.getIndexedAttestation(attestation)) | ||
getIndexedAttestationSignatureSet( | ||
state, | ||
state.epochCtx.getIndexedAttestation(state.epochCtx.config.getForkSeq(signedBlock.message.slot), attestation) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add |
||
) | ||
); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add
TODO: figure out how to reuse the attesting indices computed from state transition