Skip to content

Commit

Permalink
Using MAX_EFFECTIVE_BALANCE_ELECTRA (#8406)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucassaldanha authored Jun 26, 2024
1 parent e0160c7 commit e85a853
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ public int computeShuffledIndex(final int index, final int indexCount, final Byt

public int computeProposerIndex(
final BeaconState state, final IntList indices, final Bytes32 seed) {
return computeProposerIndex(state, indices, seed, specConfig.getMaxEffectiveBalance());
}

protected int computeProposerIndex(
final BeaconState state,
final IntList indices,
final Bytes32 seed,
final UInt64 maxEffectiveBalance) {
checkArgument(!indices.isEmpty(), "compute_proposer_index indices must not be empty");

final Sha256 sha256 = getSha256Instance();
Expand All @@ -110,10 +118,11 @@ public int computeProposerIndex(
hash = sha256.digest(seed, uint64ToBytes(Math.floorDiv(i, 32L)));
}
int randomByte = UnsignedBytes.toInt(hash[i % 32]);
UInt64 effectiveBalance = state.getValidators().get(candidateIndex).getEffectiveBalance();
if (effectiveBalance
UInt64 validatorEffectiveBalance =
state.getValidators().get(candidateIndex).getEffectiveBalance();
if (validatorEffectiveBalance
.times(MAX_RANDOM_BYTE)
.isGreaterThanOrEqualTo(specConfig.getMaxEffectiveBalance().times(randomByte))) {
.isGreaterThanOrEqualTo(maxEffectiveBalance.times(randomByte))) {
return candidateIndex;
}
i++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ public UInt64 getBaseReward(final BeaconState state, final int validatorIndex) {
* @return the sequence of sync committee indices
*/
public IntList getNextSyncCommitteeIndices(final BeaconState state) {
return getNextSyncCommitteeIndices(state, config.getMaxEffectiveBalance());
}

protected IntList getNextSyncCommitteeIndices(
final BeaconState state, final UInt64 maxEffectiveBalance) {
final UInt64 epoch = getCurrentEpoch(state).plus(1);
final IntList activeValidatorIndices = getActiveValidatorIndices(state, epoch);
final int activeValidatorCount = activeValidatorIndices.size();
Expand All @@ -107,7 +112,6 @@ public IntList getNextSyncCommitteeIndices(final BeaconState state) {
int i = 0;
final SszList<Validator> validators = state.getValidators();
final IntList syncCommitteeIndices = new IntArrayList();
final UInt64 maxEffectiveBalance = config.getMaxEffectiveBalance();
final int syncCommitteeSize = altairConfig.getSyncCommitteeSize();

final Sha256 sha256 = getSha256Instance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import static com.google.common.base.Preconditions.checkArgument;

import it.unimi.dsi.fastutil.ints.IntList;
import java.util.List;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.spec.config.SpecConfig;
Expand All @@ -26,7 +27,6 @@
import tech.pegasys.teku.spec.datastructures.state.versions.electra.PendingPartialWithdrawal;
import tech.pegasys.teku.spec.logic.common.helpers.BeaconStateAccessors;
import tech.pegasys.teku.spec.logic.versions.deneb.helpers.BeaconStateAccessorsDeneb;
import tech.pegasys.teku.spec.logic.versions.deneb.helpers.MiscHelpersDeneb;

public class BeaconStateAccessorsElectra extends BeaconStateAccessorsDeneb {

Expand All @@ -36,7 +36,7 @@ public class BeaconStateAccessorsElectra extends BeaconStateAccessorsDeneb {
public BeaconStateAccessorsElectra(
final SpecConfig config,
final PredicatesElectra predicatesElectra,
final MiscHelpersDeneb miscHelpers) {
final MiscHelpersElectra miscHelpers) {
super(SpecConfigDeneb.required(config), predicatesElectra, miscHelpers);
configElectra = config.toVersionElectra().orElseThrow();
this.predicatesElectra = predicatesElectra;
Expand All @@ -57,7 +57,6 @@ public UInt64 getActivationExitChurnLimit(final BeaconStateElectra state) {
*
* @param state The state to get the effective balance from
* @param validatorIndex the index of the validator
* @return
*/
public UInt64 getActiveBalance(final BeaconState state, final int validatorIndex) {
final Validator validator = state.getValidators().get(validatorIndex);
Expand Down Expand Up @@ -101,7 +100,6 @@ public UInt64 getBalanceChurnLimit(final BeaconStateElectra state) {
* get_consolidation_churn_limit
*
* @param state state to read churn limits from
* @return
*/
public UInt64 getConsolidationChurnLimit(final BeaconStateElectra state) {
return getBalanceChurnLimit(state).minusMinZero(getActivationExitChurnLimit(state));
Expand All @@ -127,4 +125,9 @@ public static BeaconStateAccessorsElectra required(
public UInt64 getValidatorMaxEffectiveBalance(final Validator validator) {
return predicatesElectra.getValidatorMaxEffectiveBalance(validator);
}

@Override
public IntList getNextSyncCommitteeIndices(final BeaconState state) {
return getNextSyncCommitteeIndices(state, configElectra.getMaxEffectiveBalanceElectra());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@

package tech.pegasys.teku.spec.logic.versions.electra.helpers;

import it.unimi.dsi.fastutil.ints.IntList;
import java.util.Optional;
import tech.pegasys.teku.spec.config.SpecConfig;
import org.apache.tuweni.bytes.Bytes32;
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 All @@ -27,7 +29,7 @@
public class MiscHelpersElectra extends MiscHelpersDeneb {

public MiscHelpersElectra(
final SpecConfig specConfig,
final SpecConfigElectra specConfig,
final Predicates predicates,
final SchemaDefinitions schemaDefinitions) {
super(
Expand All @@ -46,6 +48,16 @@ public static MiscHelpersElectra required(final MiscHelpers miscHelpers) {
+ miscHelpers.getClass().getSimpleName()));
}

@Override
public int computeProposerIndex(
final BeaconState state, final IntList indices, final Bytes32 seed) {
return computeProposerIndex(
state,
indices,
seed,
SpecConfigElectra.required(specConfig).getMaxEffectiveBalanceElectra());
}

@Override
public Optional<MiscHelpersElectra> toVersionElectra() {
return Optional.of(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ class ElectraStateUpgradeTest {
SchemaDefinitionsElectra.required(spec.getGenesisSchemaDefinitions());
private final MiscHelpersElectra miscHelpersElectra =
new MiscHelpersElectra(
spec.getGenesisSpecConfig(), predicatesElectra, spec.getGenesisSchemaDefinitions());
spec.getGenesisSpecConfig().toVersionElectra().orElseThrow(),
predicatesElectra,
spec.getGenesisSchemaDefinitions());
final BeaconStateAccessorsElectra stateAccessorsElectra =
new BeaconStateAccessorsElectra(
spec.getGenesisSpecConfig(), predicatesElectra, miscHelpersElectra);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright Consensys Software Inc., 2024
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package tech.pegasys.teku.spec.logic.versions.electra.helpers;

import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;

import org.junit.jupiter.api.Test;
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.datastructures.state.BeaconStateTestBuilder;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState;
import tech.pegasys.teku.spec.schemas.SchemaDefinitionsElectra;
import tech.pegasys.teku.spec.util.DataStructureUtil;

class BeaconStateAccessorsElectraTest {

private final Spec spec = TestSpecFactory.createMinimalElectra();
private final DataStructureUtil dataStructureUtil = new DataStructureUtil(spec);
private final SpecConfigElectra specConfig =
spy(SpecConfigElectra.required(spec.atSlot(UInt64.ZERO).getConfig()));
private final PredicatesElectra predicatesElectra = new PredicatesElectra(specConfig);
private final SchemaDefinitionsElectra schemaDefinitionsElectra =
new SchemaDefinitionsElectra(SpecConfigElectra.required(specConfig));
private final MiscHelpersElectra miscHelpersElectra =
new MiscHelpersElectra(
SpecConfigElectra.required(specConfig), predicatesElectra, schemaDefinitionsElectra);

@Test
public void getNextSyncCommitteeIndicesShouldUseMaxEffectiveBalanceElectra() {
final BeaconStateAccessorsElectra beaconStateAccessorsElectra =
new BeaconStateAccessorsElectra(specConfig, predicatesElectra, miscHelpersElectra);

beaconStateAccessorsElectra.getNextSyncCommitteeIndices(createBeaconState());

verify(specConfig).getMaxEffectiveBalanceElectra();
verify(specConfig, never()).getMaxEffectiveBalance();
}

private BeaconState createBeaconState() {
return new BeaconStateTestBuilder(dataStructureUtil)
.forkVersion(specConfig.getGenesisForkVersion())
.activeValidator(UInt64.THIRTY_TWO_ETH)
.activeValidator(UInt64.THIRTY_TWO_ETH)
.activeValidator(UInt64.THIRTY_TWO_ETH)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,18 @@
package tech.pegasys.teku.spec.logic.versions.electra.helpers;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;

import it.unimi.dsi.fastutil.ints.IntList;
import org.apache.tuweni.bytes.Bytes32;
import org.junit.jupiter.api.Test;
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.datastructures.state.BeaconStateTestBuilder;
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 Down Expand Up @@ -73,4 +79,25 @@ public void isFormerDepositMechanismDisabled_returnsFalseIfNotDisabled() {

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

@Test
public void computeProposerIndexShouldUseMaxEffectiveBalanceElectra() {
final SpecConfigElectra specConfigElectra =
spy(SpecConfigElectra.required(spec.getGenesisSpecConfig()));
final MiscHelpersElectra miscHelpersElectra =
new MiscHelpersElectra(specConfigElectra, predicates, schemaDefinitionsElectra);

final BeaconState state =
new BeaconStateTestBuilder(dataStructureUtil)
.forkVersion(spec.getGenesisSpecConfig().getGenesisForkVersion())
.activeValidator(UInt64.THIRTY_TWO_ETH)
.activeValidator(UInt64.THIRTY_TWO_ETH)
.activeValidator(UInt64.THIRTY_TWO_ETH)
.build();

miscHelpersElectra.computeProposerIndex(state, IntList.of(0, 1, 2), Bytes32.ZERO);

verify(specConfigElectra).getMaxEffectiveBalanceElectra();
verify(specConfigElectra, never()).getMaxEffectiveBalance();
}
}

0 comments on commit e85a853

Please sign in to comment.