Skip to content

Commit 423f1c2

Browse files
committed
refactor: import indexer-config in service and update field access
1 parent f0e39d3 commit 423f1c2

File tree

5 files changed

+102
-49
lines changed

5 files changed

+102
-49
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

common/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ version = "1.1.0"
44
edition = "2021"
55

66
[dependencies]
7+
indexer-config = { path = "../config" }
78
thiserror.workspace = true
89
async-trait.workspace = true
910
alloy.workspace = true

common/src/indexer_service/http/indexer_service.rs

Lines changed: 78 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ use crate::{
4646
tap::IndexerTapContext,
4747
};
4848

49-
use super::{request_handler::request_handler, IndexerServiceConfig};
49+
use super::request_handler::request_handler;
50+
use indexer_config::Config;
5051

5152
pub trait IndexerServiceResponse {
5253
type Data: IntoResponse;
@@ -172,7 +173,7 @@ where
172173
I: IndexerServiceImpl + Sync + Send + 'static,
173174
{
174175
pub service_impl: I,
175-
pub config: IndexerServiceConfig,
176+
pub config: Config,
176177
pub release: IndexerServiceRelease,
177178
pub url_namespace: &'static str,
178179
pub extra_routes: Router<Arc<IndexerServiceState<I>>>,
@@ -182,7 +183,7 @@ pub struct IndexerServiceState<I>
182183
where
183184
I: IndexerServiceImpl + Sync + Send + 'static,
184185
{
185-
pub config: IndexerServiceConfig,
186+
pub config: Config,
186187
pub attestation_signers: Receiver<HashMap<Address, AttestationSigner>>,
187188
pub tap_manager: Manager<IndexerTapContext>,
188189
pub service_impl: Arc<I>,
@@ -211,7 +212,7 @@ impl IndexerService {
211212
.config
212213
.graph_node
213214
.as_ref()
214-
.zip(options.config.network_subgraph.deployment)
215+
.zip(options.config.subgraphs.network.config.deployment_id)
215216
.map(|(graph_node, deployment)| {
216217
DeploymentDetails::for_graph_node(
217218
&graph_node.status_url,
@@ -221,8 +222,20 @@ impl IndexerService {
221222
})
222223
.transpose()?,
223224
DeploymentDetails::for_query_url_with_token(
224-
&options.config.network_subgraph.query_url,
225-
options.config.network_subgraph.query_auth_token.clone(),
225+
&options
226+
.config
227+
.subgraphs
228+
.network
229+
.config
230+
.query_url
231+
.to_string(),
232+
options
233+
.config
234+
.subgraphs
235+
.network
236+
.config
237+
.query_auth_token
238+
.clone(),
226239
)?,
227240
)));
228241

@@ -233,21 +246,31 @@ impl IndexerService {
233246
let allocations = indexer_allocations(
234247
network_subgraph,
235248
options.config.indexer.indexer_address,
236-
Duration::from_secs(options.config.network_subgraph.syncing_interval),
237249
Duration::from_secs(
238250
options
239251
.config
240-
.network_subgraph
241-
.recently_closed_allocation_buffer_seconds,
252+
.subgraphs
253+
.network
254+
.config
255+
.syncing_interval_secs
256+
.as_secs(),
257+
),
258+
Duration::from_secs(
259+
options
260+
.config
261+
.subgraphs
262+
.network
263+
.recently_closed_allocation_buffer_secs
264+
.as_secs(),
242265
),
243266
);
244267

245268
// Maintain an up-to-date set of attestation signers, one for each
246269
// allocation
247270
let attestation_signers = attestation_signers(
248271
allocations.clone(),
249-
options.config.indexer.operator_mnemonic.clone(),
250-
options.config.graph_network.chain_id,
272+
options.config.indexer.operator_mnemonic.to_string(),
273+
options.config.blockchain.chain_id as u64,
251274
dispute_manager,
252275
)
253276
.await;
@@ -258,7 +281,7 @@ impl IndexerService {
258281
.config
259282
.graph_node
260283
.as_ref()
261-
.zip(options.config.escrow_subgraph.deployment)
284+
.zip(options.config.subgraphs.escrow.config.deployment_id)
262285
.map(|(graph_node, deployment)| {
263286
DeploymentDetails::for_graph_node(
264287
&graph_node.status_url,
@@ -268,15 +291,29 @@ impl IndexerService {
268291
})
269292
.transpose()?,
270293
DeploymentDetails::for_query_url_with_token(
271-
&options.config.escrow_subgraph.query_url,
272-
options.config.escrow_subgraph.query_auth_token.clone(),
294+
&options.config.subgraphs.escrow.config.query_url.to_string(),
295+
options
296+
.config
297+
.subgraphs
298+
.escrow
299+
.config
300+
.query_auth_token
301+
.clone(),
273302
)?,
274303
)));
275304

276305
let escrow_accounts = escrow_accounts(
277306
escrow_subgraph,
278307
options.config.indexer.indexer_address,
279-
Duration::from_secs(options.config.escrow_subgraph.syncing_interval),
308+
Duration::from_secs(
309+
options
310+
.config
311+
.subgraphs
312+
.escrow
313+
.config
314+
.syncing_interval_secs
315+
.as_secs(),
316+
),
280317
true, // Reject thawing signers eagerly
281318
);
282319

@@ -290,19 +327,31 @@ impl IndexerService {
290327
let database = PgPoolOptions::new()
291328
.max_connections(50)
292329
.acquire_timeout(Duration::from_secs(30))
293-
.connect(&options.config.database.postgres_url)
330+
.connect(
331+
&options
332+
.config
333+
.database
334+
.get_formated_postgres_url()
335+
.to_string(),
336+
)
294337
.await?;
295338

296339
let domain_separator = tap_eip712_domain(
297-
options.config.tap.chain_id,
298-
options.config.tap.receipts_verifier_address,
340+
options.config.blockchain.chain_id as u64,
341+
options.config.blockchain.receipts_verifier_address,
299342
);
300343
let indexer_context =
301344
IndexerTapContext::new(database.clone(), domain_separator.clone()).await;
302-
let timestamp_error_tolerance =
303-
Duration::from_secs(options.config.tap.timestamp_error_tolerance);
345+
let timestamp_error_tolerance = Duration::from_secs(
346+
options
347+
.config
348+
.tap
349+
.rav_request
350+
.timestamp_buffer_secs
351+
.as_secs(),
352+
);
304353

305-
let receipt_max_value = options.config.tap.receipt_max_value;
354+
let receipt_max_value = options.config.service.tap.max_receipt_value_grt.get_value();
306355

307356
let checks = IndexerTapContext::get_checks(
308357
database,
@@ -343,7 +392,7 @@ impl IndexerService {
343392
};
344393

345394
let operator_address = Json(
346-
serde_json::json!({ "publicKey": public_key(&options.config.indexer.operator_mnemonic)?}),
395+
serde_json::json!({ "publicKey": public_key(&options.config.indexer.operator_mnemonic.to_string())?}),
347396
);
348397

349398
let mut misc_routes = Router::new()
@@ -365,37 +414,33 @@ impl IndexerService {
365414
)),
366415
};
367416

368-
if options.config.network_subgraph.serve_subgraph {
417+
if options.config.service.serve_network_subgraph {
369418
info!("Serving network subgraph at /network");
370419

371420
misc_routes = misc_routes.route(
372421
"/network",
373422
post(static_subgraph_request_handler::<I>)
374423
.route_layer(Extension(network_subgraph))
375-
.route_layer(Extension(
376-
options.config.network_subgraph.serve_auth_token.clone(),
377-
))
424+
.route_layer(Extension(options.config.service.serve_auth_token.clone()))
378425
.route_layer(static_subgraph_rate_limiter.clone()),
379426
);
380427
}
381428

382-
if options.config.escrow_subgraph.serve_subgraph {
429+
if options.config.service.serve_escrow_subgraph {
383430
info!("Serving escrow subgraph at /escrow");
384431

385432
misc_routes = misc_routes
386433
.route("/escrow", post(static_subgraph_request_handler::<I>))
387434
.route_layer(Extension(escrow_subgraph))
388-
.route_layer(Extension(
389-
options.config.escrow_subgraph.serve_auth_token.clone(),
390-
))
435+
.route_layer(Extension(options.config.service.serve_auth_token.clone()))
391436
.route_layer(static_subgraph_rate_limiter);
392437
}
393438

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

396441
let data_routes = Router::new()
397442
.route(
398-
PathBuf::from(options.config.server.url_prefix)
443+
PathBuf::from(options.config.service.url_prefix)
399444
.join(format!("{}/id/:id", options.url_namespace))
400445
.to_str()
401446
.expect("Failed to set up `/{url_namespace}/id/:id` route"),
@@ -443,10 +488,10 @@ impl IndexerService {
443488
Self::serve_metrics(options.config.server.metrics_host_and_port);
444489

445490
info!(
446-
address = %options.config.server.host_and_port,
491+
address = %options.config.service.host_and_port,
447492
"Serving requests",
448493
);
449-
let listener = TcpListener::bind(&options.config.server.host_and_port)
494+
let listener = TcpListener::bind(&options.config.service.host_and_port)
450495
.await
451496
.expect("Failed to bind to indexer-service port");
452497

config/src/config.rs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@ use figment::{
88
};
99
use serde_repr::Deserialize_repr;
1010
use serde_with::DurationSecondsWithFrac;
11-
use std::{collections::HashMap, net::SocketAddr, path::PathBuf, str::FromStr, time::Duration};
11+
use std::{
12+
collections::HashMap,
13+
net::{Ipv4Addr, SocketAddr, SocketAddrV4},
14+
path::PathBuf,
15+
str::FromStr,
16+
time::Duration,
17+
};
1218
use tracing::warn;
1319

1420
use alloy::primitives::Address;
@@ -24,7 +30,7 @@ use crate::NonZeroGRT;
2430

2531
const SHARED_PREFIX: &str = "INDEXER_";
2632

27-
#[derive(Debug, Deserialize)]
33+
#[derive(Debug, Deserialize, Clone)]
2834
#[cfg_attr(test, derive(PartialEq))]
2935
pub struct Config {
3036
pub indexer: IndexerConfig,
@@ -202,14 +208,14 @@ impl Config {
202208
}
203209
}
204210

205-
#[derive(Debug, Deserialize)]
211+
#[derive(Debug, Deserialize, Clone)]
206212
#[cfg_attr(test, derive(PartialEq))]
207213
pub struct IndexerConfig {
208214
pub indexer_address: Address,
209215
pub operator_mnemonic: Mnemonic,
210216
}
211217

212-
#[derive(Debug, Deserialize)]
218+
#[derive(Debug, Deserialize, Clone)]
213219
#[cfg_attr(test, derive(PartialEq))]
214220
#[serde(untagged)]
215221
#[serde(deny_unknown_fields)]
@@ -251,28 +257,28 @@ impl DatabaseConfig {
251257
}
252258
}
253259

254-
#[derive(Debug, Deserialize)]
260+
#[derive(Debug, Deserialize, Clone)]
255261
#[cfg_attr(test, derive(PartialEq))]
256262
pub struct GraphNodeConfig {
257263
pub query_url: Url,
258264
pub status_url: Url,
259265
}
260266

261-
#[derive(Debug, Deserialize)]
267+
#[derive(Debug, Deserialize, Clone)]
262268
#[cfg_attr(test, derive(PartialEq))]
263269
pub struct MetricsConfig {
264270
pub port: u16,
265271
}
266272

267-
#[derive(Debug, Deserialize)]
273+
#[derive(Debug, Deserialize, Clone)]
268274
#[cfg_attr(test, derive(PartialEq))]
269275
pub struct SubgraphsConfig {
270276
pub network: NetworkSubgraphConfig,
271277
pub escrow: EscrowSubgraphConfig,
272278
}
273279

274280
#[serde_as]
275-
#[derive(Debug, Deserialize)]
281+
#[derive(Debug, Deserialize, Clone)]
276282
#[cfg_attr(test, derive(PartialEq))]
277283
pub struct NetworkSubgraphConfig {
278284
#[serde(flatten)]
@@ -282,15 +288,15 @@ pub struct NetworkSubgraphConfig {
282288
pub recently_closed_allocation_buffer_secs: Duration,
283289
}
284290

285-
#[derive(Debug, Deserialize)]
291+
#[derive(Debug, Deserialize, Clone)]
286292
#[cfg_attr(test, derive(PartialEq))]
287293
pub struct EscrowSubgraphConfig {
288294
#[serde(flatten)]
289295
pub config: SubgraphConfig,
290296
}
291297

292298
#[serde_as]
293-
#[derive(Debug, Deserialize)]
299+
#[derive(Debug, Deserialize, Clone)]
294300
#[cfg_attr(test, derive(PartialEq))]
295301
pub struct SubgraphConfig {
296302
pub query_url: Url,
@@ -313,14 +319,14 @@ pub enum TheGraphChainId {
313319
Test = 1337,
314320
}
315321

316-
#[derive(Debug, Deserialize)]
322+
#[derive(Debug, Deserialize, Clone)]
317323
#[cfg_attr(test, derive(PartialEq))]
318324
pub struct BlockchainConfig {
319325
pub chain_id: TheGraphChainId,
320326
pub receipts_verifier_address: Address,
321327
}
322328

323-
#[derive(Debug, Deserialize)]
329+
#[derive(Debug, Deserialize, Clone)]
324330
#[cfg_attr(test, derive(PartialEq))]
325331
pub struct ServiceConfig {
326332
pub serve_network_subgraph: bool,
@@ -333,14 +339,14 @@ pub struct ServiceConfig {
333339
}
334340

335341
#[serde_as]
336-
#[derive(Debug, Deserialize)]
342+
#[derive(Debug, Deserialize, Clone)]
337343
#[cfg_attr(test, derive(PartialEq))]
338344
pub struct ServiceTapConfig {
339345
/// what's the maximum value we accept in a receipt
340346
pub max_receipt_value_grt: NonZeroGRT,
341347
}
342348

343-
#[derive(Debug, Deserialize)]
349+
#[derive(Debug, Deserialize, Clone)]
344350
#[cfg_attr(test, derive(PartialEq))]
345351
pub struct TapConfig {
346352
/// what is the maximum amount the indexer is willing to lose in grt
@@ -362,7 +368,7 @@ impl TapConfig {
362368
}
363369

364370
#[serde_as]
365-
#[derive(Debug, Deserialize)]
371+
#[derive(Debug, Deserialize, Clone)]
366372
#[cfg_attr(test, derive(PartialEq))]
367373
pub struct RavRequestConfig {
368374
/// what divisor of the amount willing to lose to trigger the rav request

0 commit comments

Comments
 (0)