Skip to content

Commit 05d11b8

Browse files
committed
Added simple x-timeout-ms header support to get_header
1 parent 77a5ac5 commit 05d11b8

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

crates/common/src/pbs/constants.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub const RELOAD_PATH: &str = "/reload";
1616
pub const HEADER_VERSION_KEY: &str = "X-CommitBoost-Version";
1717
pub const HEADER_VERSION_VALUE: &str = COMMIT_BOOST_VERSION;
1818
pub const HEADER_START_TIME_UNIX_MS: &str = "Date-Milliseconds";
19+
pub const HEADER_TIMEOUT_MS: &str = "X-Timeout-Ms";
1920

2021
pub const BUILDER_EVENTS_PATH: &str = "/builder_events";
2122
pub const DEFAULT_PBS_JWT_KEY: &str = "DEFAULT_PBS";

crates/pbs/src/mev_boost/get_header.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use cb_common::{
1414
pbs::{
1515
error::{PbsError, ValidationError},
1616
GetHeaderParams, GetHeaderResponse, RelayClient, VersionedResponse, EMPTY_TX_ROOT_HASH,
17-
HEADER_START_TIME_UNIX_MS,
17+
HEADER_START_TIME_UNIX_MS, HEADER_TIMEOUT_MS,
1818
},
1919
signature::verify_signed_message,
2020
types::{BlsPublicKey, BlsSignature, Chain},
@@ -81,6 +81,31 @@ pub async fn get_header<S: BuilderApiState>(
8181
return Ok(None);
8282
}
8383

84+
// Use the minimum of the time left and the user provided timeout header
85+
let max_timeout_ms = match req_headers.get(HEADER_TIMEOUT_MS) {
86+
Some(user_header_value) => match user_header_value.to_str() {
87+
Ok(user_timeout_string) => match user_timeout_string.parse::<u64>() {
88+
Ok(user_timeout) => {
89+
if user_timeout == 0 {
90+
warn!("user-supplied timeout header is 0, using {max_timeout_ms}ms");
91+
max_timeout_ms
92+
} else {
93+
user_timeout.min(max_timeout_ms)
94+
}
95+
}
96+
Err(e) => {
97+
warn!("cannot parse user-supplied timeout header '{user_timeout_string}', using {max_timeout_ms}ms ({e})");
98+
max_timeout_ms
99+
}
100+
},
101+
Err(e) => {
102+
warn!("invalid user-supplied timeout header, using {max_timeout_ms}ms ({e})");
103+
max_timeout_ms
104+
}
105+
},
106+
None => max_timeout_ms,
107+
};
108+
84109
// prepare headers, except for start time which is set in `send_one_get_header`
85110
let mut send_headers = HeaderMap::new();
86111
send_headers.insert(USER_AGENT, get_user_agent_with_version(&req_headers)?);
@@ -301,6 +326,10 @@ async fn send_one_get_header(
301326
let start_request_time = utcnow_ms();
302327
req_config.headers.insert(HEADER_START_TIME_UNIX_MS, HeaderValue::from(start_request_time));
303328

329+
// The timeout header indicating how long a relay has to respond, so they can
330+
// minimize timing games without losing the bid
331+
req_config.headers.insert(HEADER_TIMEOUT_MS, HeaderValue::from(req_config.timeout_ms));
332+
304333
let start_request = Instant::now();
305334
let res = match relay
306335
.client

0 commit comments

Comments
 (0)