Repository: MystenLabs/sui
File: crates/sui-rpc-api/src/grpc/v2/ledger_service/get_checkpoint.rs (Rust)

Code before:
    };

    let summary = verified_summary.data();
    let signature = verified_summary.auth_sig();
    let sequence_number = summary.sequence_number;
    let timestamp_ms = summary.timestamp_ms;

    if requires_indexed_data(&read_mask) {
        let highest_indexed = service
            .reader
            .inner()
            .indexes()
            .and_then(|idx| idx.get_highest_indexed_checkpoint_seq_number().ok())
            .flatten()
            .unwrap_or(0);

        if sequence_number > highest_indexed {
            return Err(RpcError::new(
                tonic::Code::NotFound,
                format!(
                    "Checkpoint {} is not yet indexed. Highest indexed checkpoint is {}",
                    sequence_number, highest_indexed
                ),
            ));
        }
    }

    let mut checkpoint = Checkpoint::default();

    checkpoint.merge(summary, &read_mask);
    checkpoint.merge(signature.clone(), &read_mask);

    if read_mask.contains(Checkpoint::CONTENTS_FIELD.name)
        || read_mask.contains(Checkpoint::TRANSACTIONS_FIELD.name)
        || read_mask.contains(Checkpoint::OBJECTS_FIELD.name)
    {
        let core_contents = service
            .reader
            .inner()
            .get_checkpoint_contents_by_sequence_number(sequence_number)
            .ok_or(CheckpointNotFoundError::sequence_number(sequence_number))?;

        if read_mask.contains(Checkpoint::CONTENTS_FIELD.name) {
            checkpoint.merge(core_contents.clone(), &read_mask);
        }

        if read_mask.contains(Checkpoint::TRANSACTIONS_FIELD.name)
            || read_mask.contains(Checkpoint::OBJECTS_FIELD.name)
        {
            let checkpoint_data = service
                .reader

Review comment (suggestion):
Similar to the comment on the `GetTransaction` api, lets always compare the requested checkpoint's sequence number with the latest executed checkpoint and just bail with a 404 if it is greater than or equal to the last executed checkpoint.

Code after:
    };

    let summary = verified_summary.data();
    let signature = verified_summary.auth_sig();
    let sequence_number = summary.sequence_number;
    let timestamp_ms = summary.timestamp_ms;

    let latest_checkpoint = service
        .reader
        .inner()
        .get_latest_checkpoint()?
        .sequence_number;

    if sequence_number > latest_checkpoint {
        return Err(CheckpointNotFoundError::sequence_number(sequence_number).into());
    }

    let mut checkpoint = Checkpoint::default();

    checkpoint.merge(summary, &read_mask);
    checkpoint.merge(signature.clone(), &read_mask);

    if read_mask.contains(Checkpoint::CONTENTS_FIELD.name)
        || read_mask.contains(Checkpoint::TRANSACTIONS_FIELD.name)
        || read_mask.contains(Checkpoint::OBJECTS_FIELD.name)
    {
        let core_contents = service
            .reader
            .inner()
            .get_checkpoint_contents_by_sequence_number(sequence_number)
            .ok_or(CheckpointNotFoundError::sequence_number(sequence_number))?;

        if read_mask.contains(Checkpoint::CONTENTS_FIELD.name) {
            checkpoint.merge(core_contents.clone(), &read_mask);
        }

        if read_mask.contains(Checkpoint::TRANSACTIONS_FIELD.name)
            || read_mask.contains(Checkpoint::OBJECTS_FIELD.name)
        {
            let checkpoint_data = service
                .reader
                .inner()
                .get_checkpoint_data(verified_summary, core_contents)?;

            if let Some(submask) = read_mask
                .subtree(Checkpoint::OBJECTS_FIELD)
                .and_then(|submask| submask.subtree(ObjectSet::OBJECTS_FIELD))
            {
                let set = checkpoint_data
                    .object_set
                    .iter()