Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
71 changes: 1 addition & 70 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions protocols/floodsub/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## 0.48.0

- Replace the duplicate-message cuckoo filter with a bounded cache, removing the `rand` 0.7.3 dependency.
See [issue 6419](https://github.com/libp2p/rust-libp2p/issues/6419).

- Raise MSRV to 1.88.0.
See [PR 6273](https://github.com/libp2p/rust-libp2p/pull/6273).

Expand Down
4 changes: 2 additions & 2 deletions protocols/floodsub/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ categories = ["network-programming", "asynchronous"]

[dependencies]
asynchronous-codec = { workspace = true }
cuckoofilter = "0.5.0"
fnv = "1.0"
bytes.workspace = true
fnv = "1.0"
futures = { workspace = true }
hashlink = { workspace = true }
libp2p-core = { workspace = true }
libp2p-swarm = { workspace = true }
libp2p-identity = { workspace = true }
Expand Down
40 changes: 12 additions & 28 deletions protocols/floodsub/src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,14 @@
// DEALINGS IN THE SOFTWARE.

use std::{
collections::{
VecDeque,
hash_map::{DefaultHasher, HashMap},
},
collections::{HashMap, VecDeque},
iter,
task::{Context, Poll},
};

use bytes::Bytes;
use cuckoofilter::{CuckooError, CuckooFilter};
use fnv::FnvHashSet;
use hashlink::LruCache;
use libp2p_core::{Endpoint, Multiaddr, transport::PortUse};
use libp2p_identity::PeerId;
use libp2p_swarm::{
Expand All @@ -49,6 +46,9 @@ use crate::{
topic::Topic,
};

// Limit the number of received messages retained for deduplication.
const RECEIVED_CACHE_CAPACITY: usize = 1 << 16;

#[deprecated = "Use `Behaviour` instead."]
pub type Floodsub = Behaviour;

Expand All @@ -71,9 +71,9 @@ pub struct Behaviour {
// erroneously.
subscribed_topics: SmallVec<[Topic; 16]>,

// We keep track of the messages we received (in the format `hash(source ID, seq_no)`) so that
// we don't dispatch the same message twice if we receive it twice on the network.
received: CuckooFilter<DefaultHasher>,
// We keep track of the messages we received so that we don't dispatch the same message twice
// if we receive it twice on the network.
received: LruCache<FloodsubMessage, ()>,
}

impl Behaviour {
Expand All @@ -90,7 +90,7 @@ impl Behaviour {
target_peers: FnvHashSet::default(),
connected_peers: HashMap::new(),
subscribed_topics: SmallVec::new(),
received: CuckooFilter::new(),
received: LruCache::new(RECEIVED_CACHE_CAPACITY),
}
}

Expand Down Expand Up @@ -235,13 +235,7 @@ impl Behaviour {
.iter()
.any(|t| message.topics.iter().any(|u| t == u));
if self_subscribed {
if let Err(e @ CuckooError::NotEnoughSpace) = self.received.add(&message) {
tracing::warn!(
"Message was added to 'received' Cuckoofilter but some \
other message was removed as a consequence: {}",
e,
);
}
self.received.insert(message.clone(), ());
if self.config.subscribe_local_messages {
self.events
.push_back(ToSwarm::GenerateEvent(Event::Message(message.clone())));
Expand Down Expand Up @@ -420,18 +414,8 @@ impl NetworkBehaviour for Behaviour {

for message in event.messages {
// Use `self.received` to skip the messages that we have already received in the past.
// Note that this can result in false positives.
match self.received.test_and_add(&message) {
Ok(true) => {} // Message was added.
Ok(false) => continue, // Message already existed.
Err(e @ CuckooError::NotEnoughSpace) => {
// Message added, but some other removed.
tracing::warn!(
"Message was added to 'received' Cuckoofilter but some \
other message was removed as a consequence: {}",
e,
);
}
if self.received.insert(message.clone(), ()).is_some() {
continue;
}

// Add the message to be dispatched to the user.
Expand Down
Loading