Skip to content

Commit 95adfe8

Browse files
committed
address review comments
1 parent e6d68a6 commit 95adfe8

3 files changed

Lines changed: 31 additions & 78 deletions

File tree

crates/common/src/pbs/error.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ pub enum PbsError {
3131

3232
#[error("tokio join error: {0}")]
3333
TokioJoinError(#[from] tokio::task::JoinError),
34+
35+
#[error("SSZ error: {0}")]
36+
SszError(#[from] SszValueError),
3437
}
3538

3639
impl PbsError {
@@ -119,16 +122,3 @@ pub enum SszValueError {
119122
#[error("unsupported fork")]
120123
UnsupportedFork { name: String },
121124
}
122-
123-
impl From<SszValueError> for PbsError {
124-
fn from(err: SszValueError) -> Self {
125-
match err {
126-
SszValueError::InvalidPayloadLength { required, actual } => PbsError::GeneralRequest(
127-
format!("invalid payload length: required {required} but payload was {actual}"),
128-
),
129-
SszValueError::UnsupportedFork { name } => {
130-
PbsError::GeneralRequest(format!("unsupported fork: {name}"))
131-
}
132-
}
133-
}
134-
}

crates/common/src/utils.rs

Lines changed: 28 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#[cfg(feature = "testing-flags")]
22
use std::cell::Cell;
33
use std::{
4-
fmt::Display,
54
net::Ipv4Addr,
65
str::FromStr,
76
sync::LazyLock,
@@ -555,16 +554,9 @@ pub const OUTBOUND_ACCEPT: &str = "application/octet-stream;q=1.0,application/js
555554
/// policy in one place prevents drift between those sites.
556555
pub const NO_PREFERENCE_DEFAULT: EncodingType = EncodingType::Json;
557556

558-
/// Encodings the original requester is willing to accept, in descending
559-
/// preference order.
560-
///
561-
/// The builder spec defines exactly two media types (SSZ and JSON), so after
562-
/// dedup the accepted set is at most one primary plus one optional fallback.
563557
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
564558
pub struct AcceptedEncodings {
565-
/// Caller's highest-preference encoding.
566559
pub primary: EncodingType,
567-
/// Second-choice encoding, if the caller provided one.
568560
pub fallback: Option<EncodingType>,
569561
}
570562

@@ -582,8 +574,6 @@ impl AcceptedEncodings {
582574
std::iter::once(self.primary).chain(self.fallback)
583575
}
584576

585-
/// Pick the caller's highest-preference encoding that the server supports.
586-
/// Returns `None` if no overlap exists.
587577
pub fn preferred(self, supported: &[EncodingType]) -> Option<EncodingType> {
588578
self.iter().find(|a| supported.contains(a))
589579
}
@@ -675,6 +665,7 @@ fn accept_q_value_for_index(index: usize) -> f32 {
675665
}
676666

677667
/// Format a single `Accept` header entry as `"<media-type>;q=<x.x>"`.
668+
#[inline]
678669
fn format_accept_entry(enc: EncodingType, q: f32) -> String {
679670
format!("{};q={:.1}", enc.content_type(), q)
680671
}
@@ -692,8 +683,6 @@ pub fn build_outbound_accept(preferred: AcceptedEncodings) -> String {
692683
.join(",")
693684
}
694685

695-
/// Parse CONTENT TYPE header to get the encoding type of the body, defaulting
696-
/// to JSON if missing or malformed.
697686
pub fn get_content_type(req_headers: &HeaderMap) -> EncodingType {
698687
EncodingType::from_str(
699688
req_headers
@@ -704,7 +693,6 @@ pub fn get_content_type(req_headers: &HeaderMap) -> EncodingType {
704693
.unwrap_or(EncodingType::Json)
705694
}
706695

707-
/// Parse CONSENSUS_VERSION header
708696
pub fn get_consensus_version_header(req_headers: &HeaderMap) -> Option<ForkName> {
709697
ForkName::from_str(
710698
req_headers
@@ -799,29 +787,19 @@ pub fn parse_response_encoding_and_fork(
799787
Ok((content_type, get_consensus_version_header(headers)))
800788
}
801789

790+
#[derive(Debug, Error)]
802791
pub enum BodyDeserializeError {
792+
#[error("JSON deserialization error: {0}")]
803793
SerdeJsonError(serde_json::Error),
794+
#[error("SSZ deserialization error: {0:?}")]
804795
SszDecodeError(ssz::DecodeError),
796+
#[error("unsupported media type")]
805797
UnsupportedMediaType,
798+
#[error("missing consensus version header")]
806799
MissingVersionHeader,
807800
}
808801

809-
impl Display for BodyDeserializeError {
810-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
811-
match self {
812-
BodyDeserializeError::SerdeJsonError(e) => write!(f, "JSON deserialization error: {e}"),
813-
BodyDeserializeError::SszDecodeError(e) => {
814-
write!(f, "SSZ deserialization error: {e:?}")
815-
}
816-
BodyDeserializeError::UnsupportedMediaType => write!(f, "unsupported media type"),
817-
BodyDeserializeError::MissingVersionHeader => {
818-
write!(f, "missing consensus version header")
819-
}
820-
}
821-
}
822-
}
823-
824-
pub async fn deserialize_body(
802+
pub fn deserialize_body(
825803
headers: &HeaderMap,
826804
body: Bytes,
827805
) -> Result<SignedBlindedBeaconBlock, BodyDeserializeError> {
@@ -900,56 +878,46 @@ pub fn bls_pubkey_from_hex_unchecked(hex: &str) -> BlsPublicKey {
900878
}
901879

902880
// Get the offset of the message in a SignedBuilderBid SSZ structure
903-
fn get_ssz_value_offset_for_fork(fork: ForkName) -> Option<usize> {
881+
fn get_ssz_value_offset_for_fork(fork: ForkName) -> Result<usize, SszValueError> {
904882
match fork {
905883
ForkName::Bellatrix => {
906884
// Message goes header -> value -> pubkey
907-
Some(
908-
get_message_offset::<BuilderBidBellatrix>() +
909-
<ExecutionPayloadHeaderBellatrix as ssz::Decode>::ssz_fixed_len(),
910-
)
885+
Ok(get_message_offset::<BuilderBidBellatrix>() +
886+
<ExecutionPayloadHeaderBellatrix as ssz::Decode>::ssz_fixed_len())
911887
}
912888

913889
ForkName::Capella => {
914890
// Message goes header -> value -> pubkey
915-
Some(
916-
get_message_offset::<BuilderBidCapella>() +
917-
<ExecutionPayloadHeaderCapella as ssz::Decode>::ssz_fixed_len(),
918-
)
891+
Ok(get_message_offset::<BuilderBidCapella>() +
892+
<ExecutionPayloadHeaderCapella as ssz::Decode>::ssz_fixed_len())
919893
}
920894

921895
ForkName::Deneb => {
922896
// Message goes header -> blob_kzg_commitments -> value -> pubkey
923-
Some(
924-
get_message_offset::<BuilderBidDeneb>() +
925-
<ExecutionPayloadHeaderDeneb as ssz::Decode>::ssz_fixed_len() +
926-
<KzgCommitments as ssz::Decode>::ssz_fixed_len(),
927-
)
897+
Ok(get_message_offset::<BuilderBidDeneb>() +
898+
<ExecutionPayloadHeaderDeneb as ssz::Decode>::ssz_fixed_len() +
899+
<KzgCommitments as ssz::Decode>::ssz_fixed_len())
928900
}
929901

930902
ForkName::Electra => {
931903
// Message goes header -> blob_kzg_commitments -> execution_requests -> value ->
932904
// pubkey
933-
Some(
934-
get_message_offset::<BuilderBidElectra>() +
935-
<ExecutionPayloadHeaderElectra as ssz::Decode>::ssz_fixed_len() +
936-
<KzgCommitments as ssz::Decode>::ssz_fixed_len() +
937-
<ExecutionRequests as ssz::Decode>::ssz_fixed_len(),
938-
)
905+
Ok(get_message_offset::<BuilderBidElectra>() +
906+
<ExecutionPayloadHeaderElectra as ssz::Decode>::ssz_fixed_len() +
907+
<KzgCommitments as ssz::Decode>::ssz_fixed_len() +
908+
<ExecutionRequests as ssz::Decode>::ssz_fixed_len())
939909
}
940910

941911
ForkName::Fulu => {
942912
// Message goes header -> blob_kzg_commitments -> execution_requests -> value ->
943913
// pubkey
944-
Some(
945-
get_message_offset::<BuilderBidFulu>() +
946-
<ExecutionPayloadHeaderFulu as ssz::Decode>::ssz_fixed_len() +
947-
<KzgCommitments as ssz::Decode>::ssz_fixed_len() +
948-
<ExecutionRequests as ssz::Decode>::ssz_fixed_len(),
949-
)
914+
Ok(get_message_offset::<BuilderBidFulu>() +
915+
<ExecutionPayloadHeaderFulu as ssz::Decode>::ssz_fixed_len() +
916+
<KzgCommitments as ssz::Decode>::ssz_fixed_len() +
917+
<ExecutionRequests as ssz::Decode>::ssz_fixed_len())
950918
}
951919

952-
_ => None,
920+
_ => Err(SszValueError::UnsupportedFork { name: fork.to_string() }),
953921
}
954922
}
955923

@@ -958,8 +926,7 @@ pub fn get_bid_value_from_signed_builder_bid_ssz(
958926
response_bytes: &[u8],
959927
fork: ForkName,
960928
) -> Result<U256, SszValueError> {
961-
let value_offset = get_ssz_value_offset_for_fork(fork)
962-
.ok_or(SszValueError::UnsupportedFork { name: fork.to_string() })?;
929+
let value_offset = get_ssz_value_offset_for_fork(fork)?;
963930

964931
// Sanity check the response length so we don't panic trying to slice it
965932
let end_offset = value_offset + 32; // U256 is 32 bytes
@@ -1825,7 +1792,7 @@ mod test {
18251792
async fn test_deserialize_body_missing_content_type_falls_back_to_json() {
18261793
let headers = HeaderMap::new();
18271794
let body = Bytes::from_static(b"not json");
1828-
let err = deserialize_body(&headers, body).await.unwrap_err();
1795+
let err = deserialize_body(&headers, body).unwrap_err();
18291796
assert!(
18301797
matches!(err, BodyDeserializeError::SerdeJsonError(_)),
18311798
"expected SerdeJsonError (JSON decode attempted), got: {err}"
@@ -1839,7 +1806,7 @@ mod test {
18391806
let mut headers = HeaderMap::new();
18401807
headers.insert(CONTENT_TYPE, HeaderValue::from_static("text/plain"));
18411808
let body = Bytes::from_static(b"hi");
1842-
let err = deserialize_body(&headers, body).await.unwrap_err();
1809+
let err = deserialize_body(&headers, body).unwrap_err();
18431810
assert!(matches!(err, BodyDeserializeError::UnsupportedMediaType));
18441811
}
18451812

@@ -1848,7 +1815,7 @@ mod test {
18481815
let mut headers = HeaderMap::new();
18491816
headers.insert(CONTENT_TYPE, HeaderValue::from_str(APPLICATION_OCTET_STREAM).unwrap());
18501817
let body = Bytes::from_static(b"\x00\x01\x02\x03");
1851-
let err = deserialize_body(&headers, body).await.unwrap_err();
1818+
let err = deserialize_body(&headers, body).unwrap_err();
18521819
assert!(matches!(err, BodyDeserializeError::MissingVersionHeader));
18531820
}
18541821
}

crates/pbs/src/error.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ pub enum PbsClientError {
88
NoPayload,
99
Internal,
1010
DecodeError(String),
11-
#[allow(dead_code)]
12-
RelayError(String),
1311
}
1412

1513
impl PbsClientError {
@@ -19,7 +17,6 @@ impl PbsClientError {
1917
PbsClientError::NoPayload => StatusCode::BAD_GATEWAY,
2018
PbsClientError::Internal => StatusCode::INTERNAL_SERVER_ERROR,
2119
PbsClientError::DecodeError(_) => StatusCode::BAD_REQUEST,
22-
PbsClientError::RelayError(_) => StatusCode::FAILED_DEPENDENCY,
2320
}
2421
}
2522
}
@@ -37,7 +34,6 @@ impl IntoResponse for PbsClientError {
3734
PbsClientError::NoPayload => "no payload from relays".to_string(),
3835
PbsClientError::Internal => "internal server error".to_string(),
3936
PbsClientError::DecodeError(e) => format!("error decoding request: {e}"),
40-
PbsClientError::RelayError(e) => format!("error processing relay response: {e}"),
4137
};
4238

4339
(self.status_code(), msg).into_response()

0 commit comments

Comments
 (0)