Search in sources :

Example 1 with MutableBeaconStateAltair

use of tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.MutableBeaconStateAltair in project teku by ConsenSys.

the class BlockProcessorAltair method processSyncAggregate.

@Override
public void processSyncAggregate(final MutableBeaconState baseState, final SyncAggregate aggregate, final BLSSignatureVerifier signatureVerifier) throws BlockProcessingException {
    final MutableBeaconStateAltair state = MutableBeaconStateAltair.required(baseState);
    final List<BLSPublicKey> participantPubkeys = new ArrayList<>();
    final List<BLSPublicKey> idlePubkeys = new ArrayList<>();
    for (int i = 0; i < specConfigAltair.getSyncCommitteeSize(); i++) {
        final BLSPublicKey publicKey = syncCommitteeUtil.getCurrentSyncCommitteeParticipantPubKey(state, i);
        if (aggregate.getSyncCommitteeBits().getBit(i)) {
            participantPubkeys.add(publicKey);
        } else {
            idlePubkeys.add(publicKey);
        }
    }
    final UInt64 previousSlot = state.getSlot().minusMinZero(1);
    final Bytes32 domain = beaconStateAccessors.getDomain(state.getForkInfo(), Domain.SYNC_COMMITTEE, miscHelpers.computeEpochAtSlot(previousSlot));
    final Bytes32 signingRoot = miscHelpersAltair.computeSigningRoot(beaconStateAccessors.getBlockRootAtSlot(state, previousSlot), domain);
    if (!eth2FastAggregateVerify(signatureVerifier, participantPubkeys, signingRoot, aggregate.getSyncCommitteeSignature().getSignature())) {
        throw new BlockProcessingException("Invalid sync committee signature in " + aggregate);
    }
    // Compute participant and proposer rewards
    final UInt64 totalActiveIncrements = beaconStateAccessors.getTotalActiveBalance(state).dividedBy(specConfig.getEffectiveBalanceIncrement());
    final UInt64 totalBaseRewards = beaconStateAccessorsAltair.getBaseRewardPerIncrement(state).times(totalActiveIncrements);
    final UInt64 maxParticipantRewards = totalBaseRewards.times(SYNC_REWARD_WEIGHT).dividedBy(WEIGHT_DENOMINATOR).dividedBy(specConfig.getSlotsPerEpoch());
    final UInt64 participantReward = maxParticipantRewards.dividedBy(specConfigAltair.getSyncCommitteeSize());
    final UInt64 proposerReward = participantReward.times(PROPOSER_WEIGHT).dividedBy(WEIGHT_DENOMINATOR.minus(PROPOSER_WEIGHT));
    // Apply participant and proposer rewards
    participantPubkeys.stream().map(pubkey -> validatorsUtil.getValidatorIndex(state, pubkey).orElseThrow()).forEach(participantIndex -> beaconStateMutators.increaseBalance(state, participantIndex, participantReward));
    UInt64 totalProposerReward = proposerReward.times(participantPubkeys.size());
    beaconStateMutators.increaseBalance(state, beaconStateAccessors.getBeaconProposerIndex(state), totalProposerReward);
    // impose penalties for any idle validators
    idlePubkeys.stream().map(pubkey -> validatorsUtil.getValidatorIndex(state, pubkey).orElseThrow()).forEach(participantIndex -> beaconStateMutators.decreaseBalance(state, participantIndex, participantReward));
}
Also used : BeaconBlockBodyAltair(tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.altair.BeaconBlockBodyAltair) MiscHelpersAltair(tech.pegasys.teku.spec.logic.versions.altair.helpers.MiscHelpersAltair) AbstractBlockProcessor(tech.pegasys.teku.spec.logic.common.block.AbstractBlockProcessor) PARTICIPATION_FLAG_WEIGHTS(tech.pegasys.teku.spec.logic.versions.altair.helpers.MiscHelpersAltair.PARTICIPATION_FLAG_WEIGHTS) BeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.BeaconBlock) BLSSignatureVerifier(tech.pegasys.teku.bls.BLSSignatureVerifier) ArrayList(java.util.ArrayList) AttestationData(tech.pegasys.teku.spec.datastructures.operations.AttestationData) ExecutionPayload(tech.pegasys.teku.spec.datastructures.execution.ExecutionPayload) SyncCommitteeUtil(tech.pegasys.teku.spec.logic.common.util.SyncCommitteeUtil) SYNC_REWARD_WEIGHT(tech.pegasys.teku.spec.constants.IncentivizationWeights.SYNC_REWARD_WEIGHT) WEIGHT_DENOMINATOR(tech.pegasys.teku.spec.constants.IncentivizationWeights.WEIGHT_DENOMINATOR) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) BeaconStateUtil(tech.pegasys.teku.spec.logic.common.util.BeaconStateUtil) Bytes32(org.apache.tuweni.bytes.Bytes32) Attestation(tech.pegasys.teku.spec.datastructures.operations.Attestation) OperationValidator(tech.pegasys.teku.spec.logic.common.operations.validation.OperationValidator) BlockProcessingException(tech.pegasys.teku.spec.logic.common.statetransition.exceptions.BlockProcessingException) Domain(tech.pegasys.teku.spec.constants.Domain) BeaconStateAccessorsAltair(tech.pegasys.teku.spec.logic.versions.altair.helpers.BeaconStateAccessorsAltair) BLSSignature(tech.pegasys.teku.bls.BLSSignature) BLSPublicKey(tech.pegasys.teku.bls.BLSPublicKey) OptimisticExecutionPayloadExecutor(tech.pegasys.teku.spec.logic.versions.bellatrix.block.OptimisticExecutionPayloadExecutor) SszUInt64List(tech.pegasys.teku.infrastructure.ssz.collections.SszUInt64List) MutableBeaconStateAltair(tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.MutableBeaconStateAltair) ValidatorsUtil(tech.pegasys.teku.spec.logic.common.util.ValidatorsUtil) Deposit(tech.pegasys.teku.spec.datastructures.operations.Deposit) Predicates(tech.pegasys.teku.spec.logic.common.helpers.Predicates) MutableBeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.MutableBeaconState) List(java.util.List) IndexedAttestationCache(tech.pegasys.teku.spec.cache.IndexedAttestationCache) AttestationUtil(tech.pegasys.teku.spec.logic.common.util.AttestationUtil) SszByte(tech.pegasys.teku.infrastructure.ssz.primitive.SszByte) BeaconStateMutators(tech.pegasys.teku.spec.logic.common.helpers.BeaconStateMutators) SszMutableList(tech.pegasys.teku.infrastructure.ssz.SszMutableList) SpecConfigAltair(tech.pegasys.teku.spec.config.SpecConfigAltair) PROPOSER_WEIGHT(tech.pegasys.teku.spec.constants.IncentivizationWeights.PROPOSER_WEIGHT) SszUInt64(tech.pegasys.teku.infrastructure.ssz.primitive.SszUInt64) SyncAggregate(tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.altair.SyncAggregate) OperationSignatureVerifier(tech.pegasys.teku.spec.logic.common.operations.OperationSignatureVerifier) BlockProcessingException(tech.pegasys.teku.spec.logic.common.statetransition.exceptions.BlockProcessingException) ArrayList(java.util.ArrayList) BLSPublicKey(tech.pegasys.teku.bls.BLSPublicKey) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) SszUInt64(tech.pegasys.teku.infrastructure.ssz.primitive.SszUInt64) MutableBeaconStateAltair(tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.MutableBeaconStateAltair) Bytes32(org.apache.tuweni.bytes.Bytes32)

Example 2 with MutableBeaconStateAltair

use of tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.MutableBeaconStateAltair in project teku by ConsenSys.

the class BlockProcessorAltair method processNewValidator.

@Override
protected void processNewValidator(final MutableBeaconState genericState, final Deposit deposit) {
    super.processNewValidator(genericState, deposit);
    final MutableBeaconStateAltair state = MutableBeaconStateAltair.required(genericState);
    state.getPreviousEpochParticipation().append(SszByte.ZERO);
    state.getCurrentEpochParticipation().append(SszByte.ZERO);
    state.getInactivityScores().append(SszUInt64.ZERO);
}
Also used : MutableBeaconStateAltair(tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.MutableBeaconStateAltair)

Example 3 with MutableBeaconStateAltair

use of tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.MutableBeaconStateAltair in project teku by ConsenSys.

the class EpochProcessorAltair method processSyncCommitteeUpdates.

@Override
public void processSyncCommitteeUpdates(final MutableBeaconState genericState) {
    final UInt64 nextEpoch = beaconStateAccessors.getCurrentEpoch(genericState).increment();
    if (nextEpoch.mod(specConfigAltair.getEpochsPerSyncCommitteePeriod()).isZero()) {
        final MutableBeaconStateAltair state = MutableBeaconStateAltair.required(genericState);
        state.setCurrentSyncCommittee(state.getNextSyncCommittee());
        state.setNextSyncCommittee(beaconStateAccessorsAltair.getNextSyncCommittee(state));
    }
}
Also used : UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) MutableBeaconStateAltair(tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.MutableBeaconStateAltair)

Example 4 with MutableBeaconStateAltair

use of tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.MutableBeaconStateAltair in project teku by ConsenSys.

the class EpochProcessorAltair method processInactivityUpdates.

@Override
public void processInactivityUpdates(final MutableBeaconState baseState, final ValidatorStatuses validatorStatuses) {
    if (beaconStateAccessors.getCurrentEpoch(baseState).equals(SpecConfig.GENESIS_EPOCH)) {
        return;
    }
    final MutableBeaconStateAltair state = MutableBeaconStateAltair.required(baseState);
    final SszMutableUInt64List inactivityScores = state.getInactivityScores();
    final List<ValidatorStatus> statuses = validatorStatuses.getStatuses();
    final boolean isInInactivityLeak = beaconStateAccessors.isInactivityLeak(state);
    for (int i = 0; i < statuses.size(); i++) {
        final ValidatorStatus validatorStatus = statuses.get(i);
        if (!validatorStatus.isEligibleValidator()) {
            continue;
        }
        // Increase inactivity score of inactive validators
        final UInt64 currentScore = inactivityScores.getElement(i);
        UInt64 newScore;
        if (validatorStatus.isNotSlashed() && validatorStatus.isPreviousEpochTargetAttester()) {
            newScore = currentScore.minusMinZero(1);
        } else {
            newScore = currentScore.plus(specConfigAltair.getInactivityScoreBias());
        }
        // Decrease the score of all validators for forgiveness when not during a leak
        if (!isInInactivityLeak) {
            newScore = newScore.minusMinZero(specConfigAltair.getInactivityScoreRecoveryRate());
        }
        if (!currentScore.equals(newScore)) {
            inactivityScores.setElement(i, newScore);
        }
    }
}
Also used : SszMutableUInt64List(tech.pegasys.teku.infrastructure.ssz.collections.SszMutableUInt64List) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) MutableBeaconStateAltair(tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.MutableBeaconStateAltair) ValidatorStatus(tech.pegasys.teku.spec.logic.common.statetransition.epoch.status.ValidatorStatus)

Example 5 with MutableBeaconStateAltair

use of tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.MutableBeaconStateAltair in project teku by ConsenSys.

the class EpochProcessorAltair method processParticipationUpdates.

/**
 * Corresponds to process_participation_flag_updates in beacon-chain spec
 *
 * @param genericState The state to process
 * @see <a
 *     href="https://github.com/ethereum/eth2.0-specs/blob/master/specs/altair/beacon-chain.md#participation-flags-updates">Altair
 *     Participation Flags updates</a>
 */
@Override
public void processParticipationUpdates(final MutableBeaconState genericState) {
    final MutableBeaconStateAltair state = MutableBeaconStateAltair.required(genericState);
    state.setPreviousEpochParticipation(state.getCurrentEpochParticipation());
    // Reset current epoch participation flags
    state.getCurrentEpochParticipation().clear();
    state.getCurrentEpochParticipation().setAll(SszByte.ZERO, state.getValidators().size());
}
Also used : MutableBeaconStateAltair(tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.MutableBeaconStateAltair)

Aggregations

MutableBeaconStateAltair (tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.MutableBeaconStateAltair)8 UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)4 SszUInt64List (tech.pegasys.teku.infrastructure.ssz.collections.SszUInt64List)2 SszByte (tech.pegasys.teku.infrastructure.ssz.primitive.SszByte)2 SszUInt64 (tech.pegasys.teku.infrastructure.ssz.primitive.SszUInt64)2 BeaconBlockBodyAltair (tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.altair.BeaconBlockBodyAltair)2 AttestationData (tech.pegasys.teku.spec.datastructures.operations.AttestationData)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Bytes32 (org.apache.tuweni.bytes.Bytes32)1 BLSPublicKey (tech.pegasys.teku.bls.BLSPublicKey)1 BLSSignature (tech.pegasys.teku.bls.BLSSignature)1 BLSSignatureVerifier (tech.pegasys.teku.bls.BLSSignatureVerifier)1 SszMutableList (tech.pegasys.teku.infrastructure.ssz.SszMutableList)1 SszMutableUInt64List (tech.pegasys.teku.infrastructure.ssz.collections.SszMutableUInt64List)1 IndexedAttestationCache (tech.pegasys.teku.spec.cache.IndexedAttestationCache)1 SpecConfigAltair (tech.pegasys.teku.spec.config.SpecConfigAltair)1 Domain (tech.pegasys.teku.spec.constants.Domain)1 PROPOSER_WEIGHT (tech.pegasys.teku.spec.constants.IncentivizationWeights.PROPOSER_WEIGHT)1 SYNC_REWARD_WEIGHT (tech.pegasys.teku.spec.constants.IncentivizationWeights.SYNC_REWARD_WEIGHT)1