libp2p / libp2p/go-libp2p-pubsub

basic pubsub test - fail to make it pass

Open
#142 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Go
Stars
364
Forks
221
PR merge metrics
No merged PRs in 30d

Description

Hi, I wanted to play with pubsub a bit, with a very basic scenario:
- 3 peers all subscribe to the same topic "random"
- peer 1 is connected to 2 and peer 2 is connected to 3
- peer 1 publishes some data for the "random" topic

I would expect that peer 2 would receive the message and also transmit it to peer 3 but in my code only peer 2 does.

I fail to find the relevant missing piece of info in the library...
Is my scenario a valid one or is this expected to fail according to the specs ? And if it is a valid one, would you know why it does not work with my code below ?

I'd be happy to add lots of comments and make a PR to show such a basic example.

PS: I added the `DisableRelay` option since otherwise that does not work, as shown in the issue https://github.com/libp2p/go-libp2p-examples/issues/21

```go
package main

import (
"bytes"
"context"
"fmt"
"time"

libp2p "github.com/libp2p/go-libp2p"
host "github.com/libp2p/go-libp2p-host"
pstore "github.com/libp2p/go-libp2p-peerstore"
pubsub "github.com/libp2p/go-libp2p-pubsub"
)

const gossipSubID = "/meshsub/1.0.0"

func main() {

//golog.SetAllLoggers(gologging.DEBUG) // Change to DEBUG for extra info
h1 := newHost(2001)
h2 := newHost(2002)
h3 := newHost(2003)
fmt.Printf("host 1: \n\t-Addr:%s\n\t-ID: %s\n", h1.Addrs()[0], h1.ID().Pretty())
fmt.Printf("host 2: \n\t-Addr:%s\n\t-ID: %s\n", h2.Addrs()[0], h2.ID().Pretty())
fmt.Printf("host 3: \n\t-Addr:%s\n\t-ID: %s\n", h3.Addrs()[0], h3.ID().Pretty())

time.Sleep(100 * time.Millisecond)

// add h1 to h2's store
h2.Peerstore().AddAddr(h1.ID(), h1.Addrs()[0], pstore.PermanentAddrTTL)
// add h2 to h1's store
h1.Peerstore().AddAddr(h2.ID(), h2.Addrs()[0], pstore.PermanentAddrTTL)
// add h3 to h2's store
h2.Peerstore().AddAddr(h3.ID(), h3.Addrs()[0], pstore.PermanentAddrTTL)
// add h2 to h3's store
h3.Peerstore().AddAddr(h3.ID(), h3.Addrs()[0], pstore.PermanentAddrTTL)

// ---- gossip sub part
topic := "random"
opts := pubsub.WithMessageSigning(false)
g1, err := pubsub.NewGossipSub(context.Background(), h1, opts)
requireNil(err)
g2, err := pubsub.NewGossipSub(context.Background(), h2, opts)
requireNil(err)
g3, err := pubsub.NewGossipSub(context.Background(), h3, opts)
requireNil(err)
s2, err := g2.Subscribe(topic)
requireNil(err)
s3, err := g3.Subscribe(topic)
requireNil(err)
time.Sleep(1 * time.Second)

// 1 connect to 2 and 2 connect to 3
err = h1.Connect(context.Background(), h2.Peerstore().PeerInfo(h2.ID()))
requireNil(err)
err = h2.Connect(context.Background(), h3.Peerstore().PeerInfo(h3.ID()))
requireNil(err)

// publish and read
msg := []byte("Hello Word")
requireNil(g1.Publish(topic, msg))

pbMsg, err := s2.Next(context.Background())
requireNil(err)
checkEqual(msg, pbMsg.Data)
fmt.Println(" GOSSIPING WORKS #1")

pbMsg, err = s3.Next(context.Background())
requireNil(err)
checkEqual(msg, pbMsg.Data)
fmt.Println(" GOSSIPING WORKS #2")
}

func checkEqual(exp, rcvd []byte) {
if !bytes.Equal(exp, rcvd) {
panic("not equal")
}
}

func requireNil(err error) {
if err != nil {
panic(err)
}
}

func newHost(port int) host.Host {
opts := []libp2p.Option{
libp2p.ListenAddrStrings(fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", port)),
libp2p.DisableRelay(),
}
basicHost, err := libp2p.New(context.Background(), opts...)
if err != nil {
panic(err)
}
return basicHost
}
```

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Run the supplied three-peer Go reproduction from main, starting with newHost and the NewGossipSub setup. Check the peerstore entries, topic subscriptions, and connections between h1, h2, and h3 to determine whether forwarding from peer 2 to peer 3 is expected. Done means establishing whether the scenario is valid and documenting or fixing the cause of the missing delivery.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
distributed-systems, networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.