diff --git a/eth-tests/src/main/java/tech/pegasys/teku/ethtests/finder/ReferenceTestFinder.java b/eth-tests/src/main/java/tech/pegasys/teku/ethtests/finder/ReferenceTestFinder.java index d58be7186fa..6d2ef5dc31a 100644 --- a/eth-tests/src/main/java/tech/pegasys/teku/ethtests/finder/ReferenceTestFinder.java +++ b/eth-tests/src/main/java/tech/pegasys/teku/ethtests/finder/ReferenceTestFinder.java @@ -31,12 +31,7 @@ public class ReferenceTestFinder { Path.of("src", "referenceTest", "resources", "consensus-spec-tests", "tests"); private static final List SUPPORTED_FORKS = List.of( - TestFork.PHASE0, - TestFork.ALTAIR, - TestFork.BELLATRIX, - TestFork.CAPELLA, - TestFork.DENEB, - TestFork.ELECTRA); + TestFork.PHASE0, TestFork.ALTAIR, TestFork.BELLATRIX, TestFork.CAPELLA, TestFork.DENEB); @MustBeClosed public static Stream findReferenceTests() throws IOException { diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/constants/WithdrawalPrefixes.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/constants/WithdrawalPrefixes.java index d2bd4b0cedd..5919e41cd8e 100644 --- a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/constants/WithdrawalPrefixes.java +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/constants/WithdrawalPrefixes.java @@ -20,5 +20,4 @@ public class WithdrawalPrefixes { public static final byte ETH1_ADDRESS_WITHDRAWAL_BYTE = 0x01; public static final byte COMPOUNDING_WITHDRAWAL_BYTE = 0x02; public static final Bytes ETH1_ADDRESS_WITHDRAWAL_PREFIX = Bytes.of(ETH1_ADDRESS_WITHDRAWAL_BYTE); - public static final Bytes COMPOUNDING_WITHDRAWAL_PREFIX = Bytes.of(COMPOUNDING_WITHDRAWAL_BYTE); } diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/logic/common/helpers/BeaconStateMutators.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/logic/common/helpers/BeaconStateMutators.java index 628e5ff437d..28b04635336 100644 --- a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/logic/common/helpers/BeaconStateMutators.java +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/logic/common/helpers/BeaconStateMutators.java @@ -148,22 +148,6 @@ public Supplier createValidatorExitContextSupplier( return Suppliers.memoize(() -> createValidatorExitContext(state)); } - /** - * add_validator_to_registry - */ - public void addValidatorToRegistry( - final MutableBeaconState state, - final BLSPublicKey pubkey, - final Bytes32 withdrawalCredentials, - final UInt64 amount) { - final Validator validator = - miscHelpers.getValidatorFromDeposit(pubkey, withdrawalCredentials, amount); - LOG.debug("Adding new validator with index {} to state", state.getValidators().size()); - state.getValidators().append(validator); - state.getBalances().appendElement(amount); - } - /** * This function implements an optimized version of exitQueueEpoch and exitQueueChurn calculation, * compared to the `initiate_validator_exit` reference implementation. @@ -239,6 +223,22 @@ public void setExitQueueChurn(final UInt64 exitQueueChurn) { } } + /** + * add_validator_to_registry + */ + public void addValidatorToRegistry( + final MutableBeaconState state, + final BLSPublicKey pubkey, + final Bytes32 withdrawalCredentials, + final UInt64 amount) { + final Validator validator = + miscHelpers.getValidatorFromDeposit(pubkey, withdrawalCredentials, amount); + LOG.debug("Adding new validator with index {} to state", state.getValidators().size()); + state.getValidators().append(validator); + state.getBalances().appendElement(amount); + } + public void slashValidator( final MutableBeaconState state, final int slashedIndex, diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/logic/common/helpers/MiscHelpers.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/logic/common/helpers/MiscHelpers.java index 559f3a01c87..d7e3aeb0f76 100644 --- a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/logic/common/helpers/MiscHelpers.java +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/logic/common/helpers/MiscHelpers.java @@ -364,6 +364,38 @@ private Bytes32 computeForkDataRoot( return new ForkData(currentVersion, genesisValidatorsRoot).hashTreeRoot(); } + /** is_valid_deposit_signature */ + public boolean isValidDepositSignature( + final BLSPublicKey pubkey, + final Bytes32 withdrawalCredentials, + final UInt64 amount, + final BLSSignature signature) { + try { + return depositSignatureVerifier.verify( + pubkey, computeDepositSigningRoot(pubkey, withdrawalCredentials, amount), signature); + } catch (final BlsException e) { + return false; + } + } + + /** get_validator_from_deposit */ + public Validator getValidatorFromDeposit( + final BLSPublicKey pubkey, final Bytes32 withdrawalCredentials, final UInt64 amount) { + final UInt64 effectiveBalance = + amount + .minus(amount.mod(specConfig.getEffectiveBalanceIncrement())) + .min(specConfig.getMaxEffectiveBalance()); + return new Validator( + pubkey, + withdrawalCredentials, + effectiveBalance, + false, + FAR_FUTURE_EPOCH, + FAR_FUTURE_EPOCH, + FAR_FUTURE_EPOCH, + FAR_FUTURE_EPOCH); + } + public boolean isMergeTransitionComplete(final BeaconState state) { return false; } @@ -414,38 +446,6 @@ public boolean isFormerDepositMechanismDisabled(final BeaconState state) { return false; } - /** is_valid_deposit_signature */ - public boolean isValidDepositSignature( - final BLSPublicKey pubkey, - final Bytes32 withdrawalCredentials, - final UInt64 amount, - final BLSSignature signature) { - try { - return depositSignatureVerifier.verify( - pubkey, computeDepositSigningRoot(pubkey, withdrawalCredentials, amount), signature); - } catch (final BlsException e) { - return false; - } - } - - /** get_validator_from_deposit */ - public Validator getValidatorFromDeposit( - final BLSPublicKey pubkey, final Bytes32 withdrawalCredentials, final UInt64 amount) { - final UInt64 effectiveBalance = - amount - .minus(amount.mod(specConfig.getEffectiveBalanceIncrement())) - .min(specConfig.getMaxEffectiveBalance()); - return new Validator( - pubkey, - withdrawalCredentials, - effectiveBalance, - false, - FAR_FUTURE_EPOCH, - FAR_FUTURE_EPOCH, - FAR_FUTURE_EPOCH, - FAR_FUTURE_EPOCH); - } - public Optional toVersionDeneb() { return Optional.empty(); } diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/logic/versions/electra/block/BlockProcessorElectra.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/logic/versions/electra/block/BlockProcessorElectra.java index 8693f37efc0..375e2b6dd0d 100644 --- a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/logic/versions/electra/block/BlockProcessorElectra.java +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/logic/versions/electra/block/BlockProcessorElectra.java @@ -164,10 +164,10 @@ protected void processOperationsNoValidation( final ExecutionRequests executionRequests = BeaconBlockBodyElectra.required(body).getExecutionRequests(); - this.processDepositRequests(state, executionRequests.getDeposits()); - this.processWithdrawalRequests( + processDepositRequests(state, executionRequests.getDeposits()); + processWithdrawalRequests( state, executionRequests.getWithdrawals(), validatorExitContextSupplier); - this.processConsolidationRequests(state, executionRequests.getConsolidations()); + processConsolidationRequests(state, executionRequests.getConsolidations()); }); } @@ -210,6 +210,36 @@ public void processWithdrawals( specConfigElectra); } + /* + Implements process_deposit_request from consensus-specs (EIP-6110) + */ + @Override + public void processDepositRequests( + final MutableBeaconState state, final List depositRequests) { + final MutableBeaconStateElectra electraState = MutableBeaconStateElectra.required(state); + final SszMutableList pendingDeposits = + MutableBeaconStateElectra.required(state).getPendingDeposits(); + for (DepositRequest depositRequest : depositRequests) { + // process_deposit_request + if (electraState + .getDepositRequestsStartIndex() + .equals(SpecConfigElectra.UNSET_DEPOSIT_REQUESTS_START_INDEX)) { + electraState.setDepositRequestsStartIndex(depositRequest.getIndex()); + } + + final PendingDeposit deposit = + schemaDefinitionsElectra + .getPendingDepositSchema() + .create( + new SszPublicKey(depositRequest.getPubkey()), + SszBytes32.of(depositRequest.getWithdrawalCredentials()), + SszUInt64.of(depositRequest.getAmount()), + new SszSignature(depositRequest.getSignature()), + SszUInt64.of(state.getSlot())); + pendingDeposits.append(deposit); + } + } + /** Implements process_withdrawal_request from consensus-specs (EIP-7002 & EIP-7251). */ @Override public void processWithdrawalRequests( @@ -360,36 +390,6 @@ public void processWithdrawalRequests( }); } - /* - Implements process_deposit_request from consensus-specs (EIP-6110) - */ - @Override - public void processDepositRequests( - final MutableBeaconState state, final List depositRequests) { - final MutableBeaconStateElectra electraState = MutableBeaconStateElectra.required(state); - final SszMutableList pendingDeposits = - MutableBeaconStateElectra.required(state).getPendingDeposits(); - for (DepositRequest depositRequest : depositRequests) { - // process_deposit_request - if (electraState - .getDepositRequestsStartIndex() - .equals(SpecConfigElectra.UNSET_DEPOSIT_REQUESTS_START_INDEX)) { - electraState.setDepositRequestsStartIndex(depositRequest.getIndex()); - } - - final PendingDeposit deposit = - schemaDefinitionsElectra - .getPendingDepositSchema() - .create( - new SszPublicKey(depositRequest.getPubkey()), - SszBytes32.of(depositRequest.getWithdrawalCredentials()), - SszUInt64.of(depositRequest.getAmount()), - new SszSignature(depositRequest.getSignature()), - SszUInt64.of(state.getSlot())); - pendingDeposits.append(deposit); - } - } - /** * Implements process_consolidation_request from consensus-spec (EIP-7251) * diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/logic/versions/electra/helpers/BeaconStateAccessorsElectra.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/logic/versions/electra/helpers/BeaconStateAccessorsElectra.java index 2bc48d0ba4f..0c48f640f3c 100644 --- a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/logic/versions/electra/helpers/BeaconStateAccessorsElectra.java +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/logic/versions/electra/helpers/BeaconStateAccessorsElectra.java @@ -21,7 +21,6 @@ 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.BeaconState; import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.electra.BeaconStateElectra; import tech.pegasys.teku.spec.datastructures.state.versions.electra.PendingPartialWithdrawal; @@ -52,19 +51,6 @@ public UInt64 getActivationExitChurnLimit(final BeaconStateElectra state) { return getBalanceChurnLimit(state).min(configElectra.getMaxPerEpochActivationExitChurnLimit()); } - /** - * get_active_balance - * - * @param state The state to get the effective balance from - * @param validatorIndex the index of the validator - */ - public UInt64 getActiveBalance(final BeaconState state, final int validatorIndex) { - final Validator validator = state.getValidators().get(validatorIndex); - final UInt64 maxEffectiveBalance = miscHelpers.getMaxEffectiveBalance(validator); - final UInt64 validatorBalance = state.getBalances().get(validatorIndex).get(); - return validatorBalance.min(maxEffectiveBalance); - } - /** * get_pending_balance_to_withdraw * diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/logic/versions/electra/helpers/MiscHelpersElectra.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/logic/versions/electra/helpers/MiscHelpersElectra.java index 52f05f74dea..e5c8869e9ad 100644 --- a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/logic/versions/electra/helpers/MiscHelpersElectra.java +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/logic/versions/electra/helpers/MiscHelpersElectra.java @@ -68,13 +68,6 @@ public int computeProposerIndex( SpecConfigElectra.required(specConfig).getMaxEffectiveBalanceElectra()); } - @Override - public UInt64 getMaxEffectiveBalance(final Validator validator) { - return predicatesElectra.hasCompoundingWithdrawalCredential(validator) - ? specConfigElectra.getMaxEffectiveBalanceElectra() - : specConfigElectra.getMinActivationBalance(); - } - @Override public Validator getValidatorFromDeposit( final BLSPublicKey pubkey, final Bytes32 withdrawalCredentials, final UInt64 amount) { @@ -99,8 +92,10 @@ public Validator getValidatorFromDeposit( } @Override - public Optional toVersionElectra() { - return Optional.of(this); + public UInt64 getMaxEffectiveBalance(final Validator validator) { + return predicatesElectra.hasCompoundingWithdrawalCredential(validator) + ? specConfigElectra.getMaxEffectiveBalanceElectra() + : specConfigElectra.getMinActivationBalance(); } @Override @@ -112,4 +107,9 @@ public boolean isFormerDepositMechanismDisabled(final BeaconState state) { .getEth1DepositIndex() .equals(BeaconStateElectra.required(state).getDepositRequestsStartIndex()); } + + @Override + public Optional toVersionElectra() { + return Optional.of(this); + } } diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/logic/versions/electra/statetransition/epoch/EpochProcessorElectra.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/logic/versions/electra/statetransition/epoch/EpochProcessorElectra.java index 8eedf3e372e..dd9e716baf6 100644 --- a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/logic/versions/electra/statetransition/epoch/EpochProcessorElectra.java +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/logic/versions/electra/statetransition/epoch/EpochProcessorElectra.java @@ -322,12 +322,18 @@ public void processPendingConsolidations(final MutableBeaconState state) { break; } - final UInt64 activeBalance = - stateAccessorsElectra.getActiveBalance(state, pendingConsolidation.getSourceIndex()); + // Calculate the consolidated balance + final UInt64 sourceEffectiveBalance = + state + .getBalances() + .get(pendingConsolidation.getSourceIndex()) + .get() + .min(sourceValidator.getEffectiveBalance()); + // Move active balance to target. Excess balance is withdrawable. beaconStateMutators.decreaseBalance( - state, pendingConsolidation.getSourceIndex(), activeBalance); + state, pendingConsolidation.getSourceIndex(), sourceEffectiveBalance); beaconStateMutators.increaseBalance( - state, pendingConsolidation.getTargetIndex(), activeBalance); + state, pendingConsolidation.getTargetIndex(), sourceEffectiveBalance); nextPendingBalanceConsolidation++; }