Skip to content

Commit

Permalink
updated proposer logic for maxEB (#8343)
Browse files Browse the repository at this point in the history
* updated proposer logic for maxEB

Added test to show that the consolidation factor affects the proposals in an epoch.

Proposer index 3 went from 0 proposals in epoch 1 up to 4 proposals in epoch 1 after this change (consolidation of 16, so it has 16x32 eth)

Signed-off-by: Paul Harris <[email protected]>

* review feedback

Signed-off-by: Paul Harris <[email protected]>

* fixed conflicts

Signed-off-by: Paul Harris <[email protected]>

* undo electra milestone

Signed-off-by: Paul Harris <[email protected]>

---------

Signed-off-by: Paul Harris <[email protected]>
  • Loading branch information
rolfyone authored Jul 9, 2024
1 parent 8b8f1da commit d2547c5
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,12 @@ protected int computeProposerIndex(
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 validatorEffectiveBalance =
final int randomByte = UnsignedBytes.toInt(hash[i % 32]);
final UInt64 validatorEffectiveBalance =
state.getValidators().get(candidateIndex).getEffectiveBalance();
if (validatorEffectiveBalance
.times(MAX_RANDOM_BYTE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
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.BeaconStateTestBuilder;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.electra.BeaconStateElectra;
Expand All @@ -36,6 +41,7 @@
public class MiscHelpersElectraTest {

private final Spec spec = TestSpecFactory.createMinimalElectra();
private static final int PROPOSER_INDEX = 3;
private final Predicates predicates = new Predicates(spec.getGenesisSpecConfig());
private final SchemaDefinitionsElectra schemaDefinitionsElectra =
SchemaDefinitionsElectra.required(spec.getGenesisSchemaDefinitions());
Expand All @@ -45,6 +51,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 @@ -100,4 +113,46 @@ public void computeProposerIndexShouldUseMaxEffectiveBalanceElectra() {
verify(specConfigElectra).getMaxEffectiveBalanceElectra();
verify(specConfigElectra, never()).getMaxEffectiveBalance();
}

@Test
void consolidatedValidatorsMoreLikelyToPropose() {
final int consolidationAmount = 16;
final BeaconState state = randomStateWithConsolidatedValidator(consolidationAmount);
int proposerIndexCount = 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)
== PROPOSER_INDEX) {
proposerIndexCount++;
}
}
assertThat(proposerIndexCount).isEqualTo(4);
}

private BeaconState randomStateWithConsolidatedValidator(final int consolidationAmount) {
final BeaconState preState = dataStructureUtil.randomBeaconState(8);
return BeaconStateElectra.required(preState)
.updated(
mutableState -> {
mutableState
.getValidators()
.set(
PROPOSER_INDEX,
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));
});
}
}

0 comments on commit d2547c5

Please sign in to comment.