Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
141 commits
Select commit Hold shift + click to select a range
aeba523
Gossipsub message signing
AgeManning May 19, 2020
573e4b6
Update ipfs-private example
AgeManning May 19, 2020
d2babd4
Add optimisation to prevent key calculation each message
AgeManning May 22, 2020
8353474
Support multiple versions
AgeManning May 22, 2020
b7db121
Initial porting of peer_scoring from go-libp2p
AgeManning May 22, 2020
ac3c977
Handle forwarding of signed messages and reviewers comments
AgeManning May 25, 2020
f3ff66b
Merge latest gossipsub signing
AgeManning May 25, 2020
c8335cc
Progress to porting scoring functions
AgeManning May 25, 2020
e9afd95
Merge branch 'master' into gossipsub-signing
AgeManning May 27, 2020
984a662
Finish first port of score params
AgeManning May 27, 2020
80ea7df
Add defaults to scoring
AgeManning May 27, 2020
58f61b4
Generalise topic hashing
AgeManning May 27, 2020
238e305
Update topic hashing structure
AgeManning May 27, 2020
fec112a
Update v1.1 tests
AgeManning May 27, 2020
bc09eb7
Add more unit tests
AgeManning May 27, 2020
3d1a025
protocols/gossipsub/src/protocol: Use quickcheck to test signing (#31)
mxinden Jun 1, 2020
31b6849
Shift signing into behaviour
AgeManning Jun 1, 2020
a8cec0b
Merge latest master
AgeManning Jul 1, 2020
2b38112
Address reviewers suggestions
AgeManning Jul 1, 2020
7d678f5
Send subscriptions to all peers
AgeManning Jul 1, 2020
dc050dd
Update examples
AgeManning Jul 1, 2020
5bf1cfc
Shift signing option into the behaviour
AgeManning Jul 1, 2020
4ffe728
Revert changes to core/
AgeManning Jul 9, 2020
24b738c
Reviewers suggestion
AgeManning Jul 9, 2020
a2e4bf0
Merge remote-tracking branch 'origin/master' into gossipsub-signing
AgeManning Jul 9, 2020
2f20571
Merge latest master
AgeManning Jul 23, 2020
2c50ba2
Revert changes to core/
AgeManning Jul 23, 2020
4de878e
Switch gossipsub state to use sets instead of vectors
rklaehn Jul 23, 2020
e43f0a0
Make tests pass
rklaehn Jul 23, 2020
57f07aa
Some clippy
rklaehn Jul 23, 2020
b6eeb3c
Prevent duplicate finding during JOIN
AgeManning Jul 24, 2020
0d5bde4
Merge #1675
AgeManning Jul 24, 2020
aec51e0
Update tests, improve logging
AgeManning Jul 24, 2020
759a5ad
Update the default d_lo constant
AgeManning Jul 24, 2020
495ffe4
Handle errors correctly
AgeManning Jul 24, 2020
1ea2dd9
Add privacy and validation options in the config
AgeManning Jul 24, 2020
8e69465
Merge branch 'master' into gossipsub-v1.1
blacktemplar Jul 24, 2020
1f73ded
A few improvements (#32)
rklaehn Jul 24, 2020
7b76c8f
Improve signing validation logic
AgeManning Jul 24, 2020
f5d32b2
Change the gossipsub rpc protocol to use bytes for message ids (#34)
rklaehn Jul 25, 2020
c3a7757
Add optional privacy and validation settings
AgeManning Jul 26, 2020
0c43ed4
Correct doc link
AgeManning Jul 27, 2020
efa40e3
Prevent invalid messages from being gossiped
AgeManning Jul 27, 2020
a514c60
Remove unvalidated messages from gossip. Reintroduce duplicate cache
AgeManning Jul 27, 2020
b257025
Send grafts when subscriptions are added to the mesh
AgeManning Jul 28, 2020
b20771b
Merge latest master
AgeManning Jul 28, 2020
cacadc0
Merge gossipsub-signing
AgeManning Jul 28, 2020
6566925
implement explicit peers
blacktemplar Jul 28, 2020
81338d5
Merge branch 'gossipsub-v1.1' into gossipsub-v1.1-explicit-peers
blacktemplar Jul 28, 2020
ecb2d8b
Fix ipfs-kad example
AgeManning Jul 28, 2020
2a830b9
Merge branch 'gossipsub-v1.1' into gossipsub-v1.1-explicit-peers
blacktemplar Jul 28, 2020
b78d771
finish merging + rustfmt
blacktemplar Jul 28, 2020
8bffa91
undo changes in ipfs-private
blacktemplar Jul 28, 2020
2f4a50a
Only add messages to memcache if not duplicates
AgeManning Jul 28, 2020
99a37bb
Merge latest signing
AgeManning Jul 28, 2020
284bf1c
Merge pull request #36 from blacktemplar/gossipsub-v1.1-explicit-peers
blacktemplar Jul 28, 2020
be7c9e2
Add mesh maintenance tests and remove excess peers from mesh
AgeManning Jul 29, 2020
e049ff7
Apply reviewers suggestions
AgeManning Jul 29, 2020
74d2779
Merge latest master
AgeManning Jul 29, 2020
81fca56
Merge latest gossipsub-signing
AgeManning Jul 29, 2020
6bac865
Wrap comments
AgeManning Jul 29, 2020
6e0a6b3
implement px and prune backoff + simplify tests for explicit peers
blacktemplar Jul 29, 2020
1257639
Merge pull request #37 from sigp/gossipsub-v1.1-prune-backoff-and-px
blacktemplar Jul 29, 2020
6c77022
improved data structure for storing backoffs + adding configurable sl…
blacktemplar Jul 30, 2020
8b17725
Merge pull request #38 from sigp/gossipsub-v1.1-prune-backoff-and-px
blacktemplar Jul 30, 2020
2518f9f
allow flood publishing (on by default)
blacktemplar Jul 30, 2020
1994c88
Merge pull request #39 from sigp/gossipsub-v1.1-flood-publishing
blacktemplar Jul 30, 2020
dbe0a84
implement adaptive gossip dissemination + complete config debug output
blacktemplar Jul 30, 2020
7155f5c
Merge pull request #40 from sigp/gossipsub-v1.1-adaptive-gossip-disse…
blacktemplar Jul 31, 2020
da34368
fix bug in backoff storage data structure
blacktemplar Jul 31, 2020
66730dd
make GossipsubConfig fields private + add getters + improve validatio…
blacktemplar Jul 31, 2020
8a0b278
implements a configurable minimum number of mesh peers that are outbound
blacktemplar Jul 31, 2020
4e1375a
Merge pull request #41 from sigp/gossipsub-v1.1-outbound-mesh-quota
blacktemplar Aug 1, 2020
94469cf
Ensure sequence number is sent
AgeManning Aug 2, 2020
60f8a1e
Merge latest master
AgeManning Aug 2, 2020
fef66dd
Maintain the debug trait
AgeManning Aug 2, 2020
799137c
use the score module in the behaviour, calling the score update metho…
blacktemplar Aug 6, 2020
efb59ac
Merge pull request #42 from sigp/gossipsub-v1.1-score-wiring
blacktemplar Aug 6, 2020
b6fbf05
implement opportunistic grafting
blacktemplar Aug 6, 2020
189ffc4
remove done TODO
blacktemplar Aug 6, 2020
f405d2a
remove done TODO
blacktemplar Aug 6, 2020
6fe395a
cargo fmt
blacktemplar Aug 6, 2020
47855d6
Merge branch 'gossipsub-v1.1' into gossip-v1.1-opportunistic-grafting
blacktemplar Aug 6, 2020
5954403
ignore grafts in unknown topics
blacktemplar Aug 6, 2020
a228f3b
Merge pull request #43 from sigp/gossip-v1.1-opportunistic-grafting
blacktemplar Aug 6, 2020
3d69944
Merge branch 'gossipsub-v1.1' into gossipsub-v1.1-spam-protection-mea…
blacktemplar Aug 6, 2020
afba384
cap the number of gossipsub retransmission for the iwants of the same…
blacktemplar Aug 6, 2020
223f0d8
cap the number of processed ihaves per peer per heartbeat, plus the m…
blacktemplar Aug 7, 2020
1ced56e
Replace lru cache with custom duplicatecache
AgeManning Aug 7, 2020
27ac942
track iwant messages we send and penalize peers not responding to them
blacktemplar Aug 7, 2020
04d3e2f
removes todo
blacktemplar Aug 7, 2020
5c52843
Improve stream management and error handling
AgeManning Aug 7, 2020
a135fc0
Merge pull request #44 from sigp/gossipsub-v1.1-spam-protection-measures
blacktemplar Aug 7, 2020
d9ee8e4
Merge remote-tracking branch 'origin/gossipsub-signing' into gossipsu…
blacktemplar Aug 7, 2020
714fee3
Merge branch 'master' into gossipsub-v1.1
blacktemplar Aug 7, 2020
7f7fd17
cargo fmt
blacktemplar Aug 7, 2020
ee7dd71
Gossipsub v1.1 check fmt (#45)
blacktemplar Aug 7, 2020
5109fe7
Merge duplicate cache
AgeManning Aug 10, 2020
b748618
Merge substream updates
AgeManning Aug 10, 2020
7bdb378
Propagate signature verification and peer kinds to behaviour (#46)
AgeManning Aug 12, 2020
83280db
treat peers that do not support the protocol as disconnected (#48)
blacktemplar Aug 12, 2020
5d93c0a
Gossipsub v1.1 various improvements (#49)
blacktemplar Aug 17, 2020
3206da5
Gossipsub 1.1 ammendments (#50)
AgeManning Aug 24, 2020
59034e2
Complete porting of go scoring tests (#51)
AgeManning Aug 25, 2020
521ecdb
Add peer blacklisting (#52)
AgeManning Aug 25, 2020
261f78e
Estimate message size (#53)
AgeManning Aug 26, 2020
f71a1bb
Message fragmentation (#54)
AgeManning Aug 27, 2020
c762d6c
Correct tests and merge latest master (#55)
AgeManning Aug 27, 2020
8ec6b66
Merge latest master
AgeManning Aug 27, 2020
4f92599
Remove github workflow
AgeManning Aug 27, 2020
618334a
Cleanup code add logs (#56)
AgeManning Aug 28, 2020
9a03f94
Further improve logging (#57)
AgeManning Aug 28, 2020
e031bce
Remove lru_time_cache_dep (#58)
AgeManning Aug 30, 2020
c7f0716
Various improvements (#60)
blacktemplar Sep 25, 2020
9da2af7
Cache published message ids (#61)
blacktemplar Oct 2, 2020
464c4da
use generic type for message data + cache message ids (#67)
blacktemplar Oct 8, 2020
88ee95e
Adds a fast message id function and an additional cache for it (#68)
blacktemplar Oct 9, 2020
238e5cf
Merge branch 'master' into gossipsub-v1.1
blacktemplar Oct 9, 2020
6ee2c5f
export FastMessageId
blacktemplar Oct 9, 2020
4c95f86
Gossipsub v1.1 bug fixes (#71)
blacktemplar Oct 14, 2020
c2c4a26
add subscription filters + some useful default filters + refactor tes…
blacktemplar Oct 14, 2020
201d533
remove support for multi-topic messages (#73)
blacktemplar Oct 20, 2020
87633a3
also consider allow self origin when using a published message ids di…
blacktemplar Oct 21, 2020
0e28cba
fix clippy warnings (#83)
blacktemplar Oct 21, 2020
9ba9b8a
Merge branch 'master' into gossipsub-v1.1
blacktemplar Oct 22, 2020
6bb58ec
Merge latest master
AgeManning Nov 3, 2020
7a316e8
Merge latest master
AgeManning Nov 6, 2020
d306455
Address reviewers comments (#88)
AgeManning Nov 8, 2020
2d1170b
implement as_ref and as_static_ref to get a &str from PeerKind (#91)
blacktemplar Nov 16, 2020
8bdc378
Merge latest master
AgeManning Nov 17, 2020
34f58fb
Update to latest master
AgeManning Nov 17, 2020
5157e70
only respond to IWANT if message got already validated (#96)
blacktemplar Nov 26, 2020
82be02d
protocols/gossipsub: Review (#93)
mxinden Dec 7, 2020
ffee445
Merge latest master
AgeManning Dec 7, 2020
23d7d04
Add optional compression to gossipsub (#100)
AgeManning Dec 16, 2020
1b5a35e
Remove feature gating from gossipsub
AgeManning Dec 16, 2020
1b19338
Merge latest master
AgeManning Dec 17, 2020
a15b246
Remove compression in favour of a more general data transform
AgeManning Dec 17, 2020
4cca4d3
Loosen trait bounds on gossipsub
AgeManning Dec 18, 2020
88ed4e5
Merge latest master
AgeManning Jan 6, 2021
f47ae78
Fix doclinks
AgeManning Jan 6, 2021
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
Prev Previous commit
Next Next commit
implement explicit peers
  • Loading branch information
blacktemplar committed Jul 28, 2020
commit 65669256c173f9eafc4c48806b4a5fdf0ac29a8b
14 changes: 11 additions & 3 deletions examples/gossipsub-chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ use async_std::{io, task};
use env_logger::{Builder, Env};
use futures::prelude::*;
use libp2p::gossipsub::protocol::MessageId;
use libp2p::gossipsub::{GossipsubEvent, GossipsubMessage, Topic};
use libp2p::gossipsub::{GossipsubEvent, GossipsubMessage, IdentTopic};
use libp2p::{gossipsub, identity, PeerId};
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
Expand All @@ -59,6 +59,7 @@ use std::{
error::Error,
task::{Context, Poll},
};
use libp2p_core::Multiaddr;

fn main() -> Result<(), Box<dyn Error>> {
Builder::from_env(Env::default().default_filter_or("info")).init();
Expand All @@ -72,7 +73,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let transport = libp2p::build_development_transport(local_key.clone())?;

// Create a Gossipsub topic
let topic = Topic::new("test-net".into());
let topic = IdentTopic::new("test-net");

// Create a Swarm to manage peers and events
let mut swarm = {
Expand All @@ -95,6 +96,13 @@ fn main() -> Result<(), Box<dyn Error>> {
// build a gossipsub network behaviour
let mut gossipsub = gossipsub::Gossipsub::new(local_key, gossipsub_config);
gossipsub.subscribe(topic.clone());
if let Some(explicit) = std::env::args().nth(2) {
let explicit = explicit.clone();
match explicit.parse() {
Ok(id) => gossipsub.add_explicit_peer(&id),
Err(err) => println!("Failed to parse explicit peer id: {:?}", err),
}
}
libp2p::Swarm::new(transport, gossipsub, local_peer_id)
};

Expand All @@ -121,7 +129,7 @@ fn main() -> Result<(), Box<dyn Error>> {
task::block_on(future::poll_fn(move |cx: &mut Context| {
loop {
match stdin.try_poll_next_unpin(cx)? {
Poll::Ready(Some(line)) => swarm.publish(&topic, line.as_bytes()),
Poll::Ready(Some(line)) => swarm.publish(topic.clone(), line.as_bytes()),
Poll::Ready(None) => panic!("Stdin closed"),
Poll::Pending => break,
};
Expand Down
4 changes: 2 additions & 2 deletions examples/ipfs-private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let transport = build_transport(local_key.clone(), psk);

// Create a Gosspipsub topic
let gossipsub_topic = gossipsub::Topic::new("chat".into());
let gossipsub_topic = gossipsub::IdentTopic::new("chat");

// We create a custom network behaviour that combines gossipsub, ping and identify.
#[derive(NetworkBehaviour)]
Expand Down Expand Up @@ -276,7 +276,7 @@ fn main() -> Result<(), Box<dyn Error>> {
loop {
match stdin.try_poll_next_unpin(cx)? {
Poll::Ready(Some(line)) => {
swarm.gossipsub.publish(&gossipsub_topic, line.as_bytes());
swarm.gossipsub.publish(gossipsub_topic.clone(), line.as_bytes());
}
Poll::Ready(None) => panic!("Stdin closed"),
Poll::Pending => break,
Expand Down
1 change: 1 addition & 0 deletions protocols/gossipsub/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ lru_time_cache = "0.10.0"
[dev-dependencies]
async-std = "1.6.2"
env_logger = "0.7.1"
futures-test = "0.3.5"
libp2p-plaintext = { path = "../plaintext" }
libp2p-yamux = { path = "../../muxers/yamux" }
quickcheck = "0.9.2"
Expand Down
150 changes: 111 additions & 39 deletions protocols/gossipsub/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,6 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

use crate::config::GossipsubConfig;
use crate::handler::GossipsubHandler;
use crate::mcache::MessageCache;
use crate::protocol::{
GossipsubControlAction, GossipsubMessage, GossipsubSubscription, GossipsubSubscriptionAction,
MessageId,
};
use crate::topic::{Hasher, Topic, TopicHash};
use futures::prelude::*;
use libp2p_core::{connection::ConnectionId, identity::Keypair, Multiaddr, PeerId};
use libp2p_swarm::{
NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, PollParameters, ProtocolsHandler,
};
use log::{debug, error, info, trace, warn};
use rand;
use rand::{seq::SliceRandom, thread_rng};
use std::{
collections::hash_map::HashMap,
collections::HashSet,
Expand All @@ -42,8 +26,26 @@ use std::{
sync::Arc,
task::{Context, Poll},
};
use std::iter::FromIterator;

use futures::prelude::*;
use log::{debug, error, info, trace, warn};
use rand;
use rand::{seq::SliceRandom, thread_rng};
use wasm_timer::{Instant, Interval};

use libp2p_core::{connection::ConnectionId, identity::Keypair, Multiaddr, PeerId};
use libp2p_swarm::{DialPeerCondition, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, PollParameters, ProtocolsHandler};

use crate::config::GossipsubConfig;
use crate::handler::GossipsubHandler;
use crate::mcache::MessageCache;
use crate::protocol::{
GossipsubControlAction, GossipsubMessage, GossipsubSubscription, GossipsubSubscriptionAction,
MessageId,
};
use crate::topic::{Hasher, Topic, TopicHash};

mod tests;

/// Network behaviour that handles the gossipsub protocol.
Expand All @@ -70,6 +72,9 @@ pub struct Gossipsub {
/// A map of all connected peers to their subscribed topics.
peer_topics: HashMap<PeerId, Vec<TopicHash>>,

///A set of all explicit peers
explicit_peers: HashSet<PeerId>,

/// Overlay network of connected peers - Maps topics to connected gossipsub peers.
mesh: HashMap<TopicHash, Vec<PeerId>>,

Expand All @@ -84,6 +89,9 @@ pub struct Gossipsub {

/// Heartbeat interval stream.
heartbeat: Interval,

/// Interval stream for checking explicit peers.
check_explicit_peers: Interval,
}

impl Gossipsub {
Expand All @@ -109,6 +117,7 @@ impl Gossipsub {
message_source_id,
topic_peers: HashMap::new(),
peer_topics: HashMap::new(),
explicit_peers: HashSet::new(),
mesh: HashMap::new(),
fanout: HashMap::new(),
fanout_last_pub: HashMap::new(),
Expand All @@ -121,6 +130,10 @@ impl Gossipsub {
Instant::now() + gs_config.heartbeat_initial_delay,
gs_config.heartbeat_interval,
),
check_explicit_peers: Interval::new_at(
Instant::now() + gs_config.heartbeat_initial_delay,
gs_config.check_explicit_peers_interval,
),
}
}

Expand Down Expand Up @@ -336,6 +349,23 @@ impl Gossipsub {
true
}

/// Adds a new peer to the list of explicitly connected peers.
pub fn add_explicit_peer(&mut self, peer_id: &PeerId) {
debug!("Adding explicit peer {:?}", peer_id);

self.explicit_peers.insert(peer_id.clone());

self.check_explicit_peer_connection(peer_id);
}

/// This removes the peer from explicitly connected peers, note that this does not disconnect
/// the peer.
pub fn remove_explicit_peer(&mut self, peer_id: &PeerId) {
debug!("Removing explicit peer {:?}", peer_id);

self.explicit_peers.remove(peer_id);
}

/// Gossipsub JOIN(topic) - adds topic peers to mesh and sends them GRAFT messages.
fn join(&mut self, topic_hash: &TopicHash) {
debug!("Running JOIN for topic: {:?}", topic_hash);
Expand All @@ -350,11 +380,14 @@ impl Gossipsub {

// check if we have mesh_n peers in fanout[topic] and add them to the mesh if we do,
// removing the fanout entry.
if let Some((_, peers)) = self.fanout.remove_entry(topic_hash) {
if let Some((_, mut peers)) = self.fanout.remove_entry(topic_hash) {
debug!(
"JOIN: Removing peers from the fanout for topic: {:?}",
topic_hash
);
//remove explicit peers
peers = peers.into_iter().filter(|p| !self.explicit_peers.contains(p)).collect();

// add up to mesh_n of them them to the mesh
// Note: These aren't randomly added, currently FIFO
let add_peers = std::cmp::min(peers.len(), self.config.mesh_n);
Expand All @@ -376,7 +409,7 @@ impl Gossipsub {
&self.topic_peers,
topic_hash,
self.config.mesh_n - added_peers.len(),
|_| true,
|p| !self.explicit_peers.contains(p),
);
added_peers.extend_from_slice(&new_peers);
// add them to the mesh
Expand Down Expand Up @@ -426,6 +459,18 @@ impl Gossipsub {
debug!("Completed LEAVE for topic: {:?}", topic_hash);
}

///Checks if the given peer is still connected and if not dials the peer again
fn check_explicit_peer_connection(&mut self, peer_id: &PeerId) {
if !self.peer_topics.contains_key(peer_id) {
//connect to peer
debug!("Connecting to explicit peer {:?}", peer_id);
self.events.push_back(NetworkBehaviourAction::DialPeer {
peer_id: peer_id.clone(),
condition: DialPeerCondition::Disconnected,
});
}
}

/// Handles an IHAVE control message. Checks our cache of messages. If the message is unknown,
/// requests it with an IWANT control message.
fn handle_ihave(&mut self, peer_id: &PeerId, ihave_msgs: Vec<(TopicHash, Vec<MessageId>)>) {
Expand Down Expand Up @@ -503,19 +548,26 @@ impl Gossipsub {
debug!("Handling GRAFT message for peer: {:?}", peer_id);

let mut to_prune_topics = HashSet::new();
for topic_hash in topics {
if let Some(peers) = self.mesh.get_mut(&topic_hash) {
// if we are subscribed, add peer to the mesh, if not already added
info!(
"GRAFT: Mesh link added for peer: {:?} in topic: {:?}",
peer_id, topic_hash
);
// ensure peer is not already added
if !peers.contains(peer_id) {
peers.push(peer_id.clone());
// we don't GRAFT to/from explicit peers; complain loudly if this happens
if self.explicit_peers.contains(peer_id) {
warn!("GRAFT: ignoring request from direct peer {:?}", peer_id);
// this is possibly a bug from non-reciprocal configuration; send a PRUNE for all topics
to_prune_topics = HashSet::from_iter(topics.into_iter());
} else {
for topic_hash in topics {
if let Some(peers) = self.mesh.get_mut(&topic_hash) {
// if we are subscribed, add peer to the mesh, if not already added
info!(
"GRAFT: Mesh link added for peer: {:?} in topic: {:?}",
peer_id, topic_hash
);
// ensure peer is not already added
if !peers.contains(peer_id) {
peers.push(peer_id.clone());
}
} else {
to_prune_topics.insert(topic_hash.clone());
}
} else {
to_prune_topics.insert(topic_hash.clone());
}
}

Expand Down Expand Up @@ -636,14 +688,16 @@ impl Gossipsub {
}

// if the mesh needs peers add the peer to the mesh
if let Some(peers) = self.mesh.get_mut(&subscription.topic_hash) {
if peers.len() < self.config.mesh_n_low {
debug!(
"SUBSCRIPTION: Adding peer {:?} to the mesh",
propagation_source,
);
if !self.explicit_peers.contains(propagation_source) {
if let Some(peers) = self.mesh.get_mut(&subscription.topic_hash) {
if peers.len() < self.config.mesh_n_low {
debug!(
"SUBSCRIPTION: Adding peer {:?} to the mesh",
propagation_source,
);
}
peers.push(propagation_source.clone());
}
peers.push(propagation_source.clone());
}
// generates a subscription event to be polled
self.events.push_back(NetworkBehaviourAction::GenerateEvent(
Expand Down Expand Up @@ -697,6 +751,7 @@ impl Gossipsub {
let mut to_prune = HashMap::new();

// maintain the mesh for each topic
let explicit_peers = &self.explicit_peers;
for (topic_hash, peers) in self.mesh.iter_mut() {
// too little peers - add some
if peers.len() < self.config.mesh_n_low {
Expand All @@ -710,7 +765,7 @@ impl Gossipsub {
let desired_peers = self.config.mesh_n - peers.len();
let peer_list =
Self::get_random_peers(&self.topic_peers, topic_hash, desired_peers, {
|peer| !peers.contains(peer)
|peer| !peers.contains(peer) && !explicit_peers.contains(peer)
});
for peer in &peer_list {
let current_topic = to_graft.entry(peer.clone()).or_insert_with(|| vec![]);
Expand Down Expand Up @@ -813,6 +868,7 @@ impl Gossipsub {

// shift the memcache
self.mcache.shift();

debug!("Completed Heartbeat");
}

Expand All @@ -831,7 +887,7 @@ impl Gossipsub {
&self.topic_peers,
&topic_hash,
self.config.gossip_lazy,
|peer| !peers.contains(peer),
|peer| !peers.contains(peer) && !self.explicit_peers.contains(peer),
);
for peer in to_msg_peers {
// send an IHAVE message
Expand Down Expand Up @@ -925,6 +981,15 @@ impl Gossipsub {
}
}

//add explicit peers
for p in &self.explicit_peers {
if let Some(topics) = self.peer_topics.get(p) {
if message.topics.iter().any(|t| topics.contains(t)) {
recipient_peers.insert(p.clone());
}
}
}

// forward the message to peers
if !recipient_peers.is_empty() {
let event = Arc::new(GossipsubRpc {
Expand Down Expand Up @@ -1193,6 +1258,13 @@ impl NetworkBehaviour for Gossipsub {
self.heartbeat();
}

//check connections to explicit peers
while let Poll::Ready(Some(())) = self.check_explicit_peers.poll_next_unpin(cx) {
for p in self.explicit_peers.clone() {
self.check_explicit_peer_connection(&p);
}
}

Poll::Pending
}
}
Expand Down
Loading