Skip to content

Commit 2b4b33d

Browse files
rahulksnvaltonen
andauthored
Check for parent of first ready block being on chain (#1812)
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 #493. --------- Co-authored-by: Aaro Altonen <[email protected]>
1 parent 93d9c8c commit 2b4b33d

File tree

2 files changed

+421
-1
lines changed

2 files changed

+421
-1
lines changed

substrate/client/network/sync/src/blocks.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,31 @@ impl<B: BlockT> BlockCollection<B> {
212212
ready
213213
}
214214

215+
/// Returns the block header of the first block that is ready for importing.
216+
/// `from` is the maximum block number for the start of the range that we are interested in.
217+
/// The function will return None if the first block ready is higher than `from`.
218+
/// The logic is structured to be consistent with ready_blocks().
219+
pub fn first_ready_block_header(&self, from: NumberFor<B>) -> Option<B::Header> {
220+
let mut prev = from;
221+
for (&start, range_data) in &self.blocks {
222+
if start > prev {
223+
break
224+
}
225+
226+
match range_data {
227+
BlockRangeState::Complete(blocks) => {
228+
let len = (blocks.len() as u32).into();
229+
prev = start + len;
230+
if let Some(BlockData { block, .. }) = blocks.first() {
231+
return block.header.clone()
232+
}
233+
},
234+
_ => continue,
235+
}
236+
}
237+
None
238+
}
239+
215240
pub fn clear_queued(&mut self, hash: &B::Hash) {
216241
if let Some((from, to)) = self.queued_blocks.remove(hash) {
217242
let mut block_num = from;

0 commit comments

Comments
 (0)