Skip to content

Commit d9b21f7

Browse files
authored
fix(pbs): body default body size (#313)
1 parent 7330f51 commit d9b21f7

File tree

5 files changed

+30
-13
lines changed

5 files changed

+30
-13
lines changed

crates/pbs/src/constants.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ pub const TIMEOUT_ERROR_CODE_STR: &str = "555";
1010

1111
/// 20 MiB to cover edge cases for heavy blocks and also add a bit of slack for
1212
/// any Ethereum upgrades in the near future
13-
pub const MAX_SIZE_SUBMIT_BLOCK: usize = 20 * 1024 * 1024;
13+
pub const MAX_SIZE_SUBMIT_BLOCK_RESPONSE: usize = 20 * 1024 * 1024;
14+
15+
/// 20 MiB, enough to process ~45000 registrations in one request
16+
pub const MAX_SIZE_REGISTER_VALIDATOR_REQUEST: usize = 20 * 1024 * 1024;
1417

1518
/// 10 KiB, headers are around 700 bytes + buffer for encoding
16-
pub const MAX_SIZE_GET_HEADER: usize = 10 * 1024;
19+
pub const MAX_SIZE_GET_HEADER_RESPONSE: usize = 10 * 1024;
1720

1821
pub const MAX_SIZE_DEFAULT: usize = 1024;

crates/pbs/src/mev_boost/get_header.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ use url::Url;
3131

3232
use crate::{
3333
constants::{
34-
GET_HEADER_ENDPOINT_TAG, MAX_SIZE_GET_HEADER, TIMEOUT_ERROR_CODE, TIMEOUT_ERROR_CODE_STR,
34+
GET_HEADER_ENDPOINT_TAG, MAX_SIZE_GET_HEADER_RESPONSE, TIMEOUT_ERROR_CODE,
35+
TIMEOUT_ERROR_CODE_STR,
3536
},
3637
metrics::{RELAY_HEADER_VALUE, RELAY_LAST_SLOT, RELAY_LATENCY, RELAY_STATUS_CODE},
3738
state::{BuilderApiState, PbsState},
@@ -323,7 +324,7 @@ async fn send_one_get_header(
323324
let code = res.status();
324325
RELAY_STATUS_CODE.with_label_values(&[code.as_str(), GET_HEADER_ENDPOINT_TAG, &relay.id]).inc();
325326

326-
let response_bytes = read_chunked_body_with_max(res, MAX_SIZE_GET_HEADER).await?;
327+
let response_bytes = read_chunked_body_with_max(res, MAX_SIZE_GET_HEADER_RESPONSE).await?;
327328
if !code.is_success() {
328329
return Err(PbsError::RelayResponse {
329330
error_msg: String::from_utf8_lossy(&response_bytes).into_owned(),

crates/pbs/src/mev_boost/submit_block.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ use tracing::{debug, warn};
1616
use url::Url;
1717

1818
use crate::{
19-
constants::{MAX_SIZE_SUBMIT_BLOCK, SUBMIT_BLINDED_BLOCK_ENDPOINT_TAG, TIMEOUT_ERROR_CODE_STR},
19+
constants::{
20+
MAX_SIZE_SUBMIT_BLOCK_RESPONSE, SUBMIT_BLINDED_BLOCK_ENDPOINT_TAG, TIMEOUT_ERROR_CODE_STR,
21+
},
2022
metrics::{RELAY_LATENCY, RELAY_STATUS_CODE},
2123
state::{BuilderApiState, PbsState},
2224
utils::read_chunked_body_with_max,
@@ -139,7 +141,7 @@ async fn send_submit_block(
139141
.with_label_values(&[code.as_str(), SUBMIT_BLINDED_BLOCK_ENDPOINT_TAG, &relay.id])
140142
.inc();
141143

142-
let response_bytes = read_chunked_body_with_max(res, MAX_SIZE_SUBMIT_BLOCK).await?;
144+
let response_bytes = read_chunked_body_with_max(res, MAX_SIZE_SUBMIT_BLOCK_RESPONSE).await?;
143145
if !code.is_success() {
144146
let err = PbsError::RelayResponse {
145147
error_msg: String::from_utf8_lossy(&response_bytes).into_owned(),

crates/pbs/src/routes/router.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use axum::{
2-
extract::{MatchedPath, Request},
2+
extract::{DefaultBodyLimit, MatchedPath, Request},
33
middleware::{self, Next},
44
response::Response,
55
routing::{get, post},
@@ -20,16 +20,27 @@ use super::{
2020
use crate::{
2121
api::BuilderApi,
2222
state::{BuilderApiState, PbsStateGuard},
23+
MAX_SIZE_REGISTER_VALIDATOR_REQUEST, MAX_SIZE_SUBMIT_BLOCK_RESPONSE,
2324
};
2425

2526
pub fn create_app_router<S: BuilderApiState, A: BuilderApi<S>>(state: PbsStateGuard<S>) -> Router {
27+
// DefaultBodyLimit is 2Mib by default, so we only increase it for a few routes
28+
// thay may need more
29+
2630
let builder_routes = Router::new()
2731
.route(GET_HEADER_PATH, get(handle_get_header::<S, A>))
2832
.route(GET_STATUS_PATH, get(handle_get_status::<S, A>))
29-
.route(REGISTER_VALIDATOR_PATH, post(handle_register_validator::<S, A>))
30-
.route(SUBMIT_BLOCK_PATH, post(handle_submit_block::<S, A>));
33+
.route(
34+
REGISTER_VALIDATOR_PATH,
35+
post(handle_register_validator::<S, A>)
36+
.route_layer(DefaultBodyLimit::max(MAX_SIZE_REGISTER_VALIDATOR_REQUEST)),
37+
)
38+
.route(
39+
SUBMIT_BLOCK_PATH,
40+
post(handle_submit_block::<S, A>)
41+
.route_layer(DefaultBodyLimit::max(MAX_SIZE_SUBMIT_BLOCK_RESPONSE)),
42+
); // header is smaller than the response but err on the safe side
3143
let reload_router = Router::new().route(RELOAD_PATH, post(handle_reload::<S, A>));
32-
3344
let builder_api = Router::new().nest(BUILDER_API_PATH, builder_routes).merge(reload_router);
3445

3546
let app = if let Some(extra_routes) = A::extra_routes() {
@@ -58,7 +69,7 @@ pub async fn tracing_middleware(req: Request, next: Next) -> Response {
5869
trace!(
5970
http.method = %req.method(),
6071
http.user_agent = req.headers().typed_get::<UserAgent>().map(|ua| ua.to_string()).unwrap_or_default(),
61-
http.content_type = req.headers().typed_get::<ContentType>().map(|ua| ua.to_string()).unwrap_or_default(),
72+
http.content_type = req.headers().typed_get::<ContentType>().map(|ua| ua.to_string()).unwrap_or_default(),
6273
"start request");
6374

6475
let response = next.run(req).await;

tests/src/mock_relay.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use cb_common::{
2525
types::Chain,
2626
utils::{blst_pubkey_to_alloy, timestamp_of_slot_start_sec},
2727
};
28-
use cb_pbs::MAX_SIZE_SUBMIT_BLOCK;
28+
use cb_pbs::MAX_SIZE_SUBMIT_BLOCK_RESPONSE;
2929
use tokio::net::TcpListener;
3030
use tracing::debug;
3131
use tree_hash::TreeHash;
@@ -136,7 +136,7 @@ async fn handle_register_validator(
136136
async fn handle_submit_block(State(state): State<Arc<MockRelayState>>) -> Response {
137137
state.received_submit_block.fetch_add(1, Ordering::Relaxed);
138138
if state.large_body() {
139-
(StatusCode::OK, Json(vec![1u8; 1 + MAX_SIZE_SUBMIT_BLOCK])).into_response()
139+
(StatusCode::OK, Json(vec![1u8; 1 + MAX_SIZE_SUBMIT_BLOCK_RESPONSE])).into_response()
140140
} else {
141141
let response = SubmitBlindedBlockResponse::default();
142142
(StatusCode::OK, Json(response)).into_response()

0 commit comments

Comments
 (0)