Skip to content

Commit 77f7d4f

Browse files
committed
address review comments:
- JsonEncode error instead of unwrap() for both submit_block and register_validators - json timeout budget now accounts for prior ssz attempt
1 parent ce168c4 commit 77f7d4f

3 files changed

Lines changed: 20 additions & 11 deletions

File tree

crates/common/src/pbs/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ pub enum PbsError {
1515
#[error("json decode error: {err:?}, raw: {raw}")]
1616
JsonDecode { err: serde_json::Error, raw: String },
1717

18+
#[error("json encode error: {0:?}")]
19+
JsonEncode(serde_json::Error),
20+
1821
#[error("ssz decode error: {err:?}, fork: {fork}")]
1922
SSZDecode { err: String, fork: ForkName },
2023

crates/pbs/src/mev_boost/register_validator.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,19 @@ pub async fn register_validator<S: BuilderApiState>(
3535
.insert(HEADER_START_TIME_UNIX_MS, HeaderValue::from_str(&utcnow_ms().to_string())?);
3636
send_headers.insert(USER_AGENT, get_user_agent_with_version(&req_headers)?);
3737

38-
// prepare the body in advance, ugly dyn
39-
let bodies: Box<dyn Iterator<Item = (usize, Bytes)>> =
38+
// prepare the body in advance
39+
let bodies: Vec<(usize, Bytes)> =
4040
if let Some(batch_size) = state.config.pbs_config.validator_registration_batch_size {
41-
Box::new(registrations.chunks(batch_size).map(|batch| {
42-
// SAFETY: unwrap is ok because we're serializing a &[serde_json::Value]
43-
let body = serde_json::to_vec(batch).unwrap();
44-
(batch.len(), Bytes::from(body))
45-
}))
41+
registrations
42+
.chunks(batch_size)
43+
.map(|batch| {
44+
let body = serde_json::to_vec(batch).map_err(PbsError::JsonEncode)?;
45+
Ok((batch.len(), Bytes::from(body)))
46+
})
47+
.collect::<Result<Vec<_>, PbsError>>()?
4648
} else {
47-
let body = serde_json::to_vec(&registrations).unwrap();
48-
Box::new(std::iter::once((registrations.len(), Bytes::from(body))))
49+
let body = serde_json::to_vec(&registrations).map_err(PbsError::JsonEncode)?;
50+
vec![(registrations.len(), Bytes::from(body))]
4951
};
5052
send_headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
5153

crates/pbs/src/mev_boost/submit_block.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,12 +377,16 @@ async fn send_submit_block_impl(
377377
status = %res.status(),
378378
"relay rejected SSZ content-type, resubmitting block with JSON content-type"
379379
);
380+
// Sharing caller's timeout budget with SSZ attempt, so JSON gets the remaining time
381+
let remaining_timeout_ms =
382+
timeout_ms.saturating_sub(start_request.elapsed().as_millis() as u64);
383+
let json_body = serde_json::to_vec(&signed_blinded_block).map_err(PbsError::JsonEncode)?;
380384
res = match relay
381385
.client
382386
.post(url.as_ref().clone())
383-
.timeout(Duration::from_millis(timeout_ms))
387+
.timeout(Duration::from_millis(remaining_timeout_ms))
384388
.headers(headers.clone())
385-
.body(serde_json::to_vec(&signed_blinded_block).unwrap())
389+
.body(json_body)
386390
.header(CONTENT_TYPE, EncodingType::Json.content_type_header())
387391
.send()
388392
.await

0 commit comments

Comments
 (0)