From 947f4a5f8fef34f8ad9e614fc651b7b0d0a319e5 Mon Sep 17 00:00:00 2001 From: Mehdi AOUADI Date: Tue, 14 Jan 2025 17:04:20 +0100 Subject: [PATCH] add inclusion committee assignment helper --- .../java/tech/pegasys/teku/spec/Spec.java | 7 +++++ .../logic/common/util/ValidatorsUtil.java | 30 +++++++++++++++++++ .../helpers/BeaconStateAccessorsEip7805.java | 13 ++++++++ 3 files changed, 50 insertions(+) diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/Spec.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/Spec.java index 1cc4ed70e7a..31c0d4ea664 100644 --- a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/Spec.java +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/Spec.java @@ -891,6 +891,13 @@ public Optional getCommitteeAssignment( return atEpoch(epoch).getValidatorsUtil().getCommitteeAssignment(state, epoch, validatorIndex); } + public Optional getInclusionCommitteeAssignment( + final BeaconState state, final UInt64 epoch, final int validatorIndex) { + return atEpoch(epoch) + .getValidatorsUtil() + .getInclusionCommitteeAssignment(state, epoch, validatorIndex); + } + public Int2ObjectMap getValidatorIndexToCommitteeAssignmentMap( final BeaconState state, final UInt64 epoch) { return atEpoch(epoch) diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/logic/common/util/ValidatorsUtil.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/logic/common/util/ValidatorsUtil.java index 2f1064aab2f..b3c8a3e254a 100644 --- a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/logic/common/util/ValidatorsUtil.java +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/logic/common/util/ValidatorsUtil.java @@ -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; @@ -136,6 +137,35 @@ private Optional 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 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); diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/logic/versions/eip7805/helpers/BeaconStateAccessorsEip7805.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/logic/versions/eip7805/helpers/BeaconStateAccessorsEip7805.java index 20bdc624709..fc81952d1d3 100644 --- a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/logic/versions/eip7805/helpers/BeaconStateAccessorsEip7805.java +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/logic/versions/eip7805/helpers/BeaconStateAccessorsEip7805.java @@ -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; @@ -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; @@ -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);