From 21b393b8f51b6f19e44cb653cb94bce58e87e27b Mon Sep 17 00:00:00 2001 From: ii-cruz Date: Thu, 8 Aug 2024 20:26:08 +0100 Subject: [PATCH] Fix the chunks request component and diffs race condition --- consensus/src/sync/live/state_queue/mod.rs | 30 +++++++++++++--------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/consensus/src/sync/live/state_queue/mod.rs b/consensus/src/sync/live/state_queue/mod.rs index 629d05d7f5..c93df41f9c 100644 --- a/consensus/src/sync/live/state_queue/mod.rs +++ b/consensus/src/sync/live/state_queue/mod.rs @@ -548,18 +548,24 @@ impl Stream for StateQueue { loop { match self.chunk_request_component.poll_next_unpin(cx) { Poll::Ready(Some((chunk, start_key, peer_id))) => { - let key = u32::from_str_radix(&format!("{}00000000", start_key)[..8], 16) - .unwrap_or(0); - - let percentage = (key as f32 / u32::MAX as f32) * 100.0; - log::info!( - ?start_key, - "Received state sync chunk, ~{:.2}% complete", - percentage, - ); - - if let Some(state_chunks) = self.on_chunk_received(chunk, start_key, peer_id) { - return Poll::Ready(Some(state_chunks)); + // We throw away the remaining incoming chunks if we are complete. + // This avoids breaking the state sync invariant new block + no diffs, then no chunks. + if !self.start_key.is_complete() { + let key = u32::from_str_radix(&format!("{}00000000", start_key)[..8], 16) + .unwrap_or(0); + + let percentage = (key as f32 / u32::MAX as f32) * 100.0; + log::info!( + ?start_key, + "Received state sync chunk, ~{:.2}% complete", + percentage, + ); + + if let Some(state_chunks) = + self.on_chunk_received(chunk, start_key, peer_id) + { + return Poll::Ready(Some(state_chunks)); + } } } // If the chunk stream is exhausted, we quit as well.