Skip to content

Commit

Permalink
flattern aggregation bits
Browse files Browse the repository at this point in the history
  • Loading branch information
mehdi-aouadi committed Mar 29, 2024
1 parent 92b9bdb commit 5ced5a1
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import tech.pegasys.teku.bls.BLSSignature;
import tech.pegasys.teku.infrastructure.ssz.SszContainer;
import tech.pegasys.teku.infrastructure.ssz.SszData;
import tech.pegasys.teku.infrastructure.ssz.SszList;
import tech.pegasys.teku.infrastructure.ssz.collections.SszBitlist;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;

Expand Down Expand Up @@ -47,11 +46,11 @@ default BLSSignature getAggregateSignatureRequired() {
.orElseThrow(() -> new IllegalArgumentException("Missing aggregate signature"));
}

default Optional<SszList<SszBitlist>> getAggregationBitsElectra() {
default Optional<SszBitlist> getAggregationBitsElectra() {
return Optional.empty();
}

default SszList<SszBitlist> getAggregationBitsElectraRequired() {
default SszBitlist getAggregationBitsElectraRequired() {
return getAggregationBitsElectra()
.orElseThrow(() -> new IllegalArgumentException("Missing aggregation bits"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.util.Optional;
import org.apache.tuweni.bytes.Bytes32;
import tech.pegasys.teku.bls.BLSSignature;
import tech.pegasys.teku.infrastructure.ssz.SszList;
import tech.pegasys.teku.infrastructure.ssz.collections.SszBitlist;
import tech.pegasys.teku.infrastructure.ssz.collections.SszBitvector;
import tech.pegasys.teku.infrastructure.ssz.containers.Container4;
Expand All @@ -31,8 +30,7 @@
import tech.pegasys.teku.spec.datastructures.type.SszSignature;

public class AttestationElectra
extends Container4<
AttestationElectra, SszList<SszBitlist>, AttestationData, SszBitvector, SszSignature>
extends Container4<AttestationElectra, SszBitlist, AttestationData, SszBitvector, SszSignature>
implements AttestationContainer {

public AttestationElectra(final AttestationElectraSchema type, final TreeNode backingNode) {
Expand All @@ -41,7 +39,7 @@ public AttestationElectra(final AttestationElectraSchema type, final TreeNode ba

public AttestationElectra(
final AttestationElectraSchema schema,
final SszList<SszBitlist> aggregationBits,
final SszBitlist aggregationBits,
final AttestationData data,
final SszBitvector committeeBits,
final BLSSignature signature) {
Expand All @@ -62,7 +60,7 @@ public Collection<Bytes32> getDependentBlockRoots() {
}

@Override
public Optional<SszList<SszBitlist>> getAggregationBitsElectra() {
public Optional<SszBitlist> getAggregationBitsElectra() {
return Optional.of(getField0());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@

public class AttestationElectraSchema
extends ContainerSchema4<
AttestationElectra, SszList<SszBitlist>, AttestationData, SszBitvector, SszSignature>
AttestationElectra, SszBitlist, AttestationData, SszBitvector, SszSignature>
implements AttestationContainerSchema<AttestationElectra> {

public AttestationElectraSchema(final SpecConfig specConfig) {
super(
"AttestationElectra",
namedSchema(
"aggregation_bits",
SszListSchema.create(
SszBitlistSchema.create(specConfig.getMaxValidatorsPerCommittee()),
specConfig.getMaxCommitteesPerSlot())),
SszBitlistSchema.create(
(long) specConfig.getMaxValidatorsPerCommittee()
* specConfig.getMaxCommitteesPerSlot())),
namedSchema("data", AttestationData.SSZ_SCHEMA),
namedSchema(
"committee_bits", SszBitvectorSchema.create(specConfig.getMaxCommitteesPerSlot())),
Expand All @@ -58,7 +58,7 @@ public AttestationElectra createFromBackingNode(TreeNode node) {
}

public AttestationElectra create(
final SszList<SszBitlist> aggregationBits,
final SszBitlist aggregationBits,
final AttestationData data,
final SszBitvector committeeBits,
final BLSSignature signature) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ enum AttestationInvalidReason implements OperationInvalidReason {
COMMITTEE_INDEX_TOO_HIGH("CommitteeIndex too high"),
COMMITTEE_INDEX_MUST_BE_ZERO("CommitteeIndex must be set to zero"),
COMMITTEE_INDICES_MISMATCH("Committee indices size do not match aggregation bits"),
COMMITTEE_COUNT_MISMATCH("Committee size do not match aggregation bits"),
PARTICIPANTS_COUNT_MISMATCH("Attesting participants count do not match aggregation bits"),
NOT_FROM_CURRENT_OR_PREVIOUS_EPOCH("Attestation not from current or previous epoch"),
SLOT_NOT_IN_EPOCH("Attestation slot not in specified epoch"),
SUBMITTED_TOO_QUICKLY("Attestation submitted too quickly"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import tech.pegasys.teku.spec.logic.common.helpers.BeaconStateMutators;
import tech.pegasys.teku.spec.logic.common.helpers.Predicates;
import tech.pegasys.teku.spec.logic.common.operations.OperationSignatureVerifier;
import tech.pegasys.teku.spec.logic.common.operations.validation.AttestationDataValidator;
import tech.pegasys.teku.spec.logic.common.operations.validation.AttestationDataValidator.AttestationInvalidReason;
import tech.pegasys.teku.spec.logic.common.operations.validation.OperationInvalidReason;
import tech.pegasys.teku.spec.logic.common.operations.validation.OperationValidator;
import tech.pegasys.teku.spec.logic.common.statetransition.blockvalidator.BlockValidationResult;
Expand Down Expand Up @@ -195,10 +195,7 @@ protected void assertAttestationValid(
state, attestation.getData().getTarget().getEpoch());
beaconStateAccessors.getCommitteeCountPerSlot(
state, attestation.getData().getTarget().getEpoch());
final SszList<SszBitlist> aggregationBits = attestation.getAggregationBitsElectraRequired();
checkArgument(
committeeIndices.size() == aggregationBits.size(),
AttestationDataValidator.AttestationInvalidReason.COMMITTEE_INDICES_MISMATCH);
final SszBitlist aggregationBits = attestation.getAggregationBitsElectraRequired();
final Optional<OperationInvalidReason> committeeCheckResult =
checkCommittees(
committeeIndices,
Expand All @@ -216,19 +213,18 @@ private Optional<OperationInvalidReason> checkCommittees(
final UInt64 committeeCountPerSlot,
final BeaconState state,
final UInt64 slot,
final SszList<SszBitlist> aggregationBits) {
for (int index = 0; index < committeeIndices.size(); index++) {
final UInt64 committeeIndex = committeeIndices.get(index);
final SszBitlist aggregationBits) {
int participantsCount = 0;
for (final UInt64 committeeIndex : committeeIndices) {
if (committeeIndex.compareTo(committeeCountPerSlot) < 0) {
return Optional.of(
AttestationDataValidator.AttestationInvalidReason.COMMITTEE_INDEX_TOO_HIGH);
return Optional.of(AttestationInvalidReason.COMMITTEE_INDEX_TOO_HIGH);
}
final IntList committee =
beaconStateAccessors.getBeaconCommittee(state, slot, committeeIndex);
if (committee.size() != aggregationBits.get(index).size()) {
return Optional.of(
AttestationDataValidator.AttestationInvalidReason.COMMITTEE_COUNT_MISMATCH);
}
participantsCount += committee.size();
}
if (participantsCount != aggregationBits.size()) {
return Optional.of(AttestationInvalidReason.PARTICIPANTS_COUNT_MISMATCH);
}
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import it.unimi.dsi.fastutil.ints.IntList;
import java.util.List;
import java.util.stream.IntStream;
import tech.pegasys.teku.infrastructure.ssz.SszList;
import tech.pegasys.teku.infrastructure.ssz.collections.SszBitlist;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.spec.config.SpecConfig;
Expand Down Expand Up @@ -45,7 +44,7 @@ public AttestationUtilElectra(
* @return
* @throws IllegalArgumentException
* @see
* <a>https://github.com/ethereum/consensus-specs/blob/v0.8.0/specs/core/0_beacon-chain.md#get_attesting_indices</a>
* <a>https://github.com/ethereum/consensus-specs/blob/dev/specs/_features/eip7549/beacon-chain.md#modified-get_attesting_indices</a>
*/
@Override
public IntList getAttestingIndices(
Expand All @@ -54,35 +53,31 @@ public IntList getAttestingIndices(
attestation
.getCommitteeIndices()
.orElseThrow(() -> new IllegalArgumentException("Missing committee indices"));
final SszList<SszBitlist> aggregationBits = attestation.getAggregationBitsElectraRequired();
final SszBitlist aggregationBits = attestation.getAggregationBitsElectraRequired();
final IntList attestingIndices = new IntArrayList();
committeeIndices.forEach(
committeeIndex -> {
final SszBitlist attesterBits = aggregationBits.get(committeeIndex.intValue());
final IntList committeeAttesters =
getCommitteeAttesters(
state, attestation.getData().getSlot(), attesterBits, committeeIndex);
attestingIndices.addAll(committeeAttesters);
});
int committeeOffset = 0;
for (final UInt64 committeeIndex : committeeIndices) {
final IntList committee =
beaconStateAccessors.getBeaconCommittee(
state, attestation.getData().getSlot(), committeeIndex);
final IntList committeeAttesters =
getCommitteeAttesters(committee, aggregationBits, committeeOffset);
attestingIndices.addAll(committeeAttesters);
committeeOffset += committee.size();
}
return attestingIndices;
}

public IntList getCommitteeAttesters(
final BeaconState state,
final UInt64 slot,
final SszBitlist attesterBits,
final UInt64 committeeIndex) {
final IntList committee, final SszBitlist aggregationBits, final int committeeOffset) {
return IntList.of(
streamCommitteeAttesters(state, slot, attesterBits, committeeIndex).toArray());
streamCommitteeAttesters(committee, aggregationBits, committeeOffset).toArray());
}

public IntStream streamCommitteeAttesters(
final BeaconState state,
final UInt64 slot,
final SszBitlist attesterBits,
final UInt64 committeeIndex) {
final IntList committee = beaconStateAccessors.getBeaconCommittee(state, slot, committeeIndex);
final IntList attestingBits = attesterBits.getAllSetBits();
return attestingBits.intStream().map(committee::getInt);
final IntList committee, final SszBitlist aggregationBits, final int committeeOffset) {
return IntStream.range(committeeOffset, committeeOffset + committee.size())
.filter(aggregationBits::isSet)
.map(attesterIndex -> committee.getInt(attesterIndex - committeeOffset));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -820,12 +820,8 @@ public AttestationElectra randomAttestationElectra(final UInt64 slot) {
final int maxValidatorsPerCommittee =
SpecConfigElectra.required(spec.forMilestone(SpecMilestone.ELECTRA).getConfig())
.getMaxValidatorsPerCommittee();
final SszList<SszBitlist> randomAggregationBits =
randomSszList(
SszListSchema.create(
SszBitlistSchema.create(maxValidatorsPerCommittee), maxCommitteePerSlot),
this::randomBitlist,
maxCommitteePerSlot);
final SszBitlist randomAggregationBits =
randomBitlist(maxCommitteePerSlot * maxValidatorsPerCommittee);
return getElectraSchemaDefinitions(slot)
.getAttestationElectraSchema()
.create(
Expand Down

0 comments on commit 5ced5a1

Please sign in to comment.