Skip to content

Commit b8f554e

Browse files
validator builder for tests (Consensys#8163)
1 parent ebd556a commit b8f554e

File tree

4 files changed

+132
-31
lines changed

4 files changed

+132
-31
lines changed

Diff for: beacon/validator/src/test/java/tech/pegasys/teku/validator/coordinator/ValidatorApiHandlerTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1267,7 +1267,7 @@ private void setupValidatorsState(
12671267
UInt64.valueOf(index),
12681268
dataStructureUtil.randomUInt64(),
12691269
statusOverrides.getOrDefault(publicKey, ValidatorStatus.active_ongoing),
1270-
dataStructureUtil.randomValidator(publicKey));
1270+
dataStructureUtil.validatorBuilder().publicKey(publicKey).build());
12711271
})
12721272
.collect(Collectors.toList());
12731273

Diff for: data/provider/src/test/java/tech/pegasys/teku/api/ChainDataProviderTest.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -718,10 +718,11 @@ void getExpectedWithdrawalsGeneratesList() {
718718
final List<Validator> validators = new ArrayList<>();
719719
while (validators.size() < 16) {
720720
validators.add(
721-
dataStructureUtil.randomValidator(
722-
dataStructureUtil.randomPublicKey(),
723-
dataStructureUtil.randomEth1WithdrawalCredentials(),
724-
specConfig.getMaxEffectiveBalance()));
721+
dataStructureUtil
722+
.validatorBuilder()
723+
.withRandomEth1WithdrawalCredentials()
724+
.effectiveBalance(specConfig.getMaxEffectiveBalance())
725+
.build());
725726
}
726727
final UInt64 eff = specConfig.getMaxEffectiveBalance();
727728
capellaBuilder.balances(

Diff for: ethereum/spec/src/testFixtures/java/tech/pegasys/teku/spec/util/DataStructureUtil.java

+22-26
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import static com.google.common.base.Preconditions.checkState;
1717
import static java.util.stream.Collectors.toList;
1818
import static tech.pegasys.teku.ethereum.pow.api.DepositConstants.DEPOSIT_CONTRACT_TREE_DEPTH;
19-
import static tech.pegasys.teku.spec.config.SpecConfig.FAR_FUTURE_EPOCH;
2019
import static tech.pegasys.teku.spec.constants.NetworkConstants.SYNC_COMMITTEE_SUBNET_COUNT;
2120
import static tech.pegasys.teku.spec.schemas.ApiSchemas.SIGNED_VALIDATOR_REGISTRATIONS_SCHEMA;
2221
import static tech.pegasys.teku.spec.schemas.ApiSchemas.SIGNED_VALIDATOR_REGISTRATION_SCHEMA;
@@ -1660,33 +1659,16 @@ public VoluntaryExit randomVoluntaryExit(final UInt64 validatorIndex) {
16601659
return new VoluntaryExit(randomUInt64(), validatorIndex);
16611660
}
16621661

1663-
public Validator randomValidator() {
1664-
return randomValidator(randomPublicKey());
1662+
public ValidatorBuilder validatorBuilder() {
1663+
return new ValidatorBuilder(spec, this);
16651664
}
16661665

1667-
public Validator randomValidator(final BLSPublicKey publicKey) {
1668-
return new Validator(
1669-
publicKey,
1670-
randomBytes32(),
1671-
getMaxEffectiveBalance(),
1672-
false,
1673-
FAR_FUTURE_EPOCH,
1674-
FAR_FUTURE_EPOCH,
1675-
FAR_FUTURE_EPOCH,
1676-
FAR_FUTURE_EPOCH);
1677-
}
1678-
1679-
public Validator randomValidator(
1680-
final BLSPublicKey publicKey, final Bytes32 withdrawalCredentials, final UInt64 balance) {
1681-
return new Validator(
1682-
publicKey,
1683-
withdrawalCredentials,
1684-
balance,
1685-
false,
1686-
FAR_FUTURE_EPOCH,
1687-
FAR_FUTURE_EPOCH,
1688-
FAR_FUTURE_EPOCH,
1689-
FAR_FUTURE_EPOCH);
1666+
/*
1667+
STOP! Before thinking about adding a new method to create a specific type of validator, consider using
1668+
DataStructureUtil.validatorBuilder() to create a custom validator that suit your needs.
1669+
*/
1670+
public Validator randomValidator() {
1671+
return validatorBuilder().build();
16901672
}
16911673

16921674
public Fork randomFork() {
@@ -2458,6 +2440,20 @@ public ExecutionLayerExit randomExecutionLayerExit() {
24582440
.create(randomEth1Address(), randomPublicKey());
24592441
}
24602442

2443+
public ExecutionLayerExit executionLayerExit(
2444+
final Bytes20 sourceAddress, BLSPublicKey validatorPubKey) {
2445+
return getElectraSchemaDefinitions(randomSlot())
2446+
.getExecutionLayerExitSchema()
2447+
.create(sourceAddress, validatorPubKey);
2448+
}
2449+
2450+
public ExecutionLayerExit executionLayerExit(final Validator validator) {
2451+
final Bytes20 executionAddress = new Bytes20(validator.getWithdrawalCredentials().slice(12));
2452+
return getElectraSchemaDefinitions(randomSlot())
2453+
.getExecutionLayerExitSchema()
2454+
.create(executionAddress, validator.getPublicKey());
2455+
}
2456+
24612457
public UInt64 randomBlobSidecarIndex() {
24622458
return randomUInt64(spec.getMaxBlobsPerBlock().orElseThrow());
24632459
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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

Comments
 (0)