Skip to content

Commit

Permalink
add inclusion committee assignment helper
Browse files Browse the repository at this point in the history
  • Loading branch information
mehdi-aouadi committed Jan 14, 2025
1 parent 7c78ac3 commit 947f4a5
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
7 changes: 7 additions & 0 deletions ethereum/spec/src/main/java/tech/pegasys/teku/spec/Spec.java
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,13 @@ public Optional<CommitteeAssignment> getCommitteeAssignment(
return atEpoch(epoch).getValidatorsUtil().getCommitteeAssignment(state, epoch, validatorIndex);
}

public Optional<UInt64> getInclusionCommitteeAssignment(
final BeaconState state, final UInt64 epoch, final int validatorIndex) {
return atEpoch(epoch)
.getValidatorsUtil()
.getInclusionCommitteeAssignment(state, epoch, validatorIndex);
}

public Int2ObjectMap<CommitteeAssignment> getValidatorIndexToCommitteeAssignmentMap(
final BeaconState state, final UInt64 epoch) {
return atEpoch(epoch)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import tech.pegasys.teku.spec.datastructures.state.versions.electra.PendingPartialWithdrawal;
import tech.pegasys.teku.spec.logic.common.helpers.BeaconStateAccessors;
import tech.pegasys.teku.spec.logic.common.helpers.MiscHelpers;
import tech.pegasys.teku.spec.logic.versions.eip7805.helpers.BeaconStateAccessorsEip7805;

public class ValidatorsUtil {
private final SpecConfig specConfig;
Expand Down Expand Up @@ -136,6 +137,35 @@ private Optional<CommitteeAssignment> getCommitteeAssignment(
return Optional.empty();
}

/**
* Returns the slot during the requested epoch in which the validator with index
* ``validator_index`` is a member of the ILC. Returns None if no assignment is found.
*
* @param state the BeaconState.
* @param epoch either on or between previous or current epoch.
* @param validatorIndex the validator that is calling this function.
* @return Optional containing the slot if any, empty otherwise
*/
public Optional<UInt64> getInclusionCommitteeAssignment(
final BeaconState state, final UInt64 epoch, final int validatorIndex) {
final UInt64 nextEpoch = beaconStateAccessors.getCurrentEpoch(state).plus(UInt64.ONE);
checkArgument(
epoch.compareTo(nextEpoch) <= 0,
"get_inclusion_committee_assignment: Epoch number too high");
final UInt64 startSlot = miscHelpers.computeStartSlotAtEpoch(epoch);
for (UInt64 slot = startSlot;
slot.isLessThan(startSlot.plus(specConfig.getSlotsPerEpoch()));
slot = slot.plus(UInt64.ONE)) {
final IntList inclusionListCommittee =
BeaconStateAccessorsEip7805.required(beaconStateAccessors)
.getInclusionListCommittee(state, slot);
if (inclusionListCommittee.contains(validatorIndex)) {
return Optional.of(slot);
}
}
return Optional.empty();
}

public EpochAttestationSchedule getAttestationCommitteesAtEpoch(
final BeaconState state, final UInt64 epoch, final UInt64 committeeCountPerSlot) {
final UInt64 nextEpoch = beaconStateAccessors.getCurrentEpoch(state).plus(UInt64.ONE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

package tech.pegasys.teku.spec.logic.versions.eip7805.helpers;

import static com.google.common.base.Preconditions.checkArgument;

import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.ints.IntList;
import org.apache.tuweni.bytes.Bytes32;
Expand All @@ -22,6 +24,7 @@
import tech.pegasys.teku.spec.config.SpecConfigElectra;
import tech.pegasys.teku.spec.constants.Domain;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState;
import tech.pegasys.teku.spec.logic.common.helpers.BeaconStateAccessors;
import tech.pegasys.teku.spec.logic.versions.electra.helpers.BeaconStateAccessorsElectra;
import tech.pegasys.teku.spec.logic.versions.electra.helpers.MiscHelpersElectra;
import tech.pegasys.teku.spec.logic.versions.electra.helpers.PredicatesElectra;
Expand All @@ -38,6 +41,16 @@ public BeaconStateAccessorsEip7805(
this.specConfigEip7805 = config.toVersionEip7805().orElseThrow();
}

public static BeaconStateAccessorsEip7805 required(
final BeaconStateAccessors beaconStateAccessors) {
checkArgument(
beaconStateAccessors instanceof BeaconStateAccessorsEip7805,
"Expected %s but it was %s",
BeaconStateAccessorsEip7805.class,
beaconStateAccessors.getClass());
return (BeaconStateAccessorsEip7805) beaconStateAccessors;
}

public IntList getInclusionListCommittee(final BeaconState state, final UInt64 slot) {
final UInt64 epoch = miscHelpers.computeEpochAtSlot(slot);
final Bytes32 seed = getSeed(state, epoch, Domain.DOMAIN_IL_COMMITTEE);
Expand Down

0 comments on commit 947f4a5

Please sign in to comment.