Skip to content

Commit fdaa06e

Browse files
committed
consolidate duplicate code in *_light and *_full into
decode_by_encoding()
1 parent 8ae1f0c commit fdaa06e

2 files changed

Lines changed: 63 additions & 63 deletions

File tree

crates/pbs/src/mev_boost/get_header.rs

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -547,17 +547,7 @@ async fn send_get_header_full(
547547
};
548548

549549
// Decode the response
550-
let get_header_response = match info.content_type {
551-
EncodingType::Json => decode_json_payload(&info.response_bytes)?,
552-
EncodingType::Ssz => {
553-
let fork = info.fork.ok_or(PbsError::RelayResponse {
554-
error_msg: "relay did not provide consensus version header for ssz payload"
555-
.to_string(),
556-
code: info.code.as_u16(),
557-
})?;
558-
decode_ssz_payload(&info.response_bytes, fork)?
559-
}
560-
};
550+
let get_header_response = decode_by_encoding(&info, decode_json_payload, decode_ssz_payload)?;
561551

562552
// Log and return
563553
info!(
@@ -594,17 +584,9 @@ async fn send_get_header_light(
594584
};
595585

596586
// Decode the value / fork from the response
597-
let (fork, value) = match info.content_type {
598-
EncodingType::Json => get_light_info_from_json(&info.response_bytes)?,
599-
EncodingType::Ssz => {
600-
let fork = info.fork.ok_or(PbsError::RelayResponse {
601-
error_msg: "relay did not provide consensus version header for ssz payload"
602-
.to_string(),
603-
code: info.code.as_u16(),
604-
})?;
605-
(fork, get_bid_value_from_signed_builder_bid_ssz(&info.response_bytes, fork)?)
606-
}
607-
};
587+
let (fork, value) = decode_by_encoding(&info, get_light_info_from_json, |bytes, fork| {
588+
Ok((fork, get_bid_value_from_signed_builder_bid_ssz(bytes, fork)?))
589+
})?;
608590

609591
// Log and return
610592
debug!(
@@ -627,6 +609,28 @@ async fn send_get_header_light(
627609
))
628610
}
629611

612+
/// Dispatch a get_header response to the appropriate decoder based on the
613+
/// negotiated content-type. SSZ requires a fork header; its absence is a
614+
/// protocol violation reported as `PbsError::RelayResponse`. Callers supply
615+
/// the format-specific decoders, keeping the encoding branch in one place.
616+
fn decode_by_encoding<T>(
617+
info: &GetHeaderResponseInfo,
618+
on_json: impl FnOnce(&[u8]) -> Result<T, PbsError>,
619+
on_ssz: impl FnOnce(&[u8], ForkName) -> Result<T, PbsError>,
620+
) -> Result<T, PbsError> {
621+
match info.content_type {
622+
EncodingType::Json => on_json(&info.response_bytes),
623+
EncodingType::Ssz => {
624+
let fork = info.fork.ok_or_else(|| PbsError::RelayResponse {
625+
error_msg: "relay did not provide consensus version header for ssz payload"
626+
.to_string(),
627+
code: info.code.as_u16(),
628+
})?;
629+
on_ssz(&info.response_bytes, fork)
630+
}
631+
}
632+
}
633+
630634
/// Sends a get_header request to a relay, returning the response, the time the
631635
/// request was started, and the encoding type of the response (if any).
632636
/// Used by send_one_get_header to perform the actual request submission.

crates/pbs/src/mev_boost/submit_block.rs

Lines changed: 37 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -341,28 +341,10 @@ async fn send_submit_block_full(
341341
return Ok(None);
342342
}
343343

344-
// Decode the payload based on content type.
345-
// The v1 guard above ensures `content_type` is Some
346-
let content_type = block_response.content_type.ok_or_else(|| PbsError::RelayResponse {
347-
error_msg: "v1 submit_block response missing Content-Type".to_string(),
348-
code: block_response.code.as_u16(),
349-
})?;
350-
let decoded_response = match content_type {
351-
EncodingType::Json => decode_json_payload(&block_response.response_bytes)?,
352-
EncodingType::Ssz => {
353-
let fork = match block_response.fork {
354-
Some(fork) => fork,
355-
None => {
356-
return Err(PbsError::RelayResponse {
357-
error_msg: "missing fork version header in SSZ submit_block response"
358-
.to_string(),
359-
code: block_response.code.as_u16(),
360-
});
361-
}
362-
};
363-
decode_ssz_payload(&block_response.response_bytes, fork)?
364-
}
365-
};
344+
// Decode the payload based on content type. The v1 guard above ensures
345+
// `content_type` is Some.
346+
let decoded_response =
347+
decode_by_encoding(&block_response, decode_json_payload, decode_ssz_payload)?;
366348

367349
// Log and return
368350
debug!(
@@ -407,24 +389,13 @@ async fn send_submit_block_light(
407389
}
408390

409391
// Decode the payload based on content type. The v1 guard above ensures
410-
// `content_type` is Some (v2 paths return None and early-exit).
411-
let content_type = block_response.content_type.ok_or_else(|| PbsError::RelayResponse {
412-
error_msg: "v1 submit_block response missing Content-Type".to_string(),
413-
code: block_response.code.as_u16(),
414-
})?;
415-
let fork = match content_type {
416-
EncodingType::Json => get_light_info_from_json(&block_response.response_bytes)?,
417-
EncodingType::Ssz => match block_response.fork {
418-
Some(fork) => fork,
419-
None => {
420-
return Err(PbsError::RelayResponse {
421-
error_msg: "missing fork version header in SSZ submit_block response"
422-
.to_string(),
423-
code: block_response.code.as_u16(),
424-
});
425-
}
426-
},
427-
};
392+
// `content_type` is Some.
393+
let fork =
394+
decode_by_encoding(&block_response, get_light_info_from_json, |_bytes, fork| Ok(fork))?;
395+
// `content_type` is guaranteed Some on v1 per decode_by_encoding.
396+
let encoding_type = block_response.content_type.expect(
397+
"v1 submit_block response carries Content-Type; decode_by_encoding already enforced this",
398+
);
428399

429400
// Log and return
430401
debug!(
@@ -437,11 +408,36 @@ async fn send_submit_block_light(
437408

438409
Ok(Some(LightSubmitBlockResponse {
439410
version: fork,
440-
encoding_type: content_type,
411+
encoding_type,
441412
raw_bytes: block_response.response_bytes,
442413
}))
443414
}
444415

416+
/// Dispatch a v1 submit_block response to the appropriate decoder based on the
417+
/// negotiated content-type. Caller guarantees `content_type` is Some (v2
418+
/// paths early-exit before reaching decode); an absent Content-Type on v1 is
419+
/// treated as a protocol violation. SSZ additionally requires a fork header.
420+
fn decode_by_encoding<T>(
421+
info: &SubmitBlockResponseInfo,
422+
on_json: impl FnOnce(&[u8]) -> Result<T, PbsError>,
423+
on_ssz: impl FnOnce(&[u8], ForkName) -> Result<T, PbsError>,
424+
) -> Result<T, PbsError> {
425+
let content_type = info.content_type.ok_or_else(|| PbsError::RelayResponse {
426+
error_msg: "v1 submit_block response missing Content-Type".to_string(),
427+
code: info.code.as_u16(),
428+
})?;
429+
match content_type {
430+
EncodingType::Json => on_json(&info.response_bytes),
431+
EncodingType::Ssz => {
432+
let fork = info.fork.ok_or_else(|| PbsError::RelayResponse {
433+
error_msg: "missing fork version header in SSZ submit_block response".to_string(),
434+
code: info.code.as_u16(),
435+
})?;
436+
on_ssz(&info.response_bytes, fork)
437+
}
438+
}
439+
}
440+
445441
/// Sends the actual HTTP request to the relay's submit_block endpoint,
446442
/// returning the response (if applicable), the round-trip time, and the
447443
/// encoding type used for the body (if any). Used by send_submit_block.

0 commit comments

Comments
 (0)