iota_data_ingestion/
common.rs

1// Copyright (c) 2025 IOTA Stiftung
2// SPDX-License-Identifier: Apache-2.0
3
4use std::ops::Range;
5
6use iota_rest_api::Client;
7use iota_types::{committee::EpochId, messages_checkpoint::CheckpointSequenceNumber};
8
9/// Get the current epoch.
10pub async fn current_epoch(rest_client: &Client) -> anyhow::Result<EpochId> {
11    let chk = rest_client.get_latest_checkpoint().await?;
12    Ok(chk.epoch)
13}
14
15/// Get the range of [`CheckpointSequenceNumber`] from the first checkpoint of
16/// the epoch containing the watermark up to but not including the watermark.
17pub async fn checkpoint_sequence_number_range_to_watermark(
18    rest_client: &Client,
19    watermark: CheckpointSequenceNumber,
20) -> anyhow::Result<Range<CheckpointSequenceNumber>> {
21    let chk = rest_client.get_checkpoint_summary(watermark).await?;
22    let chk_seq_num = epoch_first_checkpoint_sequence_number(rest_client, chk.epoch).await?;
23    Ok(chk_seq_num..watermark)
24}
25
26/// Get the [`CheckpointSequenceNumber`] of the first checkpoint in the
27/// specified epoch.
28pub async fn epoch_first_checkpoint_sequence_number(
29    rest_client: &Client,
30    epoch: EpochId,
31) -> anyhow::Result<CheckpointSequenceNumber> {
32    let previous_epoch = epoch.saturating_sub(1);
33    if epoch == 0 {
34        return Ok(0);
35    }
36    let last_epoch_chk = rest_client
37        .get_epoch_last_checkpoint(previous_epoch)
38        .await?;
39    Ok(last_epoch_chk.sequence_number + 1)
40}