Skip to content

Commit fbc2c7d

Browse files
committed
refactor: remove usage of static config references
Signed-off-by: Gustavo Inacio <[email protected]>
1 parent defb43b commit fbc2c7d

File tree

4 files changed

+56
-59
lines changed

4 files changed

+56
-59
lines changed

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/service/src/service.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ pub use tap_receipt_header::TapReceipt;
2727
#[derive(Clone)]
2828
pub struct GraphNodeState {
2929
pub graph_node_client: reqwest::Client,
30-
pub graph_node_status_url: &'static Url,
31-
pub graph_node_query_base_url: &'static Url,
30+
pub graph_node_status_url: Url,
31+
pub graph_node_query_base_url: Url,
3232
}
3333

3434
const HTTP_CLIENT_TIMEOUT: Duration = Duration::from_secs(30);
@@ -39,17 +39,16 @@ pub async fn run() -> anyhow::Result<()> {
3939
let cli = Cli::parse();
4040

4141
// Load the service configuration
42-
let config = Box::leak(Box::new(
43-
Config::parse(indexer_config::ConfigPrefix::Service, cli.config.as_ref()).map_err(|e| {
42+
let config = Config::parse(indexer_config::ConfigPrefix::Service, cli.config.as_ref())
43+
.map_err(|e| {
4444
error!(
4545
"Invalid configuration file `{}`: {}, if a value is missing you can also use \
4646
--config to fill the rest of the values",
4747
cli.config.unwrap_or_default().display(),
4848
e
4949
);
5050
anyhow!(e)
51-
})?,
52-
));
51+
})?;
5352

5453
// Parse basic configurations
5554
build_info::build_info!(fn build_info);
@@ -90,28 +89,30 @@ pub async fn run() -> anyhow::Result<()> {
9089
config.blockchain.receipts_verifier_address,
9190
);
9291

92+
let host_and_port = config.service.host_and_port;
93+
9394
let router = ServiceRouter::builder()
9495
.database(database)
9596
.domain_separator(domain_separator)
96-
.graph_node(&config.graph_node)
97+
.graph_node(config.graph_node)
9798
.http_client(http_client)
9899
.release(release)
99-
.indexer(&config.indexer)
100-
.service(&config.service)
101-
.dips(config.dips.as_ref())
102-
.blockchain(&config.blockchain)
100+
.indexer(config.indexer)
101+
.service(config.service)
102+
.dips(config.dips)
103+
.blockchain(config.blockchain)
103104
.timestamp_buffer_secs(config.tap.rav_request.timestamp_buffer_secs)
104-
.network_subgraph(network_subgraph, &config.subgraphs.network)
105-
.escrow_subgraph(escrow_subgraph, &config.subgraphs.escrow)
105+
.network_subgraph(network_subgraph, config.subgraphs.network)
106+
.escrow_subgraph(escrow_subgraph, config.subgraphs.escrow)
106107
.build();
107108

108109
serve_metrics(config.metrics.get_socket_addr());
109110

110111
info!(
111-
address = %config.service.host_and_port,
112+
address = %host_and_port,
112113
"Serving requests",
113114
);
114-
let listener = TcpListener::bind(&config.service.host_and_port)
115+
let listener = TcpListener::bind(&host_and_port)
115116
.await
116117
.expect("Failed to bind to indexer-service port");
117118

crates/service/src/service/router.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -71,29 +71,29 @@ pub struct ServiceRouter {
7171
release: Option<IndexerServiceRelease>,
7272

7373
// configuration
74-
graph_node: &'static GraphNodeConfig,
75-
indexer: &'static IndexerConfig,
76-
service: &'static ServiceConfig,
77-
blockchain: &'static BlockchainConfig,
74+
graph_node: GraphNodeConfig,
75+
indexer: IndexerConfig,
76+
service: ServiceConfig,
77+
blockchain: BlockchainConfig,
7878
timestamp_buffer_secs: Duration,
7979
#[builder(default)]
80-
dips: Option<&'static DipsConfig>,
80+
dips: Option<DipsConfig>,
8181

8282
// either provide subgraph or watcher
8383
#[builder(default, setter(transform =
8484
|subgraph: &'static SubgraphClient,
85-
config: &'static EscrowSubgraphConfig|
85+
config: EscrowSubgraphConfig|
8686
Some((subgraph, config))))]
87-
escrow_subgraph: Option<(&'static SubgraphClient, &'static EscrowSubgraphConfig)>,
87+
escrow_subgraph: Option<(&'static SubgraphClient, EscrowSubgraphConfig)>,
8888
#[builder(default, setter(strip_option))]
8989
escrow_accounts: Option<EscrowAccountsWatcher>,
9090

9191
// provide network subgraph or allocations + dispute manager
9292
#[builder(default, setter(transform =
9393
|subgraph: &'static SubgraphClient,
94-
config: &'static NetworkSubgraphConfig|
94+
config: NetworkSubgraphConfig|
9595
Some((subgraph, config))))]
96-
network_subgraph: Option<(&'static SubgraphClient, &'static NetworkSubgraphConfig)>,
96+
network_subgraph: Option<(&'static SubgraphClient, NetworkSubgraphConfig)>,
9797
#[builder(default, setter(strip_option))]
9898
allocations: Option<AllocationWatcher>,
9999
#[builder(default, setter(strip_option))]
@@ -142,9 +142,9 @@ impl ServiceRouter {
142142
let dips = match self.dips.as_ref() {
143143
Some(dips_config) => {
144144
let schema = dips::build_schema(
145-
*indexer_address,
145+
indexer_address,
146146
dips_config,
147-
self.blockchain,
147+
&self.blockchain,
148148
agreement_store,
149149
prices,
150150
);
@@ -159,7 +159,7 @@ impl ServiceRouter {
159159
(Some(allocations), _) => allocations,
160160
(_, Some((network_subgraph, network))) => indexer_allocations(
161161
network_subgraph,
162-
*indexer_address,
162+
indexer_address,
163163
network.config.syncing_interval_secs,
164164
network.recently_closed_allocation_buffer_secs,
165165
)
@@ -174,7 +174,7 @@ impl ServiceRouter {
174174
(Some(escrow_account), _) => escrow_account,
175175
(_, Some((escrow_subgraph, escrow))) => escrow_accounts(
176176
escrow_subgraph,
177-
*indexer_address,
177+
indexer_address,
178178
escrow.config.syncing_interval_secs,
179179
true, // Reject thawing signers eagerly
180180
)
@@ -385,21 +385,21 @@ impl ServiceRouter {
385385
};
386386

387387
let operator_address =
388-
Json(serde_json::json!({ "publicKey": public_key(operator_mnemonic)?}));
388+
Json(serde_json::json!({ "publicKey": public_key(&operator_mnemonic)?}));
389389

390390
// Graphnode state
391391
let graphnode_state = GraphNodeState {
392392
graph_node_client: self.http_client,
393-
graph_node_status_url: &self.graph_node.status_url,
394-
graph_node_query_base_url: &self.graph_node.query_url,
393+
graph_node_status_url: self.graph_node.status_url,
394+
graph_node_query_base_url: self.graph_node.query_url,
395395
};
396396

397397
// data layer
398398
let data_routes = Router::new()
399399
.route("/subgraphs/id/:id", post_request_handler)
400400
.with_state(graphnode_state.clone());
401401

402-
let subgraphs_route = Router::new().nest(url_prefix, data_routes);
402+
let subgraphs_route = Router::new().nest(&url_prefix, data_routes);
403403

404404
let misc_routes = Router::new()
405405
.route("/", get("Service is up and running"))

crates/service/tests/router_test.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,15 @@ async fn full_integration_test(database: PgPool) {
6666
.database(database)
6767
.domain_separator(TAP_EIP712_DOMAIN.clone())
6868
.http_client(http_client)
69-
.graph_node(leak(GraphNodeConfig {
69+
.graph_node(GraphNodeConfig {
7070
query_url: graph_node_url.clone(),
7171
status_url: graph_node_url.clone(),
72-
}))
73-
.indexer(leak(IndexerConfig {
72+
})
73+
.indexer(IndexerConfig {
7474
indexer_address: *test_assets::INDEXER_ADDRESS,
7575
operator_mnemonic: test_assets::INDEXER_MNEMONIC.clone(),
76-
}))
77-
.service(leak(indexer_config::ServiceConfig {
76+
})
77+
.service(indexer_config::ServiceConfig {
7878
serve_network_subgraph: false,
7979
serve_escrow_subgraph: false,
8080
serve_auth_token: None,
@@ -84,11 +84,11 @@ async fn full_integration_test(database: PgPool) {
8484
max_receipt_value_grt: NonZeroGRT::new(1000000000000).unwrap(),
8585
},
8686
free_query_auth_token: None,
87-
}))
88-
.blockchain(leak(BlockchainConfig {
87+
})
88+
.blockchain(BlockchainConfig {
8989
chain_id: indexer_config::TheGraphChainId::Test,
9090
receipts_verifier_address: *test_assets::VERIFIER_ADDRESS,
91-
}))
91+
})
9292
.timestamp_buffer_secs(Duration::from_secs(10))
9393
.escrow_accounts(escrow_accounts)
9494
.dispute_manager(dispute_manager)
@@ -127,7 +127,3 @@ async fn full_integration_test(database: PgPool) {
127127

128128
insta::assert_snapshot!(res);
129129
}
130-
131-
fn leak<T>(thing: T) -> &'static T {
132-
Box::leak(Box::new(thing))
133-
}

0 commit comments

Comments
 (0)