Skip to main content

ic_query/sns/report/model/reports/neurons/
detail.rs

1//! Module: sns::report::model::reports::neurons::detail
2//!
3//! Responsibility: exact SNS neuron detail, permission evidence, and shared evidence validation.
4//! Does not own: live Governance calls, SNS discovery, or text rendering.
5//! Boundary: preserves variable-size native neuron evidence outside fixed-size list caches.
6
7use super::SnsNeuronRow;
8use crate::{hex::is_lowercase_hex, report::ReportDataSource};
9use serde::{Deserialize as SerdeDeserialize, Serialize};
10use std::collections::HashSet;
11
12///
13/// SnsPolicyObservationStatus
14///
15/// Tri-state result for one observed maturity-conversion policy condition.
16///
17
18#[derive(Clone, Copy, Debug, Eq, PartialEq, SerdeDeserialize, Serialize)]
19#[serde(rename_all = "snake_case")]
20pub enum SnsPolicyObservationStatus {
21    /// Every available value satisfies the observed condition.
22    ObservedSatisfied,
23    /// At least one available value violates the observed condition.
24    Violated,
25    /// Unknown or anomalous evidence prevents a closed-world assessment.
26    Unassessable,
27}
28
29impl SnsPolicyObservationStatus {
30    /// Return the stable report label for this status.
31    #[must_use]
32    pub const fn as_str(self) -> &'static str {
33        match self {
34            Self::ObservedSatisfied => "observed_satisfied",
35            Self::Violated => "violated",
36            Self::Unassessable => "unassessable",
37        }
38    }
39
40    /// Combine two observations, preserving a known violation over uncertainty.
41    #[must_use]
42    pub const fn combine(self, other: Self) -> Self {
43        match (self, other) {
44            (Self::Violated, _) | (_, Self::Violated) => Self::Violated,
45            (Self::Unassessable, _) | (_, Self::Unassessable) => Self::Unassessable,
46            (Self::ObservedSatisfied, Self::ObservedSatisfied) => Self::ObservedSatisfied,
47        }
48    }
49}
50
51///
52/// SnsNeuronPermissionValue
53///
54/// Raw SNS Governance permission code with its current native label.
55///
56
57#[derive(Clone, Debug, Eq, PartialEq, SerdeDeserialize, Serialize)]
58#[serde(deny_unknown_fields)]
59pub struct SnsNeuronPermissionValue {
60    /// Raw integer permission code returned by Governance.
61    pub code: i32,
62    /// Current native permission label, or `unknown` for an unrecognized code.
63    pub name: String,
64}
65
66impl SnsNeuronPermissionValue {
67    /// Construct one permission value from its raw Governance code.
68    #[must_use]
69    pub fn from_code(code: i32) -> Self {
70        Self {
71            code,
72            name: sns_neuron_permission_name(code).to_string(),
73        }
74    }
75}
76
77///
78/// SnsNeuronPermissionRow
79///
80/// Permissions held by one principal on an SNS neuron.
81///
82
83#[derive(Clone, Debug, Eq, PartialEq, SerdeDeserialize, Serialize)]
84#[serde(deny_unknown_fields)]
85pub struct SnsNeuronPermissionRow {
86    /// Canonical principal text when Governance supplied the permission holder.
87    pub principal: Option<String>,
88    /// Raw permission codes and current native labels.
89    pub permission_types: Vec<SnsNeuronPermissionValue>,
90}
91
92///
93/// SnsNeuronAccount
94///
95/// Native optional destination account retained for a pending maturity disbursement.
96///
97
98#[derive(Clone, Debug, Eq, PartialEq, SerdeDeserialize, Serialize)]
99#[serde(deny_unknown_fields)]
100pub struct SnsNeuronAccount {
101    /// Canonical destination owner when Governance supplied one.
102    pub owner: Option<String>,
103    /// Native destination subaccount encoded as lowercase hexadecimal when supplied.
104    pub subaccount_hex: Option<String>,
105}
106
107///
108/// SnsMaturityDisbursementRow
109///
110/// Native maturity disbursement that Governance has not yet finalized.
111///
112
113#[derive(Clone, Debug, Eq, PartialEq, SerdeDeserialize, Serialize)]
114#[serde(deny_unknown_fields)]
115pub struct SnsMaturityDisbursementRow {
116    /// Unix timestamp at which the maturity disbursement was scheduled.
117    pub timestamp_of_disbursement_seconds: u64,
118    /// Raw scheduled amount in e8s.
119    pub amount_e8s: u64,
120    /// Complete optional destination account returned by Governance.
121    pub account_to_disburse_to: Option<SnsNeuronAccount>,
122    /// Unix timestamp at which Governance expects to finalize the disbursement.
123    pub finalize_disbursement_timestamp_seconds: Option<u64>,
124}
125
126///
127/// SnsNeuronFolloweesRow
128///
129/// Legacy function-based followees retained from one SNS neuron.
130///
131
132#[derive(Clone, Debug, Eq, PartialEq, SerdeDeserialize, Serialize)]
133#[serde(deny_unknown_fields)]
134pub struct SnsNeuronFolloweesRow {
135    /// Native nervous-system function identifier.
136    pub function_id: u64,
137    /// Full 32-byte followee neuron identifiers encoded as lowercase hexadecimal.
138    pub followee_neuron_ids: Vec<String>,
139}
140
141///
142/// SnsNeuronFolloweeRow
143///
144/// One native topic-following target and its optional alias.
145///
146
147#[derive(Clone, Debug, Eq, PartialEq, SerdeDeserialize, Serialize)]
148#[serde(deny_unknown_fields)]
149pub struct SnsNeuronFolloweeRow {
150    /// Full followee neuron identifier when Governance supplied one.
151    pub neuron_id: Option<String>,
152    /// Native followee alias when Governance supplied one.
153    pub alias: Option<String>,
154}
155
156///
157/// SnsNeuronTopicFolloweesRow
158///
159/// Native topic-following entry retained from one SNS neuron.
160///
161
162#[derive(Clone, Debug, Eq, PartialEq, SerdeDeserialize, Serialize)]
163#[serde(deny_unknown_fields)]
164pub struct SnsNeuronTopicFolloweesRow {
165    /// Raw topic code used as the native topic-following map key.
166    pub topic_code: i32,
167    /// Current native topic label when Governance supplied a known topic variant.
168    pub topic: Option<String>,
169    /// Native topic followees in response order.
170    pub followees: Vec<SnsNeuronFolloweeRow>,
171}
172
173///
174/// SnsNeuronDetail
175///
176/// Full native detail evidence for exactly one SNS neuron.
177///
178
179#[derive(Clone, Debug, Eq, PartialEq, SerdeDeserialize, Serialize)]
180#[serde(deny_unknown_fields)]
181pub struct SnsNeuronDetail {
182    /// Fixed-size neuron state shared with bounded list and snapshot rows.
183    pub neuron: SnsNeuronRow,
184    /// Every current principal permission entry.
185    pub permissions: Vec<SnsNeuronPermissionRow>,
186    /// Every pending maturity disbursement.
187    pub disburse_maturity_in_progress: Vec<SnsMaturityDisbursementRow>,
188    /// Legacy function-based followees.
189    pub followees: Vec<SnsNeuronFolloweesRow>,
190    /// Topic-based followees when the native optional collection is present.
191    pub topic_followees: Option<Vec<SnsNeuronTopicFolloweesRow>>,
192    /// Observed status for disabling maturity mint conversions through permissions 7 and 8.
193    pub maturity_mint_conversion_observed_disabled: SnsPolicyObservationStatus,
194    /// Observed status for disabling manual maturity staking through permission 9.
195    pub manual_maturity_staking_observed_disabled: SnsPolicyObservationStatus,
196}
197
198impl SnsNeuronDetail {
199    /// Recompute both neuron-local maturity policy observations from raw evidence.
200    #[must_use]
201    pub fn derived_policy_observations(
202        &self,
203    ) -> (SnsPolicyObservationStatus, SnsPolicyObservationStatus) {
204        neuron_policy_observations(
205            &self.permissions,
206            !self.disburse_maturity_in_progress.is_empty(),
207        )
208    }
209}
210
211pub(in crate::sns::report::model) fn neuron_policy_observations(
212    permissions: &[SnsNeuronPermissionRow],
213    has_pending_maturity_disbursement: bool,
214) -> (SnsPolicyObservationStatus, SnsPolicyObservationStatus) {
215    let mut mint = if has_pending_maturity_disbursement {
216        SnsPolicyObservationStatus::Violated
217    } else {
218        SnsPolicyObservationStatus::ObservedSatisfied
219    };
220    let mut staking = if permissions.is_empty() {
221        mint = mint.combine(SnsPolicyObservationStatus::Unassessable);
222        SnsPolicyObservationStatus::Unassessable
223    } else {
224        SnsPolicyObservationStatus::ObservedSatisfied
225    };
226    for permission in permissions {
227        if permission.principal.is_none() || permission.permission_types.is_empty() {
228            mint = mint.combine(SnsPolicyObservationStatus::Unassessable);
229            staking = staking.combine(SnsPolicyObservationStatus::Unassessable);
230        }
231        for code in permission.permission_types.iter().map(|value| value.code) {
232            let (code_mint, code_staking) = permission_code_policy_observations(code);
233            mint = mint.combine(code_mint);
234            staking = staking.combine(code_staking);
235        }
236    }
237    (mint, staking)
238}
239
240pub(in crate::sns::report::model) const fn permission_code_policy_observations(
241    code: i32,
242) -> (SnsPolicyObservationStatus, SnsPolicyObservationStatus) {
243    match code {
244        7 | 8 => (
245            SnsPolicyObservationStatus::Violated,
246            SnsPolicyObservationStatus::ObservedSatisfied,
247        ),
248        9 => (
249            SnsPolicyObservationStatus::ObservedSatisfied,
250            SnsPolicyObservationStatus::Violated,
251        ),
252        0 | 11..=i32::MAX | i32::MIN..=-1 => (
253            SnsPolicyObservationStatus::Unassessable,
254            SnsPolicyObservationStatus::Unassessable,
255        ),
256        1..=6 | 10 => (
257            SnsPolicyObservationStatus::ObservedSatisfied,
258            SnsPolicyObservationStatus::ObservedSatisfied,
259        ),
260    }
261}
262
263///
264/// SnsNeuronDetailReport
265///
266/// Serializable live report for one exact SNS Governance neuron lookup.
267///
268
269#[derive(Clone, Debug, Eq, PartialEq, SerdeDeserialize, Serialize)]
270#[serde(deny_unknown_fields)]
271pub struct SnsNeuronDetailReport {
272    /// Report schema version.
273    pub schema_version: u32,
274    /// Requested IC network identity.
275    pub network: String,
276    /// SNS-W canister used for targeted discovery.
277    pub sns_wasm_canister_id: String,
278    /// Collection timestamp in UTC.
279    pub fetched_at: String,
280    /// IC API endpoint used for discovery and Governance calls.
281    pub source_endpoint: String,
282    /// Collector identity recorded in report provenance.
283    pub fetched_by: String,
284    /// Current SNS-W list position retained as display metadata.
285    pub id: usize,
286    /// Current SNS name retained as display metadata.
287    pub name: String,
288    /// Stable SNS Root canister identity.
289    pub root_canister_id: String,
290    /// Stable SNS Governance canister identity.
291    pub governance_canister_id: String,
292    /// Exact requested neuron identifier.
293    pub neuron_id: String,
294    /// Explicit report data source; exact detail reports are live-only.
295    pub data_source: ReportDataSource,
296    /// Full native neuron detail and derived policy observations.
297    pub detail: SnsNeuronDetail,
298}
299
300/// Return the current native label for one raw SNS neuron permission code.
301#[must_use]
302pub const fn sns_neuron_permission_name(code: i32) -> &'static str {
303    match code {
304        0 => "unspecified",
305        1 => "configure_dissolve_state",
306        2 => "manage_principals",
307        3 => "submit_proposal",
308        4 => "vote",
309        5 => "disburse",
310        6 => "split",
311        7 => "merge_maturity",
312        8 => "disburse_maturity",
313        9 => "stake_maturity",
314        10 => "manage_voting_permission",
315        _ => "unknown",
316    }
317}
318
319pub(in crate::sns::report) fn validate_neuron_permissions(
320    permissions: &[SnsNeuronPermissionRow],
321    require_principal: bool,
322) -> Result<(), String> {
323    let mut principals = HashSet::new();
324    for permission in permissions {
325        let principal = match permission.principal.as_deref() {
326            Some(principal) => {
327                validate_canonical_principal(principal, "permission principal")?;
328                if !principals.insert(principal) {
329                    return Err(format!("duplicate permission principal {principal}"));
330                }
331                principal
332            }
333            None if require_principal => {
334                return Err("permission principal is missing".to_string());
335            }
336            None => "missing principal",
337        };
338        let mut codes = HashSet::new();
339        for value in &permission.permission_types {
340            if value.name != sns_neuron_permission_name(value.code) {
341                return Err(format!(
342                    "permission code {} has label {}, expected {}",
343                    value.code,
344                    value.name,
345                    sns_neuron_permission_name(value.code)
346                ));
347            }
348            if !codes.insert(value.code) {
349                return Err(format!(
350                    "{principal} contains duplicate permission code {}",
351                    value.code
352                ));
353            }
354        }
355    }
356    Ok(())
357}
358
359pub(in crate::sns::report) fn validate_maturity_disbursements(
360    disbursements: &[SnsMaturityDisbursementRow],
361) -> Result<(), String> {
362    for disbursement in disbursements {
363        if let Some(account) = disbursement.account_to_disburse_to.as_ref() {
364            if let Some(owner) = account.owner.as_deref() {
365                validate_canonical_principal(owner, "pending disbursement account owner")?;
366            }
367            if let Some(subaccount) = account.subaccount_hex.as_deref()
368                && (subaccount.len() != 64 || !is_lowercase_hex(subaccount))
369            {
370                return Err(
371                    "pending disbursement subaccount is not 32-byte lowercase hexadecimal text"
372                        .to_string(),
373                );
374            }
375        }
376    }
377    Ok(())
378}
379
380pub(super) fn validate_canonical_principal(value: &str, field: &str) -> Result<(), String> {
381    crate::principal_text::validate_canonical(
382        value,
383        |reason| format!("{field} {value} is invalid: {reason}"),
384        |_| format!("{field} {value} is not canonical principal text"),
385    )
386}
387
388#[cfg(test)]
389mod tests {
390    use super::*;
391
392    #[test]
393    fn permission_names_cover_native_and_unknown_codes() {
394        for (code, expected) in [
395            (0, "unspecified"),
396            (1, "configure_dissolve_state"),
397            (2, "manage_principals"),
398            (3, "submit_proposal"),
399            (4, "vote"),
400            (5, "disburse"),
401            (6, "split"),
402            (7, "merge_maturity"),
403            (8, "disburse_maturity"),
404            (9, "stake_maturity"),
405            (10, "manage_voting_permission"),
406            (11, "unknown"),
407            (-1, "unknown"),
408        ] {
409            assert_eq!(sns_neuron_permission_name(code), expected, "code {code}");
410        }
411    }
412
413    #[test]
414    fn policy_observations_fail_closed_and_prioritize_known_violations() {
415        let mut detail = SnsNeuronDetail {
416            neuron: SnsNeuronRow {
417                neuron_id: "00".repeat(32),
418                cached_neuron_stake_e8s: 0,
419                maturity_e8s_equivalent: 0,
420                staked_maturity_e8s_equivalent: None,
421                created_timestamp_seconds: 0,
422                created_at: "1970-01-01T00:00:00Z".to_string(),
423                source_nns_neuron_id: None,
424                auto_stake_maturity: None,
425                aging_since_timestamp_seconds: 0,
426                dissolve_state: None,
427                voting_power_percentage_multiplier: 100,
428                vesting_period_seconds: None,
429                neuron_fees_e8s: 0,
430            },
431            permissions: vec![SnsNeuronPermissionRow {
432                principal: Some("aaaaa-aa".to_string()),
433                permission_types: vec![SnsNeuronPermissionValue::from_code(11)],
434            }],
435            disburse_maturity_in_progress: Vec::new(),
436            followees: Vec::new(),
437            topic_followees: None,
438            maturity_mint_conversion_observed_disabled:
439                SnsPolicyObservationStatus::ObservedSatisfied,
440            manual_maturity_staking_observed_disabled:
441                SnsPolicyObservationStatus::ObservedSatisfied,
442        };
443
444        assert_eq!(
445            detail.derived_policy_observations(),
446            (
447                SnsPolicyObservationStatus::Unassessable,
448                SnsPolicyObservationStatus::Unassessable,
449            )
450        );
451
452        detail.permissions[0]
453            .permission_types
454            .push(SnsNeuronPermissionValue::from_code(7));
455        assert_eq!(
456            detail.derived_policy_observations(),
457            (
458                SnsPolicyObservationStatus::Violated,
459                SnsPolicyObservationStatus::Unassessable,
460            )
461        );
462    }
463}