use of tech.pegasys.teku.validator.api.ProposerDuty in project teku by ConsenSys.
the class ValidatorApiHandler method getProposalSlotsForEpoch.
private List<ProposerDuty> getProposalSlotsForEpoch(final BeaconState state, final UInt64 epoch) {
final UInt64 epochStartSlot = spec.computeStartSlotAtEpoch(epoch);
final UInt64 startSlot = epochStartSlot.max(GENESIS_SLOT.increment());
final UInt64 endSlot = epochStartSlot.plus(spec.slotsPerEpoch(epoch));
final List<ProposerDuty> proposerSlots = new ArrayList<>();
for (UInt64 slot = startSlot; slot.compareTo(endSlot) < 0; slot = slot.plus(UInt64.ONE)) {
final int proposerIndex = spec.getBeaconProposerIndex(state, slot);
final BLSPublicKey publicKey = spec.getValidatorPubKey(state, UInt64.valueOf(proposerIndex)).orElseThrow();
proposerSlots.add(new ProposerDuty(publicKey, proposerIndex, slot));
}
return proposerSlots;
}
use of tech.pegasys.teku.validator.api.ProposerDuty in project teku by ConsenSys.
the class RemoteValidatorApiHandlerTest method getProposerDuties_WhenFound_ReturnsDuties.
@Test
public void getProposerDuties_WhenFound_ReturnsDuties() {
final BLSPublicKey blsPublicKey = dataStructureUtil.randomPublicKey();
final int validatorIndex = 472;
final tech.pegasys.teku.api.response.v1.validator.ProposerDuty schemaValidatorDuties = new tech.pegasys.teku.api.response.v1.validator.ProposerDuty(new BLSPubKey(blsPublicKey), validatorIndex, UInt64.ZERO);
final ProposerDuty expectedValidatorDuties = new ProposerDuty(blsPublicKey, validatorIndex, UInt64.ZERO);
final GetProposerDutiesResponse response = new GetProposerDutiesResponse(Bytes32.fromHexString("0x1234"), List.of(schemaValidatorDuties));
when(apiClient.getProposerDuties(UInt64.ONE)).thenReturn(Optional.of(response));
SafeFuture<Optional<ProposerDuties>> future = apiHandler.getProposerDuties(UInt64.ONE);
ProposerDuties validatorDuties = unwrapToValue(future);
assertThat(validatorDuties.getDuties().get(0)).isEqualTo(expectedValidatorDuties);
assertThat(validatorDuties.getDependentRoot()).isEqualTo(response.dependentRoot);
}
use of tech.pegasys.teku.validator.api.ProposerDuty in project teku by ConsenSys.
the class BlockDutySchedulerTest method shouldNotPerformDutiesForSameSlotTwice.
@Test
public void shouldNotPerformDutiesForSameSlotTwice() {
createDutySchedulerWithRealDuties();
final UInt64 blockProposerSlot = UInt64.valueOf(5);
final ProposerDuty validator1Duties = new ProposerDuty(VALIDATOR1_KEY, 5, blockProposerSlot);
when(validatorApiChannel.getProposerDuties(eq(ZERO))).thenReturn(completedFuture(Optional.of(new ProposerDuties(dataStructureUtil.randomBytes32(), List.of(validator1Duties, new ProposerDuty(dataStructureUtil.randomPublicKey(), 6, UInt64.valueOf(4)))))));
final BlockProductionDuty blockCreationDuty = mock(BlockProductionDuty.class);
when(blockCreationDuty.performDuty()).thenReturn(new SafeFuture<>());
when(blockDutyFactory.createProductionDuty(blockProposerSlot, validator1)).thenReturn(blockCreationDuty);
// Load duties
dutyScheduler.onSlot(spec.computeStartSlotAtEpoch(ZERO));
// Execute
dutyScheduler.onBlockProductionDue(blockProposerSlot);
verify(blockCreationDuty).performDuty();
// Somehow we triggered the same slot again.
dutyScheduler.onBlockProductionDue(blockProposerSlot);
// But shouldn't produce another block and get ourselves slashed.
verifyNoMoreInteractions(blockCreationDuty);
}
use of tech.pegasys.teku.validator.api.ProposerDuty in project teku by ConsenSys.
the class BlockDutySchedulerTest method shouldScheduleBlockProposalDuty.
@Test
public void shouldScheduleBlockProposalDuty() {
createDutySchedulerWithRealDuties();
final UInt64 blockProposerSlot = UInt64.valueOf(5);
final ProposerDuty validator1Duties = new ProposerDuty(VALIDATOR1_KEY, 5, blockProposerSlot);
when(validatorApiChannel.getProposerDuties(ZERO)).thenReturn(completedFuture(Optional.of(new ProposerDuties(dataStructureUtil.randomBytes32(), List.of(validator1Duties)))));
final BlockProductionDuty blockCreationDuty = mock(BlockProductionDuty.class);
when(blockCreationDuty.performDuty()).thenReturn(new SafeFuture<>());
when(blockDutyFactory.createProductionDuty(blockProposerSlot, validator1)).thenReturn(blockCreationDuty);
// Load duties
dutyScheduler.onSlot(spec.computeStartSlotAtEpoch(ZERO));
// Execute
dutyScheduler.onBlockProductionDue(blockProposerSlot);
verify(blockCreationDuty).performDuty();
}
Aggregations