Search in sources :

Example 1 with BeaconStateAltair

use of tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.BeaconStateAltair 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()));
    });
}
Also used : BeaconBlockBodySchemaAltair(tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.altair.BeaconBlockBodySchemaAltair) HashMap(java.util.HashMap) SszPublicKey(tech.pegasys.teku.spec.datastructures.type.SszPublicKey) Bytes(org.apache.tuweni.bytes.Bytes) SchemaDefinitionsAltair(tech.pegasys.teku.spec.schemas.SchemaDefinitionsAltair) ContributionAndProof(tech.pegasys.teku.spec.datastructures.operations.versions.altair.ContributionAndProof) SYNC_COMMITTEE_SUBNET_COUNT(tech.pegasys.teku.spec.constants.NetworkConstants.SYNC_COMMITTEE_SUBNET_COUNT) ArrayList(java.util.ArrayList) Collectors.toUnmodifiableMap(java.util.stream.Collectors.toUnmodifiableMap) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ValidatorConstants(tech.pegasys.teku.spec.constants.ValidatorConstants) IntIterable(it.unimi.dsi.fastutil.ints.IntIterable) Map(java.util.Map) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) ForkInfo(tech.pegasys.teku.spec.datastructures.state.ForkInfo) SyncCommitteeContributionSchema(tech.pegasys.teku.spec.datastructures.operations.versions.altair.SyncCommitteeContributionSchema) Bytes32(org.apache.tuweni.bytes.Bytes32) SyncAggregateSchema(tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.altair.SyncAggregateSchema) Domain(tech.pegasys.teku.spec.constants.Domain) SyncCommitteeContribution(tech.pegasys.teku.spec.datastructures.operations.versions.altair.SyncCommitteeContribution) BeaconStateAccessorsAltair(tech.pegasys.teku.spec.logic.versions.altair.helpers.BeaconStateAccessorsAltair) MiscHelpers(tech.pegasys.teku.spec.logic.common.helpers.MiscHelpers) BLS(tech.pegasys.teku.bls.BLS) BLSSignature(tech.pegasys.teku.bls.BLSSignature) BLSPublicKey(tech.pegasys.teku.bls.BLSPublicKey) SyncAggregatorSelectionData(tech.pegasys.teku.spec.datastructures.operations.versions.altair.SyncAggregatorSelectionData) SyncSubcommitteeAssignments(tech.pegasys.teku.spec.datastructures.util.SyncSubcommitteeAssignments) MutableBeaconStateAltair(tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.MutableBeaconStateAltair) SignedContributionAndProof(tech.pegasys.teku.spec.datastructures.operations.versions.altair.SignedContributionAndProof) Hash(tech.pegasys.teku.infrastructure.crypto.Hash) SyncCommittee(tech.pegasys.teku.spec.datastructures.state.SyncCommittee) MutableBeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.MutableBeaconState) List(java.util.List) IntList(it.unimi.dsi.fastutil.ints.IntList) MathHelpers.bytesToUInt64(tech.pegasys.teku.spec.logic.common.helpers.MathHelpers.bytesToUInt64) IntOpenHashSet(it.unimi.dsi.fastutil.ints.IntOpenHashSet) SszVector(tech.pegasys.teku.infrastructure.ssz.SszVector) IntSet(it.unimi.dsi.fastutil.ints.IntSet) BeaconStateCache(tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconStateCache) BeaconStateAltair(tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.BeaconStateAltair) IntArrayList(it.unimi.dsi.fastutil.ints.IntArrayList) SpecConfigAltair(tech.pegasys.teku.spec.config.SpecConfigAltair) SyncAggregate(tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.altair.SyncAggregate) BeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState) HashMap(java.util.HashMap) SyncCommittee(tech.pegasys.teku.spec.datastructures.state.SyncCommittee) SszPublicKey(tech.pegasys.teku.spec.datastructures.type.SszPublicKey) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) MathHelpers.bytesToUInt64(tech.pegasys.teku.spec.logic.common.helpers.MathHelpers.bytesToUInt64) BLSPublicKey(tech.pegasys.teku.bls.BLSPublicKey) HashMap(java.util.HashMap) Collectors.toUnmodifiableMap(java.util.stream.Collectors.toUnmodifiableMap) Map(java.util.Map) MutableBeaconStateAltair(tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.MutableBeaconStateAltair) BeaconStateAltair(tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.BeaconStateAltair)

Example 2 with BeaconStateAltair

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

the class EpochProcessorAltair method getRewardAndPenaltyDeltas.

@Override
public RewardAndPenaltyDeltas getRewardAndPenaltyDeltas(final BeaconState genericState, final ValidatorStatuses validatorStatuses) {
    final BeaconStateAltair state = BeaconStateAltair.required(genericState);
    final RewardsAndPenaltiesCalculatorAltair calculator = new RewardsAndPenaltiesCalculatorAltair(specConfigAltair, state, validatorStatuses, miscHelpersAltair, beaconStateAccessorsAltair);
    return calculator.getDeltas();
}
Also used : MutableBeaconStateAltair(tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.MutableBeaconStateAltair) BeaconStateAltair(tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.BeaconStateAltair)

Example 3 with BeaconStateAltair

use of tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.BeaconStateAltair 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 4 with BeaconStateAltair

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

the class BellatrixStateUpgrade method upgrade.

@Override
public BeaconStateBellatrix upgrade(final BeaconState preState) {
    final UInt64 epoch = beaconStateAccessors.getCurrentEpoch(preState);
    BeaconStateAltair preStateAltair = BeaconStateAltair.required(preState);
    return schemaDefinitions.getBeaconStateSchema().createEmpty().updatedBellatrix(state -> {
        BeaconStateFields.copyCommonFieldsFromSource(state, preState);
        state.setCurrentEpochParticipation(preStateAltair.getCurrentEpochParticipation());
        state.setPreviousEpochParticipation(preStateAltair.getPreviousEpochParticipation());
        state.setCurrentSyncCommittee(preStateAltair.getCurrentSyncCommittee());
        state.setNextSyncCommittee(preStateAltair.getNextSyncCommittee());
        state.setInactivityScores(preStateAltair.getInactivityScores());
        state.setFork(new Fork(preState.getFork().getCurrent_version(), specConfig.getBellatrixForkVersion(), epoch));
        state.setLatestExecutionPayloadHeader(schemaDefinitions.getExecutionPayloadHeaderSchema().getDefault());
    });
}
Also used : Fork(tech.pegasys.teku.spec.datastructures.state.Fork) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) BeaconStateAltair(tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.BeaconStateAltair)

Example 5 with BeaconStateAltair

use of tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.BeaconStateAltair 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)

Aggregations

BeaconStateAltair (tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.BeaconStateAltair)19 Test (org.junit.jupiter.api.Test)11 UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)10 BLSPublicKey (tech.pegasys.teku.bls.BLSPublicKey)4 ValidateableSyncCommitteeMessage (tech.pegasys.teku.spec.datastructures.operations.versions.altair.ValidateableSyncCommitteeMessage)3 MutableBeaconStateAltair (tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.MutableBeaconStateAltair)3 List (java.util.List)2 Optional (java.util.Optional)2 Bytes (org.apache.tuweni.bytes.Bytes)2 SszByte (tech.pegasys.teku.infrastructure.ssz.primitive.SszByte)2 SpecConfigAltair (tech.pegasys.teku.spec.config.SpecConfigAltair)2 SYNC_COMMITTEE_SUBNET_COUNT (tech.pegasys.teku.spec.constants.NetworkConstants.SYNC_COMMITTEE_SUBNET_COUNT)2 ContributionAndProof (tech.pegasys.teku.spec.datastructures.operations.versions.altair.ContributionAndProof)2 SignedContributionAndProof (tech.pegasys.teku.spec.datastructures.operations.versions.altair.SignedContributionAndProof)2 SyncCommitteeContribution (tech.pegasys.teku.spec.datastructures.operations.versions.altair.SyncCommitteeContribution)2 SyncCommitteeMessage (tech.pegasys.teku.spec.datastructures.operations.versions.altair.SyncCommitteeMessage)2 SyncCommittee (tech.pegasys.teku.spec.datastructures.state.SyncCommittee)2 BeaconState (tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState)2 SyncCommitteeUtil (tech.pegasys.teku.spec.logic.common.util.SyncCommitteeUtil)2 InternalValidationResult (tech.pegasys.teku.statetransition.validation.InternalValidationResult)2