|
| 1 | +/* |
| 2 | + * Copyright Consensys Software Inc., 2024 |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 5 | + * the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 10 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | + * specific language governing permissions and limitations under the License. |
| 12 | + */ |
| 13 | + |
| 14 | +package tech.pegasys.teku.spec.logic.versions.electra.operations.validation; |
| 15 | + |
| 16 | +import static tech.pegasys.teku.spec.logic.common.operations.validation.OperationInvalidReason.check; |
| 17 | +import static tech.pegasys.teku.spec.logic.common.operations.validation.OperationInvalidReason.firstOf; |
| 18 | + |
| 19 | +import com.google.common.annotations.VisibleForTesting; |
| 20 | +import java.util.Optional; |
| 21 | +import tech.pegasys.teku.infrastructure.unsigned.UInt64; |
| 22 | +import tech.pegasys.teku.spec.config.SpecConfig; |
| 23 | +import tech.pegasys.teku.spec.datastructures.operations.SignedVoluntaryExit; |
| 24 | +import tech.pegasys.teku.spec.datastructures.state.Fork; |
| 25 | +import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState; |
| 26 | +import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.electra.BeaconStateElectra; |
| 27 | +import tech.pegasys.teku.spec.logic.common.helpers.BeaconStateAccessors; |
| 28 | +import tech.pegasys.teku.spec.logic.common.helpers.Predicates; |
| 29 | +import tech.pegasys.teku.spec.logic.common.operations.validation.OperationInvalidReason; |
| 30 | +import tech.pegasys.teku.spec.logic.versions.electra.helpers.BeaconStateAccessorsElectra; |
| 31 | +import tech.pegasys.teku.spec.logic.versions.phase0.operations.validation.VoluntaryExitValidator; |
| 32 | + |
| 33 | +public class VoluntaryExitValidatorElectra extends VoluntaryExitValidator { |
| 34 | + private final BeaconStateAccessorsElectra stateAccessorsElectra; |
| 35 | + |
| 36 | + public VoluntaryExitValidatorElectra( |
| 37 | + final SpecConfig specConfig, |
| 38 | + final Predicates predicates, |
| 39 | + final BeaconStateAccessors beaconStateAccessors) { |
| 40 | + super(specConfig, predicates, beaconStateAccessors); |
| 41 | + this.stateAccessorsElectra = BeaconStateAccessorsElectra.required(beaconStateAccessors); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public Optional<OperationInvalidReason> validate( |
| 46 | + final Fork fork, final BeaconState state, final SignedVoluntaryExit signedExit) { |
| 47 | + final BeaconStateElectra stateElectra = BeaconStateElectra.required(state); |
| 48 | + return firstOf( |
| 49 | + () -> super.validate(fork, state, signedExit), |
| 50 | + () -> validateElectraConditions(stateElectra, signedExit)); |
| 51 | + } |
| 52 | + |
| 53 | + @VisibleForTesting |
| 54 | + Optional<OperationInvalidReason> validateElectraConditions( |
| 55 | + final BeaconStateElectra stateElectra, final SignedVoluntaryExit exit) { |
| 56 | + final int validatorId = exit.getValidatorId(); |
| 57 | + return check( |
| 58 | + stateAccessorsElectra |
| 59 | + .getPendingBalanceToWithdraw(stateElectra, validatorId) |
| 60 | + .equals(UInt64.ZERO), |
| 61 | + VoluntaryExitValidator.ExitInvalidReason.pendingWithdrawalsInQueue()); |
| 62 | + } |
| 63 | +} |
0 commit comments