@Test public void validatorParameterToIndex_shouldDetectAboveMaxInt() { final ChainDataProvider provider = new ChainDataProvider(recentChainData, combinedChainDataClient); assertThrows( IllegalArgumentException.class, () -> provider.validatorParameterToIndex( UInt64.valueOf(Integer.MAX_VALUE).increment().toString())); }
public Optional<Integer> validatorParameterToIndex(final String validatorParameter) { if (!isStoreAvailable()) { throw new ChainDataUnavailableException(); } final Optional<tech.pegasys.teku.datastructures.state.BeaconState> maybeState = recentChainData.getBestState(); if (maybeState.isEmpty()) { return Optional.empty(); } final tech.pegasys.teku.datastructures.state.BeaconState state = maybeState.get(); if (validatorParameter.toLowerCase().startsWith("0x")) { try { BLSPubKey publicKey = BLSPubKey.fromHexString(validatorParameter); return ValidatorsUtil.getValidatorIndex(state, publicKey.asBLSPublicKey()); } catch (PublicKeyException ex) { throw new IllegalArgumentException( String.format("Invalid public key: %s", validatorParameter)); } } else { try { final UInt64 numericValidator = UInt64.valueOf(validatorParameter); if (numericValidator.isGreaterThan(UInt64.valueOf(Integer.MAX_VALUE))) { throw new IllegalArgumentException( String.format("Validator Index is too high to use: %s", validatorParameter)); } final int validatorIndex = numericValidator.intValue(); final int validatorCount = state.getValidators().size(); if (validatorIndex > validatorCount) { throw new IllegalArgumentException( String.format( "Invalid validator index: %d, exceeds validator count: %d", validatorIndex, validatorCount)); } return Optional.of(validatorIndex); } catch (NumberFormatException ex) { throw new IllegalArgumentException( String.format("Invalid validator: %s", validatorParameter)); } } }
ChainDataProvider { public Optional<Integer> validatorParameterToIndex(final String validatorParameter) { if (!isStoreAvailable()) { throw new ChainDataUnavailableException(); } final Optional<tech.pegasys.teku.datastructures.state.BeaconState> maybeState = recentChainData.getBestState(); if (maybeState.isEmpty()) { return Optional.empty(); } final tech.pegasys.teku.datastructures.state.BeaconState state = maybeState.get(); if (validatorParameter.toLowerCase().startsWith("0x")) { try { BLSPubKey publicKey = BLSPubKey.fromHexString(validatorParameter); return ValidatorsUtil.getValidatorIndex(state, publicKey.asBLSPublicKey()); } catch (PublicKeyException ex) { throw new IllegalArgumentException( String.format("Invalid public key: %s", validatorParameter)); } } else { try { final UInt64 numericValidator = UInt64.valueOf(validatorParameter); if (numericValidator.isGreaterThan(UInt64.valueOf(Integer.MAX_VALUE))) { throw new IllegalArgumentException( String.format("Validator Index is too high to use: %s", validatorParameter)); } final int validatorIndex = numericValidator.intValue(); final int validatorCount = state.getValidators().size(); if (validatorIndex > validatorCount) { throw new IllegalArgumentException( String.format( "Invalid validator index: %d, exceeds validator count: %d", validatorIndex, validatorCount)); } return Optional.of(validatorIndex); } catch (NumberFormatException ex) { throw new IllegalArgumentException( String.format("Invalid validator: %s", validatorParameter)); } } } }
ChainDataProvider { public Optional<Integer> validatorParameterToIndex(final String validatorParameter) { if (!isStoreAvailable()) { throw new ChainDataUnavailableException(); } final Optional<tech.pegasys.teku.datastructures.state.BeaconState> maybeState = recentChainData.getBestState(); if (maybeState.isEmpty()) { return Optional.empty(); } final tech.pegasys.teku.datastructures.state.BeaconState state = maybeState.get(); if (validatorParameter.toLowerCase().startsWith("0x")) { try { BLSPubKey publicKey = BLSPubKey.fromHexString(validatorParameter); return ValidatorsUtil.getValidatorIndex(state, publicKey.asBLSPublicKey()); } catch (PublicKeyException ex) { throw new IllegalArgumentException( String.format("Invalid public key: %s", validatorParameter)); } } else { try { final UInt64 numericValidator = UInt64.valueOf(validatorParameter); if (numericValidator.isGreaterThan(UInt64.valueOf(Integer.MAX_VALUE))) { throw new IllegalArgumentException( String.format("Validator Index is too high to use: %s", validatorParameter)); } final int validatorIndex = numericValidator.intValue(); final int validatorCount = state.getValidators().size(); if (validatorIndex > validatorCount) { throw new IllegalArgumentException( String.format( "Invalid validator index: %d, exceeds validator count: %d", validatorIndex, validatorCount)); } return Optional.of(validatorIndex); } catch (NumberFormatException ex) { throw new IllegalArgumentException( String.format("Invalid validator: %s", validatorParameter)); } } } ChainDataProvider(
      final RecentChainData recentChainData,
      final CombinedChainDataClient combinedChainDataClient); }
ChainDataProvider { public Optional<Integer> validatorParameterToIndex(final String validatorParameter) { if (!isStoreAvailable()) { throw new ChainDataUnavailableException(); } final Optional<tech.pegasys.teku.datastructures.state.BeaconState> maybeState = recentChainData.getBestState(); if (maybeState.isEmpty()) { return Optional.empty(); } final tech.pegasys.teku.datastructures.state.BeaconState state = maybeState.get(); if (validatorParameter.toLowerCase().startsWith("0x")) { try { BLSPubKey publicKey = BLSPubKey.fromHexString(validatorParameter); return ValidatorsUtil.getValidatorIndex(state, publicKey.asBLSPublicKey()); } catch (PublicKeyException ex) { throw new IllegalArgumentException( String.format("Invalid public key: %s", validatorParameter)); } } else { try { final UInt64 numericValidator = UInt64.valueOf(validatorParameter); if (numericValidator.isGreaterThan(UInt64.valueOf(Integer.MAX_VALUE))) { throw new IllegalArgumentException( String.format("Validator Index is too high to use: %s", validatorParameter)); } final int validatorIndex = numericValidator.intValue(); final int validatorCount = state.getValidators().size(); if (validatorIndex > validatorCount) { throw new IllegalArgumentException( String.format( "Invalid validator index: %d, exceeds validator count: %d", validatorIndex, validatorCount)); } return Optional.of(validatorIndex); } catch (NumberFormatException ex) { throw new IllegalArgumentException( String.format("Invalid validator: %s", validatorParameter)); } } } ChainDataProvider(
      final RecentChainData recentChainData,
      final CombinedChainDataClient combinedChainDataClient); UInt64 getGenesisTime(); Optional<BeaconHead> getBeaconHead(); GetForkResponse getForkInfo(); SafeFuture<Optional<List<Committee>>> getCommitteesAtEpoch(final UInt64 epoch); SafeFuture<Optional<GetBlockResponse>> getBlockBySlot(final UInt64 slot); boolean isStoreAvailable(); Optional<Bytes32> getBestBlockRoot(); UInt64 getCurrentEpoch(); SafeFuture<Optional<GetBlockResponse>> getBlockByBlockRoot(final Bytes32 blockParam); SafeFuture<Optional<BeaconState>> getStateByBlockRoot(final Bytes32 blockRoot); SafeFuture<Optional<BeaconState>> getStateByStateRoot(final Bytes32 stateRoot); SafeFuture<Optional<BeaconState>> getStateAtSlot(final UInt64 slot); SafeFuture<Optional<Bytes32>> getStateRootAtSlot(final UInt64 slot); SafeFuture<Optional<BeaconValidators>> getValidatorsByValidatorsRequest(
      final ValidatorsRequest request); boolean isFinalized(final SignedBeaconBlock signedBeaconBlock); boolean isFinalized(final UInt64 slot); boolean isFinalizedEpoch(final UInt64 epoch); Optional<BeaconChainHead> getHeadState(); Optional<UInt64> stateParameterToSlot(final String pathParam); Optional<Integer> validatorParameterToIndex(final String validatorParameter); SafeFuture<Optional<Fork>> getForkAtSlot(final UInt64 slot); SafeFuture<Optional<ValidatorResponse>> getValidatorDetails(
      final Optional<UInt64> maybeSlot, final Optional<Integer> maybeIndex); Optional<Bytes32> getStateRootFromBlockRoot(final Bytes32 blockRoot); }
ChainDataProvider { public Optional<Integer> validatorParameterToIndex(final String validatorParameter) { if (!isStoreAvailable()) { throw new ChainDataUnavailableException(); } final Optional<tech.pegasys.teku.datastructures.state.BeaconState> maybeState = recentChainData.getBestState(); if (maybeState.isEmpty()) { return Optional.empty(); } final tech.pegasys.teku.datastructures.state.BeaconState state = maybeState.get(); if (validatorParameter.toLowerCase().startsWith("0x")) { try { BLSPubKey publicKey = BLSPubKey.fromHexString(validatorParameter); return ValidatorsUtil.getValidatorIndex(state, publicKey.asBLSPublicKey()); } catch (PublicKeyException ex) { throw new IllegalArgumentException( String.format("Invalid public key: %s", validatorParameter)); } } else { try { final UInt64 numericValidator = UInt64.valueOf(validatorParameter); if (numericValidator.isGreaterThan(UInt64.valueOf(Integer.MAX_VALUE))) { throw new IllegalArgumentException( String.format("Validator Index is too high to use: %s", validatorParameter)); } final int validatorIndex = numericValidator.intValue(); final int validatorCount = state.getValidators().size(); if (validatorIndex > validatorCount) { throw new IllegalArgumentException( String.format( "Invalid validator index: %d, exceeds validator count: %d", validatorIndex, validatorCount)); } return Optional.of(validatorIndex); } catch (NumberFormatException ex) { throw new IllegalArgumentException( String.format("Invalid validator: %s", validatorParameter)); } } } ChainDataProvider(
      final RecentChainData recentChainData,
      final CombinedChainDataClient combinedChainDataClient); UInt64 getGenesisTime(); Optional<BeaconHead> getBeaconHead(); GetForkResponse getForkInfo(); SafeFuture<Optional<List<Committee>>> getCommitteesAtEpoch(final UInt64 epoch); SafeFuture<Optional<GetBlockResponse>> getBlockBySlot(final UInt64 slot); boolean isStoreAvailable(); Optional<Bytes32> getBestBlockRoot(); UInt64 getCurrentEpoch(); SafeFuture<Optional<GetBlockResponse>> getBlockByBlockRoot(final Bytes32 blockParam); SafeFuture<Optional<BeaconState>> getStateByBlockRoot(final Bytes32 blockRoot); SafeFuture<Optional<BeaconState>> getStateByStateRoot(final Bytes32 stateRoot); SafeFuture<Optional<BeaconState>> getStateAtSlot(final UInt64 slot); SafeFuture<Optional<Bytes32>> getStateRootAtSlot(final UInt64 slot); SafeFuture<Optional<BeaconValidators>> getValidatorsByValidatorsRequest(
      final ValidatorsRequest request); boolean isFinalized(final SignedBeaconBlock signedBeaconBlock); boolean isFinalized(final UInt64 slot); boolean isFinalizedEpoch(final UInt64 epoch); Optional<BeaconChainHead> getHeadState(); Optional<UInt64> stateParameterToSlot(final String pathParam); Optional<Integer> validatorParameterToIndex(final String validatorParameter); SafeFuture<Optional<Fork>> getForkAtSlot(final UInt64 slot); SafeFuture<Optional<ValidatorResponse>> getValidatorDetails(
      final Optional<UInt64> maybeSlot, final Optional<Integer> maybeIndex); Optional<Bytes32> getStateRootFromBlockRoot(final Bytes32 blockRoot);  }