Skip to content

Commit

Permalink
also consider allow self origin when using a published message ids di…
Browse files Browse the repository at this point in the history
…ct (libp2p#84)

* also consider allow self origin when using a published message ids dictionary

* make allow_self_origin configurable via the config builder
  • Loading branch information
blacktemplar authored Oct 21, 2020
1 parent 201d533 commit 87633a3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
17 changes: 9 additions & 8 deletions protocols/gossipsub/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,9 @@ where
// If the message is anonymous or has a random author add it to the published message ids
// cache.
if let PublishConfig::RandomAuthor | PublishConfig::Anonymous = self.publish_config {
self.published_message_ids.insert(msg_id.clone());
if !self.config.allow_self_origin() {
self.published_message_ids.insert(msg_id.clone());
}
}

// If we are not flood publishing forward the message to mesh peers.
Expand Down Expand Up @@ -1429,13 +1431,12 @@ where
}

// reject messages claiming to be from ourselves but not locally published
let self_published = if let Some(own_id) = self.publish_config.get_own_id() {
!self.config.allow_self_origin()
&& own_id != propagation_source
&& msg.source.as_ref().map_or(false, |s| s == own_id)
} else {
self.published_message_ids.contains(msg.message_id())
};
let self_published = !self.config.allow_self_origin()
&& if let Some(own_id) = self.publish_config.get_own_id() {
own_id != propagation_source && msg.source.as_ref().map_or(false, |s| s == own_id)
} else {
self.published_message_ids.contains(msg.message_id())
};

if self_published {
debug!(
Expand Down
8 changes: 8 additions & 0 deletions protocols/gossipsub/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,14 @@ impl<T: Clone> GenericGossipsubConfigBuilder<T> {
self
}

/// By default, gossipsub will reject messages that are sent to us that has the same message
/// source as we have specified locally. Enabling this, allows these messages and prevents
/// penalizing the peer that sent us the message. Default is false.
pub fn allow_self_origin(&mut self, allow_self_origin: bool) -> &mut Self {
self.config.allow_self_origin = allow_self_origin;
self
}

pub fn iwant_followup_time(&mut self, iwant_followup_time: Duration) -> &mut Self {
self.config.iwant_followup_time = iwant_followup_time;
self
Expand Down

0 comments on commit 87633a3

Please sign in to comment.