Skip to content

Commit

Permalink
Implemented getConsolidationChurnLimit and getActivationExitChurnLimit (
Browse files Browse the repository at this point in the history
Consensys#8203)


also added to the electra upgrade, and wrote a test to show the computations, but these will be tested further when we can run reference tests too.

We should be able to pass state upgrade from deneb now, but we'd need to confirm via reference test.

fixes Consensys#8147

Signed-off-by: Paul Harris <[email protected]>
  • Loading branch information
rolfyone authored Apr 15, 2024
1 parent 064ce08 commit 84b6ac6
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.BeaconStateSchemaAltair;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.bellatrix.BeaconStateSchemaBellatrix;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.capella.BeaconStateSchemaCapella;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.deneb.BeaconStateSchemaDeneb;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.phase0.BeaconStateSchemaPhase0;
import tech.pegasys.teku.spec.logic.common.forktransition.StateUpgrade;

Expand All @@ -50,6 +51,7 @@ private void processUpgrade(final TestDefinition testDefinition, final SpecMiles
case BELLATRIX -> BeaconStateSchemaAltair.create(spec.getConfig());
case CAPELLA -> BeaconStateSchemaBellatrix.create(spec.getConfig());
case DENEB -> BeaconStateSchemaCapella.create(spec.getConfig());
case ELECTRA -> BeaconStateSchemaDeneb.create(spec.getConfig());
default -> throw new IllegalStateException(
"Unhandled fork upgrade for test "
+ testDefinition.getDisplayName()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,33 @@
package tech.pegasys.teku.spec.logic.versions.electra.forktransition;

import org.apache.tuweni.bytes.Bytes32;
import tech.pegasys.teku.infrastructure.ssz.SszList;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.spec.config.SpecConfigElectra;
import tech.pegasys.teku.spec.datastructures.execution.ExecutionPayloadHeader;
import tech.pegasys.teku.spec.datastructures.execution.versions.deneb.ExecutionPayloadHeaderDeneb;
import tech.pegasys.teku.spec.datastructures.state.Fork;
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.common.BeaconStateFields;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.deneb.BeaconStateDeneb;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.electra.BeaconStateElectra;
import tech.pegasys.teku.spec.logic.common.forktransition.StateUpgrade;
import tech.pegasys.teku.spec.logic.versions.altair.helpers.BeaconStateAccessorsAltair;
import tech.pegasys.teku.spec.logic.versions.electra.helpers.BeaconStateAccessorsElectra;
import tech.pegasys.teku.spec.logic.versions.electra.helpers.MiscHelpersElectra;
import tech.pegasys.teku.spec.logic.versions.electra.helpers.PredicatesElectra;
import tech.pegasys.teku.spec.schemas.SchemaDefinitionsElectra;

public class ElectraStateUpgrade implements StateUpgrade<BeaconStateDeneb> {

private final SpecConfigElectra specConfig;
private final SchemaDefinitionsElectra schemaDefinitions;
private final BeaconStateAccessorsAltair beaconStateAccessors;
private final BeaconStateAccessorsElectra beaconStateAccessors;

public ElectraStateUpgrade(
final SpecConfigElectra specConfig,
final SchemaDefinitionsElectra schemaDefinitions,
final BeaconStateAccessorsAltair beaconStateAccessors) {
final BeaconStateAccessorsElectra beaconStateAccessors) {
this.specConfig = specConfig;
this.schemaDefinitions = schemaDefinitions;
this.beaconStateAccessors = beaconStateAccessors;
Expand All @@ -46,6 +50,8 @@ public ElectraStateUpgrade(
public BeaconStateElectra upgrade(final BeaconState preState) {
final UInt64 epoch = beaconStateAccessors.getCurrentEpoch(preState);
final BeaconStateDeneb preStateDeneb = BeaconStateDeneb.required(preState);
final MiscHelpersElectra miscHelpersElectra =
new MiscHelpersElectra(specConfig, new PredicatesElectra(specConfig), schemaDefinitions);
return schemaDefinitions
.getBeaconStateSchema()
.createEmpty()
Expand Down Expand Up @@ -101,6 +107,26 @@ public BeaconStateElectra upgrade(final BeaconState preState) {
state.setHistoricalSummaries(preStateDeneb.getHistoricalSummaries());
state.setDepositReceiptsStartIndex(
SpecConfigElectra.UNSET_DEPOSIT_RECEIPTS_START_INDEX);
state.setDepositBalanceToConsume(UInt64.ZERO);
state.setExitBalanceToConsume(
beaconStateAccessors.getActivationExitChurnLimit(state));
state.setEarliestExitEpoch(findEarliestExitEpoch(state));
state.setConsolidationBalanceToConsume(
beaconStateAccessors.getConsolidationChurnLimit(state));
state.setEarliestConsolidationEpoch(
miscHelpersElectra.computeActivationExitEpoch(epoch));
});
}

private UInt64 findEarliestExitEpoch(final BeaconState state) {
final SszList<Validator> validators = state.getValidators();
UInt64 lastExitEpoch = UInt64.ZERO;
for (int i = 0; i < validators.size(); i++) {
final UInt64 exitEpoch = validators.get(i).getExitEpoch();
if (exitEpoch.isLessThan(UInt64.MAX_VALUE)) {
lastExitEpoch = lastExitEpoch.max(exitEpoch);
}
}
return lastExitEpoch.increment();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,25 @@
package tech.pegasys.teku.spec.logic.versions.electra.helpers;

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.Validator;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.electra.BeaconStateElectra;
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;

private final SpecConfigElectra configElectra;
protected PredicatesElectra predicatesElectra;

public BeaconStateAccessorsElectra(
final SpecConfigDeneb config,
final SpecConfig config,
final PredicatesElectra predicatesElectra,
final MiscHelpersDeneb miscHelpers) {
super(config, predicatesElectra, miscHelpers);
this.maxEffectiveBalanceElectra =
config.toVersionElectra().orElseThrow().getMaxEffectiveBalanceElectra();
this.minActivationBalance = config.toVersionElectra().orElseThrow().getMinActivationBalance();
super(SpecConfigDeneb.required(config), predicatesElectra, miscHelpers);
configElectra = config.toVersionElectra().orElseThrow();
this.predicatesElectra = predicatesElectra;
}

Expand All @@ -46,7 +45,41 @@ public BeaconStateAccessorsElectra(
*/
public UInt64 getValidatorMaxEffectiveBalance(final Validator validator) {
return predicatesElectra.hasCompoundingWithdrawalCredential(validator)
? maxEffectiveBalanceElectra
: minActivationBalance;
? configElectra.getMaxEffectiveBalanceElectra()
: configElectra.getMinActivationBalance();
}

/**
* get_activation_exit_churn_limit
*
* @param state - the state to use to get the churn limit from
* @return Return the churn limit for the current epoch dedicated to activations and exits.
*/
public UInt64 getActivationExitChurnLimit(final BeaconStateElectra state) {
return getChurnLimit(state).min(configElectra.getMaxPerEpochActivationExitChurnLimit());
}

/**
* get_churn_limit
*
* @param state the state to read active balance from
* @return Return the churn limit for the current epoch.
*/
public UInt64 getChurnLimit(final BeaconStateElectra state) {
final UInt64 churn =
configElectra
.getMinPerEpochChurnLimitElectra()
.max(getTotalActiveBalance(state).dividedBy(configElectra.getChurnLimitQuotient()));
return churn.minusMinZero(churn.mod(configElectra.getEffectiveBalanceIncrement()));
}

/**
* get_consolidation_churn_limit
*
* @param state state to read churn limits from
* @return
*/
public UInt64 getConsolidationChurnLimit(final BeaconStateElectra state) {
return getChurnLimit(state).minusMinZero(getActivationExitChurnLimit(state));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,25 @@

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

import tech.pegasys.teku.spec.config.SpecConfigElectra;
import tech.pegasys.teku.spec.config.SpecConfig;
import tech.pegasys.teku.spec.config.SpecConfigDeneb;
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.Predicates;
import tech.pegasys.teku.spec.logic.versions.deneb.helpers.MiscHelpersDeneb;
import tech.pegasys.teku.spec.schemas.SchemaDefinitionsElectra;
import tech.pegasys.teku.spec.schemas.SchemaDefinitions;
import tech.pegasys.teku.spec.schemas.SchemaDefinitionsDeneb;

public class MiscHelpersElectra extends MiscHelpersDeneb {

public MiscHelpersElectra(
final SpecConfigElectra specConfig,
final SpecConfig specConfig,
final Predicates predicates,
final SchemaDefinitionsElectra schemaDefinitions) {
super(specConfig, predicates, schemaDefinitions);
final SchemaDefinitions schemaDefinitions) {
super(
SpecConfigDeneb.required(specConfig),
predicates,
SchemaDefinitionsDeneb.required(schemaDefinitions));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* 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.forktransition;

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.SpecMilestone;
import tech.pegasys.teku.spec.TestSpecFactory;
import tech.pegasys.teku.spec.config.SpecConfigElectra;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.deneb.BeaconStateDeneb;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.electra.BeaconStateElectra;
import tech.pegasys.teku.spec.logic.versions.electra.helpers.BeaconStateAccessorsElectra;
import tech.pegasys.teku.spec.logic.versions.electra.helpers.MiscHelpersElectra;
import tech.pegasys.teku.spec.logic.versions.electra.helpers.PredicatesElectra;
import tech.pegasys.teku.spec.schemas.SchemaDefinitionsElectra;
import tech.pegasys.teku.spec.util.DataStructureUtil;

class ElectraStateUpgradeTest {

private final Spec spec = TestSpecFactory.createMinimalElectra();
private final PredicatesElectra predicatesElectra =
new PredicatesElectra(spec.getGenesisSpecConfig());
private final MiscHelpersElectra miscHelpersElectra =
new MiscHelpersElectra(
spec.getGenesisSpecConfig(), predicatesElectra, spec.getGenesisSchemaDefinitions());
final BeaconStateAccessorsElectra stateAccessorsElectra =
new BeaconStateAccessorsElectra(
spec.getGenesisSpecConfig(), predicatesElectra, miscHelpersElectra);
private final DataStructureUtil dataStructureUtil = new DataStructureUtil(spec);

@Test
void canUpgradeFromDeneb() {
final BeaconStateDeneb pre =
BeaconStateDeneb.required(
dataStructureUtil
.stateBuilder(SpecMilestone.DENEB, 1, 0)
.slot(UInt64.valueOf(80_000L))
.build());
final ElectraStateUpgrade upgrade =
new ElectraStateUpgrade(
SpecConfigElectra.required(spec.getGenesisSpecConfig()),
SchemaDefinitionsElectra.required(spec.getGenesisSchemaDefinitions()),
stateAccessorsElectra);

final BeaconStateElectra post = upgrade.upgrade(pre);

assertThat(post.getDepositBalanceToConsume()).isEqualTo(UInt64.ZERO);
// min churn - churn % balance_increment
// = (64 *10^9) - (64 *10^9) MOD 10^9
// = (64 *10^9) - 0
assertThat(post.getExitBalanceToConsume()).isEqualTo(UInt64.valueOf(64_000_000_000L));
assertThat(post.getEarliestExitEpoch()).isEqualTo(UInt64.ONE);
assertThat(post.getConsolidationBalanceToConsume()).isEqualTo(UInt64.ZERO);
// 80_000/8 (slots -> epochs) + max_seed_lookahead + 1
assertThat(post.getEarliestConsolidationEpoch()).isEqualTo(UInt64.valueOf(10005));
assertThat(post.getPendingBalanceDeposits()).isEmpty();
assertThat(post.getPendingConsolidations()).isEmpty();
assertThat(post.getPendingPartialWithdrawals()).isEmpty();
}
}

0 comments on commit 84b6ac6

Please sign in to comment.