Do not send messages twice in bitfield distribution (#2005)

* Do not send messages twice in bitfield distribution

This removes a bug which resulted in sending bitfield messages multiple
times by not checking if we already relayed them. Besides that it also
adds an optimization to not relay a message to a peer that send us
this message.

* Review comments

* Break some lines
This commit is contained in:
Bastian Köcher
2020-11-24 22:40:36 +01:00
committed by GitHub
parent 74efba2a21
commit 884b432035
4 changed files with 233 additions and 65 deletions
@@ -166,6 +166,15 @@ impl<M> NetworkBridgeEvent<M> {
pub struct View(pub Vec<Hash>);
impl View {
/// Replace `self` with `new`.
///
/// Returns an iterator that will yield all elements of `new` that were not part of `self`.
pub fn replace_difference(&mut self, new: View) -> impl Iterator<Item = &Hash> {
let old = std::mem::replace(self, new);
self.0.iter().filter(move |h| !old.contains(h))
}
/// Returns an iterator of the hashes present in `Self` but not in `other`.
pub fn difference<'a>(&'a self, other: &'a View) -> impl Iterator<Item = &'a Hash> + 'a {
self.0.iter().filter(move |h| !other.contains(h))