Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter out indices in the caches which are larger than the validators in the state #8171

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,15 @@ public ValidatorIndexCache() {

public Optional<Integer> getValidatorIndex(
final BeaconState state, final BLSPublicKey publicKey) {
// Store latestFinalizedIndex here in case we need to scan keys from the state.
// This ensures we're adding from a point that we're confident the cache is at
// when we scan for more keys through the state later.
final int latestFinalizedIndexSnapshot = latestFinalizedIndex.get();
final SszList<Validator> validators = state.getValidators();
return validatorIndices
.getCached(publicKey)
.or(() -> findIndexFromFinalizedState(validators, publicKey, latestFinalizedIndexSnapshot))
final Optional<Integer> validatorIndex = validatorIndices.getCached(publicKey);
if (validatorIndex.isPresent()) {
return validatorIndex.filter(index -> index < validators.size());
}
// Using the same latestFinalizedIndex when scanning through
// the finalized and the non-finalized states ensures consistency
final int latestFinalizedIndexSnapshot = latestFinalizedIndex.get();
return findIndexFromFinalizedState(validators, publicKey, latestFinalizedIndexSnapshot)
.or(
() ->
findIndexFromNonFinalizedState(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,23 @@
@SuppressWarnings("unchecked")
final Cache<BLSPublicKey, Integer> cache = mock(Cache.class);

@Test
public void shouldReturnEmptyIfValidatorIndexIsNotConsistentWithNumberOfValidatorsInState() {
final SszList<Validator> 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);
Dismissed Show dismissed Hide dismissed

assertThat(validatorIndexCache.getValidatorIndex(state, publicKey)).isEmpty();
}

@Test
public void shouldScanFinalizedStateAndCache() {
final SszList<Validator> validators = state.getValidators();
Expand Down