Make sure we remove a peer on disconnect in gossip (#5104)

* Make sure we remove peers on disconnect in gossip state machine

* Clear up the code

* Add a comment
This commit is contained in:
Bastian Köcher
2020-03-02 18:20:04 +01:00
committed by GitHub
parent 9ab9134ea5
commit d3244b728a
3 changed files with 63 additions and 16 deletions
@@ -104,8 +104,7 @@ impl<B: BlockT> BlockCollection<B> {
common: NumberFor<B>,
max_parallel: u32,
max_ahead: u32,
) -> Option<Range<NumberFor<B>>>
{
) -> Option<Range<NumberFor<B>>> {
if peer_best <= common {
// Bail out early
return None;
@@ -165,20 +164,20 @@ impl<B: BlockT> BlockCollection<B> {
pub fn drain(&mut self, from: NumberFor<B>) -> Vec<BlockData<B>> {
let mut drained = Vec::new();
let mut ranges = Vec::new();
{
let mut prev = from;
for (start, range_data) in &mut self.blocks {
match range_data {
&mut BlockRangeState::Complete(ref mut blocks) if *start <= prev => {
prev = *start + (blocks.len() as u32).into();
let mut blocks = mem::replace(blocks, Vec::new());
drained.append(&mut blocks);
ranges.push(*start);
},
_ => break,
}
let mut prev = from;
for (start, range_data) in &mut self.blocks {
match range_data {
&mut BlockRangeState::Complete(ref mut blocks) if *start <= prev => {
prev = *start + (blocks.len() as u32).into();
// Remove all elements from `blocks` and add them to `drained`
drained.append(blocks);
ranges.push(*start);
},
_ => break,
}
}
for r in ranges {
self.blocks.remove(&r);
}