Skip to content

Commit ebcc1a7

Browse files
committed
fix: handle empty responses as failed
1 parent 2162947 commit ebcc1a7

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

crates/net/p2p/src/req_resp/handlers.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use rand::seq::SliceRandom;
44
use tokio::time::Duration;
55
use tracing::{debug, error, info, warn};
66

7-
use ethlambda_types::block::SignedBlockWithAttestation;
87
use ethlambda_types::primitives::ssz::TreeHash;
8+
use ethlambda_types::{block::SignedBlockWithAttestation, primitives::H256};
99

1010
use super::{
1111
BLOCKS_BY_ROOT_PROTOCOL_V1, BlocksByRootRequest, Request, Response, ResponsePayload, Status,
@@ -59,7 +59,7 @@ pub async fn handle_req_resp_message(
5959

6060
// Check if this was a block fetch request
6161
if let Some(root) = server.request_id_map.remove(&request_id) {
62-
handle_fetch_failure(server, root, peer, error).await;
62+
handle_fetch_failure(server, root, peer).await;
6363
}
6464
}
6565
request_response::Event::InboundFailure {
@@ -144,6 +144,13 @@ async fn handle_blocks_by_root_response(
144144
return;
145145
};
146146

147+
if blocks.is_empty() {
148+
server.request_id_map.insert(request_id, requested_root);
149+
warn!(%peer, "Received empty BlocksByRoot response");
150+
handle_fetch_failure(server, requested_root, peer).await;
151+
return;
152+
}
153+
147154
for block in blocks {
148155
let root = block.message.block.tree_hash_root();
149156

@@ -184,10 +191,7 @@ pub fn build_status(store: &Store) -> Status {
184191

185192
/// Fetch a missing block from a random connected peer.
186193
/// Handles tracking in both pending_requests and request_id_map.
187-
pub async fn fetch_block_from_peer(
188-
server: &mut P2PServer,
189-
root: ethlambda_types::primitives::H256,
190-
) -> bool {
194+
pub async fn fetch_block_from_peer(server: &mut P2PServer, root: H256) -> bool {
191195
if server.connected_peers.is_empty() {
192196
warn!(%root, "Cannot fetch block: no connected peers");
193197
return false;
@@ -240,18 +244,13 @@ pub async fn fetch_block_from_peer(
240244
true
241245
}
242246

243-
async fn handle_fetch_failure(
244-
server: &mut P2PServer,
245-
root: ethlambda_types::primitives::H256,
246-
peer: PeerId,
247-
error: request_response::OutboundFailure,
248-
) {
247+
async fn handle_fetch_failure(server: &mut P2PServer, root: H256, peer: PeerId) {
249248
let Some(pending) = server.pending_requests.get_mut(&root) else {
250249
return;
251250
};
252251

253252
if pending.attempts >= MAX_FETCH_RETRIES {
254-
error!(%root, %peer, attempts=%pending.attempts, %error,
253+
error!(%root, %peer, attempts=%pending.attempts,
255254
"Block fetch failed after max retries, giving up");
256255
server.pending_requests.remove(&root);
257256
return;
@@ -260,8 +259,7 @@ async fn handle_fetch_failure(
260259
let backoff_ms = INITIAL_BACKOFF_MS * BACKOFF_MULTIPLIER.pow(pending.attempts - 1);
261260
let backoff = Duration::from_millis(backoff_ms);
262261

263-
warn!(%root, %peer, attempts=%pending.attempts, ?backoff, %error,
264-
"Block fetch failed, scheduling retry");
262+
warn!(%root, %peer, attempts=%pending.attempts, ?backoff, "Block fetch failed, scheduling retry");
265263

266264
pending.attempts += 1;
267265

0 commit comments

Comments
 (0)