Skip to content

Commit

Permalink
WIP: feat: add serving /network and /escrow subgraphs back in
Browse files Browse the repository at this point in the history
  • Loading branch information
Jannis committed Dec 11, 2023
1 parent 6d520f9 commit d402ea9
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 40 deletions.
4 changes: 4 additions & 0 deletions common/src/indexer_service/http/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ pub struct DatabaseConfig {

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct SubgraphConfig {
#[serde(default)]
pub serve_subgraph: bool,
pub serve_auth_token: Option<String>,

pub deployment: Option<DeploymentId>,
pub query_url: String,
pub syncing_interval: u64,
Expand Down
49 changes: 41 additions & 8 deletions common/src/indexer_service/http/indexer_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use axum::{
error_handling::HandleErrorLayer,
response::{IntoResponse, Response},
routing::{get, post},
BoxError, Json, Router, Server,
BoxError, Extension, Json, Router, Server,
};
use build_info::BuildInfo;
use eventuals::Eventual;
Expand All @@ -31,7 +31,9 @@ use tower_governor::{errors::display_error, governor::GovernorConfigBuilder, Gov
use tracing::info;

use crate::{
indexer_service::http::metrics::IndexerServiceMetrics,
indexer_service::http::{
metrics::IndexerServiceMetrics, static_subgraph::static_subgraph_request_handler,
},
prelude::{
attestation_signers, dispute_manager, escrow_accounts, indexer_allocations,
AttestationSigner, DeploymentDetails, SubgraphClient,
Expand Down Expand Up @@ -83,7 +85,7 @@ where
InvalidRequest(anyhow::Error),
#[error("Error while processing the request: {0}")]
ProcessingError(E),
#[error("No receipt or free query auth token provided")]
#[error("No valid receipt or free query auth token provided")]
Unauthorized,
#[error("Invalid free query auth token")]
InvalidFreeQueryAuthToken,
Expand All @@ -93,6 +95,8 @@ where
FailedToProvideAttestation,
#[error("Failed to provide response")]
FailedToProvideResponse,
#[error("Failed to query subgraph: {0}")]
FailedToQueryStaticSubgraph(anyhow::Error),
}

impl<E> From<&IndexerServiceError<E>> for StatusCode
Expand All @@ -119,6 +123,8 @@ where
InvalidRequest(_) => StatusCode::BAD_REQUEST,
InvalidFreeQueryAuthToken => StatusCode::BAD_REQUEST,
ProcessingError(_) => StatusCode::BAD_REQUEST,

FailedToQueryStaticSubgraph(_) => StatusCode::INTERNAL_SERVER_ERROR,
}
}
}
Expand Down Expand Up @@ -192,7 +198,7 @@ impl IndexerService {
.build()
.expect("Failed to init HTTP client");

let network_subgraph = Box::leak(Box::new(SubgraphClient::new(
let network_subgraph: &'static SubgraphClient = Box::leak(Box::new(SubgraphClient::new(
http_client.clone(),
options
.config
Expand Down Expand Up @@ -234,7 +240,7 @@ impl IndexerService {
dispute_manager,
);

let escrow_subgraph = Box::leak(Box::new(SubgraphClient::new(
let escrow_subgraph: &'static SubgraphClient = Box::leak(Box::new(SubgraphClient::new(
http_client,
options
.config
Expand Down Expand Up @@ -304,7 +310,7 @@ impl IndexerService {
)),
};

let misc_routes = Router::new()
let mut misc_routes = Router::new()
.route("/", get("Service is up and running"))
.route("/version", get(Json(options.release)))
.layer(
Expand All @@ -313,8 +319,35 @@ impl IndexerService {
display_error(e)
}))
.layer(rate_limiter),
)
.with_state(state.clone());
);

if options.config.network_subgraph.serve_subgraph {
info!("Serving network subgraph at /network");

// TODO: Add rate limiting?
misc_routes = misc_routes.route(
"/network",
post(static_subgraph_request_handler::<I>)
.route_layer(Extension(network_subgraph))
.route_layer(Extension(
options.config.network_subgraph.serve_auth_token.clone(),
)),
);
}

if options.config.escrow_subgraph.serve_subgraph {
info!("Serving escrow subgraph at /escrow");

// TODO: Add rate limiting?
misc_routes = misc_routes
.route("/escrow", post(static_subgraph_request_handler::<I>))
.route_layer(Extension(escrow_subgraph))
.route_layer(Extension(
options.config.escrow_subgraph.serve_auth_token.clone(),
));
}

misc_routes = misc_routes.with_state(state.clone());

let data_routes = Router::new()
.route(
Expand Down
1 change: 1 addition & 0 deletions common/src/indexer_service/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod indexer_service;
mod metrics;
mod request_handler;
mod scalar_receipt_header;
mod static_subgraph;

pub use config::{
DatabaseConfig, GraphNetworkConfig, IndexerConfig, IndexerServiceConfig, ServerConfig,
Expand Down
33 changes: 1 addition & 32 deletions service/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,18 @@

use std::path::PathBuf;

use alloy_primitives::Address;
use figment::{
providers::{Format, Toml},
Figment,
};
use indexer_common::indexer_service::http::IndexerServiceConfig;
use serde::{Deserialize, Serialize};
use thegraph::types::DeploymentId;
use serde::Deserialize;

#[derive(Clone, Debug, Deserialize)]
pub struct Config {
// pub receipts: Receipts,
// pub indexer_infrastructure: IndexerInfrastructure,
// pub postgres: Postgres,
// pub network_subgraph: NetworkSubgraph,
// pub escrow_subgraph: EscrowSubgraph,
pub common: IndexerServiceConfig,
}

#[derive(Clone, Debug, Serialize, Deserialize, Default)]
pub struct NetworkSubgraph {
// #[clap(
// long,
// value_name = "serve-network-subgraph",
// env = "SERVE_NETWORK_SUBGRAPH",
// default_value_t = false,
// help = "Whether to serve the network subgraph at /network"
// )]
pub serve_network_subgraph: bool,
}

#[derive(Clone, Debug, Serialize, Deserialize, Default)]
pub struct EscrowSubgraph {
// #[clap(
// long,
// value_name = "serve-escrow-subgraph",
// env = "SERVE_ESCROW_SUBGRAPH",
// default_value_t = false,
// help = "Whether to serve the escrow subgraph at /escrow"
// )]
// pub serve_escrow_subgraph: bool,
}

impl Config {
pub fn load(filename: &PathBuf) -> Result<Self, figment::Error> {
Figment::new().merge(Toml::file(filename)).extract()
Expand Down

0 comments on commit d402ea9

Please sign in to comment.