diff --git a/crates/interfaces/src/p2p/error.rs b/crates/interfaces/src/p2p/error.rs index a949c04622..dc0f32ee99 100644 --- a/crates/interfaces/src/p2p/error.rs +++ b/crates/interfaces/src/p2p/error.rs @@ -11,14 +11,14 @@ pub type RequestResult = Result; /// Result with [PeerId] pub type PeerRequestResult = RequestResult>; -/// Trait used to validate requests -pub trait RequestValidation { - /// Determine whether the response matches what we requested in request - fn is_likely_a_bad_message(&self, request: &HeadersRequest) -> bool; +/// Helper trait used to validate responses. +pub trait EthResponseValidator { + /// Determine whether the response matches what we requested in [HeadersRequest] + fn is_likely_bad_headers_response(&self, request: &HeadersRequest) -> bool; } -impl RequestValidation for RequestResult> { - fn is_likely_a_bad_message(&self, request: &HeadersRequest) -> bool { +impl EthResponseValidator for RequestResult> { + fn is_likely_bad_headers_response(&self, request: &HeadersRequest) -> bool { match self { Ok(headers) => { let request_length = headers.len() as u64; @@ -28,10 +28,11 @@ impl RequestValidation for RequestResult> { } match request.start { - BlockHashOrNumber::Number(block_number) => { - Some(block_number) != headers.get(0).map(|h| h.number) + BlockHashOrNumber::Number(block_number) => block_number != headers[0].number, + BlockHashOrNumber::Hash(_) => { + // we don't want to hash the header + false } - BlockHashOrNumber::Hash(_) => false, } } Err(_) => true, diff --git a/crates/net/network/src/fetch/mod.rs b/crates/net/network/src/fetch/mod.rs index 0d2dfc18ff..c89dfc7e1d 100644 --- a/crates/net/network/src/fetch/mod.rs +++ b/crates/net/network/src/fetch/mod.rs @@ -4,7 +4,7 @@ use crate::{message::BlockRequest, peers::PeersHandle}; use futures::StreamExt; use reth_eth_wire::{BlockBody, GetBlockBodies, GetBlockHeaders}; use reth_interfaces::p2p::{ - error::{PeerRequestResult, RequestError, RequestResult, RequestValidation}, + error::{EthResponseValidator, PeerRequestResult, RequestError, RequestResult}, headers::client::HeadersRequest, priority::Priority, }; @@ -226,9 +226,12 @@ impl StateFetcher { res: RequestResult>, ) -> Option { let is_error = res.is_err(); + let resp = self.inflight_headers_requests.remove(&peer_id); - let is_likely_a_bad_message = - resp.as_ref().map(|r| res.is_likely_a_bad_message(&r.request)).unwrap_or_default(); + let is_likely_bad_response = resp + .as_ref() + .map(|r| res.is_likely_bad_headers_response(&r.request)) + .unwrap_or_default(); if let Some(resp) = resp { let _ = resp.response.send(res.map(|h| (peer_id, h).into())); @@ -242,13 +245,11 @@ impl StateFetcher { )) } - if !is_likely_a_bad_message { - if let Some(peer) = self.peers.get_mut(&peer_id) { - // If the peer is still ready to be accept new requests, we try to send a followup - // request immediately. - if peer.state.on_request_finished() { - return self.followup_request(peer_id) - } + if let Some(peer) = self.peers.get_mut(&peer_id) { + // If the peer is still ready to be accept new requests, we try to send a followup + // request immediately. + if peer.state.on_request_finished() && !is_likely_bad_response { + return self.followup_request(peer_id) } }