Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
import tech.pegasys.teku.spec.datastructures.operations.Attestation;
import tech.pegasys.teku.spec.datastructures.operations.versions.altair.SyncCommitteeMessage;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState;
import tech.pegasys.teku.statetransition.attestation.utils.AttestationBitsAggregator;
import tech.pegasys.teku.statetransition.attestation.utils.AttestationBits;
import tech.pegasys.teku.storage.client.CombinedChainDataClient;
import tech.pegasys.teku.validator.api.ValidatorPerformanceTrackingMode;
import tech.pegasys.teku.validator.coordinator.ActiveValidatorTracker;
Expand Down Expand Up @@ -304,19 +304,19 @@ private AttestationPerformance calculateAttestationPerformance(

// Pre-process attestations included on chain to group them by
// data hash to inclusion slot to aggregation bitlist
final Map<Bytes32, NavigableMap<UInt64, AttestationBitsAggregator>>
slotAndBitlistsByAttestationDataHash = new HashMap<>();
final Map<Bytes32, NavigableMap<UInt64, AttestationBits>> slotAndBitlistsByAttestationDataHash =
new HashMap<>();
for (final Map.Entry<UInt64, List<Attestation>> entry :
attestationsIncludedOnChain.entrySet()) {
for (final Attestation attestation : entry.getValue()) {
final Optional<Int2IntMap> committeesSize = getCommitteesSize(attestation, state);
final Bytes32 attestationDataHash = attestation.getData().hashTreeRoot();
final NavigableMap<UInt64, AttestationBitsAggregator> slotToBitlists =
final NavigableMap<UInt64, AttestationBits> slotToBitlists =
slotAndBitlistsByAttestationDataHash.computeIfAbsent(
attestationDataHash, __ -> new TreeMap<>());
slotToBitlists.merge(
entry.getKey(),
AttestationBitsAggregator.of(attestation, committeesSize),
AttestationBits.of(attestation, committeesSize),
(firstBitsAggregator, secondBitsAggregator) -> {
firstBitsAggregator.or(secondBitsAggregator);
return firstBitsAggregator;
Expand All @@ -330,7 +330,7 @@ private AttestationPerformance calculateAttestationPerformance(
if (!slotAndBitlistsByAttestationDataHash.containsKey(sentAttestationDataHash)) {
continue;
}
final NavigableMap<UInt64, AttestationBitsAggregator> slotAndBitlists =
final NavigableMap<UInt64, AttestationBits> slotAndBitlists =
slotAndBitlistsByAttestationDataHash.get(sentAttestationDataHash);
for (UInt64 slot : slotAndBitlists.keySet()) {
if (slotAndBitlists.get(slot).isSuperSetOf(sentAttestation)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
import java.util.Collection;
import java.util.List;
import tech.pegasys.teku.bls.BLS;
import tech.pegasys.teku.statetransition.attestation.utils.AttestationBitsAggregator;
import tech.pegasys.teku.statetransition.attestation.utils.AttestationBits;

/**
* Builds an aggregate attestation, providing functions to test if an attestation can be added or is
* made redundant by the current aggregate.
*/
class AggregateAttestationBuilder {
private final List<PooledAttestation> includedAttestations = new ArrayList<>();
private AttestationBitsAggregator currentAggregateBits;
private AttestationBits currentAggregateBits;

public boolean aggregate(final PooledAttestation attestation) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import tech.pegasys.teku.spec.Spec;
import tech.pegasys.teku.spec.datastructures.operations.Attestation;
import tech.pegasys.teku.spec.datastructures.operations.AttestationData;
import tech.pegasys.teku.statetransition.attestation.utils.AttestationBitsAggregator;
import tech.pegasys.teku.statetransition.attestation.utils.AttestationBits;

/**
* Maintains an aggregated collection of attestations which all share the same {@link
Expand Down Expand Up @@ -67,11 +67,10 @@ public class MatchingDataAttestationGroup implements Iterable<PooledAttestation>
* <p>Pruning isn't required for this map because the entire attestation group is dropped by
* {@link AggregatingAttestationPool} once it is too old to be included in blocks (32 slots).
*/
private final NavigableMap<UInt64, AttestationBitsAggregator> includedValidatorsBySlot =
new TreeMap<>();
private final NavigableMap<UInt64, AttestationBits> includedValidatorsBySlot = new TreeMap<>();

/** Precalculated combined list of included validators across all blocks. */
private AttestationBitsAggregator includedValidators;
private AttestationBits includedValidators;

public MatchingDataAttestationGroup(
final Spec spec,
Expand All @@ -83,8 +82,8 @@ public MatchingDataAttestationGroup(
this.includedValidators = createEmptyAttestationBits();
}

private AttestationBitsAggregator createEmptyAttestationBits() {
return AttestationBitsAggregator.fromEmptyFromAttestationSchema(
private AttestationBits createEmptyAttestationBits() {
return AttestationBits.fromEmptyFromAttestationSchema(
spec.atSlot(attestationData.getSlot()).getSchemaDefinitions().getAttestationSchema(),
committeesSize);
}
Expand Down Expand Up @@ -193,7 +192,7 @@ public int onAttestationIncludedInBlock(final UInt64 slot, final Attestation att
slot,
(__, attestationBitsCalculator) -> {
if (attestationBitsCalculator == null) {
return AttestationBitsAggregator.of(attestation, committeesSize);
return AttestationBits.of(attestation, committeesSize);
}
attestationBitsCalculator.or(attestation);
return attestationBitsCalculator;
Expand Down Expand Up @@ -226,7 +225,7 @@ public int onAttestationIncludedInBlock(final UInt64 slot, final Attestation att
}

public void onReorg(final UInt64 commonAncestorSlot) {
final NavigableMap<UInt64, AttestationBitsAggregator> removedSlots =
final NavigableMap<UInt64, AttestationBits> removedSlots =
includedValidatorsBySlot.tailMap(commonAncestorSlot, false);
if (removedSlots.isEmpty()) {
// No relevant attestations in affected slots, so nothing to do.
Expand Down Expand Up @@ -258,7 +257,7 @@ private boolean noMatchingPreElectraAttestations(final Optional<UInt64> committe
private class AggregatingIterator implements Iterator<PooledAttestation> {

private final Optional<UInt64> maybeCommitteeIndex;
private final AttestationBitsAggregator includedValidators;
private final AttestationBits includedValidators;

private Iterator<PooledAttestation> remainingAttestations = getRemainingAttestations();

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

import tech.pegasys.teku.bls.BLSSignature;
import tech.pegasys.teku.spec.datastructures.attestation.ValidatableAttestation;
import tech.pegasys.teku.statetransition.attestation.utils.AttestationBitsAggregator;
import tech.pegasys.teku.statetransition.attestation.utils.AttestationBits;

public record PooledAttestation(
AttestationBitsAggregator bits, BLSSignature aggregatedSignature, boolean isSingleAttestation) {
AttestationBits bits, BLSSignature aggregatedSignature, boolean isSingleAttestation) {

public static PooledAttestation fromValidatableAttestation(
final ValidatableAttestation attestation) {
return new PooledAttestation(
AttestationBitsAggregator.of(attestation),
AttestationBits.of(attestation),
attestation.getAttestation().getAggregateSignature(),
attestation.getUnconvertedAttestation().isSingleAttestation());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,55 +21,52 @@
import tech.pegasys.teku.spec.datastructures.operations.Attestation;
import tech.pegasys.teku.spec.datastructures.operations.AttestationSchema;

public interface AttestationBitsAggregator {
static AttestationBitsAggregator fromEmptyFromAttestationSchema(
public interface AttestationBits {
static AttestationBits fromEmptyFromAttestationSchema(
final AttestationSchema<?> attestationSchema, final Optional<Int2IntMap> committeesSize) {
return attestationSchema
.toVersionElectra()
.map(
schema ->
AttestationBitsAggregatorElectra.fromAttestationSchema(
schema, committeesSize.orElseThrow()))
.orElseGet(() -> AttestationBitsAggregatorPhase0.fromAttestationSchema(attestationSchema));
AttestationBitsElectra.fromAttestationSchema(schema, committeesSize.orElseThrow()))
.orElseGet(() -> AttestationBitsPhase0.fromAttestationSchema(attestationSchema));
}

static AttestationBitsAggregator of(final ValidatableAttestation attestation) {
static AttestationBits of(final ValidatableAttestation attestation) {
return attestation
.getAttestation()
.getCommitteeBits()
.map(
committeeBits ->
(AttestationBitsAggregator)
new AttestationBitsAggregatorElectra(
(AttestationBits)
new AttestationBitsElectra(
attestation.getAttestation().getAggregationBits(),
committeeBits,
attestation.getCommitteesSize().orElseThrow()))
.orElseGet(
() ->
new AttestationBitsAggregatorPhase0(
attestation.getAttestation().getAggregationBits()));
() -> new AttestationBitsPhase0(attestation.getAttestation().getAggregationBits()));
}

static AttestationBitsAggregator of(
static AttestationBits of(
final Attestation attestation, final Optional<Int2IntMap> committeesSize) {
return attestation
.getCommitteeBits()
.<AttestationBitsAggregator>map(
.<AttestationBits>map(
committeeBits ->
new AttestationBitsAggregatorElectra(
new AttestationBitsElectra(
attestation.getAggregationBits(), committeeBits, committeesSize.orElseThrow()))
.orElseGet(() -> new AttestationBitsAggregatorPhase0(attestation.getAggregationBits()));
.orElseGet(() -> new AttestationBitsPhase0(attestation.getAggregationBits()));
}

void or(AttestationBitsAggregator other);
void or(AttestationBits other);

boolean aggregateWith(AttestationBitsAggregator other);
boolean aggregateWith(AttestationBits other);

void or(Attestation other);

boolean isSuperSetOf(Attestation other);

boolean isSuperSetOf(AttestationBitsAggregator other);
boolean isSuperSetOf(AttestationBits other);

SszBitlist getAggregationBits();

Expand All @@ -84,5 +81,5 @@ static AttestationBitsAggregator of(
boolean isExclusivelyFromCommittee(int committeeIndex);

/** Creates an independent copy of this instance */
AttestationBitsAggregator copy();
AttestationBits copy();
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import tech.pegasys.teku.spec.datastructures.operations.Attestation;
import tech.pegasys.teku.spec.datastructures.operations.AttestationSchema;

class AttestationBitsAggregatorElectra implements AttestationBitsAggregator {
class AttestationBitsElectra implements AttestationBits {

private final SszBitlistSchema<?> aggregationBitsSchema;
private final SszBitvectorSchema<?> committeeBitsSchema;
Expand All @@ -41,7 +41,7 @@ class AttestationBitsAggregatorElectra implements AttestationBitsAggregator {
private SszBitlist cachedAggregationBits = null;
private SszBitvector cachedCommitteeBits = null;

AttestationBitsAggregatorElectra(
AttestationBitsElectra(
final SszBitlist initialAggregationBits,
final SszBitvector initialCommitteeBits,
final Int2IntMap committeesSize) {
Expand All @@ -53,7 +53,7 @@ class AttestationBitsAggregatorElectra implements AttestationBitsAggregator {
parseAggregationBits(initialAggregationBits, this.committeeBits, this.committeesSize);
}

private AttestationBitsAggregatorElectra(
private AttestationBitsElectra(
final SszBitlistSchema<?> aggregationBitsSchema,
final SszBitvectorSchema<?> committeeBitsSchema,
final Int2IntMap committeesSize,
Expand All @@ -66,16 +66,15 @@ private AttestationBitsAggregatorElectra(
this.committeeAggregationBitsMap = committeeAggregationBitsMap;
}

static AttestationBitsAggregator fromAttestationSchema(
static AttestationBits fromAttestationSchema(
final AttestationSchema<?> attestationSchema, final Int2IntMap committeesSize) {
final SszBitlist emptyAggregationBits = attestationSchema.createEmptyAggregationBits();
final SszBitvector emptyCommitteeBits =
attestationSchema
.createEmptyCommitteeBits()
.orElseThrow(
() -> new IllegalStateException("Electra schema must provide committee bits"));
return new AttestationBitsAggregatorElectra(
emptyAggregationBits, emptyCommitteeBits, committeesSize);
return new AttestationBitsElectra(emptyAggregationBits, emptyCommitteeBits, committeesSize);
}

private static Int2ObjectMap<BitSet> parseAggregationBits(
Expand Down Expand Up @@ -103,15 +102,15 @@ private static Int2ObjectMap<BitSet> parseAggregationBits(
}

@Override
public void or(final AttestationBitsAggregator other) {
final AttestationBitsAggregatorElectra otherElectra = requiresElectra(other);
public void or(final AttestationBits other) {
final AttestationBitsElectra otherElectra = requiresElectra(other);

performMerge(otherElectra.committeeBits, otherElectra.committeeAggregationBitsMap, false);
}

@Override
public boolean aggregateWith(final AttestationBitsAggregator other) {
final AttestationBitsAggregatorElectra otherElectra = requiresElectra(other);
public boolean aggregateWith(final AttestationBits other) {
final AttestationBitsElectra otherElectra = requiresElectra(other);
return performMerge(otherElectra.committeeBits, otherElectra.committeeAggregationBitsMap, true);
}

Expand Down Expand Up @@ -204,8 +203,8 @@ public boolean isSuperSetOf(final Attestation other) {
}

@Override
public boolean isSuperSetOf(final AttestationBitsAggregator other) {
final AttestationBitsAggregatorElectra otherElectra = requiresElectra(other);
public boolean isSuperSetOf(final AttestationBits other) {
final AttestationBitsElectra otherElectra = requiresElectra(other);

return isSuperSetOf(otherElectra.committeeBits, () -> otherElectra.committeeAggregationBitsMap);
}
Expand Down Expand Up @@ -310,8 +309,8 @@ public boolean requiresCommitteeBits() {
}

@Override
public AttestationBitsAggregator copy() {
return new AttestationBitsAggregatorElectra(
public AttestationBits copy() {
return new AttestationBitsElectra(
aggregationBitsSchema,
committeeBitsSchema,
committeesSize,
Expand Down Expand Up @@ -348,9 +347,8 @@ public String toString() {
.toString();
}

static AttestationBitsAggregatorElectra requiresElectra(
final AttestationBitsAggregator aggregator) {
if (!(aggregator instanceof AttestationBitsAggregatorElectra aggregatorElectra)) {
static AttestationBitsElectra requiresElectra(final AttestationBits aggregator) {
if (!(aggregator instanceof AttestationBitsElectra aggregatorElectra)) {
throw new IllegalArgumentException(
"AttestationBitsAggregator required to be Electra but was: "
+ aggregator.getClass().getSimpleName());
Expand All @@ -364,7 +362,7 @@ public boolean equals(final Object o) {
return true;
}

if (!(o instanceof AttestationBitsAggregatorElectra that)) {
if (!(o instanceof AttestationBitsElectra that)) {
return false;
}
return this.committeeBits.equals(that.committeeBits)
Expand Down
Loading