Skip to content

Commit 430c5d4

Browse files
authored
refactor: use router builder (#513)
1 parent a50e23d commit 430c5d4

40 files changed

+1145
-1018
lines changed

Cargo.lock

Lines changed: 113 additions & 245 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ alloy = { version = "=0.5.4", features = [
2929
], default-features = false }
3030
clap = "4.4.3"
3131
lazy_static = "1.4.0"
32-
axum = { version = "0.7.7", default-features = false }
32+
axum = { version = "0.7.9", default-features = false, features = [
33+
"tokio",
34+
"http1",
35+
"http2",
36+
] }
3337
tokio = "1.40"
3438
prometheus = "0.13.3"
3539
anyhow = { version = "1.0.72" }
@@ -72,3 +76,5 @@ thegraph-graphql-http = "0.2.0"
7276
graphql_client = { version = "0.14.0", features = ["reqwest-rustls"] }
7377
bip39 = "2.0.0"
7478
rstest = "0.23.0"
79+
wiremock = "0.6.1"
80+
typed-builder = "0.20.0"

crates/config/src/config.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use crate::NonZeroGRT;
3030

3131
const SHARED_PREFIX: &str = "INDEXER_";
3232

33-
#[derive(Debug, Deserialize, Clone)]
33+
#[derive(Debug, Deserialize)]
3434
#[cfg_attr(test, derive(PartialEq))]
3535
pub struct Config {
3636
pub indexer: IndexerConfig,
@@ -229,14 +229,14 @@ impl Config {
229229
}
230230
}
231231

232-
#[derive(Debug, Deserialize, Clone)]
232+
#[derive(Debug, Deserialize)]
233233
#[cfg_attr(test, derive(PartialEq))]
234234
pub struct IndexerConfig {
235235
pub indexer_address: Address,
236236
pub operator_mnemonic: Mnemonic,
237237
}
238238

239-
#[derive(Clone, Debug, Deserialize)]
239+
#[derive(Debug, Deserialize, Clone)]
240240
#[cfg_attr(test, derive(PartialEq))]
241241
#[serde(untagged)]
242242
#[serde(deny_unknown_fields)]
@@ -278,14 +278,14 @@ impl DatabaseConfig {
278278
}
279279
}
280280

281-
#[derive(Debug, Deserialize, Clone)]
281+
#[derive(Debug, Deserialize)]
282282
#[cfg_attr(test, derive(PartialEq))]
283283
pub struct GraphNodeConfig {
284284
pub query_url: Url,
285285
pub status_url: Url,
286286
}
287287

288-
#[derive(Debug, Deserialize, Clone)]
288+
#[derive(Debug, Deserialize)]
289289
#[cfg_attr(test, derive(PartialEq))]
290290
pub struct MetricsConfig {
291291
pub port: u16,
@@ -297,15 +297,15 @@ impl MetricsConfig {
297297
}
298298
}
299299

300-
#[derive(Debug, Deserialize, Clone)]
300+
#[derive(Debug, Deserialize)]
301301
#[cfg_attr(test, derive(PartialEq))]
302302
pub struct SubgraphsConfig {
303303
pub network: NetworkSubgraphConfig,
304304
pub escrow: EscrowSubgraphConfig,
305305
}
306306

307307
#[serde_as]
308-
#[derive(Debug, Deserialize, Clone)]
308+
#[derive(Debug, Deserialize)]
309309
#[cfg_attr(test, derive(PartialEq))]
310310
pub struct NetworkSubgraphConfig {
311311
#[serde(flatten)]
@@ -315,15 +315,15 @@ pub struct NetworkSubgraphConfig {
315315
pub recently_closed_allocation_buffer_secs: Duration,
316316
}
317317

318-
#[derive(Debug, Deserialize, Clone)]
318+
#[derive(Debug, Deserialize)]
319319
#[cfg_attr(test, derive(PartialEq))]
320320
pub struct EscrowSubgraphConfig {
321321
#[serde(flatten)]
322322
pub config: SubgraphConfig,
323323
}
324324

325325
#[serde_as]
326-
#[derive(Debug, Deserialize, Clone)]
326+
#[derive(Debug, Deserialize)]
327327
#[cfg_attr(test, derive(PartialEq))]
328328
pub struct SubgraphConfig {
329329
pub query_url: Url,
@@ -346,14 +346,14 @@ pub enum TheGraphChainId {
346346
Test = 1337,
347347
}
348348

349-
#[derive(Debug, Deserialize, Clone)]
349+
#[derive(Debug, Deserialize)]
350350
#[cfg_attr(test, derive(PartialEq))]
351351
pub struct BlockchainConfig {
352352
pub chain_id: TheGraphChainId,
353353
pub receipts_verifier_address: Address,
354354
}
355355

356-
#[derive(Debug, Deserialize, Clone)]
356+
#[derive(Debug, Deserialize)]
357357
#[cfg_attr(test, derive(PartialEq))]
358358
pub struct ServiceConfig {
359359
pub serve_network_subgraph: bool,
@@ -366,14 +366,14 @@ pub struct ServiceConfig {
366366
}
367367

368368
#[serde_as]
369-
#[derive(Debug, Deserialize, Clone)]
369+
#[derive(Debug, Deserialize)]
370370
#[cfg_attr(test, derive(PartialEq))]
371371
pub struct ServiceTapConfig {
372372
/// what's the maximum value we accept in a receipt
373373
pub max_receipt_value_grt: NonZeroGRT,
374374
}
375375

376-
#[derive(Debug, Deserialize, Clone)]
376+
#[derive(Debug, Deserialize)]
377377
#[cfg_attr(test, derive(PartialEq))]
378378
pub struct TapConfig {
379379
/// what is the maximum amount the indexer is willing to lose in grt
@@ -383,7 +383,7 @@ pub struct TapConfig {
383383
pub sender_aggregator_endpoints: HashMap<Address, Url>,
384384
}
385385

386-
#[derive(Debug, Deserialize, Clone)]
386+
#[derive(Debug, Deserialize)]
387387
#[cfg_attr(test, derive(PartialEq))]
388388
pub struct DipsConfig {
389389
pub allowed_payers: Vec<Address>,
@@ -402,7 +402,7 @@ impl TapConfig {
402402
}
403403

404404
#[serde_as]
405-
#[derive(Debug, Deserialize, Clone)]
405+
#[derive(Debug, Deserialize)]
406406
#[cfg_attr(test, derive(PartialEq))]
407407
pub struct RavRequestConfig {
408408
/// what divisor of the amount willing to lose to trigger the rav request

crates/monitor/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ bip39.workspace = true
2424
[dev-dependencies]
2525
env_logger = { version = "0.11.0", default-features = false }
2626
test-log = { version = "0.2.12", default-features = false }
27-
wiremock = "0.5.19"
27+
wiremock.workspace = true
2828
test-assets = { path = "../test-assets" }

crates/monitor/src/allocations.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@ use std::{
1212
};
1313
use tokio::sync::watch::Receiver;
1414

15+
/// Receiver of Map between allocation id and allocation struct
16+
pub type AllocationWatcher = Receiver<HashMap<Address, Allocation>>;
17+
1518
/// An always up-to-date list of an indexer's active and recently closed allocations.
1619
pub async fn indexer_allocations(
1720
network_subgraph: &'static SubgraphClient,
1821
indexer_address: Address,
1922
interval: Duration,
2023
recently_closed_allocation_buffer: Duration,
21-
) -> anyhow::Result<Receiver<HashMap<Address, Allocation>>> {
24+
) -> anyhow::Result<AllocationWatcher> {
2225
new_watcher(interval, move || async move {
2326
get_allocations(
2427
network_subgraph,

crates/monitor/src/attestation.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,18 @@ use thegraph_core::{Address, ChainId};
1111
use tokio::sync::watch::Receiver;
1212
use tracing::warn;
1313

14+
use crate::{AllocationWatcher, DisputeManagerWatcher};
15+
16+
/// Receiver for Map of allocation id and attestation signer
17+
pub type AttestationWatcher = Receiver<HashMap<Address, AttestationSigner>>;
18+
1419
/// An always up-to-date list of attestation signers, one for each of the indexer's allocations.
1520
pub fn attestation_signers(
16-
indexer_allocations_rx: Receiver<HashMap<Address, Allocation>>,
21+
indexer_allocations_rx: AllocationWatcher,
1722
indexer_mnemonic: Mnemonic,
1823
chain_id: ChainId,
19-
dispute_manager_rx: Receiver<Address>,
20-
) -> Receiver<HashMap<Address, AttestationSigner>> {
24+
dispute_manager_rx: DisputeManagerWatcher,
25+
) -> AttestationWatcher {
2126
let attestation_signers_map: &'static Mutex<HashMap<Address, AttestationSigner>> =
2227
Box::leak(Box::new(Mutex::new(HashMap::new())));
2328
let indexer_mnemonic = Arc::new(indexer_mnemonic.to_string());

crates/monitor/src/client/subgraph_client.rs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,26 @@ impl DeploymentDetails {
2626
graph_node_base_url: &str,
2727
deployment: DeploymentId,
2828
) -> Result<Self, anyhow::Error> {
29-
Self::for_graph_node_url(
29+
Ok(Self::for_graph_node_url(
3030
Url::parse(graph_node_status_url)?,
3131
Url::parse(graph_node_base_url)?,
3232
deployment,
33-
)
33+
))
3434
}
3535

3636
pub fn for_graph_node_url(
3737
graph_node_status_url: Url,
3838
graph_node_base_url: Url,
3939
deployment: DeploymentId,
40-
) -> Result<Self, anyhow::Error> {
41-
Ok(Self {
40+
) -> Self {
41+
Self {
4242
deployment: Some(deployment),
4343
status_url: Some(graph_node_status_url),
44-
query_url: graph_node_base_url.join(&format!("subgraphs/id/{deployment}"))?,
44+
query_url: graph_node_base_url
45+
.join(&format!("subgraphs/id/{deployment}"))
46+
.expect("Must be correct"),
4547
query_auth_token: None,
46-
})
48+
}
4749
}
4850

4951
pub fn for_query_url(query_url: &str) -> Result<Self, anyhow::Error> {
@@ -55,17 +57,7 @@ impl DeploymentDetails {
5557
})
5658
}
5759

58-
pub fn for_query_url_with_token(
59-
query_url: &str,
60-
query_auth_token: Option<String>,
61-
) -> Result<Self, anyhow::Error> {
62-
Ok(Self::for_query_url_with_token_url(
63-
Url::parse(query_url)?,
64-
query_auth_token,
65-
))
66-
}
67-
68-
pub fn for_query_url_with_token_url(query_url: Url, query_auth_token: Option<String>) -> Self {
60+
pub fn for_query_url_with_token(query_url: Url, query_auth_token: Option<String>) -> Self {
6961
Self {
7062
deployment: None,
7163
status_url: None,

crates/monitor/src/deployment_to_allocation.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@ use std::collections::HashMap;
55
use thegraph_core::{Address, DeploymentId};
66
use tokio::sync::watch::Receiver;
77

8-
use indexer_allocation::Allocation;
98
use indexer_watcher::map_watcher;
109

10+
use crate::AllocationWatcher;
11+
12+
/// Watcher for Map of deployment id and allocation id
13+
pub type DeploymentToAllocationWatcher = Receiver<HashMap<DeploymentId, Address>>;
14+
1115
/// Watcher of indexer allocation
1216
/// returning a map of subgraph deployment to allocation id
1317
pub fn deployment_to_allocation(
14-
indexer_allocations_rx: Receiver<HashMap<Address, Allocation>>,
15-
) -> Receiver<HashMap<DeploymentId, Address>> {
18+
indexer_allocations_rx: AllocationWatcher,
19+
) -> DeploymentToAllocationWatcher {
1620
map_watcher(indexer_allocations_rx, move |allocation| {
1721
allocation
1822
.iter()

crates/monitor/src/dispute_manager.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@ use indexer_watcher::new_watcher;
99
use std::time::Duration;
1010
use tokio::sync::watch::Receiver;
1111

12+
/// Watcher for Dispute Manager Address
13+
pub type DisputeManagerWatcher = Receiver<Address>;
14+
15+
/// Monitors the subgraph for dispute manager address
1216
pub async fn dispute_manager(
1317
network_subgraph: &'static SubgraphClient,
1418
interval: Duration,
15-
) -> anyhow::Result<Receiver<Address>> {
19+
) -> anyhow::Result<DisputeManagerWatcher> {
1620
new_watcher(interval, move || async move {
1721
let response = network_subgraph
1822
.query::<DisputeManager, _>(dispute_manager::Variables {})

crates/monitor/src/escrow_accounts.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,14 @@ impl EscrowAccounts {
8787
}
8888
}
8989

90+
pub type EscrowAccountsWatcher = Receiver<EscrowAccounts>;
91+
9092
pub async fn escrow_accounts(
9193
escrow_subgraph: &'static SubgraphClient,
9294
indexer_address: Address,
9395
interval: Duration,
9496
reject_thawing_signers: bool,
95-
) -> Result<Receiver<EscrowAccounts>, anyhow::Error> {
97+
) -> Result<EscrowAccountsWatcher, anyhow::Error> {
9698
indexer_watcher::new_watcher(interval, move || {
9799
get_escrow_accounts(escrow_subgraph, indexer_address, reject_thawing_signers)
98100
})

0 commit comments

Comments
 (0)