Skip to content

Commit

Permalink
Refactor get_attesting_indices (#8090)
Browse files Browse the repository at this point in the history
* refactor get_attesting_indices (Electra phase0 changes)
  • Loading branch information
mehdi-aouadi authored Mar 25, 2024
1 parent 628cb79 commit 684dcb5
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -693,12 +693,7 @@ public void waitForAttestationBeingGossiped(

Set<UInt64> attesterIndicesInAttestations =
block.getMessage().getBody().attestations.stream()
.map(
a ->
spec.getAttestingIndices(
state,
a.asInternalAttestation(spec).getData(),
a.asInternalAttestation(spec).getAggregationBits()))
.map(a -> spec.getAttestingIndices(state, a.asInternalAttestation(spec)))
.flatMap(Collection::stream)
.map(UInt64::valueOf)
.collect(toSet());
Expand Down
5 changes: 2 additions & 3 deletions ethereum/spec/src/main/java/tech/pegasys/teku/spec/Spec.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import tech.pegasys.teku.infrastructure.bytes.Bytes4;
import tech.pegasys.teku.infrastructure.ssz.Merkleizable;
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.cache.IndexedAttestationCache;
import tech.pegasys.teku.spec.config.NetworkingSpecConfig;
Expand Down Expand Up @@ -868,8 +867,8 @@ public Map<Integer, CommitteeAssignment> getValidatorIndexToCommitteeAssignmentM
}

// Attestation helpers
public IntList getAttestingIndices(BeaconState state, AttestationData data, SszBitlist bits) {
return atState(state).getAttestationUtil().getAttestingIndices(state, data, bits);
public IntList getAttestingIndices(final BeaconState state, final Attestation attestation) {
return atState(state).getAttestationUtil().getAttestingIndices(state, attestation);
}

public AttestationData getGenericAttestationData(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
import tech.pegasys.teku.spec.datastructures.type.SszSignature;
import tech.pegasys.teku.spec.datastructures.type.SszSignatureSchema;

public class Attestation
extends Container3<Attestation, SszBitlist, AttestationData, SszSignature> {
public class Attestation extends Container3<Attestation, SszBitlist, AttestationData, SszSignature>
implements AttestationContainer {

public static class AttestationSchema
extends ContainerSchema3<Attestation, SszBitlist, AttestationData, SszSignature> {
Expand Down Expand Up @@ -91,10 +91,12 @@ public Collection<Bytes32> getDependentBlockRoots() {
return Sets.newHashSet(getData().getTarget().getRoot(), getData().getBeaconBlockRoot());
}

@Override
public SszBitlist getAggregationBits() {
return getField0();
}

@Override
public AttestationData getData() {
return getField1();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright Consensys Software Inc., 2024
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package tech.pegasys.teku.spec.datastructures.operations;

import tech.pegasys.teku.infrastructure.ssz.SszContainer;
import tech.pegasys.teku.infrastructure.ssz.SszData;
import tech.pegasys.teku.infrastructure.ssz.collections.SszBitlist;

/**
* Interface used to represent different types of attestations ({@link Attestation} and {@link
* tech.pegasys.teku.spec.datastructures.state.PendingAttestation})
*/
public interface AttestationContainer extends SszData, SszContainer {

AttestationData getData();

SszBitlist getAggregationBits();
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
import tech.pegasys.teku.infrastructure.ssz.tree.TreeNode;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.spec.config.SpecConfig;
import tech.pegasys.teku.spec.datastructures.operations.AttestationContainer;
import tech.pegasys.teku.spec.datastructures.operations.AttestationData;

public class PendingAttestation
extends Container4<PendingAttestation, SszBitlist, AttestationData, SszUInt64, SszUInt64> {
extends Container4<PendingAttestation, SszBitlist, AttestationData, SszUInt64, SszUInt64>
implements AttestationContainer {

public static class PendingAttestationSchema
extends ContainerSchema4<
Expand Down Expand Up @@ -82,10 +84,12 @@ public PendingAttestationSchema getSchema() {
return (PendingAttestationSchema) super.getSchema();
}

@Override
public SszBitlist getAggregationBits() {
return getField0();
}

@Override
public AttestationData getData() {
return getField1();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import tech.pegasys.teku.spec.datastructures.attestation.ValidatableAttestation;
import tech.pegasys.teku.spec.datastructures.blocks.BeaconBlockSummary;
import tech.pegasys.teku.spec.datastructures.operations.Attestation;
import tech.pegasys.teku.spec.datastructures.operations.AttestationContainer;
import tech.pegasys.teku.spec.datastructures.operations.AttestationData;
import tech.pegasys.teku.spec.datastructures.operations.IndexedAttestation;
import tech.pegasys.teku.spec.datastructures.operations.IndexedAttestation.IndexedAttestationSchema;
Expand Down Expand Up @@ -98,8 +99,7 @@ public boolean isSlashableAttestationData(
*/
public IndexedAttestation getIndexedAttestation(
final BeaconState state, final Attestation attestation) {
final List<Integer> attestingIndices =
getAttestingIndices(state, attestation.getData(), attestation.getAggregationBits());
final List<Integer> attestingIndices = getAttestingIndices(state, attestation);

final IndexedAttestationSchema indexedAttestationSchema =
schemaDefinitions.getIndexedAttestationSchema();
Expand All @@ -116,28 +116,31 @@ public IndexedAttestation getIndexedAttestation(
* Return the sorted attesting indices corresponding to ``data`` and ``bits``.
*
* @param state
* @param data
* @param bits
* @param attestation
* @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/phase0/beacon-chain.md#get_attesting_indices</a>
*/
public IntList getAttestingIndices(
final BeaconState state, final AttestationData data, final SszBitlist bits) {
return IntList.of(streamAttestingIndices(state, data, bits).toArray());
final BeaconState state, final AttestationContainer attestation) {
return IntList.of(streamAttestingIndices(state, attestation).toArray());
}

public IntStream streamAttestingIndices(
final BeaconState state, final AttestationData data, final SszBitlist bits) {
IntList committee =
final BeaconState state, final AttestationContainer attestation) {
final AttestationData data = attestation.getData();
final SszBitlist aggregationBits = attestation.getAggregationBits();
final IntList committee =
beaconStateAccessors.getBeaconCommittee(state, data.getSlot(), data.getIndex());
checkArgument(
bits.size() == committee.size(),
aggregationBits.size() == committee.size(),
"Aggregation bitlist size (%s) does not match committee size (%s)",
bits.size(),
aggregationBits.size(),
committee.size());
return IntStream.range(0, committee.size()).filter(bits::getBit).map(committee::getInt);
return IntStream.range(0, committee.size())
.filter(aggregationBits::getBit)
.map(committee::getInt);
}

public AttestationProcessingResult isValidIndexedAttestation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private void translateParticipation(
// Apply flags to all attesting validators
final SszMutableList<SszByte> epochParticipation = state.getPreviousEpochParticipation();
attestationUtil
.streamAttestingIndices(state, data, attestation.getAggregationBits())
.streamAttestingIndices(state, attestation)
.forEach(
index -> {
final byte previousFlags = epochParticipation.get(index).get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ protected void processParticipation(

// Apply flags to attestingIndices
attestationUtil
.streamAttestingIndices(state, data, attestation.getAggregationBits())
.streamAttestingIndices(state, attestation)
.mapToObj(statuses::get)
.forEach(updates::apply);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,7 @@ private SafeFuture<InternalValidationResult> verifyAggregate(
final BeaconState state = maybeState.get();

// [REJECT] The aggregate attestation has participants
final IntList attestingIndices =
spec.getAttestingIndices(
state, aggregate.getData(), aggregate.getAggregationBits());
final IntList attestingIndices = spec.getAttestingIndices(state, aggregate);
if (attestingIndices.isEmpty()) {
return SafeFuture.completedFuture(
reject(
Expand Down

0 comments on commit 684dcb5

Please sign in to comment.