Skip to content

Commit 57002f5

Browse files
committed
review fixes
1 parent cbf451d commit 57002f5

2 files changed

Lines changed: 36 additions & 40 deletions

File tree

crates/common/src/wire.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use bytes::Bytes;
77
use futures::StreamExt;
88
use headers_accept::Accept;
99
use lh_types::{BeaconBlock, ForkName};
10-
use mediatype::{MediaType, ReadParams};
10+
use mediatype::{MediaType, ReadParams, names};
1111
use reqwest::{
1212
Response,
1313
header::{ACCEPT, CONTENT_TYPE, HeaderMap, ToStrError},
@@ -239,13 +239,7 @@ pub fn get_accept_types(
239239
continue;
240240
}
241241

242-
let parsed = match mt.essence().to_string().as_str() {
243-
APPLICATION_OCTET_STREAM => Some(EncodingType::Ssz),
244-
APPLICATION_JSON => Some(EncodingType::Json),
245-
WILDCARD => Some(NO_PREFERENCE_DEFAULT),
246-
_ => None,
247-
};
248-
if let Some(enc) = parsed {
242+
if let Some(enc) = essence_encoding(&mt.essence()) {
249243
had_supported = true;
250244
match primary {
251245
None => primary = Some(enc),
@@ -269,6 +263,22 @@ pub fn get_accept_types(
269263
Ok(AcceptedEncodings::single(get_content_type(req_headers)))
270264
}
271265

266+
fn essence_encoding(mt: &MediaType) -> Option<EncodingType> {
267+
if mt.suffix.is_some() {
268+
return None;
269+
}
270+
271+
match () {
272+
_ if mt.ty == names::_STAR && mt.subty == names::_STAR => Some(NO_PREFERENCE_DEFAULT),
273+
_ if mt.ty == names::APPLICATION && mt.subty == names::OCTET_STREAM => {
274+
Some(EncodingType::Ssz)
275+
}
276+
_ if mt.ty == names::APPLICATION && mt.subty == names::JSON => Some(EncodingType::Json),
277+
_ if mt.ty == names::APPLICATION && mt.subty == names::_STAR => Some(NO_PREFERENCE_DEFAULT),
278+
_ => None,
279+
}
280+
}
281+
272282
/// Compute the q-value for the `index`-th preferred encoding when building an
273283
/// outbound `Accept` header. The first entry gets q=1.0, each subsequent entry
274284
/// decreases by 0.1, and the value is clamped to a minimum of 0.1 so we never
@@ -363,11 +373,7 @@ impl FromStr for EncodingType {
363373
// (e.g. `application/json; charset=utf-8`). Compare essence only.
364374
let parsed =
365375
MediaType::parse(value).map_err(|e| format!("invalid content type {value}: {e}"))?;
366-
match parsed.essence().to_string().to_ascii_lowercase().as_str() {
367-
APPLICATION_JSON => Ok(EncodingType::Json),
368-
APPLICATION_OCTET_STREAM => Ok(EncodingType::Ssz),
369-
_ => Err(format!("unsupported encoding type: {value}")),
370-
}
376+
essence_encoding(&parsed).ok_or_else(|| format!("unsupported encoding type: {value}"))
371377
}
372378
}
373379

crates/pbs/src/mev_boost/get_header.rs

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ struct RequestInfo {
5555
params: GetHeaderParams,
5656

5757
/// Common baseline of headers to send with each request
58-
headers: Arc<HeaderMap>,
58+
headers: HeaderMap,
5959

6060
/// The chain the request is for
6161
chain: Chain,
@@ -152,10 +152,9 @@ pub async fn get_header<S: BuilderApiState>(
152152
// Create the Accept headers for requests
153153
// Use the documented, deterministic preference:
154154
// SSZ first (wire-efficient), JSON fallback.
155-
let accept_types = OUTBOUND_ACCEPT.to_string();
156155
send_headers.insert(
157156
ACCEPT,
158-
HeaderValue::from_str(&accept_types).map_err(|e| {
157+
HeaderValue::from_str(OUTBOUND_ACCEPT).map_err(|e| {
159158
AcceptedEncodingsError::InvalidEncoding {
160159
error_msg: (format!("invalid accept header: {e}")).to_string(),
161160
}
@@ -166,7 +165,7 @@ pub async fn get_header<S: BuilderApiState>(
166165
let slot = params.slot as i64;
167166
let request_info = Arc::new(RequestInfo {
168167
params,
169-
headers: Arc::new(send_headers),
168+
headers: send_headers,
170169
chain: state.config.chain,
171170
validation: ValidationContext {
172171
skip_sigverify: state.pbs_config().skip_sigverify,
@@ -255,7 +254,7 @@ async fn send_timed_get_header(
255254
mut timeout_left_ms: u64,
256255
) -> Result<Option<GetHeaderResponse>, PbsError> {
257256
let params = &request_info.params;
258-
let url = Arc::new(relay.get_header_url(params.slot, &params.parent_hash, &params.pubkey)?);
257+
let url = relay.get_header_url(params.slot, &params.parent_hash, &params.pubkey)?;
259258

260259
if relay.config.enable_timing_games {
261260
if let Some(target_ms) = relay.config.target_first_request_ms {
@@ -357,18 +356,18 @@ async fn send_timed_get_header(
357356
async fn send_one_get_header(
358357
request_info: Arc<RequestInfo>,
359358
relay: RelayClient,
360-
url: Arc<Url>,
359+
url: Url,
361360
timeout_left_ms: u64,
362361
) -> Result<(u64, Option<GetHeaderResponse>), PbsError> {
363362
// Full processing: decode full response and validate
364363
let (start_request_time, get_header_response) = send_get_header_full(
365364
&relay,
366365
url,
367366
timeout_left_ms,
368-
(*request_info.headers).clone(), /* Create a copy of the HeaderMap because the
369-
* impl
370-
* will
371-
* modify it */
367+
request_info.headers.clone(), /* Create a copy of the HeaderMap because the
368+
* impl
369+
* will
370+
* modify it */
372371
)
373372
.await?;
374373
let get_header_response = match get_header_response {
@@ -396,7 +395,6 @@ async fn send_one_get_header(
396395
}),
397396
}?;
398397

399-
// Validate the header
400398
let chain = request_info.chain;
401399
let params = &request_info.params;
402400
let validation = &request_info.validation;
@@ -408,7 +406,6 @@ async fn send_one_get_header(
408406
params.slot,
409407
)?;
410408

411-
// Validate the relay signature
412409
if !validation.skip_sigverify {
413410
validate_signature(
414411
chain,
@@ -419,7 +416,6 @@ async fn send_one_get_header(
419416
)?;
420417
}
421418

422-
// Validate the parent block if enabled
423419
if validation.extra_validation_enabled {
424420
let parent_block = validation.parent_block.read();
425421
if let Some(parent_block) = parent_block.as_ref() {
@@ -438,11 +434,10 @@ async fn send_one_get_header(
438434
/// Send and decode a full get_header response, with all of the fields.
439435
async fn send_get_header_full(
440436
relay: &RelayClient,
441-
url: Arc<Url>,
437+
url: Url,
442438
timeout_left_ms: u64,
443439
headers: HeaderMap,
444440
) -> Result<(u64, Option<GetHeaderResponse>), PbsError> {
445-
// Send the request
446441
let (start_request_time, info) =
447442
send_get_header_impl(relay, url, timeout_left_ms, headers).await?;
448443
let info = match info {
@@ -452,10 +447,8 @@ async fn send_get_header_full(
452447
}
453448
};
454449

455-
// Decode the response
456450
let get_header_response = decode_by_encoding(&info, decode_json_payload, decode_ssz_payload)?;
457451

458-
// Log and return
459452
info!(
460453
relay_id = info.relay_id.as_ref(),
461454
header_size_bytes = info.response_bytes.len(),
@@ -473,11 +466,11 @@ async fn send_get_header_full(
473466
/// negotiated content-type. SSZ requires a fork header; its absence is a
474467
/// protocol violation reported as `PbsError::RelayResponse`. Callers supply
475468
/// the format-specific decoders, keeping the encoding branch in one place.
476-
fn decode_by_encoding<T>(
469+
fn decode_by_encoding(
477470
info: &GetHeaderResponseInfo,
478-
on_json: impl FnOnce(&[u8]) -> Result<T, PbsError>,
479-
on_ssz: impl FnOnce(&[u8], ForkName) -> Result<T, PbsError>,
480-
) -> Result<T, PbsError> {
471+
on_json: impl FnOnce(&[u8]) -> Result<GetHeaderResponse, PbsError>,
472+
on_ssz: impl FnOnce(&[u8], ForkName) -> Result<GetHeaderResponse, PbsError>,
473+
) -> Result<GetHeaderResponse, PbsError> {
481474
match info.content_type {
482475
EncodingType::Json => on_json(&info.response_bytes),
483476
EncodingType::Ssz => {
@@ -496,7 +489,7 @@ fn decode_by_encoding<T>(
496489
/// Used by send_one_get_header to perform the actual request submission.
497490
async fn send_get_header_impl(
498491
relay: &RelayClient,
499-
url: Arc<Url>,
492+
url: Url,
500493
timeout_left_ms: u64,
501494
mut headers: HeaderMap,
502495
) -> Result<(u64, Option<GetHeaderResponseInfo>), PbsError> {
@@ -513,7 +506,7 @@ async fn send_get_header_impl(
513506
let start_request = Instant::now();
514507
let res = match relay
515508
.client
516-
.get(url.as_ref().clone())
509+
.get(url.as_ref())
517510
.timeout(Duration::from_millis(timeout_left_ms))
518511
.headers(headers)
519512
.send()
@@ -582,10 +575,7 @@ fn decode_ssz_payload(
582575
fork: ForkName,
583576
) -> Result<GetHeaderResponse, PbsError> {
584577
let data = SignedBuilderBid::from_ssz_bytes_by_fork(response_bytes, fork).map_err(|e| {
585-
PbsError::SSZDecode {
586-
err: (format!("error decoding relay payload: {e:?}")).to_string(),
587-
fork,
588-
}
578+
PbsError::SSZDecode { err: (format!("error decoding relay payload: {e:?}")), fork }
589579
})?;
590580
Ok(GetHeaderResponse { version: fork, data, metadata: Default::default() })
591581
}

0 commit comments

Comments
 (0)