Skip to main content

ic_query/nns/governance/source/
mod.rs

1//! Module: nns::governance::source
2//!
3//! Responsibility: define the portable direct NNS Governance source boundary.
4//! Does not own: CLI parsing, persistence, scheduling, retries, or process output.
5//! Boundary: every adapter returns typed data with transport-specific provenance.
6
7#[cfg(all(feature = "canister", target_arch = "wasm32"))]
8pub mod canister;
9#[cfg(feature = "nns-host")]
10pub mod host;
11
12use super::{
13    NnsGovernanceEconomics, NnsGovernanceError, NnsGovernanceMaturityModulation,
14    NnsGovernanceMetrics, NnsGovernanceRequest, NnsGovernanceRewardEvent,
15    NnsGovernanceSourceProvenance,
16};
17use std::{future::Future, pin::Pin};
18
19#[cfg(all(feature = "canister", target_arch = "wasm32"))]
20pub use canister::CanisterNnsSource;
21
22///
23/// NnsGovernanceSourceData
24///
25/// One typed source value and the evidence describing how it was collected.
26///
27
28#[derive(Clone, Debug, Eq, PartialEq)]
29pub struct NnsGovernanceSourceData<T> {
30    /// Typed value returned by Governance.
31    pub value: T,
32    /// Transport-specific source evidence.
33    pub provenance: NnsGovernanceSourceProvenance,
34}
35
36impl<T> NnsGovernanceSourceData<T> {
37    /// Pair a typed value with its source provenance.
38    #[must_use]
39    pub const fn new(value: T, provenance: NnsGovernanceSourceProvenance) -> Self {
40        Self { value, provenance }
41    }
42}
43
44///
45/// NnsGovernanceSourceFuture
46///
47/// Boxed caller-runtime future returned by a direct Governance source.
48///
49
50pub type NnsGovernanceSourceFuture<'a, T> = Pin<
51    Box<dyn Future<Output = Result<NnsGovernanceSourceData<T>, NnsGovernanceError>> + Send + 'a>,
52>;
53
54///
55/// NnsGovernanceSource
56///
57/// Portable async source capability for the four bounded Governance point reports.
58///
59
60pub trait NnsGovernanceSource: Send + Sync {
61    /// Fetch the native network economics parameters.
62    fn fetch_economics<'a>(
63        &'a self,
64        request: &'a NnsGovernanceRequest,
65    ) -> NnsGovernanceSourceFuture<'a, NnsGovernanceEconomics>;
66
67    /// Fetch the native cached Governance metrics.
68    fn fetch_metrics<'a>(
69        &'a self,
70        request: &'a NnsGovernanceRequest,
71    ) -> NnsGovernanceSourceFuture<'a, NnsGovernanceMetrics>;
72
73    /// Fetch the latest native voting reward event.
74    fn fetch_reward_event<'a>(
75        &'a self,
76        request: &'a NnsGovernanceRequest,
77    ) -> NnsGovernanceSourceFuture<'a, NnsGovernanceRewardEvent>;
78
79    /// Fetch the current native maturity modulation when supplied.
80    fn fetch_maturity_modulation<'a>(
81        &'a self,
82        request: &'a NnsGovernanceRequest,
83    ) -> NnsGovernanceSourceFuture<'a, Option<NnsGovernanceMaturityModulation>>;
84}
85
86#[cfg(any(feature = "nns-host", feature = "canister"))]
87pub(super) fn metrics_result(
88    result: super::wire::GetMetricsResult,
89) -> Result<super::wire::GovernanceCachedMetrics, NnsGovernanceError> {
90    match result {
91        super::wire::GetMetricsResult::Ok(metrics) => Ok(*metrics),
92        super::wire::GetMetricsResult::Err(error) => Err(NnsGovernanceError::Governance {
93            error_type: error.error_type,
94            message: error.error_message,
95        }),
96    }
97}