Skip to content

Commit e66f04f

Browse files
protocols/gossipsub: Do not overwrite msg's peers if put again into mcache (#2493)
Co-authored-by: Max Inden <[email protected]>
1 parent 60666f5 commit e66f04f

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

protocols/gossipsub/CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@
88

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

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

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

15+
- Do not overwrite msg's peers if put again into mcache (see [PR 2493]).
16+
1517
[PR 2442]: https://github.com/libp2p/rust-libp2p/pull/2442
1618
[PR 2481]: https://github.com/libp2p/rust-libp2p/pull/2481
1719
[PR 2445]: https://github.com/libp2p/rust-libp2p/pull/2445
1820
[PR 2506]: https://github.com/libp2p/rust-libp2p/pull/2506
21+
[PR 2493]: https://github.com/libp2p/rust-libp2p/pull/2493
1922

2023
# 0.35.0 [2022-01-27]
2124

protocols/gossipsub/src/mcache.rs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use crate::topic::TopicHash;
2222
use crate::types::{MessageId, RawGossipsubMessage};
2323
use libp2p_core::PeerId;
2424
use log::{debug, trace};
25+
use std::collections::hash_map::Entry;
2526
use std::fmt::Debug;
2627
use std::{
2728
collections::{HashMap, HashSet},
@@ -73,22 +74,22 @@ impl MessageCache {
7374
///
7475
/// Returns true if the message didn't already exist in the cache.
7576
pub fn put(&mut self, message_id: &MessageId, msg: RawGossipsubMessage) -> bool {
76-
trace!("Put message {:?} in mcache", message_id);
77-
let cache_entry = CacheEntry {
78-
mid: message_id.clone(),
79-
topic: msg.topic.clone(),
80-
};
81-
82-
if self
83-
.msgs
84-
.insert(message_id.clone(), (msg, HashSet::new()))
85-
.is_none()
86-
{
87-
// Don't add duplicate entries to the cache.
88-
self.history[0].push(cache_entry);
89-
return true;
90-
} else {
91-
return false;
77+
match self.msgs.entry(message_id.clone()) {
78+
Entry::Occupied(_) => {
79+
// Don't add duplicate entries to the cache.
80+
false
81+
}
82+
Entry::Vacant(entry) => {
83+
let cache_entry = CacheEntry {
84+
mid: message_id.clone(),
85+
topic: msg.topic.clone(),
86+
};
87+
entry.insert((msg, HashSet::default()));
88+
self.history[0].push(cache_entry);
89+
90+
trace!("Put message {:?} in mcache", message_id);
91+
true
92+
}
9293
}
9394
}
9495

0 commit comments

Comments
 (0)