Skip to content

Commit

Permalink
Merge branch 'nc/7549-1' into nc/7549
Browse files Browse the repository at this point in the history
  • Loading branch information
ensi321 committed May 7, 2024
2 parents 0a58616 + 4760572 commit 70e770d
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 22 deletions.
1 change: 1 addition & 0 deletions packages/beacon-node/src/chain/blocks/importBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export async function importBlock(

for (const attestation of attestations) {
try {
// TODO Electra: figure out how to reuse the attesting indices computed from state transition
const indexedAttestation = postState.epochCtx.getIndexedAttestation(fork, attestation);
const {target, beaconBlockRoot} = attestation.data;

Expand Down
2 changes: 1 addition & 1 deletion packages/beacon-node/test/spec/presets/fork_choice.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ const forkChoiceTest =
const headState = chain.getHeadState();
const attDataRootHex = toHexString(ssz.phase0.AttestationData.hashTreeRoot(attestation.data));
chain.forkChoice.onAttestation(
headState.epochCtx.getIndexedAttestation(ForkSeq.phase0, attestation),
headState.epochCtx.getIndexedAttestation(ForkSeq[fork], attestation),
attDataRootHex
);
}
Expand Down
17 changes: 8 additions & 9 deletions packages/state-transition/src/block/processAttestationPhase0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {toHexString} from "@chainsafe/ssz";
import {Slot, allForks, electra, phase0, ssz} from "@lodestar/types";

import {MIN_ATTESTATION_INCLUSION_DELAY, SLOTS_PER_EPOCH, ForkSeq} from "@lodestar/params";
import {assert} from "@lodestar/utils";
import {computeEpochAtSlot} from "../util/index.js";
import {CachedBeaconStatePhase0, CachedBeaconStateAllForks} from "../types.js";
import {isValidIndexedAttestation} from "./index.js";
Expand Down Expand Up @@ -89,9 +90,7 @@ export function validateAttestation(
}

if (fork >= ForkSeq.electra) {
if (data.index !== 0) {
throw new Error(`AttestationData.index must be zero: index=${data.index}`);
}
assert.equal(data.index, 0, `AttestationData.index must be zero: index=${data.index}`);
const attestationElectra = attestation as electra.Attestation;
const committeeBitsLength = attestationElectra.committeeBits.bitLen;

Expand All @@ -101,19 +100,19 @@ export function validateAttestation(
);
}

// TODO Electra: this should be obsolete soon when we switch to committeeIndices
// 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) {
throw new Error(
`Attestation aggregation bits length does not match total number of committee participant aggregationBitsLength=${attestation.aggregationBits.bitLen} participantCount=${participantCount}`
);
}
assert.equal(
attestationElectra.aggregationBits.bitLen,
participantCount,
`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(
Expand Down
24 changes: 12 additions & 12 deletions packages/state-transition/src/cache/epochCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -653,21 +653,17 @@ export class EpochCache {
* Return the beacon committee at slot for index.
*/
getBeaconCommittee(slot: Slot, index: CommitteeIndex): Uint32Array {
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,
});
}
return slotCommittees[index];
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];
const committees = [];

Expand All @@ -682,6 +678,11 @@ export class EpochCache {
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);
Expand Down Expand Up @@ -807,13 +808,12 @@ export class EpochCache {
// 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
// TODO Electra: resolve the naming conflicts
const committeeIndices = committeeBits.getTrueBitIndexes();

const validatorIndices = this.getBeaconCommittees(data.slot, committeeIndices);

const attestingIndices = new Set(aggregationBits.intersectValues(validatorIndices));

return Array.from(attestingIndices);
return aggregationBits.intersectValues(validatorIndices);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export function getAttestationsSignatureSets(
state: CachedBeaconStateAllForks,
signedBlock: allForks.SignedBeaconBlock
): ISignatureSet[] {
// TODO: figure how to get attesting indices of an attestation once per block processing
return signedBlock.message.body.attestations.map((attestation) =>
getIndexedAttestationSignatureSet(
state,
Expand Down

0 comments on commit 70e770d

Please sign in to comment.