Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated proposer logic for maxEB #8343

Merged
merged 12 commits into from
Jul 9, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,22 @@
checkArgument(!indices.isEmpty(), "compute_proposer_index indices must not be empty");

final Sha256 sha256 = getSha256Instance();
final UInt64 maxEffectiveBalance = getProposerMaxEffectiveBalance();

int i = 0;
final int total = indices.size();
byte[] hash = null;
while (true) {
int candidateIndex = indices.getInt(computeShuffledIndex(i % total, total, seed));
final int candidateIndex = indices.getInt(computeShuffledIndex(i % total, total, seed));
if (i % 32 == 0) {
hash = sha256.digest(seed, uint64ToBytes(Math.floorDiv(i, 32L)));
}
int randomByte = UnsignedBytes.toInt(hash[i % 32]);
UInt64 effectiveBalance = state.getValidators().get(candidateIndex).getEffectiveBalance();
final int randomByte = UnsignedBytes.toInt(hash[i % 32]);
final UInt64 effectiveBalance =
state.getValidators().get(candidateIndex).getEffectiveBalance();
if (effectiveBalance
.times(MAX_RANDOM_BYTE)
.isGreaterThanOrEqualTo(specConfig.getMaxEffectiveBalance().times(randomByte))) {
.isGreaterThanOrEqualTo(maxEffectiveBalance.times(randomByte))) {
return candidateIndex;
}
i++;
Expand Down Expand Up @@ -388,4 +390,8 @@
public Optional<MiscHelpersElectra> toVersionElectra() {
return Optional.empty();
}

protected UInt64 getProposerMaxEffectiveBalance() {
return specConfig.getMaxEffectiveBalance();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
package tech.pegasys.teku.spec.logic.versions.electra.helpers;

import java.util.Optional;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.spec.config.SpecConfig;
import tech.pegasys.teku.spec.config.SpecConfigDeneb;
import tech.pegasys.teku.spec.config.SpecConfigElectra;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.electra.BeaconStateElectra;
import tech.pegasys.teku.spec.logic.common.helpers.MiscHelpers;
Expand Down Expand Up @@ -60,4 +62,9 @@ public boolean isFormerDepositMechanismDisabled(final BeaconState state) {
.getEth1DepositIndex()
.equals(BeaconStateElectra.required(state).getDepositReceiptsStartIndex());
}

@Override
protected UInt64 getProposerMaxEffectiveBalance() {
return SpecConfigElectra.required(specConfig).getMaxEffectiveBalanceElectra();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,19 @@
package tech.pegasys.teku.spec.logic.versions.electra.helpers;

import static org.assertj.core.api.Assertions.assertThat;
import static tech.pegasys.teku.spec.logic.common.helpers.MathHelpers.uint64ToBytes;

import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.ints.IntList;
import org.apache.tuweni.bytes.Bytes32;
import org.junit.jupiter.api.Test;
import tech.pegasys.teku.infrastructure.crypto.Hash;
import tech.pegasys.teku.infrastructure.ssz.primitive.SszUInt64;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.spec.Spec;
import tech.pegasys.teku.spec.TestSpecFactory;
import tech.pegasys.teku.spec.config.SpecConfigElectra;
import tech.pegasys.teku.spec.constants.Domain;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.electra.BeaconStateElectra;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.electra.MutableBeaconStateElectra;
Expand All @@ -39,6 +46,13 @@ public class MiscHelpersElectraTest {
predicates,
schemaDefinitionsElectra);
private final DataStructureUtil dataStructureUtil = new DataStructureUtil(spec);
final BeaconStateAccessorsElectra beaconStateAccessors =
new BeaconStateAccessorsElectra(
spec.getGenesisSpecConfig(),
new PredicatesElectra(spec.getGenesisSpecConfig()),
miscHelpersElectra);

private final IntList validatorIndices = IntArrayList.of(1, 2, 3, 4, 5, 6, 7, 0);

@Test
public void isFormerDepositMechanismDisabled_returnsTrueIfDisabled() {
Expand Down Expand Up @@ -73,4 +87,45 @@ public void isFormerDepositMechanismDisabled_returnsFalseIfNotDisabled() {

assertThat(miscHelpersElectra.isFormerDepositMechanismDisabled(state)).isFalse();
}

@Test
void consolidatedValidatorsMoreLikelyToPropose() {
final int consolidationAmount = 16;
final BeaconState state = randomStateWithConsolidatedValidator(consolidationAmount);
int idx3ProposalCount = 0;
for (int i = 1; i < 8; i++) {
final UInt64 slot = UInt64.valueOf(8 + i);
final Bytes32 seed =
Hash.sha256(
beaconStateAccessors.getSeed(state, UInt64.ONE, Domain.BEACON_PROPOSER),
uint64ToBytes(slot));

if (miscHelpersElectra.computeProposerIndex(state, validatorIndices, seed) == 3) {
idx3ProposalCount++;
}
}
assertThat(idx3ProposalCount).isEqualTo(4);
}

private BeaconState randomStateWithConsolidatedValidator(int consolidationAmount) {
final BeaconState preState = dataStructureUtil.randomBeaconState(8);
return BeaconStateElectra.required(preState)
.updated(
mutableState -> {
mutableState
.getValidators()
.set(
3,
dataStructureUtil
.validatorBuilder()
.withdrawalCredentials(
dataStructureUtil.randomCompoundingWithdrawalCredentials())
.effectiveBalance(UInt64.THIRTY_TWO_ETH.times(consolidationAmount))
.build());
mutableState
.getBalances()
.set(3, SszUInt64.of(UInt64.THIRTY_TWO_ETH.times(consolidationAmount)));
mutableState.setSlot(UInt64.valueOf(8));
});
}
}