forked from Consensys/teku
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/master' into duty-time-metric
- Loading branch information
Showing
16 changed files
with
467 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
...java/tech/pegasys/teku/validator/coordinator/duties/AttesterDutiesGeneraterBenchmark.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/* | ||
* Copyright Consensys Software Inc., 2023 | ||
* | ||
* 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.validator.coordinator.duties; | ||
|
||
import static tech.pegasys.teku.ethereum.json.types.EthereumTypes.PUBLIC_KEY_TYPE; | ||
import static tech.pegasys.teku.infrastructure.http.RestApiConstants.EXECUTION_OPTIMISTIC; | ||
import static tech.pegasys.teku.infrastructure.json.types.CoreTypes.BOOLEAN_TYPE; | ||
import static tech.pegasys.teku.infrastructure.json.types.CoreTypes.BYTES32_TYPE; | ||
import static tech.pegasys.teku.infrastructure.json.types.CoreTypes.INTEGER_TYPE; | ||
import static tech.pegasys.teku.infrastructure.json.types.CoreTypes.UINT64_TYPE; | ||
|
||
import it.unimi.dsi.fastutil.ints.IntArrayList; | ||
import it.unimi.dsi.fastutil.ints.IntList; | ||
import java.util.List; | ||
import java.util.concurrent.TimeUnit; | ||
import org.openjdk.jmh.annotations.Benchmark; | ||
import org.openjdk.jmh.annotations.Fork; | ||
import org.openjdk.jmh.annotations.Level; | ||
import org.openjdk.jmh.annotations.Measurement; | ||
import org.openjdk.jmh.annotations.Param; | ||
import org.openjdk.jmh.annotations.Scope; | ||
import org.openjdk.jmh.annotations.Setup; | ||
import org.openjdk.jmh.annotations.State; | ||
import org.openjdk.jmh.annotations.Warmup; | ||
import org.openjdk.jmh.infra.Blackhole; | ||
import tech.pegasys.teku.benchmarks.gen.KeyFileGenerator; | ||
import tech.pegasys.teku.bls.BLSKeyPair; | ||
import tech.pegasys.teku.infrastructure.json.types.SerializableTypeDefinition; | ||
import tech.pegasys.teku.infrastructure.unsigned.UInt64; | ||
import tech.pegasys.teku.spec.Spec; | ||
import tech.pegasys.teku.spec.TestSpecFactory; | ||
import tech.pegasys.teku.spec.datastructures.interop.GenesisStateBuilder; | ||
import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.bellatrix.BeaconStateBellatrix; | ||
import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.bellatrix.MutableBeaconStateBellatrix; | ||
import tech.pegasys.teku.validator.api.AttesterDuties; | ||
import tech.pegasys.teku.validator.api.AttesterDuty; | ||
|
||
@Fork(1) | ||
@State(Scope.Thread) | ||
public class AttesterDutiesGeneraterBenchmark { | ||
private static final SerializableTypeDefinition<AttesterDuty> ATTESTER_DUTY_TYPE = | ||
SerializableTypeDefinition.object(AttesterDuty.class) | ||
.name("AttesterDuty") | ||
.withField("pubkey", PUBLIC_KEY_TYPE, AttesterDuty::getPublicKey) | ||
.withField("validator_index", INTEGER_TYPE, AttesterDuty::getValidatorIndex) | ||
.withField("committee_index", INTEGER_TYPE, AttesterDuty::getCommitteeIndex) | ||
.withField("committee_length", INTEGER_TYPE, AttesterDuty::getCommitteeLength) | ||
.withField("committees_at_slot", INTEGER_TYPE, AttesterDuty::getCommitteesAtSlot) | ||
.withField( | ||
"validator_committee_index", INTEGER_TYPE, AttesterDuty::getValidatorCommitteeIndex) | ||
.withField("slot", UINT64_TYPE, AttesterDuty::getSlot) | ||
.build(); | ||
public static final SerializableTypeDefinition<AttesterDuties> RESPONSE_TYPE = | ||
SerializableTypeDefinition.object(AttesterDuties.class) | ||
.name("GetAttesterDutiesResponse") | ||
.withField("dependent_root", BYTES32_TYPE, AttesterDuties::getDependentRoot) | ||
.withField(EXECUTION_OPTIMISTIC, BOOLEAN_TYPE, AttesterDuties::isExecutionOptimistic) | ||
.withField( | ||
"data", | ||
SerializableTypeDefinition.listOf(ATTESTER_DUTY_TYPE), | ||
AttesterDuties::getDuties) | ||
.build(); | ||
private final Spec spec = TestSpecFactory.createMinimalBellatrix(); | ||
private BeaconStateBellatrix state; | ||
private AttesterDutiesGenerator attesterDutiesGenerator; | ||
|
||
private UInt64 epoch; | ||
|
||
IntList validatorIndices = new IntArrayList(); | ||
|
||
@Param({"800000"}) | ||
int validatorsCount = 800_000; | ||
|
||
@Param({"500000"}) | ||
int querySize = 50_000; | ||
|
||
@Setup(Level.Trial) | ||
public void init() { | ||
List<BLSKeyPair> validatorKeys = KeyFileGenerator.readValidatorKeys(validatorsCount); | ||
state = | ||
BeaconStateBellatrix.required( | ||
new GenesisStateBuilder() | ||
.spec(spec) | ||
.signDeposits(false) | ||
.addValidators(validatorKeys) | ||
.build()); | ||
final MutableBeaconStateBellatrix mutableState = state.createWritableCopy(); | ||
mutableState.setSlot(UInt64.ONE); | ||
state = mutableState.commitChanges(); | ||
|
||
for (int i = 0; i < querySize; i++) { | ||
validatorIndices.add(i); | ||
} | ||
|
||
attesterDutiesGenerator = new AttesterDutiesGenerator(spec); | ||
System.out.println("Done!"); | ||
epoch = spec.computeEpochAtSlot(state.getSlot()).increment(); | ||
} | ||
|
||
@Benchmark | ||
@Warmup(iterations = 5, time = 2000, timeUnit = TimeUnit.MILLISECONDS) | ||
@Measurement(iterations = 10) | ||
public void computeAttesterDuties(Blackhole bh) { | ||
AttesterDuties attesterDutiesFromIndicesAndState = | ||
attesterDutiesGenerator.getAttesterDutiesFromIndicesAndState( | ||
state, epoch, validatorIndices, false); | ||
|
||
bh.consume(attesterDutiesFromIndicesAndState); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
...src/main/java/tech/pegasys/teku/validator/coordinator/duties/AttesterDutiesGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright Consensys Software Inc., 2023 | ||
* | ||
* 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.validator.coordinator.duties; | ||
|
||
import it.unimi.dsi.fastutil.ints.IntCollection; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import org.apache.tuweni.bytes.Bytes32; | ||
import tech.pegasys.teku.infrastructure.unsigned.UInt64; | ||
import tech.pegasys.teku.spec.Spec; | ||
import tech.pegasys.teku.spec.datastructures.state.CommitteeAssignment; | ||
import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState; | ||
import tech.pegasys.teku.spec.logic.common.helpers.BeaconStateAccessors; | ||
import tech.pegasys.teku.validator.api.AttesterDuties; | ||
import tech.pegasys.teku.validator.api.AttesterDuty; | ||
|
||
public class AttesterDutiesGenerator { | ||
private final Spec spec; | ||
|
||
public AttesterDutiesGenerator(Spec spec) { | ||
this.spec = spec; | ||
} | ||
|
||
public AttesterDuties getAttesterDutiesFromIndicesAndState( | ||
final BeaconState state, | ||
final UInt64 epoch, | ||
final IntCollection validatorIndices, | ||
final boolean isChainHeadOptimistic) { | ||
final Bytes32 dependentRoot = | ||
epoch.isGreaterThan(spec.getCurrentEpoch(state)) | ||
? spec.atEpoch(epoch).getBeaconStateUtil().getCurrentDutyDependentRoot(state) | ||
: spec.atEpoch(epoch).getBeaconStateUtil().getPreviousDutyDependentRoot(state); | ||
final List<AttesterDuty> duties = createAttesterDuties(state, epoch, validatorIndices); | ||
return new AttesterDuties(isChainHeadOptimistic, dependentRoot, duties); | ||
} | ||
|
||
private List<AttesterDuty> createAttesterDuties( | ||
final BeaconState state, final UInt64 epoch, final IntCollection validatorIndices) { | ||
final List<Optional<AttesterDuty>> maybeAttesterDutyList = new ArrayList<>(); | ||
final BeaconStateAccessors beaconStateAccessors = spec.atEpoch(epoch).beaconStateAccessors(); | ||
final UInt64 committeeCountPerSlot = | ||
beaconStateAccessors.getCommitteeCountPerSlot(state, epoch); | ||
final Map<Integer, CommitteeAssignment> validatorIndexToCommitteeAssignmentMap = | ||
spec.getValidatorIndexToCommitteeAssignmentMap(state, epoch); | ||
for (final Integer validatorIndex : validatorIndices) { | ||
final CommitteeAssignment committeeAssignment = | ||
validatorIndexToCommitteeAssignmentMap.get(validatorIndex); | ||
if (committeeAssignment != null) { | ||
maybeAttesterDutyList.add( | ||
attesterDutyFromCommitteeAssignment( | ||
committeeAssignment, validatorIndex, committeeCountPerSlot, state)); | ||
} | ||
} | ||
return maybeAttesterDutyList.stream().filter(Optional::isPresent).map(Optional::get).toList(); | ||
} | ||
|
||
private Optional<AttesterDuty> attesterDutyFromCommitteeAssignment( | ||
final CommitteeAssignment committeeAssignment, | ||
final int validatorIndex, | ||
final UInt64 committeeCountPerSlot, | ||
final BeaconState state) { | ||
return spec.getValidatorPubKey(state, UInt64.valueOf(validatorIndex)) | ||
.map( | ||
publicKey -> | ||
new AttesterDuty( | ||
publicKey, | ||
validatorIndex, | ||
committeeAssignment.getCommittee().size(), | ||
committeeAssignment.getCommitteeIndex().intValue(), | ||
committeeCountPerSlot.intValue(), | ||
committeeAssignment.getCommittee().indexOf(validatorIndex), | ||
committeeAssignment.getSlot())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.