forked from Consensys/teku
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
346 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...va/tech/pegasys/teku/spec/logic/versions/electra/helpers/BeaconStateAccessorsElectra.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* 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 tech.pegasys.teku.infrastructure.unsigned.UInt64; | ||
import tech.pegasys.teku.spec.config.SpecConfigDeneb; | ||
import tech.pegasys.teku.spec.datastructures.state.Validator; | ||
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 { | ||
|
||
private final UInt64 maxEffectiveBalanceElectra; | ||
private final UInt64 minActivationBalance; | ||
|
||
protected PredicatesElectra predicatesElectra; | ||
|
||
public BeaconStateAccessorsElectra( | ||
final SpecConfigDeneb config, | ||
final PredicatesElectra predicatesElectra, | ||
final MiscHelpersDeneb miscHelpers) { | ||
super(config, predicatesElectra, miscHelpers); | ||
this.maxEffectiveBalanceElectra = | ||
config.toVersionElectra().orElseThrow().getMaxEffectiveBalanceElectra(); | ||
this.minActivationBalance = config.toVersionElectra().orElseThrow().getMinActivationBalance(); | ||
this.predicatesElectra = predicatesElectra; | ||
} | ||
|
||
/** | ||
* 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.hasCompoundingWithdrawalCredential(validator) | ||
? maxEffectiveBalanceElectra | ||
: minActivationBalance; | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
...rc/main/java/tech/pegasys/teku/spec/logic/versions/electra/helpers/PredicatesElectra.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 tech.pegasys.teku.spec.constants.WithdrawalPrefixes.COMPOUNDING_WITHDRAWAL_BYTE; | ||
|
||
import org.apache.tuweni.bytes.Bytes32; | ||
import tech.pegasys.teku.infrastructure.unsigned.UInt64; | ||
import tech.pegasys.teku.spec.config.SpecConfig; | ||
import tech.pegasys.teku.spec.config.SpecConfigElectra; | ||
import tech.pegasys.teku.spec.datastructures.state.Validator; | ||
import tech.pegasys.teku.spec.logic.common.helpers.Predicates; | ||
|
||
public class PredicatesElectra extends Predicates { | ||
private final SpecConfigElectra specConfigElectra; | ||
|
||
public PredicatesElectra(SpecConfig specConfig) { | ||
super(specConfig); | ||
this.specConfigElectra = SpecConfigElectra.required(specConfig); | ||
} | ||
|
||
@Override | ||
public boolean isPartiallyWithdrawableValidator(final Validator validator, final UInt64 balance) { | ||
if (hasEth1WithdrawalCredential(validator)) { | ||
return isPartiallyWithdrawableValidatorEth1CredentialsChecked(validator, balance); | ||
} | ||
if (hasCompoundingWithdrawalCredential(validator)) { | ||
return isPartiallyWithdrawableValidatorCompoundingCredentialChecked(validator, balance); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private boolean isPartiallyWithdrawableValidatorCompoundingCredentialChecked( | ||
final Validator validator, final UInt64 balance) { | ||
final UInt64 maxEffectiveBalance = specConfigElectra.getMaxEffectiveBalanceElectra(); | ||
final boolean hasMaxEffectiveBalance = | ||
validator.getEffectiveBalance().equals(maxEffectiveBalance); | ||
final boolean hasExcessBalance = balance.isGreaterThan(maxEffectiveBalance); | ||
|
||
return hasMaxEffectiveBalance && hasExcessBalance; | ||
} | ||
|
||
protected boolean hasCompoundingWithdrawalCredential(final Validator validator) { | ||
return isCompoundingWithdrawalCredential(validator.getWithdrawalCredentials()); | ||
} | ||
|
||
protected boolean isCompoundingWithdrawalCredential(final Bytes32 withdrawalCredentials) { | ||
return withdrawalCredentials.get(0) == COMPOUNDING_WITHDRAWAL_BYTE; | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
...pegasys/teku/spec/logic/versions/electra/statetransition/epoch/EpochProcessorElectra.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* 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.statetransition.epoch; | ||
|
||
import tech.pegasys.teku.infrastructure.unsigned.UInt64; | ||
import tech.pegasys.teku.spec.config.SpecConfigBellatrix; | ||
import tech.pegasys.teku.spec.logic.common.helpers.BeaconStateMutators; | ||
import tech.pegasys.teku.spec.logic.common.statetransition.epoch.status.ValidatorStatus; | ||
import tech.pegasys.teku.spec.logic.common.statetransition.epoch.status.ValidatorStatusFactory; | ||
import tech.pegasys.teku.spec.logic.common.util.BeaconStateUtil; | ||
import tech.pegasys.teku.spec.logic.common.util.ValidatorsUtil; | ||
import tech.pegasys.teku.spec.logic.versions.altair.helpers.BeaconStateAccessorsAltair; | ||
import tech.pegasys.teku.spec.logic.versions.altair.helpers.MiscHelpersAltair; | ||
import tech.pegasys.teku.spec.logic.versions.bellatrix.statetransition.epoch.EpochProcessorBellatrix; | ||
import tech.pegasys.teku.spec.schemas.SchemaDefinitions; | ||
|
||
public class EpochProcessorElectra extends EpochProcessorBellatrix { | ||
|
||
private final UInt64 minActivationBalance; | ||
|
||
public EpochProcessorElectra( | ||
final SpecConfigBellatrix specConfig, | ||
final MiscHelpersAltair miscHelpers, | ||
final BeaconStateAccessorsAltair beaconStateAccessors, | ||
final BeaconStateMutators beaconStateMutators, | ||
final ValidatorsUtil validatorsUtil, | ||
final BeaconStateUtil beaconStateUtil, | ||
final ValidatorStatusFactory validatorStatusFactory, | ||
final SchemaDefinitions schemaDefinitions) { | ||
super( | ||
specConfig, | ||
miscHelpers, | ||
beaconStateAccessors, | ||
beaconStateMutators, | ||
validatorsUtil, | ||
beaconStateUtil, | ||
validatorStatusFactory, | ||
schemaDefinitions); | ||
this.minActivationBalance = | ||
specConfig.toVersionElectra().orElseThrow().getMinActivationBalance(); | ||
} | ||
|
||
@Override | ||
protected boolean isEligibleForActivationQueue(final ValidatorStatus status) { | ||
return !status.isActiveInCurrentEpoch() | ||
&& status.getCurrentEpochEffectiveBalance().isGreaterThanOrEqualTo(minActivationBalance); | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
...est/java/tech/pegasys/teku/spec/logic/versions/electra/helpers/PredicatesElectraTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* 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.assertj.core.api.Assertions.assertThat; | ||
|
||
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.datastructures.state.Validator; | ||
import tech.pegasys.teku.spec.util.DataStructureUtil; | ||
|
||
class PredicatesElectraTest { | ||
private final Spec spec = TestSpecFactory.createMinimalElectra(); | ||
private final DataStructureUtil dataStructureUtil = new DataStructureUtil(spec); | ||
private final PredicatesElectra predicates = new PredicatesElectra(spec.getGenesisSpecConfig()); | ||
|
||
private final UInt64 excessLargeValidatorBalance = UInt64.valueOf(2050_000_000_000L); | ||
|
||
private final UInt64 maxEffectiveBalanceNonCompounding = UInt64.THIRTY_TWO_ETH; | ||
|
||
private final UInt64 maxEffectiveBalanceCompounding = UInt64.valueOf(2048_000_000_000L); | ||
|
||
@Test | ||
void isPartiallyWithdrawableValidator_shouldNotDetermineBlsWithdrawalAsNotWithdrawable() { | ||
final Validator validator = | ||
dataStructureUtil | ||
.validatorBuilder() | ||
.withdrawalCredentials(dataStructureUtil.randomBlsWithdrawalCredentials()) | ||
.effectiveBalance(maxEffectiveBalanceNonCompounding) | ||
.build(); | ||
assertThat(predicates.isPartiallyWithdrawableValidator(validator, excessLargeValidatorBalance)) | ||
.isFalse(); | ||
} | ||
|
||
@Test | ||
void isPartiallyWithdrawableValidator_shouldDetermineEth1WithdrawalAsWithdrawable() { | ||
final Validator validator = | ||
dataStructureUtil | ||
.validatorBuilder() | ||
.withdrawalCredentials(dataStructureUtil.randomEth1WithdrawalCredentials()) | ||
.effectiveBalance(maxEffectiveBalanceNonCompounding) | ||
.build(); | ||
assertThat(predicates.isPartiallyWithdrawableValidator(validator, excessLargeValidatorBalance)) | ||
.isTrue(); | ||
} | ||
|
||
@Test | ||
void isPartiallyWithdrawableValidator_shouldDetermineCompoundingWithdrawalAsWithdrawable() { | ||
final Validator validator = | ||
dataStructureUtil | ||
.validatorBuilder() | ||
.withdrawalCredentials(dataStructureUtil.randomCompoundingWithdrawalCredentials()) | ||
.effectiveBalance(maxEffectiveBalanceCompounding) | ||
.build(); | ||
assertThat(predicates.isPartiallyWithdrawableValidator(validator, excessLargeValidatorBalance)) | ||
.isTrue(); | ||
} | ||
|
||
@Test | ||
void isPartiallyWithdrawableValidator_shouldDetermineCompoundingWithdrawalAsAsNotWithdrawable() { | ||
final Validator validator = | ||
dataStructureUtil | ||
.validatorBuilder() | ||
.withdrawalCredentials(dataStructureUtil.randomCompoundingWithdrawalCredentials()) | ||
.effectiveBalance(maxEffectiveBalanceNonCompounding) | ||
.build(); | ||
assertThat( | ||
predicates.isPartiallyWithdrawableValidator( | ||
validator, maxEffectiveBalanceNonCompounding)) | ||
.isFalse(); | ||
} | ||
} |
Oops, something went wrong.