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));
}
});
}
}
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);
}
}
}
}
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);
}
}
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);
}
}
Aggregations