Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

protocols/gossipsub: Do not overwrite msg's peers if put again into mcache #2493

Merged
merged 3 commits into from
Feb 14, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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