sui_graphql_rpc/
functional_group.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use std::collections::BTreeMap;

use async_graphql::*;
use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};
use serde_json as json;

/// Groups of features served by the RPC service.  The GraphQL Service can be configured to enable
/// or disable these features.
#[derive(Enum, Copy, Clone, Serialize, Deserialize, Debug, Eq, PartialEq, Ord, PartialOrd)]
#[serde(rename_all = "kebab-case")]
#[graphql(name = "Feature")]
pub enum FunctionalGroup {
    /// Statistics about how the network was running (TPS, top packages, APY, etc)
    Analytics,

    /// Coin metadata, per-address coin and balance information.
    Coins,

    /// Querying an object's dynamic fields.
    DynamicFields,

    /// SuiNS name and reverse name look-up.
    NameService,

    /// Transaction and Event subscriptions.
    Subscriptions,

    /// Aspects that affect the running of the system that are managed by the
    /// validators either directly, or through system transactions.
    SystemState,

    /// Named packages service (utilizing dotmove package registry).
    MoveRegistry,
}

impl FunctionalGroup {
    /// Name that the group is referred to by in configuration and responses on the GraphQL API.
    /// Not a suitable `Display` implementation because it enquotes the representation.
    pub(crate) fn name(&self) -> String {
        json::ser::to_string(self).expect("Serializing `FunctionalGroup` cannot fail.")
    }

    /// List of all functional groups
    pub(crate) fn all() -> &'static [FunctionalGroup] {
        use FunctionalGroup as G;
        static ALL: &[FunctionalGroup] = &[
            G::Analytics,
            G::Coins,
            G::DynamicFields,
            G::NameService,
            G::Subscriptions,
            G::SystemState,
            G::MoveRegistry,
        ];
        ALL
    }
}

/// Mapping from type and field name in the schema to the functional group it belongs to.
fn functional_groups() -> &'static BTreeMap<(&'static str, &'static str), FunctionalGroup> {
    // TODO: Introduce a macro to declare the functional group for a field and/or type on the
    // appropriate type, field, or function, instead of here.  This may also be able to set the
    // graphql `visible` attribute to control schema visibility by functional groups.

    use FunctionalGroup as G;
    static GROUPS: Lazy<BTreeMap<(&str, &str), FunctionalGroup>> = Lazy::new(|| {
        BTreeMap::from_iter([
            (("Address", "balance"), G::Coins),
            (("Address", "balances"), G::Coins),
            (("Address", "coins"), G::Coins),
            (("Address", "defaultSuinsName"), G::NameService),
            (("Address", "suinsRegistrations"), G::NameService),
            (("Checkpoint", "addressMetrics"), G::Analytics),
            (("Checkpoint", "networkTotalTransactions"), G::Analytics),
            (("Epoch", "protocolConfigs"), G::SystemState),
            (("Epoch", "referenceGasPrice"), G::SystemState),
            (("Epoch", "validatorSet"), G::SystemState),
            (("Object", "balance"), G::Coins),
            (("Object", "balances"), G::Coins),
            (("Object", "coins"), G::Coins),
            (("Object", "defaultSuinsName"), G::NameService),
            (("Object", "dynamicField"), G::DynamicFields),
            (("Object", "dynamicObjectField"), G::DynamicFields),
            (("Object", "dynamicFields"), G::DynamicFields),
            (("Object", "suinsRegistrations"), G::NameService),
            (("Owner", "balance"), G::Coins),
            (("Owner", "balances"), G::Coins),
            (("Owner", "coins"), G::Coins),
            (("Owner", "defaultSuinsName"), G::NameService),
            (("Owner", "dynamicField"), G::DynamicFields),
            (("Owner", "dynamicObjectField"), G::DynamicFields),
            (("Owner", "dynamicFields"), G::DynamicFields),
            (("Owner", "suinsRegistrations"), G::NameService),
            (("Query", "coinMetadata"), G::Coins),
            (("Query", "moveCallMetrics"), G::Analytics),
            (("Query", "networkMetrics"), G::Analytics),
            (("Query", "protocolConfig"), G::SystemState),
            (("Query", "resolveSuinsAddress"), G::NameService),
            (("Query", "packageByName"), G::MoveRegistry),
            (("Query", "typeByName"), G::MoveRegistry),
            (("Subscription", "events"), G::Subscriptions),
            (("Subscription", "transactions"), G::Subscriptions),
            (("SystemStateSummary", "safeMode"), G::SystemState),
            (("SystemStateSummary", "storageFund"), G::SystemState),
            (("SystemStateSummary", "systemParameters"), G::SystemState),
            (("SystemStateSummary", "systemStateVersion"), G::SystemState),
        ])
    });

    Lazy::force(&GROUPS)
}

/// Map a type and field name to a functional group.  If an explicit group does not exist for the
/// field, then it is assumed to be a "core" feature.
pub(crate) fn functional_group(type_: &str, field: &str) -> Option<FunctionalGroup> {
    functional_groups().get(&(type_, field)).copied()
}

#[cfg(test)]
mod tests {
    use std::collections::BTreeSet;

    use async_graphql::registry::Registry;
    use async_graphql::OutputType;

    use crate::types::query::Query;

    use super::*;

    #[test]
    /// Makes sure all the functional groups correspond to real elements of the schema unless they
    /// are explicitly recorded as unimplemented.  Complementarily, makes sure that fields marked as
    /// unimplemented don't appear in the set of unimplemented fields.
    fn test_groups_match_schema() {
        let mut registry = Registry::default();
        Query::create_type_info(&mut registry);

        let unimplemented = BTreeSet::from_iter([
            ("Checkpoint", "addressMetrics"),
            ("Epoch", "protocolConfig"),
            ("Query", "moveCallMetrics"),
            ("Query", "networkMetrics"),
            ("Subscription", "events"),
            ("Subscription", "transactions"),
        ]);

        for (type_, field) in &unimplemented {
            let Some(meta_type) = registry.concrete_type_by_name(type_) else {
                continue;
            };

            let Some(_) = meta_type.field_by_name(field) else {
                continue;
            };

            panic!(
                "Field '{type_}.{field}' is marked as unimplemented in this test, but it's in the \
                 schema.  Fix this by removing it from the `unimplemented` set."
            );
        }

        for (type_, field) in functional_groups().keys() {
            if unimplemented.contains(&(type_, field)) {
                continue;
            }

            let Some(meta_type) = registry.concrete_type_by_name(type_) else {
                panic!("Type '{type_}' from functional group configs does not appear in schema.");
            };

            let Some(_) = meta_type.field_by_name(field) else {
                panic!(
                    "Field '{type_}.{field}' from functional group configs does not appear in \
                     schema."
                );
            };
        }
    }
}