-
-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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
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
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 |
---|---|---|
|
@@ -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,47 @@ 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 +779,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 +792,31 @@ 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 | ||
// TODO Electra: resolve the naming conflicts | ||
const committeeIndices = committeeBits.getTrueBitIndexes(); | ||
|
||
const validatorIndices = this.getBeaconCommittees(data.slot, committeeIndices); | ||
|
||
return aggregationBits.intersectValues(validatorIndices); | ||
} | ||
} | ||
|
||
getCommitteeAssignments( | ||
epoch: Epoch, | ||
requestedValidatorIndices: ValidatorIndex[] | ||
|
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 |
---|---|---|
|
@@ -41,7 +41,11 @@ 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, 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 |
||
) | ||
); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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