Skip to content

Commit 490adc8

Browse files
authored
feat: add the ability to set GET parameters in relayer calls (#290)
1 parent ff0838d commit 490adc8

File tree

5 files changed

+50
-2
lines changed

5 files changed

+50
-2
lines changed

benches/pbs/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ fn get_mock_validator(bench: BenchConfig) -> RelayClient {
157157
entry,
158158
id: None,
159159
headers: None,
160+
get_params: None,
160161
enable_timing_games: false,
161162
target_first_request_ms: None,
162163
frequency_get_header_ms: None,

config.example.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ url = "http://0xa1cec75a3f0661e99299274182938151e8433c61a19222347ea1313d839229cb
6666
# Headers to send with each request for this relay
6767
# OPTIONAL
6868
headers = { X-MyCustomHeader = "MyCustomValue" }
69+
# GET parameters to add to each request URL for this relay
70+
# OPTIONAL
71+
get_params = { param1 = "value1", param2 = "value2" }
6972
# Whether to enable timing games, as tuned by `target_first_request_ms` and `frequency_get_header_ms`.
7073
# These values should be carefully chosen for each relay, as each relay has different latency and timing games setups.
7174
# They should only be used by advanced users, and if mis-configured can result in unforeseen effects, e.g. fetching a lower header value,
@@ -130,7 +133,7 @@ timeout_get_header_ms = 900
130133
late_in_slot_time_ms = 1500
131134
# For each mux, one or more [[mux.relays]] can be defined, which will be used for the matching validator pubkeys
132135
# Only the relays defined here will be used, and the relays defined in the main [[relays]] config will be ignored
133-
# The fields specified here are the same as in [[relays]] (headers, enable_timing_games, target_first_request_ms, frequency_get_header_ms)
136+
# The fields specified here are the same as in [[relays]] (headers, get_params, enable_timing_games, target_first_request_ms, frequency_get_header_ms)
134137
[[mux.relays]]
135138
id = "mux-relay-1"
136139
url = "http://0xa119589bb33ef52acbb8116832bec2b58fca590fe5c85eac5d3230b44d5bc09fe73ccd21f88eab31d6de16194d17782e@def.xyz"

crates/common/src/config/pbs.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ pub struct RelayConfig {
4343
pub entry: RelayEntry,
4444
/// Optional headers to send with each request
4545
pub headers: Option<HashMap<String, String>>,
46+
/// Optional GET parameters to add to each request
47+
pub get_params: Option<HashMap<String, String>>,
4648
/// Whether to enable timing games
4749
#[serde(default = "default_bool::<false>")]
4850
pub enable_timing_games: bool,

crates/common/src/pbs/relay.rs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,16 @@ impl RelayClient {
9090

9191
// URL builders
9292
pub fn get_url(&self, path: &str) -> Result<Url, PbsError> {
93-
self.config.entry.url.join(path).map_err(PbsError::UrlParsing)
93+
let mut url = self.config.entry.url.join(path).map_err(PbsError::UrlParsing)?;
94+
95+
if let Some(get_params) = &self.config.get_params {
96+
let mut query_pairs = url.query_pairs_mut();
97+
for (key, value) in get_params {
98+
query_pairs.append_pair(key, value);
99+
}
100+
}
101+
102+
Ok(url)
94103
}
95104
pub fn builder_api_url(&self, path: &str) -> Result<Url, PbsError> {
96105
self.get_url(&format!("{BUILDER_API_PATH}{path}"))
@@ -120,6 +129,8 @@ impl RelayClient {
120129

121130
#[cfg(test)]
122131
mod tests {
132+
use std::collections::HashMap;
133+
123134
use alloy::{
124135
primitives::{hex::FromHex, B256},
125136
rpc::types::beacon::BlsPublicKey,
@@ -172,4 +183,33 @@ mod tests {
172183
expected
173184
);
174185
}
186+
187+
#[test]
188+
fn test_relay_url_with_get_params() {
189+
let slot = 0;
190+
let parent_hash = B256::ZERO;
191+
let validator_pubkey = BlsPublicKey::ZERO;
192+
// Note: HashMap iteration order is not guaranteed, so we can't predict the
193+
// exact order of parameters Instead of hard-coding the order, we'll
194+
// check that both parameters are present in the URL
195+
let url_prefix = format!("http://0xa1cec75a3f0661e99299274182938151e8433c61a19222347ea1313d839229cb4ce4e3e5aa2bdeb71c8fcf1b084963c2@abc.xyz/eth/v1/builder/header/{slot}/{parent_hash}/{validator_pubkey}?");
196+
197+
let mut get_params = HashMap::new();
198+
get_params.insert("param1".to_string(), "value1".to_string());
199+
get_params.insert("param2".to_string(), "value2".to_string());
200+
201+
let relay_config = r#"
202+
{
203+
"url": "http://0xa1cec75a3f0661e99299274182938151e8433c61a19222347ea1313d839229cb4ce4e3e5aa2bdeb71c8fcf1b084963c2@abc.xyz"
204+
}"#;
205+
206+
let mut config = serde_json::from_str::<RelayConfig>(relay_config).unwrap();
207+
config.get_params = Some(get_params);
208+
let relay = RelayClient::new(config).unwrap();
209+
210+
let url = relay.get_header_url(slot, parent_hash, validator_pubkey).unwrap().to_string();
211+
assert!(url.starts_with(&url_prefix));
212+
assert!(url.contains("param1=value1"));
213+
assert!(url.contains("param2=value2"));
214+
}
175215
}

tests/src/utils.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub fn generate_mock_relay(port: u16, pubkey: BlsPublicKey) -> Result<RelayClien
2929
entry,
3030
id: None,
3131
headers: None,
32+
get_params: None,
3233
enable_timing_games: false,
3334
target_first_request_ms: None,
3435
frequency_get_header_ms: None,
@@ -48,6 +49,7 @@ pub fn generate_mock_relay_with_batch_size(
4849
entry,
4950
id: None,
5051
headers: None,
52+
get_params: None,
5153
enable_timing_games: false,
5254
target_first_request_ms: None,
5355
frequency_get_header_ms: None,

0 commit comments

Comments
 (0)