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

Co-authored-by: Max Inden <mail@max-inden.de>
This commit is contained in:
Divma
2022-02-14 09:49:00 -05:00
committed by GitHub
parent 60666f5455
commit e66f04f41b
2 changed files with 20 additions and 16 deletions

View File

@ -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]

View File

@ -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},
@ -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);
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(),
};
if self
.msgs
.insert(message_id.clone(), (msg, HashSet::new()))
.is_none()
{
// Don't add duplicate entries to the cache.
entry.insert((msg, HashSet::default()));
self.history[0].push(cache_entry);
return true;
} else {
return false;
trace!("Put message {:?} in mcache", message_id);
true
}
}
}