From 74ad259e27ec84de745db36f747e091e827c2924 Mon Sep 17 00:00:00 2001 From: Jannis Pohlmann Date: Thu, 12 Oct 2023 16:05:29 +0200 Subject: [PATCH] refactor: type network subgraph deployment better --- common/src/allocations/monitor.rs | 8 ++++---- common/src/network_subgraph/mod.rs | 20 ++++++++++++++------ common/src/test_vectors.rs | 4 ++-- service/src/main.rs | 5 ++++- 4 files changed, 24 insertions(+), 13 deletions(-) diff --git a/common/src/allocations/monitor.rs b/common/src/allocations/monitor.rs index abb92dd8..d7417a18 100644 --- a/common/src/allocations/monitor.rs +++ b/common/src/allocations/monitor.rs @@ -210,11 +210,11 @@ mod test { let mock_server = MockServer::start().await; let network_subgraph_endpoint = NetworkSubgraph::local_deployment_endpoint( &mock_server.uri(), - test_vectors::NETWORK_SUBGRAPH_ID, + &test_vectors::NETWORK_SUBGRAPH_DEPLOYMENT, ); let network_subgraph = NetworkSubgraph::new( Some(&mock_server.uri()), - Some(test_vectors::NETWORK_SUBGRAPH_ID), + Some(&test_vectors::NETWORK_SUBGRAPH_DEPLOYMENT), network_subgraph_endpoint.as_ref(), ); @@ -224,7 +224,7 @@ mod test { Mock::given(method("POST")) .and(path(format!( "/subgraphs/id/{}", - test_vectors::NETWORK_SUBGRAPH_ID + *test_vectors::NETWORK_SUBGRAPH_DEPLOYMENT ))) .and(body_string_contains("currentEpoch")) .respond_with(ResponseTemplate::new(200).set_body_json( @@ -239,7 +239,7 @@ mod test { Mock::given(method("POST")) .and(path(format!( "/subgraphs/id/{}", - test_vectors::NETWORK_SUBGRAPH_ID + *test_vectors::NETWORK_SUBGRAPH_DEPLOYMENT ))) .and(body_string_contains("activeAllocations")) .respond_with(ResponseTemplate::new(200).set_body_raw( diff --git a/common/src/network_subgraph/mod.rs b/common/src/network_subgraph/mod.rs index 6b8d852f..196ce1ce 100644 --- a/common/src/network_subgraph/mod.rs +++ b/common/src/network_subgraph/mod.rs @@ -7,6 +7,7 @@ use graphql::http::Response; use reqwest::{header, Client, Url}; use serde::de::Deserialize; use serde_json::Value; +use toolshed::thegraph::DeploymentId; /// Network subgraph query wrapper /// @@ -20,7 +21,7 @@ pub struct NetworkSubgraph { impl NetworkSubgraph { pub fn new( graph_node_query_endpoint: Option<&str>, - deployment: Option<&str>, + deployment: Option<&DeploymentId>, network_subgraph_url: &str, ) -> NetworkSubgraph { //TODO: Check indexing status of the local network subgraph deployment @@ -46,10 +47,13 @@ impl NetworkSubgraph { } } - pub fn local_deployment_endpoint(graph_node_query_endpoint: &str, deployment: &str) -> Url { + pub fn local_deployment_endpoint( + graph_node_query_endpoint: &str, + deployment: &DeploymentId, + ) -> Url { Url::parse(graph_node_query_endpoint) .and_then(|u| u.join("/subgraphs/id/")) - .and_then(|u| u.join(deployment)) + .and_then(|u| u.join(&deployment.to_string())) .expect("Could not parse graph node query endpoint for the network subgraph deployment") } @@ -75,17 +79,21 @@ mod test { use wiremock::matchers::{method, path}; use wiremock::{Mock, MockServer, ResponseTemplate}; + use crate::test_vectors; + use super::*; const GRAPH_NODE_STATUS_ENDPOINT: &str = "http://localhost:8000/"; - const NETWORK_SUBGRAPH_ID: &str = "QmV614UpBCpuusv5MsismmPYu4KqLtdeNMKpiNrX56kw6u"; const NETWORK_SUBGRAPH_URL: &str = "https://api.thegraph.com/subgraphs/name/graphprotocol/graph-network-goerli"; async fn mock_graph_node_server() -> MockServer { let mock_server = MockServer::start().await; let mock = Mock::given(method("POST")) - .and(path("/subgraphs/id/".to_string() + NETWORK_SUBGRAPH_ID)) + .and(path(format!( + "/subgraphs/id/{}", + *test_vectors::NETWORK_SUBGRAPH_DEPLOYMENT + ))) .respond_with(ResponseTemplate::new(200).set_body_raw( r#" { @@ -106,7 +114,7 @@ mod test { fn network_subgraph() -> NetworkSubgraph { NetworkSubgraph::new( Some(GRAPH_NODE_STATUS_ENDPOINT), - Some(NETWORK_SUBGRAPH_ID), + Some(&test_vectors::NETWORK_SUBGRAPH_DEPLOYMENT), NETWORK_SUBGRAPH_URL, ) } diff --git a/common/src/test_vectors.rs b/common/src/test_vectors.rs index 36c48292..d3529fa6 100644 --- a/common/src/test_vectors.rs +++ b/common/src/test_vectors.rs @@ -10,8 +10,6 @@ use toolshed::thegraph::DeploymentId; use crate::prelude::{Allocation, AllocationStatus, SubgraphDeployment}; -pub const NETWORK_SUBGRAPH_ID: &str = "QmU7zqJyHSyUP3yFii8sBtHT8FaJn2WmUnRvwjAUTjwMBP"; - /// The allocation IDs below are generated using the mnemonic /// "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about" /// and the following epoch and index: @@ -104,6 +102,8 @@ pub const ALLOCATIONS_QUERY_RESPONSE: &str = r#" "#; lazy_static! { + pub static ref NETWORK_SUBGRAPH_DEPLOYMENT: DeploymentId = DeploymentId::from_str("QmU7zqJyHSyUP3yFii8sBtHT8FaJn2WmUnRvwjAUTjwMBP").unwrap(); + pub static ref INDEXER_OPERATOR_MNEMONIC: String = String::from( "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about", ); diff --git a/service/src/main.rs b/service/src/main.rs index 1653dae9..e91f36b0 100644 --- a/service/src/main.rs +++ b/service/src/main.rs @@ -72,7 +72,10 @@ async fn main() -> Result<(), std::io::Error> { config .network_subgraph .network_subgraph_deployment - .as_deref(), + .map(|s| DeploymentId::from_str(&s)) + .transpose() + .expect("Failed to parse invalid network subgraph deployment") + .as_ref(), &config.network_subgraph.network_subgraph_endpoint, )));