-
Notifications
You must be signed in to change notification settings - Fork 11.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[GraphQL/Config] Config for functional groups
Introduce config for functional groups to enable and disable groups of features in the schema, so that different operators can choose to run the RPC service with different sets of features enabled (for example running public nodes without analytics). The precise set of functional groups may shift -- the current list is derived from the list in the RFC (#13700). This PR only introduces the config, and not the logic in the schema to listen to the functional group config. The config is read from a TOML file whose path is passed as a command-line parameter. It is stored as data in the schema's context so that it can later be accessed by whatever system limits access to endpoints by flags. A stub "experiments" section has also been added as a place to keep ad-hoc experimental flags (to gate in-progress features). This PR also brings the `ServerConfig` (renamed to `ConnectionConfig`) into the same module, and applies a common pattern to ensure there's a single source of truth for default values (the `Default` impl for the config structs). Stack: - #13745 Test Plan: New unit tests: ``` sui-graphql-rpc$ cargo nextest run sui-graphql-rpc$ cargo run ```
- Loading branch information
Showing
9 changed files
with
214 additions
and
72 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use std::collections::BTreeSet; | ||
|
||
use serde::Deserialize; | ||
|
||
use crate::functional_group::FunctionalGroup; | ||
|
||
/// Configuration on connections for the RPC, passed in as command-line arguments. | ||
pub struct ConnectionConfig { | ||
pub(crate) port: u16, | ||
pub(crate) host: String, | ||
pub(crate) rpc_url: String, | ||
} | ||
|
||
/// Configuration on features supported by the RPC, passed in a TOML-based file. | ||
#[derive(Deserialize, Debug, Eq, PartialEq)] | ||
#[serde(rename_all = "kebab-case")] | ||
pub struct ServiceConfig { | ||
#[serde(default)] | ||
pub(crate) enabled_features: BTreeSet<FunctionalGroup>, | ||
|
||
#[serde(default)] | ||
pub(crate) experiments: Experiments, | ||
} | ||
|
||
#[derive(Deserialize, Debug, Eq, PartialEq, Default)] | ||
#[serde(rename_all = "kebab-case")] | ||
pub struct Experiments { | ||
// Add experimental flags here, to provide access to them through-out the GraphQL | ||
// implementation. | ||
#[cfg(test)] | ||
test_flag: bool, | ||
} | ||
|
||
impl ConnectionConfig { | ||
pub fn new(port: Option<u16>, host: Option<String>, rpc_url: Option<String>) -> Self { | ||
let default = Self::default(); | ||
Self { | ||
port: port.unwrap_or(default.port), | ||
host: host.unwrap_or(default.host), | ||
rpc_url: rpc_url.unwrap_or(default.rpc_url), | ||
} | ||
} | ||
} | ||
|
||
impl ServiceConfig { | ||
pub fn read(contents: &str) -> Result<Self, toml::de::Error> { | ||
toml::de::from_str::<Self>(contents) | ||
} | ||
} | ||
|
||
impl Default for ConnectionConfig { | ||
fn default() -> Self { | ||
Self { | ||
port: 8000, | ||
host: "127.0.0.1".to_string(), | ||
rpc_url: "https://fullnode.testnet.sui.io:443/".to_string(), | ||
} | ||
} | ||
} | ||
|
||
impl Default for ServiceConfig { | ||
fn default() -> Self { | ||
use FunctionalGroup as G; | ||
|
||
Self { | ||
enabled_features: BTreeSet::from([ | ||
G::Analytics, | ||
G::Coins, | ||
G::DynamicFields, | ||
G::NameServer, | ||
G::Packages, | ||
G::Subscriptions, | ||
G::SystemState, | ||
]), | ||
experiments: Experiments::default(), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_read_service_config() { | ||
let actual = ServiceConfig::read( | ||
r#" enabled-features = [ | ||
"coins", | ||
"name-server", | ||
] | ||
"#, | ||
) | ||
.unwrap(); | ||
|
||
use FunctionalGroup as G; | ||
let expect = ServiceConfig { | ||
enabled_features: BTreeSet::from([G::Coins, G::NameServer]), | ||
experiments: Experiments::default(), | ||
}; | ||
|
||
assert_eq!(actual, expect) | ||
} | ||
|
||
#[test] | ||
fn test_read_empty_service_config() { | ||
let actual = ServiceConfig::read("").unwrap(); | ||
let expect = ServiceConfig { | ||
enabled_features: BTreeSet::new(), | ||
experiments: Experiments::default(), | ||
}; | ||
assert_eq!(actual, expect); | ||
} | ||
|
||
#[test] | ||
fn test_read_experiemts_in_service_config() { | ||
let actual = ServiceConfig::read( | ||
r#" [experiments] | ||
test-flag = true | ||
"#, | ||
) | ||
.unwrap(); | ||
|
||
let expect = ServiceConfig { | ||
enabled_features: BTreeSet::new(), | ||
experiments: Experiments { test_flag: true }, | ||
}; | ||
|
||
assert_eq!(actual, expect) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use serde::Deserialize; | ||
|
||
/// Logical Groups categorise APIs exposed by GraphQL. Groups can be enabled or disabled based on | ||
/// settings in the RPC's TOML configuration file. | ||
#[derive(Deserialize, Debug, Eq, PartialEq, Ord, PartialOrd)] | ||
#[serde(rename_all = "kebab-case")] | ||
pub(crate) 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. | ||
NameServer, | ||
|
||
/// Struct and function signatures, and popular packages. | ||
Packages, | ||
|
||
/// Transaction and Event subscriptions. | ||
Subscriptions, | ||
|
||
/// Information about the system that changes from epoch to epoch (protocol config, committee, | ||
/// reference gas price). | ||
SystemState, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.