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

Move getMaxEffectiveBalance to MiscHelpers #8714

Merged
merged 5 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ private static List<Withdrawal> getExpectedWithdrawals(
withdrawalIndex,
UInt64.valueOf(validatorIndex),
new Bytes20(validator.getWithdrawalCredentials().slice(12)),
balance.minusMinZero(predicates.getValidatorMaxEffectiveBalance(validator))));
balance.minusMinZero(miscHelpers.getMaxEffectiveBalance(validator))));
withdrawalIndex = withdrawalIndex.increment();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import tech.pegasys.teku.spec.datastructures.blocks.BeaconBlock;
import tech.pegasys.teku.spec.datastructures.state.ForkData;
import tech.pegasys.teku.spec.datastructures.state.SigningData;
import tech.pegasys.teku.spec.datastructures.state.Validator;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconStateCache;
import tech.pegasys.teku.spec.logic.versions.deneb.helpers.MiscHelpersDeneb;
Expand Down Expand Up @@ -386,6 +387,10 @@ public UInt64 getMaxRequestBlocks() {
return UInt64.valueOf(specConfig.getNetworkingConfig().getMaxRequestBlocks());
}

public UInt64 getMaxEffectiveBalance(final Validator validator) {
return specConfig.getMaxEffectiveBalance();
}

public boolean isFormerDepositMechanismDisabled(final BeaconState state) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,18 +160,14 @@ public boolean isPartiallyWithdrawableValidator(final Validator validator, final

public boolean isPartiallyWithdrawableValidatorEth1CredentialsChecked(
final Validator validator, final UInt64 balance) {
final UInt64 maxEffectiveBalance = getValidatorMaxEffectiveBalance(validator);
final UInt64 maxEffectiveBalance = specConfig.getMaxEffectiveBalance();
final boolean hasMaxEffectiveBalance =
validator.getEffectiveBalance().equals(maxEffectiveBalance);
final boolean hasExcessBalance = balance.isGreaterThan(maxEffectiveBalance);

return hasMaxEffectiveBalance && hasExcessBalance;
}

public UInt64 getValidatorMaxEffectiveBalance(final Validator validator) {
return specConfig.getMaxEffectiveBalance();
}

public Optional<PredicatesElectra> toVersionElectra() {
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -725,8 +725,7 @@ protected Validator getValidatorFromDeposit(
FAR_FUTURE_EPOCH,
FAR_FUTURE_EPOCH);

final UInt64 maxEffectiveBalance =
beaconStateAccessorsElectra.getValidatorMaxEffectiveBalance(validator);
final UInt64 maxEffectiveBalance = miscHelpers.getMaxEffectiveBalance(validator);
final UInt64 validatorEffectiveBalance =
amount
.minusMinZero(amount.mod(specConfig.getEffectiveBalanceIncrement()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public UInt64 getActivationExitChurnLimit(final BeaconStateElectra state) {
*/
public UInt64 getActiveBalance(final BeaconState state, final int validatorIndex) {
final Validator validator = state.getValidators().get(validatorIndex);
final UInt64 maxEffectiveBalance = predicatesElectra.getValidatorMaxEffectiveBalance(validator);
final UInt64 maxEffectiveBalance = miscHelpers.getMaxEffectiveBalance(validator);
final UInt64 validatorBalance = state.getBalances().get(validatorIndex).get();
return validatorBalance.min(maxEffectiveBalance);
}
Expand Down Expand Up @@ -115,17 +115,6 @@ public static BeaconStateAccessorsElectra required(
return (BeaconStateAccessorsElectra) beaconStateAccessors;
}

/**
* implements get_validator_max_effective_balance state accessor
*
* @param validator - a validator from a state.
* @return the max effective balance for the specified validator based on its withdrawal
* credentials.
*/
public UInt64 getValidatorMaxEffectiveBalance(final Validator validator) {
return predicatesElectra.getValidatorMaxEffectiveBalance(validator);
}

@Override
public IntList getNextSyncCommitteeIndices(final BeaconState state) {
return getNextSyncCommitteeIndices(state, configElectra.getMaxEffectiveBalanceElectra());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
import it.unimi.dsi.fastutil.ints.IntList;
import java.util.Optional;
import org.apache.tuweni.bytes.Bytes32;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.spec.config.SpecConfigDeneb;
import tech.pegasys.teku.spec.config.SpecConfigElectra;
import tech.pegasys.teku.spec.datastructures.state.Validator;
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,6 +29,8 @@
import tech.pegasys.teku.spec.schemas.SchemaDefinitionsDeneb;

public class MiscHelpersElectra extends MiscHelpersDeneb {
private final SpecConfigElectra specConfigElectra;
private final PredicatesElectra predicatesElectra;

public MiscHelpersElectra(
final SpecConfigElectra specConfig,
Expand All @@ -36,6 +40,8 @@ public MiscHelpersElectra(
SpecConfigDeneb.required(specConfig),
predicates,
SchemaDefinitionsDeneb.required(schemaDefinitions));
this.specConfigElectra = SpecConfigElectra.required(specConfig);
this.predicatesElectra = PredicatesElectra.required(predicates);
}

public static MiscHelpersElectra required(final MiscHelpers miscHelpers) {
Expand All @@ -58,6 +64,13 @@ public int computeProposerIndex(
SpecConfigElectra.required(specConfig).getMaxEffectiveBalanceElectra());
}

@Override
public UInt64 getMaxEffectiveBalance(final Validator validator) {
return predicatesElectra.hasCompoundingWithdrawalCredential(validator)
? specConfigElectra.getMaxEffectiveBalanceElectra()
: specConfigElectra.getMinActivationBalance();
}

@Override
public Optional<MiscHelpersElectra> toVersionElectra() {
return Optional.of(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,22 @@ public Optional<PredicatesElectra> toVersionElectra() {
*/
@Override
public boolean isPartiallyWithdrawableValidator(final Validator validator, final UInt64 balance) {
if (hasExecutionWithdrawalCredential(validator)) {
final UInt64 maxEffectiveBalance = getValidatorMaxEffectiveBalance(validator);
return (balance.isGreaterThan(maxEffectiveBalance)
&& maxEffectiveBalance.equals(validator.getEffectiveBalance()));
}
return false;
return hasExecutionWithdrawalCredential(validator)
&& isPartiallyWithdrawableValidatorEth1CredentialsChecked(validator, balance);
}

@Override
public boolean isPartiallyWithdrawableValidatorEth1CredentialsChecked(
final Validator validator, final UInt64 balance) {
final UInt64 maxEffectiveBalance =
hasCompoundingWithdrawalCredential(validator)
? configElectra.getMaxEffectiveBalanceElectra()
: configElectra.getMinActivationBalance();
final boolean hasMaxEffectiveBalance =
validator.getEffectiveBalance().equals(maxEffectiveBalance);
final boolean hasExcessBalance = balance.isGreaterThan(maxEffectiveBalance);

return hasMaxEffectiveBalance && hasExcessBalance;
}

/**
Expand Down Expand Up @@ -108,18 +118,4 @@ public boolean hasCompoundingWithdrawalCredential(final Validator validator) {
public boolean isCompoundingWithdrawalCredential(final Bytes32 withdrawalCredentials) {
return withdrawalCredentials.get(0) == COMPOUNDING_WITHDRAWAL_BYTE;
}

/**
* implements get_validator_max_effective_balance state accessor
*
* @param validator - a validator from a state.
* @return the max effective balance for the specified validator based on its withdrawal
* credentials.
*/
@Override
public UInt64 getValidatorMaxEffectiveBalance(final Validator validator) {
return hasCompoundingWithdrawalCredential(validator)
? configElectra.getMaxEffectiveBalanceElectra()
: configElectra.getMinActivationBalance();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ protected boolean isEligibleForActivationQueue(final ValidatorStatus status) {

@Override
protected UInt64 getEffectiveBalanceLimitForValidator(final Validator validator) {
return stateAccessorsElectra.getValidatorMaxEffectiveBalance(validator);
return miscHelpers.getMaxEffectiveBalance(validator);
}

// process_effective_balance_updates
Expand Down Expand Up @@ -266,8 +266,7 @@ private Validator getValidatorFromDeposit(
FAR_FUTURE_EPOCH,
FAR_FUTURE_EPOCH);

final UInt64 maxEffectiveBalance =
stateAccessorsElectra.getValidatorMaxEffectiveBalance(validator);
final UInt64 maxEffectiveBalance = miscHelpers.getMaxEffectiveBalance(validator);
final UInt64 validatorEffectiveBalance =
amount
.minusMinZero(amount.mod(specConfig.getEffectiveBalanceIncrement()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,26 @@
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;
import tech.pegasys.teku.spec.logic.common.helpers.Predicates;
import tech.pegasys.teku.spec.schemas.SchemaDefinitionsElectra;
import tech.pegasys.teku.spec.util.DataStructureUtil;

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 PredicatesElectra predicatesElectra =
new PredicatesElectra(spec.getGenesisSpecConfig());
private final SchemaDefinitionsElectra schemaDefinitionsElectra =
SchemaDefinitionsElectra.required(spec.getGenesisSchemaDefinitions());
private final MiscHelpersElectra miscHelpersElectra =
new MiscHelpersElectra(
spec.getGenesisSpecConfig().toVersionElectra().orElseThrow(),
predicates,
predicatesElectra,
schemaDefinitionsElectra);
private final DataStructureUtil dataStructureUtil = new DataStructureUtil(spec);
final BeaconStateAccessorsElectra beaconStateAccessors =
new BeaconStateAccessorsElectra(
spec.getGenesisSpecConfig(),
new PredicatesElectra(spec.getGenesisSpecConfig()),
miscHelpersElectra);
spec.getGenesisSpecConfig(), predicatesElectra, miscHelpersElectra);

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

Expand Down Expand Up @@ -98,7 +96,7 @@ public void computeProposerIndexShouldUseMaxEffectiveBalanceElectra() {
final SpecConfigElectra specConfigElectra =
spy(SpecConfigElectra.required(spec.getGenesisSpecConfig()));
final MiscHelpersElectra miscHelpersElectra =
new MiscHelpersElectra(specConfigElectra, predicates, schemaDefinitionsElectra);
new MiscHelpersElectra(specConfigElectra, predicatesElectra, schemaDefinitionsElectra);

final BeaconState state =
new BeaconStateTestBuilder(dataStructureUtil)
Expand Down