Skip to content

Commit dfc7d1d

Browse files
committed
add discovery option, and update to floodsub 0.4.2
License: MIT Signed-off-by: Jeromy <[email protected]>
1 parent bcb20ee commit dfc7d1d

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed

core/commands/pubsub.go

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,21 @@ package commands
22

33
import (
44
"bytes"
5+
"context"
56
"encoding/binary"
67
"io"
8+
"sync"
9+
"time"
710

11+
blocks "github.com/ipfs/go-ipfs/blocks"
812
cmds "github.com/ipfs/go-ipfs/commands"
13+
core "github.com/ipfs/go-ipfs/core"
914

10-
floodsub "gx/ipfs/QmSWp1Yx7Z5pbpeCbUy6tfFj2DrHUe7tGQqyYC2vspbXH1/floodsub"
15+
floodsub "gx/ipfs/QmQtsU1T46uxjFMd5r5PfyaY1HdV5jcxZbvvHbAVRL52hc/floodsub"
1116
u "gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
17+
key "gx/ipfs/Qmce4Y4zg3sYr7xKM5UueS67vhNni6EeWgCRnb7MbLJMew/go-key"
18+
pstore "gx/ipfs/QmdMfSLMDBDYhtc4oF3NYGCZr5dy4wQb6Ji26N4D4mdxa2/go-libp2p-peerstore"
19+
cid "gx/ipfs/QmfSc2xehWmWLnwwYR91Y8QF4xdASypTFVknutoKQS3GHp/go-cid"
1220
)
1321

1422
var PubsubCmd = &cmds.Command{
@@ -41,6 +49,9 @@ to be used in a production environment.
4149
Arguments: []cmds.Argument{
4250
cmds.StringArg("topic", true, false, "String name of topic to subscribe to."),
4351
},
52+
Options: []cmds.Option{
53+
cmds.BoolOption("discover", "try to discover other peers subscribed to the same topic"),
54+
},
4455
Run: func(req cmds.Request, res cmds.Response) {
4556
n, err := req.InvocContext().GetNode()
4657
if err != nil {
@@ -79,6 +90,18 @@ to be used in a production environment.
7990
}
8091
}
8192
}()
93+
94+
discover, _, _ := req.Option("discover").Bool()
95+
if discover {
96+
blk := blocks.NewBlock([]byte("floodsub:" + topic))
97+
cid, err := n.Blocks.AddObject(blk)
98+
if err != nil {
99+
log.Error("pubsub discovery: ", err)
100+
return
101+
}
102+
103+
connectToPubSubPeers(req.Context(), n, cid)
104+
}
82105
},
83106
Marshalers: cmds.MarshalerMap{
84107
cmds.Text: getPsMsgMarshaler(func(m *floodsub.Message) (io.Reader, error) {
@@ -97,6 +120,30 @@ to be used in a production environment.
97120
Type: floodsub.Message{},
98121
}
99122

123+
func connectToPubSubPeers(ctx context.Context, n *core.IpfsNode, cid *cid.Cid) {
124+
ctx, cancel := context.WithCancel(ctx)
125+
defer cancel()
126+
127+
provs := n.Routing.FindProvidersAsync(ctx, key.Key(cid.Hash()), 10)
128+
wg := &sync.WaitGroup{}
129+
for p := range provs {
130+
wg.Add(1)
131+
go func(pi pstore.PeerInfo) {
132+
defer wg.Done()
133+
ctx, cancel := context.WithTimeout(ctx, time.Second*10)
134+
defer cancel()
135+
err := n.PeerHost.Connect(ctx, pi)
136+
if err != nil {
137+
log.Info("pubsub discover: ", err)
138+
return
139+
}
140+
log.Info("connected to pubsub peer:", pi.ID)
141+
}(p)
142+
}
143+
144+
wg.Wait()
145+
}
146+
100147
func getPsMsgMarshaler(f func(m *floodsub.Message) (io.Reader, error)) func(cmds.Response) (io.Reader, error) {
101148
return func(res cmds.Response) (io.Reader, error) {
102149
outChan, ok := res.Output().(<-chan interface{})

core/core.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ import (
1717
"time"
1818

1919
diag "github.com/ipfs/go-ipfs/diagnostics"
20+
floodsub "gx/ipfs/QmQtsU1T46uxjFMd5r5PfyaY1HdV5jcxZbvvHbAVRL52hc/floodsub"
2021
goprocess "gx/ipfs/QmSF8fPo3jgVBAy8fpdjjYqgG87dkJgUprRBHRd2tmfgpP/goprocess"
2122
mamask "gx/ipfs/QmSMZwvs3n4GBikZ7hKzT17c3bk65FmyZo2JqtJ16swqCv/multiaddr-filter"
22-
floodsub "gx/ipfs/QmSWp1Yx7Z5pbpeCbUy6tfFj2DrHUe7tGQqyYC2vspbXH1/floodsub"
2323
logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log"
2424
b58 "gx/ipfs/QmT8rehPR3F6bmwL6zjUN8XpiDBFFpMP2myPdC6ApsWfJf/go-base58"
2525
discovery "gx/ipfs/QmUuwQUJmtvC6ReYcu7xaYKEUM3pD46H18dFn3LBhVt2Di/go-libp2p/p2p/discovery"

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,9 @@
242242
},
243243
{
244244
"author": "whyrusleeping",
245-
"hash": "QmSWp1Yx7Z5pbpeCbUy6tfFj2DrHUe7tGQqyYC2vspbXH1",
245+
"hash": "QmQtsU1T46uxjFMd5r5PfyaY1HdV5jcxZbvvHbAVRL52hc",
246246
"name": "floodsub",
247-
"version": "0.4.1"
247+
"version": "0.4.2"
248248
}
249249
],
250250
"gxVersion": "0.4.0",

0 commit comments

Comments
 (0)