Query ledger tip of chain

How do you query the ledger to get to know what’s the very last block index?

Found following in NNS-dapp but it does not exist in the public API I think - i.e. there is not call with protobuf.

pub async fn tip_of_chain() -> Result<BlockIndex, String> {
    let response: TipOfChainResponsePb =
        dfn_core::call(LEDGER_CANISTER_ID, "tip_of_chain_pb", protobuf, TipOfChainRequestPb {})
            .await
            .map_err(|e| e.1)?;

    Ok(response.chain_length.map(|c| c.height).unwrap_or_default())
}

I did following and I keep in memory last index I queried until top to avoid panic overflow but feels hacky…

pub async fn chain_length(block_index: BlockIndex) -> CallResult<u64> {
    let ledger = Principal::from_text(&LEDGER).unwrap();
    let response = query_blocks(
        ledger,
        GetBlocksArgs {
            start: block_index,
            length: 1,
        },
    )
    .await?;
    Ok(response.chain_length)
}