diff --git a/ethereum/spec/src/test/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/common/ValidatorIndexCacheTest.java b/ethereum/spec/src/test/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/common/ValidatorIndexCacheTest.java index 21da5ab67fc..3e09ce9b3c8 100644 --- a/ethereum/spec/src/test/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/common/ValidatorIndexCacheTest.java +++ b/ethereum/spec/src/test/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/common/ValidatorIndexCacheTest.java @@ -43,6 +43,23 @@ public class ValidatorIndexCacheTest { @SuppressWarnings("unchecked") final Cache cache = mock(Cache.class); + @Test + public void shouldReturnEmptyIfValidatorIndexIsNotConsistentWithNumberOfValidatorsInState() { + final SszList validators = state.getValidators(); + final int latestFinalizedIndex = NUMBER_OF_VALIDATORS - 1; + final ValidatorIndexCache validatorIndexCache = new ValidatorIndexCache(); + validatorIndexCache.updateLatestFinalizedIndex(state); + + final BLSPublicKey publicKey = validators.get(latestFinalizedIndex).getPublicKey(); + // cache eagerly the last validator public key + validatorIndexCache.invalidateWithNewValue(publicKey, latestFinalizedIndex); + + // state with one less validator + final BeaconState state = dataStructureUtil.randomBeaconState(NUMBER_OF_VALIDATORS - 1); + + assertThat(validatorIndexCache.getValidatorIndex(state, publicKey)).isEmpty(); + } + @Test public void shouldScanFinalizedStateAndCache() { final SszList validators = state.getValidators();