diff --git a/validator/coordinator/src/main/java/tech/pegasys/teku/validator/coordinator/ValidatorApiHandler.java b/validator/coordinator/src/main/java/tech/pegasys/teku/validator/coordinator/ValidatorApiHandler.java index ee3ed44e253..96bc6504c1c 100644 --- a/validator/coordinator/src/main/java/tech/pegasys/teku/validator/coordinator/ValidatorApiHandler.java +++ b/validator/coordinator/src/main/java/tech/pegasys/teku/validator/coordinator/ValidatorApiHandler.java @@ -13,7 +13,6 @@ package tech.pegasys.teku.validator.coordinator; -import static java.util.Collections.emptyMap; import static java.util.stream.Collectors.toList; import static tech.pegasys.teku.datastructures.util.BeaconStateUtil.compute_epoch_at_slot; import static tech.pegasys.teku.datastructures.util.BeaconStateUtil.compute_start_slot_at_epoch; @@ -147,19 +146,20 @@ public SafeFuture> getGenesisData() { @Override public SafeFuture> getValidatorIndices( final List publicKeys) { - return SafeFuture.completedFuture( - combinedChainDataClient - .getBestState() - .map( - state -> { - final Map results = new HashMap<>(); - publicKeys.forEach( - publicKey -> - ValidatorsUtil.getValidatorIndex(state, publicKey) - .ifPresent(index -> results.put(publicKey, index))); - return results; - }) - .orElse(emptyMap())); + return SafeFuture.of( + () -> + combinedChainDataClient + .getBestState() + .map( + state -> { + final Map results = new HashMap<>(); + publicKeys.forEach( + publicKey -> + ValidatorsUtil.getValidatorIndex(state, publicKey) + .ifPresent(index -> results.put(publicKey, index))); + return results; + }) + .orElseThrow(() -> new IllegalStateException("Head state is not yet available"))); } @Override diff --git a/validator/coordinator/src/test/java/tech/pegasys/teku/validator/coordinator/ValidatorApiHandlerTest.java b/validator/coordinator/src/test/java/tech/pegasys/teku/validator/coordinator/ValidatorApiHandlerTest.java index c46eed07090..301fc4fb053 100644 --- a/validator/coordinator/src/test/java/tech/pegasys/teku/validator/coordinator/ValidatorApiHandlerTest.java +++ b/validator/coordinator/src/test/java/tech/pegasys/teku/validator/coordinator/ValidatorApiHandlerTest.java @@ -14,7 +14,6 @@ package tech.pegasys.teku.validator.coordinator; import static java.util.Collections.emptyList; -import static java.util.Collections.emptyMap; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.ArgumentMatchers.any; @@ -526,12 +525,14 @@ public void sendAggregateAndProof_shouldPostAggregateAndProof() { } @Test - void getValidatorIndices_shouldReturnEmptyMapWhenBestStateNotAvailable() { + void getValidatorIndices_shouldThrowExceptionWhenBestStateNotAvailable() { when(chainDataClient.getBestState()).thenReturn(Optional.empty()); + // The validator client needs to be able to differentiate between the state not yet being loaded + // and the requested validators not existing so it doesn't skip scheduling duties. assertThatSafeFuture( validatorApiHandler.getValidatorIndices(List.of(dataStructureUtil.randomPublicKey()))) - .isCompletedWithValue(emptyMap()); + .isCompletedExceptionallyWith(IllegalStateException.class); } @Test