Skip to content

Commit

Permalink
Check for parent of first ready block being on chain (paritytech#1812)
Browse files Browse the repository at this point in the history
When retrieving the ready blocks, verify that the parent of the first
ready block is on chain. If the parent is not on chain, we are
downloading from a fork. In this case, keep downloading until we have a
parent on chain (common ancestor).

Resolves paritytech#493.

---------

Co-authored-by: Aaro Altonen <48052676+altonen@users.noreply.github.com>
  • Loading branch information
rahulksnv and altonen authored Oct 10, 2023
1 parent 60e6c5e commit 2151bf6
Show file tree
Hide file tree
Showing 2 changed files with 421 additions and 1 deletion.
25 changes: 25 additions & 0 deletions substrate/client/network/sync/src/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,31 @@ impl<B: BlockT> BlockCollection<B> {
ready
}

/// Returns the block header of the first block that is ready for importing.
/// `from` is the maximum block number for the start of the range that we are interested in.
/// The function will return None if the first block ready is higher than `from`.
/// The logic is structured to be consistent with ready_blocks().
pub fn first_ready_block_header(&self, from: NumberFor<B>) -> Option<B::Header> {
let mut prev = from;
for (&start, range_data) in &self.blocks {
if start > prev {
break
}

match range_data {
BlockRangeState::Complete(blocks) => {
let len = (blocks.len() as u32).into();
prev = start + len;
if let Some(BlockData { block, .. }) = blocks.first() {
return block.header.clone()
}
},
_ => continue,
}
}
None
}

pub fn clear_queued(&mut self, hash: &B::Hash) {
if let Some((from, to)) = self.queued_blocks.remove(hash) {
let mut block_num = from;
Expand Down
Loading

0 comments on commit 2151bf6

Please sign in to comment.