Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

indexer-alt: generalize GraphQLConfig #20460

Merged
merged 3 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 3 additions & 54 deletions crates/sui-default-config/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use std::collections::BTreeSet;

use proc_macro::TokenStream;
use quote::{format_ident, quote};
use syn::{
parse_macro_input, Attribute, Data, DataStruct, DeriveInput, Fields, FieldsNamed, Ident, Meta,
NestedMeta,
};
use syn::{parse_macro_input, Attribute, Data, DataStruct, DeriveInput, Fields, FieldsNamed};

/// Attribute macro to be applied to config-based structs. It ensures that the struct derives serde
/// traits, and `Debug`, that all fields are renamed with "kebab case", and adds a `#[serde(default
Expand Down Expand Up @@ -43,9 +38,6 @@ pub fn DefaultConfig(_attr: TokenStream, input: TokenStream) -> TokenStream {
panic!("Default configs must have named fields.");
};

// Figure out which derives need to be added to meet the criteria of a config struct.
let core_derives = core_derives(&attrs);

// Extract field names once to avoid having to check for their existence multiple times.
let fields_with_names: Vec<_> = named
.iter()
Expand Down Expand Up @@ -73,13 +65,13 @@ pub fn DefaultConfig(_attr: TokenStream, input: TokenStream) -> TokenStream {
quote! {
#[doc(hidden)] #cfg
fn #fn_name() -> #ty {
Self::default().#name
<Self as std::default::Default>::default().#name
}
}
});

TokenStream::from(quote! {
#[derive(#(#core_derives),*)]
#[derive(serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "kebab-case")]
#(#attrs)* #vis #struct_token #ident #generics {
#(#fields),*
Expand All @@ -91,49 +83,6 @@ pub fn DefaultConfig(_attr: TokenStream, input: TokenStream) -> TokenStream {
})
}

/// Return a set of derives that should be added to the struct to make sure it derives all the
/// things we expect from a config, namely `Serialize`, `Deserialize`, and `Debug`.
///
/// We cannot add core derives unconditionally, because they will conflict with existing ones.
fn core_derives(attrs: &[Attribute]) -> BTreeSet<Ident> {
let mut derives = BTreeSet::from_iter([
format_ident!("Serialize"),
format_ident!("Deserialize"),
format_ident!("Debug"),
format_ident!("Clone"),
format_ident!("Eq"),
format_ident!("PartialEq"),
]);

for attr in attrs {
let Ok(Meta::List(list)) = attr.parse_meta() else {
continue;
};

let Some(ident) = list.path.get_ident() else {
continue;
};

if ident != "derive" {
continue;
}

for nested in list.nested {
let NestedMeta::Meta(Meta::Path(path)) = nested else {
continue;
};

let Some(ident) = path.get_ident() else {
continue;
};

derives.remove(ident);
}
}

derives
}

/// Find the attribute that corresponds to a `#[cfg(...)]` annotation, if it exists.
fn extract_cfg(attrs: &[Attribute]) -> Option<&Attribute> {
attrs.iter().find(|attr| {
Expand Down
20 changes: 11 additions & 9 deletions crates/sui-graphql-rpc/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const DEFAULT_PAGE_LIMIT: u16 = 50;

/// The combination of all configurations for the GraphQL service.
#[DefaultConfig]
#[derive(Default)]
#[derive(Clone, Default, Debug)]
pub struct ServerConfig {
pub service: ServiceConfig,
pub connection: ConnectionConfig,
Expand All @@ -42,7 +42,7 @@ pub struct ServerConfig {
/// specific connections between this service and other services, and might differ from instance to
/// instance of the GraphQL service.
#[DefaultConfig]
#[derive(clap::Args, Clone, Eq, PartialEq)]
#[derive(clap::Args, Clone, Eq, PartialEq, Debug)]
pub struct ConnectionConfig {
/// Port to bind the server to
#[clap(short, long, default_value_t = ConnectionConfig::default().port)]
Expand Down Expand Up @@ -72,7 +72,7 @@ pub struct ConnectionConfig {
/// configurations are shared across fleets of the service, i.e. all testnet services will have the
/// same `ServiceConfig`.
#[DefaultConfig]
#[derive(Default)]
#[derive(Clone, Default, Eq, PartialEq, Debug)]
pub struct ServiceConfig {
pub limits: Limits,
pub disabled_features: BTreeSet<FunctionalGroup>,
Expand All @@ -84,6 +84,7 @@ pub struct ServiceConfig {
}

#[DefaultConfig]
#[derive(Clone, Eq, PartialEq, Debug)]
pub struct Limits {
/// Maximum depth of nodes in the requests.
pub max_query_depth: u32,
Expand Down Expand Up @@ -128,15 +129,15 @@ pub struct Limits {
}

#[DefaultConfig]
#[derive(Copy)]
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub struct BackgroundTasksConfig {
/// How often the watermark task checks the indexer database to update the checkpoint and epoch
/// watermarks.
pub watermark_update_ms: u64,
}

#[DefaultConfig]
#[derive(Clone)]
#[derive(Clone, Eq, PartialEq, Debug)]
pub struct MoveRegistryConfig {
pub(crate) external_api_url: Option<String>,
pub(crate) resolution_type: ResolutionType,
Expand Down Expand Up @@ -186,15 +187,15 @@ impl Version {
}

#[DefaultConfig]
#[derive(clap::Args)]
#[derive(clap::Args, Clone, Debug)]
pub struct Ide {
/// The title to display at the top of the web-based GraphiQL IDE.
#[clap(short, long, default_value_t = Ide::default().ide_title)]
pub ide_title: String,
}

#[DefaultConfig]
#[derive(Default)]
#[derive(Clone, Default, Eq, PartialEq, Debug)]
pub struct Experiments {
// Add experimental flags here, to provide access to them through-out the GraphQL
// implementation.
Expand All @@ -203,6 +204,7 @@ pub struct Experiments {
}

#[DefaultConfig]
#[derive(Clone, Debug)]
pub struct InternalFeatureConfig {
pub(crate) query_limits_checker: bool,
pub(crate) directive_checker: bool,
Expand All @@ -216,15 +218,15 @@ pub struct InternalFeatureConfig {
}

#[DefaultConfig]
#[derive(clap::Args, Default)]
#[derive(clap::Args, Clone, Default, Debug)]
pub struct TxExecFullNodeConfig {
/// RPC URL for the fullnode to send transactions to execute and dry-run.
#[clap(long)]
pub(crate) node_rpc_url: Option<String>,
}

#[DefaultConfig]
#[derive(Default)]
#[derive(Clone, Default, Eq, PartialEq, Debug)]
pub struct ZkLoginConfig {
pub env: ZkLoginEnv,
}
Expand Down
20 changes: 11 additions & 9 deletions crates/sui-mvr-graphql-rpc/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const DEFAULT_PAGE_LIMIT: u16 = 50;

/// The combination of all configurations for the GraphQL service.
#[DefaultConfig]
#[derive(Default)]
#[derive(Clone, Default, Debug)]
pub struct ServerConfig {
pub service: ServiceConfig,
pub connection: ConnectionConfig,
Expand All @@ -42,7 +42,7 @@ pub struct ServerConfig {
/// specific connections between this service and other services, and might differ from instance to
/// instance of the GraphQL service.
#[DefaultConfig]
#[derive(clap::Args, Clone, Eq, PartialEq)]
#[derive(clap::Args, Clone, Eq, PartialEq, Debug)]
pub struct ConnectionConfig {
/// Port to bind the server to
#[clap(short, long, default_value_t = ConnectionConfig::default().port)]
Expand Down Expand Up @@ -72,7 +72,7 @@ pub struct ConnectionConfig {
/// configurations are shared across fleets of the service, i.e. all testnet services will have the
/// same `ServiceConfig`.
#[DefaultConfig]
#[derive(Default)]
#[derive(Clone, Default, Eq, PartialEq, Debug)]
pub struct ServiceConfig {
pub limits: Limits,
pub disabled_features: BTreeSet<FunctionalGroup>,
Expand All @@ -84,6 +84,7 @@ pub struct ServiceConfig {
}

#[DefaultConfig]
#[derive(Clone, Eq, PartialEq, Debug)]
pub struct Limits {
/// Maximum depth of nodes in the requests.
pub max_query_depth: u32,
Expand Down Expand Up @@ -128,15 +129,15 @@ pub struct Limits {
}

#[DefaultConfig]
#[derive(Copy)]
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub struct BackgroundTasksConfig {
/// How often the watermark task checks the indexer database to update the checkpoint and epoch
/// watermarks.
pub watermark_update_ms: u64,
}

#[DefaultConfig]
#[derive(Clone)]
#[derive(Clone, Eq, PartialEq, Debug)]
pub struct MoveRegistryConfig {
pub(crate) external_api_url: Option<String>,
pub(crate) resolution_type: ResolutionType,
Expand Down Expand Up @@ -186,15 +187,15 @@ impl Version {
}

#[DefaultConfig]
#[derive(clap::Args)]
#[derive(clap::Args, Clone, Debug)]
pub struct Ide {
/// The title to display at the top of the web-based GraphiQL IDE.
#[clap(short, long, default_value_t = Ide::default().ide_title)]
pub ide_title: String,
}

#[DefaultConfig]
#[derive(Default)]
#[derive(Clone, Default, Eq, PartialEq, Debug)]
pub struct Experiments {
// Add experimental flags here, to provide access to them through-out the GraphQL
// implementation.
Expand All @@ -203,6 +204,7 @@ pub struct Experiments {
}

#[DefaultConfig]
#[derive(Clone, Debug)]
pub struct InternalFeatureConfig {
pub(crate) query_limits_checker: bool,
pub(crate) directive_checker: bool,
Expand All @@ -216,15 +218,15 @@ pub struct InternalFeatureConfig {
}

#[DefaultConfig]
#[derive(clap::Args, Default)]
#[derive(clap::Args, Clone, Default, Debug)]
pub struct TxExecFullNodeConfig {
/// RPC URL for the fullnode to send transactions to execute and dry-run.
#[clap(long)]
pub(crate) node_rpc_url: Option<String>,
}

#[DefaultConfig]
#[derive(Default)]
#[derive(Clone, Default, Eq, PartialEq, Debug)]
pub struct ZkLoginConfig {
pub env: ZkLoginEnv,
}
Expand Down