Skip to content

Commit

Permalink
fix: remove extra configuration from network subgraph
Browse files Browse the repository at this point in the history
  • Loading branch information
Theodus committed Jul 15, 2024
1 parent 75113e8 commit 0b1c2f0
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 81 deletions.
24 changes: 6 additions & 18 deletions graph-gateway/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ pub struct Config {
/// Minimum indexer-service version that will receive queries
#[serde_as(as = "DisplayFromStr")]
pub min_indexer_version: Version,
/// Network subgraph discovery service configuration
pub network: NetworkSubgraphServiceConfig,
/// Indexers used to query the network subgraph
pub trusted_indexers: Vec<TrustedIndexer>,
/// Check payment state of client (disable for testnets)
pub payment_required: bool,
/// POI blocklist
Expand Down Expand Up @@ -215,27 +215,15 @@ impl From<ProofOfIndexingInfo> for ((DeploymentId, BlockNumber), ProofOfIndexing
}
}

/// Network service configuration.
#[derive(Debug, Deserialize)]
pub struct NetworkSubgraphServiceConfig {
/// Deployment ID of the network subgraph.
pub deployment_id: DeploymentId,
/// List of network subgraph update candidates.
pub indexers: Vec<NetworkSubgraphIndexer>,
}

/// Network subgraph update candidate.
#[serde_as]
#[derive(CustomDebug, Deserialize)]
pub struct NetworkSubgraphIndexer {
/// The indexer's ID.
pub id: Address,
/// The indexer base URL.
pub struct TrustedIndexer {
/// network subgraph endpoint
#[debug(with = std::fmt::Display::fmt)]
#[serde_as(as = "DisplayFromStr")]
pub url: Url,
/// The indexer's free query auth token.
pub auth: String,
/// free query auth token
pub auth: Hidden<String>,
}

/// Load the configuration from a JSON file.
Expand Down
1 change: 1 addition & 0 deletions graph-gateway/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod block_constraints;
pub mod client_query;
pub mod config;
pub mod indexer_client;
pub mod indexers;
pub mod indexing_performance;
Expand Down
26 changes: 3 additions & 23 deletions graph-gateway/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use axum::{
response::Response,
routing, Router,
};
use config::{ApiKeys, ExchangeRateProvider};
use ethers::signers::{Signer, Wallet};
use gateway_framework::{
auth::AuthContext,
Expand All @@ -33,6 +32,7 @@ use gateway_framework::{
};
use graph_gateway::{
client_query::{self, context::Context},
config::{self, ApiKeys, ExchangeRateProvider},
indexer_client::IndexerClient,
indexing_performance::IndexingPerformance,
network::{
Expand All @@ -50,7 +50,7 @@ use secp256k1::SecretKey;
use semver::Version;
use serde_json::json;
use simple_rate_limiter::RateLimiter;
use thegraph_core::types::{attestation, DeploymentId};
use thegraph_core::types::attestation;
use tokio::{
net::TcpListener,
signal::unix::SignalKind,
Expand All @@ -59,8 +59,6 @@ use tokio::{
};
use tower_http::cors::{self, CorsLayer};

mod config;

#[global_allocator]
static ALLOC: snmalloc_rs::SnMalloc = snmalloc_rs::SnMalloc;

Expand Down Expand Up @@ -100,12 +98,7 @@ async fn main() {
conf.attestations.dispute_manager,
)));

// Initialize the network service and wait for the initial network state synchronization
let network_subgraph_client = init_subgraph_client(
http_client.clone(),
conf.network.deployment_id,
conf.network.indexers,
);
let network_subgraph_client = SubgraphClient::new(http_client.clone(), conf.trusted_indexers);
let mut network = match init_network_service(
network_subgraph_client,
conf.l2_gateway.is_some(),
Expand Down Expand Up @@ -416,16 +409,3 @@ fn init_network_service(

Ok(builder.build().spawn())
}

/// Construct the indexers' list based network subgraph client
fn init_subgraph_client(
indexer_http_client: reqwest::Client,
subgraph: DeploymentId,
candidates: Vec<config::NetworkSubgraphIndexer>,
) -> SubgraphClient {
let candidates = candidates
.into_iter()
.map(|indexer| (indexer.id, (indexer.url, indexer.auth)))
.collect::<Vec<_>>();
SubgraphClient::new(indexer_http_client, subgraph, candidates)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,30 @@
//! The client inserts an `Authorization` header with the indexer's free query auth token to
//! authenticate the request with the indexer.

use alloy_primitives::Address;
use inner_client::Client as PaginatedIndexerSubgraphClient;
use serde::Deserialize;
use thegraph_core::types::DeploymentId;
use thegraph_graphql_http::graphql::Document;
use url::Url;

use crate::config::TrustedIndexer;

use super::paginated_client::PaginatedClient;

mod inner_client;
mod queries;

/// Builds a URL to query an indexer's subgraph.
fn indexer_subgraph_url(base: &Url, deployment: &DeploymentId) -> anyhow::Result<Url> {
base.join(&format!("subgraphs/id/{}", deployment))
.map_err(Into::into)
}

/// A client that fetches data from a list of candidate indexers.
///
/// The client will try to fetch data from each candidate in the order they were
/// provided. If a candidate fails to fetch the data, the client will try the
/// next candidate in the list.
/// A client that fetches data from a list of trusted indexers.
///
/// The client will return an error if all candidates fail to fetch the data.
/// The client will try to fetch data from each indexer in the order they are
/// provided. The client will return an error if requests to all idnexers fail.
pub struct Client {
inner: PaginatedIndexerSubgraphClient,
candidates: Vec<SubgraphCandidate>,
indexers: Vec<TrustedIndexer>,
}

impl Client {
pub fn new(
client: reqwest::Client,
subgraph: DeploymentId,
candidates: impl IntoIterator<Item = (Address, (Url, String))>,
) -> Self {
pub fn new(client: reqwest::Client, indexers: Vec<TrustedIndexer>) -> Self {
let inner = PaginatedIndexerSubgraphClient::new(client);
let candidates = candidates
.into_iter()
.filter_map(|(id, (url, auth))| {
let url = indexer_subgraph_url(&url, &subgraph).ok()?;
Some(SubgraphCandidate { id, url, auth })
})
.collect();

Self { inner, candidates }
Self { inner, indexers }
}
}

Expand All @@ -57,7 +35,7 @@ impl PaginatedClient for Client {
where
T: for<'de> Deserialize<'de> + Send,
{
for indexer in self.candidates.iter() {
for indexer in self.indexers.iter() {
let result = self
.inner
.paginated_query(indexer.url.clone(), &indexer.auth, query.clone(), page_size)
Expand All @@ -66,14 +44,14 @@ impl PaginatedClient for Client {
match result {
Err(err) => {
tracing::warn!(
indexer=%indexer.id,
indexer_url=%indexer.url,
error=?err,
"Failed to fetch network subgraph from indexer",
);
}
Ok(result) if result.is_empty() => {
tracing::warn!(
indexer=%indexer.id,
indexer_url=%indexer.url,
"Indexer returned an empty response",
);
}
Expand All @@ -84,10 +62,3 @@ impl PaginatedClient for Client {
Err("no candidate indexers left".to_string())
}
}

#[derive(Debug, Clone)]
struct SubgraphCandidate {
id: Address,
url: Url,
auth: String,
}

0 comments on commit 0b1c2f0

Please sign in to comment.