|
| 1 | +// Copyright 2023-, Edge & Node, GraphOps, and Semiotic Labs. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +use std::time::{Duration, SystemTime}; |
| 5 | + |
| 6 | +use alloy::primitives::Address; |
| 7 | +use axum::http::Request; |
| 8 | +use axum_extra::headers::Header; |
| 9 | +use indexer_config::{BlockchainConfig, GraphNodeConfig, IndexerConfig, NonZeroGRT}; |
| 10 | +use indexer_monitor::EscrowAccounts; |
| 11 | +use indexer_service_rs::{ |
| 12 | + service::{ServiceRouter, TapReceipt}, |
| 13 | + QueryBody, |
| 14 | +}; |
| 15 | +use reqwest::{StatusCode, Url}; |
| 16 | +use sqlx::PgPool; |
| 17 | +use test_assets::{create_signed_receipt, INDEXER_ALLOCATIONS, TAP_EIP712_DOMAIN}; |
| 18 | +use tokio::sync::watch; |
| 19 | +use tower::ServiceExt; |
| 20 | +use wiremock::{ |
| 21 | + matchers::{method, path}, |
| 22 | + Mock, MockServer, ResponseTemplate, |
| 23 | +}; |
| 24 | + |
| 25 | +#[sqlx::test(migrations = "../../migrations")] |
| 26 | +async fn full_integration_test(database: PgPool) { |
| 27 | + let http_client = reqwest::Client::builder() |
| 28 | + .tcp_nodelay(true) |
| 29 | + .build() |
| 30 | + .expect("Failed to init HTTP client"); |
| 31 | + |
| 32 | + let allocation = INDEXER_ALLOCATIONS |
| 33 | + .values() |
| 34 | + .collect::<Vec<_>>() |
| 35 | + .pop() |
| 36 | + .unwrap() |
| 37 | + .clone(); |
| 38 | + let deployment = allocation.subgraph_deployment.id; |
| 39 | + |
| 40 | + let mock_server = MockServer::start().await; |
| 41 | + |
| 42 | + let mock = Mock::given(method("POST")) |
| 43 | + .and(path(format!("/subgraphs/id/{deployment}"))) |
| 44 | + .respond_with(ResponseTemplate::new(200).set_body_raw( |
| 45 | + r#" |
| 46 | + { |
| 47 | + "data": { |
| 48 | + "graphNetwork": { |
| 49 | + "currentEpoch": 960 |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + "#, |
| 54 | + "application/json", |
| 55 | + )); |
| 56 | + mock_server.register(mock).await; |
| 57 | + |
| 58 | + let (_escrow_tx, escrow_accounts) = watch::channel(EscrowAccounts::new( |
| 59 | + test_assets::ESCROW_ACCOUNTS_BALANCES.clone(), |
| 60 | + test_assets::ESCROW_ACCOUNTS_SENDERS_TO_SIGNERS.clone(), |
| 61 | + )); |
| 62 | + let (_dispute_tx, dispute_manager) = watch::channel(Address::ZERO); |
| 63 | + |
| 64 | + let (_allocations_tx, allocations) = watch::channel(test_assets::INDEXER_ALLOCATIONS.clone()); |
| 65 | + |
| 66 | + let graph_node_url = Url::parse(&mock_server.uri()).unwrap(); |
| 67 | + |
| 68 | + let router = ServiceRouter::builder() |
| 69 | + .database(database) |
| 70 | + .domain_separator(TAP_EIP712_DOMAIN.clone()) |
| 71 | + .http_client(http_client) |
| 72 | + .graph_node(leak(GraphNodeConfig { |
| 73 | + query_url: graph_node_url.clone(), |
| 74 | + status_url: graph_node_url.clone(), |
| 75 | + })) |
| 76 | + .indexer(leak(IndexerConfig { |
| 77 | + indexer_address: *test_assets::INDEXER_ADDRESS, |
| 78 | + operator_mnemonic: test_assets::INDEXER_MNEMONIC.clone(), |
| 79 | + })) |
| 80 | + .service(leak(indexer_config::ServiceConfig { |
| 81 | + serve_network_subgraph: false, |
| 82 | + serve_escrow_subgraph: false, |
| 83 | + serve_auth_token: None, |
| 84 | + host_and_port: "0.0.0.0:0".parse().unwrap(), |
| 85 | + url_prefix: "/".into(), |
| 86 | + tap: indexer_config::ServiceTapConfig { |
| 87 | + max_receipt_value_grt: NonZeroGRT::new(1000000000000).unwrap(), |
| 88 | + }, |
| 89 | + free_query_auth_token: None, |
| 90 | + })) |
| 91 | + .blockchain(leak(BlockchainConfig { |
| 92 | + chain_id: indexer_config::TheGraphChainId::Test, |
| 93 | + receipts_verifier_address: *test_assets::VERIFIER_ADDRESS, |
| 94 | + })) |
| 95 | + .timestamp_buffer_secs(Duration::from_secs(10)) |
| 96 | + .escrow_accounts(escrow_accounts) |
| 97 | + .dispute_manager(dispute_manager) |
| 98 | + .allocations(allocations) |
| 99 | + .build(); |
| 100 | + |
| 101 | + let app = router.create_router().await.unwrap(); |
| 102 | + |
| 103 | + let timestamp = SystemTime::now() |
| 104 | + .duration_since(SystemTime::UNIX_EPOCH) |
| 105 | + .expect("Time went backwards") |
| 106 | + .as_nanos(); |
| 107 | + |
| 108 | + let receipt = create_signed_receipt(allocation.id, 0, timestamp as u64, 100).await; |
| 109 | + |
| 110 | + let query = QueryBody { |
| 111 | + query: "query".into(), |
| 112 | + variables: None, |
| 113 | + }; |
| 114 | + |
| 115 | + let request = Request::builder() |
| 116 | + .uri(format!("/subgraphs/id/{deployment}")) |
| 117 | + .header(TapReceipt::name(), serde_json::to_string(&receipt).unwrap()) |
| 118 | + .body(serde_json::to_string(&query).unwrap()) |
| 119 | + .unwrap(); |
| 120 | + |
| 121 | + // with deployment |
| 122 | + let res = app.oneshot(request).await.unwrap(); |
| 123 | + assert_eq!(res.status(), StatusCode::OK); |
| 124 | +} |
| 125 | + |
| 126 | +fn leak<T>(thing: T) -> &'static T { |
| 127 | + Box::leak(Box::new(thing)) |
| 128 | +} |
0 commit comments