Apply suggestions from code review

Co-authored-by: hinto-janai <hinto.janai@protonmail.com>
This commit is contained in:
Boog900 2024-06-20 02:01:13 +01:00 committed by GitHub
parent 96d4ae5c3f
commit c79ea87ac8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 22 additions and 12 deletions

View file

@ -78,6 +78,7 @@ pub struct BlockDownloaderConfig {
pub initial_batch_size: usize,
}
/// An error that occurred in the [`BlockDownloader`].
#[derive(Debug, thiserror::Error)]
pub enum BlockDownloadError {
#[error("A request to a peer timed out.")]
@ -110,7 +111,7 @@ pub enum ChainSvcRequest {
/// The response type for the chain service.
pub enum ChainSvcResponse {
/// The response for [`ChainSvcRequest::CompactHistory`]
/// The response for [`ChainSvcRequest::CompactHistory`].
CompactHistory {
/// A list of blocks IDs in our chain, starting with the most recent block, all the way to the genesis block.
///
@ -119,9 +120,12 @@ pub enum ChainSvcResponse {
/// The current cumulative difficulty of the chain.
cumulative_difficulty: u128,
},
/// The response for [`ChainSvcRequest::FindFirstUnknown`], contains the index of the first unknown
/// block and its expected height.
/// The response for [`ChainSvcRequest::FindFirstUnknown`].
///
/// Contains the index of the first unknown block and its expected height.
FindFirstUnknown(usize, u64),
/// The response for [`ChainSvcRequest::CumulativeDifficulty`].
///
/// The current cumulative difficulty of our chain.
CumulativeDifficulty(u128),
}
@ -241,7 +245,7 @@ struct BlockDownloader<N: NetworkZone, S, C> {
/// The amount of blocks to request in the next batch.
amount_of_blocks_to_request: usize,
/// The height at which `amount_of_blocks_to_request` was updated.
/// The height at which [`Self::amount_of_blocks_to_request`] was updated.
amount_of_blocks_to_request_updated_at: u64,
/// The amount of consecutive empty chain entries we received.
@ -268,10 +272,10 @@ struct BlockDownloader<N: NetworkZone, S, C> {
/// A queue of ready batches.
ready_batches: BinaryHeap<ReadyQueueBatch>,
/// The size, in bytes, of all the batches in `ready_batches`.
/// The size, in bytes, of all the batches in [`Self::ready_batches`].
ready_batches_size: usize,
/// A queue of start heights from failed batches that should be retried.
/// A queue of start heights from failed batches that should be retried.
///
/// Wrapped in [`Reverse`] so we prioritize early batches.
failed_batches: BinaryHeap<Reverse<u64>>,
@ -359,9 +363,10 @@ where
self.ready_batches_size
);
if self.inflight_requests.is_empty() {
panic!("We need requests inflight to be able to send the request again");
}
assert!(
!self.inflight_requests.is_empty(),
"We need requests inflight to be able to send the request again",
);
let oldest_ready_batch = self.ready_batches.peek().unwrap().start_height;

View file

@ -23,7 +23,7 @@ pub(crate) struct ChainEntry<N: NetworkZone> {
pub struct BlocksToRetrieve<N: NetworkZone> {
/// The block IDs to get.
pub ids: ByteArrayVec<32>,
/// The expected height of the first block in `ids`.
/// The expected height of the first block in [`BlocksToRetrieve::ids`].
pub start_height: u64,
/// The peer who told us about this batch.
pub peer_who_told_us: InternalPeerID<N::Addr>,
@ -97,6 +97,9 @@ impl<N: NetworkZone> ChainTracker<N> {
}
/// Returns the total number of queued batches for a certain `batch_size`.
///
/// # Panics
/// This functions panics if `batch_size` is `0`.
pub fn block_requests_queued(&self, batch_size: usize) -> usize {
self.entries
.iter()
@ -152,8 +155,10 @@ impl<N: NetworkZone> ChainTracker<N> {
let entry = self.entries.front_mut()?;
// Calculate the ending index for us to get in this batch, will be the smallest out of `max_blocks`, the length of the batch or
// the index of the next pruned block for this seed.
// Calculate the ending index for us to get in this batch, it will be one of these:
// - smallest out of `max_blocks`
// - length of the batch
// - index of the next pruned block for this seed
let end_idx = min(
min(entry.ids.len(), max_blocks),
usize::try_from(