Skip to content

Commit ebe3f7c

Browse files
authored
refactor: remove clones and use static config in service (#420)
1 parent ff06fa6 commit ebe3f7c

File tree

3 files changed

+15
-14
lines changed

3 files changed

+15
-14
lines changed

common/src/address.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub fn build_wallet(value: &str) -> Result<PrivateKeySigner, LocalSignerError> {
1414
}
1515

1616
// Format public key to a String
17-
pub fn public_key(value: Mnemonic) -> Result<String, LocalSignerError> {
17+
pub fn public_key(value: &Mnemonic) -> Result<String, LocalSignerError> {
1818
let wallet = build_wallet(&value.to_string())?;
1919
let addr = format!("{:?}", wallet.address());
2020
Ok(addr)

common/src/indexer_service/http/indexer_service.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ where
173173
I: IndexerServiceImpl + Sync + Send + 'static,
174174
{
175175
pub service_impl: I,
176-
pub config: Config,
176+
pub config: &'static Config,
177177
pub release: IndexerServiceRelease,
178178
pub url_namespace: &'static str,
179179
pub extra_routes: Router<Arc<IndexerServiceState<I>>>,
@@ -352,7 +352,7 @@ impl IndexerService {
352352
};
353353

354354
let operator_address = Json(
355-
serde_json::json!({ "publicKey": public_key(options.config.indexer.operator_mnemonic)?}),
355+
serde_json::json!({ "publicKey": public_key(&options.config.indexer.operator_mnemonic)?}),
356356
);
357357

358358
let mut misc_routes = Router::new()
@@ -400,7 +400,7 @@ impl IndexerService {
400400

401401
let data_routes = Router::new()
402402
.route(
403-
PathBuf::from(options.config.service.url_prefix)
403+
PathBuf::from(&options.config.service.url_prefix)
404404
.join(format!("{}/id/:id", options.url_namespace))
405405
.to_str()
406406
.expect("Failed to set up `/{url_namespace}/id/:id` route"),

service/src/service.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ impl IndexerServiceResponse for SubgraphServiceResponse {
6161
}
6262

6363
pub struct SubgraphServiceState {
64-
pub config: Config,
64+
pub config: &'static Config,
6565
pub database: PgPool,
6666
pub cost_schema: routes::cost::CostSchema,
6767
pub graph_node_client: reqwest::Client,
68-
pub graph_node_status_url: Url,
69-
pub graph_node_query_base_url: Url,
68+
pub graph_node_status_url: &'static Url,
69+
pub graph_node_query_base_url: &'static Url,
7070
}
7171

7272
struct SubgraphService {
@@ -130,16 +130,17 @@ pub async fn run() -> anyhow::Result<()> {
130130
// Load the json-rpc service configuration, which is a combination of the
131131
// general configuration options for any indexer service and specific
132132
// options added for JSON-RPC
133-
let config = Config::parse(indexer_config::ConfigPrefix::Service, cli.config.as_ref())
134-
.map_err(|e| {
133+
let config = Box::leak(Box::new(
134+
Config::parse(indexer_config::ConfigPrefix::Service, cli.config.as_ref()).map_err(|e| {
135135
error!(
136136
"Invalid configuration file `{}`: {}, if a value is missing you can also use \
137137
--config to fill the rest of the values",
138138
cli.config.unwrap_or_default().display(),
139139
e
140140
);
141141
anyhow!(e)
142-
})?;
142+
})?,
143+
));
143144

144145
// Parse basic configurations
145146
build_info::build_info!(fn build_info);
@@ -149,7 +150,7 @@ pub async fn run() -> anyhow::Result<()> {
149150
// "state", which will be passed to any request handler, middleware etc.
150151
// that is involved in serving requests
151152
let state = Arc::new(SubgraphServiceState {
152-
config: config.clone(),
153+
config,
153154
database: database::connect(config.database.clone().get_formated_postgres_url().as_ref())
154155
.await,
155156
cost_schema: routes::cost::build_schema().await,
@@ -158,13 +159,13 @@ pub async fn run() -> anyhow::Result<()> {
158159
.timeout(Duration::from_secs(30))
159160
.build()
160161
.expect("Failed to init HTTP client for Graph Node"),
161-
graph_node_status_url: config.graph_node.status_url.clone(),
162-
graph_node_query_base_url: config.graph_node.query_url.clone(),
162+
graph_node_status_url: &config.graph_node.status_url,
163+
graph_node_query_base_url: &config.graph_node.query_url,
163164
});
164165

165166
IndexerService::run(IndexerServiceOptions {
166167
release,
167-
config: config.clone(),
168+
config,
168169
url_namespace: "subgraphs",
169170
service_impl: SubgraphService::new(state.clone()),
170171
extra_routes: Router::new()

0 commit comments

Comments
 (0)