Search in sources :

Example 1 with SszMutableList

use of tech.pegasys.teku.infrastructure.ssz.SszMutableList in project teku by ConsenSys.

the class AbstractEpochProcessor method processRegistryUpdates.

/**
 * Processes validator registry updates
 */
@Override
public void processRegistryUpdates(MutableBeaconState state, List<ValidatorStatus> statuses) throws EpochProcessingException {
    try {
        // Process activation eligibility and ejections
        SszMutableList<Validator> validators = state.getValidators();
        final UInt64 currentEpoch = beaconStateAccessors.getCurrentEpoch(state);
        for (int index = 0; index < validators.size(); index++) {
            final ValidatorStatus status = statuses.get(index);
            // confirm it isn't already in the queue.
            if (!status.isActiveInCurrentEpoch() && status.getCurrentEpochEffectiveBalance().equals(specConfig.getMaxEffectiveBalance())) {
                final Validator validator = validators.get(index);
                if (validator.getActivation_eligibility_epoch().equals(SpecConfig.FAR_FUTURE_EPOCH)) {
                    validators.set(index, validator.withActivation_eligibility_epoch(currentEpoch.plus(UInt64.ONE)));
                }
            }
            if (status.isActiveInCurrentEpoch() && status.getCurrentEpochEffectiveBalance().isLessThanOrEqualTo(specConfig.getEjectionBalance())) {
                beaconStateMutators.initiateValidatorExit(state, index);
            }
        }
        // Queue validators eligible for activation and not yet dequeued for activation
        List<Integer> activationQueue = IntStream.range(0, state.getValidators().size()).filter(index -> !statuses.get(index).isActiveInCurrentEpoch()).filter(index -> {
            Validator validator = state.getValidators().get(index);
            return validatorsUtil.isEligibleForActivation(state, validator);
        }).boxed().sorted((index1, index2) -> {
            int comparisonResult = state.getValidators().get(index1).getActivation_eligibility_epoch().compareTo(state.getValidators().get(index2).getActivation_eligibility_epoch());
            if (comparisonResult == 0) {
                return index1.compareTo(index2);
            } else {
                return comparisonResult;
            }
        }).collect(Collectors.toList());
        // Dequeued validators for activation up to churn limit (without resetting activation epoch)
        int churnLimit = beaconStateAccessors.getValidatorChurnLimit(state).intValue();
        int sublistSize = Math.min(churnLimit, activationQueue.size());
        for (Integer index : activationQueue.subList(0, sublistSize)) {
            state.getValidators().update(index, validator -> validator.withActivation_epoch(miscHelpers.computeActivationExitEpoch(currentEpoch)));
        }
    } catch (IllegalArgumentException e) {
        throw new EpochProcessingException(e);
    }
}
Also used : IntStream(java.util.stream.IntStream) ValidatorStatusFactory(tech.pegasys.teku.spec.logic.common.statetransition.epoch.status.ValidatorStatusFactory) SszList(tech.pegasys.teku.infrastructure.ssz.SszList) BeaconStateAccessors(tech.pegasys.teku.spec.logic.common.helpers.BeaconStateAccessors) TotalBalances(tech.pegasys.teku.spec.logic.common.statetransition.epoch.status.TotalBalances) HistoricalBatch(tech.pegasys.teku.spec.datastructures.state.HistoricalBatch) RewardAndPenalty(tech.pegasys.teku.spec.logic.common.statetransition.epoch.RewardAndPenaltyDeltas.RewardAndPenalty) SszBitvector(tech.pegasys.teku.infrastructure.ssz.collections.SszBitvector) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) ValidatorStatus(tech.pegasys.teku.spec.logic.common.statetransition.epoch.status.ValidatorStatus) BeaconStateUtil(tech.pegasys.teku.spec.logic.common.util.BeaconStateUtil) ValidatorStatuses(tech.pegasys.teku.spec.logic.common.statetransition.epoch.status.ValidatorStatuses) Validator(tech.pegasys.teku.spec.datastructures.state.Validator) MiscHelpers(tech.pegasys.teku.spec.logic.common.helpers.MiscHelpers) SszMutableUInt64List(tech.pegasys.teku.infrastructure.ssz.collections.SszMutableUInt64List) SpecConfig(tech.pegasys.teku.spec.config.SpecConfig) SszUInt64List(tech.pegasys.teku.infrastructure.ssz.collections.SszUInt64List) SchemaDefinitions(tech.pegasys.teku.spec.schemas.SchemaDefinitions) ValidatorsUtil(tech.pegasys.teku.spec.logic.common.util.ValidatorsUtil) Collectors(java.util.stream.Collectors) MutableBeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.MutableBeaconState) List(java.util.List) EpochProcessingException(tech.pegasys.teku.spec.logic.common.statetransition.exceptions.EpochProcessingException) BeaconStateMutators(tech.pegasys.teku.spec.logic.common.helpers.BeaconStateMutators) SszMutableList(tech.pegasys.teku.infrastructure.ssz.SszMutableList) Checkpoint(tech.pegasys.teku.spec.datastructures.state.Checkpoint) BeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState) EpochProcessingException(tech.pegasys.teku.spec.logic.common.statetransition.exceptions.EpochProcessingException) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) Validator(tech.pegasys.teku.spec.datastructures.state.Validator) Checkpoint(tech.pegasys.teku.spec.datastructures.state.Checkpoint) ValidatorStatus(tech.pegasys.teku.spec.logic.common.statetransition.epoch.status.ValidatorStatus)

Aggregations

List (java.util.List)1 Collectors (java.util.stream.Collectors)1 IntStream (java.util.stream.IntStream)1 SszList (tech.pegasys.teku.infrastructure.ssz.SszList)1 SszMutableList (tech.pegasys.teku.infrastructure.ssz.SszMutableList)1 SszBitvector (tech.pegasys.teku.infrastructure.ssz.collections.SszBitvector)1 SszMutableUInt64List (tech.pegasys.teku.infrastructure.ssz.collections.SszMutableUInt64List)1 SszUInt64List (tech.pegasys.teku.infrastructure.ssz.collections.SszUInt64List)1 UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)1 SpecConfig (tech.pegasys.teku.spec.config.SpecConfig)1 Checkpoint (tech.pegasys.teku.spec.datastructures.state.Checkpoint)1 HistoricalBatch (tech.pegasys.teku.spec.datastructures.state.HistoricalBatch)1 Validator (tech.pegasys.teku.spec.datastructures.state.Validator)1 BeaconState (tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState)1 MutableBeaconState (tech.pegasys.teku.spec.datastructures.state.beaconstate.MutableBeaconState)1 BeaconStateAccessors (tech.pegasys.teku.spec.logic.common.helpers.BeaconStateAccessors)1 BeaconStateMutators (tech.pegasys.teku.spec.logic.common.helpers.BeaconStateMutators)1 MiscHelpers (tech.pegasys.teku.spec.logic.common.helpers.MiscHelpers)1 RewardAndPenalty (tech.pegasys.teku.spec.logic.common.statetransition.epoch.RewardAndPenaltyDeltas.RewardAndPenalty)1 TotalBalances (tech.pegasys.teku.spec.logic.common.statetransition.epoch.status.TotalBalances)1