Search in sources :

Example 1 with SszByte

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

the class AltairStateUpgrade method translateParticipation.

private void translateParticipation(final MutableBeaconStateAltair state, final SszList<PendingAttestation> pendingAttestations) {
    for (PendingAttestation attestation : pendingAttestations) {
        final AttestationData data = attestation.getData();
        final UInt64 inclusionDelay = attestation.getInclusion_delay();
        // Translate attestation inclusion info to flag indices
        final List<Integer> participationFlagIndices = beaconStateAccessors.getAttestationParticipationFlagIndices(state, data, inclusionDelay);
        // Apply flags to all attesting validators
        final SszMutableList<SszByte> epochParticipation = state.getPreviousEpochParticipation();
        attestationUtil.streamAttestingIndices(state, data, attestation.getAggregation_bits()).forEach(index -> {
            final byte previousFlags = epochParticipation.get(index).get();
            byte newFlags = previousFlags;
            for (int flagIndex : participationFlagIndices) {
                newFlags = miscHelpersAltair.addFlag(newFlags, flagIndex);
            }
            if (previousFlags != newFlags) {
                epochParticipation.set(index, SszByte.of(newFlags));
            }
        });
    }
}
Also used : PendingAttestation(tech.pegasys.teku.spec.datastructures.state.PendingAttestation) AttestationData(tech.pegasys.teku.spec.datastructures.operations.AttestationData) SszByte(tech.pegasys.teku.infrastructure.ssz.primitive.SszByte) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) SszUInt64(tech.pegasys.teku.infrastructure.ssz.primitive.SszUInt64)

Example 2 with SszByte

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

the class ValidatorStatusFactoryAltair method processParticipation.

@Override
protected void processParticipation(final List<ValidatorStatus> statuses, final BeaconState genericState, final UInt64 previousEpoch, final UInt64 currentEpoch) {
    final BeaconStateAltair state = BeaconStateAltair.required(genericState);
    final SszList<SszByte> previousParticipation = state.getPreviousEpochParticipation();
    final SszList<SszByte> currentParticipation = state.getCurrentEpochParticipation();
    for (int i = 0; i < statuses.size(); i++) {
        final ValidatorStatus status = statuses.get(i);
        if (status.isActiveInPreviousEpoch()) {
            final byte previousParticipationFlags = previousParticipation.get(i).get();
            if (miscHelpersAltair.hasFlag(previousParticipationFlags, ParticipationFlags.TIMELY_SOURCE_FLAG_INDEX)) {
                status.updatePreviousEpochSourceAttester(true);
            }
            if (miscHelpersAltair.hasFlag(previousParticipationFlags, ParticipationFlags.TIMELY_TARGET_FLAG_INDEX)) {
                status.updatePreviousEpochTargetAttester(true);
            }
            if (miscHelpersAltair.hasFlag(previousParticipationFlags, ParticipationFlags.TIMELY_HEAD_FLAG_INDEX)) {
                status.updatePreviousEpochHeadAttester(true);
            }
        }
        if (status.isActiveInCurrentEpoch()) {
            final byte currentParticipationFlags = currentParticipation.get(i).get();
            if (miscHelpersAltair.hasFlag(currentParticipationFlags, ParticipationFlags.TIMELY_SOURCE_FLAG_INDEX)) {
                status.updateCurrentEpochSourceAttester(true);
            }
            if (miscHelpersAltair.hasFlag(currentParticipationFlags, ParticipationFlags.TIMELY_TARGET_FLAG_INDEX)) {
                status.updateCurrentEpochTargetAttester(true);
            }
        }
    }
}
Also used : SszByte(tech.pegasys.teku.infrastructure.ssz.primitive.SszByte) BeaconStateAltair(tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.BeaconStateAltair) ValidatorStatus(tech.pegasys.teku.spec.logic.common.statetransition.epoch.status.ValidatorStatus)

Example 3 with SszByte

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

the class EpochProcessorAltairTest method processParticipationUpdates.

@Test
public void processParticipationUpdates() {
    final BeaconStateAltair randomState = generateRandomState();
    final int validatorCount = randomState.getValidators().size();
    final SszList<SszByte> currentEpochParticipationOrig = randomState.getCurrentEpochParticipation();
    final SszList<SszByte> previousEpochParticipationOrig = randomState.getPreviousEpochParticipation();
    final BeaconStateAltair updated = randomState.updatedAltair(epochProcessor::processParticipationUpdates);
    assertThat(updated.getPreviousEpochParticipation()).isNotEqualTo(previousEpochParticipationOrig);
    assertThat(updated.getPreviousEpochParticipation()).isEqualTo(currentEpochParticipationOrig);
    assertThat(updated.getCurrentEpochParticipation().size()).isEqualTo(validatorCount);
    for (int i = 0; i < validatorCount; i++) {
        assertThat(updated.getCurrentEpochParticipation().get(i).get()).isEqualTo((byte) 0);
    }
}
Also used : SszByte(tech.pegasys.teku.infrastructure.ssz.primitive.SszByte) BeaconStateAltair(tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.BeaconStateAltair) Test(org.junit.jupiter.api.Test)

Example 4 with SszByte

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

the class BlockProcessorAltair method processAttestation.

@Override
protected void processAttestation(final MutableBeaconState genericState, final Attestation attestation, final IndexedAttestationProvider indexedAttestationProvider) {
    final MutableBeaconStateAltair state = MutableBeaconStateAltair.required(genericState);
    final AttestationData data = attestation.getData();
    final List<Integer> participationFlagIndices = beaconStateAccessorsAltair.getAttestationParticipationFlagIndices(state, data, state.getSlot().minus(data.getSlot()));
    // Update epoch participation flags
    final SszMutableList<SszByte> epochParticipation;
    if (data.getTarget().getEpoch().equals(beaconStateAccessors.getCurrentEpoch(state))) {
        epochParticipation = state.getCurrentEpochParticipation();
    } else {
        epochParticipation = state.getPreviousEpochParticipation();
    }
    UInt64 proposerRewardNumerator = UInt64.ZERO;
    final SszUInt64List attestingIndices = indexedAttestationProvider.getIndexedAttestation(attestation).getAttesting_indices();
    for (SszUInt64 attestingIndex : attestingIndices) {
        final int index = attestingIndex.get().intValue();
        byte participationFlags = epochParticipation.get(index).get();
        final UInt64 baseReward = beaconStateAccessorsAltair.getBaseReward(state, index);
        boolean shouldUpdate = false;
        for (int flagIndex = 0; flagIndex < PARTICIPATION_FLAG_WEIGHTS.size(); flagIndex++) {
            final UInt64 weight = PARTICIPATION_FLAG_WEIGHTS.get(flagIndex);
            if (participationFlagIndices.contains(flagIndex) && !miscHelpersAltair.hasFlag(participationFlags, flagIndex)) {
                shouldUpdate = true;
                participationFlags = miscHelpersAltair.addFlag(participationFlags, flagIndex);
                proposerRewardNumerator = proposerRewardNumerator.plus(baseReward.times(weight));
            }
        }
        if (shouldUpdate) {
            epochParticipation.set(index, SszByte.of(participationFlags));
        }
    }
    if (!proposerRewardNumerator.isZero()) {
        final int proposerIndex = beaconStateAccessors.getBeaconProposerIndex(state);
        final UInt64 proposerRewardDenominator = WEIGHT_DENOMINATOR.minus(PROPOSER_WEIGHT).times(WEIGHT_DENOMINATOR).dividedBy(PROPOSER_WEIGHT);
        final UInt64 proposerReward = proposerRewardNumerator.dividedBy(proposerRewardDenominator);
        beaconStateMutators.increaseBalance(state, proposerIndex, proposerReward);
    }
}
Also used : AttestationData(tech.pegasys.teku.spec.datastructures.operations.AttestationData) SszUInt64(tech.pegasys.teku.infrastructure.ssz.primitive.SszUInt64) SszUInt64List(tech.pegasys.teku.infrastructure.ssz.collections.SszUInt64List) SszByte(tech.pegasys.teku.infrastructure.ssz.primitive.SszByte) 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)

Aggregations

SszByte (tech.pegasys.teku.infrastructure.ssz.primitive.SszByte)4 SszUInt64 (tech.pegasys.teku.infrastructure.ssz.primitive.SszUInt64)2 UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)2 AttestationData (tech.pegasys.teku.spec.datastructures.operations.AttestationData)2 BeaconStateAltair (tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.BeaconStateAltair)2 Test (org.junit.jupiter.api.Test)1 SszUInt64List (tech.pegasys.teku.infrastructure.ssz.collections.SszUInt64List)1 PendingAttestation (tech.pegasys.teku.spec.datastructures.state.PendingAttestation)1 MutableBeaconStateAltair (tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.MutableBeaconStateAltair)1 ValidatorStatus (tech.pegasys.teku.spec.logic.common.statetransition.epoch.status.ValidatorStatus)1