@cryptotaxi247 / kubo / commits / 8aac84464

add -r flag for dht provide

License: MIT Signed-off-by: Jeromy <why@ipfs.io>

Jeromy committed Aug 22, 2016 at 20:56 UTC 8aac844641dbf06176943a0d2fcd33030eb55824
1 file changed +59 -13
core/commands/dht.go
+59 -13
@@ -9,12 +9,15 @@ import (
9
10 key "github.com/ipfs/go-ipfs/blocks/key"
11 cmds "github.com/ipfs/go-ipfs/commands"
12 + dag "github.com/ipfs/go-ipfs/merkledag"
13 notif "github.com/ipfs/go-ipfs/notifications"
14 path "github.com/ipfs/go-ipfs/path"
15 + routing "github.com/ipfs/go-ipfs/routing"
16 ipdht "github.com/ipfs/go-ipfs/routing/dht"
17 pstore "gx/ipfs/QmQdnfvZQuhdT93LNc5bos52wAmdr3G2p6G8teLJMEN32P/go-libp2p-peerstore"
18 peer "gx/ipfs/QmRBqJF7hb8ZSpRcMwUt8hNhydWcxGEhtk81HKq6oUwKvs/go-libp2p-peer"
19 u "gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
20 + "gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
21 )
22
23 var ErrNotDHT = errors.New("routing service is not a DHT")
@@ -238,6 +241,7 @@ var provideRefDhtCmd = &cmds.Command{
241 },
242 Options: []cmds.Option{
243 cmds.BoolOption("verbose", "v", "Print extra information.").Default(false),
244 + cmds.BoolOption("recursive", "r", "Recursively provide entire graph.").Default(false),
245 },
246 Run: func(req cmds.Request, res cmds.Response) {
247 n, err := req.InvocContext().GetNode()
@@ -246,12 +250,13 @@ var provideRefDhtCmd = &cmds.Command{
250 return
251 }
252
249 - dht, ok := n.Routing.(*ipdht.IpfsDHT)
250 - if !ok {
251 - res.SetError(ErrNotDHT, cmds.ErrNormal)
253 + if n.Routing == nil {
254 + res.SetError(errNotOnline, cmds.ErrNormal)
255 return
256 }
257
258 + rec, _, _ := req.Option("recursive").Bool()
259 +
260 var keys []key.Key
261 for _, arg := range req.Arguments() {
262 k := key.B58KeyDecode(arg)
@@ -288,16 +293,11 @@ var provideRefDhtCmd = &cmds.Command{
293 }()
294
295 go func() {
291 - defer close(outChan)
292 - for _, k := range keys {
293 - err := dht.Provide(ctx, k)
294 - if err != nil {
295 - notif.PublishQueryEvent(ctx, &notif.QueryEvent{
296 - Type: notif.QueryError,
297 - Extra: err.Error(),
298 - })
299 - return
300 - }
296 + defer close(events)
297 + if rec {
298 + provideKeysRec(ctx, n.Routing, n.DAG, keys)
299 + } else {
300 + provideKeys(ctx, n.Routing, keys)
301 }
302 }()
303 },
@@ -338,6 +338,52 @@ var provideRefDhtCmd = &cmds.Command{
338 Type: notif.QueryEvent{},
339 }
340
341 +func provideKeys(ctx context.Context, r routing.IpfsRouting, keys []key.Key) {
342 + for _, k := range keys {
343 + err := r.Provide(ctx, k)
344 + if err != nil {
345 + notif.PublishQueryEvent(ctx, &notif.QueryEvent{
346 + Type: notif.QueryError,
347 + Extra: err.Error(),
348 + })
349 + return
350 + }
351 + }
352 +}
353 +
354 +func provideKeysRec(ctx context.Context, r routing.IpfsRouting, dserv dag.DAGService, keys []key.Key) {
355 + for _, k := range keys {
356 + kset := key.NewKeySet()
357 + node, err := dserv.Get(ctx, k)
358 + if err != nil {
359 + notif.PublishQueryEvent(ctx, &notif.QueryEvent{
360 + Type: notif.QueryError,
361 + Extra: err.Error(),
362 + })
363 + }
364 +
365 + err = dag.EnumerateChildrenAsync(ctx, dserv, node, kset)
366 + if err != nil {
367 + notif.PublishQueryEvent(ctx, &notif.QueryEvent{
368 + Type: notif.QueryError,
369 + Extra: err.Error(),
370 + })
371 + }
372 +
373 + for _, k := range kset.Keys() {
374 + err = r.Provide(ctx, k)
375 + if err != nil {
376 + notif.PublishQueryEvent(ctx, &notif.QueryEvent{
377 + Type: notif.QueryError,
378 + Extra: err.Error(),
379 + })
380 + return
381 + }
382 + }
383 + }
384 +
385 +}
386 +
387 var findPeerDhtCmd = &cmds.Command{
388 Helptext: cmds.HelpText{
389 Tagline: "Query the DHT for all of the multiaddresses associated with a Peer ID.",