Skip to content

Commit 4685a77

Browse files
committed
test(rpc): cover BatchSizeLimitMiddleware with max_entries=0
The middleware's `max_entries = 0` corner case (allowed in-process because `BatchSizeLimitMiddleware::new` has no guard, even though the CLI value parser `parse_max_batch_entries` rejects 0) was untested. Add a focused test that locks in the documented behaviour: any non-empty batch is rejected with `BATCH_TOO_LARGE_ERROR_CODE`, and the error message names the offending size and limit. An empty-batch pass-through test was considered and dropped: the `MockRpcService` itself rejects empty batches with a different error code (-32600 'Invalid request'), and asserting on the final response shape would lock in that quirk rather than the middleware's contract. The empty-batch case is left to the JSON-RPC layer below the middleware.
1 parent a85368c commit 4685a77

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

‎crates/evm-node/src/rpc_middleware.rs‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,6 +1179,33 @@ mod tests {
11791179
assert_eq!(response.as_error_code(), Some(BATCH_TOO_LARGE_ERROR_CODE));
11801180
}
11811181

1182+
/// `max_entries = 0` is rejected at the CLI level by
1183+
/// `parse_max_batch_entries`, but the in-process constructor has no
1184+
/// such guard — callers can build a "no batches allowed" layer
1185+
/// intentionally. The middleware must reject any non-empty batch
1186+
/// with `BATCH_TOO_LARGE_ERROR_CODE`. The empty-batch case is
1187+
/// covered by the JSON-RPC layer below the middleware and is
1188+
/// deliberately not asserted here, because the mock service
1189+
/// itself rejects empty batches with a different error code and
1190+
/// we don't want to lock in that quirk.
1191+
#[tokio::test]
1192+
async fn test_batch_size_limit_zero_max_entries_rejects_non_empty() {
1193+
let middleware = BatchSizeLimitMiddleware::new(MockRpcService, 0);
1194+
let batch = Batch::from(vec![Ok(make_call_entry("eth_blockNumber", 1))]);
1195+
let response = middleware.batch(batch).await;
1196+
assert_eq!(
1197+
response.as_error_code(),
1198+
Some(BATCH_TOO_LARGE_ERROR_CODE),
1199+
"max_entries=0 must reject any non-empty batch"
1200+
);
1201+
let json: serde_json::Value = serde_json::from_str(response.into_json().get()).unwrap();
1202+
let message = json["error"]["message"].as_str().unwrap_or("");
1203+
assert!(
1204+
message.contains("batch size 1") && message.contains("limit of 0"),
1205+
"error message should name the offending size and limit; got: {message}"
1206+
);
1207+
}
1208+
11821209
#[tokio::test]
11831210
async fn test_batch_size_limit_applies_when_pending_filter_disabled() {
11841211
// The batch cap must apply even when --arc.expose-pending-txs disables

0 commit comments

Comments
 (0)