Skip to content

Commit 5847e5d

Browse files
authored
refactor: align radix custom rpc header with evm implementation (#7110)
1 parent dd4928b commit 5847e5d

12 files changed

Lines changed: 183 additions & 211 deletions

File tree

rust/main/Cargo.lock

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

rust/main/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ members = [
2222
"utils/backtrace-oneline",
2323
"utils/crypto",
2424
"utils/hex",
25+
"utils/reqwest-utils",
2526
"utils/run-locally",
2627
]
2728
resolver = "2"
@@ -190,8 +191,8 @@ radix-engine-interface = { git = "https://github.com/hyperlane-xyz/radixdlt-scry
190191
scrypto = { git = "https://github.com/hyperlane-xyz/radixdlt-scrypto.git", branch = "hyperlane", features = [
191192
"serde",
192193
] }
193-
core-api-client = { git = "https://github.com/hyperlane-xyz/radix-apis-rust-clients.git", branch = "hyperlane" }
194-
gateway-api-client = { git = "https://github.com/hyperlane-xyz/radix-apis-rust-clients.git", branch = "hyperlane" }
194+
core-api-client = { git = "https://github.com/hyperlane-xyz/radix-apis-rust-clients.git", rev = "2d284dad4ef48cab302cfa0435c548b54923b7e1" }
195+
gateway-api-client = { git = "https://github.com/hyperlane-xyz/radix-apis-rust-clients.git", rev = "2d284dad4ef48cab302cfa0435c548b54923b7e1" }
195196
scrypto-derive = { git = "https://github.com/hyperlane-xyz/radixdlt-scrypto.git", branch = "hyperlane" }
196197
sbor = { git = "https://github.com/hyperlane-xyz/radixdlt-scrypto.git", branch = "hyperlane", features = [
197198
"serde",

rust/main/chains/hyperlane-ethereum/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ hyperlane-core = { path = "../../hyperlane-core", features = ["async"] }
3737
hyperlane-metric = { path = "../../hyperlane-metric" }
3838
hyperlane-operation-verifier = { path = "../../applications/hyperlane-operation-verifier" }
3939
hyperlane-warp-route = { path = "../../applications/hyperlane-warp-route" }
40+
reqwest-utils = { path = "../../utils/reqwest-utils" }
4041

4142
[build-dependencies]
4243
abigen = { path = "../../utils/abigen", features = ["ethers"] }

rust/main/chains/hyperlane-ethereum/src/rpc_clients/trait_builder.rs

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use std::fmt::Debug;
2-
use std::str::FromStr;
32
use std::sync::{Arc, OnceLock};
43
use std::time::Duration;
54

@@ -17,8 +16,8 @@ use ethers::types::Address;
1716
use ethers_signers::Signer;
1817
use hyperlane_core::rpc_clients::FallbackProvider;
1918
use hyperlane_metric::utils::url_to_host_info;
20-
use reqwest::header::{HeaderName, HeaderValue};
2119
use reqwest::{Client, Url};
20+
use reqwest_utils::parse_custom_rpc_headers;
2221
use thiserror::Error;
2322

2423
use ethers_prometheus::json_rpc_client::{JsonRpcBlockGetter, PrometheusJsonRpcClient};
@@ -328,50 +327,14 @@ fn get_reqwest_client(url: &Url) -> ChainResult<Client> {
328327
if let Some(client) = client_cache.get(url) {
329328
return Ok(client.clone());
330329
}
331-
let client = build_new_reqwest_client(url.clone())?;
332-
client_cache.insert(url.clone(), client.clone());
333-
Ok(client)
334-
}
335-
336-
/// Builds a new reqwest client with the given URL.
337-
/// Generally `get_reqwest_client` should be used instead of this function,
338-
/// as it caches the client for reuse.
339-
fn build_new_reqwest_client(url: Url) -> ChainResult<Client> {
340-
let mut queries_to_keep = vec![];
341-
let mut headers = reqwest::header::HeaderMap::new();
342-
343-
// A hack to pass custom headers to the provider without
344-
// requiring a bunch of changes to our configuration surface area.
345-
// Any `custom_rpc_header` query parameter is expected to have the value
346-
// format: `header_name:header_value`, will be added to the headers
347-
// of the HTTP client, and removed from the URL params.
348-
let mut updated_url = url.clone();
349-
for (key, value) in url.query_pairs() {
350-
if key != "custom_rpc_header" {
351-
queries_to_keep.push((key.clone(), value.clone()));
352-
continue;
353-
}
354-
if let Some((header_name, header_value)) = value.split_once(':') {
355-
let header_name =
356-
HeaderName::from_str(header_name).map_err(ChainCommunicationError::from_other)?;
357-
let mut header_value =
358-
HeaderValue::from_str(header_value).map_err(ChainCommunicationError::from_other)?;
359-
header_value.set_sensitive(true);
360-
headers.insert(header_name, header_value);
361-
}
362-
}
363-
364-
updated_url
365-
.query_pairs_mut()
366-
.clear()
367-
.extend_pairs(queries_to_keep);
368-
330+
let (headers, _) =
331+
parse_custom_rpc_headers(url).map_err(ChainCommunicationError::from_other)?;
369332
let client = Client::builder()
370333
.timeout(HTTP_CLIENT_TIMEOUT)
371334
.default_headers(headers)
372335
.build()
373336
.map_err(EthereumProviderConnectionError::from)?;
374-
337+
client_cache.insert(url.clone(), client.clone());
375338
Ok(client)
376339
}
377340

rust/main/chains/hyperlane-radix/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ bech32 = { workspace = true }
99
chrono = { workspace = true }
1010
core-api-client = { workspace = true }
1111
crypto = { path = "../../utils/crypto" }
12+
reqwest-utils = { path = "../../utils/reqwest-utils" }
1213
derive-new = { workspace = true }
1314
futures = { workspace = true }
1415
gateway-api-client = { workspace = true }
@@ -26,7 +27,7 @@ radix-common = { workspace = true }
2627
radix-engine-interface = { workspace = true }
2728
radix-transactions = { workspace = true }
2829
regex = { workspace = true }
29-
reqwest = "0.12.15"
30+
reqwest = { workspace = true }
3031
ripemd = { workspace = true }
3132
sbor = { workspace = true, features = ["serde"] }
3233
scrypto = { workspace = true, features = ["serde"] }

rust/main/chains/hyperlane-radix/src/config.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{collections::HashMap, str::FromStr};
1+
use std::str::FromStr;
22

33
use scrypto::network::NetworkDefinition;
44
use url::Url;
@@ -12,31 +12,19 @@ pub struct ConnectionConf {
1212
pub gateway: Vec<Url>,
1313
/// Network definitions
1414
pub network: NetworkDefinition,
15-
/// Core Headers
16-
pub core_header: Vec<HashMap<String, String>>,
17-
/// Gateway Headers
18-
pub gateway_header: Vec<HashMap<String, String>>,
1915
}
2016

2117
impl ConnectionConf {
2218
/// Returns a new Connection Config
23-
pub fn new(
24-
core: Vec<Url>,
25-
gateway: Vec<Url>,
26-
network_name: String,
27-
core_header: Vec<HashMap<String, String>>,
28-
gateway_header: Vec<HashMap<String, String>>,
29-
) -> Self {
19+
pub fn new(core: Vec<Url>, gateway: Vec<Url>, network_name: String) -> Self {
3020
let network = match network_name.as_str() {
3121
"localnet" => NetworkDefinition::localnet(),
3222
_ => NetworkDefinition::from_str(&network_name).unwrap_or(NetworkDefinition::mainnet()),
3323
};
3424

3525
Self {
3626
core,
37-
core_header,
3827
gateway,
39-
gateway_header,
4028
network,
4129
}
4230
}

0 commit comments

Comments
 (0)