libp2p / specs

Technical specifications for the libp2p networking stack
https://libp2p.io
1.56k stars 273 forks source link

Questions regarding Gossip part in Gossipsub heartbeat #231

Closed NIC619 closed 4 years ago

NIC619 commented 4 years ago

Had a few questions regarding the Gossip part of the spec and Go implementation in Gossipsub heartbeat. It says:

Gossip is emitted by selecting peers for each topic that are not already part of the mesh:

for each topic in mesh+fanout:
  let mids be mcache.window[topic]
  if mids is not empty:
    select D peers from peers.gossipsub[topic]
    for each peer not in mesh[topic] or fanout[topic]
      emit IHAVE(mids)

shift the mcache

Q1: The pseudo code is a bit confusing to me. I interpret it as: for each topic in both mesh and fanout, get gossipssub peers from that topic excluding peers already in mesh and fanout. But in Go implementation, it's separated into two loops: mesh and fanout. In mesh loop, for each topic, we get gossipssub peers from that topic excluding peers already in mesh (but they could be in fanout) and vice versa for fanout loop. https://github.com/libp2p/go-libp2p-pubsub/blob/01b9825fbee1848751d90a8469e3f5f43bac8466/gossipsub.go#L482-L484 https://github.com/libp2p/go-libp2p-pubsub/blob/01b9825fbee1848751d90a8469e3f5f43bac8466/gossipsub.go#L520-L522

Q2: In the pseudo code, we first choose D gossipsub peers from the topic then filter our peers that's not already in mesh and fanout but in Go implementation peers are filtered before returning the first count number of peers. https://github.com/libp2p/go-libp2p-pubsub/blob/01b9825fbee1848751d90a8469e3f5f43bac8466/gossipsub.go#L674-L694

aschmahmann commented 4 years ago

Q1: You're not able to have mesh and fanout peers for the same topic simultaneously. fanout is for when we're not subscribed, mesh is for when we are subscribed.

Q2: Yes, that was a relatively recent change in the Go implementation. There is a spec PR to update the spec at https://github.com/libp2p/specs/pull/219. @vyzo is that PR ready to merge now?

@NIC619 Just a heads up that if you haven't checked out https://docs.libp2p.io/concepts/publish-subscribe/ yet that you might find it useful. It's got some really nice descriptions and breakdown of how the protocol works.

vyzo commented 4 years ago

yeah, I just merged it.

NIC619 commented 4 years ago

@aschmahmann The answers and linked document are really helpful. Thanks!