Skip to content

Commit

Permalink
protocols/gossipsub: Do not overwrite msg's peers if put again into m…
Browse files Browse the repository at this point in the history
…cache (libp2p#2493)

Co-authored-by: Max Inden <mail@max-inden.de>
  • Loading branch information
divagant-martian and mxinden authored Feb 14, 2022
1 parent 60666f5 commit e66f04f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
5 changes: 4 additions & 1 deletion protocols/gossipsub/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@

- Emit gossip of all non empty topics (see [PR 2481]).

- Merge NetworkBehaviour's inject_\* paired methods (see PR 2445).
- Merge NetworkBehaviour's inject_\* paired methods (see [PR 2445]).

- Revert to wasm-timer (see [PR 2506]).

- Do not overwrite msg's peers if put again into mcache (see [PR 2493]).

[PR 2442]: https://github.com/libp2p/rust-libp2p/pull/2442
[PR 2481]: https://github.com/libp2p/rust-libp2p/pull/2481
[PR 2445]: https://github.com/libp2p/rust-libp2p/pull/2445
[PR 2506]: https://github.com/libp2p/rust-libp2p/pull/2506
[PR 2493]: https://github.com/libp2p/rust-libp2p/pull/2493

# 0.35.0 [2022-01-27]

Expand Down
33 changes: 17 additions & 16 deletions protocols/gossipsub/src/mcache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use crate::topic::TopicHash;
use crate::types::{MessageId, RawGossipsubMessage};
use libp2p_core::PeerId;
use log::{debug, trace};
use std::collections::hash_map::Entry;
use std::fmt::Debug;
use std::{
collections::{HashMap, HashSet},
Expand Down Expand Up @@ -73,22 +74,22 @@ impl MessageCache {
///
/// Returns true if the message didn't already exist in the cache.
pub fn put(&mut self, message_id: &MessageId, msg: RawGossipsubMessage) -> bool {
trace!("Put message {:?} in mcache", message_id);
let cache_entry = CacheEntry {
mid: message_id.clone(),
topic: msg.topic.clone(),
};

if self
.msgs
.insert(message_id.clone(), (msg, HashSet::new()))
.is_none()
{
// Don't add duplicate entries to the cache.
self.history[0].push(cache_entry);
return true;
} else {
return false;
match self.msgs.entry(message_id.clone()) {
Entry::Occupied(_) => {
// Don't add duplicate entries to the cache.
false
}
Entry::Vacant(entry) => {
let cache_entry = CacheEntry {
mid: message_id.clone(),
topic: msg.topic.clone(),
};
entry.insert((msg, HashSet::default()));
self.history[0].push(cache_entry);

trace!("Put message {:?} in mcache", message_id);
true
}
}
}

Expand Down

0 comments on commit e66f04f

Please sign in to comment.