Skip to content

Commit 6a4f28b

Browse files
committed
handle_get_header, collapse redundant if/else into more legible match
1 parent 7959cb1 commit 6a4f28b

1 file changed

Lines changed: 34 additions & 55 deletions

File tree

crates/pbs/src/routes/get_header.rs

Lines changed: 34 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ pub async fn handle_get_header<S: BuilderApiState, A: BuilderApi<S>>(
4343
// Honor caller q-value preference: pick the highest-priority encoding that
4444
// we can actually produce. Server preference for tiebreaks is SSZ first.
4545
let response_encoding = accept_types.preferred(&[EncodingType::Ssz, EncodingType::Json]);
46-
let accepts_ssz = response_encoding == Some(EncodingType::Ssz);
47-
let accepts_json = response_encoding == Some(EncodingType::Json);
4846

4947
info!(ua, ms_into_slot, "new request");
5048

@@ -60,20 +58,14 @@ pub async fn handle_get_header<S: BuilderApiState, A: BuilderApi<S>>(
6058
"received header (unvalidated)"
6159
);
6260

63-
// Create the headers
61+
// ForkName::to_string() always yields valid ASCII,
62+
// so HeaderValue::from_str cannot fail here.
6463
let consensus_version_header =
65-
match HeaderValue::from_str(&light_bid.version.to_string()) {
66-
Ok(consensus_version_header) => {
67-
Ok::<HeaderValue, PbsClientError>(consensus_version_header)
68-
}
69-
Err(e) => {
70-
return Err(PbsClientError::RelayError(format!(
71-
"error decoding consensus version from relay payload: {e}"
72-
)));
73-
}
74-
}?;
64+
HeaderValue::from_str(&light_bid.version.to_string())
65+
.expect("fork name is always a valid header value");
7566
let content_type = light_bid.encoding_type.content_type();
76-
let content_type_header = HeaderValue::from_str(content_type).unwrap();
67+
let content_type_header = HeaderValue::from_str(content_type)
68+
.expect("content type is a static ASCII string");
7769

7870
// Build response
7971
let mut res = light_bid.raw_bytes.into_response();
@@ -87,48 +79,35 @@ pub async fn handle_get_header<S: BuilderApiState, A: BuilderApi<S>>(
8779
// Full validation mode, so respond based on requester accept types
8880
info!(value_eth = format_ether(*max_bid.data.message.value()), block_hash =% max_bid.block_hash(), "received header");
8981

90-
// Handle SSZ
91-
if accepts_ssz {
92-
let mut res = max_bid.data.as_ssz_bytes().into_response();
93-
let consensus_version_header = match HeaderValue::from_str(
94-
&max_bid.version.to_string(),
95-
) {
96-
Ok(consensus_version_header) => {
97-
Ok::<HeaderValue, PbsClientError>(consensus_version_header)
98-
}
99-
Err(e) => {
100-
if accepts_json {
101-
info!("sending response as JSON");
102-
return Ok(
103-
(StatusCode::OK, axum::Json(max_bid)).into_response()
104-
);
105-
} else {
106-
return Err(PbsClientError::RelayError(format!(
107-
"error decoding consensus version from relay payload: {e}"
108-
)));
109-
}
110-
}
111-
}?;
112-
113-
// This won't actually fail since the string is a const
114-
let content_type_header =
115-
HeaderValue::from_str(EncodingType::Ssz.content_type()).unwrap();
116-
117-
res.headers_mut()
118-
.insert(CONSENSUS_VERSION_HEADER, consensus_version_header);
119-
res.headers_mut().insert(CONTENT_TYPE, content_type_header);
120-
info!("sending response as SSZ");
121-
return Ok(res);
122-
}
123-
124-
// Handle JSON
125-
if accepts_json {
126-
Ok((StatusCode::OK, axum::Json(max_bid)).into_response())
127-
} else {
128-
// This shouldn't ever happen but the compiler needs it
129-
Err(PbsClientError::DecodeError(
82+
// Three arms: no viable encoding (unreachable in
83+
// practice — `get_accept_types` errors earlier if
84+
// the caller offers nothing we support), SSZ, or JSON.
85+
match response_encoding {
86+
None => Err(PbsClientError::DecodeError(
13087
"no viable accept types in request".to_string(),
131-
))
88+
)),
89+
Some(EncodingType::Ssz) => {
90+
// ForkName::to_string() always yields valid
91+
// ASCII, so HeaderValue::from_str cannot
92+
// fail here.
93+
let consensus_version_header =
94+
HeaderValue::from_str(&max_bid.version.to_string())
95+
.expect("fork name is always a valid header value");
96+
let content_type_header =
97+
HeaderValue::from_str(EncodingType::Ssz.content_type())
98+
.expect("content type is a static ASCII string");
99+
100+
let mut res = max_bid.data.as_ssz_bytes().into_response();
101+
res.headers_mut()
102+
.insert(CONSENSUS_VERSION_HEADER, consensus_version_header);
103+
res.headers_mut().insert(CONTENT_TYPE, content_type_header);
104+
info!("sending response as SSZ");
105+
Ok(res)
106+
}
107+
Some(EncodingType::Json) => {
108+
info!("sending response as JSON");
109+
Ok((StatusCode::OK, axum::Json(max_bid)).into_response())
110+
}
132111
}
133112
}
134113
}

0 commit comments

Comments
 (0)