|
| 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.util; |
| 15 | + |
| 16 | +import static tech.pegasys.teku.spec.config.SpecConfig.FAR_FUTURE_EPOCH; |
| 17 | + |
| 18 | +import org.apache.tuweni.bytes.Bytes32; |
| 19 | +import tech.pegasys.teku.bls.BLSPublicKey; |
| 20 | +import tech.pegasys.teku.infrastructure.unsigned.UInt64; |
| 21 | +import tech.pegasys.teku.spec.Spec; |
| 22 | +import tech.pegasys.teku.spec.datastructures.state.Validator; |
| 23 | + |
| 24 | +public class ValidatorBuilder { |
| 25 | + |
| 26 | + private final DataStructureUtil dataStructureUtil; |
| 27 | + private BLSPublicKey publicKey; |
| 28 | + private Bytes32 withdrawalCredentials; |
| 29 | + private UInt64 effectiveBalance; |
| 30 | + private boolean slashed = false; |
| 31 | + private UInt64 activationEligibilityEpoch = FAR_FUTURE_EPOCH; |
| 32 | + private UInt64 activationEpoch = FAR_FUTURE_EPOCH; |
| 33 | + private UInt64 exitEpoch = FAR_FUTURE_EPOCH; |
| 34 | + private UInt64 withdrawableEpoch = FAR_FUTURE_EPOCH; |
| 35 | + |
| 36 | + public ValidatorBuilder(final Spec spec, final DataStructureUtil dataStructureUtil) { |
| 37 | + this.dataStructureUtil = dataStructureUtil; |
| 38 | + this.publicKey = dataStructureUtil.randomPublicKey(); |
| 39 | + this.withdrawalCredentials = dataStructureUtil.randomBytes32(); |
| 40 | + this.effectiveBalance = spec.getGenesisSpec().getConfig().getMaxEffectiveBalance(); |
| 41 | + } |
| 42 | + |
| 43 | + public ValidatorBuilder publicKey(final BLSPublicKey publicKey) { |
| 44 | + this.publicKey = publicKey; |
| 45 | + return this; |
| 46 | + } |
| 47 | + |
| 48 | + public ValidatorBuilder withdrawalCredentials(final Bytes32 withdrawalCredentials) { |
| 49 | + this.withdrawalCredentials = withdrawalCredentials; |
| 50 | + return this; |
| 51 | + } |
| 52 | + |
| 53 | + public ValidatorBuilder withRandomEth1WithdrawalCredentials() { |
| 54 | + this.withdrawalCredentials = dataStructureUtil.randomEth1WithdrawalCredentials(); |
| 55 | + return this; |
| 56 | + } |
| 57 | + |
| 58 | + public ValidatorBuilder withRandomBlsWithdrawalCredentials() { |
| 59 | + this.withdrawalCredentials = dataStructureUtil.randomBlsWithdrawalCredentials(); |
| 60 | + return this; |
| 61 | + } |
| 62 | + |
| 63 | + public ValidatorBuilder effectiveBalance(final UInt64 effectiveBalance) { |
| 64 | + this.effectiveBalance = effectiveBalance; |
| 65 | + return this; |
| 66 | + } |
| 67 | + |
| 68 | + public ValidatorBuilder slashed(final boolean slashed) { |
| 69 | + this.slashed = slashed; |
| 70 | + return this; |
| 71 | + } |
| 72 | + |
| 73 | + public ValidatorBuilder activationEligibilityEpoch(final UInt64 activationEligibilityEpoch) { |
| 74 | + this.activationEligibilityEpoch = activationEligibilityEpoch; |
| 75 | + return this; |
| 76 | + } |
| 77 | + |
| 78 | + public ValidatorBuilder activationEpoch(final UInt64 activationEpoch) { |
| 79 | + this.activationEpoch = activationEpoch; |
| 80 | + return this; |
| 81 | + } |
| 82 | + |
| 83 | + public ValidatorBuilder exitEpoch(final UInt64 exitEpoch) { |
| 84 | + this.exitEpoch = exitEpoch; |
| 85 | + return this; |
| 86 | + } |
| 87 | + |
| 88 | + public ValidatorBuilder withdrawableEpoch(final UInt64 withdrawableEpoch) { |
| 89 | + this.withdrawableEpoch = withdrawableEpoch; |
| 90 | + return this; |
| 91 | + } |
| 92 | + |
| 93 | + public Validator build() { |
| 94 | + return new Validator( |
| 95 | + publicKey, |
| 96 | + withdrawalCredentials, |
| 97 | + effectiveBalance, |
| 98 | + slashed, |
| 99 | + activationEligibilityEpoch, |
| 100 | + activationEpoch, |
| 101 | + exitEpoch, |
| 102 | + withdrawableEpoch); |
| 103 | + } |
| 104 | +} |
0 commit comments