@Test public void stateParameterToSlot_shouldParseSlotNumber() { final UInt64 slot = UInt64.valueOf(1); final ChainDataProvider provider = new ChainDataProvider(recentChainData, combinedChainDataClient); Optional<UInt64> result = provider.stateParameterToSlot(slot.toString()); assertThat(result.isPresent()).isTrue(); assertThat(result.get()).isEqualTo(slot); }
public Optional<UInt64> stateParameterToSlot(final String pathParam) { if (!isStoreAvailable()) { throw new ChainDataUnavailableException(); } try { switch (pathParam) { case ("head"): return recentChainData.getCurrentSlot(); case ("genesis"): return Optional.of(UInt64.ZERO); case ("finalized"): return recentChainData.getFinalizedCheckpoint().map(Checkpoint::getEpochStartSlot); case ("justified"): return recentChainData.getJustifiedCheckpoint().map(Checkpoint::getEpochStartSlot); } if (pathParam.toLowerCase().startsWith("0x")) { Bytes32 stateRoot = Bytes32.fromHexString(pathParam); return combinedChainDataClient.getSlotByStateRoot(stateRoot).join(); } else { final UInt64 slot = UInt64.valueOf(pathParam); final UInt64 headSlot = recentChainData.getHeadSlot(); if (slot.isGreaterThan(headSlot)) { throw new IllegalArgumentException( String.format("Invalid state: %s is beyond head slot %s", slot, headSlot)); } return Optional.of(UInt64.valueOf(pathParam)); } } catch (NumberFormatException ex) { throw new IllegalArgumentException(String.format("Invalid state: %s", pathParam)); } }
ChainDataProvider { public Optional<UInt64> stateParameterToSlot(final String pathParam) { if (!isStoreAvailable()) { throw new ChainDataUnavailableException(); } try { switch (pathParam) { case ("head"): return recentChainData.getCurrentSlot(); case ("genesis"): return Optional.of(UInt64.ZERO); case ("finalized"): return recentChainData.getFinalizedCheckpoint().map(Checkpoint::getEpochStartSlot); case ("justified"): return recentChainData.getJustifiedCheckpoint().map(Checkpoint::getEpochStartSlot); } if (pathParam.toLowerCase().startsWith("0x")) { Bytes32 stateRoot = Bytes32.fromHexString(pathParam); return combinedChainDataClient.getSlotByStateRoot(stateRoot).join(); } else { final UInt64 slot = UInt64.valueOf(pathParam); final UInt64 headSlot = recentChainData.getHeadSlot(); if (slot.isGreaterThan(headSlot)) { throw new IllegalArgumentException( String.format("Invalid state: %s is beyond head slot %s", slot, headSlot)); } return Optional.of(UInt64.valueOf(pathParam)); } } catch (NumberFormatException ex) { throw new IllegalArgumentException(String.format("Invalid state: %s", pathParam)); } } }
ChainDataProvider { public Optional<UInt64> stateParameterToSlot(final String pathParam) { if (!isStoreAvailable()) { throw new ChainDataUnavailableException(); } try { switch (pathParam) { case ("head"): return recentChainData.getCurrentSlot(); case ("genesis"): return Optional.of(UInt64.ZERO); case ("finalized"): return recentChainData.getFinalizedCheckpoint().map(Checkpoint::getEpochStartSlot); case ("justified"): return recentChainData.getJustifiedCheckpoint().map(Checkpoint::getEpochStartSlot); } if (pathParam.toLowerCase().startsWith("0x")) { Bytes32 stateRoot = Bytes32.fromHexString(pathParam); return combinedChainDataClient.getSlotByStateRoot(stateRoot).join(); } else { final UInt64 slot = UInt64.valueOf(pathParam); final UInt64 headSlot = recentChainData.getHeadSlot(); if (slot.isGreaterThan(headSlot)) { throw new IllegalArgumentException( String.format("Invalid state: %s is beyond head slot %s", slot, headSlot)); } return Optional.of(UInt64.valueOf(pathParam)); } } catch (NumberFormatException ex) { throw new IllegalArgumentException(String.format("Invalid state: %s", pathParam)); } } ChainDataProvider(
      final RecentChainData recentChainData,
      final CombinedChainDataClient combinedChainDataClient); }
ChainDataProvider { public Optional<UInt64> stateParameterToSlot(final String pathParam) { if (!isStoreAvailable()) { throw new ChainDataUnavailableException(); } try { switch (pathParam) { case ("head"): return recentChainData.getCurrentSlot(); case ("genesis"): return Optional.of(UInt64.ZERO); case ("finalized"): return recentChainData.getFinalizedCheckpoint().map(Checkpoint::getEpochStartSlot); case ("justified"): return recentChainData.getJustifiedCheckpoint().map(Checkpoint::getEpochStartSlot); } if (pathParam.toLowerCase().startsWith("0x")) { Bytes32 stateRoot = Bytes32.fromHexString(pathParam); return combinedChainDataClient.getSlotByStateRoot(stateRoot).join(); } else { final UInt64 slot = UInt64.valueOf(pathParam); final UInt64 headSlot = recentChainData.getHeadSlot(); if (slot.isGreaterThan(headSlot)) { throw new IllegalArgumentException( String.format("Invalid state: %s is beyond head slot %s", slot, headSlot)); } return Optional.of(UInt64.valueOf(pathParam)); } } catch (NumberFormatException ex) { throw new IllegalArgumentException(String.format("Invalid state: %s", pathParam)); } } 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<UInt64> stateParameterToSlot(final String pathParam) { if (!isStoreAvailable()) { throw new ChainDataUnavailableException(); } try { switch (pathParam) { case ("head"): return recentChainData.getCurrentSlot(); case ("genesis"): return Optional.of(UInt64.ZERO); case ("finalized"): return recentChainData.getFinalizedCheckpoint().map(Checkpoint::getEpochStartSlot); case ("justified"): return recentChainData.getJustifiedCheckpoint().map(Checkpoint::getEpochStartSlot); } if (pathParam.toLowerCase().startsWith("0x")) { Bytes32 stateRoot = Bytes32.fromHexString(pathParam); return combinedChainDataClient.getSlotByStateRoot(stateRoot).join(); } else { final UInt64 slot = UInt64.valueOf(pathParam); final UInt64 headSlot = recentChainData.getHeadSlot(); if (slot.isGreaterThan(headSlot)) { throw new IllegalArgumentException( String.format("Invalid state: %s is beyond head slot %s", slot, headSlot)); } return Optional.of(UInt64.valueOf(pathParam)); } } catch (NumberFormatException ex) { throw new IllegalArgumentException(String.format("Invalid state: %s", pathParam)); } } 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);  }