use of tech.pegasys.teku.infrastructure.ssz.SszVector in project teku by ConsenSys.
the class SyncCommitteeUtil method getSyncSubcommittees.
public Map<UInt64, SyncSubcommitteeAssignments> getSyncSubcommittees(final BeaconState state, final UInt64 epoch) {
final UInt64 syncCommitteePeriod = computeSyncCommitteePeriod(epoch);
final UInt64 currentEpoch = beaconStateAccessors.getCurrentEpoch(state);
final UInt64 currentSyncCommitteePeriod = computeSyncCommitteePeriod(currentEpoch);
checkArgument(isStateUsableForCommitteeCalculationAtEpoch(state, epoch), "State must be in the same or previous sync committee period. Cannot calculate epoch %s from state at slot %s", epoch, state.getSlot());
final BeaconStateAltair altairState = BeaconStateAltair.required(state);
return BeaconStateCache.getTransitionCaches(altairState).getSyncCommitteeCache().get(syncCommitteePeriod, period -> {
final SyncCommittee syncCommittee;
if (syncCommitteePeriod.equals(currentSyncCommitteePeriod)) {
syncCommittee = altairState.getCurrentSyncCommittee();
} else {
syncCommittee = altairState.getNextSyncCommittee();
}
final int subcommitteeSize = getSubcommitteeSize();
final SszVector<SszPublicKey> pubkeys = syncCommittee.getPubkeys();
final Map<UInt64, SyncSubcommitteeAssignments.Builder> subcommitteeAssignments = new HashMap<>();
for (int index = 0; index < pubkeys.size(); index++) {
final BLSPublicKey pubkey = pubkeys.get(index).getBLSPublicKey();
final UInt64 validatorIndex = UInt64.valueOf(validatorsUtil.getValidatorIndex(altairState, pubkey).orElseThrow(() -> new IllegalStateException("Unknown validator assigned to sync committee: " + pubkey)));
final int subcommitteeIndex = index / subcommitteeSize;
final int subcommitteeParticipationIndex = index - (subcommitteeIndex * subcommitteeSize);
// Note we're using plain HashMap here instead of a concurrent alternative as they
// are created once here and then never modified so safe to access from multiple
// threads.
subcommitteeAssignments.computeIfAbsent(validatorIndex, __ -> SyncSubcommitteeAssignments.builder()).addAssignment(subcommitteeIndex, subcommitteeParticipationIndex).addCommitteeIndex(index);
}
return subcommitteeAssignments.entrySet().stream().collect(toUnmodifiableMap(Map.Entry::getKey, entry -> entry.getValue().build()));
});
}
Aggregations