ic-query 0.92.0

Internet Computer query library for CloudEngine, NNS, SNS, ICRC, system canisters, and public network metadata
Documentation
use crate::nns::inventory::{NnsInventoryInputKind, resolve_nns_inventory_row};

use super::{
    NNS_NODE_INFO_REPORT_SCHEMA_VERSION, NnsInventoryError, NnsInventoryInfoRequest,
    NnsNodeInfoReport, NnsNodeListReport, NnsNodeListRequest, filters::filter_node_list_report,
};
use crate::nns::{
    LiveNnsSource, NnsRegistryInventorySource, inventory::load_or_refresh_nns_inventory_report,
};
use crate::{QueryProgress, progress::IgnoreQueryProgress};

pub async fn build_nns_node_list_report(
    request: &NnsNodeListRequest,
) -> Result<NnsNodeListReport, NnsInventoryError> {
    build_nns_node_list_report_with_source_and_progress(
        request,
        &LiveNnsSource::default(),
        &mut IgnoreQueryProgress,
    )
    .await
}

pub async fn build_nns_node_info_report(
    request: &NnsInventoryInfoRequest,
) -> Result<NnsNodeInfoReport, NnsInventoryError> {
    build_nns_node_info_report_with_source_and_progress(
        request,
        &LiveNnsSource::default(),
        &mut IgnoreQueryProgress,
    )
    .await
}

/// Build the report with a caller-owned source and progress sink.
pub async fn build_nns_node_list_report_with_source_and_progress(
    request: &NnsNodeListRequest,
    source: &dyn NnsRegistryInventorySource,
    progress: &mut dyn QueryProgress,
) -> Result<NnsNodeListReport, NnsInventoryError> {
    let snapshot =
        load_or_refresh_nns_inventory_report(&request.inventory, source, progress).await?;
    let report = NnsNodeListReport {
        schema_version: super::NNS_NODE_LIST_REPORT_SCHEMA_VERSION,
        provenance: snapshot.provenance,
        nodes: snapshot.nodes,
    };
    Ok(filter_node_list_report(report, &request.filters))
}

/// Build the report with a caller-owned source and progress sink.
pub async fn build_nns_node_info_report_with_source_and_progress(
    request: &NnsInventoryInfoRequest,
    source: &dyn NnsRegistryInventorySource,
    progress: &mut dyn QueryProgress,
) -> Result<NnsNodeInfoReport, NnsInventoryError> {
    let list_request = NnsNodeListRequest::new(
        request.cache.clone(),
        request.source_endpoint.clone(),
        request.now_unix_secs,
    );
    let mut report =
        build_nns_node_list_report_with_source_and_progress(&list_request, source, progress)
            .await?;
    let (index, resolved_from) = resolve_node(&report, &request.input)?;
    let node = report.nodes.swap_remove(index);
    Ok(NnsNodeInfoReport {
        schema_version: NNS_NODE_INFO_REPORT_SCHEMA_VERSION,
        input: request.input.clone(),
        resolved_from,
        provenance: report.provenance,
        node,
    })
}

pub(super) fn resolve_node(
    report: &NnsNodeListReport,
    input: &str,
) -> Result<(usize, String), NnsInventoryError> {
    resolve_nns_inventory_row(
        &report.nodes,
        |row| row.node_principal.as_str(),
        input,
        NnsInventoryInputKind::Principal,
        "node_principal",
    )
    .map_err(|error| error.for_collection(super::COLLECTION))
}