This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
sync: Fixed clearing subsequent ranges #11815
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -180,6 +180,9 @@ impl<B: BlockT> BlockCollection<B> { | |
|
|
||
| /// Get a valid chain of blocks ordered in descending order and ready for importing into | ||
| /// the blockchain. | ||
| /// `from` is the maximum block number for the start of the range that we are interested in. | ||
| /// The function will return empty Vec if the first block ready is higher than `from`. | ||
| /// For each returned block hash `clear_queued` must be called at some later stage. | ||
| pub fn ready_blocks(&mut self, from: NumberFor<B>) -> Vec<BlockData<B>> { | ||
| let mut ready = Vec::new(); | ||
|
|
||
|
|
@@ -192,6 +195,10 @@ impl<B: BlockT> BlockCollection<B> { | |
| BlockRangeState::Complete(blocks) => { | ||
| let len = (blocks.len() as u32).into(); | ||
| prev = start + len; | ||
| if let Some(BlockData { block, .. }) = blocks.first() { | ||
| self.queued_blocks | ||
| .insert(block.hash, (start, start + (blocks.len() as u32).into())); | ||
| } | ||
| // Remove all elements from `blocks` and add them to `ready` | ||
| ready.append(blocks); | ||
| len | ||
|
|
@@ -201,18 +208,12 @@ impl<B: BlockT> BlockCollection<B> { | |
| }; | ||
| *range_data = BlockRangeState::Queued { len }; | ||
| } | ||
|
|
||
| if let Some(BlockData { block, .. }) = ready.first() { | ||
| self.queued_blocks | ||
| .insert(block.hash, (from, from + (ready.len() as u32).into())); | ||
| } | ||
|
|
||
| trace!(target: "sync", "{} blocks ready for import", ready.len()); | ||
| ready | ||
| } | ||
|
|
||
| pub fn clear_queued(&mut self, from_hash: &B::Hash) { | ||
| if let Some((from, to)) = self.queued_blocks.remove(from_hash) { | ||
| pub fn clear_queued(&mut self, hash: &B::Hash) { | ||
| if let Some((from, to)) = self.queued_blocks.remove(hash) { | ||
| let mut block_num = from; | ||
| while block_num < to { | ||
| self.blocks.remove(&block_num); | ||
|
|
@@ -399,4 +400,35 @@ mod test { | |
|
|
||
| assert_eq!(bc.needed_blocks(peer.clone(), 5, 50, 39, 0, 200), Some(45..50)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn clear_queued_subsequent_ranges() { | ||
| let mut bc = BlockCollection::new(); | ||
| assert!(is_empty(&bc)); | ||
| let peer = PeerId::random(); | ||
|
|
||
| let blocks = generate_blocks(10); | ||
|
|
||
| // Request 2 ranges | ||
| assert_eq!(bc.needed_blocks(peer.clone(), 5, 50, 39, 0, 200), Some(40..45)); | ||
| assert_eq!(bc.needed_blocks(peer.clone(), 5, 50, 39, 0, 200), Some(45..50)); | ||
|
|
||
| // got a response on the request for `40..50` | ||
| bc.clear_peer_download(&peer); | ||
| bc.insert(40, blocks.to_vec(), peer.clone()); | ||
|
|
||
| // request any blocks starting from 1000 or lower. | ||
| let ready = bc.ready_blocks(1000); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe a comment indicating why we use 1000 rather than 41. |
||
| assert_eq!( | ||
| ready, | ||
| blocks | ||
| .iter() | ||
| .map(|b| BlockData { block: b.clone(), origin: Some(peer.clone()) }) | ||
| .collect::<Vec<_>>() | ||
| ); | ||
|
|
||
| bc.clear_queued(&blocks[0].hash); | ||
| assert!(bc.blocks.is_empty()); | ||
| assert!(bc.queued_blocks.is_empty()); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMHO this should be documented. The "problem" is now that
clear_queuedshould be called for every hash ofready. We already do this, but someone could forget this in the future.