chore(utils): Util function for headers request (#12501)

This commit is contained in:
Ayodeji Akinola
2024-11-13 18:07:59 +01:00
committed by GitHub
parent 001f3899fd
commit 7a1698c504
3 changed files with 37 additions and 17 deletions

View File

@ -13,7 +13,7 @@ use reth_consensus::Consensus;
use reth_network_p2p::{
error::{DownloadError, DownloadResult, PeerRequestResult},
headers::{
client::{HeadersClient, HeadersDirection, HeadersRequest},
client::{HeadersClient, HeadersRequest},
downloader::{validate_header_download, HeaderDownloader, SyncTarget},
error::{HeadersDownloaderError, HeadersDownloaderResult},
},
@ -60,9 +60,10 @@ impl<H> From<HeadersResponseError> for ReverseHeadersDownloaderError<H> {
/// tries to fill the gap between the local head of the node and the chain tip by issuing multiple
/// requests at a time but yielding them in batches on [`Stream::poll_next`].
///
/// **Note:** This downloader downloads in reverse, see also [`HeadersDirection::Falling`], this
/// means the batches of headers that this downloader yields will start at the chain tip and move
/// towards the local head: falling block numbers.
/// **Note:** This downloader downloads in reverse, see also
/// [`reth_network_p2p::headers::client::HeadersDirection`], this means the batches of headers that
/// this downloader yields will start at the chain tip and move towards the local head: falling
/// block numbers.
#[must_use = "Stream does nothing unless polled"]
#[derive(Debug)]
pub struct ReverseHeadersDownloader<H: HeadersClient> {
@ -567,7 +568,7 @@ where
/// Returns the request for the `sync_target` header.
const fn get_sync_target_request(&self, start: BlockHashOrNumber) -> HeadersRequest {
HeadersRequest { start, limit: 1, direction: HeadersDirection::Falling }
HeadersRequest::falling(start, 1)
}
/// Starts a request future
@ -1216,7 +1217,7 @@ fn calc_next_request(
let diff = next_request_block_number - local_head;
let limit = diff.min(request_limit);
let start = next_request_block_number;
HeadersRequest { start: start.into(), limit, direction: HeadersDirection::Falling }
HeadersRequest::falling(start.into(), limit)
}
#[cfg(test)]

View File

@ -96,11 +96,7 @@ where
start_hash: hash,
count,
request: FullBlockRangeRequest {
headers: Some(client.get_headers(HeadersRequest {
start: hash.into(),
limit: count,
direction: HeadersDirection::Falling,
})),
headers: Some(client.get_headers(HeadersRequest::falling(hash.into(), count))),
bodies: None,
},
client,

View File

@ -21,6 +21,34 @@ pub struct HeadersRequest {
pub direction: HeadersDirection,
}
impl HeadersRequest {
/// Creates a request for a single header (direction doesn't matter).
///
/// # Arguments
/// * `start` - The block hash or number to start from
pub const fn one(start: BlockHashOrNumber) -> Self {
Self { direction: HeadersDirection::Rising, limit: 1, start }
}
/// Creates a request for headers in rising direction (ascending block numbers).
///
/// # Arguments
/// * `start` - The block hash or number to start from
/// * `limit` - Maximum number of headers to retrieve
pub const fn rising(start: BlockHashOrNumber, limit: u64) -> Self {
Self { direction: HeadersDirection::Rising, limit, start }
}
/// Creates a request for headers in falling direction (descending block numbers).
///
/// # Arguments
/// * `start` - The block hash or number to start from
/// * `limit` - Maximum number of headers to retrieve
pub const fn falling(start: BlockHashOrNumber, limit: u64) -> Self {
Self { direction: HeadersDirection::Falling, limit, start }
}
}
/// The headers future type
pub type HeadersFut = Pin<Box<dyn Future<Output = PeerRequestResult<Vec<Header>>> + Send + Sync>>;
@ -57,12 +85,7 @@ pub trait HeadersClient: DownloadClient {
start: BlockHashOrNumber,
priority: Priority,
) -> SingleHeaderRequest<Self::Output> {
let req = HeadersRequest {
start,
limit: 1,
// doesn't matter for a single header
direction: HeadersDirection::Rising,
};
let req = HeadersRequest::one(start);
let fut = self.get_headers_with_priority(req, priority);
SingleHeaderRequest { fut }
}