Skip to content

Commit

Permalink
fix: block downloader polling
Browse files Browse the repository at this point in the history
  • Loading branch information
klkvr committed Oct 22, 2024
1 parent 3174bd5 commit 5532d34
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions crates/engine/tree/src/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,6 @@ where

/// Advances the download process.
fn poll(&mut self, cx: &mut Context<'_>) -> Poll<DownloadOutcome> {
if let Some(pending_event) = self.pop_pending_event() {
return Poll::Ready(pending_event);
}

// advance all full block requests
for idx in (0..self.inflight_full_block_requests.len()).rev() {
let mut request = self.inflight_full_block_requests.swap_remove(idx);
Expand Down Expand Up @@ -235,25 +231,33 @@ where

self.update_block_download_metrics();

if self.set_buffered_blocks.is_empty() {
return Poll::Pending;
if !self.set_buffered_blocks.is_empty() {
// drain all unique element of the block buffer if there are any
let mut downloaded_blocks: Vec<SealedBlockWithSenders> =
Vec::with_capacity(self.set_buffered_blocks.len());
while let Some(block) = self.set_buffered_blocks.pop() {
// peek ahead and pop duplicates
while let Some(peek) = self.set_buffered_blocks.peek_mut() {
if peek.0 .0.hash() == block.0 .0.hash() {
PeekMut::pop(peek);
} else {
break
}
}
downloaded_blocks.push(block.0.into());
}

self.push_pending_event(DownloadOutcome::Blocks(downloaded_blocks));
}

// drain all unique element of the block buffer if there are any
let mut downloaded_blocks: Vec<SealedBlockWithSenders> =
Vec::with_capacity(self.set_buffered_blocks.len());
while let Some(block) = self.set_buffered_blocks.pop() {
// peek ahead and pop duplicates
while let Some(peek) = self.set_buffered_blocks.peek_mut() {
if peek.0 .0.hash() == block.0 .0.hash() {
PeekMut::pop(peek);
} else {
break
}
if let Some(pending_event) = self.pop_pending_event() {
if !self.pending_events.is_empty() {
cx.waker().wake_by_ref();
}
downloaded_blocks.push(block.0.into());
return Poll::Ready(pending_event);
}
Poll::Ready(DownloadOutcome::Blocks(downloaded_blocks))

Poll::Pending
}
}

Expand Down

0 comments on commit 5532d34

Please sign in to comment.