mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
fix: mark response correctly (#966)
This commit is contained in:
@ -11,14 +11,14 @@ pub type RequestResult<T> = Result<T, RequestError>;
|
||||
/// Result with [PeerId]
|
||||
pub type PeerRequestResult<T> = RequestResult<WithPeerId<T>>;
|
||||
|
||||
/// 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<Vec<Header>> {
|
||||
fn is_likely_a_bad_message(&self, request: &HeadersRequest) -> bool {
|
||||
impl EthResponseValidator for RequestResult<Vec<Header>> {
|
||||
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<Vec<Header>> {
|
||||
}
|
||||
|
||||
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,
|
||||
|
||||
@ -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<Vec<Header>>,
|
||||
) -> Option<BlockResponseOutcome> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user