Skip to content

Commit 0e9c127

Browse files
committed
test: add tap agent test
1 parent 22466dd commit 0e9c127

File tree

2 files changed

+216
-2
lines changed

2 files changed

+216
-2
lines changed

crates/tap-agent/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ pub mod cli;
2121
pub mod database;
2222
pub mod metrics;
2323
pub mod tap;
24-
pub mod tracker;
25-
24+
#[cfg(test)]
25+
pub mod tap_agent_test;
2626
#[cfg(test)]
2727
pub mod test;
28+
pub mod tracker;
Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
// Copyright 2023-, Edge & Node, GraphOps, and Semiotic Labs.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
use bip39::Mnemonic;
5+
use indexer_config::{
6+
Config, DatabaseConfig, EscrowSubgraphConfig, GraphNodeConfig, IndexerConfig,
7+
NetworkSubgraphConfig, NonZeroGRT, RavRequestConfig, SubgraphConfig, SubgraphsConfig,
8+
TapConfig,
9+
};
10+
use indexer_monitor::{indexer_allocations, DeploymentDetails, SubgraphClient};
11+
use ractor::{concurrency::JoinHandle, Actor, ActorRef};
12+
use serde_json::json;
13+
use wiremock::matchers::body_string_contains;
14+
use wiremock::MockGuard;
15+
16+
use crate::agent::{
17+
sender_account::SenderAccountConfig, sender_accounts_manager::SenderAccountsManager,
18+
};
19+
use crate::{
20+
agent::sender_accounts_manager::{SenderAccountsManagerArgs, SenderAccountsManagerMessage},
21+
database, CONFIG, EIP_712_DOMAIN,
22+
};
23+
use std::collections::HashMap;
24+
use std::str::FromStr;
25+
use std::time::Duration;
26+
27+
use indexer_monitor::EscrowAccounts;
28+
29+
use reqwest::Url;
30+
use sqlx::PgPool;
31+
use tap_aggregator::server::run_server;
32+
use test_assets::INDEXER_ALLOCATIONS;
33+
use thegraph_core::alloy::primitives::Address;
34+
use tokio::sync::watch;
35+
36+
use wiremock::{
37+
matchers::{method, path},
38+
Mock, MockServer, ResponseTemplate,
39+
};
40+
41+
async fn mock_escrow_subgraph_empty_response() -> (MockServer, MockGuard) {
42+
let mock_ecrow_subgraph_server: MockServer = MockServer::start().await;
43+
let _mock_ecrow_subgraph = mock_ecrow_subgraph_server
44+
.register_as_scoped(
45+
Mock::given(method("POST"))
46+
.and(body_string_contains("TapTransactions"))
47+
.respond_with(ResponseTemplate::new(200).set_body_json(json!({ "data": {
48+
"transactions": [],
49+
}
50+
}))),
51+
)
52+
.await;
53+
(mock_ecrow_subgraph_server, _mock_ecrow_subgraph)
54+
}
55+
pub async fn start_agent() -> (ActorRef<SenderAccountsManagerMessage>, JoinHandle<()>) {
56+
let allocation = INDEXER_ALLOCATIONS.values().next().unwrap().clone();
57+
let deployment = allocation.subgraph_deployment.id;
58+
59+
let (escrow_subgraph_mock_server, _escrow_subgraph_guard) =
60+
mock_escrow_subgraph_empty_response().await;
61+
let graph_node_mock_server = MockServer::start().await;
62+
let network_subgraph_mock_server = MockServer::start().await;
63+
let graph_node_mock_server = MockServer::start().await;
64+
let mock = Mock::given(method("POST"))
65+
.and(path(format!("/subgraphs/id/{deployment}")))
66+
.respond_with(ResponseTemplate::new(200).set_body_raw(
67+
r#"
68+
{
69+
"data": {
70+
"graphNetwork": {
71+
"currentEpoch": 960
72+
}
73+
}
74+
}
75+
"#,
76+
"application/json",
77+
));
78+
graph_node_mock_server.register(mock).await;
79+
80+
// Start a TAP aggregator server.
81+
let (handle, aggregator_endpoint) = run_server(
82+
0,
83+
test_assets::TAP_SIGNER.0.clone(),
84+
vec![test_assets::TAP_SIGNER.1].into_iter().collect(),
85+
test_assets::TAP_EIP712_DOMAIN.clone(),
86+
100 * 1024,
87+
100 * 1024,
88+
1,
89+
)
90+
.await
91+
.unwrap();
92+
93+
let (_escrow_tx, escrow_accounts) = watch::channel(EscrowAccounts::new(
94+
test_assets::ESCROW_ACCOUNTS_BALANCES.clone(),
95+
test_assets::ESCROW_ACCOUNTS_SENDERS_TO_SIGNERS.clone(),
96+
));
97+
let (_dispute_tx, dispute_manager) = watch::channel(Address::ZERO);
98+
99+
let (_allocations_tx, allocations) = watch::channel(test_assets::INDEXER_ALLOCATIONS.clone());
100+
101+
let graph_node_url = Url::parse(&graph_node_mock_server.uri()).unwrap();
102+
let pool_connection: str = "";
103+
let mut sender_aggregator_endpoints: HashMap<Address, Url> = HashMap::new();
104+
sender_aggregator_endpoints.insert(
105+
test_assets::TAP_SENDER.1,
106+
Url::from_str(&format!("http://{}", aggregator_endpoint)).expect("This should not fail"),
107+
);
108+
let config = Config {
109+
indexer: IndexerConfig {
110+
indexer_address: test_assets::INDEXER_ADDRESS,
111+
operator_mnemonic: test_assets::INDEXER_MNEMONIC.clone(),
112+
},
113+
graph_node: GraphNodeConfig {
114+
status_url: graph_node_url.clone(),
115+
query_url: graph_node_url.clone(),
116+
},
117+
database: DatabaseConfig::PostgresUrl {
118+
postgres_url: pool_connection,
119+
},
120+
subgraphs: SubgraphsConfig {
121+
network: NetworkSubgraphConfig {
122+
config: SubgraphConfig {
123+
query_url: Url::from_str(&network_subgraph_mock_server.uri()).unwrap(),
124+
query_auth_token: None,
125+
deployment_id: None,
126+
syncing_interval_secs: Duration::from_secs(30),
127+
},
128+
recently_closed_allocation_buffer_secs: Duration::from_secs(30),
129+
},
130+
escrow: EscrowSubgraphConfig {
131+
config: SubgraphConfig {
132+
query_url: Url::from_str(&escrow_subgraph_mock_server.uri()).unwrap(),
133+
query_auth_token: None,
134+
deployment_id: None,
135+
syncing_interval_secs: Duration::from_secs(30),
136+
},
137+
},
138+
},
139+
tap: TapConfig {
140+
// TODO: replace with a proper implementation once the gateway registry contract is ready
141+
max_amount_willing_to_lose_grt: NonZeroGRT::new(1).expect("Should be able to convert"),
142+
rav_request: RavRequestConfig {
143+
trigger_value_divisor: todo!(),
144+
timestamp_buffer_secs: todo!(),
145+
request_timeout_secs: todo!(),
146+
max_receipts_per_request: todo!(),
147+
},
148+
sender_timeout_secs: Duration::from_secs(60),
149+
sender_aggregator_endpoints,
150+
},
151+
metrics: todo!(),
152+
blockchain: todo!(),
153+
service: todo!(),
154+
dips: todo!(),
155+
};
156+
let pgpool = database::connect(database.clone()).await;
157+
158+
let http_client = reqwest::Client::new();
159+
160+
let network_subgraph = Box::leak(Box::new(
161+
SubgraphClient::new(
162+
http_client.clone(),
163+
None,
164+
DeploymentDetails::for_query_url(&network_subgraph_mock_server.uri()).unwrap(),
165+
)
166+
.await,
167+
));
168+
169+
let indexer_allocations = indexer_allocations(
170+
network_subgraph,
171+
test_assets::INDEXER_ADDRESS,
172+
Duration::from_secs(30),
173+
Duration::from_secs(30),
174+
)
175+
.await
176+
.expect("Failed to initialize indexer_allocations watcher");
177+
178+
let escrow_subgraph = Box::leak(Box::new(
179+
SubgraphClient::new(
180+
http_client.clone(),
181+
None,
182+
DeploymentDetails::for_query_url(&escrow_subgraph_mock_server.uri()).unwrap(),
183+
)
184+
.await,
185+
));
186+
187+
// let escrow_accounts = escrow_accounts(
188+
// escrow_subgraph,
189+
// test_assets::INDEXER_ADDRESS,
190+
// Duration::from_secs(30),
191+
// false,
192+
// )
193+
// .await
194+
// .expect("Error creating escrow_accounts channel");
195+
196+
let config = Box::leak(Box::new(SenderAccountConfig::from_config(&CONFIG)));
197+
198+
let args = SenderAccountsManagerArgs {
199+
config,
200+
domain_separator: EIP_712_DOMAIN.clone(),
201+
pgpool,
202+
indexer_allocations,
203+
escrow_accounts,
204+
escrow_subgraph,
205+
network_subgraph,
206+
sender_aggregator_endpoints: sender_aggregator_endpoints.clone(),
207+
prefix: None,
208+
};
209+
210+
SenderAccountsManager::spawn(None, SenderAccountsManager, args)
211+
.await
212+
.expect("Failed to start sender accounts manager actor.")
213+
}

0 commit comments

Comments
 (0)